AgentLevelServices.php 14 KB

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