StoreDelivery.php 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277
  1. <?php
  2. // +----------------------------------------------------------------------
  3. // | CRMEB [ CRMEB赋能开发者,助力企业发展 ]
  4. // +----------------------------------------------------------------------
  5. // | Copyright (c) 2016~2020 https://www.crmeb.com All rights reserved.
  6. // +----------------------------------------------------------------------
  7. // | Licensed CRMEB并不是自由软件,未经许可不能去掉CRMEB相关版权
  8. // +----------------------------------------------------------------------
  9. // | Author: CRMEB Team <admin@crmeb.com>
  10. // +----------------------------------------------------------------------
  11. namespace app\controller\store\staff;
  12. use app\services\order\store\BranchOrderServices;
  13. use app\services\order\StoreOrderServices;
  14. use think\facade\App;
  15. use app\controller\store\AuthController;
  16. use app\services\store\DeliveryServiceServices;
  17. use app\services\user\UserWechatuserServices;
  18. /**
  19. * 配送员
  20. * Class StoreDelivery
  21. * @package app\controller\store\staff
  22. */
  23. class StoreDelivery extends AuthController
  24. {
  25. /**
  26. * DeliveryService constructor.
  27. * @param App $app
  28. * @param DeliveryServiceServices $services
  29. */
  30. public function __construct(App $app, DeliveryServiceServices $services)
  31. {
  32. parent::__construct($app);
  33. $this->services = $services;
  34. }
  35. /**
  36. * 显示资源列表
  37. *
  38. * @return \think\Response
  39. */
  40. public function index()
  41. {
  42. $where = $this->request->getMore([
  43. ['field_key', ''],
  44. ['keyword', '']
  45. ]);
  46. $where['type'] = 1;
  47. $where['relation_id'] = $this->storeId;
  48. $where['is_del'] = 0;
  49. return app('json')->success($this->services->getServiceList($where));
  50. }
  51. /**
  52. * 配送员新增表单
  53. * @return mixed
  54. * @throws \FormBuilder\Exception\FormBuilderException
  55. */
  56. public function create()
  57. {
  58. return app('json')->success($this->services->createStoreDeliveryForm());
  59. }
  60. /**
  61. * 配送员
  62. * @param $id
  63. * @return mixed
  64. */
  65. public function deliveryDetail($id)
  66. {
  67. $id = (int)$id;
  68. if (!$id) return $this->fail('缺少参数');
  69. return $this->success($this->services->deliveryDetail($id));
  70. }
  71. /**
  72. * 配送员修改表单
  73. * @return mixed
  74. * @throws \FormBuilder\Exception\FormBuilderException
  75. */
  76. public function edit()
  77. {
  78. [$id] = $this->request->getMore([
  79. [['id', 'd'], 0],
  80. ], true);
  81. $storeDelivery = $this->services->getOne(['id' => $id, 'is_del' => 0]);
  82. if (!$storeDelivery) {
  83. return app('json')->fail('配送员不存在');
  84. }
  85. return app('json')->success($this->services->updateStoreDeliveryForm($id, $storeDelivery));
  86. }
  87. /**
  88. * 显示创建资源表单页.
  89. *
  90. * @return \think\Response
  91. */
  92. public function userList(UserWechatuserServices $services)
  93. {
  94. $where = $this->request->getMore([
  95. ['nickname', ''],
  96. ['data', '', '', 'time'],
  97. ['type', '', '', 'user_type'],
  98. ]);
  99. [$list, $count] = $services->getWhereUserList($where, 'u.nickname,u.uid,u.avatar as headimgurl,w.subscribe,w.province,w.country,w.city,w.sex');
  100. return app('json')->success(compact('list', 'count'));
  101. }
  102. /*
  103. * 保存新建的资源
  104. */
  105. public function save()
  106. {
  107. $data = $this->request->postMore([
  108. ['image', ''],
  109. ['uid', 0],
  110. ['avatar', ''],
  111. ['phone', ''],
  112. ['nickname', ''],
  113. ['status', 1],
  114. ]);
  115. if ($data['image'] == '') return $this->fail('请选择用户');
  116. $data['uid'] = $data['image']['uid'];
  117. if (!$data['nickname']) {
  118. return app()->fail('请输入配送员名称');
  119. }
  120. if (!check_phone($data['phone'])) {
  121. return app('json')->fail('请输入正确的手机号!');
  122. }
  123. if ($this->services->count(['type' => 1, 'relation_id' => $this->storeId, 'phone' => $data['phone'], 'is_del' => 0])) {
  124. return app('json')->fail('同一个手机号的配送员只能添加一个!');
  125. }
  126. $data['avatar'] = $data['image']['image'];
  127. if ($this->services->count(['uid' => $data['uid'], 'type' => 1, 'relation_id' => $this->storeId, 'is_del' => 0])) {
  128. return $this->fail('配送员已存在!');
  129. }
  130. unset($data['image']);
  131. $data['type'] = 1;
  132. $data['relation_id'] = $this->storeId;
  133. unset($data['image']);
  134. $data['add_time'] = time();
  135. $res = $this->services->save($data);
  136. if ($res) {
  137. return app('json')->success('配送员添加成功');
  138. } else {
  139. return app('json')->fail('配送员添加失败,请稍后再试');
  140. }
  141. }
  142. /**
  143. * 保存新建的资源
  144. *
  145. * @param \think\Request $request
  146. * @return \think\Response
  147. */
  148. public function update($id)
  149. {
  150. $data = $this->request->postMore([
  151. ['avatar', ''],
  152. ['nickname', ''],
  153. ['phone', ''],
  154. ['status', 1],
  155. ]);
  156. $delivery = $this->services->get((int)$id);
  157. if (!$delivery) {
  158. return app('json')->fail('数据不存在');
  159. }
  160. if ($data["nickname"] == '') {
  161. return app('json')->fail("配送员名称不能为空!");
  162. }
  163. if (!$data['phone']) {
  164. return app('json')->fail("手机号不能为空!");
  165. }
  166. if (!check_phone($data['phone'])) {
  167. return app('json')->fail('请输入正确的手机号!');
  168. }
  169. if ($delivery['phone'] != $data['phone'] && $this->services->count(['type' => 1, 'relation_id' => $this->storeId, 'phone' => $data['phone'], 'is_del' => 0])) {
  170. return app('json')->fail('同一个手机号的配送员只能添加一个!');
  171. }
  172. $this->services->update($id, $data);
  173. return app('json')->success('修改成功!');
  174. }
  175. /**
  176. * 删除指定资源
  177. *
  178. * @param int $id
  179. * @return \think\Response
  180. */
  181. public function delete($id)
  182. {
  183. if (!$this->services->update($id, ['is_del' => 1]))
  184. return app('json')->fail('删除失败,请稍候再试!');
  185. else
  186. return app('json')->success('删除成功!');
  187. }
  188. /**
  189. * 修改状态
  190. * @param $id
  191. * @param $status
  192. * @return mixed
  193. */
  194. public function set_status($id, $status)
  195. {
  196. if ($status == '' || $id == 0) return app('json')->fail('参数错误');
  197. $this->services->update($id, ['status' => $status]);
  198. return app('json')->success($status == 0 ? '关闭成功' : '开启成功');
  199. }
  200. /**
  201. * 获取配送员select
  202. * @return mixed
  203. */
  204. public function getDeliverySelect()
  205. {
  206. $where['type'] = 1;
  207. $where['relation_id'] = $this->storeId;
  208. $where['is_del'] = 0;
  209. $where['status'] = 1;
  210. return app('json')->success($this->services->getSelectList($where));
  211. }
  212. /**
  213. * 获取所有配送员列表
  214. * @return mixed
  215. * @throws \think\db\exception\DataNotFoundException
  216. * @throws \think\db\exception\DbException
  217. * @throws \think\db\exception\ModelNotFoundException
  218. */
  219. public function get_delivery_list()
  220. {
  221. $data = $this->services->getDeliveryList(1, (int)$this->storeId);
  222. return $this->success($data);
  223. }
  224. /**
  225. * 获取配送员订单统计列表
  226. * @param StoreOrderServices $services
  227. * @return mixed
  228. * @throws \think\db\exception\DataNotFoundException
  229. * @throws \think\db\exception\DbException
  230. * @throws \think\db\exception\ModelNotFoundException
  231. */
  232. public function statistics(StoreOrderServices $services, BranchOrderServices $orderServices)
  233. {
  234. $where = $this->request->getMore([
  235. ['delivery_uid', 0],
  236. ['data', '', '', 'time'],
  237. ]);
  238. $where['type'] = 1;
  239. $where['relation_id'] = $this->storeId;
  240. $where['time'] = $orderServices->timeHandle($where['time']);
  241. return app('json')->success($services->getDeliveryStatistics($where));
  242. }
  243. /**
  244. * 取配送员订单统计头部图表数据
  245. * @param StoreOrderServices $services
  246. * @return mixed
  247. */
  248. public function statisticsHeader(StoreOrderServices $services, BranchOrderServices $orderServices)
  249. {
  250. [$delivery_uid, $time] = $this->request->getMore([
  251. ['delivery_uid', 0],
  252. ['data', '', '', 'time']
  253. ], true);
  254. $time = $orderServices->timeHandle($time, true);
  255. $store_id = (int)$this->storeId;
  256. return app('json')->success($services->getStatisticsHeader($store_id, $delivery_uid, $time));
  257. }
  258. }