SystemStore.php 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239
  1. <?php
  2. namespace app\models\system;
  3. use app\admin\model\system\SystemStorePoint;
  4. use app\admin\model\system\SystemStoreProductStockLog;
  5. use app\models\store\StoreOrderCartInfo;
  6. use app\models\user\User;
  7. use crmeb\traits\ModelTrait;
  8. use crmeb\basic\BaseModel;
  9. use think\db\exception\DataNotFoundException;
  10. use think\db\exception\DbException;
  11. use think\db\exception\ModelNotFoundException;
  12. /**
  13. * 门店自提 model
  14. * Class SystemStore
  15. * @package app\model\system
  16. */
  17. class SystemStore extends BaseModel
  18. {
  19. const EARTH_RADIUS = 6371;
  20. use ModelTrait;
  21. /**
  22. * 数据表主键
  23. * @var string
  24. */
  25. protected $pk = 'id';
  26. /**
  27. * 模型名称
  28. * @var string
  29. */
  30. protected $name = 'system_store';
  31. public static function getLatlngAttr($value, $data)
  32. {
  33. return $data['latitude'] . ',' . $data['longitude'];
  34. }
  35. public static function verificWhere()
  36. {
  37. return self::where('is_show', 1)->where('is_del', 0);
  38. }
  39. /**
  40. * 获取门店信息
  41. * @param int $id
  42. * @param string $felid
  43. * @return array|mixed|null|string|\think\Model
  44. * @throws \think\db\exception\DataNotFoundException
  45. * @throws \think\db\exception\DbException
  46. * @throws \think\db\exception\ModelNotFoundException
  47. */
  48. public static function getStoreDispose($id = 0, $felid = '')
  49. {
  50. if ($id)
  51. $storeInfo = self::verificWhere()->where('id', $id)->find();
  52. else
  53. $storeInfo = self::verificWhere()->find();
  54. if ($storeInfo) {
  55. $storeInfo['latlng'] = self::getLatlngAttr(null, $storeInfo);
  56. $storeInfo['valid_time'] = $storeInfo['valid_time'] ? explode(' - ', $storeInfo['valid_time']) : [];
  57. $storeInfo['_valid_time'] = str_replace('-', '/', ($storeInfo['valid_time'][0] ?? '') . ' ~ ' . ($storeInfo['valid_time'][1] ?? ""));
  58. $storeInfo['day_time'] = $storeInfo['day_time'] ? str_replace(' - ', ' ~ ', $storeInfo['day_time']) : [];
  59. $storeInfo['_detailed_address'] = $storeInfo['address'] . ' ' . $storeInfo['detailed_address'];
  60. $storeInfo['address'] = $storeInfo['address'] ? explode(',', $storeInfo['address']) : [];
  61. if ($felid) return $storeInfo[$felid] ?? '';
  62. }
  63. return $storeInfo;
  64. }
  65. /**
  66. * 门店列表
  67. * @return mixed
  68. */
  69. // public static function lst()
  70. // {
  71. // $model = new self;
  72. // $model = $model->where('is_show', 1);
  73. // $model = $model->where('is_del', 0);
  74. // $model = $model->order('id DESC');
  75. // return $model->select();
  76. // }
  77. /**
  78. * 计算某个经纬度的周围某段距离的正方形的四个点
  79. *
  80. * @param lng float 经度
  81. * @param lat float 纬度
  82. * @param distance float 该点所在圆的半径,该圆与此正方形内切,默认值为2.5千米
  83. * @return array 正方形的四个点的经纬度坐标
  84. */
  85. public static function returnSquarePoint($lng, $lat, $distance = 200)
  86. {
  87. $dlng = 2 * asin(sin($distance / (2 * self::EARTH_RADIUS)) / cos(deg2rad($lat)));
  88. $dlng = rad2deg($dlng);
  89. $dlat = rad2deg($distance / self::EARTH_RADIUS);
  90. return [
  91. 'left_top' => [
  92. 'lat' => $lat + $dlat,
  93. 'lng' => $lng - $dlng,
  94. ],
  95. 'right_top' => [
  96. 'lat' => $lat + $dlat,
  97. 'lng' => $lng + $dlng
  98. ],
  99. 'left_bottom' => [
  100. 'lat' => $lat - $dlat,
  101. 'lng' => $lng - $dlng
  102. ],
  103. 'right_bottom' => [
  104. 'lat' => $lat - $dlat,
  105. 'lng' => $lng + $dlng
  106. ]
  107. ];
  108. }
  109. /*
  110. 设置where条件
  111. */
  112. public static function nearbyWhere($model = null, $latitude = 0, $longitude = 0)
  113. {
  114. if (!is_object($model)) {
  115. $latitude = $model;
  116. $model = new self();
  117. $longitude = $latitude;
  118. }
  119. $field = "(round(6367000 * 2 * asin(sqrt(pow(sin(((latitude * pi()) / 180 - ({$latitude} * pi()) / 180) / 2), 2) + cos(({$latitude} * pi()) / 180) * cos((latitude * pi()) / 180) * pow(sin(((longitude * pi()) / 180 - ({$longitude} * pi()) / 180) / 2), 2))))) AS distance";
  120. $model->field($field);
  121. return $model;
  122. }
  123. /**
  124. * 获取排序sql
  125. * @param $latitude
  126. * @param $longitude
  127. * @return mixed
  128. */
  129. public static function distanceSql($latitude, $longitude)
  130. {
  131. $field = "(round(6367000 * 2 * asin(sqrt(pow(sin(((latitude * pi()) / 180 - ({$latitude} * pi()) / 180) / 2), 2) + cos(({$latitude} * pi()) / 180) * cos((latitude * pi()) / 180) * pow(sin(((longitude * pi()) / 180 - ({$longitude} * pi()) / 180) / 2), 2))))) AS distance";
  132. return $field;
  133. }
  134. /**
  135. * 门店列表
  136. * @return mixed
  137. */
  138. public static function lst($latitude, $longitude, $page, $limit)
  139. {
  140. $model = new self();
  141. $model = $model->where('is_del', 0);
  142. $model = $model->where('is_show', 1);
  143. if ($latitude && $longitude) {
  144. $model = $model->field(['*', self::distanceSql($latitude, $longitude)])->order('distance asc');
  145. }
  146. $list = $model->page((int)$page, (int)$limit)
  147. ->select()
  148. ->hidden(['is_show', 'is_del'])
  149. ->toArray();
  150. if ($latitude && $longitude) {
  151. foreach ($list as &$value) {
  152. //计算距离
  153. $value['distance'] = sqrt((pow((($latitude - $value['latitude']) * 111000), 2)) + (pow((($longitude - $value['longitude']) * 111000), 2)));
  154. //转换单位
  155. $value['range'] = bcdiv($value['distance'], 1000, 1);
  156. }
  157. // $distanceKey = array_column($list, 'distance');
  158. // array_multisort($distanceKey, SORT_ASC, $list);
  159. }
  160. return $list;
  161. }
  162. /**
  163. * 门店列表
  164. * @return mixed
  165. */
  166. public static function point_lst($latitude, $longitude, $page, $limit, $store_id)
  167. {
  168. $model = new SystemStorePoint();
  169. $model = $model->where('is_del', 0);
  170. $model = $model->where('store_id', $store_id);
  171. $model = $model->where('is_show', 1);
  172. if ($latitude && $longitude) {
  173. $model = $model->field(['*', self::distanceSql($latitude, $longitude)])->order('distance asc');
  174. }
  175. $list = $model->page((int)$page, (int)$limit)
  176. ->select()
  177. ->hidden(['is_show', 'is_del'])
  178. ->toArray();
  179. if ($latitude && $longitude) {
  180. foreach ($list as &$value) {
  181. //计算距离
  182. $value['distance'] = sqrt((pow((($latitude - $value['latitude']) * 111000), 2)) + (pow((($longitude - $value['longitude']) * 111000), 2)));
  183. //转换单位
  184. $value['range'] = bcdiv($value['distance'], 1000, 1);
  185. }
  186. // $distanceKey = array_column($list, 'distance');
  187. // array_multisort($distanceKey, SORT_ASC, $list);
  188. }
  189. return $list;
  190. }
  191. /**
  192. * @param $orderInfo
  193. * @return bool
  194. * @throws DataNotFoundException
  195. * @throws DbException
  196. * @throws ModelNotFoundException
  197. */
  198. public static function decStock($orderInfo)
  199. {
  200. $res = true;
  201. if (!$orderInfo['store_id']) return $res;
  202. // var_dump($orderInfo);
  203. $cartId = is_string($orderInfo['cart_id']) ? json_decode($orderInfo['cart_id'], true) : $orderInfo['cart_id'];
  204. $cartInfo = StoreOrderCartInfo::whereIn('cart_id', $cartId)->column('cart_info');
  205. // dump($cartId);
  206. // exit;
  207. $userInfo = User::getUserInfo($orderInfo['uid']);
  208. foreach ($cartInfo as $value) {
  209. $product = json_decode($value, true);
  210. // var_dump($product);
  211. // exit;
  212. $cartNum = $product['cart_num'] ?? 0;
  213. if (isset($product['productInfo'])) {
  214. $res = $res && SystemStoreProductStockLog::expend($orderInfo['store_id'], $product['product_id'], $product['product_attr_unique'], 'sale', $orderInfo['id'], $cartNum, 0, '用户' . $userInfo['nickname'] . '购买订单完成');
  215. }
  216. }
  217. // var_dump($res);
  218. return $res ?: self::setErrorInfo(SystemStoreProductStockLog::getErrorInfo());
  219. }
  220. }