SystemStoreStaffServices.php 36 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763
  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\services\store;
  12. use app\dao\store\SystemStoreStaffDao;
  13. use app\services\BaseServices;
  14. use app\services\order\OtherOrderServices;
  15. use app\services\order\store\BranchOrderServices;
  16. use app\services\system\SystemRoleServices;
  17. use app\services\user\UserCardServices;
  18. use app\services\user\UserRechargeServices;
  19. use app\services\user\UserSpreadServices;
  20. use crmeb\exceptions\AdminException;
  21. use crmeb\services\FormBuilder;
  22. use think\exception\ValidateException;
  23. /**
  24. * 门店店员
  25. * Class SystemStoreStaffServices
  26. * @package app\services\system\store
  27. * @mixin SystemStoreStaffDao
  28. */
  29. class SystemStoreStaffServices extends BaseServices
  30. {
  31. /**
  32. * @var FormBuilder
  33. */
  34. protected $builder;
  35. /**
  36. * 构造方法
  37. * SystemStoreStaffServices constructor.
  38. * @param SystemStoreStaffDao $dao
  39. */
  40. public function __construct(SystemStoreStaffDao $dao, FormBuilder $builder)
  41. {
  42. $this->dao = $dao;
  43. $this->builder = $builder;
  44. }
  45. /**
  46. * 获取低于等级的店员名称和id
  47. * @param string $field
  48. * @param int $level
  49. * @return array
  50. * @throws \think\db\exception\DataNotFoundException
  51. * @throws \think\db\exception\DbException
  52. * @throws \think\db\exception\ModelNotFoundException
  53. */
  54. public function getOrdAdmin(string $field = 'real_name,id', int $storeId = 0, int $level = 0)
  55. {
  56. return $this->dao->getWhere()->when('store_id', $storeId)->where('level', '>=', $level)->field($field)->select()->toArray();
  57. }
  58. /**
  59. * 获取门店客服列表
  60. * @param int $store_id
  61. * @param string $field
  62. * @return array
  63. * @throws \think\db\exception\DataNotFoundException
  64. * @throws \think\db\exception\DbException
  65. * @throws \think\db\exception\ModelNotFoundException
  66. */
  67. public function getCustomerList(int $store_id, string $field = '*')
  68. {
  69. return $this->dao->getWhere()->where('store_id', $store_id)->where('status', 1)->where('is_del', 0)->where('is_customer', 1)->field($field)->select()->toArray();
  70. }
  71. /**
  72. * 获取店员详情
  73. * @param int $id
  74. * @param string $field
  75. * @return array|\think\Model|null
  76. * @throws \think\db\exception\DataNotFoundException
  77. * @throws \think\db\exception\DbException
  78. * @throws \think\db\exception\ModelNotFoundException
  79. */
  80. public function getStaffInfo(int $id, string $field = '*')
  81. {
  82. $info = $this->dao->getOne(['id' => $id, 'is_del' => 0], $field);
  83. if (!$info) {
  84. throw new ValidateException('店员不存在');
  85. }
  86. return $info;
  87. }
  88. /**
  89. * 根据uid获取门店店员信息
  90. * @param int $uid
  91. * @param int $store_id
  92. * @param string $field
  93. * @return array|\think\Model
  94. * @throws \think\db\exception\DataNotFoundException
  95. * @throws \think\db\exception\DbException
  96. * @throws \think\db\exception\ModelNotFoundException
  97. */
  98. public function getStaffInfoByUid(int $uid, int $store_id = 0, string $field = '*')
  99. {
  100. $where = ['uid' => $uid, 'is_del' => 0, 'status' => 1];
  101. if ($store_id) $where['store_id'] = $store_id;
  102. $info = $this->dao->getOne($where, $field);
  103. if (!$info) {
  104. throw new ValidateException('店员不存在');
  105. }
  106. return $info;
  107. }
  108. /**
  109. * 获取门店|店员统计
  110. * @param int $store_id
  111. * @param int $staff_id
  112. * @param string $time
  113. * @return array
  114. */
  115. public function getStoreData(int $uid, int $store_id, int $staff_id = 0, string $time = 'today')
  116. {
  117. $where = ['store_id' => $store_id, 'time' => $time];
  118. if ($staff_id) {
  119. $where['staff_id'] = $staff_id;
  120. }
  121. $data = [];
  122. $order_where = ['pid' => 0, 'paid' => 1, 'refund_status' => [0, 3], 'is_del' => 0, 'is_system_del' => 0];
  123. /** @var BranchOrderServices $orderServices */
  124. $orderServices = app()->make(BranchOrderServices::class);
  125. $data['send_price'] = $orderServices->sum($where + $order_where + ['type' => 7], 'pay_price', true);
  126. $data['send_count'] = $orderServices->count($where + $order_where + ['type' => 7]);
  127. $data['refund_price'] = $orderServices->sum($where + ['status' => -3], 'pay_price', true);
  128. $data['refund_count'] = $orderServices->count($where + ['status' => -3]);
  129. $data['cashier_price'] = $orderServices->sum($where + $order_where + ['type' => 6], 'pay_price', true);
  130. $data['writeoff_price'] = $orderServices->sum($where + $order_where + ['type' => 5], 'pay_price', true);
  131. /** @var OtherOrderServices $otherOrder */
  132. $otherOrder = app()->make(OtherOrderServices::class);
  133. $data['svip_price'] = $otherOrder->sum($where + ['paid' => 1, 'type' => [0, 1, 2, 4]], 'pay_price', true);
  134. /** @var UserRechargeServices $userRecharge */
  135. $userRecharge = app()->make(UserRechargeServices::class);
  136. $data['recharge_price'] = $userRecharge->getWhereSumField($where + ['paid' => 1], 'price');
  137. /** @var UserSpreadServices $userSpread */
  138. $userSpread = app()->make(UserSpreadServices::class);
  139. $data['spread_count'] = $userSpread->count($where + ['timeKey' => 'spread_time']);
  140. /** @var UserCardServices $userCard */
  141. $userCard = app()->make(UserCardServices::class);
  142. $data['card_count'] = $userCard->count($where + ['is_submit' => 1]);
  143. return $data;
  144. }
  145. /**
  146. * 判断是否是有权限核销的店员
  147. * @param $uid
  148. * @return bool
  149. */
  150. public function verifyStatus($uid)
  151. {
  152. return (bool)$this->dao->getOne(['uid' => $uid, 'status' => 1, 'is_del' => 0, 'verify_status' => 1]);
  153. }
  154. /**
  155. * 获取店员列表
  156. * @param array $where
  157. * @param array $with
  158. * @return array
  159. * @throws \think\db\exception\DataNotFoundException
  160. * @throws \think\db\exception\DbException
  161. * @throws \think\db\exception\ModelNotFoundException
  162. */
  163. public function getStoreStaffList(array $where, array $with = [])
  164. {
  165. // $with = array_merge($with, [
  166. // 'workMember' => function ($query) {
  167. // $query->field(['uid', 'name', 'position', 'qr_code', 'external_position']);
  168. // }
  169. // ]);
  170. [$page, $limit] = $this->getPageValue();
  171. $list = $this->dao->getStoreStaffList($where, '*', $page, $limit, $with);
  172. if ($list) {
  173. /** @var SystemRoleServices $service */
  174. $service = app()->make(SystemRoleServices::class);
  175. $allRole = $service->getRoleArray(['type' => 1, 'store_id' => $where['store_id'], 'status' => 1]);
  176. foreach ($list as &$item) {
  177. $item['workMember'] = [];
  178. if ($item['level']) {
  179. if ($item['roles']) {
  180. $roles = [];
  181. foreach ($item['roles'] as $id) {
  182. if (isset($allRole[$id])) $roles[] = $allRole[$id];
  183. }
  184. if ($roles) {
  185. $item['roles'] = implode(',', $roles);
  186. } else {
  187. $item['roles'] = '';
  188. }
  189. } else {
  190. $item['roles'] = '';
  191. }
  192. } else {
  193. $item['roles'] = '超级管理员';
  194. }
  195. }
  196. }
  197. $count = $this->dao->count($where);
  198. return compact('list', 'count');
  199. }
  200. /**
  201. * 不查询总数
  202. * @param array $where
  203. * @param array $with
  204. * @return array
  205. * @throws \think\db\exception\DataNotFoundException
  206. * @throws \think\db\exception\DbException
  207. * @throws \think\db\exception\ModelNotFoundException
  208. */
  209. public function getStoreStaff(array $where, array $with = [])
  210. {
  211. [$page, $limit] = $this->getPageValue();
  212. $list = $this->dao->getStoreStaffList($where, '*', $page, $limit, $with);
  213. foreach ($list as $key => $item) {
  214. unset($list[$key]['pwd']);
  215. }
  216. return $list;
  217. }
  218. /**
  219. * 店员详情
  220. * @param int $id
  221. * @return array|\think\Model
  222. * @throws \think\db\exception\DataNotFoundException
  223. * @throws \think\db\exception\DbException
  224. * @throws \think\db\exception\ModelNotFoundException
  225. */
  226. public function read(int $id)
  227. {
  228. $staffInfo = $this->getStaffInfo($id);
  229. $info = [
  230. 'id' => $id,
  231. 'headerList' => $this->getHeaderList($id, $staffInfo),
  232. 'ps_info' => $staffInfo
  233. ];
  234. return $info;
  235. }
  236. /**
  237. * 获取单个店员统计信息
  238. * @param $id 用户id
  239. * @return mixed
  240. */
  241. public function staffDetail(int $id, string $type)
  242. {
  243. $staffInfo = $this->getStaffInfo($id);
  244. if (!$staffInfo) {
  245. throw new AdminException('店员不存在');
  246. }
  247. $where = ['store_id' => $staffInfo['store_id'], 'staff_id' => $staffInfo['id']];
  248. $data = [];
  249. switch ($type) {
  250. case 'cashier_order':
  251. /** @var BranchOrderServices $orderServices */
  252. $orderServices = app()->make(BranchOrderServices::class);
  253. $where = array_merge($where, ['pid' => 0, 'type' => 6, 'paid' => 1, 'refund_status' => [0, 3], 'is_del' => 0, 'is_system_del' => 0]);
  254. $field = ['uid', 'order_id', 'real_name', 'total_num', 'total_price', 'pay_price', 'FROM_UNIXTIME(pay_time,"%Y-%m-%d") as pay_time', 'paid', 'pay_type', 'type', 'activity_id', 'pink_id'];
  255. $data = $orderServices->getStoreOrderList($where, $field, [], true);
  256. break;
  257. case 'self_order':
  258. /** @var BranchOrderServices $orderServices */
  259. $orderServices = app()->make(BranchOrderServices::class);
  260. $where = array_merge($where, ['pid' => 0, 'type' => 7, 'paid' => 1, 'refund_status' => [0, 3], 'is_del' => 0, 'is_system_del' => 0]);
  261. $field = ['uid', 'order_id', 'real_name', 'total_num', 'total_price', 'pay_price', 'FROM_UNIXTIME(pay_time,"%Y-%m-%d") as pay_time', 'paid', 'pay_type', 'type', 'activity_id', 'pink_id'];
  262. $data = $orderServices->getStoreOrderList($where, $field, [], true);
  263. break;
  264. case 'writeoff_order':
  265. /** @var BranchOrderServices $orderServices */
  266. $orderServices = app()->make(BranchOrderServices::class);
  267. $where = array_merge($where, ['pid' => 0, 'type' => 5, 'paid' => 1, 'refund_status' => [0, 3], 'is_del' => 0, 'is_system_del' => 0]);
  268. $field = ['uid', 'order_id', 'real_name', 'total_num', 'total_price', 'pay_price', 'FROM_UNIXTIME(pay_time,"%Y-%m-%d") as pay_time', 'paid', 'pay_type', 'type', 'activity_id', 'pink_id'];
  269. $data = $orderServices->getStoreOrderList($where, $field, [], true);
  270. break;
  271. case 'recharge':
  272. /** @var UserRechargeServices $userRechargeServices */
  273. $userRechargeServices = app()->make(UserRechargeServices::class);
  274. $data = $userRechargeServices->getRechargeList($where + ['paid' => 1]);
  275. break;
  276. case 'spread':
  277. /** @var UserSpreadServices $userSpreadServices */
  278. $userSpreadServices = app()->make(UserSpreadServices::class);
  279. $data = $userSpreadServices->getSpreadList($where);
  280. break;
  281. case 'card':
  282. /** @var UserCardServices $userCardServices */
  283. $userCardServices = app()->make(UserCardServices::class);
  284. $data = $userCardServices->getCardList($where + ['is_submit' => 1]);
  285. break;
  286. case 'svip':
  287. /** @var OtherOrderServices $otherOrderServices */
  288. $otherOrderServices = app()->make(OtherOrderServices::class);
  289. $data = $otherOrderServices->getMemberRecord($where);
  290. break;
  291. default:
  292. throw new AdminException('type参数错误');
  293. }
  294. return $data;
  295. }
  296. /**
  297. * 店员详情头部信息
  298. * @param int $id
  299. * @param array $staffInfo
  300. * @return array[]
  301. * @throws \think\db\exception\DataNotFoundException
  302. * @throws \think\db\exception\DbException
  303. * @throws \think\db\exception\ModelNotFoundException
  304. */
  305. public function getHeaderList(int $id, $staffInfo = [])
  306. {
  307. if (!$staffInfo) {
  308. $staffInfo = $this->dao->get($id);
  309. }
  310. $where = ['store_id' => $staffInfo['store_id'], 'staff_id' => $staffInfo['id']];
  311. /** @var BranchOrderServices $orderServices */
  312. $orderServices = app()->make(BranchOrderServices::class);
  313. $cashier_order = $orderServices->sum($where + ['pid' => 0, 'type' => 6, 'paid' => 1, 'refund_status' => 0, 'is_del' => 0, 'is_system_del' => 0], 'pay_price', true);
  314. $writeoff_order = $orderServices->sum($where + ['pid' => 0, 'type' => 5, 'paid' => 1, 'refund_status' => 0, 'is_del' => 0, 'is_system_del' => 0], 'pay_price', true);
  315. $self_order = $orderServices->sum($where + ['pid' => 0, 'type' => 7, 'paid' => 1, 'refund_status' => 0, 'is_del' => 0, 'is_system_del' => 0], 'pay_price', true);
  316. /** @var UserRechargeServices $userRechargeServices */
  317. $userRechargeServices = app()->make(UserRechargeServices::class);
  318. $recharge = $userRechargeServices->sum($where + ['paid' => 1], 'price', true);
  319. /** @var UserSpreadServices $userSpreadServices */
  320. $userSpreadServices = app()->make(UserSpreadServices::class);
  321. $spread = $userSpreadServices->count($where);
  322. /** @var UserCardServices $userCardServices */
  323. $userCardServices = app()->make(UserCardServices::class);
  324. $card = $userCardServices->count($where + ['is_submit' => 1]);
  325. /** @var OtherOrderServices $otherOrderServices */
  326. $otherOrderServices = app()->make(OtherOrderServices::class);
  327. $svip = $otherOrderServices->sum($where, 'pay_price', true);
  328. return [
  329. [
  330. 'title' => '收银订单',
  331. 'value' => $cashier_order,
  332. 'key' => '元',
  333. ],
  334. [
  335. 'title' => '核销订单',
  336. 'value' => $writeoff_order,
  337. 'key' => '元',
  338. ],
  339. [
  340. 'title' => '配送订单',
  341. 'value' => $self_order,
  342. 'key' => '元',
  343. ],
  344. [
  345. 'title' => '充值订单',
  346. 'value' => $recharge,
  347. 'key' => '元',
  348. ],
  349. [
  350. 'title' => '付费会员',
  351. 'value' => $svip,
  352. 'key' => '元',
  353. ],
  354. [
  355. 'title' => '推广用户数',
  356. 'value' => $spread,
  357. 'key' => '人',
  358. ],
  359. [
  360. 'title' => '激活会员卡',
  361. 'value' => $card,
  362. 'key' => '张',
  363. ]
  364. ];
  365. }
  366. /**
  367. * 获取select选择框中的门店列表
  368. * @return array
  369. * @throws \think\db\exception\DataNotFoundException
  370. * @throws \think\db\exception\DbException
  371. * @throws \think\db\exception\ModelNotFoundException
  372. */
  373. public function getStoreSelectFormData()
  374. {
  375. /** @var SystemStoreServices $service */
  376. $service = app()->make(SystemStoreServices::class);
  377. $menus = [];
  378. foreach ($service->getStore() as $menu) {
  379. $menus[] = ['value' => $menu['id'], 'label' => $menu['name']];
  380. }
  381. return $menus;
  382. }
  383. /**
  384. * 获取核销员表单
  385. * @param array $formData
  386. * @return mixed
  387. * @throws \FormBuilder\Exception\FormBuilderException
  388. * @throws \think\db\exception\DataNotFoundException
  389. * @throws \think\db\exception\DbException
  390. * @throws \think\db\exception\ModelNotFoundException
  391. */
  392. public function createStaffForm(array $formData = [])
  393. {
  394. if ($formData) {
  395. $field[] = $this->builder->frameImage('image', '更换头像', $this->url(config('admin.admin_prefix') . '/widget.images/index', array('fodder' => 'image'), true), $formData['avatar'] ?? '')->icon('ios-add')->width('960px')->height('505px')->modal(['footer-hide' => true]);
  396. } else {
  397. $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']);
  398. }
  399. $field[] = $this->builder->hidden('uid', $formData['uid'] ?? 0);
  400. $field[] = $this->builder->hidden('avatar', $formData['avatar'] ?? '');
  401. $field[] = $this->builder->select('store_id', '所属提货点', ($formData['store_id'] ?? 0))->setOptions($this->getStoreSelectFormData())->filterable(true);
  402. $field[] = $this->builder->input('staff_name', '核销员名称', $formData['staff_name'] ?? '')->col(24)->required();
  403. $field[] = $this->builder->input('phone', '手机号码', $formData['phone'] ?? '')->col(24)->required();
  404. $field[] = $this->builder->radio('verify_status', '核销开关', $formData['verify_status'] ?? 1)->options([['value' => 1, 'label' => '开启'], ['value' => 0, 'label' => '关闭']]);
  405. $field[] = $this->builder->radio('status', '状态', $formData['status'] ?? 1)->options([['value' => 1, 'label' => '开启'], ['value' => 0, 'label' => '关闭']]);
  406. return $field;
  407. }
  408. /**
  409. * 添加核销员表单
  410. * @return array
  411. * @throws \FormBuilder\Exception\FormBuilderException
  412. * @throws \think\db\exception\DataNotFoundException
  413. * @throws \think\db\exception\DbException
  414. * @throws \think\db\exception\ModelNotFoundException
  415. */
  416. public function createForm()
  417. {
  418. return create_form('添加核销员', $this->createStaffForm(), $this->url('/merchant/store_staff/save/0'));
  419. }
  420. /**
  421. * 编辑核销员form表单
  422. * @param int $id
  423. * @return array
  424. * @throws \FormBuilder\Exception\FormBuilderException
  425. * @throws \think\db\exception\DataNotFoundException
  426. * @throws \think\db\exception\DbException
  427. * @throws \think\db\exception\ModelNotFoundException
  428. */
  429. public function updateForm(int $id)
  430. {
  431. $storeStaff = $this->dao->get($id);
  432. if (!$storeStaff) {
  433. throw new AdminException('没有查到信息,无法修改');
  434. }
  435. return create_form('修改核销员', $this->createStaffForm($storeStaff->toArray()), $this->url('/merchant/store_staff/save/' . $id));
  436. }
  437. /**
  438. * 获取门店店员
  439. * @param $where
  440. * @return array
  441. * @throws \think\db\exception\DataNotFoundException
  442. * @throws \think\db\exception\DbException
  443. * @throws \think\db\exception\ModelNotFoundException
  444. */
  445. public function getStoreAdminList($where)
  446. {
  447. [$page, $limit] = $this->getPageValue();
  448. $list = $this->dao->getStoreAdminList($where, $page, $limit);
  449. /** @var SystemRoleServices $service */
  450. $service = app()->make(SystemRoleServices::class);
  451. $allRole = $service->getRoleArray(['type' => 1, 'store_id' => $where['store_id']]);
  452. foreach ($list as &$item) {
  453. if ($item['roles']) {
  454. $roles = [];
  455. foreach ($item['roles'] as $id) {
  456. if (isset($allRole[$id])) $roles[] = $allRole[$id];
  457. }
  458. if ($roles) {
  459. $item['roles'] = implode(',', $roles);
  460. } else {
  461. $item['roles'] = '';
  462. }
  463. }
  464. }
  465. $count = $this->dao->count($where);
  466. return compact('list', 'count');
  467. }
  468. /**
  469. * 添加门店管理员
  470. * @param int $store_id
  471. * @param $level
  472. * @return array
  473. * @throws \FormBuilder\Exception\FormBuilderException
  474. */
  475. public function createStoreAdminForm(int $store_id, $level)
  476. {
  477. $field[] = $this->builder->input('staff_name', '管理员名称')->col(24)->required();
  478. $field[] = $this->builder->frameImage('avatar', '管理员头像', $this->url(config('admin.admin_prefix') . '/widget.images/index', ['fodder' => 'avatar'], true))->icon('ios-add')->width('960px')->height('505px')->modal(['footer-hide' => true]);
  479. $field[] = $this->builder->input('account', '管理员账号')->maxlength(35)->required('请填写管理员账号');
  480. $field[] = $this->builder->input('phone', '手机号码')->col(24)->required();
  481. $field[] = $this->builder->input('pwd', '管理员密码')->type('password')->required('请填写管理员密码');
  482. $field[] = $this->builder->input('conf_pwd', '确认密码')->type('password')->required('请输入确认密码');
  483. /** @var SystemRoleServices $service */
  484. $service = app()->make(SystemRoleServices::class);
  485. $options = $service->getRoleFormSelect($level, 1, $store_id);
  486. $roles = [];
  487. $field[] = $this->builder->select('roles', '管理员身份', $roles)->setOptions(FormBuilder::setOptions($options))->multiple(true)->required('请选择管理员身份');
  488. $field[] = $this->builder->radio('status', '状态', 1)->options([['value' => 1, 'label' => '开启'], ['value' => 0, 'label' => '关闭']]);
  489. return create_form('添加门店管理员', $field, $this->url('/system/admin'));
  490. }
  491. /**
  492. * 修改门店管理员
  493. * @param $id
  494. * @param $level
  495. * @return array
  496. * @throws \FormBuilder\Exception\FormBuilderException
  497. * @throws \think\db\exception\DataNotFoundException
  498. * @throws \think\db\exception\DbException
  499. * @throws \think\db\exception\ModelNotFoundException
  500. */
  501. public function updateStoreAdminForm($id, $level)
  502. {
  503. $adminInfo = $this->dao->get($id);
  504. if (!$adminInfo) {
  505. throw new AdminException('门店管理员不存在!');
  506. }
  507. if ($adminInfo->is_del) {
  508. throw new AdminException('门店管理员已经删除');
  509. }
  510. $adminInfo = $adminInfo->toArray();
  511. $field[] = $this->builder->input('staff_name', '门店管理员名称', $adminInfo['staff_name'])->col(24)->required('请填写门店管理员名称');
  512. $field[] = $this->builder->frameImage('avatar', '管理员头像', $this->url(config('admin.store_prefix') . '/widget.images/index', ['fodder' => 'avatar'], true), $adminInfo['avatar'] ?? '')->icon('ios-add')->width('960px')->height('505px')->modal(['footer-hide' => true]);
  513. $field[] = $this->builder->input('account', '门店管理员账号', $adminInfo['account'])->maxlength(35)->required('请填写门店管理员账号');
  514. $field[] = $this->builder->input('phone', '手机号码', $adminInfo['phone'])->col(24)->required();
  515. $field[] = $this->builder->input('pwd', '门店管理员密码')->placeholder('不更改密码请留空')->type('password');
  516. $field[] = $this->builder->input('conf_pwd', '确认密码')->placeholder('不更改密码请留空')->type('password');
  517. /** @var SystemRoleServices $service */
  518. $service = app()->make(SystemRoleServices::class);
  519. $options = $service->getRoleFormSelect($level, 1, (int)$adminInfo['store_id']);
  520. $roles = [];
  521. if ($adminInfo && isset($adminInfo['roles']) && $adminInfo['roles']) {
  522. foreach ($adminInfo['roles'] as $role) {
  523. $roles[] = (int)$role;
  524. }
  525. }
  526. $field[] = $this->builder->select('roles', '管理员身份', $roles)->setOptions(FormBuilder::setOptions($options))->multiple(true)->required('请选择门店管理员身份');
  527. $field[] = $this->builder->radio('status', '状态', (int)$adminInfo['status'])->options([['value' => 1, 'label' => '开启'], ['value' => 0, 'label' => '关闭']]);
  528. return create_form('修改门店管理员', $field, $this->url('/system/admin/' . $id), 'put');
  529. }
  530. /**
  531. * 添加门店店员
  532. * @param int $store_id
  533. * @param $level
  534. * @return array
  535. * @throws \FormBuilder\Exception\FormBuilderException
  536. */
  537. public function createStoreStaffForm(int $store_id, $level)
  538. {
  539. $field[] = $this->builder->input('staff_name', '店员名称')->col(24)->required('请输入门店店员名称');
  540. $field[] = $this->builder->frameImage('image', '商城用户', $this->url(config('admin.store_prefix') . '/system.User/list', ['fodder' => 'image'], true))->icon('ios-add')->width('960px')->height('450px')->modal(['footer-hide' => true])->Props(['srcKey' => 'image']);
  541. $field[] = $this->builder->hidden('uid', 0);
  542. $field[] = $this->builder->hidden('avatar', '');
  543. $field[] = $this->builder->input('account', '店员账号')->maxlength(35)->required('请填写门店店员账号');
  544. $field[] = $this->builder->input('pwd', '店员密码')->type('password')->required('请填写门店店员密码');
  545. $field[] = $this->builder->input('conf_pwd', '确认密码')->type('password')->required('请输入确认密码');
  546. $field[] = $this->builder->input('phone', '手机号码')->col(24)->required('请输入手机号');
  547. /** @var SystemRoleServices $service */
  548. $service = app()->make(SystemRoleServices::class);
  549. $options = $service->getRoleFormSelect($level, 1, $store_id);
  550. $roles = [];
  551. $field[] = $this->builder->select('roles', '店员身份', $roles)->setOptions(FormBuilder::setOptions($options))->multiple(true)->required('请选择店员身份');
  552. $field[] = $this->builder->radio('is_manager', '是否是店长', 0)->options([['value' => 1, 'label' => '开启'], ['value' => 0, 'label' => '关闭']]);
  553. $field[] = $this->builder->radio('order_status', '订单管理', 1)->options([['value' => 1, 'label' => '开启'], ['value' => 0, 'label' => '关闭']]);
  554. $field[] = $this->builder->radio('verify_status', '核销开关', 1)->options([['value' => 1, 'label' => '开启'], ['value' => 0, 'label' => '关闭']]);
  555. $field[] = $this->builder->radio('is_cashier', '是否是收银员', 1)->options([['value' => 1, 'label' => '开启'], ['value' => 0, 'label' => '关闭']]);
  556. $field[] = $this->builder->radio('is_customer', '是否是客服', 1)->options([['value' => 1, 'label' => '开启'], ['value' => 0, 'label' => '关闭']])->appendControl(1, [
  557. $this->builder->input('customer_phone', '客服手机号码')->col(24),
  558. $this->builder->frameImage('customer_url', '客服二维码', $this->url(config('admin.store_prefix') . '/widget.images/index', ['fodder' => 'customer_url'], true))->icon('ios-add')->width('960px')->height('505px')->modal(['footer-hide' => true])
  559. ]);
  560. $field[] = $this->builder->radio('notify', '通知开关', 0)->options([['value' => 1, 'label' => '开启'], ['value' => 0, 'label' => '关闭']]);
  561. $field[] = $this->builder->radio('status', '状态', 1)->options([['value' => 1, 'label' => '开启'], ['value' => 0, 'label' => '关闭']]);
  562. return create_form('添加门店店员', $field, $this->url('/staff/staff'));
  563. }
  564. /**
  565. * 编辑门店店员
  566. * @param $id
  567. * @return array
  568. * @throws \FormBuilder\Exception\FormBuilderException
  569. * @throws \think\db\exception\DataNotFoundException
  570. * @throws \think\db\exception\DbException
  571. * @throws \think\db\exception\ModelNotFoundException
  572. */
  573. public function updateStoreStaffForm($id, $level)
  574. {
  575. $staffInfo = $this->dao->get($id);
  576. if (!$staffInfo) {
  577. throw new AdminException('门店店员不存在!');
  578. }
  579. if ($staffInfo->is_del) {
  580. throw new AdminException('门店店员已经删除');
  581. }
  582. $field[] = $this->builder->input('staff_name', '店员名称', $staffInfo['staff_name'])->col(24)->required('请填写门店店员名称');
  583. if ($staffInfo['uid']) {
  584. $field[] = $this->builder->frameImage('avatar', '店员头像', $this->url(config('admin.store_prefix') . '/widget.images/index', ['fodder' => 'avatar'], true), $staffInfo['avatar'] ?? '')->icon('ios-add')->width('960px')->height('505px')->modal(['footer-hide' => true]);
  585. } else {//没绑定过商城用户
  586. $field[] = $this->builder->frameImage('image', '商城用户', $this->url(config('admin.store_prefix') . '/system.User/list', ['fodder' => 'image'], true))->icon('ios-add')->width('960px')->height('450px')->modal(['footer-hide' => true])->Props(['srcKey' => 'image']);
  587. $field[] = $this->builder->hidden('uid', 0);
  588. $field[] = $this->builder->hidden('avatar', '');
  589. }
  590. $field[] = $this->builder->input('account', '店员账号', $staffInfo['account'])->maxlength(35)->required('请填写门店店员账号');
  591. $field[] = $this->builder->input('pwd', '店员密码')->placeholder('不更改密码请留空')->type('password');
  592. $field[] = $this->builder->input('conf_pwd', '确认密码')->placeholder('不更改密码请留空')->type('password');
  593. $field[] = $this->builder->input('phone', '手机号码', $staffInfo['phone'])->col(24)->required('请输入手机号');
  594. /** @var SystemRoleServices $service */
  595. $service = app()->make(SystemRoleServices::class);
  596. $options = $service->getRoleFormSelect($level, 1, (int)$staffInfo['store_id']);
  597. $roles = [];
  598. if ($staffInfo && isset($staffInfo['roles']) && $staffInfo['roles']) {
  599. foreach ($staffInfo['roles'] as $role) {
  600. $roles[] = (int)$role;
  601. }
  602. }
  603. $field[] = $this->builder->select('roles', '店员身份', $roles)->setOptions(FormBuilder::setOptions($options))->multiple(true)->required('请选择店员身份');
  604. $field[] = $this->builder->radio('is_manager', '是否是店长', (int)$staffInfo['is_manager'])->options([['value' => 1, 'label' => '开启'], ['value' => 0, 'label' => '关闭']]);
  605. $field[] = $this->builder->radio('order_status', '订单管理', (int)$staffInfo['order_status'])->options([['value' => 1, 'label' => '开启'], ['value' => 0, 'label' => '关闭']]);
  606. $field[] = $this->builder->radio('verify_status', '核销开关', (int)$staffInfo['verify_status'])->options([['value' => 1, 'label' => '开启'], ['value' => 0, 'label' => '关闭']]);
  607. $field[] = $this->builder->radio('is_cashier', '是否是收银员', (int)$staffInfo['is_cashier'])->options([['value' => 1, 'label' => '开启'], ['value' => 0, 'label' => '关闭']]);
  608. $field[] = $this->builder->radio('is_customer', '是否是客服', (int)$staffInfo['is_customer'])->options([['value' => 1, 'label' => '开启'], ['value' => 0, 'label' => '关闭']])->appendControl(1, [
  609. $this->builder->input('customer_phone', '客服手机号码', $staffInfo['customer_phone'] ?? '')->col(24),
  610. $this->builder->frameImage('customer_url', '客服二维码', $this->url(config('admin.store_prefix') . '/widget.images/index', ['fodder' => 'customer_url'], true), $staffInfo['customer_url'] ?? '')->icon('ios-add')->width('960px')->height('505px')->modal(['footer-hide' => true])
  611. ]);
  612. $field[] = $this->builder->radio('notify', '通知开关', (int)$staffInfo['notify'])->options([['value' => 1, 'label' => '开启'], ['value' => 0, 'label' => '关闭']]);
  613. $field[] = $this->builder->radio('status', '状态', (int)$staffInfo['status'])->options([['value' => 1, 'label' => '开启'], ['value' => 0, 'label' => '关闭']]);
  614. return create_form('编辑门店店员', $field, $this->url('/staff/staff/' . $id), 'put');
  615. }
  616. /**
  617. * 获取店员select
  618. * @param array $where
  619. * @return mixed
  620. */
  621. public function getSelectList($where = [])
  622. {
  623. $list = $this->dao->getSelectList($where);
  624. $menus = [];
  625. foreach ($list as $menu) {
  626. $menus[] = ['value' => $menu['id'], 'label' => $menu['staff_name'] ?? ''];
  627. }
  628. return $menus;
  629. }
  630. /**
  631. * 首页店员统计
  632. * @param int $store_id
  633. * @param array $time
  634. * @return array
  635. */
  636. public function staffChart(int $store_id, array $time)
  637. {
  638. $list = $this->dao->getStoreStaffList(['store_id' => $store_id, 'is_del' => 0], 'id,uid,avatar,staff_name');
  639. if ($list) {
  640. /** @var UserSpreadServices $userSpreadServices */
  641. $userSpreadServices = app()->make(UserSpreadServices::class);
  642. /** @var BranchOrderServices $orderServices */
  643. $orderServices = app()->make(BranchOrderServices::class);
  644. /** @var OtherOrderServices $otherOrderServices */
  645. $otherOrderServices = app()->make(OtherOrderServices::class);
  646. $where = ['store_id' => $store_id, 'time' => $time];
  647. $order_where = ['paid' => 1, 'pid' => 0, 'is_del' => 0, 'is_system_del' => 0, 'refund_status' => [0, 3]];
  648. $staffIds = array_unique(array_column($list, 'id'));
  649. $otherStaff = $otherOrderServices->preStaffTotal($where + ['staff_id' => $staffIds, 'paid' => 1, 'type' => [0, 1, 2, 4]], 'distinct(`uid`)');
  650. $otherStaff = array_combine(array_column($otherStaff, 'staff_id'), $otherStaff);
  651. foreach ($list as &$item) {
  652. $staff_where = ['staff_id' => $item['id']];
  653. $spread_uid = $userSpreadServices->getColumn($where + ['timeKey' => 'spread_time'] + $staff_where, 'uid', '', true);
  654. $item['spread_count'] = count($spread_uid);
  655. $item['speread_order_price'] = 0;
  656. if ($spread_uid) {
  657. $item['speread_order_price'] = $orderServices->sum($where + $order_where + ['uid' => $spread_uid], 'pay_price', true);
  658. }
  659. $item['vip_count'] = $otherStaff[$item['id']]['count'] ?? 0;
  660. $item['vip_price'] = $otherStaff[$item['id']]['price'] ?? 0;
  661. unset($spread);
  662. }
  663. }
  664. return $list;
  665. }
  666. /**
  667. * 修改当前店员信息
  668. * @param int $id
  669. * @param array $data
  670. * @return bool
  671. */
  672. public function updateStaffPwd(int $id, array $data)
  673. {
  674. $staffInfo = $this->dao->get($id);
  675. if (!$staffInfo)
  676. throw new AdminException('店员信息未查到');
  677. if ($staffInfo->is_del) {
  678. throw new AdminException('店员已经删除');
  679. }
  680. if ($data['real_name']) {
  681. $staffInfo->staff_name = $data['real_name'];
  682. }
  683. if ($data['avatar']) {
  684. $staffInfo->avatar = $data['avatar'];
  685. }
  686. if ($data['pwd']) {
  687. if (!password_verify($data['pwd'], $staffInfo['pwd']))
  688. throw new AdminException('原始密码错误');
  689. if (!$data['new_pwd'])
  690. throw new AdminException('请输入新密码');
  691. if (!$data['conf_pwd'])
  692. throw new AdminException('请输入确认密码');
  693. if ($data['new_pwd'] != $data['conf_pwd'])
  694. throw new AdminException('两次输入的密码不一致');
  695. $staffInfo->pwd = $this->passwordHash($data['new_pwd']);
  696. }
  697. if ($staffInfo->save())
  698. return true;
  699. else
  700. return false;
  701. }
  702. /**
  703. * 获取门店接收通知店员
  704. * @param int $store_id
  705. * @param string $field
  706. * @return array
  707. * @throws \think\db\exception\DataNotFoundException
  708. * @throws \think\db\exception\DbException
  709. * @throws \think\db\exception\ModelNotFoundException
  710. */
  711. public function getNotifyStoreStaffList(int $store_id, string $field = '*')
  712. {
  713. $where = [
  714. 'store_id' => $store_id,
  715. 'status' => 1,
  716. 'is_del' => 0,
  717. 'notify' => 1
  718. ];
  719. $list = $this->dao->getStoreStaffList($where, $field);
  720. return $list;
  721. }
  722. /**
  723. * 获取所有门店有用的手机号
  724. * @return array
  725. */
  726. public function getPhoneAll()
  727. {
  728. $phone = array_merge(array_unique($this->dao->getColumn([
  729. ['is_del', '=', 0],
  730. ['status', '=', 1],
  731. ], 'phone')));
  732. return $phone ?: [0];
  733. }
  734. }