SystemStore.php 20 KB

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