SystemUserTask.php 22 KB

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