SystemStore.php 16 KB

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