SystemStore.php 8.8 KB

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