SystemStore.php 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602
  1. <?php
  2. namespace app\admin\controller\system;
  3. use app\admin\controller\AuthController;
  4. use app\admin\model\store\StoreCategory;
  5. use app\admin\model\system\SystemAdmin;
  6. use app\admin\model\system\SystemStoreBill;
  7. use app\admin\model\system\SystemStorePoint;
  8. use app\admin\model\system\SystemStoreProductStock;
  9. use app\admin\model\system\SystemStoreProductStockLog;
  10. use app\admin\model\system\SystemStoreSender;
  11. use crmeb\services\FormBuilder;
  12. use crmeb\services\JsonService;
  13. use crmeb\services\JsonService as Json;
  14. use app\admin\model\system\SystemStore as SystemStoreModel;
  15. use crmeb\services\UtilService;
  16. use think\db\exception\DataNotFoundException;
  17. use think\db\exception\ModelNotFoundException;
  18. use think\facade\Route;
  19. /**
  20. * 门店管理控制器
  21. * Class SystemAttachment
  22. * @package app\admin\controller\system
  23. *
  24. */
  25. class SystemStore extends AuthController
  26. {
  27. /**
  28. * 门店列表
  29. */
  30. public function list()
  31. {
  32. $where = UtilService::getMore([
  33. ['page', 1],
  34. ['limit', 20],
  35. ['name', ''],
  36. ['excel', 0],
  37. ['type', $this->request->param('type')]
  38. ]);
  39. JsonService::successlayui(SystemStoreModel::getStoreList($where));
  40. }
  41. /**
  42. * 门店设置
  43. * @return string
  44. */
  45. public function index()
  46. {
  47. $type = $this->request->param('type');
  48. $show = SystemStoreModel::where('is_show', 1)->where('is_del', 0)->count();//显示中的门店
  49. $hide = SystemStoreModel::where('is_show', 0)->count();//隐藏的门店
  50. $recycle = SystemStoreModel::where('is_del', 1)->count();//删除的门店
  51. if ($type == null) $type = 1;
  52. $this->assign(compact('type', 'show', 'hide', 'recycle'));
  53. return $this->fetch();
  54. }
  55. /**
  56. * 门店添加
  57. * @param int $id
  58. * @return string
  59. */
  60. public function add($id = 0)
  61. {
  62. $store = SystemStoreModel::getStoreDispose($id);
  63. $this->assign(compact('store'));
  64. return $this->fetch();
  65. }
  66. /**
  67. * 删除恢复门店
  68. * @param $id
  69. */
  70. public function delete($id)
  71. {
  72. if (!$id) return $this->failed('数据不存在');
  73. if (!SystemStoreModel::be(['id' => $id])) return $this->failed('产品数据不存在');
  74. if (SystemStoreModel::be(['id' => $id, 'is_del' => 1])) {
  75. $data['is_del'] = 0;
  76. if (!SystemStoreModel::edit($data, $id))
  77. return Json::fail(SystemStoreModel::getErrorInfo('恢复失败,请稍候再试!'));
  78. else
  79. return Json::successful('恢复门店成功!');
  80. } else {
  81. $data['is_del'] = 1;
  82. if (!SystemStoreModel::edit($data, $id))
  83. return Json::fail(SystemStoreModel::getErrorInfo('删除失败,请稍候再试!'));
  84. else
  85. return Json::successful('删除门店成功!');
  86. }
  87. }
  88. /**
  89. * 设置单个门店是否显示
  90. * @param string $is_show
  91. * @param string $id
  92. * @return json
  93. */
  94. public function set_show($is_show = '', $id = '')
  95. {
  96. ($is_show == '' || $id == '') && JsonService::fail('缺少参数');
  97. $res = SystemStoreModel::where(['id' => $id])->update(['is_show' => (int)$is_show]);
  98. if ($res) {
  99. return JsonService::successful($is_show == 1 ? '设置显示成功' : '设置隐藏成功');
  100. } else {
  101. return JsonService::fail($is_show == 1 ? '设置显示失败' : '设置隐藏失败');
  102. }
  103. }
  104. /**
  105. * 位置选择
  106. * @return string|void
  107. */
  108. public function select_address()
  109. {
  110. $key = sys_config('tengxun_map_key');
  111. if (!$key) return $this->failed('请前往设置->物流设置->物流配置 配置腾讯地图KEY', '#');
  112. $this->assign(compact('key'));
  113. return $this->fetch();
  114. }
  115. /**
  116. * 保存修改门店信息
  117. * @param int $id
  118. */
  119. public function save($id = 0)
  120. {
  121. $data = UtilService::postMore([
  122. ['name', ''],
  123. ['introduction', ''],
  124. ['image', ''],
  125. ['phone', ''],
  126. ['address', ''],
  127. ['detailed_address', ''],
  128. ['latlng', ''],
  129. ['valid_time', []],
  130. ['day_time', []],
  131. ['radius', 0],
  132. ['leader', ''],
  133. ['pictures', []],
  134. ['recharge_award_ratio', 0],
  135. ['terminal_number', ''],
  136. ['sh_start_money', 0],
  137. ['sh_start_distance', 0],
  138. ['sh_step', 0],
  139. ['sh_step_money', 0],
  140. ['sharing', 0],
  141. ['store_user', 0],
  142. ]);
  143. SystemStoreModel::beginTrans();
  144. try {
  145. $data['address'] = implode(',', $data['address']);
  146. $data['latlng'] = is_string($data['latlng']) ? explode(',', $data['latlng']) : $data['latlng'];
  147. if (!isset($data['latlng'][0]) || !isset($data['latlng'][1])) JsonService::fail('请选择门店位置');
  148. if ($data['sh_step'] <= 0) JsonService::fail('超过距离基数不能小于等于0');
  149. $data['latitude'] = $data['latlng'][0];
  150. $data['longitude'] = $data['latlng'][1];
  151. $data['valid_time'] = implode(' - ', $data['valid_time']);
  152. $data['day_time'] = implode(' - ', $data['day_time']);
  153. unset($data['latlng']);
  154. if ($data['image'] && strstr($data['image'], 'http') === false) {
  155. $site_url = sys_config('site_url');
  156. $data['image'] = $site_url . $data['image'];
  157. }
  158. $new = [];
  159. foreach ($data['pictures'] as $v) {
  160. $site_url = sys_config('site_url');
  161. $new[] = $site_url . $v;
  162. }
  163. $data['pictures'] = implode(',', $new);
  164. if ($id) {
  165. if (SystemStoreModel::where('id', $id)->update($data)) {
  166. SystemStoreModel::commitTrans();
  167. JsonService::success('修改成功');
  168. } else {
  169. SystemStoreModel::rollbackTrans();
  170. JsonService::fail('修改失败或者您没有修改什么!');
  171. }
  172. } else {
  173. $data['add_time'] = time();
  174. $data['is_show'] = 1;
  175. if ($res = SystemStoreModel::create($data)) {
  176. SystemAdmin::create([
  177. 'account' => 'store_admin_' . $res->id,
  178. 'pwd' => md5('123456'),
  179. 'real_name' => $data['leader'],
  180. 'roles' => sys_config('default_store_admin', 7),
  181. 'add_time' => time(),
  182. 'status' => 1,
  183. 'level' => 2,
  184. 'store_id' => $res->id,
  185. ]);
  186. SystemStoreModel::commitTrans();
  187. JsonService::success('保存成功', ['id' => $res->id]);
  188. } else {
  189. SystemStoreModel::rollbackTrans();
  190. JsonService::fail('保存失败!');
  191. }
  192. }
  193. } catch (\Exception $e) {
  194. SystemStoreModel::rollbackTrans();
  195. JsonService::fail($e->getMessage());
  196. }
  197. }
  198. /**
  199. * @param $id
  200. * @return string
  201. * @throws Exception
  202. */
  203. public function storeProductInfo($id)
  204. {
  205. $this->assign('id', $id);
  206. $this->assign('year', get_month());
  207. return $this->fetch();
  208. }
  209. public function stock_list($id)
  210. {
  211. $where = UtilService::getMore([
  212. ['page', 1],
  213. ['data', ''],
  214. ['limit', 20],
  215. ['product_id', ''],
  216. ['excel', 0],
  217. ]);
  218. JsonService::successlayui(SystemStoreProductStock::getStockList($where, $id));
  219. }
  220. public function add_stock($id)
  221. {
  222. if (!$id) {
  223. return $this->failed('请选择门店');
  224. }
  225. $where = UtilService::getMore([
  226. ['product_id', 0],
  227. ['unique', ''],
  228. ]);
  229. if (!$where['product_id'] || !$where['unique']) {
  230. $field = [];
  231. $field[] = FormBuilder::frameImages('product', '选择商品', Route::buildUrl('productList', array('fodder' => 'product')))->icon('plus')->width('100%')->height('500px');
  232. $field[] = FormBuilder::hidden('product_id', '');
  233. $field[] = FormBuilder::number('price', '价格', 0)->step(0.01);
  234. $field[] = FormBuilder::hidden('unique', '');
  235. } else {
  236. $field = [];
  237. $field[] = FormBuilder::hidden('product_id', $where['product_id']);
  238. $field[] = FormBuilder::number('price', '价格', SystemStoreProductStock::where(['product_id' => $where['product_id'], 'unique' => $where['unique'], 'store_id' => $id])->value('price'))->step(0.01);
  239. $field[] = FormBuilder::hidden('unique', $where['unique']);
  240. }
  241. $field[] = FormBuilder::number('stock', '库存增加量', 0);
  242. $form = FormBuilder::make_post_form('添加库存', $field, Route::buildUrl('save_stock', ['id' => $id]), 3);
  243. $this->assign(compact('form'));
  244. return $this->fetch('public/form-builder');
  245. }
  246. /**
  247. * @return string
  248. * @throws DataNotFoundException
  249. * @throws ModelNotFoundException
  250. * @throws Exception
  251. */
  252. public function productList()
  253. {
  254. $cate = StoreCategory::getTierList(null, 1);
  255. $this->assign('cate', $cate);
  256. return $this->fetch();
  257. }
  258. public function save_stock($id)
  259. {
  260. if (!$id) {
  261. Json::fail('请选择门店');
  262. }
  263. list($product_id, $unique, $stock, $price) = UtilService::postMore([['product_id', []], ['unique', []], ['stock', 0], ['price', 0]], $this->request, true);
  264. if (!count($product_id)) {
  265. Json::fail('请选择补货商品');
  266. }
  267. $res = true;
  268. SystemStoreProductStockLog::beginTrans();
  269. foreach ($product_id as $k => $v) {
  270. if ($stock > 0) {
  271. $res = $res && SystemStoreProductStockLog::income($id, $v, $unique[$k], 'add_stock', 0, $stock, $this->adminId, '后台补货' . $stock . '件', 1, $price);
  272. } else {
  273. $res = $res && SystemStoreProductStockLog::expend($id, $v, $unique[$k], 'dec_stock', 0, abs($stock), $this->adminId, '后台减货' . $stock . '件', 1, $price);
  274. }
  275. }
  276. if ($res) {
  277. SystemStoreProductStockLog::commitTrans();
  278. Json::success('补货成功');
  279. } else {
  280. SystemStoreProductStockLog::rollbackTrans();
  281. Json::fail(SystemStoreProductStockLog::getErrorInfo('补货失败'));
  282. }
  283. }
  284. /**
  285. * @param $id
  286. * @return string
  287. * @throws Exception
  288. */
  289. public function stock_log($id)
  290. {
  291. $this->assign('id', $id);
  292. return $this->fetch();
  293. }
  294. public function stock_log_list($id)
  295. {
  296. $where = UtilService::getMore([
  297. ['page', 1],
  298. ['limit', 20],
  299. ]);
  300. JsonService::successlayui(SystemStoreProductStockLog::getList($where, $id));
  301. }
  302. /**
  303. * @param $id
  304. * @return string
  305. * @throws Exception
  306. */
  307. public function store_bill_log($id)
  308. {
  309. $this->assign('id', $id);
  310. $this->assign('all', SystemStoreModel::get($id)['brokerage_price']);
  311. return $this->fetch();
  312. }
  313. public function store_bill_log_list($id)
  314. {
  315. $where = UtilService::getMore([
  316. ['page', 1],
  317. ['limit', 20],
  318. ]);
  319. JsonService::successlayui(SystemStoreBill::getList($where, $id));
  320. }
  321. /**
  322. * @param $id
  323. * @return string
  324. * @throws Exception
  325. */
  326. public function store_point($id)
  327. {
  328. $type = $this->request->param('type');
  329. $show = SystemStorePoint::where('store_id', $id)->where('is_show', 1)->where('is_del', 0)->count();//显示中的门店
  330. $hide = SystemStorePoint::where('store_id', $id)->where('is_show', 0)->count();//隐藏的门店
  331. $recycle = SystemStorePoint::where('store_id', $id)->where('is_del', 1)->count();//删除的门店
  332. if ($type == null) $type = 1;
  333. $this->assign(compact('type', 'show', 'hide', 'recycle', 'id'));
  334. return $this->fetch();
  335. }
  336. public function store_point_list($id)
  337. {
  338. $where = UtilService::getMore([
  339. ['page', 1],
  340. ['limit', 20],
  341. ['name', ''],
  342. ['excel', 0],
  343. ['type', $this->request->param('type')]
  344. ]);
  345. $where['store_id'] = $id;
  346. JsonService::successlayui(SystemStorePoint::getStoreList($where));
  347. }
  348. /**
  349. * 门店添加
  350. * @param int $id
  351. * @return string
  352. */
  353. public function add_point($id = 0, $store_id = 0)
  354. {
  355. $store = SystemStorePoint::getStoreDispose($id);
  356. $this->assign(compact('store', 'store_id'));
  357. return $this->fetch();
  358. }
  359. /**
  360. * 保存修改门店信息
  361. * @param int $id
  362. */
  363. public function save_point($id = 0)
  364. {
  365. $data = UtilService::postMore([
  366. ['name', ''],
  367. ['phone', ''],
  368. ['address', ''],
  369. ['detailed_address', ''],
  370. ['latlng', ''],
  371. ['valid_time', []],
  372. ['day_time', []],
  373. ['store_id', 0],
  374. ]);
  375. // var_dump($data['store_id']);
  376. if (!$data['store_id'] || !SystemStoreModel::getStoreDispose($data['store_id'])) JsonService::fail('无效门店');
  377. SystemStorePoint::beginTrans();
  378. try {
  379. $data['address'] = implode(',', $data['address']);
  380. $data['latlng'] = is_string($data['latlng']) ? explode(',', $data['latlng']) : $data['latlng'];
  381. if (!isset($data['latlng'][0]) || !isset($data['latlng'][1])) JsonService::fail('请选择门店位置');
  382. $data['latitude'] = $data['latlng'][0];
  383. $data['longitude'] = $data['latlng'][1];
  384. $data['valid_time'] = implode(' - ', $data['valid_time']);
  385. $data['day_time'] = implode(' - ', $data['day_time']);
  386. unset($data['latlng']);
  387. if ($id) {
  388. if (SystemStorePoint::where('id', $id)->update($data)) {
  389. SystemStorePoint::commitTrans();
  390. JsonService::success('修改成功');
  391. } else {
  392. SystemStorePoint::rollbackTrans();
  393. JsonService::fail('修改失败或者您没有修改什么!');
  394. }
  395. } else {
  396. $data['add_time'] = time();
  397. $data['is_show'] = 1;
  398. if ($res = SystemStorePoint::create($data)) {
  399. SystemStorePoint::commitTrans();
  400. JsonService::success('保存成功', ['id' => $res->id]);
  401. } else {
  402. SystemStorePoint::rollbackTrans();
  403. JsonService::fail('保存失败!');
  404. }
  405. }
  406. } catch (\Exception $e) {
  407. SystemStorePoint::rollbackTrans();
  408. JsonService::fail($e->getMessage());
  409. }
  410. }
  411. /**
  412. * 设置单个门店是否显示
  413. * @param string $is_show
  414. * @param string $id
  415. * @return json
  416. */
  417. public function set_point_show($is_show = '', $id = '')
  418. {
  419. ($is_show == '' || $id == '') && JsonService::fail('缺少参数');
  420. $res = SystemStorePoint::where(['id' => $id])->update(['is_show' => (int)$is_show]);
  421. if ($res) {
  422. return JsonService::successful($is_show == 1 ? '设置显示成功' : '设置隐藏成功');
  423. } else {
  424. return JsonService::fail($is_show == 1 ? '设置显示失败' : '设置隐藏失败');
  425. }
  426. }
  427. /**
  428. * 删除恢复门店
  429. * @param $id
  430. */
  431. public function point_delete($id)
  432. {
  433. if (!$id) return $this->failed('数据不存在');
  434. if (!SystemStorePoint::be(['id' => $id])) return $this->failed('数据不存在');
  435. if (SystemStorePoint::be(['id' => $id, 'is_del' => 1])) {
  436. $data['is_del'] = 0;
  437. if (!SystemStorePoint::edit($data, $id))
  438. return Json::fail(SystemStorePoint::getErrorInfo('恢复失败,请稍候再试!'));
  439. else
  440. return Json::successful('恢复门店成功!');
  441. } else {
  442. $data['is_del'] = 1;
  443. if (!SystemStorePoint::edit($data, $id))
  444. return Json::fail(SystemStorePoint::getErrorInfo('删除失败,请稍候再试!'));
  445. else
  446. return Json::successful('删除门店成功!');
  447. }
  448. }
  449. /**
  450. * @param $id
  451. * @return string
  452. * @throws Exception
  453. */
  454. public function store_sender($id)
  455. {
  456. $type = $this->request->param('type');
  457. $show = SystemStoreSender::where('store_id', $id)->where('is_show', 1)->where('is_del', 0)->count();//显示中的门店
  458. $hide = SystemStoreSender::where('store_id', $id)->where('is_show', 0)->count();//隐藏的门店
  459. $recycle = SystemStoreSender::where('store_id', $id)->where('is_del', 1)->count();//删除的门店
  460. if ($type == null) $type = 1;
  461. $this->assign(compact('type', 'show', 'hide', 'recycle', 'id'));
  462. return $this->fetch();
  463. }
  464. public function store_sender_list($id)
  465. {
  466. $where = UtilService::getMore([
  467. ['page', 1],
  468. ['limit', 20],
  469. ['name', ''],
  470. ['excel', 0],
  471. ['type', $this->request->param('type')]
  472. ]);
  473. $where['store_id'] = $id;
  474. JsonService::successlayui(SystemStoreSender::getStoreList($where));
  475. }
  476. /**
  477. * 门店添加
  478. * @param int $id
  479. * @return string
  480. */
  481. public function add_sender($id = 0, $store_id = 0)
  482. {
  483. $store = SystemStoreSender::getStoreDispose($id);
  484. $this->assign(compact('store', 'store_id'));
  485. return $this->fetch();
  486. }
  487. /**
  488. * 保存修改门店信息
  489. * @param int $id
  490. */
  491. public function save_sender($id = 0)
  492. {
  493. $data = UtilService::postMore([
  494. ['name', ''],
  495. ['phone', ''],
  496. ['store_id', 0],
  497. ]);
  498. // var_dump($data['store_id']);
  499. if (!$data['store_id'] || !SystemStoreModel::getStoreDispose($data['store_id'])) JsonService::fail('无效门店');
  500. SystemStoreSender::beginTrans();
  501. try {
  502. if ($id) {
  503. if (SystemStoreSender::where('id', $id)->update($data)) {
  504. SystemStoreSender::commitTrans();
  505. JsonService::success('修改成功');
  506. } else {
  507. SystemStoreSender::rollbackTrans();
  508. JsonService::fail('修改失败或者您没有修改什么!');
  509. }
  510. } else {
  511. $data['add_time'] = time();
  512. $data['is_show'] = 1;
  513. if ($res = SystemStoreSender::create($data)) {
  514. SystemStoreSender::commitTrans();
  515. JsonService::success('保存成功', ['id' => $res->id]);
  516. } else {
  517. SystemStoreSender::rollbackTrans();
  518. JsonService::fail('保存失败!');
  519. }
  520. }
  521. } catch (\Exception $e) {
  522. SystemStoreSender::rollbackTrans();
  523. JsonService::fail($e->getMessage());
  524. }
  525. }
  526. /**
  527. * 设置单个门店是否显示
  528. * @param string $is_show
  529. * @param string $id
  530. * @return json
  531. */
  532. public function set_sender_show($is_show = '', $id = '')
  533. {
  534. ($is_show == '' || $id == '') && JsonService::fail('缺少参数');
  535. $res = SystemStoreSender::where(['id' => $id])->update(['is_show' => (int)$is_show]);
  536. if ($res) {
  537. return JsonService::successful($is_show == 1 ? '设置显示成功' : '设置隐藏成功');
  538. } else {
  539. return JsonService::fail($is_show == 1 ? '设置显示失败' : '设置隐藏失败');
  540. }
  541. }
  542. /**
  543. * 删除恢复门店
  544. * @param $id
  545. */
  546. public function sender_delete($id)
  547. {
  548. if (!$id) return $this->failed('数据不存在');
  549. if (!SystemStoreSender::be(['id' => $id])) return $this->failed('数据不存在');
  550. if (SystemStoreSender::be(['id' => $id, 'is_del' => 1])) {
  551. $data['is_del'] = 0;
  552. if (!SystemStoreSender::edit($data, $id))
  553. return Json::fail(SystemStoreSender::getErrorInfo('恢复失败,请稍候再试!'));
  554. else
  555. return Json::successful('恢复门店成功!');
  556. } else {
  557. $data['is_del'] = 1;
  558. if (!SystemStoreSender::edit($data, $id))
  559. return Json::fail(SystemStoreSender::getErrorInfo('删除失败,请稍候再试!'));
  560. else
  561. return Json::successful('删除门店成功!');
  562. }
  563. }
  564. }