SystemStore.php 16 KB

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