AgentLevelTaskServices.php 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409
  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. /**
  82. * AgentLevelTaskServices constructor.
  83. * @param AgentLevelTaskDao $dao
  84. */
  85. public function __construct(AgentLevelTaskDao $dao)
  86. {
  87. $this->dao = $dao;
  88. }
  89. /**
  90. * 获取某一个任务信息
  91. * @param int $id
  92. * @param string $field
  93. * @param array $with
  94. * @return array|\think\Model|null
  95. * @throws \think\db\exception\DataNotFoundException
  96. * @throws \think\db\exception\DbException
  97. * @throws \think\db\exception\ModelNotFoundException
  98. */
  99. public function getLevelTaskInfo(int $id, string $field = '*', array $with = [])
  100. {
  101. return $this->dao->getOne(['id' => $id, 'is_del' => 0], $field, $with);
  102. }
  103. /**
  104. * 获取等级列表
  105. * @param array $where
  106. * @return array
  107. * @throws \think\db\exception\DataNotFoundException
  108. * @throws \think\db\exception\DbException
  109. * @throws \think\db\exception\ModelNotFoundException
  110. */
  111. public function getLevelTaskList(array $where)
  112. {
  113. $where['is_del'] = 0;
  114. [$page, $limit] = $this->getPageValue();
  115. $list = $this->dao->getTaskList($where, '*', [], $page, $limit);
  116. if ($list) {
  117. $allTyep = $this->getTaskTypeAll();
  118. $allTyep = array_combine(array_column($allTyep, 'type'), $allTyep);
  119. foreach ($list as &$item) {
  120. $item['type_name'] = $allTyep[$item['type']]['real_name'] ?? '';
  121. }
  122. }
  123. $count = $this->dao->count($where);
  124. return compact('count', 'list');
  125. }
  126. /**
  127. * 获取某个等级某个类型任务
  128. * @param int $level_id
  129. * @param int $type
  130. * @return array|\think\Model|null
  131. * @throws \think\db\exception\DataNotFoundException
  132. * @throws \think\db\exception\DbException
  133. * @throws \think\db\exception\ModelNotFoundException
  134. */
  135. public function getLevelTypeTask(int $level_id, int $type = 1)
  136. {
  137. return $this->dao->get(['level_id' => $level_id, 'type' => $type, 'is_del' => 0]);
  138. }
  139. /**
  140. * 添加等级任务表单
  141. * @param int $id
  142. * @return array
  143. * @throws \FormBuilder\Exception\FormBuilderException
  144. */
  145. public function createForm(int $level_id)
  146. {
  147. /** @var AgentLevelServices $levelServices */
  148. $levelServices = app()->make(AgentLevelServices::class);
  149. if (!$levelServices->getLevelInfo($level_id)) {
  150. throw new AdminException(400443);
  151. }
  152. $taskList = $this->getTaskTypeAll();
  153. $setOptionLabel = function () use ($taskList) {
  154. $menus = [];
  155. foreach ($taskList as $task) {
  156. $menus[] = ['value' => $task['type'], 'label' => $task['real_name'] ?? '' . '(' . $task['unit'] ?? '' . ')'];
  157. }
  158. return $menus;
  159. };
  160. $field[] = Form::hidden('level_id', $level_id);
  161. $field[] = Form::select('type', '任务类型')->setOptions(Form::setOptions($setOptionLabel))->filterable(true);
  162. $field[] = Form::input('name', '任务名称')->col(24);
  163. $field[] = Form::number('number', '限定数量', 0)->precision(0);
  164. $field[] = Form::textarea('desc', '任务描述');
  165. $field[] = Form::number('sort', '排序', 0)->precision(0);
  166. $field[] = Form::radio('status', '是否显示', 1)->options([['value' => 1, 'label' => '显示'], ['value' => 0, 'label' => '隐藏']]);
  167. return create_form('添加等级任务', $field, Url::buildUrl('/agent/level_task'), 'POST');
  168. }
  169. /**
  170. * 获取修改任务数据
  171. * @param int $id
  172. * @return array
  173. * @throws \FormBuilder\Exception\FormBuilderException
  174. */
  175. public function editForm(int $id)
  176. {
  177. $levelTaskInfo = $this->getLevelTaskInfo($id);
  178. if (!$levelTaskInfo)
  179. throw new AdminException(100026);
  180. $field = [];
  181. $field[] = Form::hidden('id', $id);
  182. $taskList = $this->getTaskTypeAll();
  183. $setOptionLabel = function () use ($taskList) {
  184. $menus = [];
  185. foreach ($taskList as $task) {
  186. $menus[] = ['value' => $task['type'], 'label' => $task['real_name'] ?? '' . '(' . $task['unit'] ?? '' . ')'];
  187. }
  188. return $menus;
  189. };
  190. $field[] = Form::select('type', '任务类型', $levelTaskInfo['type'])->setOptions(Form::setOptions($setOptionLabel))->filterable(true);
  191. $field[] = Form::input('name', '任务名称', $levelTaskInfo['name']);
  192. $field[] = Form::number('number', '限定数量', $levelTaskInfo['number'])->min(0);
  193. $field[] = Form::textarea('desc', '任务描述', $levelTaskInfo['desc']);
  194. $field[] = Form::number('sort', '排序', $levelTaskInfo['sort'])->precision(0);
  195. $field[] = Form::radio('status', '是否显示', $levelTaskInfo['status'])->options([['value' => 1, 'label' => '显示'], ['value' => 0, 'label' => '隐藏']]);
  196. return create_form('编辑等级任务', $field, Url::buildUrl('/agent/level_task/' . $id), 'PUT');
  197. }
  198. /**
  199. * 获取任务类型
  200. * @return array[]
  201. */
  202. public function getTaskTypeAll()
  203. {
  204. return $this->TaskType;
  205. }
  206. /**
  207. * 获取某个任务
  208. * @param string $type 任务类型
  209. * @return array
  210. * */
  211. public static function getTaskType($type)
  212. {
  213. foreach (self::$TaskType as $item) {
  214. if ($item['type'] == $type) return $item;
  215. }
  216. }
  217. /**
  218. * 获取用户某一个分销等级任务情况
  219. * @param int $uid
  220. * @param int $level_id
  221. * @return array
  222. * @throws \think\db\exception\DataNotFoundException
  223. * @throws \think\db\exception\DbException
  224. * @throws \think\db\exception\ModelNotFoundException
  225. */
  226. public function getUserLevelTaskList(int $uid, int $level_id)
  227. {
  228. //商城分销是否开启
  229. if (!sys_config('brokerage_func_status')) {
  230. return [];
  231. }
  232. /** @var UserServices $userServices */
  233. $userServices = app()->make(UserServices::class);
  234. $user = $userServices->getUserInfo($uid);
  235. if (!$user) {
  236. throw new ApiException(410032);
  237. }
  238. /** @var AgentLevelServices $levelServices */
  239. $levelServices = app()->make(AgentLevelServices::class);
  240. $levelInfo = $levelServices->getLevelInfo($level_id);
  241. if (!$levelInfo) {
  242. throw new ApiException(410071);
  243. }
  244. $taskList = $this->dao->getTaskList(['level_id' => $level_id, 'is_del' => 0, 'status' => 1]);
  245. if ($taskList) {
  246. $userLevel = [];
  247. if ($user['agent_level'] ?? 0) $userLevel = $levelServices->getLevelInfo($user['agent_level']);
  248. $allTyep = $this->getTaskTypeAll();
  249. $allTyep = array_combine(array_column($allTyep, 'type'), $allTyep);
  250. foreach ($taskList as &$task) {
  251. $task['finish'] = 1;
  252. $task['task_type_title'] = '已完成';
  253. $task['speed'] = 100;
  254. $task['new_number'] = $task['number'];
  255. //当前等级之前的等级任务 全部为完成
  256. if (!$userLevel || $userLevel['grade'] < $levelInfo['grade']) {
  257. [$title, $num, $isComplete] = $this->checkLevelTaskFinish($uid, (int)$task['id']);
  258. if (!$isComplete) {
  259. $scale = in_array($task['type'], [2, 4]) ? 2 : 0;
  260. $task['finish'] = 0;
  261. $numdata = bcsub($task['number'], $num, $scale);
  262. $task['task_type_title'] = '还需' . str_replace('{$num}', $numdata . $allTyep[$task['type']]['unit'] ?? '', $title);
  263. $task['speed'] = bcmul((string)bcdiv((string)$num, (string)$task['number'], 2), '100', 0);
  264. $task['new_number'] = $num;
  265. }
  266. }
  267. }
  268. }
  269. return $taskList;
  270. }
  271. /**
  272. * 检测某个任务完成情况
  273. * @param int $uid
  274. * @param int $task_id
  275. * @param array $levelTaskInfo
  276. * @return array|false
  277. * @throws \think\db\exception\DataNotFoundException
  278. * @throws \think\db\exception\DbException
  279. * @throws \think\db\exception\ModelNotFoundException
  280. */
  281. public function checkLevelTaskFinish(int $uid, int $task_id, $levelTaskInfo = [])
  282. {
  283. if (!$levelTaskInfo) {
  284. $levelTaskInfo = $this->getLevelTaskInfo($task_id);
  285. }
  286. if (!$levelTaskInfo) return false;
  287. $allTyep = $this->getTaskTypeAll();
  288. $allTyep = array_combine(array_column($allTyep, 'type'), $allTyep);
  289. $userNumber = 0;
  290. $msg = $allTyep[$levelTaskInfo['type']]['name'] ?? '';
  291. switch ($levelTaskInfo['type']) {
  292. case 1:
  293. /** @var UserServices $userServices */
  294. $userServices = app()->make(UserServices::class);
  295. $userNumber = $userServices->count(['spread_uid' => $uid, 'pid' => 0]);
  296. break;
  297. case 2:
  298. /** @var StoreOrderServices $storeOrderServices */
  299. $storeOrderServices = app()->make(StoreOrderServices::class);
  300. $where = ['uid' => $uid, 'paid' => 1, 'refund_status' => 0, 'pid' => 0];
  301. $userNumber = $storeOrderServices->sum($where, 'pay_price');
  302. break;
  303. case 3:
  304. /** @var StoreOrderServices $storeOrderServices */
  305. $storeOrderServices = app()->make(StoreOrderServices::class);
  306. $where = ['uid' => $uid, 'paid' => 1, 'refund_status' => 0, 'pid' => 0];
  307. $userNumber = $storeOrderServices->count($where);
  308. break;
  309. case 4:
  310. /** @var UserServices $userServices */
  311. $userServices = app()->make(UserServices::class);
  312. $spread_uids = $userServices->getColumn(['spread_uid' => $uid], 'uid');
  313. if ($spread_uids) {
  314. /** @var StoreOrderServices $storeOrderServices */
  315. $storeOrderServices = app()->make(StoreOrderServices::class);
  316. $where = ['uid' => $spread_uids, 'paid' => 1, 'refund_status' => 0, 'pid' => 0];
  317. $userNumber = $storeOrderServices->sum($where, 'pay_price');
  318. }
  319. break;
  320. case 5:
  321. /** @var UserServices $userServices */
  322. $userServices = app()->make(UserServices::class);
  323. $spread_uids = $userServices->getColumn(['spread_uid' => $uid], 'uid');
  324. if ($spread_uids) {
  325. /** @var StoreOrderServices $storeOrderServices */
  326. $storeOrderServices = app()->make(StoreOrderServices::class);
  327. $where = ['uid' => $spread_uids, 'paid' => 1, 'refund_status' => 0, 'pid' => 0];
  328. $userNumber = $storeOrderServices->count($where);
  329. }
  330. break;
  331. default:
  332. return false;
  333. }
  334. $isComplete = false;
  335. if ($userNumber >= $levelTaskInfo['number']) {
  336. /** @var AgentLevelTaskRecordServices $agentLevelTaskRecordServices */
  337. $agentLevelTaskRecordServices = app()->make(AgentLevelTaskRecordServices::class);
  338. $isComplete = true;
  339. if (!$agentLevelTaskRecordServices->get(['uid' => $uid, 'level_id' => $levelTaskInfo['level_id'], 'task_id' => $levelTaskInfo['id']])) {
  340. $data = ['uid' => $uid, 'level_id' => $levelTaskInfo['level_id'], 'task_id' => $levelTaskInfo['id'], 'add_time' => time()];
  341. $isComplete = $agentLevelTaskRecordServices->save($data);
  342. }
  343. }
  344. return [$msg, $userNumber, $isComplete];
  345. }
  346. /**
  347. * 检测等级任务
  348. * @param int $id
  349. * @param array $data
  350. * @return bool
  351. * @throws \think\db\exception\DataNotFoundException
  352. * @throws \think\db\exception\DbException
  353. * @throws \think\db\exception\ModelNotFoundException
  354. */
  355. public function checkTypeTask(int $id, array $data)
  356. {
  357. if (!$id && (!isset($data['level_id']) || !$data['level_id'])) {
  358. throw new AdminException(100100);
  359. }
  360. if ($id) {
  361. $task = $this->getLevelTaskInfo($id);
  362. if (!$task) {
  363. throw new AdminException(100026);
  364. }
  365. $data['level_id'] = $task['level_id'];
  366. }
  367. /** @var AgentLevelServices $agentLevelServices */
  368. $agentLevelServices = app()->make(AgentLevelServices::class);
  369. $levelInfo = $agentLevelServices->getLevelInfo($data['level_id']);
  370. if (!$levelInfo) {
  371. throw new AdminException(100026);
  372. }
  373. $task = $this->dao->getOne(['level_id' => $data['level_id'], 'type' => $data['type'], 'is_del' => 0]);
  374. if (($id && $task && $task['id'] != $id) || (!$id && $task)) {
  375. throw new AdminException(400444);
  376. }
  377. $taskList = $this->dao->getTypTaskList($data['type']);
  378. if ($taskList) {
  379. foreach ($taskList as $taskInfo) {
  380. if (is_null($taskInfo['grade'])) continue;
  381. if ($levelInfo['grade'] > $taskInfo['grade'] && $data['number'] <= $taskInfo['number']) {
  382. throw new AdminException(400445);
  383. }
  384. if ($levelInfo['grade'] < $taskInfo['grade'] && $data['number'] >= $taskInfo['number']) {
  385. throw new AdminException(400446);
  386. }
  387. }
  388. }
  389. return true;
  390. }
  391. }