AgentLevelServices.php 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367
  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. }
  67. return compact('count', 'list');
  68. }
  69. /**
  70. * 商城获取分销员等级列表
  71. * @param int $uid
  72. * @return array
  73. */
  74. public function getUserlevelList(int $uid)
  75. {
  76. //商城分销是否开启
  77. if (!sys_config('brokerage_func_status')) {
  78. return [];
  79. }
  80. /** @var UserServices $userServices */
  81. $userServices = app()->make(UserServices::class);
  82. $user = $userServices->getUserInfo($uid);
  83. if (!$user) {
  84. throw new ApiException(410032);
  85. }
  86. //检测升级
  87. $this->checkUserLevelFinish($uid);
  88. $list = $this->dao->getList(['is_del' => 0, 'status' => 1]);
  89. foreach ($list as &$item) {
  90. $item['image'] = set_file_url($item['image']);
  91. }
  92. $agent_level = $user['agent_level'] ?? 0;
  93. //没等级默认最低等级
  94. if (!$agent_level) {
  95. $levelInfo = $list[0] ?? [];
  96. $levelInfo['grade'] = -1;
  97. } else {
  98. $levelInfo = $this->getLevelInfo($agent_level) ?: [];
  99. }
  100. $sum_task = $finish_task = 0;
  101. if ($levelInfo) {
  102. /** @var AgentLevelTaskServices $levelTaskServices */
  103. $levelTaskServices = app()->make(AgentLevelTaskServices::class);
  104. $sum_task = $levelTaskServices->count(['level_id' => $levelInfo['id'], 'is_del' => 0, 'status' => 1]);
  105. /** @var AgentLevelTaskRecordServices $levelTaskRecordServices */
  106. $levelTaskRecordServices = app()->make(AgentLevelTaskRecordServices::class);
  107. $finish_task = $levelTaskRecordServices->count(['level_id' => $levelInfo['id'], 'uid' => $uid]);
  108. }
  109. $levelInfo['sum_task'] = $sum_task;
  110. $levelInfo['finish_task'] = $finish_task;
  111. return ['user' => $user, 'level_list' => $list, 'level_info' => $levelInfo];
  112. }
  113. /**
  114. * 获取下一等级
  115. * @param int $level_id
  116. * @return array|\think\Model|null
  117. * @throws \think\db\exception\DataNotFoundException
  118. * @throws \think\db\exception\DbException
  119. * @throws \think\db\exception\ModelNotFoundException
  120. */
  121. public function getNextLevelInfo(int $level_id = 0)
  122. {
  123. $grade = 0;
  124. if ($level_id) {
  125. $grade = $this->dao->value(['id' => $level_id, 'is_del' => 0, 'status' => 1], 'grade') ?: 0;
  126. }
  127. return $this->dao->getOne([['grade', '>', $grade], ['is_del', '=', 0], ['status', '=', 1]]);
  128. }
  129. /**
  130. * 检测用户是否能升级
  131. * @param int $uid
  132. * @param array $uids
  133. * @return bool
  134. * @throws \think\db\exception\DataNotFoundException
  135. * @throws \think\db\exception\DbException
  136. * @throws \think\db\exception\ModelNotFoundException
  137. */
  138. public function checkUserLevelFinish(int $uid, array $uids = [])
  139. {
  140. //商城分销是否开启
  141. if (!sys_config('brokerage_func_status')) {
  142. return false;
  143. }
  144. /** @var UserServices $userServices */
  145. $userServices = app()->make(UserServices::class);
  146. $userInfo = $userServices->getUserInfo($uid);
  147. if (!$userInfo) {
  148. return false;
  149. }
  150. $list = $this->dao->getList(['is_del' => 0, 'status' => 1]);
  151. if (!$list) {
  152. return false;
  153. }
  154. if (!$uids) {
  155. //获取上级uid || 开启自购返回自己uid
  156. $spread_uid = $userServices->getSpreadUid($uid, $userInfo);
  157. $two_spread_uid = 0;
  158. if ($spread_uid > 0 && $one_user_info = $userServices->getUserInfo($spread_uid)) {
  159. $two_spread_uid = $userServices->getSpreadUid($spread_uid, $one_user_info, false);
  160. }
  161. $uids = array_unique([$uid, $spread_uid, $two_spread_uid]);
  162. }
  163. foreach ($uids as $uid) {
  164. if ($uid <= 0) continue;
  165. if ($uid != $userInfo['uid']) {
  166. $userInfo = $userServices->getUserInfo($uid);
  167. }
  168. $now_grade = 0;
  169. if ($userInfo['agent_level']) {
  170. $now_grade = $this->dao->value(['id' => $userInfo['agent_level']], 'grade') ?: 0;
  171. }
  172. foreach ($list as $levelInfo) {
  173. if (!$levelInfo || $levelInfo['grade'] <= $now_grade) {
  174. continue;
  175. }
  176. /** @var AgentLevelTaskServices $levelTaskServices */
  177. $levelTaskServices = app()->make(AgentLevelTaskServices::class);
  178. $task_list = $levelTaskServices->getTaskList(['level_id' => $levelInfo['id'], 'is_del' => 0, 'status' => 1]);
  179. if (!$task_list) {
  180. continue;
  181. }
  182. foreach ($task_list as $task) {
  183. $levelTaskServices->checkLevelTaskFinish($uid, (int)$task['id'], $task);
  184. }
  185. /** @var AgentLevelTaskRecordServices $levelTaskRecordServices */
  186. $levelTaskRecordServices = app()->make(AgentLevelTaskRecordServices::class);
  187. $ids = array_column($task_list, 'id');
  188. $finish_task = $levelTaskRecordServices->count(['level_id' => $levelInfo['id'], 'uid' => $uid, 'task_id' => $ids]);
  189. //任务完成升这一等级
  190. if ($finish_task >= count($task_list)) {
  191. $userServices->update($uid, ['agent_level' => $levelInfo['grade']]);
  192. } else {
  193. break;
  194. }
  195. }
  196. }
  197. return true;
  198. }
  199. /**
  200. * 分销等级上浮
  201. * @param int $uid
  202. * @param array $userInfo
  203. * @return array|int[]
  204. * @throws \think\db\exception\DataNotFoundException
  205. * @throws \think\db\exception\DbException
  206. * @throws \think\db\exception\ModelNotFoundException
  207. */
  208. public function getAgentLevelBrokerage(int $uid, $userInfo = [])
  209. {
  210. $one_brokerage_up = $two_brokerage_up = $spread_one_uid = $spread_two_uid = 0;
  211. if (!$uid) {
  212. return [$one_brokerage_up, $two_brokerage_up, $spread_one_uid, $spread_two_uid];
  213. }
  214. //商城分销是否开启
  215. if (!sys_config('brokerage_func_status')) {
  216. return [$one_brokerage_up, $two_brokerage_up, $spread_one_uid, $spread_two_uid];
  217. }
  218. /** @var UserServices $userServices */
  219. $userServices = app()->make(UserServices::class);
  220. if (!$userInfo) {
  221. $userInfo = $userServices->getUserInfo($uid);
  222. }
  223. if (!$userInfo) {
  224. return [$one_brokerage_up, $two_brokerage_up, $spread_one_uid, $spread_two_uid];
  225. }
  226. //获取上级uid || 开启自购返回自己uid
  227. $spread_one_uid = $userServices->getSpreadUid($uid, $userInfo);
  228. $one_agent_level = 0;
  229. $two_agent_level = 0;
  230. $spread_two_uid = 0;
  231. if ($spread_one_uid > 0 && $one_user_info = $userServices->getUserInfo($spread_one_uid)) {
  232. $one_agent_level = $one_user_info['agent_level'] ?? 0;
  233. $spread_two_uid = $userServices->getSpreadUid($spread_one_uid, $one_user_info, false);
  234. if ($spread_two_uid > 0 && $two_user_info = $userServices->getUserInfo($spread_two_uid)) {
  235. $two_agent_level = $two_user_info['agent_level'] ?? 0;
  236. }
  237. }
  238. $one_brokerage_up = $one_agent_level ? ($this->getLevelInfo($one_agent_level)['one_brokerage'] ?? 0) : 0;
  239. $two_brokerage_up = $two_agent_level ? ($this->getLevelInfo($two_agent_level)['two_brokerage'] ?? 0) : 0;
  240. return [$one_brokerage_up, $two_brokerage_up, $spread_one_uid, $spread_two_uid];
  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('admin/widget.images/index', array('fodder' => 'image')))->icon('ios-add')->width('950px')->height('505px')->modal(['footer-hide' => true]);
  253. $field[] = Form::number('one_brokerage', '一级上浮', 0)->appendRule('suffix', [
  254. 'type' => 'div',
  255. 'class' => 'tips-info',
  256. 'domProps' => ['innerHTML' => '在分销一级佣金基础上浮(0-1000之间整数)百分比,目前一级返佣比率:10%,上浮5%,则返佣比率:一级返佣比率 * (1 + 一级上浮比率) = 10.50%']
  257. ])->max(1000)->precision(0);
  258. $field[] = Form::number('two_brokerage', '二级上浮', 0)->appendRule('suffix', [
  259. 'type' => 'div',
  260. 'class' => 'tips-info',
  261. 'domProps' => ['innerHTML' => '在分销二级佣金基础上浮(0-1000之间整数)百分比,目前二级返佣比率:10%,上浮2%,则返佣比率:二级返佣比率 * (1 + 二级上浮比率) = 5.10%']
  262. ])->min(0)->max(1000)->precision(0);
  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('admin/widget.images/index', array('fodder' => 'image')), $levelInfo['image'])->icon('ios-add')->width('950px')->height('505px')->modal(['footer-hide' => true]);
  282. $field[] = Form::number('one_brokerage', '一级上浮', $levelInfo['one_brokerage'])->appendRule('suffix', [
  283. 'type' => 'div',
  284. 'class' => 'tips-info',
  285. 'domProps' => ['innerHTML' => '在分销一级佣金基础上浮(0-1000之间整数)百分比,目前一级返佣比率:10%,上浮5%,则返佣比率:一级返佣比率 * (1 + 一级上浮比率) = 10.50%']
  286. ])->min(0)->max(1000)->precision(0);
  287. $field[] = Form::number('two_brokerage', '二级上浮', $levelInfo['two_brokerage'])->appendRule('suffix', [
  288. 'type' => 'div',
  289. 'class' => 'tips-info',
  290. 'domProps' => ['innerHTML' => '在分销二级佣金基础上浮(0-1000之间整数)百分比,目前二级返佣比率:10%,上浮2%,则返佣比率:二级返佣比率 * (1 + 二级上浮比率) = 5.10%']
  291. ])->min(0)->max(1000)->precision(0);
  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)->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. }