SystemUserTask.php 20 KB

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