SystemStore.php 16 KB

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