AgentLevelServices.php 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413
  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\order\StoreOrderServices;
  15. use app\services\user\AwardIntegralServices;
  16. use app\services\user\UserAwardIntegralServices;
  17. use app\services\user\UserBrokerageServices;
  18. use app\services\user\UserExtractServices;
  19. use app\services\user\UserServices;
  20. use app\services\user\UserSpreadServices;
  21. use crmeb\exceptions\AdminException;
  22. use crmeb\services\FormBuilder as Form;
  23. use FormBuilder\Factory\Iview;
  24. use think\exception\ValidateException;
  25. use think\facade\Route as Url;
  26. /**
  27. * 分销等级
  28. * Class AgentLevelServices
  29. * @package app\services\agent
  30. * @mixin AgentLevelDao
  31. */
  32. class AgentLevelServices extends BaseServices
  33. {
  34. /**
  35. * AgentLevelServices constructor.
  36. * @param AgentLevelDao $dao
  37. */
  38. public function __construct(AgentLevelDao $dao)
  39. {
  40. $this->dao = $dao;
  41. }
  42. /**
  43. * 获取某一个等级信息
  44. * @param int $id
  45. * @param string $field
  46. * @param array $with
  47. * @return array|\think\Model|null
  48. * @throws \think\db\exception\DataNotFoundException
  49. * @throws \think\db\exception\DbException
  50. * @throws \think\db\exception\ModelNotFoundException
  51. */
  52. public function getLevelInfo(int $id, string $field = '*', array $with = [])
  53. {
  54. return $this->dao->getOne(['id' => $id, 'is_del' => 0], $field, $with);
  55. }
  56. /**
  57. * 获取等级列表
  58. * @param array $where
  59. * @return array
  60. * @throws \think\db\exception\DataNotFoundException
  61. * @throws \think\db\exception\DbException
  62. * @throws \think\db\exception\ModelNotFoundException
  63. */
  64. public function getLevelList(array $where)
  65. {
  66. $where['is_del'] = 0;
  67. [$page, $limit] = $this->getPageValue();
  68. $count = $this->dao->count($where);
  69. $list = [];
  70. if ($count) {
  71. $list = $this->dao->getList($where, '*', ['task' => function ($query) {
  72. $query->field('count(*) as sum');
  73. }], $page, $limit);
  74. $store_brokerage_ratio = sys_config('store_brokerage_ratio');
  75. $store_brokerage_two = sys_config('store_brokerage_two');
  76. foreach ($list as &$item) {
  77. $item['one_brokerage_ratio'] = $this->compoteBrokerage($store_brokerage_ratio, $item['one_brokerage']);
  78. $item['two_brokerage_ratio'] = $this->compoteBrokerage($store_brokerage_two, $item['two_brokerage']);
  79. }
  80. }
  81. return compact('count', 'list');
  82. }
  83. /**
  84. * 商城获取分销员等级列表
  85. * @param int $uid
  86. * @return array
  87. */
  88. public function getUserlevelList(int $uid)
  89. {
  90. //商城分销是否开启
  91. if (!sys_config('brokerage_func_status')) {
  92. return [];
  93. }
  94. /** @var UserServices $userServices */
  95. $userServices = app()->make(UserServices::class);
  96. $user = $userServices->getUserCacheInfo($uid);
  97. if (!$user) {
  98. throw new ValidateException('没有此用户');
  99. }
  100. try {
  101. //检测升级
  102. $this->checkUserLevelFinish($uid);
  103. } catch (\Throwable $e) {
  104. }
  105. $list = $this->dao->getList(['is_del' => 0, 'status' => 1]);
  106. $agent_level = $user['agent_level'] ?? 0;
  107. //没等级默认最低等级
  108. if (!$agent_level) {
  109. $levelInfo = [];
  110. } else {
  111. $levelInfo = $this->getLevelInfo($agent_level) ?: [];
  112. }
  113. $sum_task = $finish_task = 0;
  114. if ($levelInfo) {
  115. /** @var AgentLevelTaskServices $levelTaskServices */
  116. $levelTaskServices = app()->make(AgentLevelTaskServices::class);
  117. $sum_task = $levelTaskServices->count(['level_id' => $levelInfo['id'], 'is_del' => 0, 'status' => 1]);
  118. /** @var AgentLevelTaskRecordServices $levelTaskRecordServices */
  119. $levelTaskRecordServices = app()->make(AgentLevelTaskRecordServices::class);
  120. $finish_task = $levelTaskRecordServices->count(['level_id' => $levelInfo['id'], 'uid' => $uid]);
  121. }
  122. $levelInfo['sum_task'] = $sum_task;
  123. $levelInfo['finish_task'] = $finish_task;
  124. //推广订单总数
  125. /** @var StoreOrderServices $orderServices */
  126. $orderServices = app()->make(StoreOrderServices::class);
  127. $user['spread_order_count'] = $orderServices->count(['type' => 0, 'paid' => 1, 'refund_status' => [0, 3], 'is_del' => 0, 'is_system_del' => 0, 'spread_or_uid' => $uid]);
  128. //冻结佣金和可提现金额
  129. /** @var UserBrokerageServices $userBrokerageServices */
  130. $userBrokerageServices = app()->make(UserBrokerageServices::class);
  131. $user['broken_commission'] = $userBrokerageServices->getUserFrozenPrice($uid);
  132. $user['commissionCount'] = bcsub($user['brokerage_price'], $user['broken_commission'], 2);
  133. //佣金排行
  134. $user['position_count'] = $userBrokerageServices->getUserBrokerageRank($uid, 'week');
  135. //推广人排行
  136. $startTime = strtotime('this week Monday');
  137. $endTime = time();
  138. $field = 'spread_uid,count(uid) AS count,spread_time';
  139. /** @var UserSpreadServices $userSpreadServices */
  140. $userSpreadServices = app()->make(UserSpreadServices::class);
  141. $rankList = $userSpreadServices->getAgentRankList([$startTime, $endTime], $field);
  142. $rank = 0;
  143. foreach ($rankList as $key => $item) {
  144. if ($item['spread_uid'] == $uid) $rank = $key + 1;
  145. }
  146. $user['rank_count'] = $rank;
  147. $user['spread_count'] = $userServices->count(['spread_uid' => $uid]);
  148. /** @var UserExtractServices $extractService */
  149. $extractService = app()->make(UserExtractServices::class);
  150. $user['extract_price'] = $extractService->sum(['uid' => $uid, 'status' => 1], 'extract_price');
  151. $award_integral_service = app()->make(AwardIntegralServices::class);
  152. // $user['award_lack'] = $award_integral_service->getLake();
  153. // $user['integral_price'] = $award_integral_service->getPrice();
  154. // $user['static_integral'] = $award_integral_service->getIntegralSum(['status' => 0, 'type' => 0, 'uid' => $user['uid']]);
  155. // $user['action_integral'] = $award_integral_service->getIntegralSum(['status' => 0, 'type' => 1, 'uid' => $user['uid']]);
  156. $user['is_default_avatar'] = $user['avatar'] == sys_config('h5_avatar') ? 1 : 0;
  157. $user['achievement'] = $award_integral_service->getAchievement($user['uid']);
  158. return ['user' => $user, 'level_list' => $list, 'level_info' => $levelInfo];
  159. }
  160. /**
  161. * 检测用户是否能升级
  162. * @param int $uid
  163. * @param array $uids
  164. * @return bool
  165. * @throws \think\db\exception\DataNotFoundException
  166. * @throws \think\db\exception\DbException
  167. * @throws \think\db\exception\ModelNotFoundException
  168. */
  169. public function checkUserLevelFinish(int $uid, array $uids = [])
  170. {
  171. //商城分销是否开启
  172. if (!sys_config('brokerage_func_status')) {
  173. return false;
  174. }
  175. /** @var UserServices $userServices */
  176. $userServices = app()->make(UserServices::class);
  177. $userInfo = $userServices->getUserCacheInfo($uid);
  178. if (!$userInfo) {
  179. return false;
  180. }
  181. $list = $this->dao->getList(['is_del' => 0, 'status' => 1]);
  182. if (!$list) {
  183. return false;
  184. }
  185. if (!$uids) {
  186. //获取上级uid || 开启自购返回自己uid
  187. $spread_uid = $userServices->getSpreadUid($uid, $userInfo);
  188. $two_spread_uid = 0;
  189. if ($spread_uid > 0 && $one_user_info = $userServices->getUserCacheInfo($spread_uid)) {
  190. $two_spread_uid = $userServices->getSpreadUid($spread_uid, $one_user_info, false);
  191. }
  192. $uids = array_unique([$uid, $spread_uid, $two_spread_uid]);
  193. }
  194. foreach ($uids as $uid) {
  195. if ($uid <= 0) continue;
  196. if ($uid != $userInfo['uid']) {
  197. $userInfo = $userServices->getUserCacheInfo($uid);
  198. if (!$userInfo)
  199. continue;
  200. }
  201. $now_grade = 0;
  202. if ($userInfo['agent_level']) {
  203. $now_grade = $this->dao->value(['id' => $userInfo['agent_level']], 'grade') ?: 0;
  204. }
  205. foreach ($list as $levelInfo) {
  206. if (!$levelInfo || $levelInfo['grade'] <= $now_grade) {
  207. continue;
  208. }
  209. /** @var AgentLevelTaskServices $levelTaskServices */
  210. $levelTaskServices = app()->make(AgentLevelTaskServices::class);
  211. $task_list = $levelTaskServices->getTaskList(['level_id' => $levelInfo['id'], 'is_del' => 0, 'status' => 1]);
  212. if (!$task_list) {
  213. continue;
  214. }
  215. foreach ($task_list as $task) {
  216. $levelTaskServices->checkLevelTaskFinish($uid, (int)$task['id'], $task);
  217. }
  218. /** @var AgentLevelTaskRecordServices $levelTaskRecordServices */
  219. $levelTaskRecordServices = app()->make(AgentLevelTaskRecordServices::class);
  220. $ids = array_column($task_list, 'id');
  221. $finish_task = $levelTaskRecordServices->count(['level_id' => $levelInfo['id'], 'uid' => $uid, 'task_id' => $ids]);
  222. //任务完成升这一等级
  223. if ($finish_task >= count($task_list)) {
  224. $userServices->update($uid, ['agent_level' => $levelInfo['id']]);
  225. } else {
  226. break;
  227. }
  228. }
  229. }
  230. return true;
  231. }
  232. /**
  233. * 分销等级上浮
  234. * @param int $uid
  235. * @param array $userInfo
  236. * @return array|int[]
  237. * @throws \think\db\exception\DataNotFoundException
  238. * @throws \think\db\exception\DbException
  239. * @throws \think\db\exception\ModelNotFoundException
  240. */
  241. public function getAgentLevelBrokerage(int $uid, $userInfo = [])
  242. {
  243. $one_brokerage_up = $two_brokerage_up = $spread_uid = $spread_two_uid = 0;
  244. $data = [$one_brokerage_up, $two_brokerage_up, $spread_uid, $spread_two_uid];
  245. if (!$uid) {
  246. return $data;
  247. }
  248. //商城分销是否开启
  249. if (!sys_config('brokerage_func_status')) {
  250. return $data;
  251. }
  252. /** @var UserServices $userServices */
  253. $userServices = app()->make(UserServices::class);
  254. if (!$userInfo) {
  255. $userInfo = $userServices->getUserCacheInfo($uid);
  256. }
  257. if (!$userInfo) {
  258. return $data;
  259. }
  260. //获取上级uid || 开启自购返回自己uid
  261. $spread_uid = $userServices->getSpreadUid($uid, $userInfo);
  262. $one_agent_level = 0;
  263. $two_agent_level = 0;;
  264. if ($spread_uid > 0 && $one_user_info = $userServices->getUserInfo($spread_uid)) {
  265. $one_agent_level = $one_user_info['agent_level'] ?? 0;
  266. $spread_two_uid = $userServices->getSpreadUid($spread_uid, $one_user_info, false);
  267. if ($spread_two_uid > 0 && $two_user_info = $userServices->getUserInfo($spread_two_uid)) {
  268. $two_agent_level = $two_user_info['agent_level'] ?? 0;
  269. }
  270. }
  271. $one_brokerage_up = $one_agent_level ? ($this->getLevelInfo($one_agent_level)['one_brokerage'] ?? 0) : 0;
  272. $two_brokerage_up = $two_agent_level ? ($this->getLevelInfo($two_agent_level)['two_brokerage'] ?? 0) : 0;
  273. return [$one_brokerage_up, $two_brokerage_up, $spread_uid, $spread_two_uid];
  274. }
  275. /**
  276. * 计算一二级返佣比率上浮
  277. * @param $ratio
  278. * @param $brokerage
  279. * @return int|string
  280. */
  281. public function compoteBrokerage($ratio, $brokerage)
  282. {
  283. if (!$ratio) {
  284. return 0;
  285. }
  286. $brokerage = bcdiv((string)$brokerage, '100', 4);
  287. if ($brokerage) {
  288. return bcmul((string)$ratio, bcadd('1', (string)$brokerage, 4), 2);
  289. }
  290. return $brokerage;
  291. }
  292. /**
  293. * 添加等级表单
  294. * @param int $id
  295. * @return array
  296. * @throws \FormBuilder\Exception\FormBuilderException
  297. */
  298. public function createForm()
  299. {
  300. $store_brokerage_ratio = sys_config('store_brokerage_ratio');
  301. $store_brokerage_two = sys_config('store_brokerage_two');
  302. $field[] = Form::input('name', '等级名称')->col(24);
  303. $field[] = Form::number('grade', '等级', 0)->min(0)->precision(0);
  304. $field[] = Form::frameImage('image', '背景图', Url::buildUrl(config('admin.admin_prefix') . '/widget.images/index', array('fodder' => 'image')))->icon('ios-add')->width('960px')->height('505px')->modal(['footer-hide' => true])->appendValidate(Iview::validateStr()->required()->message('请选择背景图'));
  305. $field[] = Form::number('one_brokerage', '一级上浮', 0)->info('在分销一级佣金基础上浮(0-1000之间整数)百分比,目前一级返佣比率:' . $store_brokerage_ratio . '%,例如上浮10%,则返佣比率:一级返佣比率 * (1 + 一级上浮比率) = ' . $this->compoteBrokerage($store_brokerage_ratio, 10) . '%')->min(0)->max(1000);
  306. $field[] = Form::number('two_brokerage', '二级上浮', 0)->info('在分销二级佣金基础上浮(0-1000之间整数)百分比,目前二级返佣比率:' . $store_brokerage_two . '%,例如上浮10%,则返佣比率:二级返佣比率 * (1 + 二级上浮比率) = ' . $this->compoteBrokerage($store_brokerage_two, 10) . '%')->min(0)->max(1000);
  307. $field[] = Form::radio('status', '是否显示', 1)->options([['value' => 1, 'label' => '显示'], ['value' => 0, 'label' => '隐藏']]);
  308. return create_form('添加分销员等级', $field, Url::buildUrl('/agent/level'), 'POST');
  309. }
  310. /**
  311. * 获取修改等级表单
  312. * @param int $id
  313. * @return array
  314. * @throws \FormBuilder\Exception\FormBuilderException
  315. */
  316. public function editForm(int $id)
  317. {
  318. $store_brokerage_ratio = sys_config('store_brokerage_ratio');
  319. $store_brokerage_two = sys_config('store_brokerage_two');
  320. $levelInfo = $this->getLevelInfo($id);
  321. if (!$levelInfo)
  322. throw new AdminException('数据不存在');
  323. $field = [];
  324. $field[] = Form::hidden('id', $id);
  325. $field[] = Form::input('name', '等级名称', $levelInfo['name'])->col(24);
  326. $field[] = Form::number('grade', '等级', $levelInfo['grade'])->min(0)->precision(0);
  327. $field[] = Form::frameImage('image', '背景图', Url::buildUrl(config('admin.admin_prefix') . '/widget.images/index', array('fodder' => 'image')), $levelInfo['image'])->icon('ios-add')->width('960px')->height('505px')->modal(['footer-hide' => true])->appendValidate(Iview::validateStr()->required()->message('请选择背景图'));
  328. $field[] = Form::number('one_brokerage', '一级上浮', $levelInfo['one_brokerage'])->info('在分销一级佣金基础上浮(0-1000之间整数)百分比,目前一级返佣比率:' . $store_brokerage_ratio . '%,上浮' . $levelInfo['one_brokerage'] . '%,则返佣比率:一级返佣比率 * (1 + 一级上浮比率) = ' . $this->compoteBrokerage($store_brokerage_ratio, $levelInfo['one_brokerage']) . '%')->min(0)->max(1000);
  329. $field[] = Form::number('two_brokerage', '二级上浮', $levelInfo['two_brokerage'])->info('在分销二级佣金基础上浮(0-1000之间整数)百分比,目前二级返佣比率:' . $store_brokerage_two . '%,上浮' . $levelInfo['two_brokerage'] . '%,则返佣比率:二级返佣比率 * (1 + 二级上浮比率) = ' . $this->compoteBrokerage($store_brokerage_two, $levelInfo['two_brokerage']) . '%')->min(0)->max(1000);
  330. $field[] = Form::radio('status', '是否显示', $levelInfo['status'])->options([['value' => 1, 'label' => '显示'], ['value' => 0, 'label' => '隐藏']]);
  331. return create_form('编辑分销员等级', $field, Url::buildUrl('/agent/level/' . $id), 'PUT');
  332. }
  333. /**
  334. * 赠送分销等级表单
  335. * @param int $uid
  336. * @return array
  337. * @throws \FormBuilder\Exception\FormBuilderException
  338. * @throws \think\db\exception\DataNotFoundException
  339. * @throws \think\db\exception\DbException
  340. * @throws \think\db\exception\ModelNotFoundException
  341. */
  342. public function levelForm(int $uid)
  343. {
  344. /** @var UserServices $userServices */
  345. $userServices = app()->make(UserServices::class);
  346. $userInfo = $userServices->getUserInfo($uid);
  347. if (!$userInfo) {
  348. throw new AdminException('分销员不存在');
  349. }
  350. $levelList = $this->dao->getList(['is_del' => 0, 'status' => 1]);
  351. $setOptionLabel = function () use ($levelList) {
  352. $menus = [];
  353. foreach ($levelList as $level) {
  354. $menus[] = ['value' => $level['id'], 'label' => $level['name']];
  355. }
  356. return $menus;
  357. };
  358. $field[] = Form::hidden('uid', $uid);
  359. $field[] = Form::select('id', '分销等级', $userInfo['agent_level'] ?? 0)->setOptions(Form::setOptions($setOptionLabel))->filterable(true);
  360. return create_form('赠送分销等级', $field, Url::buildUrl('/agent/give_level'), 'post');
  361. }
  362. /**
  363. * 赠送分销等级
  364. * @param int $uid
  365. * @param int $id
  366. * @return bool
  367. * @throws \think\db\exception\DataNotFoundException
  368. * @throws \think\db\exception\DbException
  369. * @throws \think\db\exception\ModelNotFoundException
  370. */
  371. public function givelevel(int $uid, int $id)
  372. {
  373. /** @var UserServices $userServices */
  374. $userServices = app()->make(UserServices::class);
  375. $userInfo = $userServices->getUserInfo($uid);
  376. if (!$userInfo) {
  377. throw new AdminException('分销员不存在');
  378. }
  379. $levelInfo = $this->getLevelInfo($id);
  380. if (!$levelInfo) {
  381. throw new AdminException('分销等级不存在');
  382. }
  383. if ($userServices->update($uid, ['agent_level' => $id]) === false) {
  384. throw new AdminException('赠送失败');
  385. }
  386. return true;
  387. }
  388. }