SystemUserTask.php 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488
  1. <?php
  2. namespace app\models\system;
  3. use app\models\store\StoreOrder;
  4. use app\models\user\User;
  5. use app\models\user\UserBill;
  6. use app\models\user\UserLevel;
  7. use app\models\user\UserTaskFinish;
  8. use crmeb\traits\ModelTrait;
  9. use crmeb\basic\BaseModel;
  10. use think\db\exception\DataNotFoundException;
  11. use think\db\exception\DbException;
  12. use think\db\exception\ModelNotFoundException;
  13. /**
  14. * TODO 设置等级任务Model
  15. * Class SystemUserTask
  16. * @package app\models\system
  17. */
  18. class SystemUserTask extends BaseModel
  19. {
  20. /**
  21. * 数据表主键
  22. * @var string
  23. */
  24. protected $pk = 'id';
  25. /**
  26. * 模型名称
  27. * @var string
  28. */
  29. protected $name = 'system_user_task';
  30. use ModelTrait;
  31. /**
  32. * 任务类型
  33. * type 记录在数据库中用来区分任务
  34. * name 任务名 (任务名中的{$num}会自动替换成设置的数字 + 单位)
  35. * max_number 最大设定数值 0为不限定
  36. * min_number 最小设定数值
  37. * unit 单位
  38. * */
  39. protected static $TaskType = [
  40. [
  41. 'type' => 'SatisfactionIntegral',
  42. 'name' => '满足积分{$num}',
  43. 'real_name' => '积分数',
  44. 'max_number' => 0,
  45. 'min_number' => 0,
  46. 'unit' => '分'
  47. ],
  48. [
  49. 'type' => 'ConsumptionAmount',
  50. 'name' => '消费满{$num}',
  51. 'real_name' => '消费金额',
  52. 'max_number' => 0,
  53. 'min_number' => 0,
  54. 'unit' => '元'
  55. ],
  56. [
  57. 'type' => 'ConsumptionFrequency',
  58. 'name' => '消费{$num}',
  59. 'real_name' => '消费次数',
  60. 'max_number' => 0,
  61. 'min_number' => 0,
  62. 'unit' => '次'
  63. ],
  64. [
  65. 'type' => 'TeamConsumptionFrequency',
  66. 'name' => '团队消费{$num}',
  67. 'real_name' => '消费次数',
  68. 'max_number' => 0,
  69. 'min_number' => 0,
  70. 'unit' => '次'
  71. ],
  72. [
  73. 'type' => 'CumulativeAttendance',
  74. 'name' => '累计签到{$num}',
  75. 'real_name' => '累计签到',
  76. 'max_number' => 365,
  77. 'min_number' => 1,
  78. 'unit' => '天'
  79. ],
  80. [
  81. 'type' => 'SharingTimes',
  82. 'name' => '分享给朋友{$num}',
  83. 'real_name' => '分享给朋友',
  84. 'max_number' => 1000,
  85. 'min_number' => 1,
  86. 'unit' => '次'
  87. ],
  88. [
  89. 'type' => 'InviteGoodFriends',
  90. 'name' => '邀请好友{$num}成为下线',
  91. 'real_name' => '邀请好友成为下线',
  92. 'max_number' => 1000,
  93. 'min_number' => 1,
  94. 'unit' => '人'
  95. ],
  96. [
  97. 'type' => 'InviteGoodFriendsLevel',
  98. 'name' => '邀请好友{$num}成为会员',
  99. 'real_name' => '邀请好友成为会员',
  100. 'max_number' => 1000,
  101. 'min_number' => 1,
  102. 'unit' => '人'
  103. ],
  104. ];
  105. public function profile()
  106. {
  107. return $this->hasOne('SystemUserLevel', 'level_id', 'id')->field('name');
  108. }
  109. public static function getTaskTypeAll()
  110. {
  111. return self::$TaskType;
  112. }
  113. /**
  114. * 获取某个任务
  115. * @param string $type 任务类型
  116. * @return array
  117. * */
  118. public static function getTaskType($type)
  119. {
  120. foreach (self::$TaskType as $item) {
  121. if ($item['type'] == $type) return $item;
  122. }
  123. }
  124. /**
  125. * 设置任务名
  126. * @param string $type 任务类型
  127. * @param int $num 预设值
  128. * @return string
  129. * */
  130. public static function setTaskName($type, $num)
  131. {
  132. $systemType = self::getTaskType($type);
  133. return str_replace('{$num}', $num . $systemType['unit'], $systemType['name']);
  134. }
  135. /**
  136. * 累计消费金额
  137. * @param int $task_id 任务id
  138. * @param int $uid 用户id
  139. * @param int $start_time 开始时间
  140. * @param int $number 限定时间
  141. * @return boolean
  142. * */
  143. public static function ConsumptionAmount($task_id, $uid = 0, $start_time = 0, $number = 0)
  144. {
  145. $isComplete = false;
  146. $SumPayPrice = StoreOrder::where('paid', 1)->where('refund_status', 0)->where('is_del', 0)->where('uid', $uid)->where('add_time', '>', $start_time)->sum('pay_price');
  147. if ($SumPayPrice >= $number) $isComplete = UserTaskFinish::setFinish($uid, $task_id) ? true : false;
  148. return ['还需消费{$num}元', $SumPayPrice, $isComplete];
  149. }
  150. /**
  151. * 累计消费次数
  152. * @param int $task_id 任务id
  153. * @param int $uid 用户id
  154. * @param int $start_time 开始时间
  155. * @param int $number 限定时间
  156. * @return boolean
  157. * */
  158. public static function ConsumptionFrequency($task_id, $uid = 0, $start_time = 0, $number = 0)
  159. {
  160. $isComplete = false;
  161. $countPay = StoreOrder::where('paid', 1)->where('refund_status', 0)->where('is_del', 0)->where('uid', $uid)->where('add_time', '>', $start_time)->count();
  162. if ($countPay >= $number) $isComplete = UserTaskFinish::setFinish($uid, $task_id) ? true : false;
  163. return ['还需消费{$num}次', $countPay, $isComplete];
  164. }
  165. /**
  166. * 团队累计消费次数
  167. * @param int $task_id 任务id
  168. * @param int $uid 用户id
  169. * @param int $start_time 开始时间
  170. * @param int $number 限定时间
  171. * @return boolean
  172. * */
  173. public static function TeamConsumptionFrequency($task_id, $uid = 0, $start_time = 0, $number = 0)
  174. {
  175. $isComplete = false;
  176. $team_users = self::getAllTeamMembers($uid);
  177. $countPay = User::where('uid','in',$team_users)->sum('pay_count');
  178. // $countPay = StoreOrder::where('paid', 1)->where('refund_status', 0)->where('is_del', 0)->where('uid', $uid)->where('add_time', '>', $start_time)->count();
  179. if ($countPay >= $number) $isComplete = UserTaskFinish::setFinish($uid, $task_id) ? true : false;
  180. return ['还需团队消费{$num}次', $countPay, $isComplete];
  181. }
  182. /**
  183. * 邀请好友成为会员
  184. * @param int $task_id 任务id
  185. * @param int $uid 用户id
  186. * @param int $start_time 开始时间
  187. * @param int $number 限定时间
  188. * @return boolean
  189. * */
  190. public static function InviteGoodFriendsLevel($task_id, $uid = 0, $start_time = 0, $number = 0)
  191. {
  192. $isComplete = false;
  193. $uids = User::where('spread_uid', $uid)->where('spread_time', '>', $start_time)->column('uid', 'uid');
  194. $levelCount = count($uids) ? UserLevel::setUserLevelCount($uids) : 0;
  195. if ($levelCount >= $number) $isComplete = UserTaskFinish::setFinish($uid, $task_id) ? true : false;
  196. return ['还需邀请{$num}人成为会员', $levelCount, $isComplete];
  197. }
  198. /**
  199. * 邀请好友成为下线
  200. * @param int $task_id 任务id
  201. * @param int $uid 用户id
  202. * @param int $start_time 查询开始时间
  203. * @param int $number 限定数量
  204. * */
  205. public static function InviteGoodFriends($task_id, $uid = 0, $start_time = 0, $number = 0)
  206. {
  207. $isComplete = false;
  208. $spreadCount = User::where('spread_uid', $uid)->where('spread_time', '>', $start_time)->count();
  209. if ($spreadCount >= $number) $isComplete = UserTaskFinish::setFinish($uid, $task_id) ? true : false;
  210. return ['还需邀请{$num}人成为下线', $spreadCount, $isComplete];
  211. }
  212. /**
  213. * 满足积分
  214. * @param int $task_id 任务id
  215. * @param int $uid 用户id
  216. * @param int $start_time 查询开始时间
  217. * @param int $number 限定数量
  218. * @return Boolean
  219. * */
  220. public static function SatisfactionIntegral($task_id, $uid = 0, $start_time = 0, $number = 0)
  221. {
  222. $isComplete = false;
  223. $sumNumber = UserBill::where('uid', $uid)->where('category', 'integral')->where('pm', 1)->where('add_time', '>', $start_time)->where('type', 'in', ['system_add', 'sign', 'gain'])->sum('number');
  224. if ($sumNumber >= $number) $isComplete = UserTaskFinish::setFinish($uid, $task_id) ? true : false;
  225. return ['还需要{$num}经验', $sumNumber, $isComplete];
  226. }
  227. /**
  228. * 分享给朋友次数完成情况
  229. * @param int $task_id 任务id
  230. * @param int $uid 用户id
  231. * @param int $start_time 查询开始时间
  232. * @param int $number 限定数量
  233. * @return Boolean
  234. * */
  235. public static function SharingTimes($task_id, $uid = 0, $start_time = 0, $number = 0)
  236. {
  237. $isComplete = false;
  238. $sumCount = UserBill::where('uid', $uid)->where('category', 'share')->where('pm', 1)->where('add_time', '>', $start_time)->where('type', 'share')->count();
  239. if ($sumCount >= $number) $isComplete = UserTaskFinish::setFinish($uid, $task_id) ? true : false;
  240. return ['还需分享{$num}次', $sumCount, $isComplete];
  241. }
  242. /**
  243. * 累计签到
  244. * @param int $task_id 任务id
  245. * @param int $uid 用户id
  246. * @param int $start_time 查询开始时间
  247. * @param int $number 限定数量
  248. * @return Boolean
  249. * */
  250. public static function CumulativeAttendance($task_id, $uid = 0, $start_time = 0, $number = 0)
  251. {
  252. $isComplete = false;
  253. $sumCount = UserBill::where('uid', $uid)->where('category', 'integral')->where('pm', 1)->where('add_time', '>', $start_time)->where('type', 'sign')->count();
  254. if ($sumCount >= $number) $isComplete = UserTaskFinish::setFinish($uid, $task_id) ? true : false;
  255. return ['还需签到{$num}天', $sumCount, $isComplete];
  256. }
  257. /**
  258. * 设置任务完成情况
  259. * @param int $task_id 任务id
  260. * @param int $uid 用户uid
  261. * @param int $start_time 查询开始时间
  262. * @return Boolean
  263. * */
  264. public static function setTaskFinish($task_id = 0, $uid = 0, $start_time = 0)
  265. {
  266. if (!$task_id) return self::setErrorInfo('缺少任务id参数');
  267. if (!$uid) return self::setErrorInfo('缺少用户uid');
  268. $task = self::where('id', $task_id)->where('is_show', 1)->find();
  269. if (!$task) return self::setErrorInfo('任务不存在');
  270. $task_type = $task->task_type;
  271. if ($task_type && method_exists(self::class, $task_type)) {
  272. try {
  273. $start_time = User::getCleanTime($uid);
  274. return self::$task_type($task_id, $uid, $start_time, $task->number);
  275. } catch (\Exception $e) {
  276. return self::setErrorInfo($e->getMessage());
  277. }
  278. }
  279. return self::setErrorInfo('没有此任务');
  280. }
  281. /**
  282. * 设置任务显示条件
  283. * @param string $alert 表别名
  284. * @param object $model 模型实例
  285. * @return object
  286. * */
  287. public static function visibleWhere($alert = '', $model = null)
  288. {
  289. $model = $model === null ? new self() : $model;
  290. if ($alert) $model = $model->alias($alert);
  291. $alert = $alert ? $alert . '.' : '';
  292. return $model->where("{$alert}is_show", 1);
  293. }
  294. /**
  295. * 获取等级会员任务列表
  296. * @param int $level_id 会员等级id
  297. * @param int $uid 用户id
  298. * @param null $level
  299. * @param int $expire
  300. * @return array
  301. * @throws DataNotFoundException
  302. * @throws DbException
  303. * @throws ModelNotFoundException
  304. */
  305. public static function getTashList($level_id, $uid = 0, $level = null, $expire = 1400)
  306. {
  307. $level_id = is_string($level_id) ? (int)$level_id : $level_id;
  308. $list = self::visibleWhere()->where('level_id', $level_id)->field('name,real_name,task_type,illustrate,number,id')->order('sort desc')->select();
  309. $list = count($list) ? $list->toArray() : [];
  310. if ($uid == 0) return $list;
  311. if ($level === null) $level = SystemUserLevel::getLevelInfo($uid);
  312. //获取下一个vip的id
  313. $LeveId = SystemUserLevel::getNextLevelId($level['id'], SystemUserLevel::setWhere()->where('grade', '>', $level['grade'] ?? 0)->min('grade'));
  314. $is_clear = SystemUserLevel::getClear($level['id']);
  315. if ($is_clear == false && $LeveId == $level_id) $is_clear = true;
  316. $reach_count = self::getTaskComplete($level_id, $uid, true);
  317. return [
  318. 'list' => $list,
  319. 'reach_count' => $reach_count,
  320. 'task' => self::tidyTask($list, $uid, $is_clear, User::getCleanTime($uid)),
  321. ];
  322. }
  323. /**
  324. * 获取未完成任务的详细值
  325. * @param array $item 任务
  326. * @param int $uid 用户id
  327. * @param int $startTime 开始时间
  328. * @return array
  329. * */
  330. protected static function set_task_type($item, $uid, $startTime = 0)
  331. {
  332. $task = ['task_type_title' => '', 'new_number' => 0, 'speed' => 0, 'finish' => 0];
  333. $task_type = $item['task_type'];
  334. switch ($task_type) {
  335. case 'SatisfactionIntegral':
  336. case 'ConsumptionAmount':
  337. case 'ConsumptionFrequency':
  338. case 'TeamConsumptionFrequency':
  339. case 'CumulativeAttendance':
  340. case 'SharingTimes':
  341. case 'InviteGoodFriends':
  342. case 'InviteGoodFriendsLevel':
  343. try {
  344. list($task_type_title, $num, $isComplete) = self::$task_type($item['id'], $uid, $startTime, $item['number']);
  345. if ($isComplete) {
  346. $task['finish'] = 1;
  347. $task['speed'] = 100;
  348. $task['speed'] = $item['number'];
  349. $task['new_number'] = $item['number'];
  350. } else {
  351. $numdata = bcsub($item['number'], $num, 0);
  352. $task['task_type_title'] = str_replace('{$num}', $numdata, $task_type_title);
  353. $task['speed'] = bcdiv($num, $item['number'], 2);
  354. $task['speed'] = bcmul($task['speed'], 100, 0);
  355. $task['new_number'] = $num;
  356. }
  357. } catch (\Exception $e) {
  358. }
  359. break;
  360. }
  361. return [$task['new_number'], $task['speed'], $task['task_type_title'], $task['finish']];
  362. }
  363. /**
  364. * 设置任务完成状态,已被使用
  365. * @param int $level_id 会员id
  366. * @param int $uid 用户id
  367. * @return Boolean
  368. * */
  369. public static function setTarkStatus($level_id, $uid)
  370. {
  371. $taskIds = self::visibleWhere()->where('level_id', $level_id)->column('id', 'id');
  372. if (!count($taskIds)) return true;
  373. return UserTaskFinish::where('uid', $uid)->where('task_id', 'in', $taskIds)->update(['status' => 1]);
  374. }
  375. /**
  376. * 检查当前等级是否完成全部任务
  377. * @param int $level_id 会员id
  378. * @param int $uid 用户uid
  379. * @return boolean
  380. * */
  381. public static function getTaskComplete($level_id, $uid, $isCount = false)
  382. {
  383. $taskIds = self::visibleWhere()->where('level_id', $level_id)->column('id', 'id');
  384. $taskIdsCount = count($taskIds);
  385. //如果当前会员没有任务默认为直接升级为下一等级
  386. if ($taskIdsCount) {
  387. if ($isCount) {
  388. return UserTaskFinish::group('task_id')->where('uid', $uid)->where('task_id', 'in', $taskIds)->count();
  389. } else {
  390. $finishCount = UserTaskFinish::group('task_id')->where('status', $isCount ? 1 : 0)->where('uid', $uid)->where('task_id', 'in', implode(',', $taskIds))->count();
  391. }
  392. //如果当前任务有完成其一的,查询当前完成的任务数量,如果有任务完成则达成当前vip
  393. if (self::visibleWhere()->where('id', 'in', implode(',', $taskIds))->where('is_must', 0)->count() && $finishCount) {
  394. return true;
  395. }
  396. return $finishCount >= $taskIdsCount;
  397. }
  398. if ($isCount) return 0;
  399. //如果没有设置任务当前等级无需购买则返回false
  400. if (SystemUserLevel::be(['id' => $level_id, 'is_pay' => 0])) return false;
  401. return true;
  402. }
  403. /**
  404. * 设置任务内容完成情况
  405. * @param array $task 任务列表
  406. * @param int $uid 用户id
  407. * @热图图呢 array
  408. * */
  409. public static function tidyTask($task, $uid, $is_clear, $startTime)
  410. {
  411. if (!is_array($task)) return $task;
  412. foreach ($task as &$item) {
  413. //如果已完成该任务进度直接为100
  414. if (UserTaskFinish::where('uid', $uid)->where('task_id', $item['id'])->count()) {
  415. $item['new_number'] = $item['number'];
  416. $item['speed'] = 100;
  417. $item['finish'] = 1;
  418. $item['task_type_title'] = '';
  419. } else {
  420. // if($is_clear){
  421. list($new_number, $speed, $task_type_title, $finish) = self::set_task_type($item, $uid, $startTime);
  422. $item['new_number'] = $new_number;
  423. $item['speed'] = $speed;
  424. $item['task_type_title'] = $task_type_title;
  425. $item['finish'] = $finish;
  426. // }else {
  427. // list($new_number, $speed, $task_type_title, $finish) = self::set_task_type($item,-1,time()+86400);
  428. // $item['new_number'] = $new_number;
  429. // $item['speed'] = $speed;
  430. // $item['task_type_title'] = $task_type_title;
  431. // $item['finish'] = $finish;
  432. // }
  433. }
  434. }
  435. return $task;
  436. }
  437. /**
  438. * 递归获取所有团队成员
  439. * @param int $uid 用户ID
  440. * @return array
  441. */
  442. public static function getAllTeamMembers($uid, &$teamUids = [])
  443. {
  444. // 获取直接下级
  445. $directSubordinates = User::where('spread_uid', $uid)->column('uid');
  446. if (!empty($directSubordinates)) {
  447. // 过滤掉最开始传入的UID
  448. $directSubordinates = array_diff($directSubordinates, [$uid]);
  449. // 合并当前直接下级UID到团队UID列表
  450. $teamUids = array_merge($teamUids, $directSubordinates);
  451. // 递归获取下级的下级
  452. foreach ($directSubordinates as $subordinateUid) {
  453. self::getAllTeamMembers($subordinateUid, $teamUids);
  454. }
  455. }
  456. return $teamUids;
  457. }
  458. }