SystemUserTask.php 18 KB

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