DeliveryServiceServices.php 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337
  1. <?php
  2. // +----------------------------------------------------------------------
  3. // | CRMEB [ CRMEB赋能开发者,助力企业发展 ]
  4. // +----------------------------------------------------------------------
  5. // | Copyright (c) 2016~2022 https://www.crmeb.com All rights reserved.
  6. // +----------------------------------------------------------------------
  7. // | Licensed CRMEB并不是自由软件,未经许可不能去掉CRMEB相关版权
  8. // +----------------------------------------------------------------------
  9. // | Author: CRMEB Team <admin@crmeb.com>
  10. // +----------------------------------------------------------------------
  11. namespace app\services\store;
  12. use app\dao\store\DeliveryServiceDao;
  13. use app\services\BaseServices;
  14. use app\services\order\store\BranchOrderServices;
  15. use app\services\user\UserServices;
  16. use crmeb\exceptions\AdminException;
  17. use crmeb\services\FormBuilder;
  18. use think\exception\ValidateException;
  19. /**
  20. * 配送
  21. * Class DeliveryServiceServices
  22. * @package app\services\store
  23. * @mixin DeliveryServiceDao
  24. */
  25. class DeliveryServiceServices extends BaseServices
  26. {
  27. /**
  28. * 创建form表单
  29. * @var Form
  30. */
  31. protected $builder;
  32. /**
  33. * 构造方法
  34. * DeliveryServiceServices constructor.
  35. * @param DeliveryServiceDao $dao
  36. * @param FormBuilder $builder
  37. */
  38. public function __construct(DeliveryServiceDao $dao, FormBuilder $builder)
  39. {
  40. $this->dao = $dao;
  41. $this->builder = $builder;
  42. }
  43. /**
  44. * 获取配送员详情
  45. * @param int $id
  46. * @param string $field
  47. * @return array|\think\Model|null
  48. * @throws \think\db\exception\DataNotFoundException
  49. * @throws \think\db\exception\DbException
  50. * @throws \think\db\exception\ModelNotFoundException
  51. */
  52. public function getDeliveryInfo(int $id, string $field = '*')
  53. {
  54. $info = $this->dao->getOne(['id' => $id, 'is_del' => 0], $field);
  55. if (!$info) {
  56. throw new ValidateException('配送员不存在');
  57. }
  58. return $info;
  59. }
  60. /**
  61. * 更具uid获取配送员信息
  62. * @param int $uid
  63. * @param int $type
  64. * @param int $relation_id
  65. * @param string $field
  66. * @return array|\think\Model
  67. * @throws \think\db\exception\DataNotFoundException
  68. * @throws \think\db\exception\DbException
  69. * @throws \think\db\exception\ModelNotFoundException
  70. */
  71. public function getDeliveryInfoByUid(int $uid, int $type = 0, int $relation_id = 0, string $field = '*')
  72. {
  73. $where = ['uid' => $uid, 'is_del' => 0, 'status' => 1, 'type' => $type, 'relation_id' => $relation_id];
  74. $info = $this->dao->getOne($where, $field);
  75. if (!$info) {
  76. throw new ValidateException('配送员不存在');
  77. }
  78. return $info;
  79. }
  80. /**
  81. * 获取配送员所在门店id
  82. * @param int $uid
  83. * @param int $type
  84. * @param string $field
  85. * @param string $key
  86. * @return array
  87. */
  88. public function getDeliveryStoreIds(int $uid, int $type = 0, string $field = '*', string $key = '')
  89. {
  90. return $this->dao->getColumn(['uid' => $uid, 'type' => $type, 'is_del' => 0, 'status' => 1], $field, $key);
  91. }
  92. /**
  93. * 获取单个配送员统计信息
  94. * @param int $id
  95. * @return array
  96. * @throws \think\db\exception\DataNotFoundException
  97. * @throws \think\db\exception\DbException
  98. * @throws \think\db\exception\ModelNotFoundException
  99. */
  100. public function deliveryDetail(int $id)
  101. {
  102. $deliveryInfo = $this->getDeliveryInfo($id);
  103. if (!$deliveryInfo) {
  104. throw new AdminException('配送员不存在');
  105. }
  106. $where = ['store_id' => $deliveryInfo['store_id'], 'delivery_uid' => $deliveryInfo['uid']];
  107. /** @var BranchOrderServices $orderServices */
  108. $orderServices = app()->make(BranchOrderServices::class);
  109. $order_where = ['paid' => 1, 'refund_status' => [0, 3], 'is_del' => 0, 'is_system_del' => 0];
  110. $field = ['id', 'type', 'order_id', 'real_name', 'total_num', 'total_price', 'pay_price', 'FROM_UNIXTIME(pay_time,"%Y-%m-%d") as pay_time', 'paid', 'pay_type', 'activity_id', 'pink_id'];
  111. $list = $orderServices->getStoreOrderList($where + $order_where, $field, [], true);
  112. $data = ['id' => $id];
  113. $data['info'] = $deliveryInfo;
  114. $data['list'] = $list;
  115. $data['data'] = [
  116. [
  117. 'title' => '待配送订单数',
  118. 'value' => $orderServices->count($where + $order_where + ['status' => 2]),
  119. 'key' => '单',
  120. ],
  121. [
  122. 'title' => '已配送订单数',
  123. 'value' => $orderServices->count($where + $order_where + ['status' => 3]),
  124. 'key' => '单',
  125. ],
  126. [
  127. 'title' => '待配送订单金额',
  128. 'value' => $orderServices->sum($where + $order_where + ['status' => 2], 'pay_price', true),
  129. 'key' => '元',
  130. ],
  131. [
  132. 'title' => '已配送订单',
  133. 'value' => $orderServices->sum($where + $order_where + ['status' => 3], 'pay_price', true),
  134. 'key' => '元',
  135. ]
  136. ];
  137. return $data;
  138. }
  139. /**
  140. * 获取门店配送员订单统计
  141. * @param int $uid
  142. * @param int $store_id
  143. * @param string $time
  144. * @return array
  145. */
  146. public function getStoreData(int $uid, int $store_id, string $time = 'today')
  147. {
  148. $this->getDeliveryInfoByUid($uid, $store_id ? 1: 0, $store_id);
  149. $data = [];
  150. $where = ['delivery_uid' => $uid, 'time' => $time, 'paid' => 1, 'is_del' => 0, 'is_system_del' => 0, 'refund_status' => [0, 3]];
  151. if ($store_id) {
  152. $where['store_id'] = $store_id;
  153. }
  154. /** @var BranchOrderServices $order */
  155. $order = app()->make(BranchOrderServices::class);
  156. $where['status'] = 2;
  157. $data['unsend'] = $order->count($where);
  158. $where['status'] = 9;
  159. $data['send'] = $order->count($where);
  160. $data['send_price'] = $order->sum($where, 'pay_price', true);
  161. return $data;
  162. }
  163. /**
  164. * 获取配送员列表
  165. * @param array $where
  166. * @return array
  167. * @throws \think\db\exception\DataNotFoundException
  168. * @throws \think\db\exception\DbException
  169. * @throws \think\db\exception\ModelNotFoundException
  170. */
  171. public function getServiceList(array $where)
  172. {
  173. [$page, $limit] = $this->getPageValue();
  174. $list = $this->dao->getServiceList($where, $page, $limit);
  175. $count = $this->dao->count($where);
  176. return compact('list', 'count');
  177. }
  178. /**
  179. * 获取配送员列表
  180. * @param int $type
  181. * @param int $store_id
  182. * @return array
  183. * @throws \think\db\exception\DataNotFoundException
  184. * @throws \think\db\exception\DbException
  185. * @throws \think\db\exception\ModelNotFoundException
  186. */
  187. public function getDeliveryList(int $type = 0, int $relation_id = 0)
  188. {
  189. [$page, $limit] = $this->getPageValue();
  190. $where = ['status' => 1, 'is_del' => 0, 'type' => $type, 'relation_id' => $relation_id];
  191. $list = $this->dao->getServiceList($where, $page, $limit);
  192. foreach ($list as &$item) {
  193. if (!$item['nickname']) $item['nickname'] = $item['wx_name'];
  194. }
  195. $count = $this->dao->count($where);
  196. return compact('list', 'count');
  197. }
  198. /**
  199. * 创建配送员表单
  200. * @param array $formData
  201. * @return mixed
  202. * @throws \FormBuilder\Exception\FormBuilderException
  203. */
  204. public function createServiceForm(array $formData = [])
  205. {
  206. if ($formData) {
  207. $field[] = $this->builder->frameImage('avatar', '配送员头像', $this->url(config('admin.admin_prefix') . '/widget.images/index', ['fodder' => 'avatar'], true), $formData['avatar'] ?? '')->icon('ios-add')->width('960px')->height('505px')->modal(['footer-hide' => true]);
  208. } else {
  209. $field[] = $this->builder->frameImage('image', '商城用户', $this->url(config('admin.admin_prefix') . '/system.user/list', ['fodder' => 'image'], true))->icon('ios-add')->width('960px')->height('550px')->modal(['footer-hide' => true])->Props(['srcKey' => 'image']);
  210. $field[] = $this->builder->hidden('uid', 0);
  211. $field[] = $this->builder->hidden('avatar', '');
  212. }
  213. $field[] = $this->builder->input('nickname', '配送员名称', $formData['nickname'] ?? '')->required('请填写配送员名称')->col(24);
  214. $field[] = $this->builder->input('phone', '手机号码', $formData['phone'] ?? '')->required('请填写电话')->col(24)->maxlength(11);
  215. $field[] = $this->builder->radio('status', '配送员状态', $formData['status'] ?? 1)->options([['value' => 1, 'label' => '显示'], ['value' => 0, 'label' => '隐藏']]);
  216. return $field;
  217. }
  218. /**
  219. * 创建配送员获取表单
  220. * @return array
  221. * @throws \FormBuilder\Exception\FormBuilderException
  222. */
  223. public function create()
  224. {
  225. return create_form('添加配送员', $this->createServiceForm(), $this->url('/order/delivery/save'), 'POST');
  226. }
  227. /**
  228. * 编辑获取表单
  229. * @param int $id
  230. * @return array
  231. * @throws \FormBuilder\Exception\FormBuilderException
  232. */
  233. public function edit(int $id)
  234. {
  235. $serviceInfo = $this->dao->get($id);
  236. if (!$serviceInfo) {
  237. throw new AdminException('数据不存在!');
  238. }
  239. return create_form('编辑配送员', $this->createServiceForm($serviceInfo->toArray()), $this->url('/order/delivery/update/' . $id), 'PUT');
  240. }
  241. /**
  242. * 获取某人的聊天记录用户列表
  243. * @param int $uid
  244. * @return array|array[]
  245. * @throws \think\db\exception\DataNotFoundException
  246. * @throws \think\db\exception\DbException
  247. * @throws \think\db\exception\ModelNotFoundException
  248. */
  249. public function getChatUser(int $uid)
  250. {
  251. /** @var StoreServiceLogServices $serviceLog */
  252. $serviceLog = app()->make(StoreServiceLogServices::class);
  253. /** @var UserServices $serviceUser */
  254. $serviceUser = app()->make(UserServices::class);
  255. $uids = $serviceLog->getChatUserIds($uid);
  256. if (!$uids) {
  257. return [];
  258. }
  259. return $serviceUser->getUserList(['uid' => $uids], 'nickname,uid,avatar as headimgurl');
  260. }
  261. /**
  262. * 检查用户是否是配送员
  263. * @param int $uid
  264. * @return bool
  265. * @throws \think\db\exception\DataNotFoundException
  266. * @throws \think\db\exception\DbException
  267. * @throws \think\db\exception\ModelNotFoundException
  268. */
  269. public function checkoutIsService(int $uid)
  270. {
  271. return (bool)$this->dao->count(['uid' => $uid, 'status' => 1, 'is_del' => 0]);
  272. }
  273. /**
  274. * 添加门店配送员
  275. * @return array
  276. * @throws \FormBuilder\Exception\FormBuilderException
  277. */
  278. public function createStoreDeliveryForm()
  279. {
  280. $field[] = $this->builder->frameImage('image', '商城用户', $this->url(config('admin.store_prefix') . '/system.User/list', ['fodder' => 'image'], true))->icon('ios-add')->width('960px')->height('430px')->modal(['footer-hide' => true])->Props(['srcKey' => 'image']);
  281. $field[] = $this->builder->hidden('uid', 0);
  282. $field[] = $this->builder->hidden('avatar', '');
  283. $field[] = $this->builder->input('nickname', '配送员名称')->required('请填写配送员名称')->col(24);
  284. $field[] = $this->builder->input('phone', '手机号码')->required('请填写电话')->col(24)->maxlength(11);
  285. $field[] = $this->builder->radio('status', '配送员状态', 1)->options([['value' => 1, 'label' => '显示'], ['value' => 0, 'label' => '隐藏']]);
  286. return create_form('添加门店配送员', $field, $this->url('/staff/delivery'));
  287. }
  288. /**
  289. * 编辑门店配送员
  290. * @return array
  291. * @throws \FormBuilder\Exception\FormBuilderException
  292. */
  293. public function updateStoreDeliveryForm($id, $formData)
  294. {
  295. $field[] = $this->builder->input('nickname', '配送员名称', $formData['nickname'] ?? '')->required('请填写配送员名称')->col(24);
  296. $field[] = $this->builder->frameImage('avatar', '配送员头像', $this->url(config('admin.store_prefix') . '/widget.images/index', ['fodder' => 'avatar'], true), $formData['avatar'] ?? '')->icon('ios-add')->width('960px')->height('505px')->modal(['footer-hide' => true]);
  297. $field[] = $this->builder->input('phone', '手机号码', $formData['phone'] ?? '')->required('请填写电话')->col(24)->maxlength(11);
  298. $field[] = $this->builder->radio('status', '配送员状态', $formData['status'] ?? 1)->options([['value' => 1, 'label' => '显示'], ['value' => 0, 'label' => '隐藏']]);
  299. return create_form('编辑门店配送员', $field, $this->url('/staff/delivery/' . $id), 'put');
  300. }
  301. /**
  302. * 获取配送员select(Uid)
  303. * @param array $where
  304. * @return mixed
  305. */
  306. public function getSelectList($where = [])
  307. {
  308. $list = $this->dao->getSelectList($where);
  309. $menus = [];
  310. foreach ($list as $menu) {
  311. $menus[] = ['value' => $menu['uid'], 'label' => $menu['nickname']];
  312. }
  313. return $menus;
  314. }
  315. }