SystemStore.php 21 KB

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