AgentLevelServices.php 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370
  1. <?php
  2. // +----------------------------------------------------------------------
  3. // | CRMEB [ CRMEB赋能开发者,助力企业发展 ]
  4. // +----------------------------------------------------------------------
  5. // | Copyright (c) 2016~2023 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\agent;
  12. use app\dao\agent\AgentLevelDao;
  13. use app\services\BaseServices;
  14. use app\services\user\UserServices;
  15. use crmeb\exceptions\AdminException;
  16. use crmeb\exceptions\ApiException;
  17. use crmeb\services\FormBuilder as Form;
  18. use think\facade\Route as Url;
  19. /**
  20. * Class AgentLevelServices
  21. * @package app\services\agent
  22. */
  23. class AgentLevelServices extends BaseServices
  24. {
  25. /**
  26. * AgentLevelServices constructor.
  27. * @param AgentLevelDao $dao
  28. */
  29. public function __construct(AgentLevelDao $dao)
  30. {
  31. $this->dao = $dao;
  32. }
  33. /**
  34. * 获取某一个等级信息
  35. * @param int $id
  36. * @param string $field
  37. * @param array $with
  38. * @return array|\think\Model|null
  39. * @throws \think\db\exception\DataNotFoundException
  40. * @throws \think\db\exception\DbException
  41. * @throws \think\db\exception\ModelNotFoundException
  42. */
  43. public function getLevelInfo(int $id, string $field = '*', array $with = [])
  44. {
  45. return $this->dao->getOne(['id' => $id, 'is_del' => 0], $field, $with);
  46. }
  47. /**
  48. * 获取等级列表
  49. * @param array $where
  50. * @return array
  51. * @throws \think\db\exception\DataNotFoundException
  52. * @throws \think\db\exception\DbException
  53. * @throws \think\db\exception\ModelNotFoundException
  54. */
  55. public function getLevelList(array $where)
  56. {
  57. $where['is_del'] = 0;
  58. [$page, $limit] = $this->getPageValue();
  59. $list = $this->dao->getList($where, '*', ['task' => function ($query) {
  60. $query->field('count(*) as sum');
  61. }], $page, $limit);
  62. $count = $this->dao->count($where);
  63. foreach ($list as &$item) {
  64. // $item['one_brokerage_ratio'] = bcdiv(bcmul((string)sys_config('store_brokerage_ratio'), bcadd('100', (string)$item['one_brokerage'], 2), 2), '100', 2);
  65. // $item['two_brokerage_ratio'] = bcdiv(bcmul((string)sys_config('store_brokerage_two'), bcadd('100', (string)$item['two_brokerage'], 2), 2), '100', 2);
  66. $item['one_brokerage_ratio'] = $item['one_brokerage_percent'];
  67. $item['two_brokerage_ratio'] = $item['two_brokerage_percent'];
  68. }
  69. return compact('count', 'list');
  70. }
  71. /**
  72. * 商城获取分销员等级列表
  73. * @param int $uid
  74. * @return array
  75. */
  76. public function getUserlevelList(int $uid)
  77. {
  78. //商城分销是否开启
  79. if (!sys_config('brokerage_func_status')) {
  80. return [];
  81. }
  82. /** @var UserServices $userServices */
  83. $userServices = app()->make(UserServices::class);
  84. $user = $userServices->getUserInfo($uid);
  85. if (!$user) {
  86. throw new ApiException(410032);
  87. }
  88. //检测升级
  89. $this->checkUserLevelFinish($uid);
  90. $list = $this->dao->getList(['is_del' => 0, 'status' => 1]);
  91. foreach ($list as &$item) {
  92. $item['image'] = set_file_url($item['image']);
  93. }
  94. $agent_level = $user['agent_level'] ?? 0;
  95. //没等级默认最低等级
  96. if (!$agent_level) {
  97. $levelInfo = $list[0] ?? [];
  98. $levelInfo['grade'] = -1;
  99. } else {
  100. $levelInfo = $this->getLevelInfo($agent_level) ?: [];
  101. }
  102. $sum_task = $finish_task = 0;
  103. if ($levelInfo) {
  104. /** @var AgentLevelTaskServices $levelTaskServices */
  105. $levelTaskServices = app()->make(AgentLevelTaskServices::class);
  106. $sum_task = $levelTaskServices->count(['level_id' => $levelInfo['id'], 'is_del' => 0, 'status' => 1]);
  107. /** @var AgentLevelTaskRecordServices $levelTaskRecordServices */
  108. $levelTaskRecordServices = app()->make(AgentLevelTaskRecordServices::class);
  109. $finish_task = $levelTaskRecordServices->count(['level_id' => $levelInfo['id'], 'uid' => $uid]);
  110. }
  111. $levelInfo['sum_task'] = $sum_task;
  112. $levelInfo['finish_task'] = $finish_task;
  113. return ['user' => $user, 'level_list' => $list, 'level_info' => $levelInfo];
  114. }
  115. /**
  116. * 获取下一等级
  117. * @param int $level_id
  118. * @return array|\think\Model|null
  119. * @throws \think\db\exception\DataNotFoundException
  120. * @throws \think\db\exception\DbException
  121. * @throws \think\db\exception\ModelNotFoundException
  122. */
  123. public function getNextLevelInfo(int $level_id = 0)
  124. {
  125. $grade = 0;
  126. if ($level_id) {
  127. $grade = $this->dao->value(['id' => $level_id, 'is_del' => 0, 'status' => 1], 'grade') ?: 0;
  128. }
  129. return $this->dao->getOne([['grade', '>', $grade], ['is_del', '=', 0], ['status', '=', 1]]);
  130. }
  131. /**
  132. * 检测用户是否能升级
  133. * @param int $uid
  134. * @param array $uids
  135. * @return bool
  136. * @throws \think\db\exception\DataNotFoundException
  137. * @throws \think\db\exception\DbException
  138. * @throws \think\db\exception\ModelNotFoundException
  139. */
  140. public function checkUserLevelFinish(int $uid, array $uids = [])
  141. {
  142. //商城分销是否开启
  143. if (!sys_config('brokerage_func_status')) {
  144. return false;
  145. }
  146. /** @var UserServices $userServices */
  147. $userServices = app()->make(UserServices::class);
  148. $userInfo = $userServices->getUserInfo($uid);
  149. if (!$userInfo) {
  150. return false;
  151. }
  152. $list = $this->dao->getList(['is_del' => 0, 'status' => 1]);
  153. if (!$list) {
  154. return false;
  155. }
  156. if (!$uids) {
  157. //获取上级uid || 开启自购返回自己uid
  158. $spread_uid = $userServices->getSpreadUid($uid, $userInfo);
  159. $two_spread_uid = 0;
  160. if ($spread_uid > 0 && $one_user_info = $userServices->getUserInfo($spread_uid)) {
  161. $two_spread_uid = $userServices->getSpreadUid($spread_uid, $one_user_info, false);
  162. }
  163. $uids = array_unique([$uid, $spread_uid, $two_spread_uid]);
  164. }
  165. foreach ($uids as $uid) {
  166. if ($uid <= 0) continue;
  167. if ($uid != $userInfo['uid']) {
  168. $userInfo = $userServices->getUserInfo($uid);
  169. }
  170. $now_grade = 0;
  171. if ($userInfo['agent_level']) {
  172. $now_grade = $this->dao->value(['id' => $userInfo['agent_level']], 'grade') ?: 0;
  173. }
  174. foreach ($list as $levelInfo) {
  175. if (!$levelInfo || $levelInfo['grade'] <= $now_grade) {
  176. continue;
  177. }
  178. /** @var AgentLevelTaskServices $levelTaskServices */
  179. $levelTaskServices = app()->make(AgentLevelTaskServices::class);
  180. $task_list = $levelTaskServices->getTaskList(['level_id' => $levelInfo['id'], 'is_del' => 0, 'status' => 1]);
  181. if (!$task_list) {
  182. continue;
  183. }
  184. foreach ($task_list as $task) {
  185. $levelTaskServices->checkLevelTaskFinish($uid, (int)$task['id'], $task);
  186. }
  187. /** @var AgentLevelTaskRecordServices $levelTaskRecordServices */
  188. $levelTaskRecordServices = app()->make(AgentLevelTaskRecordServices::class);
  189. $ids = array_column($task_list, 'id');
  190. $finish_task = $levelTaskRecordServices->count(['level_id' => $levelInfo['id'], 'uid' => $uid, 'task_id' => $ids]);
  191. //任务完成升这一等级
  192. if ($finish_task >= count($task_list)) {
  193. $userServices->update($uid, ['agent_level' => $levelInfo['grade']]);
  194. } else {
  195. break;
  196. }
  197. }
  198. }
  199. return true;
  200. }
  201. /**
  202. * 分销等级上浮
  203. * @param $storeBrokerageRatio
  204. * @param $storeBrokerageTwo
  205. * @param $spread_one_uid
  206. * @param $spread_two_uid
  207. * @return array|int[]
  208. * @throws \think\db\exception\DataNotFoundException
  209. * @throws \think\db\exception\DbException
  210. * @throws \think\db\exception\ModelNotFoundException
  211. */
  212. public function getAgentLevelBrokerage($storeBrokerageRatio, $storeBrokerageTwo, $spread_one_uid, $spread_two_uid)
  213. {
  214. /** @var UserServices $userServices */
  215. $userServices = app()->make(UserServices::class);
  216. $one_agent_level = $userServices->value(['uid' => $spread_one_uid], 'agent_level') ?? 0;
  217. $two_agent_level = $userServices->value(['uid' => $spread_two_uid], 'agent_level') ?? 0;
  218. if ($one_agent_level) {
  219. $oneLevelInfo = $this->getLevelInfo($one_agent_level);
  220. if ($oneLevelInfo) {
  221. $storeBrokerageRatio = $oneLevelInfo['one_brokerage_percent'];
  222. // if ($oneLevelInfo['one_brokerage_percent'] == '0.00') {
  223. // $storeBrokerageRatio = $storeBrokerageRatio + (($storeBrokerageRatio * $oneLevelInfo['one_brokerage'] ?? 0) / 100);
  224. // } else {
  225. // $storeBrokerageRatio = $oneLevelInfo['one_brokerage_percent'];
  226. // }
  227. }
  228. }
  229. if ($two_agent_level) {
  230. $twoLevelInfo = $this->getLevelInfo($two_agent_level);
  231. if ($twoLevelInfo) {
  232. $storeBrokerageTwo = $twoLevelInfo['two_brokerage_percent'];
  233. // if ($twoLevelInfo['two_brokerage_percent'] == '0.00') {
  234. // $storeBrokerageTwo = $storeBrokerageTwo + (($storeBrokerageTwo * $twoLevelInfo['two_brokerage'] ?? 0) / 100);
  235. // } else {
  236. // $storeBrokerageTwo = $twoLevelInfo['two_brokerage_percent'];
  237. // }
  238. }
  239. }
  240. return [$storeBrokerageRatio, $storeBrokerageTwo];
  241. }
  242. /**
  243. * 添加等级表单
  244. * @param int $id
  245. * @return array
  246. * @throws \FormBuilder\Exception\FormBuilderException
  247. */
  248. public function createForm()
  249. {
  250. $field[] = Form::input('name', '等级名称')->maxlength(8)->col(24);
  251. $field[] = Form::number('grade', '等级', 0)->min(0)->precision(0);
  252. $field[] = Form::frameImage('image', '背景图', Url::buildUrl(config('app.admin_prefix', 'admin') . '/widget.images/index', array('fodder' => 'image')))->icon('el-icon-picture-outline')->width('950px')->height('560px')->props(['footer' => false]);
  253. $field[] = Form::number('one_brokerage_percent', '一级佣金比例', 0)->appendRule('suffix', [
  254. 'type' => 'div',
  255. 'class' => 'tips-info',
  256. 'domProps' => ['innerHTML' => '到达该等级之后,一级分佣按照此比例计算佣金']
  257. ])->max(100)->precision(2);
  258. $field[] = Form::number('two_brokerage_percent', '二级佣金比例', 0)->appendRule('suffix', [
  259. 'type' => 'div',
  260. 'class' => 'tips-info',
  261. 'domProps' => ['innerHTML' => '到达该等级之后,二级分佣按照此比例计算佣金']
  262. ])->min(0)->max(100)->precision(2);
  263. $field[] = Form::radio('status', '是否显示', 1)->options([['value' => 1, 'label' => '显示'], ['value' => 0, 'label' => '隐藏']]);
  264. return create_form('添加分销员等级', $field, Url::buildUrl('/agent/level'), 'POST');
  265. }
  266. /**
  267. * 获取修改等级表单
  268. * @param int $id
  269. * @return array
  270. * @throws \FormBuilder\Exception\FormBuilderException
  271. */
  272. public function editForm(int $id)
  273. {
  274. $levelInfo = $this->getLevelInfo($id);
  275. if (!$levelInfo)
  276. throw new AdminException(100026);
  277. $field = [];
  278. $field[] = Form::hidden('id', $id);
  279. $field[] = Form::input('name', '等级名称', $levelInfo['name'])->maxlength(8)->col(24);
  280. $field[] = Form::number('grade', '等级', $levelInfo['grade'])->min(0)->precision(0);
  281. $field[] = Form::frameImage('image', '背景图', Url::buildUrl(config('app.admin_prefix', 'admin') . '/widget.images/index', array('fodder' => 'image')), $levelInfo['image'])->icon('el-icon-picture-outline')->width('950px')->height('560px')->props(['footer' => false]);
  282. $field[] = Form::number('one_brokerage_percent', '一级佣金比例', $levelInfo['one_brokerage_percent'])->appendRule('suffix', [
  283. 'type' => 'div',
  284. 'class' => 'tips-info',
  285. 'domProps' => ['innerHTML' => '到达该等级之后,一级分佣按照此比例计算佣金']
  286. ])->max(100)->precision(2);
  287. $field[] = Form::number('two_brokerage_percent', '二级佣金比例', $levelInfo['two_brokerage_percent'])->appendRule('suffix', [
  288. 'type' => 'div',
  289. 'class' => 'tips-info',
  290. 'domProps' => ['innerHTML' => '到达该等级之后,二级分佣按照此比例计算佣金']
  291. ])->min(0)->max(100)->precision(2);
  292. $field[] = Form::radio('status', '是否显示', $levelInfo['status'])->options([['value' => 1, 'label' => '显示'], ['value' => 0, 'label' => '隐藏']]);
  293. return create_form('编辑分销员等级', $field, Url::buildUrl('/agent/level/' . $id), 'PUT');
  294. }
  295. /**
  296. * 赠送分销等级表单
  297. * @param int $uid
  298. * @return array
  299. * @throws \FormBuilder\Exception\FormBuilderException
  300. * @throws \think\db\exception\DataNotFoundException
  301. * @throws \think\db\exception\DbException
  302. * @throws \think\db\exception\ModelNotFoundException
  303. */
  304. public function levelForm(int $uid)
  305. {
  306. /** @var UserServices $userServices */
  307. $userServices = app()->make(UserServices::class);
  308. $userInfo = $userServices->getUserInfo($uid);
  309. if (!$userInfo) {
  310. throw new AdminException(400214);
  311. }
  312. $levelList = $this->dao->getList(['is_del' => 0, 'status' => 1], '*', [], 0, 0, $userInfo['agent_level']);
  313. $setOptionLabel = function () use ($levelList) {
  314. $menus = [];
  315. foreach ($levelList as $level) {
  316. $menus[] = ['value' => $level['id'], 'label' => $level['name']];
  317. }
  318. return $menus;
  319. };
  320. $field[] = Form::hidden('uid', $uid);
  321. $field[] = Form::select('id', '分销等级', $userInfo['agent_level'] != 0 ? $userInfo['agent_level'] : '')->setOptions(Form::setOptions($setOptionLabel))->filterable(true);
  322. return create_form('修改分销等级', $field, Url::buildUrl('/agent/give_level'), 'post');
  323. }
  324. /**
  325. * 赠送分销等级
  326. * @param int $uid
  327. * @param int $id
  328. * @return bool
  329. * @throws \think\db\exception\DataNotFoundException
  330. * @throws \think\db\exception\DbException
  331. * @throws \think\db\exception\ModelNotFoundException
  332. */
  333. public function givelevel(int $uid, int $id)
  334. {
  335. /** @var UserServices $userServices */
  336. $userServices = app()->make(UserServices::class);
  337. $userInfo = $userServices->getUserInfo($uid, 'uid');
  338. if (!$userInfo) {
  339. throw new AdminException(400214);
  340. }
  341. $levelInfo = $this->getLevelInfo($id, 'id');
  342. if (!$levelInfo) {
  343. throw new AdminException(400442);
  344. }
  345. if ($userServices->update($uid, ['agent_level' => $id]) === false) {
  346. throw new AdminException(400219);
  347. }
  348. return true;
  349. }
  350. }