AgentLevelTaskServices.php 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427
  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\AgentLevelTaskDao;
  13. use app\services\BaseServices;
  14. use app\services\order\StoreOrderServices;
  15. use app\services\user\UserServices;
  16. use crmeb\exceptions\AdminException;
  17. use crmeb\exceptions\ApiException;
  18. use crmeb\services\FormBuilder as Form;
  19. use think\facade\Route as Url;
  20. /**
  21. * Class AgentLevelTaskServices
  22. * @package app\services\agent
  23. */
  24. class AgentLevelTaskServices extends BaseServices
  25. {
  26. /**
  27. * 任务类型
  28. * type 记录在数据库中用来区分任务
  29. * name 任务名 (任务名中的{$num}会自动替换成设置的数字 + 单位)
  30. * max_number 最大设定数值 0为不限定
  31. * min_number 最小设定数值
  32. * unit 单位
  33. * */
  34. protected $TaskType = [
  35. [
  36. 'type' => 1,
  37. 'method' => 'spread',
  38. 'name' => '邀请好友{$num}成为下线',
  39. 'real_name' => '邀请好友成为下线',
  40. 'max_number' => 0,
  41. 'min_number' => 1,
  42. 'unit' => '人'
  43. ],
  44. [
  45. 'type' => 2,
  46. 'method' => 'consumePrice',
  47. 'name' => '自身消费满{$num}',
  48. 'real_name' => '自身消费金额',
  49. 'max_number' => 0,
  50. 'min_number' => 0,
  51. 'unit' => '元'
  52. ],
  53. [
  54. 'type' => 3,
  55. 'method' => 'consumeCount',
  56. 'name' => '自身消费满{$num}',
  57. 'real_name' => '自身消费单数',
  58. 'max_number' => 0,
  59. 'min_number' => 0,
  60. 'unit' => '单'
  61. ],
  62. [
  63. 'type' => 4,
  64. 'method' => 'spreadConsumePrice',
  65. 'name' => '下级消费满{$num}',
  66. 'real_name' => '下级消费金额',
  67. 'max_number' => 0,
  68. 'min_number' => 0,
  69. 'unit' => '元'
  70. ],
  71. [
  72. 'type' => 5,
  73. 'method' => 'spreadConsumeCount',
  74. 'name' => '下级消费满{$num}',
  75. 'real_name' => '下级消费单数',
  76. 'max_number' => 0,
  77. 'min_number' => 0,
  78. 'unit' => '单'
  79. ],
  80. [
  81. 'type' => 6,
  82. 'method' => 'spreadGiftCount',
  83. 'name' => '下级购买礼包单数满{$num}',
  84. 'real_name' => '下级购买礼包',
  85. 'max_number' => 0,
  86. 'min_number' => 0,
  87. 'unit' => '单'
  88. ],
  89. [
  90. 'type' => 7,
  91. 'method' => 'spreadV2Count',
  92. 'name' => '下级成为V2人数满{$num}人',
  93. 'real_name' => '下级团队V2人数',
  94. 'max_number' => 0,
  95. 'min_number' => 0,
  96. 'unit' => '单'
  97. ],
  98. ];
  99. /**
  100. * AgentLevelTaskServices constructor.
  101. * @param AgentLevelTaskDao $dao
  102. */
  103. public function __construct(AgentLevelTaskDao $dao)
  104. {
  105. $this->dao = $dao;
  106. }
  107. /**
  108. * 获取某一个任务信息
  109. * @param int $id
  110. * @param string $field
  111. * @param array $with
  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 getLevelTaskInfo(int $id, string $field = '*', array $with = [])
  118. {
  119. return $this->dao->getOne(['id' => $id, 'is_del' => 0], $field, $with);
  120. }
  121. /**
  122. * 获取等级列表
  123. * @param array $where
  124. * @return array
  125. * @throws \think\db\exception\DataNotFoundException
  126. * @throws \think\db\exception\DbException
  127. * @throws \think\db\exception\ModelNotFoundException
  128. */
  129. public function getLevelTaskList(array $where)
  130. {
  131. $where['is_del'] = 0;
  132. [$page, $limit] = $this->getPageValue();
  133. $list = $this->dao->getTaskList($where, '*', [], $page, $limit);
  134. if ($list) {
  135. $allTyep = $this->getTaskTypeAll();
  136. $allTyep = array_combine(array_column($allTyep, 'type'), $allTyep);
  137. foreach ($list as &$item) {
  138. $item['type_name'] = $allTyep[$item['type']]['real_name'] ?? '';
  139. }
  140. }
  141. $count = $this->dao->count($where);
  142. return compact('count', 'list');
  143. }
  144. /**
  145. * 获取某个等级某个类型任务
  146. * @param int $level_id
  147. * @param int $type
  148. * @return array|\think\Model|null
  149. * @throws \think\db\exception\DataNotFoundException
  150. * @throws \think\db\exception\DbException
  151. * @throws \think\db\exception\ModelNotFoundException
  152. */
  153. public function getLevelTypeTask(int $level_id, int $type = 1)
  154. {
  155. return $this->dao->get(['level_id' => $level_id, 'type' => $type, 'is_del' => 0]);
  156. }
  157. /**
  158. * 添加等级任务表单
  159. * @param int $id
  160. * @return array
  161. * @throws \FormBuilder\Exception\FormBuilderException
  162. */
  163. public function createForm(int $level_id)
  164. {
  165. /** @var AgentLevelServices $levelServices */
  166. $levelServices = app()->make(AgentLevelServices::class);
  167. if (!$levelServices->getLevelInfo($level_id)) {
  168. throw new AdminException(400443);
  169. }
  170. $taskList = $this->getTaskTypeAll();
  171. $setOptionLabel = function () use ($taskList) {
  172. $menus = [];
  173. foreach ($taskList as $task) {
  174. $menus[] = ['value' => $task['type'], 'label' => $task['real_name'] ?? '' . '(' . $task['unit'] ?? '' . ')'];
  175. }
  176. return $menus;
  177. };
  178. $field[] = Form::hidden('level_id', $level_id);
  179. $field[] = Form::select('type', '任务类型')->setOptions(Form::setOptions($setOptionLabel))->filterable(true);
  180. $field[] = Form::input('name', '任务名称')->col(24);
  181. $field[] = Form::number('number', '限定数量', 0)->precision(0);
  182. $field[] = Form::textarea('desc', '任务描述');
  183. $field[] = Form::number('sort', '排序', 0)->precision(0);
  184. $field[] = Form::radio('status', '是否显示', 1)->options([['value' => 1, 'label' => '显示'], ['value' => 0, 'label' => '隐藏']]);
  185. return create_form('添加等级任务', $field, Url::buildUrl('/agent/level_task'), 'POST');
  186. }
  187. /**
  188. * 获取修改任务数据
  189. * @param int $id
  190. * @return array
  191. * @throws \FormBuilder\Exception\FormBuilderException
  192. */
  193. public function editForm(int $id)
  194. {
  195. $levelTaskInfo = $this->getLevelTaskInfo($id);
  196. if (!$levelTaskInfo)
  197. throw new AdminException(100026);
  198. $field = [];
  199. $field[] = Form::hidden('id', $id);
  200. $taskList = $this->getTaskTypeAll();
  201. $setOptionLabel = function () use ($taskList) {
  202. $menus = [];
  203. foreach ($taskList as $task) {
  204. $menus[] = ['value' => $task['type'], 'label' => $task['real_name'] ?? '' . '(' . $task['unit'] ?? '' . ')'];
  205. }
  206. return $menus;
  207. };
  208. $field[] = Form::select('type', '任务类型', $levelTaskInfo['type'])->setOptions(Form::setOptions($setOptionLabel))->filterable(true);
  209. $field[] = Form::input('name', '任务名称', $levelTaskInfo['name']);
  210. $field[] = Form::number('number', '限定数量', $levelTaskInfo['number'])->min(0);
  211. $field[] = Form::textarea('desc', '任务描述', $levelTaskInfo['desc']);
  212. $field[] = Form::number('sort', '排序', $levelTaskInfo['sort'])->precision(0);
  213. $field[] = Form::radio('status', '是否显示', $levelTaskInfo['status'])->options([['value' => 1, 'label' => '显示'], ['value' => 0, 'label' => '隐藏']]);
  214. return create_form('编辑等级任务', $field, Url::buildUrl('/agent/level_task/' . $id), 'PUT');
  215. }
  216. /**
  217. * 获取任务类型
  218. * @return array[]
  219. */
  220. public function getTaskTypeAll()
  221. {
  222. return $this->TaskType;
  223. }
  224. /**
  225. * 获取某个任务
  226. * @param string $type 任务类型
  227. * @return array
  228. * */
  229. public static function getTaskType($type)
  230. {
  231. foreach (self::$TaskType as $item) {
  232. if ($item['type'] == $type) return $item;
  233. }
  234. }
  235. /**
  236. * 获取用户某一个分销等级任务情况
  237. * @param int $uid
  238. * @param int $level_id
  239. * @return array
  240. * @throws \think\db\exception\DataNotFoundException
  241. * @throws \think\db\exception\DbException
  242. * @throws \think\db\exception\ModelNotFoundException
  243. */
  244. public function getUserLevelTaskList(int $uid, int $level_id)
  245. {
  246. //商城分销是否开启
  247. if (!sys_config('brokerage_func_status')) {
  248. return [];
  249. }
  250. /** @var UserServices $userServices */
  251. $userServices = app()->make(UserServices::class);
  252. $user = $userServices->getUserInfo($uid);
  253. if (!$user) {
  254. throw new ApiException(410032);
  255. }
  256. /** @var AgentLevelServices $levelServices */
  257. $levelServices = app()->make(AgentLevelServices::class);
  258. $levelInfo = $levelServices->getLevelInfo($level_id);
  259. if (!$levelInfo) {
  260. throw new ApiException(410071);
  261. }
  262. $taskList = $this->dao->getTaskList(['level_id' => $level_id, 'is_del' => 0, 'status' => 1]);
  263. if ($taskList) {
  264. $userLevel = [];
  265. if ($user['agent_level'] ?? 0) $userLevel = $levelServices->getLevelInfo($user['agent_level']);
  266. $allTyep = $this->getTaskTypeAll();
  267. $allTyep = array_combine(array_column($allTyep, 'type'), $allTyep);
  268. foreach ($taskList as &$task) {
  269. $task['finish'] = 1;
  270. $task['task_type_title'] = '已完成';
  271. $task['speed'] = 100;
  272. $task['new_number'] = $task['number'];
  273. //当前等级之前的等级任务 全部为完成
  274. if (!$userLevel || $userLevel['grade'] < $levelInfo['grade']) {
  275. [$title, $num, $isComplete] = $this->checkLevelTaskFinish($uid, (int)$task['id']);
  276. if (!$isComplete) {
  277. $scale = in_array($task['type'], [2, 4]) ? 2 : 0;
  278. $task['finish'] = 0;
  279. $numdata = bcsub($task['number'], $num, $scale);
  280. $task['task_type_title'] = '还需' . str_replace('{$num}', $numdata . $allTyep[$task['type']]['unit'] ?? '', $title);
  281. $task['speed'] = bcmul((string)bcdiv((string)$num, (string)$task['number'], 2), '100', 0);
  282. $task['new_number'] = $num;
  283. }
  284. }
  285. }
  286. }
  287. return $taskList;
  288. }
  289. /**
  290. * 检测某个任务完成情况
  291. * @param int $uid
  292. * @param int $task_id
  293. * @param array $levelTaskInfo
  294. * @return array|false
  295. * @throws \think\db\exception\DataNotFoundException
  296. * @throws \think\db\exception\DbException
  297. * @throws \think\db\exception\ModelNotFoundException
  298. */
  299. public function checkLevelTaskFinish(int $uid, int $task_id, $levelTaskInfo = [])
  300. {
  301. if (!$levelTaskInfo) {
  302. $levelTaskInfo = $this->getLevelTaskInfo($task_id);
  303. }
  304. if (!$levelTaskInfo) return false;
  305. $allTyep = $this->getTaskTypeAll();
  306. $allTyep = array_combine(array_column($allTyep, 'type'), $allTyep);
  307. $userNumber = 0;
  308. $msg = $allTyep[$levelTaskInfo['type']]['name'] ?? '';
  309. switch ($levelTaskInfo['type']) {
  310. case 1:
  311. /** @var UserServices $userServices */
  312. $userServices = app()->make(UserServices::class);
  313. $userNumber = $userServices->count(['spread_uid' => $uid, 'pid' => 0]);
  314. break;
  315. case 2:
  316. /** @var StoreOrderServices $storeOrderServices */
  317. $storeOrderServices = app()->make(StoreOrderServices::class);
  318. $where = ['uid' => $uid, 'paid' => 1, 'refund_status' => 0, 'pid' => 0];
  319. $userNumber = $storeOrderServices->sum($where, 'pay_price');
  320. break;
  321. case 3:
  322. /** @var StoreOrderServices $storeOrderServices */
  323. $storeOrderServices = app()->make(StoreOrderServices::class);
  324. $where = ['uid' => $uid, 'paid' => 1, 'refund_status' => 0, 'pid' => 0];
  325. $userNumber = $storeOrderServices->count($where);
  326. break;
  327. case 4:
  328. /** @var UserServices $userServices */
  329. $userServices = app()->make(UserServices::class);
  330. $spread_uids = $userServices->getColumn(['spread_uid' => $uid], 'uid');
  331. if ($spread_uids) {
  332. /** @var StoreOrderServices $storeOrderServices */
  333. $storeOrderServices = app()->make(StoreOrderServices::class);
  334. $where = ['uid' => $spread_uids, 'paid' => 1, 'refund_status' => 0, 'pid' => 0];
  335. $userNumber = $storeOrderServices->sum($where, 'pay_price');
  336. }
  337. break;
  338. case 5:
  339. /** @var UserServices $userServices */
  340. $userServices = app()->make(UserServices::class);
  341. $spread_uids = $userServices->getColumn(['spread_uid' => $uid], 'uid');
  342. if ($spread_uids) {
  343. /** @var StoreOrderServices $storeOrderServices */
  344. $storeOrderServices = app()->make(StoreOrderServices::class);
  345. $where = ['uid' => $spread_uids, 'paid' => 1, 'refund_status' => 0, 'pid' => 0];
  346. $userNumber = $storeOrderServices->count($where);
  347. }
  348. break;
  349. default:
  350. return false;
  351. }
  352. $isComplete = false;
  353. if ($userNumber >= $levelTaskInfo['number']) {
  354. /** @var AgentLevelTaskRecordServices $agentLevelTaskRecordServices */
  355. $agentLevelTaskRecordServices = app()->make(AgentLevelTaskRecordServices::class);
  356. $isComplete = true;
  357. if (!$agentLevelTaskRecordServices->get(['uid' => $uid, 'level_id' => $levelTaskInfo['level_id'], 'task_id' => $levelTaskInfo['id']])) {
  358. $data = ['uid' => $uid, 'level_id' => $levelTaskInfo['level_id'], 'task_id' => $levelTaskInfo['id'], 'add_time' => time()];
  359. $isComplete = $agentLevelTaskRecordServices->save($data);
  360. }
  361. }
  362. return [$msg, $userNumber, $isComplete];
  363. }
  364. /**
  365. * 检测等级任务
  366. * @param int $id
  367. * @param array $data
  368. * @return bool
  369. * @throws \think\db\exception\DataNotFoundException
  370. * @throws \think\db\exception\DbException
  371. * @throws \think\db\exception\ModelNotFoundException
  372. */
  373. public function checkTypeTask(int $id, array $data)
  374. {
  375. if (!$id && (!isset($data['level_id']) || !$data['level_id'])) {
  376. throw new AdminException(100100);
  377. }
  378. if ($id) {
  379. $task = $this->getLevelTaskInfo($id);
  380. if (!$task) {
  381. throw new AdminException(100026);
  382. }
  383. $data['level_id'] = $task['level_id'];
  384. }
  385. /** @var AgentLevelServices $agentLevelServices */
  386. $agentLevelServices = app()->make(AgentLevelServices::class);
  387. $levelInfo = $agentLevelServices->getLevelInfo($data['level_id']);
  388. if (!$levelInfo) {
  389. throw new AdminException(100026);
  390. }
  391. $task = $this->dao->getOne(['level_id' => $data['level_id'], 'type' => $data['type'], 'is_del' => 0]);
  392. if (($id && $task && $task['id'] != $id) || (!$id && $task)) {
  393. throw new AdminException(400444);
  394. }
  395. $taskList = $this->dao->getTypTaskList($data['type']);
  396. if ($taskList) {
  397. foreach ($taskList as $taskInfo) {
  398. if (is_null($taskInfo['grade'])) continue;
  399. if ($levelInfo['grade'] > $taskInfo['grade'] && $data['number'] <= $taskInfo['number']) {
  400. throw new AdminException(400445);
  401. }
  402. if ($levelInfo['grade'] < $taskInfo['grade'] && $data['number'] >= $taskInfo['number']) {
  403. throw new AdminException(400446);
  404. }
  405. }
  406. }
  407. return true;
  408. }
  409. }