Store.php 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426
  1. <?php
  2. /**
  3. * Niushop商城系统 - 团队十年电商经验汇集巨献!
  4. * =========================================================
  5. * Copy right 2019-2029 杭州牛之云科技有限公司, 保留所有权利。
  6. * ----------------------------------------------
  7. * 官方网址: https://www.niushop.com
  8. * =========================================================
  9. */
  10. namespace app\model\store;
  11. use app\model\BaseModel;
  12. use app\model\express\Config;
  13. use think\facade\Db;
  14. use app\model\upload\Upload;
  15. /**
  16. * 门店管理
  17. */
  18. class Store extends BaseModel
  19. {
  20. /**
  21. * 添加门店
  22. * @param $data
  23. * @param array $user_data
  24. * @param int $is_store
  25. * @return array
  26. */
  27. public function addStore($data, $user_data = [], $is_store = 0)
  28. {
  29. $site_id = isset($data[ 'site_id' ]) ? $data[ 'site_id' ] : '';
  30. if ($site_id === '') {
  31. return $this->error('', 'REQUEST_SITE_ID');
  32. }
  33. // if (empty($data[ 'longitude' ]) || empty($data[ 'latitude' ])) {
  34. // return $this->error('', '门店经纬度不能为空');
  35. // }
  36. $data[ 'create_time' ] = time();
  37. model('store')->startTrans();
  38. try {
  39. $store_id = model('store')->add($data);
  40. if ($is_store == 1 && isset($user_data[ 'uid' ]) && !empty($user_data[ 'uid' ])) {
  41. // 添加门店管理员
  42. $group_id = model('cashier_auth_group')->getValue([ [ 'site_id', '=', $site_id ], [ 'keyword', '=', 'admin' ] ], 'group_id');
  43. model('user_group')->add([
  44. 'uid' => $user_data[ 'uid' ],
  45. 'site_id' => $site_id,
  46. 'store_id' => $store_id,
  47. 'group_id' => $group_id,
  48. 'create_time' => time(),
  49. 'app_module' => 'store'
  50. ]);
  51. }
  52. //执行事件
  53. event("AddStore", [ 'store_id' => $store_id, 'site_id' => $data[ 'site_id' ] ]);
  54. model('store')->commit();
  55. return $this->success($store_id);
  56. } catch (\Exception $e) {
  57. model('store')->rollback();
  58. return $this->error('', $e->getMessage());
  59. }
  60. }
  61. /**
  62. * 修改门店
  63. * @param $data
  64. * @param $condition
  65. * @param array $user_data
  66. * @param int $is_exit
  67. * @param int $user_type
  68. * @return array
  69. */
  70. public function editStore($data, $condition, $user_data = [], $is_exit = 0, $user_type = 1)
  71. {
  72. // if (( isset($data[ 'longitude' ]) && empty($data[ 'longitude' ]) ) || ( isset($data[ 'latitude' ]) && empty($data[ 'latitude' ]) )) {
  73. // return $this->error('', '门店经纬度不能为空');
  74. // }
  75. $check_condition = array_column($condition, 2, 0);
  76. $site_id = isset($check_condition[ 'site_id' ]) ? $check_condition[ 'site_id' ] : '';
  77. $store_id = isset($check_condition[ 'store_id' ]) ? $check_condition[ 'store_id' ] : '';
  78. if ($site_id === '') {
  79. return $this->error('', 'REQUEST_SITE_ID');
  80. }
  81. $data[ "modify_time" ] = time();
  82. model('store')->startTrans();
  83. try {
  84. $store_info = model('store')->getInfo($condition);
  85. if ($store_info[ 'store_image' ] && !empty($data[ 'store_image' ]) && $store_info[ 'store_image' ] != $data[ 'store_image' ]) {
  86. $upload_model = new Upload();
  87. $upload_model->deletePic($store_info[ 'store_image' ], $site_id);
  88. }
  89. model('store')->update($data, $condition);
  90. //可能会关闭门店自提方式
  91. $this->checkCloseStoreTrade($site_id);
  92. model('store')->commit();
  93. return $this->success($store_id);
  94. } catch (\Exception $e) {
  95. model('store')->rollback();
  96. return $this->error('', $e->getMessage());
  97. }
  98. }
  99. /**
  100. * 删除门店
  101. * @param array $condition
  102. */
  103. public function deleteStore($condition)
  104. {
  105. $check_condition = array_column($condition, 2, 0);
  106. $site_id = isset($check_condition[ 'site_id' ]) ? $check_condition[ 'site_id' ] : '';
  107. $store_id = isset($check_condition[ 'store_id' ]) ? $check_condition[ 'store_id' ] : '';
  108. if ($site_id === '') {
  109. return $this->error('', 'REQUEST_SITE_ID');
  110. }
  111. $store_info = model('store')->getInfo([ [ 'store_id', '=', $store_id ] ], 'uid, store_image');
  112. if (!empty($store_info[ 'store_image' ])) {
  113. $upload_model = new Upload();
  114. $upload_model->deletePic($store_info[ 'store_image' ], $site_id);
  115. }
  116. $res = model('store')->delete($condition);
  117. if ($res) {
  118. model('store_goods')->delete([ [ 'store_id', '=', $store_id ] ]);
  119. model('store_goods_sku')->delete([ [ 'store_id', '=', $store_id ] ]);
  120. model('store_member')->delete([ [ 'store_id', '=', $store_id ] ]);
  121. model('store_settlement')->delete([ [ 'store_id', '=', $store_id ], [ 'site_id', '=', $site_id ] ]);
  122. model('user')->delete([ [ 'app_module', '=', 'store' ], [ 'site_id', '=', $site_id ], [ 'uid', '=', $store_info[ 'uid' ] ] ]);
  123. model('site_diy_view')->delete([ [ 'name', '=', 'DIY_STORE_' . $store_id ], [ 'site_id', '=', $site_id ] ]);
  124. }
  125. //可能会关闭门店自提方式
  126. $this->checkCloseStoreTrade($site_id);
  127. return $this->success($res);
  128. }
  129. /**
  130. * 获取门店数量
  131. * @param $where
  132. * @param $field
  133. * @return array
  134. */
  135. public function getStoreCount($where, $field = 'store_id')
  136. {
  137. $res = model('store')->getCount($where, $field);
  138. return $this->success($res);
  139. }
  140. /**
  141. * 获取门店字段和
  142. * @param $where
  143. * @param $field
  144. * @return array
  145. */
  146. public function getStoreSum($where, $field)
  147. {
  148. $res = model('store')->getSum($where, $field);
  149. return $this->success($res);
  150. }
  151. /**
  152. * @param $condition
  153. * @param $is_frozen
  154. */
  155. public function frozenStore($condition, $is_frozen)
  156. {
  157. $check_condition = array_column($condition, 2, 0);
  158. $site_id = isset($check_condition[ 'site_id' ]) ? $check_condition[ 'site_id' ] : '';
  159. if ($site_id === '') {
  160. return $this->error('', 'REQUEST_SITE_ID');
  161. }
  162. $res = model('store')->update([ 'is_frozen' => $is_frozen == 1 ? 0 : 1 ], $condition);
  163. //可能会关闭门店自提方式
  164. $this->checkCloseStoreTrade($site_id);
  165. return $this->success($res);
  166. }
  167. /**
  168. * 重置密码
  169. * @param string $password
  170. * @param $condition
  171. * @return array
  172. */
  173. public function resetStorePassword($password = '123456', $condition = [])
  174. {
  175. //获取用户id
  176. $uid = model('store')->getValue($condition, 'uid');
  177. if ($uid) {
  178. $res = model('user')->update([
  179. 'password' => data_md5($password)
  180. ], [ [ 'uid', '=', $uid ] ]);
  181. } else {
  182. $res = 1;
  183. }
  184. if ($res === false) {
  185. return $this->error('', 'RESULT_ERROR');
  186. }
  187. return $this->success($res);
  188. }
  189. /**
  190. * 获取门店信息
  191. * @param array $condition
  192. * @param string $field
  193. */
  194. public function getStoreInfo($condition, $field = '*')
  195. {
  196. $res = model('store')->getInfo($condition, $field);
  197. return $this->success($res);
  198. }
  199. /**
  200. * 获取门店详情
  201. * @param $condition
  202. */
  203. public function getStoreDetail($condition)
  204. {
  205. $res = model('store')->getInfo($condition, '*');
  206. if (!empty($res)) {
  207. if (!empty($res[ 'time_week' ])) {
  208. $res[ 'time_week' ] = explode(',', $res[ 'time_week' ]);
  209. }
  210. if (empty($res[ 'delivery_time' ])) {
  211. $res[ 'delivery_time' ] = [
  212. [ 'start_time' => $res[ 'start_time' ], 'end_time' => $res[ 'end_time' ] ]
  213. ];
  214. } else {
  215. $res[ 'delivery_time' ] = json_decode($res[ 'delivery_time' ], true);
  216. }
  217. }
  218. return $this->success($res);
  219. }
  220. /**
  221. * 获取门店列表
  222. * @param array $condition
  223. * @param string $field
  224. * @param string $order
  225. * @param string $limit
  226. */
  227. public function getStoreList($condition = [], $field = '*', $order = '', $limit = null)
  228. {
  229. $list = model('store')->getList($condition, $field, $order, '', '', '', $limit);
  230. return $this->success($list);
  231. }
  232. /**
  233. * 获取门店分页列表
  234. * @param array $condition
  235. * @param number $page
  236. * @param string $page_size
  237. * @param string $order
  238. * @param string $field
  239. */
  240. public function getStorePageList($condition = [], $page = 1, $page_size = PAGE_LIST_ROWS, $order = '', $field = '*')
  241. {
  242. $list = model('store')->pageList($condition, $field, $order, $page, $page_size);
  243. return $this->success($list);
  244. }
  245. /**
  246. * 查询门店 带有距离
  247. * @param $condition
  248. * @param $lnglat
  249. */
  250. public function getLocationStoreList($condition, $field, $lnglat)
  251. {
  252. $order = '';
  253. if (!empty($lnglat[ 'lat' ]) && !empty($lnglat[ 'lng' ])) {
  254. $field .= ' , ROUND(st_distance ( point ( ' . $lnglat[ 'lng' ] . ', ' . $lnglat[ 'lat' ] . ' ), point ( longitude, latitude ) ) * 111195 / 1000, 2) as distance ';
  255. $condition[] = [ '', 'exp', Db::raw(' FORMAT(st_distance ( point ( ' . $lnglat[ 'lng' ] . ', ' . $lnglat[ 'lat' ] . ' ), point ( longitude, latitude ) ) * 111195 / 1000, 2) < 10000') ];
  256. $order = 'distance asc';
  257. }
  258. $list = model('store')->getList($condition, $field, $order);
  259. return $this->success($list);
  260. }
  261. /**
  262. * 查询门店 带有距离
  263. * @param $condition
  264. * @param $lnglat
  265. */
  266. public function getLocationStorePageList($condition, $page, $page_size, $field, $lnglat)
  267. {
  268. $order = '';
  269. if (!empty($lnglat[ 'lat' ]) && !empty($lnglat[ 'lng' ])) {
  270. $field .= ',FORMAT(st_distance ( point ( ' . $lnglat[ 'lng' ] . ', ' . $lnglat[ 'lat' ] . ' ), point ( longitude, latitude ) ) * 111195 / 1000, 2) as distance';
  271. $condition[] = [ '', 'exp', Db::raw(' FORMAT(st_distance ( point ( ' . $lnglat[ 'lng' ] . ', ' . $lnglat[ 'lat' ] . ' ), point ( longitude, latitude ) ) * 111195 / 1000, 2) < 10000') ];
  272. $order = Db::raw(' st_distance ( point ( ' . $lnglat[ 'lng' ] . ', ' . $lnglat[ 'lat' ] . ' ), point ( longitude, latitude ) ) * 111195 / 1000 asc');
  273. }
  274. $list = model('store')->pageList($condition, $field, $order, $page, $page_size);
  275. return $this->success($list);
  276. }
  277. /**
  278. * 核验是否可以关闭门店自提
  279. * @param $site_id
  280. */
  281. public function checkCloseStoreTrade($site_id)
  282. {
  283. $count = model('store')->getCount([ [ 'site_id', '=', $site_id ], [ 'is_pickup', '=', 1 ], [ 'status', '=', 1 ], [ 'is_frozen', '=', 0 ] ]);
  284. if ($count == 0) {
  285. //站点的所有门店都被删除后,门店开关也会被关闭
  286. $config_model = new Config();
  287. $config_model->setStoreIsuse(0, $site_id);
  288. }
  289. return $this->success();
  290. }
  291. /**
  292. * 核验是否可以开启门店自提
  293. * @param $site_id
  294. */
  295. public function checkIscanStoreTrade($site_id)
  296. {
  297. $count = model('store')->getCount([ [ 'site_id', '=', $site_id ], [ 'is_pickup', '=', 1 ], [ 'status', '=', 1 ], [ 'is_frozen', '=', 0 ] ]);
  298. if ($count == 0) {
  299. return $this->error('', '需至少存在一个营业中且开启自提业务的门店,才能开启门店自提开关');
  300. } else {
  301. return $this->success();
  302. }
  303. }
  304. /**
  305. * 获取门店名称(鉴于调用场景过多,封装一个只返回门店名称的函数,todo 做缓存)
  306. * @param $condition
  307. */
  308. public function getStoreName($condition)
  309. {
  310. $name = model('store')->getValue($condition, 'store_name');
  311. return $this->success($name);
  312. }
  313. /**
  314. * 获取默认门店
  315. * @param int $site_id 只有单商户这么写
  316. * @param string $field
  317. * @return array
  318. */
  319. public function getDefaultStore($site_id = 0, $field = '*')
  320. {
  321. $condition = array (
  322. [ 'is_default', '=', 1 ]
  323. );
  324. if ($site_id > 0) {
  325. $condition[] = [ 'site_id', '=', $site_id ];
  326. }
  327. $info = model('store')->getInfo($condition, $field);
  328. return $this->success($info);
  329. }
  330. /**
  331. * 填写店铺默认门店
  332. * @param $params
  333. */
  334. public function addDefaultStore($params)
  335. {
  336. $site_id = $params[ 'site_id' ] ?? 1;
  337. $data = array (
  338. 'site_id' => $site_id,
  339. 'store_name' => '默认门店',
  340. 'is_default' => 1,
  341. 'create_time' => time()
  342. );
  343. $res = model('store')->add($data);
  344. return $this->success();
  345. }
  346. /**
  347. * 获取门店类型
  348. * @param string $type
  349. * @return array
  350. */
  351. public function getStoreType($type = '')
  352. {
  353. $store_type = [
  354. 'directsale' => [
  355. 'type' => 'directsale',
  356. 'name' => '直营店'
  357. ],
  358. 'franchise' => [
  359. 'type' => 'franchise',
  360. 'name' => '加盟店'
  361. ]
  362. ];
  363. return $type ? $store_type[ $type ] : $store_type;
  364. }
  365. /**
  366. * 获取扣除库存门店
  367. * @param $params
  368. * @return array
  369. */
  370. public function getStoreStockTypeStoreId($params)
  371. {
  372. $store_id = $params[ 'store_id' ];
  373. $store_condition = array (
  374. [ 'store_id', '=', $store_id ]
  375. );
  376. $store_info = $this->getStoreInfo($store_condition)[ 'data' ] ?? [];
  377. if (empty($store_info)) {
  378. return $this->error();
  379. }
  380. $stock_type = $store_info[ 'stock_type' ];
  381. if ($stock_type == 'all') {
  382. $default_store_info = $this->getDefaultStore()[ 'data' ] ?? [];
  383. $store_id = $default_store_info[ 'store_id' ];
  384. }
  385. return $this->success($store_id);
  386. }
  387. }