AgentLevelServices.php 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347
  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. return compact('count', 'list');
  64. }
  65. /**
  66. * 商城获取分销员等级列表
  67. * @param int $uid
  68. * @return array
  69. */
  70. public function getUserlevelList(int $uid)
  71. {
  72. //商城分销是否开启
  73. if (!sys_config('brokerage_func_status')) {
  74. return [];
  75. }
  76. /** @var UserServices $userServices */
  77. $userServices = app()->make(UserServices::class);
  78. $user = $userServices->getUserInfo($uid);
  79. if (!$user) {
  80. throw new ApiException(410032);
  81. }
  82. //检测升级
  83. $this->checkUserLevelFinish($uid);
  84. $list = $this->dao->getList(['is_del' => 0, 'status' => 1]);
  85. foreach ($list as &$item) {
  86. $item['image'] = set_file_url($item['image']);
  87. }
  88. $agent_level = $user['agent_level'] ?? 0;
  89. //没等级默认最低等级
  90. if (!$agent_level) {
  91. $levelInfo = $list[0] ?? [];
  92. $levelInfo['grade'] = -1;
  93. } else {
  94. $levelInfo = $this->getLevelInfo($agent_level) ?: [];
  95. }
  96. $sum_task = $finish_task = 0;
  97. if ($levelInfo) {
  98. /** @var AgentLevelTaskServices $levelTaskServices */
  99. $levelTaskServices = app()->make(AgentLevelTaskServices::class);
  100. $sum_task = $levelTaskServices->count(['level_id' => $levelInfo['id'], 'is_del' => 0, 'status' => 1]);
  101. /** @var AgentLevelTaskRecordServices $levelTaskRecordServices */
  102. $levelTaskRecordServices = app()->make(AgentLevelTaskRecordServices::class);
  103. $finish_task = $levelTaskRecordServices->count(['level_id' => $levelInfo['id'], 'uid' => $uid]);
  104. }
  105. $levelInfo['sum_task'] = $sum_task;
  106. $levelInfo['finish_task'] = $finish_task;
  107. return ['user' => $user, 'level_list' => $list, 'level_info' => $levelInfo];
  108. }
  109. /**
  110. * 获取下一等级
  111. * @param int $level_id
  112. * @return array|\think\Model|null
  113. * @throws \think\db\exception\DataNotFoundException
  114. * @throws \think\db\exception\DbException
  115. * @throws \think\db\exception\ModelNotFoundException
  116. */
  117. public function getNextLevelInfo(int $level_id = 0)
  118. {
  119. $grade = 0;
  120. if ($level_id) {
  121. $grade = $this->dao->value(['id' => $level_id, 'is_del' => 0, 'status' => 1], 'grade') ?: 0;
  122. }
  123. return $this->dao->getOne([['grade', '>', $grade], ['is_del', '=', 0], ['status', '=', 1]]);
  124. }
  125. /**
  126. * 检测用户是否能升级
  127. * @param int $uid
  128. * @param array $uids
  129. * @return bool
  130. * @throws \think\db\exception\DataNotFoundException
  131. * @throws \think\db\exception\DbException
  132. * @throws \think\db\exception\ModelNotFoundException
  133. */
  134. public function checkUserLevelFinish(int $uid, array $uids = [])
  135. {
  136. //商城分销是否开启
  137. if (!sys_config('brokerage_func_status')) {
  138. return false;
  139. }
  140. /** @var UserServices $userServices */
  141. $userServices = app()->make(UserServices::class);
  142. $userInfo = $userServices->getUserInfo($uid);
  143. if (!$userInfo) {
  144. return false;
  145. }
  146. $list = $this->dao->getList(['is_del' => 0, 'status' => 1]);
  147. if (!$list) {
  148. return false;
  149. }
  150. if (!$uids) {
  151. //获取上级uid || 开启自购返回自己uid
  152. $spread_uid = $userServices->getSpreadUid($uid, $userInfo);
  153. $two_spread_uid = 0;
  154. if ($spread_uid > 0 && $one_user_info = $userServices->getUserInfo($spread_uid)) {
  155. $two_spread_uid = $userServices->getSpreadUid($spread_uid, $one_user_info, false);
  156. }
  157. $uids = array_unique([$uid, $spread_uid, $two_spread_uid]);
  158. }
  159. foreach ($uids as $uid) {
  160. if ($uid <= 0) continue;
  161. if ($uid != $userInfo['uid']) {
  162. $userInfo = $userServices->getUserInfo($uid);
  163. }
  164. $now_grade = 0;
  165. if ($userInfo['agent_level']) {
  166. $now_grade = $this->dao->value(['id' => $userInfo['agent_level']], 'grade') ?: 0;
  167. }
  168. foreach ($list as $levelInfo) {
  169. if (!$levelInfo || $levelInfo['grade'] <= $now_grade) {
  170. continue;
  171. }
  172. /** @var AgentLevelTaskServices $levelTaskServices */
  173. $levelTaskServices = app()->make(AgentLevelTaskServices::class);
  174. $task_list = $levelTaskServices->getTaskList(['level_id' => $levelInfo['id'], 'is_del' => 0, 'status' => 1]);
  175. if (!$task_list) {
  176. continue;
  177. }
  178. foreach ($task_list as $task) {
  179. $levelTaskServices->checkLevelTaskFinish($uid, (int)$task['id'], $task);
  180. }
  181. /** @var AgentLevelTaskRecordServices $levelTaskRecordServices */
  182. $levelTaskRecordServices = app()->make(AgentLevelTaskRecordServices::class);
  183. $ids = array_column($task_list, 'id');
  184. $finish_task = $levelTaskRecordServices->count(['level_id' => $levelInfo['id'], 'uid' => $uid, 'task_id' => $ids]);
  185. //任务完成升这一等级
  186. if ($finish_task >= count($task_list)) {
  187. $userServices->update($uid, ['agent_level' => $levelInfo['grade']]);
  188. } else {
  189. break;
  190. }
  191. }
  192. }
  193. return true;
  194. }
  195. /**
  196. * 分销等级上浮
  197. * @param int $uid
  198. * @param array $userInfo
  199. * @return array|int[]
  200. * @throws \think\db\exception\DataNotFoundException
  201. * @throws \think\db\exception\DbException
  202. * @throws \think\db\exception\ModelNotFoundException
  203. */
  204. public function getAgentLevelBrokerage(int $uid, $userInfo = [])
  205. {
  206. $one_brokerage_up = $two_brokerage_up = $spread_one_uid = $spread_two_uid = 0;
  207. if (!$uid) {
  208. return [$one_brokerage_up, $two_brokerage_up, $spread_one_uid, $spread_two_uid];
  209. }
  210. //商城分销是否开启
  211. if (!sys_config('brokerage_func_status')) {
  212. return [$one_brokerage_up, $two_brokerage_up, $spread_one_uid, $spread_two_uid];
  213. }
  214. /** @var UserServices $userServices */
  215. $userServices = app()->make(UserServices::class);
  216. if (!$userInfo) {
  217. $userInfo = $userServices->getUserInfo($uid);
  218. }
  219. if (!$userInfo) {
  220. return [$one_brokerage_up, $two_brokerage_up, $spread_one_uid, $spread_two_uid];
  221. }
  222. //获取上级uid || 开启自购返回自己uid
  223. $spread_one_uid = $userServices->getSpreadUid($uid, $userInfo);
  224. $one_agent_level = 0;
  225. $two_agent_level = 0;
  226. $spread_two_uid = 0;
  227. if ($spread_one_uid > 0 && $one_user_info = $userServices->getUserInfo($spread_one_uid)) {
  228. $one_agent_level = $one_user_info['agent_level'] ?? 0;
  229. $spread_two_uid = $userServices->getSpreadUid($spread_one_uid, $one_user_info, false);
  230. if ($spread_two_uid > 0 && $two_user_info = $userServices->getUserInfo($spread_two_uid)) {
  231. $two_agent_level = $two_user_info['agent_level'] ?? 0;
  232. }
  233. }
  234. $one_brokerage_up = $one_agent_level ? ($this->getLevelInfo($one_agent_level)['one_brokerage'] ?? 0) : 0;
  235. $two_brokerage_up = $two_agent_level ? ($this->getLevelInfo($two_agent_level)['two_brokerage'] ?? 0) : 0;
  236. return [$one_brokerage_up, $two_brokerage_up, $spread_one_uid, $spread_two_uid];
  237. }
  238. /**
  239. * 添加等级表单
  240. * @param int $id
  241. * @return array
  242. * @throws \FormBuilder\Exception\FormBuilderException
  243. */
  244. public function createForm()
  245. {
  246. $field[] = Form::input('name', '等级名称')->maxlength(8)->col(24);
  247. $field[] = Form::number('grade', '等级', 0)->min(0)->precision(0);
  248. $field[] = Form::frameImage('image', '背景图', Url::buildUrl('admin/widget.images/index', array('fodder' => 'image')))->icon('ios-add')->width('950px')->height('505px')->modal(['footer-hide' => true]);
  249. $field[] = Form::number('one_brokerage', '一级上浮', 0)->info('在分销一级佣金基础上浮(0-1000之间整数)百分比')->min(0)->max(1000)->precision(0);
  250. $field[] = Form::number('two_brokerage', '二级上浮', 0)->info('在分销二级佣金基础上浮(0-1000之间整数)百分比')->min(0)->max(1000)->precision(0);
  251. $field[] = Form::radio('status', '是否显示', 1)->options([['value' => 1, 'label' => '显示'], ['value' => 0, 'label' => '隐藏']]);
  252. return create_form('添加分销员等级', $field, Url::buildUrl('/agent/level'), 'POST');
  253. }
  254. /**
  255. * 获取修改等级表单
  256. * @param int $id
  257. * @return array
  258. * @throws \FormBuilder\Exception\FormBuilderException
  259. */
  260. public function editForm(int $id)
  261. {
  262. $levelInfo = $this->getLevelInfo($id);
  263. if (!$levelInfo)
  264. throw new AdminException(100026);
  265. $field = [];
  266. $field[] = Form::hidden('id', $id);
  267. $field[] = Form::input('name', '等级名称', $levelInfo['name'])->maxlength(8)->col(24);
  268. $field[] = Form::number('grade', '等级', $levelInfo['grade'])->min(0)->precision(0);
  269. $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]);
  270. $field[] = Form::number('one_brokerage', '一级上浮', $levelInfo['one_brokerage'])->info('在分销一级佣金基础上浮(0-1000之间整数)百分比')->min(0)->max(1000)->precision(0);
  271. $field[] = Form::number('two_brokerage', '二级上浮', $levelInfo['two_brokerage'])->info('在分销二级佣金基础上浮(0-1000之间整数)百分比')->min(0)->max(1000)->precision(0);
  272. $field[] = Form::radio('status', '是否显示', $levelInfo['status'])->options([['value' => 1, 'label' => '显示'], ['value' => 0, 'label' => '隐藏']]);
  273. return create_form('编辑分销员等级', $field, Url::buildUrl('/agent/level/' . $id), 'PUT');
  274. }
  275. /**
  276. * 赠送分销等级表单
  277. * @param int $uid
  278. * @return array
  279. * @throws \FormBuilder\Exception\FormBuilderException
  280. * @throws \think\db\exception\DataNotFoundException
  281. * @throws \think\db\exception\DbException
  282. * @throws \think\db\exception\ModelNotFoundException
  283. */
  284. public function levelForm(int $uid)
  285. {
  286. /** @var UserServices $userServices */
  287. $userServices = app()->make(UserServices::class);
  288. $userInfo = $userServices->getUserInfo($uid);
  289. if (!$userInfo) {
  290. throw new AdminException(400214);
  291. }
  292. $levelList = $this->dao->getList(['is_del' => 0, 'status' => 1], '*', [], 0, 0, $userInfo['agent_level']);
  293. $setOptionLabel = function () use ($levelList) {
  294. $menus = [];
  295. foreach ($levelList as $level) {
  296. $menus[] = ['value' => $level['id'], 'label' => $level['name']];
  297. }
  298. return $menus;
  299. };
  300. $field[] = Form::hidden('uid', $uid);
  301. $field[] = Form::select('id', '分销等级', $userInfo['agent_level'] ?? 0)->setOptions(Form::setOptions($setOptionLabel))->filterable(true);
  302. return create_form('赠送分销等级', $field, Url::buildUrl('/agent/give_level'), 'post');
  303. }
  304. /**
  305. * 赠送分销等级
  306. * @param int $uid
  307. * @param int $id
  308. * @return bool
  309. * @throws \think\db\exception\DataNotFoundException
  310. * @throws \think\db\exception\DbException
  311. * @throws \think\db\exception\ModelNotFoundException
  312. */
  313. public function givelevel(int $uid, int $id)
  314. {
  315. /** @var UserServices $userServices */
  316. $userServices = app()->make(UserServices::class);
  317. $userInfo = $userServices->getUserInfo($uid, 'uid');
  318. if (!$userInfo) {
  319. throw new AdminException(400214);
  320. }
  321. $levelInfo = $this->getLevelInfo($id, 'id');
  322. if (!$levelInfo) {
  323. throw new AdminException(400442);
  324. }
  325. if ($userServices->update($uid, ['agent_level' => $id]) === false) {
  326. throw new AdminException(400219);
  327. }
  328. return true;
  329. }
  330. }