AgentLevelServices.php 14 KB

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