SystemUserTask.php 19 KB

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