Shop.php 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  1. <?php
  2. namespace app\api\model\service;
  3. use think\Db;
  4. use think\Model;
  5. use traits\model\SoftDelete;
  6. class Shop extends Model
  7. {
  8. use SoftDelete;
  9. // 表名
  10. protected $name = 'service_shop';
  11. // 自动写入时间戳字段
  12. protected $autoWriteTimestamp = 'int';
  13. // 定义时间戳字段名
  14. protected $createTime = 'createtime';
  15. protected $updateTime = 'updatetime';
  16. protected $deleteTime = 'deletetime';
  17. /**
  18. * 获取商户列表
  19. * @param $get
  20. * @return bool|\PDOStatement|string|\think\Collection
  21. * @throws \think\db\exception\DataNotFoundException
  22. * @throws \think\db\exception\ModelNotFoundException
  23. * @throws \think\exception\DbException
  24. */
  25. public static function searchShop($get)
  26. {
  27. extract($get);
  28. $lng = $get['lng'];
  29. $lat = $get['lat'];
  30. $orderBy = 'distance asc';
  31. $earth = 6378.137;
  32. $pi = 3.1415926535898;
  33. $where['state'] = 1;
  34. if(isset($city) && $city != '')
  35. {
  36. $where['city'] = $city;
  37. }
  38. if (isset($name) && $name != '') {
  39. $where['name|abbr'] = ['like', "%$name%"];
  40. }
  41. if(isset($category_id) && $category_id != '')
  42. {
  43. $where[] =['exp',Db::raw("FIND_IN_SET('$category_id',category_ids)")];
  44. }
  45. $list = self::where($where)
  46. ->field("id,user_id,name,abbr,logo_image,category_ids,goods_ids,city,district,address,lng,lat,city,trade_hour,(2*$earth*ASIN(SQRT(POW(SIN($pi*(".$lat."-lat)/360),2)+COS($pi*".$lat."/180)*COS(lat*$pi/180)*POW(SIN($pi*(".$lng."-lng)/360),2)))) as distance")
  47. ->order($orderBy)
  48. ->page($get['page'])
  49. ->limit(10)
  50. ->select();
  51. foreach ($list as &$value)
  52. {
  53. $value['categoryname'] = (new Category())->getCategoryList($value['category_ids']);
  54. $value['followNum'] = Follow::getCount(['follow_id'=>$value['id'],'state'=>1]);
  55. $value['commentScore'] = Comment::getCommentScore(['shop_id'=>$value['id'],'state'=>1]);
  56. $value['goodsList'] = Goods::getNewShopGoods($value['goods_ids']);
  57. }
  58. return $list;
  59. }
  60. public static function getShop($id)
  61. {
  62. return self::where(['id'=>$id,'state'=>1])->field('id,credit_code,to_shop,name,abbr,type,code,intro,logo_image,leader_mobile,leader_name,province,city,district,address,lng,lat,trade_hour')->find();
  63. }
  64. public static function getShopDetail($params)
  65. {
  66. return self::where(['id'=>$params['id'],'state'=>1])->field('id,to_shop,name,abbr,code,intro,logo_image,leader_mobile,leader_name,province,city,district,address,lng,lat,trade_hour')->find();;
  67. }
  68. public static function updateShopGoodsIds($shop_user_id,$ids)
  69. {
  70. $shop_id = self::where('user_id',$shop_user_id)->value('id');
  71. $shopGoods = Goods::where('shop_id',$shop_id)->column('id');
  72. if(!$shopGoods){
  73. return $ids;
  74. }
  75. return implode(',',$shopGoods).','.$ids;
  76. }
  77. public static function getShopTime()
  78. {
  79. $minute = date("i");
  80. $currentTime = time(); // 获取当前时间戳
  81. $nearestHour = ceil($currentTime / 3600) * 3600;
  82. $nearTime = ($minute>=0 && $minute<31)?$nearestHour:$nearestHour+1800;
  83. $data = ['id'=>'','starttime'=>$nearTime];
  84. return $data;
  85. }
  86. public static function getShopInfo($params)
  87. {
  88. $shop = self::where(['id'=>$params['id'],'state'=>1])->field('id,to_shop,name,abbr,type,code,intro,logo_image,leader_mobile,leader_name,category_ids,province,city,district,trade_hour,address,lng,lat')->find();
  89. if(!$shop)
  90. {
  91. return false;
  92. }
  93. $shop['distance'] = \addons\service\library\Common::distance($params['lng'],$params['lat'],$shop['lng'],$shop['lat']);
  94. $shop['categoryname'] = (new Category())->getCategoryList($shop['category_ids']);
  95. $shop['commentScore'] = Comment::getCommentScore(['shop_id'=>$shop['id'],'state'=>1]);
  96. return $shop;
  97. }
  98. public static function money($money, $user_id, $memo)
  99. {
  100. Db::startTrans();
  101. try {
  102. $shop = self::where('user_id',$user_id)->lock(true)->find();
  103. if ($shop && $money != 0) {
  104. $before = $shop->ensure_price;
  105. $after = function_exists('bcadd') ? bcadd($shop->ensure_price, $money, 2) : $shop->ensure_price + $money;
  106. //更新会员信息
  107. $shop->save(['ensure_price' => $after]);
  108. //写入日志
  109. EnsureLog::create(['user_id' => $user_id, 'money' => $money, 'type'=>1, 'memo' => $memo]);
  110. }
  111. Db::commit();
  112. } catch (\Exception $e) {
  113. Db::rollback();
  114. }
  115. }
  116. /**
  117. * 返回商家姓名
  118. * @param $id
  119. * @return float|mixed|string
  120. */
  121. public static function getName($id)
  122. {
  123. return self::where(['id'=>$id])->value('abbr');
  124. }
  125. public static function getSettleDay($id)
  126. {
  127. return self::where(['id'=>$id])->value('settle_day');
  128. }
  129. }