SystemUserTask.php 20 KB

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