SystemUserTask.php 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520
  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' => 'CumulativeAttendance',
  66. 'name' => '累计签到{$num}',
  67. 'real_name' => '累计签到',
  68. 'max_number' => 365,
  69. 'min_number' => 1,
  70. 'unit' => '天'
  71. ],
  72. [
  73. 'type' => 'SharingTimes',
  74. 'name' => '分享给朋友{$num}',
  75. 'real_name' => '分享给朋友',
  76. 'max_number' => 1000,
  77. 'min_number' => 1,
  78. 'unit' => '次'
  79. ],
  80. [
  81. 'type' => 'InviteGoodFriends',
  82. 'name' => '邀请好友{$num}成为下线',
  83. 'real_name' => '邀请好友成为下线',
  84. 'max_number' => 1000,
  85. 'min_number' => 1,
  86. 'unit' => '人'
  87. ],
  88. [
  89. 'type' => 'InviteGoodFriendsLevel',
  90. 'name' => '邀请好友{$num}成为会员',
  91. 'real_name' => '邀请好友成为会员',
  92. 'max_number' => 1000,
  93. 'min_number' => 1,
  94. 'unit' => '人'
  95. ],
  96. ];
  97. public function profile()
  98. {
  99. return $this->hasOne('SystemUserLevel', 'level_id', 'id')->field('name');
  100. }
  101. public static function getTaskTypeAll()
  102. {
  103. return self::$TaskType;
  104. }
  105. /**
  106. * 获取某个任务
  107. * @param string $type 任务类型
  108. * @return array
  109. * */
  110. public static function getTaskType($type)
  111. {
  112. foreach (self::$TaskType as $item) {
  113. if ($item['type'] == $type) return $item;
  114. }
  115. }
  116. /**
  117. * 设置任务名
  118. * @param string $type 任务类型
  119. * @param int $num 预设值
  120. * @return string
  121. * */
  122. public static function setTaskName($type, $num)
  123. {
  124. $systemType = self::getTaskType($type);
  125. return str_replace('{$num}', $num . $systemType['unit'], $systemType['name']);
  126. }
  127. /**
  128. * 累计消费金额
  129. * @param int $task_id 任务id
  130. * @param int $uid 用户id
  131. * @param int $start_time 开始时间
  132. * @param int $number 限定时间
  133. * @return boolean
  134. * */
  135. public static function ConsumptionAmount($task_id, $uid = 0, $start_time = 0, $number = 0)
  136. {
  137. $isComplete = false;
  138. $SumPayPrice = StoreOrder::where('paid', 1)->where('refund_status', 0)->where('is_del', 0)->where('uid', $uid)->where('add_time', '>', $start_time)->sum('pay_price');
  139. if ($SumPayPrice >= $number) $isComplete = UserTaskFinish::setFinish($uid, $task_id) ? true : false;
  140. return ['还需消费{$num}元', $SumPayPrice, $isComplete];
  141. }
  142. /**
  143. * 累计消费次数
  144. * @param int $task_id 任务id
  145. * @param int $uid 用户id
  146. * @param int $start_time 开始时间
  147. * @param int $number 限定时间
  148. * @return boolean
  149. * */
  150. public static function ConsumptionFrequency($task_id, $uid = 0, $start_time = 0, $number = 0)
  151. {
  152. $isComplete = false;
  153. $countPay = StoreOrder::where('paid', 1)->where('refund_status', 0)->where('is_del', 0)->where('uid', $uid)->where('add_time', '>', $start_time)->count();
  154. if ($countPay >= $number) $isComplete = UserTaskFinish::setFinish($uid, $task_id) ? true : false;
  155. return ['还需消费{$num}次', $countPay, $isComplete];
  156. }
  157. /**
  158. * 邀请好友成为会员
  159. * @param int $task_id 任务id
  160. * @param int $uid 用户id
  161. * @param int $start_time 开始时间
  162. * @param int $number 限定时间
  163. * @return boolean
  164. * */
  165. public static function InviteGoodFriendsLevel($task_id, $uid = 0, $start_time = 0, $number = 0)
  166. {
  167. $isComplete = false;
  168. $uids = User::where('spread_uid', $uid)->where('spread_time', '>', $start_time)->column('uid', 'uid');
  169. $levelCount = count($uids) ? UserLevel::setUserLevelCount($uids) : 0;
  170. if ($levelCount >= $number) $isComplete = UserTaskFinish::setFinish($uid, $task_id) ? true : false;
  171. return ['还需邀请{$num}人成为会员', $levelCount, $isComplete];
  172. }
  173. /**
  174. * 邀请好友成为下线
  175. * @param int $task_id 任务id
  176. * @param int $uid 用户id
  177. * @param int $start_time 查询开始时间
  178. * @param int $number 限定数量
  179. * */
  180. public static function InviteGoodFriends($task_id, $uid = 0, $start_time = 0, $number = 0)
  181. {
  182. $isComplete = false;
  183. $spreadCount = User::where('spread_uid', $uid)->where('spread_time', '>', $start_time)->count();
  184. if ($spreadCount >= $number) $isComplete = UserTaskFinish::setFinish($uid, $task_id) ? true : false;
  185. return ['还需邀请{$num}人成为下线', $spreadCount, $isComplete];
  186. }
  187. /**
  188. * 满足积分
  189. * @param int $task_id 任务id
  190. * @param int $uid 用户id
  191. * @param int $start_time 查询开始时间
  192. * @param int $number 限定数量
  193. * @return Boolean
  194. * */
  195. public static function SatisfactionIntegral($task_id, $uid = 0, $start_time = 0, $number = 0)
  196. {
  197. $isComplete = false;
  198. $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');
  199. if ($sumNumber >= $number) $isComplete = UserTaskFinish::setFinish($uid, $task_id) ? true : false;
  200. return ['还需要{$num}经验', $sumNumber, $isComplete];
  201. }
  202. /**
  203. * 分享给朋友次数完成情况
  204. * @param int $task_id 任务id
  205. * @param int $uid 用户id
  206. * @param int $start_time 查询开始时间
  207. * @param int $number 限定数量
  208. * @return Boolean
  209. * */
  210. public static function SharingTimes($task_id, $uid = 0, $start_time = 0, $number = 0)
  211. {
  212. $isComplete = false;
  213. $sumCount = UserBill::where('uid', $uid)->where('category', 'share')->where('pm', 1)->where('add_time', '>', $start_time)->where('type', 'share')->count();
  214. if ($sumCount >= $number) $isComplete = UserTaskFinish::setFinish($uid, $task_id) ? true : false;
  215. return ['还需分享{$num}次', $sumCount, $isComplete];
  216. }
  217. /**
  218. * 累计签到
  219. * @param int $task_id 任务id
  220. * @param int $uid 用户id
  221. * @param int $start_time 查询开始时间
  222. * @param int $number 限定数量
  223. * @return Boolean
  224. * */
  225. public static function CumulativeAttendance($task_id, $uid = 0, $start_time = 0, $number = 0)
  226. {
  227. $isComplete = false;
  228. $sumCount = UserBill::where('uid', $uid)->where('category', 'integral')->where('pm', 1)->where('add_time', '>', $start_time)->where('type', 'sign')->count();
  229. if ($sumCount >= $number) $isComplete = UserTaskFinish::setFinish($uid, $task_id) ? true : false;
  230. return ['还需签到{$num}天', $sumCount, $isComplete];
  231. }
  232. /**
  233. * 直推用户
  234. * @param int $task_id 任务id
  235. * @param int $uid 用户id
  236. * @param int $start_time 查询开始时间
  237. * @param int $number 限定数量
  238. * @return Boolean
  239. * */
  240. public static function ValidMembers($task_id, $uid = 0, $start_time = 0, $number = 0)
  241. {
  242. $isComplete = false;
  243. $uids = User::where('spread_uid', $uid)->column('uid');
  244. $uidis = [];
  245. if (count($uids) > 0){
  246. foreach ($uids as $item){
  247. $price = StoreOrder::where('uid', $item)->where('paid', 1)->sum('pay_price');
  248. if ($price >= 198){
  249. $uidis[] = $item;
  250. }
  251. }
  252. }
  253. if (count($uidis) >= $number){
  254. $isComplete = UserTaskFinish::setFinish($uid, $task_id) ? true : false;
  255. $count = User::where('level', 1)->count();
  256. if ($count < 30){
  257. User::where('uid', $uid)->update(['shares' => 2]);
  258. }
  259. }
  260. return ['还需签到{$num}天', 1, $isComplete];
  261. }
  262. /**
  263. * 直推代理
  264. * @param int $task_id 任务id
  265. * @param int $uid 用户id
  266. * @param int $start_time 查询开始时间
  267. * @param int $number 限定数量
  268. * @return Boolean
  269. * */
  270. public static function InviteAgent($task_id, $uid = 0, $start_time = 0, $number = 0)
  271. {
  272. $isComplete = false;
  273. $uids = get_downline(User::select(), $uid);
  274. $user_count = User::where('spread_uid', $uid)->where('level', 1)->count();
  275. if ($user_count >= $number)$isComplete = UserTaskFinish::setFinish($uid, $task_id) ? true : false;
  276. return ['还需签到{$num}天', 1, $isComplete];
  277. }
  278. /**
  279. * 伞下会员
  280. * @param int $task_id 任务id
  281. * @param int $uid 用户id
  282. * @param int $start_time 查询开始时间
  283. * @param int $number 限定数量
  284. * @return Boolean
  285. * */
  286. public static function UserCount($task_id, $uid = 0, $start_time = 0, $number = 0)
  287. {
  288. $isComplete = false;
  289. $uids = get_downline(User::select(), $uid);
  290. if (count($uids) >= $number)$isComplete = UserTaskFinish::setFinish($uid, $task_id) ? true : false;
  291. return ['还需签到{$num}天', 1, $isComplete];
  292. }
  293. /**
  294. * 直推事业部
  295. * @param int $task_id 任务id
  296. * @param int $uid 用户id
  297. * @param int $start_time 查询开始时间
  298. * @param int $number 限定数量
  299. * @return Boolean
  300. * */
  301. public static function InviteCause($task_id, $uid = 0, $start_time = 0, $number = 0)
  302. {
  303. $isComplete = false;
  304. $user_count = User::where('spread_uid', $uid)->where('level', 2)->count();
  305. if ($user_count >= $number)$isComplete = UserTaskFinish::setFinish($uid, $task_id) ? true : false;
  306. return ['还需签到{$num}天', 1, $isComplete];
  307. }
  308. /**
  309. * 设置任务完成情况
  310. * @param int $task_id 任务id
  311. * @param int $uid 用户uid
  312. * @param int $start_time 查询开始时间
  313. * @return Boolean
  314. * */
  315. public static function setTaskFinish($task_id = 0, $uid = 0, $start_time = 0)
  316. {
  317. if (!$task_id) return self::setErrorInfo('缺少任务id参数');
  318. if (!$uid) return self::setErrorInfo('缺少用户uid');
  319. $task = self::where('id', $task_id)->where('is_show', 1)->find();
  320. if (!$task) return self::setErrorInfo('任务不存在');
  321. $task_type = $task->task_type;
  322. if ($task_type && method_exists(self::class, $task_type)) {
  323. try {
  324. $start_time = User::getCleanTime($uid);
  325. return self::$task_type($task_id, $uid, $start_time, $task->number);
  326. } catch (\Exception $e) {
  327. return self::setErrorInfo($e->getMessage());
  328. }
  329. }
  330. return self::setErrorInfo('没有此任务');
  331. }
  332. /**
  333. * 设置任务显示条件
  334. * @param string $alert 表别名
  335. * @param object $model 模型实例
  336. * @return object
  337. * */
  338. public static function visibleWhere($alert = '', $model = null)
  339. {
  340. $model = $model === null ? new self() : $model;
  341. if ($alert) $model = $model->alias($alert);
  342. $alert = $alert ? $alert . '.' : '';
  343. return $model->where("{$alert}is_show", 1);
  344. }
  345. /**
  346. * 获取等级会员任务列表
  347. * @param int $level_id 会员等级id
  348. * @param int $uid 用户id
  349. * @param null $level
  350. * @param int $expire
  351. * @return array
  352. * @throws DataNotFoundException
  353. * @throws DbException
  354. * @throws ModelNotFoundException
  355. */
  356. public static function getTashList($level_id, $uid = 0, $level = null, $expire = 1400)
  357. {
  358. $level_id = is_string($level_id) ? (int)$level_id : $level_id;
  359. $list = self::visibleWhere()->where('level_id', $level_id)->field('name,real_name,task_type,illustrate,number,id')->order('sort desc')->select();
  360. $list = count($list) ? $list->toArray() : [];
  361. if ($uid == 0) return $list;
  362. if ($level === null) $level = SystemUserLevel::getLevelInfo($uid);
  363. //获取下一个vip的id
  364. $LeveId = SystemUserLevel::getNextLevelId($level['id'], SystemUserLevel::setWhere()->where('grade', '>', $level ? $level['grade'] : 0)->min('grade'));
  365. $is_clear = SystemUserLevel::getClear($level['id']);
  366. if ($is_clear == false && $LeveId == $level_id) $is_clear = true;
  367. $reach_count = self::getTaskComplete($level_id, $uid, true);
  368. return [
  369. 'list' => $list,
  370. 'reach_count' => $reach_count,
  371. 'task' => self::tidyTask($list, $uid, $is_clear, User::getCleanTime($uid)),
  372. ];
  373. }
  374. /**
  375. * 获取未完成任务的详细值
  376. * @param array $item 任务
  377. * @param int $uid 用户id
  378. * @param int $startTime 开始时间
  379. * @return array
  380. * */
  381. protected static function set_task_type($item, $uid, $startTime = 0)
  382. {
  383. $task = ['task_type_title' => '', 'new_number' => 0, 'speed' => 0, 'finish' => 0];
  384. $task_type = $item['task_type'];
  385. switch ($task_type) {
  386. case 'SatisfactionIntegral':
  387. case 'ConsumptionAmount':
  388. case 'ConsumptionFrequency':
  389. case 'CumulativeAttendance':
  390. case 'SharingTimes':
  391. case 'InviteGoodFriends':
  392. case 'InviteGoodFriendsLevel':
  393. try {
  394. list($task_type_title, $num, $isComplete) = self::$task_type($item['id'], $uid, $startTime, $item['number']);
  395. if ($isComplete) {
  396. $task['finish'] = 1;
  397. $task['speed'] = 100;
  398. $task['speed'] = $item['number'];
  399. $task['new_number'] = $item['number'];
  400. } else {
  401. $numdata = bcsub($item['number'], $num, 0);
  402. $task['task_type_title'] = str_replace('{$num}', $numdata, $task_type_title);
  403. $task['speed'] = bcdiv($num, $item['number'], 2);
  404. $task['speed'] = bcmul($task['speed'], 100, 0);
  405. $task['new_number'] = $num;
  406. }
  407. } catch (\Exception $e) {
  408. }
  409. break;
  410. }
  411. return [$task['new_number'], $task['speed'], $task['task_type_title'], $task['finish']];
  412. }
  413. /**
  414. * 设置任务完成状态,已被使用
  415. * @param int $level_id 会员id
  416. * @param int $uid 用户id
  417. * @return Boolean
  418. * */
  419. public static function setTarkStatus($level_id, $uid)
  420. {
  421. $taskIds = self::visibleWhere()->where('level_id', $level_id)->column('id', 'id');
  422. if (!count($taskIds)) return true;
  423. return UserTaskFinish::where('uid', $uid)->where('task_id', 'in', $taskIds)->update(['status' => 1]);
  424. }
  425. /**
  426. * 检查当前等级是否完成全部任务
  427. * @param int $level_id 会员id
  428. * @param int $uid 用户uid
  429. * @return boolean
  430. * */
  431. public static function getTaskComplete($level_id, $uid, $isCount = false)
  432. {
  433. $taskIds = self::visibleWhere()->where('level_id', $level_id)->column('id', 'id');
  434. $taskIdsCount = count($taskIds);
  435. //如果当前会员没有任务默认为直接升级为下一等级
  436. if ($taskIdsCount) {
  437. if ($isCount) {
  438. return UserTaskFinish::group('task_id')->where('uid', $uid)->where('task_id', 'in', $taskIds)->count();
  439. } else {
  440. $finishCount = UserTaskFinish::group('task_id')->where('status', $isCount ? 1 : 0)->where('uid', $uid)->where('task_id', 'in', implode(',', $taskIds))->count();
  441. }
  442. //如果当前任务有完成其一的,查询当前完成的任务数量,如果有任务完成则达成当前vip
  443. if (self::visibleWhere()->where('id', 'in', implode(',', $taskIds))->where('is_must', 0)->count() && $finishCount) {
  444. return true;
  445. }
  446. return $finishCount >= $taskIdsCount;
  447. }
  448. if ($isCount) return 0;
  449. //如果没有设置任务当前等级无需购买则返回false
  450. if (SystemUserLevel::be(['id' => $level_id, 'is_pay' => 0])) return false;
  451. return true;
  452. }
  453. /**
  454. * 设置任务内容完成情况
  455. * @param array $task 任务列表
  456. * @param int $uid 用户id
  457. * @热图图呢 array
  458. * */
  459. public static function tidyTask($task, $uid, $is_clear, $startTime)
  460. {
  461. if (!is_array($task)) return $task;
  462. foreach ($task as &$item) {
  463. //如果已完成该任务进度直接为100
  464. if (UserTaskFinish::where('uid', $uid)->where('task_id', $item['id'])->count()) {
  465. $item['new_number'] = $item['number'];
  466. $item['speed'] = 100;
  467. $item['finish'] = 1;
  468. $item['task_type_title'] = '';
  469. } else {
  470. // if($is_clear){
  471. list($new_number, $speed, $task_type_title, $finish) = self::set_task_type($item, $uid, $startTime);
  472. $item['new_number'] = $new_number;
  473. $item['speed'] = $speed;
  474. $item['task_type_title'] = $task_type_title;
  475. $item['finish'] = $finish;
  476. // }else {
  477. // list($new_number, $speed, $task_type_title, $finish) = self::set_task_type($item,-1,time()+86400);
  478. // $item['new_number'] = $new_number;
  479. // $item['speed'] = $speed;
  480. // $item['task_type_title'] = $task_type_title;
  481. // $item['finish'] = $finish;
  482. // }
  483. }
  484. }
  485. return $task;
  486. }
  487. }