SystemStore.php 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601
  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. ]);
  142. SystemStoreModel::beginTrans();
  143. try {
  144. $data['address'] = implode(',', $data['address']);
  145. $data['latlng'] = is_string($data['latlng']) ? explode(',', $data['latlng']) : $data['latlng'];
  146. if (!isset($data['latlng'][0]) || !isset($data['latlng'][1])) JsonService::fail('请选择门店位置');
  147. if ($data['sh_step'] <= 0) JsonService::fail('超过距离基数不能小于等于0');
  148. $data['latitude'] = $data['latlng'][0];
  149. $data['longitude'] = $data['latlng'][1];
  150. $data['valid_time'] = implode(' - ', $data['valid_time']);
  151. $data['day_time'] = implode(' - ', $data['day_time']);
  152. unset($data['latlng']);
  153. if ($data['image'] && strstr($data['image'], 'http') === false) {
  154. $site_url = sys_config('site_url');
  155. $data['image'] = $site_url . $data['image'];
  156. }
  157. $new = [];
  158. foreach ($data['pictures'] as $v) {
  159. $site_url = sys_config('site_url');
  160. $new[] = $site_url . $v;
  161. }
  162. $data['pictures'] = implode(',', $new);
  163. if ($id) {
  164. if (SystemStoreModel::where('id', $id)->update($data)) {
  165. SystemStoreModel::commitTrans();
  166. JsonService::success('修改成功');
  167. } else {
  168. SystemStoreModel::rollbackTrans();
  169. JsonService::fail('修改失败或者您没有修改什么!');
  170. }
  171. } else {
  172. $data['add_time'] = time();
  173. $data['is_show'] = 1;
  174. if ($res = SystemStoreModel::create($data)) {
  175. SystemAdmin::create([
  176. 'account' => 'store_admin_' . $res->id,
  177. 'pwd' => md5('123456'),
  178. 'real_name' => $data['leader'],
  179. 'roles' => sys_config('default_store_admin', 7),
  180. 'add_time' => time(),
  181. 'status' => 1,
  182. 'level' => 2,
  183. 'store_id' => $res->id,
  184. ]);
  185. SystemStoreModel::commitTrans();
  186. JsonService::success('保存成功', ['id' => $res->id]);
  187. } else {
  188. SystemStoreModel::rollbackTrans();
  189. JsonService::fail('保存失败!');
  190. }
  191. }
  192. } catch (\Exception $e) {
  193. SystemStoreModel::rollbackTrans();
  194. JsonService::fail($e->getMessage());
  195. }
  196. }
  197. /**
  198. * @param $id
  199. * @return string
  200. * @throws Exception
  201. */
  202. public function storeProductInfo($id)
  203. {
  204. $this->assign('id', $id);
  205. $this->assign('year', get_month());
  206. return $this->fetch();
  207. }
  208. public function stock_list($id)
  209. {
  210. $where = UtilService::getMore([
  211. ['page', 1],
  212. ['data', ''],
  213. ['limit', 20],
  214. ['product_id', ''],
  215. ['excel', 0],
  216. ]);
  217. JsonService::successlayui(SystemStoreProductStock::getStockList($where, $id));
  218. }
  219. public function add_stock($id)
  220. {
  221. if (!$id) {
  222. return $this->failed('请选择门店');
  223. }
  224. $where = UtilService::getMore([
  225. ['product_id', 0],
  226. ['unique', ''],
  227. ]);
  228. if (!$where['product_id'] || !$where['unique']) {
  229. $field = [];
  230. $field[] = FormBuilder::frameImages('product', '选择商品', Route::buildUrl('productList', array('fodder' => 'product')))->icon('plus')->width('100%')->height('500px');
  231. $field[] = FormBuilder::hidden('product_id', '');
  232. $field[] = FormBuilder::number('price', '价格', 0)->step(0.01);
  233. $field[] = FormBuilder::hidden('unique', '');
  234. } else {
  235. $field = [];
  236. $field[] = FormBuilder::hidden('product_id', $where['product_id']);
  237. $field[] = FormBuilder::number('price', '价格', SystemStoreProductStock::where(['product_id' => $where['product_id'], 'unique' => $where['unique'], 'store_id' => $id])->value('price'))->step(0.01);
  238. $field[] = FormBuilder::hidden('unique', $where['unique']);
  239. }
  240. $field[] = FormBuilder::number('stock', '库存增加量', 0);
  241. $form = FormBuilder::make_post_form('添加库存', $field, Route::buildUrl('save_stock', ['id' => $id]), 3);
  242. $this->assign(compact('form'));
  243. return $this->fetch('public/form-builder');
  244. }
  245. /**
  246. * @return string
  247. * @throws DataNotFoundException
  248. * @throws ModelNotFoundException
  249. * @throws Exception
  250. */
  251. public function productList()
  252. {
  253. $cate = StoreCategory::getTierList(null, 1);
  254. $this->assign('cate', $cate);
  255. return $this->fetch();
  256. }
  257. public function save_stock($id)
  258. {
  259. if (!$id) {
  260. Json::fail('请选择门店');
  261. }
  262. list($product_id, $unique, $stock, $price) = UtilService::postMore([['product_id', []], ['unique', []], ['stock', 0], ['price', 0]], $this->request, true);
  263. if (!count($product_id)) {
  264. Json::fail('请选择补货商品');
  265. }
  266. $res = true;
  267. SystemStoreProductStockLog::beginTrans();
  268. foreach ($product_id as $k => $v) {
  269. if ($stock > 0) {
  270. $res = $res && SystemStoreProductStockLog::income($id, $v, $unique[$k], 'add_stock', 0, $stock, $this->adminId, '后台补货' . $stock . '件', 1, $price);
  271. } else {
  272. $res = $res && SystemStoreProductStockLog::expend($id, $v, $unique[$k], 'dec_stock', 0, abs($stock), $this->adminId, '后台减货' . $stock . '件', 1, $price);
  273. }
  274. }
  275. if ($res) {
  276. SystemStoreProductStockLog::commitTrans();
  277. Json::success('补货成功');
  278. } else {
  279. SystemStoreProductStockLog::rollbackTrans();
  280. Json::fail(SystemStoreProductStockLog::getErrorInfo('补货失败'));
  281. }
  282. }
  283. /**
  284. * @param $id
  285. * @return string
  286. * @throws Exception
  287. */
  288. public function stock_log($id)
  289. {
  290. $this->assign('id', $id);
  291. return $this->fetch();
  292. }
  293. public function stock_log_list($id)
  294. {
  295. $where = UtilService::getMore([
  296. ['page', 1],
  297. ['limit', 20],
  298. ]);
  299. JsonService::successlayui(SystemStoreProductStockLog::getList($where, $id));
  300. }
  301. /**
  302. * @param $id
  303. * @return string
  304. * @throws Exception
  305. */
  306. public function store_bill_log($id)
  307. {
  308. $this->assign('id', $id);
  309. $this->assign('all', SystemStoreModel::get($id)['brokerage_price']);
  310. return $this->fetch();
  311. }
  312. public function store_bill_log_list($id)
  313. {
  314. $where = UtilService::getMore([
  315. ['page', 1],
  316. ['limit', 20],
  317. ]);
  318. JsonService::successlayui(SystemStoreBill::getList($where, $id));
  319. }
  320. /**
  321. * @param $id
  322. * @return string
  323. * @throws Exception
  324. */
  325. public function store_point($id)
  326. {
  327. $type = $this->request->param('type');
  328. $show = SystemStorePoint::where('store_id', $id)->where('is_show', 1)->where('is_del', 0)->count();//显示中的门店
  329. $hide = SystemStorePoint::where('store_id', $id)->where('is_show', 0)->count();//隐藏的门店
  330. $recycle = SystemStorePoint::where('store_id', $id)->where('is_del', 1)->count();//删除的门店
  331. if ($type == null) $type = 1;
  332. $this->assign(compact('type', 'show', 'hide', 'recycle', 'id'));
  333. return $this->fetch();
  334. }
  335. public function store_point_list($id)
  336. {
  337. $where = UtilService::getMore([
  338. ['page', 1],
  339. ['limit', 20],
  340. ['name', ''],
  341. ['excel', 0],
  342. ['type', $this->request->param('type')]
  343. ]);
  344. $where['store_id'] = $id;
  345. JsonService::successlayui(SystemStorePoint::getStoreList($where));
  346. }
  347. /**
  348. * 门店添加
  349. * @param int $id
  350. * @return string
  351. */
  352. public function add_point($id = 0, $store_id = 0)
  353. {
  354. $store = SystemStorePoint::getStoreDispose($id);
  355. $this->assign(compact('store', 'store_id'));
  356. return $this->fetch();
  357. }
  358. /**
  359. * 保存修改门店信息
  360. * @param int $id
  361. */
  362. public function save_point($id = 0)
  363. {
  364. $data = UtilService::postMore([
  365. ['name', ''],
  366. ['phone', ''],
  367. ['address', ''],
  368. ['detailed_address', ''],
  369. ['latlng', ''],
  370. ['valid_time', []],
  371. ['day_time', []],
  372. ['store_id', 0],
  373. ]);
  374. // var_dump($data['store_id']);
  375. if (!$data['store_id'] || !SystemStoreModel::getStoreDispose($data['store_id'])) JsonService::fail('无效门店');
  376. SystemStorePoint::beginTrans();
  377. try {
  378. $data['address'] = implode(',', $data['address']);
  379. $data['latlng'] = is_string($data['latlng']) ? explode(',', $data['latlng']) : $data['latlng'];
  380. if (!isset($data['latlng'][0]) || !isset($data['latlng'][1])) JsonService::fail('请选择门店位置');
  381. $data['latitude'] = $data['latlng'][0];
  382. $data['longitude'] = $data['latlng'][1];
  383. $data['valid_time'] = implode(' - ', $data['valid_time']);
  384. $data['day_time'] = implode(' - ', $data['day_time']);
  385. unset($data['latlng']);
  386. if ($id) {
  387. if (SystemStorePoint::where('id', $id)->update($data)) {
  388. SystemStorePoint::commitTrans();
  389. JsonService::success('修改成功');
  390. } else {
  391. SystemStorePoint::rollbackTrans();
  392. JsonService::fail('修改失败或者您没有修改什么!');
  393. }
  394. } else {
  395. $data['add_time'] = time();
  396. $data['is_show'] = 1;
  397. if ($res = SystemStorePoint::create($data)) {
  398. SystemStorePoint::commitTrans();
  399. JsonService::success('保存成功', ['id' => $res->id]);
  400. } else {
  401. SystemStorePoint::rollbackTrans();
  402. JsonService::fail('保存失败!');
  403. }
  404. }
  405. } catch (\Exception $e) {
  406. SystemStorePoint::rollbackTrans();
  407. JsonService::fail($e->getMessage());
  408. }
  409. }
  410. /**
  411. * 设置单个门店是否显示
  412. * @param string $is_show
  413. * @param string $id
  414. * @return json
  415. */
  416. public function set_point_show($is_show = '', $id = '')
  417. {
  418. ($is_show == '' || $id == '') && JsonService::fail('缺少参数');
  419. $res = SystemStorePoint::where(['id' => $id])->update(['is_show' => (int)$is_show]);
  420. if ($res) {
  421. return JsonService::successful($is_show == 1 ? '设置显示成功' : '设置隐藏成功');
  422. } else {
  423. return JsonService::fail($is_show == 1 ? '设置显示失败' : '设置隐藏失败');
  424. }
  425. }
  426. /**
  427. * 删除恢复门店
  428. * @param $id
  429. */
  430. public function point_delete($id)
  431. {
  432. if (!$id) return $this->failed('数据不存在');
  433. if (!SystemStorePoint::be(['id' => $id])) return $this->failed('数据不存在');
  434. if (SystemStorePoint::be(['id' => $id, 'is_del' => 1])) {
  435. $data['is_del'] = 0;
  436. if (!SystemStorePoint::edit($data, $id))
  437. return Json::fail(SystemStorePoint::getErrorInfo('恢复失败,请稍候再试!'));
  438. else
  439. return Json::successful('恢复门店成功!');
  440. } else {
  441. $data['is_del'] = 1;
  442. if (!SystemStorePoint::edit($data, $id))
  443. return Json::fail(SystemStorePoint::getErrorInfo('删除失败,请稍候再试!'));
  444. else
  445. return Json::successful('删除门店成功!');
  446. }
  447. }
  448. /**
  449. * @param $id
  450. * @return string
  451. * @throws Exception
  452. */
  453. public function store_sender($id)
  454. {
  455. $type = $this->request->param('type');
  456. $show = SystemStoreSender::where('store_id', $id)->where('is_show', 1)->where('is_del', 0)->count();//显示中的门店
  457. $hide = SystemStoreSender::where('store_id', $id)->where('is_show', 0)->count();//隐藏的门店
  458. $recycle = SystemStoreSender::where('store_id', $id)->where('is_del', 1)->count();//删除的门店
  459. if ($type == null) $type = 1;
  460. $this->assign(compact('type', 'show', 'hide', 'recycle', 'id'));
  461. return $this->fetch();
  462. }
  463. public function store_sender_list($id)
  464. {
  465. $where = UtilService::getMore([
  466. ['page', 1],
  467. ['limit', 20],
  468. ['name', ''],
  469. ['excel', 0],
  470. ['type', $this->request->param('type')]
  471. ]);
  472. $where['store_id'] = $id;
  473. JsonService::successlayui(SystemStoreSender::getStoreList($where));
  474. }
  475. /**
  476. * 门店添加
  477. * @param int $id
  478. * @return string
  479. */
  480. public function add_sender($id = 0, $store_id = 0)
  481. {
  482. $store = SystemStoreSender::getStoreDispose($id);
  483. $this->assign(compact('store', 'store_id'));
  484. return $this->fetch();
  485. }
  486. /**
  487. * 保存修改门店信息
  488. * @param int $id
  489. */
  490. public function save_sender($id = 0)
  491. {
  492. $data = UtilService::postMore([
  493. ['name', ''],
  494. ['phone', ''],
  495. ['store_id', 0],
  496. ]);
  497. // var_dump($data['store_id']);
  498. if (!$data['store_id'] || !SystemStoreModel::getStoreDispose($data['store_id'])) JsonService::fail('无效门店');
  499. SystemStoreSender::beginTrans();
  500. try {
  501. if ($id) {
  502. if (SystemStoreSender::where('id', $id)->update($data)) {
  503. SystemStoreSender::commitTrans();
  504. JsonService::success('修改成功');
  505. } else {
  506. SystemStoreSender::rollbackTrans();
  507. JsonService::fail('修改失败或者您没有修改什么!');
  508. }
  509. } else {
  510. $data['add_time'] = time();
  511. $data['is_show'] = 1;
  512. if ($res = SystemStoreSender::create($data)) {
  513. SystemStoreSender::commitTrans();
  514. JsonService::success('保存成功', ['id' => $res->id]);
  515. } else {
  516. SystemStoreSender::rollbackTrans();
  517. JsonService::fail('保存失败!');
  518. }
  519. }
  520. } catch (\Exception $e) {
  521. SystemStoreSender::rollbackTrans();
  522. JsonService::fail($e->getMessage());
  523. }
  524. }
  525. /**
  526. * 设置单个门店是否显示
  527. * @param string $is_show
  528. * @param string $id
  529. * @return json
  530. */
  531. public function set_sender_show($is_show = '', $id = '')
  532. {
  533. ($is_show == '' || $id == '') && JsonService::fail('缺少参数');
  534. $res = SystemStoreSender::where(['id' => $id])->update(['is_show' => (int)$is_show]);
  535. if ($res) {
  536. return JsonService::successful($is_show == 1 ? '设置显示成功' : '设置隐藏成功');
  537. } else {
  538. return JsonService::fail($is_show == 1 ? '设置显示失败' : '设置隐藏失败');
  539. }
  540. }
  541. /**
  542. * 删除恢复门店
  543. * @param $id
  544. */
  545. public function sender_delete($id)
  546. {
  547. if (!$id) return $this->failed('数据不存在');
  548. if (!SystemStoreSender::be(['id' => $id])) return $this->failed('数据不存在');
  549. if (SystemStoreSender::be(['id' => $id, 'is_del' => 1])) {
  550. $data['is_del'] = 0;
  551. if (!SystemStoreSender::edit($data, $id))
  552. return Json::fail(SystemStoreSender::getErrorInfo('恢复失败,请稍候再试!'));
  553. else
  554. return Json::successful('恢复门店成功!');
  555. } else {
  556. $data['is_del'] = 1;
  557. if (!SystemStoreSender::edit($data, $id))
  558. return Json::fail(SystemStoreSender::getErrorInfo('删除失败,请稍候再试!'));
  559. else
  560. return Json::successful('删除门店成功!');
  561. }
  562. }
  563. }