DivisionServices.php 35 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722
  1. <?php
  2. namespace app\services\agent;
  3. use app\services\BaseServices;
  4. use app\services\order\StoreOrderServices;
  5. use app\services\other\QrcodeServices;
  6. use app\services\system\admin\SystemAdminServices;
  7. use app\services\system\admin\SystemRoleServices;
  8. use app\services\user\UserServices;
  9. use crmeb\exceptions\AdminException;
  10. use crmeb\exceptions\ApiException;
  11. use crmeb\services\FormBuilder as Form;
  12. use think\db\exception\DataNotFoundException;
  13. use think\db\exception\DbException;
  14. use think\db\exception\ModelNotFoundException;
  15. use think\facade\Route;
  16. class DivisionServices extends BaseServices
  17. {
  18. /**
  19. * 获取事业部/代理/员工列表
  20. * @param array $where
  21. * @return array
  22. * @throws \think\db\exception\DataNotFoundException
  23. * @throws \think\db\exception\DbException
  24. * @throws \think\db\exception\ModelNotFoundException
  25. */
  26. public function getDivisionList(array $where = [])
  27. {
  28. /** @var UserServices $userServices */
  29. $userServices = app()->make(UserServices::class);
  30. $data = $userServices->getDivisionList($where + ['status' => 1], 'uid,nickname,avatar,division_name,division_percent,division_end_time,division_status,division_invite');
  31. foreach ($data['list'] as &$item) {
  32. $item['division_end_time'] = date('Y-m-d', $item['division_end_time']);
  33. $item['agent_count'] = $userServices->count([
  34. $where['division_type'] == 1 ? 'division_id' : 'agent_id' => $item['uid'],
  35. 'division_type' => $where['division_type'] + 1,
  36. 'status' => 1,
  37. 'is_del' => 0
  38. ]);
  39. unset($item['label']);
  40. }
  41. return $data;
  42. }
  43. /**
  44. * 下级列表
  45. * @param $type
  46. * @param $uid
  47. * @return array
  48. * @throws \think\db\exception\DataNotFoundException
  49. * @throws \think\db\exception\DbException
  50. * @throws \think\db\exception\ModelNotFoundException
  51. */
  52. public function divisionDownList($type, $uid)
  53. {
  54. /** @var UserServices $userServices */
  55. $userServices = app()->make(UserServices::class);
  56. $where = [
  57. $type == 2 ? 'division_id' : 'agent_id' => $uid,
  58. 'division_type' => $type
  59. ];
  60. $where['status'] = 1;
  61. $where['is_del'] = 0;
  62. $data = $userServices->getDivisionList($where, 'uid,nickname,avatar,division_name,division_percent,division_end_time,division_status');
  63. foreach ($data['list'] as &$item) {
  64. $item['agent_count'] = $userServices->count([
  65. 'agent_id' => $item['uid'],
  66. 'division_type' => $type + 1,
  67. 'status' => 1
  68. ]);
  69. unset($item['label']);
  70. }
  71. return $data;
  72. }
  73. /**
  74. * 添加编辑事业部表单
  75. * @param $uid
  76. * @return array
  77. * @throws \FormBuilder\Exception\FormBuilderException
  78. */
  79. public function getDivisionForm($uid)
  80. {
  81. /** @var UserServices $userServices */
  82. $userServices = app()->make(UserServices::class);
  83. /** @var SystemAdminServices $adminService */
  84. $adminService = app()->make(SystemAdminServices::class);
  85. $userInfo = $userServices->getUserInfo($uid);
  86. if ($uid && !$userInfo) throw new AdminException(100100);
  87. if ($uid) {
  88. $adminInfo = $adminService->getInfo(['division_id' => $uid])->toArray();
  89. if (isset($adminInfo['roles'])) {
  90. foreach ($adminInfo['roles'] as &$item) {
  91. $item = intval($item);
  92. }
  93. }
  94. }
  95. $field = [];
  96. $title = '事业部';
  97. $field[] = Form::input('division_name', '事业部名称', $userInfo['division_name'] ?? '')->required('请输入事业部名称');
  98. if ($uid) {
  99. $field[] = Form::hidden('uid', $uid);
  100. } else {
  101. $field[] = Form::frameImage('image', '关联用户', $this->url(config('app.admin_prefix', 'admin') . '/system.user/list', ['fodder' => 'image'], true))->icon('el-icon-user')->width('950px')->height('560px')->Props(['srcKey' => 'image', 'footer' => false]);
  102. }
  103. $field[] = Form::hidden('aid', $adminInfo['id'] ?? 0);
  104. $field[] = Form::number('division_percent', '佣金比例', $userInfo['division_percent'] ?? '')->placeholder('区域代理佣金比例1-100')->info('填写1-100,如填写50代表返佣50%')->style(['width' => '173px'])->min(0)->max(100)->required();
  105. $field[] = Form::date('division_end_time', '到期时间', ($userInfo['division_end_time'] ?? '') != 0 ? date('Y-m-d H:i:s', $userInfo['division_end_time']) : '')->placeholder('区域代理到期时间');
  106. $field[] = Form::radio('division_status', '代理状态', $userInfo['division_status'] ?? 1)->options([['label' => '开通', 'value' => 1], ['label' => '关闭', 'value' => 0]]);
  107. $field[] = Form::input('account', '管理账号', $adminInfo['account'] ?? '')->required('请填写管理员账号');
  108. $field[] = Form::input('pwd', '管理密码')->type('password')->placeholder('请填写管理员密码');
  109. $field[] = Form::input('conf_pwd', '确认密码')->type('password')->placeholder('请输入确认密码');
  110. /** @var SystemRoleServices $service */
  111. $service = app()->make(SystemRoleServices::class);
  112. $options = $service->getRoleFormSelect(1);
  113. $field[] = Form::select('roles', '管理员身份', $adminInfo['roles'] ?? [])->setOptions(Form::setOptions($options))->multiple(true)->required('请选择管理员身份');
  114. return create_form($title, $field, Route::buildUrl('/agent/division/save'), 'POST');
  115. }
  116. /**
  117. * 保存事业部数据
  118. * @param $data
  119. * @return mixed
  120. */
  121. public function divisionSave($data)
  122. {
  123. if ((int)$data['uid'] == 0) $data['uid'] = $data['image']['uid'];
  124. if ((int)$data['uid'] == 0) throw new AdminException(400450);
  125. /** @var UserServices $userServices */
  126. $userServices = app()->make(UserServices::class);
  127. if ($data['aid'] == 0) {
  128. $userInfo = $userServices->getUserInfo($data['uid'], 'is_division,is_agent,is_staff');
  129. if (!$userInfo) throw new AdminException('用户不存在');
  130. if ($userInfo['is_division']) throw new AdminException('此用户是事业部,请勿重复添加');
  131. if ($userInfo['is_agent']) throw new AdminException('此用户是代理商,无法添加为事业部');
  132. if ($userInfo['is_staff']) throw new AdminException('此用户是下级员工,无法添加为事业部');
  133. }
  134. $uid = $data['uid'];
  135. $aid = $data['aid'];
  136. $agentData = [
  137. 'division_percent' => $data['division_percent'],
  138. 'division_end_time' => strtotime($data['division_end_time']),
  139. 'division_change_time' => time(),
  140. 'is_division' => 1,
  141. 'is_agent' => 0,
  142. 'is_staff' => 0,
  143. 'division_id' => $uid,
  144. 'agent_id' => 0,
  145. 'staff_id' => 0,
  146. 'division_type' => 1,
  147. 'division_status' => $data['division_status'],
  148. 'spread_uid' => 0,
  149. 'spread_time' => 0,
  150. 'division_name' => $data['division_name'],
  151. 'is_promoter' => 1
  152. ];
  153. $adminData = [
  154. 'account' => $data['account'],
  155. 'pwd' => $data['pwd'],
  156. 'conf_pwd' => $data['conf_pwd'],
  157. 'real_name' => $data['division_name'],
  158. 'roles' => $data['roles'],
  159. 'status' => 1,
  160. 'level' => 1,
  161. 'division_id' => $uid
  162. ];
  163. return $this->transaction(function () use ($uid, $agentData, $adminData, $aid, $userServices) {
  164. $agentData['division_invite'] = $userServices->value(['uid' => $uid], 'division_invite') ?: rand(10000000, 99999999);
  165. $userServices->update($uid, $agentData);
  166. /** @var SystemAdminServices $adminService */
  167. $adminService = app()->make(SystemAdminServices::class);
  168. if (!$aid) {
  169. if ($adminData['pwd']) {
  170. if (!$adminData['conf_pwd']) throw new AdminException(400263);
  171. if ($adminData['pwd'] != $adminData['conf_pwd']) throw new AdminException(400264);
  172. $adminService->create($adminData);
  173. } else {
  174. throw new AdminException(400263);
  175. }
  176. } else {
  177. $adminInfo = $adminService->get($aid);
  178. if (!$adminInfo)
  179. throw new AdminException(400451);
  180. if ($adminInfo->is_del) {
  181. throw new AdminException(400452);
  182. }
  183. if (!$adminData['real_name'])
  184. throw new AdminException(400453);
  185. if ($adminData['pwd']) {
  186. if (!$adminData['conf_pwd']) throw new AdminException(400263);
  187. if ($adminData['pwd'] != $adminData['conf_pwd']) throw new AdminException(400264);
  188. $adminInfo->pwd = $this->passwordHash($adminData['pwd']);
  189. }
  190. $adminInfo->real_name = $adminData['real_name'];
  191. $adminInfo->account = $adminData['account'];
  192. $adminInfo->roles = implode(',', $adminData['roles']);
  193. if ($adminInfo->save())
  194. return true;
  195. else
  196. return false;
  197. }
  198. return true;
  199. });
  200. }
  201. // /**
  202. // * 生成邀请码
  203. // * @return false|string
  204. // */
  205. // public function getDivisionInvite()
  206. // {
  207. // /** @var UserServices $userServices */
  208. // $userServices = app()->make(UserServices::class);
  209. // list($msec, $sec) = explode(' ', microtime());
  210. // $num = time() + mt_rand(10, 999999) . '' . substr($msec, 2, 3);//生成随机数
  211. // if (strlen($num) < 12)
  212. // $num = str_pad((string)$num, 8, 0, STR_PAD_RIGHT);
  213. // else
  214. // $num = substr($num, 0, 8);
  215. // if ($userServices->count(['division_invite' => $num])) {
  216. // return $this->getDivisionInvite();
  217. // }
  218. // return $num;
  219. // }
  220. /**
  221. * 添加编辑代理商
  222. * @param $uid
  223. * @return array
  224. * @throws \FormBuilder\Exception\FormBuilderException
  225. */
  226. public function getDivisionAgentForm($uid)
  227. {
  228. /** @var UserServices $userService */
  229. $userService = app()->make(UserServices::class);
  230. $userInfo = $userService->get($uid);
  231. if ($uid && !$userInfo) throw new AdminException(400214);
  232. $field = [];
  233. $options = [];
  234. $divisionList = $userService->getDivisionList(['status' => 1, 'division_type' => 1], 'uid,division_name');
  235. foreach ($divisionList['list'] as $item) {
  236. $options[] = ['value' => $item['uid'], 'label' => $item['division_name']];
  237. }
  238. $field[] = Form::input('division_name', '代理商名称', $userInfo['division_name'] ?? '')->required('请输入代理商名称');
  239. if ($uid) {
  240. $field[] = Form::hidden('uid', $uid);
  241. $field[] = Form::hidden('edit', 1);
  242. $field[] = Form::hidden('division_id', $userInfo['division_id']);
  243. } else {
  244. $field[] = Form::select('division_id', '上级事业部', '')->setOptions(Form::setOptions($options))->filterable(1);
  245. $field[] = Form::frameImage('image', '关联用户', $this->url(config('app.admin_prefix', 'admin') . '/system.user/list', ['fodder' => 'image'], true))->icon('el-icon-user')->width('950px')->height('560px')->Props(['srcKey' => 'image', 'footer' => false]);
  246. $field[] = Form::hidden('edit', 0);
  247. }
  248. $field[] = Form::number('division_percent', '佣金比例', $userInfo['division_percent'] ?? '')->placeholder('代理商佣金比例1-100')->info('填写1-100,如填写50代表返佣50%,但是不能高于上级事业部的比例')->style(['width' => '173px'])->min(0)->max(100)->required();
  249. $field[] = Form::date('division_end_time', '到期时间', ($userInfo['division_end_time'] ?? '') != 0 ? date('Y-m-d H:i:s', $userInfo['division_end_time']) : '')->placeholder('代理商代理到期时间');
  250. $field[] = Form::radio('division_status', '代理状态', $userInfo['division_status'] ?? 1)->options([['label' => '开通', 'value' => 1], ['label' => '关闭', 'value' => 0]]);
  251. return create_form('代理商', $field, Route::buildUrl('/agent/division/agent/save'), 'POST');
  252. }
  253. /**
  254. * 保存代理商
  255. * @param $data
  256. * @return bool
  257. * @throws \think\db\exception\DataNotFoundException
  258. * @throws \think\db\exception\DbException
  259. * @throws \think\db\exception\ModelNotFoundException
  260. */
  261. public function divisionAgentSave($data)
  262. {
  263. /** @var UserServices $userServices */
  264. $userServices = app()->make(UserServices::class);
  265. $uid = $data['uid'];
  266. $agentData = [
  267. 'spread_uid' => $data['division_id'],
  268. 'spread_time' => time(),
  269. 'division_id' => $data['division_id'],
  270. 'division_status' => $data['division_status'],
  271. 'division_percent' => $data['division_percent'],
  272. 'division_change_time' => time(),
  273. 'division_end_time' => strtotime($data['division_end_time']),
  274. 'division_type' => 2,
  275. 'is_agent' => 1,
  276. 'agent_id' => $uid,
  277. 'is_staff' => 0,
  278. 'staff_id' => 0,
  279. 'division_name' => $data['division_name'],
  280. 'is_promoter' => 1
  281. ];
  282. $division_info = $userServices->getUserInfo($data['division_id'], 'division_end_time,division_percent');
  283. if ($division_info) {
  284. if ($agentData['division_percent'] > $division_info['division_percent']) throw new AdminException(400448);
  285. if ($agentData['division_end_time'] > $division_info['division_end_time']) throw new AdminException(400449);
  286. }
  287. $res = $userServices->update($uid, $agentData);
  288. if ($res) return true;
  289. throw new AdminException('保存失败');
  290. }
  291. /**
  292. * 修改状态
  293. * @param $status
  294. * @param $uid
  295. * @return bool
  296. */
  297. public function setDivisionStatus($status, $uid)
  298. {
  299. /** @var UserServices $userServices */
  300. $userServices = app()->make(UserServices::class);
  301. /** @var SystemAdminServices $adminServices */
  302. $adminServices = app()->make(SystemAdminServices::class);
  303. $res = $userServices->update($uid, ['division_status' => $status]);
  304. $res = $res && $adminServices->update(['division_id' => $uid], ['status' => $status]);
  305. if ($res) {
  306. return true;
  307. } else {
  308. throw new AdminException(100005);
  309. }
  310. }
  311. /**
  312. * 删除事业部/代理商
  313. * @param $type
  314. * @param $uid
  315. * @return mixed
  316. */
  317. public function delDivision($type, $uid)
  318. {
  319. return $this->transaction(function () use ($type, $uid) {
  320. /** @var UserServices $userServices */
  321. $userServices = app()->make(UserServices::class);
  322. $userInfo = $userServices->getUserInfo($uid);
  323. if (!$userInfo) throw new AdminException('用户不存在');
  324. $userInfo = $userInfo->toArray();
  325. $data = [
  326. 'division_name' => '',
  327. 'division_type' => 0,
  328. 'division_status' => 0,
  329. 'is_division' => 0,
  330. 'is_agent' => 0,
  331. 'is_staff' => 0,
  332. 'division_id' => 0,
  333. 'agent_id' => 0,
  334. 'staff_id' => 0,
  335. 'division_percent' => 0,
  336. 'division_end_time' => 0,
  337. 'division_change_time' => time(),
  338. 'division_invite' => 0
  339. ];
  340. $userServices->update(['uid' => $uid], $data);
  341. if ($userInfo['division_type'] == 1) {
  342. app()->make(SystemAdminServices::class)->delete(['division_id' => $uid]);
  343. $userServices->update(['division_id' => $uid], $data);
  344. } elseif ($userInfo['division_type'] == 2) {
  345. app()->make(DivisionAgentApplyServices::class)->delete(['uid' => $uid]);
  346. $userServices->update(['agent_id' => $uid], $data);
  347. } elseif ($userInfo['division_type'] == 3) {
  348. $userServices->update(['staff_id' => $uid], $data);
  349. }
  350. });
  351. }
  352. /**
  353. * 后台添加员工
  354. * @param $uid
  355. * @return array
  356. * @throws \FormBuilder\Exception\FormBuilderException
  357. * @author 吴汐
  358. * @email 442384644@qq.com
  359. * @date 2024/1/22
  360. */
  361. public function getDivisionStaffForm($uid)
  362. {
  363. $field = [];
  364. $field[] = Form::frameImage('image', '员工', $this->url(config('app.admin_prefix', 'admin') . '/system.user/list', ['fodder' => 'image'], true))->icon('el-icon-user')->width('950px')->height('560px')->Props(['srcKey' => 'image', 'footer' => false]);
  365. $field[] = Form::number('division_percent', '佣金比例', '')->placeholder('员工佣金比例1-100')->info('填写1-100,如填写50代表返佣50%,但是不能高于上级代理商的比例')->style(['width' => '173px'])->min(0)->max(100)->required();
  366. $field[] = Form::hidden('agent_id', $uid);
  367. return create_form('员工', $field, Route::buildUrl('/agent/division/staff/save'), 'POST');
  368. }
  369. /**
  370. * 保存员工
  371. * @param $data
  372. * @return true
  373. * @throws \think\db\exception\DataNotFoundException
  374. * @throws \think\db\exception\DbException
  375. * @throws \think\db\exception\ModelNotFoundException
  376. * @author 吴汐
  377. * @email 442384644@qq.com
  378. * @date 2024/1/22
  379. */
  380. public function divisionStaffSave($data)
  381. {
  382. $data['uid'] = $data['image']['uid'];
  383. /** @var UserServices $userServices */
  384. $userServices = app()->make(UserServices::class);
  385. $userInfo = $userServices->getUserInfo($data['uid'], 'is_division,is_agent,is_staff,division_id,agent_id,staff_id,division_end_time,division_percent');
  386. if (!$userInfo) throw new AdminException('用户不存在');
  387. if ($userInfo['is_division']) throw new AdminException('此用户是事业部,无法绑定为员工');
  388. if ($userInfo['is_agent']) throw new AdminException('此用户是代理商,无法绑定为员工');
  389. if ($userInfo['is_staff'] && $userInfo['agent_id'] == $data['agent_id']) throw new AdminException('此用户是您的员工,请勿重复添加');
  390. $agentInfo = $userServices->getUserInfo($data['agent_id'], 'division_id,agent_id,division_end_time,division_percent');
  391. $staffData = [
  392. 'spread_uid' => $data['agent_id'],
  393. 'spread_time' => time(),
  394. 'division_type' => 3,
  395. 'division_status' => 1,
  396. 'is_staff' => 1,
  397. 'division_id' => $agentInfo['division_id'],
  398. 'agent_id' => $agentInfo['agent_id'],
  399. 'staff_id' => $data['uid'],
  400. 'division_percent' => $data['division_percent'],
  401. 'division_change_time' => time(),
  402. 'division_end_time' => $agentInfo['division_end_time'],
  403. 'is_promoter' => 1
  404. ];
  405. if ($staffData['division_percent'] > $agentInfo['division_percent']) throw new AdminException(400448);
  406. if ($userInfo['agent_id'] != 0 && $userInfo['agent_id'] != $agentInfo['agent_id']) {
  407. $userServices->update(['staff_id' => $userInfo['uid'], 'spread_uid' => $userInfo['uid']], ['spread_uid' => $agentInfo['agent_id'], 'staff_id' => 0]);
  408. $userServices->update(['staff_id' => $userInfo['uid'], 'not_spread_uid' => $userInfo['uid']], ['staff_id' => 0]);
  409. }
  410. $res = $userServices->update($data['uid'], $staffData);
  411. if ($res) return true;
  412. throw new AdminException('保存失败');
  413. }
  414. /**
  415. * 扫码绑定员工
  416. * @param $uid
  417. * @param int $agentId
  418. * @param int $agentCode
  419. * @return string
  420. * @throws DataNotFoundException
  421. * @throws DbException
  422. * @throws ModelNotFoundException
  423. * @author 吴汐
  424. * @email 442384644@qq.com
  425. * @date 2024/2/2
  426. */
  427. public function agentSpreadStaff($uid, int $agentId = 0, int $agentCode = 0)
  428. {
  429. if ($agentCode && !$agentId) {
  430. /** @var QrcodeServices $qrCode */
  431. $qrCode = app()->make(QrcodeServices::class);
  432. if ($info = $qrCode->getOne(['id' => $agentCode, 'third_type' => 'agent', 'status' => 1])) {
  433. $agentId = $info['third_id'];
  434. }
  435. }
  436. if (!$agentId) return false;
  437. if ($uid == $agentId) return '自己不能推荐自己';
  438. /** @var UserServices $userServices */
  439. $userServices = app()->make(UserServices::class);
  440. $agentInfo = $userServices->getUserInfo($agentId, 'division_id,agent_id,division_end_time,division_percent');
  441. if (!$agentInfo) return '上级用户不存在';
  442. $userInfo = $userServices->getUserInfo($uid, 'is_division,is_agent,is_staff,division_id,agent_id,staff_id,division_end_time,division_percent');
  443. if (!$userInfo) return '用户不存在';
  444. if ($userInfo['is_division']) return '您是事业部,不能绑定成为别人的员工';
  445. if ($userInfo['is_agent']) return '您是代理商,不能绑定成为别人的员工';
  446. $staffData = [
  447. 'spread_uid' => $agentId,
  448. 'spread_time' => time(),
  449. 'division_type' => 3,
  450. 'division_status' => 1,
  451. 'is_staff' => 1,
  452. 'division_id' => $agentInfo['division_id'],
  453. 'agent_id' => $agentInfo['agent_id'],
  454. 'staff_id' => $uid,
  455. 'division_change_time' => time(),
  456. 'is_promoter' => 1
  457. ];
  458. if ($userInfo['agent_id'] != 0 && $userInfo['agent_id'] != $agentInfo['agent_id']) {
  459. $userServices->update(['staff_id' => $userInfo['uid'], 'spread_uid' => $userInfo['uid']], ['spread_uid' => $agentInfo['agent_id'], 'staff_id' => 0]);
  460. $userServices->update(['staff_id' => $userInfo['uid'], 'not_spread_uid' => $userInfo['uid']], ['staff_id' => 0]);
  461. }
  462. $res = $userServices->update($uid, $staffData);
  463. if ($res) return '绑定员工成功';
  464. return '绑定员工失败';
  465. }
  466. /**
  467. * 获取返佣比例佣金比例
  468. * 当前方法会将获得的佣金逐步的递减
  469. * @param $uid
  470. * @param $storeBrokerageRatio
  471. * @param $storeBrokerageRatioTwo
  472. * @param $isSelfBrokerage
  473. * @return array
  474. */
  475. public function getDivisionPercent($uid, $storeBrokerageRatio, $storeBrokerageRatioTwo, $isSelfBrokerage)
  476. {
  477. $division_open = (int)sys_config('division_status', 1);
  478. if (!$division_open) {
  479. /** 代理商关闭 */
  480. $storeBrokerageOne = $storeBrokerageRatio;
  481. $storeBrokerageTwo = $storeBrokerageRatioTwo;
  482. $staffPercent = 0;
  483. $agentPercent = 0;
  484. $divisionPercent = 0;
  485. } else {
  486. /** @var UserServices $userServices */
  487. $userServices = app()->make(UserServices::class);
  488. $userInfo = $userServices->get($uid);
  489. if ($userInfo['is_division'] == 1) {
  490. /** 自己是事业部 */
  491. $storeBrokerageOne = 0;
  492. $storeBrokerageTwo = 0;
  493. $staffPercent = 0;
  494. $agentPercent = 0;
  495. if ($userInfo['division_status'] == 1 && $userInfo['division_end_time'] > time()) {
  496. $divisionPercent = $isSelfBrokerage ? $userInfo['division_percent'] : 0;
  497. } else {
  498. $divisionPercent = 0;
  499. }
  500. } elseif ($userInfo['is_agent'] == 1) {
  501. /** 自己是代理商 */
  502. $divisionInfo = $userServices->get($userInfo['division_id']);
  503. $storeBrokerageOne = 0;
  504. $storeBrokerageTwo = 0;
  505. $staffPercent = 0;
  506. if ($userInfo['division_status'] == 1 && $userInfo['division_end_time'] > time()) {
  507. $agentPercent = $isSelfBrokerage ? $userInfo['division_percent'] : 0;
  508. } else {
  509. $agentPercent = 0;
  510. }
  511. if ($divisionInfo['division_status'] == 1 && $divisionInfo['division_end_time'] > time()) {
  512. $divisionPercent = bcsub($divisionInfo['division_percent'], $agentPercent, 2);
  513. $divisionPercent = $divisionPercent < 0 ? 0 : $divisionPercent;
  514. } else {
  515. $divisionPercent = 0;
  516. }
  517. } elseif ($userInfo['is_staff'] == 1) { // 自己是员工
  518. /** 自己是员工 */
  519. $agentInfo = $userServices->get($userInfo['agent_id']);
  520. $divisionInfo = $userServices->get($userInfo['division_id']);
  521. $storeBrokerageOne = 0;
  522. $storeBrokerageTwo = 0;
  523. if ($userInfo['division_status'] == 1 && $userInfo['division_end_time'] > time()) {
  524. $staffPercent = $isSelfBrokerage ? $userInfo['division_percent'] : 0;
  525. } else {
  526. $staffPercent = 0;
  527. }
  528. if ($agentInfo['division_status'] == 1 && $agentInfo['division_end_time'] > time()) {
  529. $agentPercent = bcsub($agentInfo['division_percent'], $staffPercent, 2);
  530. $agentPercent = $agentPercent < 0 ? 0 : $agentPercent;
  531. } else {
  532. $agentPercent = 0;
  533. }
  534. if ($divisionInfo['division_status'] == 1 && $divisionInfo['division_end_time'] > time()) {
  535. $divisionPercent = bcsub($divisionInfo['division_percent'], bcadd($staffPercent, $agentPercent, 2), 2);
  536. $divisionPercent = $divisionPercent < 0 ? 0 : $divisionPercent;
  537. } else {
  538. $divisionPercent = 0;
  539. }
  540. } else {
  541. /** 自己是普通用户 */
  542. $staffInfo = $userServices->get($userInfo['staff_id']);
  543. $agentInfo = $userServices->get($userInfo['agent_id']);
  544. $divisionInfo = $userServices->get($userInfo['division_id']);
  545. if ($userInfo['staff_id']) {
  546. /** 该用户为员工推广 */
  547. if ($userInfo['staff_id'] == $userInfo['spread_uid']) {
  548. /** 员工直接下级 */
  549. $storeBrokerageOne = $isSelfBrokerage ? $storeBrokerageRatio : 0;
  550. $storeBrokerageTwo = 0;
  551. if ($staffInfo['division_status'] == 1 && $staffInfo['division_end_time'] > time()) {
  552. $staffPercent = bcsub($staffInfo['division_percent'], $storeBrokerageOne, 2);
  553. $staffPercent = $staffPercent < 0 ? 0 : $staffPercent;
  554. } else {
  555. $staffPercent = 0;
  556. }
  557. if ($agentInfo['division_status'] == 1 && $agentInfo['division_end_time'] > time()) {
  558. $agentPercent = bcsub($agentInfo['division_percent'], bcadd($storeBrokerageOne, $staffPercent, 2), 2);
  559. $agentPercent = $agentPercent < 0 ? 0 : $agentPercent;
  560. } else {
  561. $agentPercent = 0;
  562. }
  563. if ($divisionInfo['division_status'] == 1 && $divisionInfo['division_end_time'] > time()) {
  564. $divisionPercent = bcsub($divisionInfo['division_percent'], bcadd(bcadd($storeBrokerageOne, $staffPercent, 2), $agentPercent, 2), 2);
  565. $divisionPercent = $divisionPercent < 0 ? 0 : $divisionPercent;
  566. } else {
  567. $divisionPercent = 0;
  568. }
  569. } else {
  570. $storeBrokerageOne = $storeBrokerageRatio;
  571. $storeBrokerageTwo = $userServices->value(['uid' => $userInfo['spread_uid']], 'spread_uid') == $userInfo['staff_id'] && !$isSelfBrokerage ? 0 : $storeBrokerageRatioTwo;
  572. $brokerageOneTwo = bcadd($storeBrokerageOne, $storeBrokerageTwo, 2);
  573. if ($staffInfo['division_status'] == 1 && $staffInfo['division_end_time'] > time()) {
  574. $staffPercent = bcsub($staffInfo['division_percent'], $brokerageOneTwo, 2);
  575. $staffPercent = $staffPercent < 0 ? 0 : $staffPercent;
  576. } else {
  577. $staffPercent = 0;
  578. }
  579. if ($agentInfo['division_status'] == 1 && $agentInfo['division_end_time'] > time()) {
  580. $agentPercent = bcsub($agentInfo['division_percent'], bcadd($brokerageOneTwo, $staffPercent, 2), 2);
  581. $agentPercent = $agentPercent < 0 ? 0 : $agentPercent;
  582. } else {
  583. $agentPercent = 0;
  584. }
  585. if ($divisionInfo['division_status'] == 1 && $divisionInfo['division_end_time'] > time()) {
  586. $divisionPercent = bcsub($divisionInfo['division_percent'], bcadd(bcadd($brokerageOneTwo, $staffPercent, 2), $agentPercent, 2), 2);
  587. $divisionPercent = $divisionPercent < 0 ? 0 : $divisionPercent;
  588. } else {
  589. $divisionPercent = 0;
  590. }
  591. }
  592. } elseif ($userInfo['agent_id']) {
  593. /** 该用户为代理商推广 */
  594. if ($userInfo['agent_id'] == $userInfo['spread_uid']) {
  595. $storeBrokerageOne = $isSelfBrokerage ? $storeBrokerageRatio : 0;
  596. $storeBrokerageTwo = 0;
  597. $staffPercent = 0;
  598. if ($agentInfo['division_status'] == 1 && $agentInfo['division_end_time'] > time()) {
  599. $agentPercent = bcsub($agentInfo['division_percent'], $storeBrokerageOne, 2);
  600. $agentPercent = $agentPercent < 0 ? 0 : $agentPercent;
  601. } else {
  602. $agentPercent = 0;
  603. }
  604. if ($divisionInfo['division_status'] == 1 && $divisionInfo['division_end_time'] > time()) {
  605. $divisionPercent = bcsub($divisionInfo['division_percent'], bcadd($storeBrokerageOne, $agentPercent, 2), 2);
  606. $divisionPercent = $divisionPercent < 0 ? 0 : $divisionPercent;
  607. } else {
  608. $divisionPercent = 0;
  609. }
  610. } else {
  611. $storeBrokerageOne = $storeBrokerageRatio;
  612. $storeBrokerageTwo = $userServices->value(['uid' => $userInfo['spread_uid']], 'spread_uid') == $userInfo['agent_id'] && !$isSelfBrokerage ? 0 : $storeBrokerageRatioTwo;
  613. $brokerageOneTwo = bcadd($storeBrokerageOne, $storeBrokerageTwo, 2);
  614. $staffPercent = 0;
  615. if ($agentInfo['division_status'] == 1 && $agentInfo['division_end_time'] > time()) {
  616. $agentPercent = bcsub($agentInfo['division_percent'], $brokerageOneTwo, 2);
  617. $agentPercent = $agentPercent < 0 ? 0 : $agentPercent;
  618. } else {
  619. $agentPercent = 0;
  620. }
  621. if ($divisionInfo['division_status'] == 1 && $divisionInfo['division_end_time'] > time()) {
  622. $divisionPercent = bcsub($divisionInfo['division_percent'], bcadd($brokerageOneTwo, $agentPercent, 2), 2);
  623. $divisionPercent = $divisionPercent < 0 ? 0 : $divisionPercent;
  624. } else {
  625. $divisionPercent = 0;
  626. }
  627. }
  628. } elseif ($userInfo['division_id']) {
  629. /** 该用户为事业部推广 */
  630. if ($userInfo['division_id'] == $userInfo['spread_uid']) {
  631. /** 事业部直接下级 */
  632. $storeBrokerageOne = $isSelfBrokerage ? $storeBrokerageRatio : 0;
  633. $storeBrokerageTwo = 0;
  634. $staffPercent = 0;
  635. $agentPercent = 0;
  636. if ($divisionInfo['division_status'] == 1 && $divisionInfo['division_end_time'] > time()) {
  637. $divisionPercent = bcsub($divisionInfo['division_percent'], $storeBrokerageOne, 2);
  638. $divisionPercent = $divisionPercent < 0 ? 0 : $divisionPercent;
  639. } else {
  640. $divisionPercent = 0;
  641. }
  642. } else {
  643. $storeBrokerageOne = $storeBrokerageRatio;
  644. $storeBrokerageTwo = $userServices->value(['uid' => $userInfo['spread_uid']], 'spread_uid') == $userInfo['division_id'] && !$isSelfBrokerage ? 0 : $storeBrokerageRatioTwo;
  645. $brokerageOneTwo = bcadd($storeBrokerageOne, $storeBrokerageTwo, 2);
  646. $staffPercent = 0;
  647. $agentPercent = 0;
  648. if ($divisionInfo['division_status'] == 1 && $divisionInfo['division_end_time'] > time()) {
  649. $divisionPercent = bcsub($divisionInfo['division_percent'], $brokerageOneTwo, 2);
  650. $divisionPercent = $divisionPercent < 0 ? 0 : $divisionPercent;
  651. } else {
  652. $divisionPercent = 0;
  653. }
  654. }
  655. } else {
  656. /** 没有任何代理商关系 */
  657. $storeBrokerageOne = $storeBrokerageRatio;
  658. $storeBrokerageTwo = $storeBrokerageRatioTwo;
  659. $staffPercent = 0;
  660. $agentPercent = 0;
  661. $divisionPercent = 0;
  662. }
  663. }
  664. }
  665. return [max($storeBrokerageOne, 0), max($storeBrokerageTwo, 0), max($staffPercent, 0), max($agentPercent, 0), max($divisionPercent, 0)];
  666. }
  667. /**
  668. * 事业部统计
  669. * @param $type
  670. * @param $time
  671. * @param $page
  672. * @param $limit
  673. * @param $sort
  674. * @param $order
  675. * @return mixed
  676. * @author wuhaotian
  677. * @email 442384644@qq.com
  678. * @date 2025/4/8
  679. */
  680. public function divisionStatistics($type, $time, $page, $limit, $sort, $order)
  681. {
  682. switch ($type) {
  683. case 1:
  684. $field = 'division_id';
  685. break;
  686. case 2:
  687. $field = 'agent_id';
  688. break;
  689. case 3:
  690. $field = 'staff_id';
  691. break;
  692. default:
  693. $field = 'division_id';
  694. }
  695. $data = app()->make(StoreOrderServices::class)->divisionStatistics($field, $time, $page, $limit, $sort, $order);
  696. $uids = array_column($data['list'], $field);
  697. $userInfos = app()->make(UserServices::class)->getColumn(['uid' => $uids], 'uid,nickname,avatar', 'uid');
  698. foreach ($data['list'] as $key => &$val) {
  699. $val['uid'] = $val[$field];
  700. $val['name'] = $userInfos[$val[$field]]['nickname'];
  701. $val['avatar'] = set_file_url($userInfos[$val[$field]]['avatar']);
  702. }
  703. return $data;
  704. }
  705. }