AgentLevelServices.php 17 KB

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