SystemStore.php 21 KB

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