SystemUserTask.php 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461
  1. <?php
  2. namespace app\models\system;
  3. use app\models\store\StoreCart;
  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. 'type' => 'BuySomeProduct',
  99. 'name' => '购买ID为{$num}的商品',
  100. 'real_name' => '购买商品',
  101. 'max_number' => 0,
  102. 'min_number' => 0,
  103. 'unit' => '号'
  104. ],
  105. ];
  106. public function profile()
  107. {
  108. return $this->hasOne('SystemUserLevel', 'level_id', 'id')->field('name');
  109. }
  110. public static function getTaskTypeAll()
  111. {
  112. return self::$TaskType;
  113. }
  114. /**
  115. * 获取某个任务
  116. * @param string $type 任务类型
  117. * @return array
  118. * */
  119. public static function getTaskType($type)
  120. {
  121. foreach (self::$TaskType as $item) {
  122. if ($item['type'] == $type) return $item;
  123. }
  124. }
  125. /**
  126. * 设置任务名
  127. * @param string $type 任务类型
  128. * @param int $num 预设值
  129. * @return string
  130. * */
  131. public static function setTaskName($type, $num)
  132. {
  133. $systemType = self::getTaskType($type);
  134. return str_replace('{$num}', $num . $systemType['unit'], $systemType['name']);
  135. }
  136. /**
  137. * 累计消费金额
  138. * @param int $task_id 任务id
  139. * @param int $uid 用户id
  140. * @param int $start_time 开始时间
  141. * @param int $number 限定时间
  142. * @return boolean
  143. * */
  144. public static function ConsumptionAmount($task_id, $uid = 0, $start_time = 0, $number = 0)
  145. {
  146. $isComplete = false;
  147. $SumPayPrice = StoreOrder::where('paid', 1)->where('refund_status', 0)->where('is_del', 0)->where('uid', $uid)->where('add_time', '>', $start_time)->sum('pay_price');
  148. if ($SumPayPrice >= $number) $isComplete = UserTaskFinish::setFinish($uid, $task_id) ? true : false;
  149. return ['还需消费{$num}元', $SumPayPrice, $isComplete];
  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 ConsumptionFrequency($task_id, $uid = 0, $start_time = 0, $number = 0)
  160. {
  161. $isComplete = false;
  162. $countPay = StoreOrder::where('paid', 1)->where('refund_status', 0)->where('is_del', 0)->where('uid', $uid)->where('add_time', '>', $start_time)->count();
  163. if ($countPay >= $number) $isComplete = UserTaskFinish::setFinish($uid, $task_id) ? true : false;
  164. return ['还需消费{$num}次', $countPay, $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 InviteGoodFriendsLevel($task_id, $uid = 0, $start_time = 0, $number = 0)
  175. {
  176. $isComplete = false;
  177. $uids = User::where('spread_uid', $uid)->where('spread_time', '>', $start_time)->column('uid', 'uid');
  178. $levelCount = count($uids) ? UserLevel::setUserLevelCount($uids) : 0;
  179. if ($levelCount >= $number) $isComplete = UserTaskFinish::setFinish($uid, $task_id) ? true : false;
  180. return ['还需邀请{$num}人成为会员', $levelCount, $isComplete];
  181. }
  182. /**
  183. * 邀请好友成为下线
  184. * @param int $task_id 任务id
  185. * @param int $uid 用户id
  186. * @param int $start_time 查询开始时间
  187. * @param int $number 限定数量
  188. * */
  189. public static function InviteGoodFriends($task_id, $uid = 0, $start_time = 0, $number = 0)
  190. {
  191. $isComplete = false;
  192. $spreadCount = User::where('spread_uid', $uid)->where('spread_time', '>', $start_time)->count();
  193. if ($spreadCount >= $number) $isComplete = UserTaskFinish::setFinish($uid, $task_id) ? true : false;
  194. return ['还需邀请{$num}人成为下线', $spreadCount, $isComplete];
  195. }
  196. public static function BuySomeProduct($task_id, $uid = 0, $start_time = 0, $number = 0)
  197. {
  198. $isComplete = false;
  199. $SumPayPrice = StoreOrder::where('paid', 1)
  200. ->where('refund_status', 0)
  201. ->where('is_del', 0)
  202. ->where('uid', $uid)
  203. ->where('add_time', '>', $start_time)
  204. ->order('add_time', 'desc')
  205. ->find();
  206. $product_id = StoreCart::where('id', 'in', $SumPayPrice['cart_id'])->column('product_id');
  207. if (in_array($number, $product_id)) $isComplete = UserTaskFinish::setFinish($uid, $task_id) ? true : false;
  208. return ['还需购买ID为{$num}的商品一件', 0, $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 用户uid
  259. * @param int $start_time 查询开始时间
  260. * @return Boolean
  261. * */
  262. public static function setTaskFinish($task_id = 0, $uid = 0, $start_time = 0)
  263. {
  264. if (!$task_id) return self::setErrorInfo('缺少任务id参数');
  265. if (!$uid) return self::setErrorInfo('缺少用户uid');
  266. $task = self::where('id', $task_id)->where('is_show', 1)->find();
  267. if (!$task) return self::setErrorInfo('任务不存在');
  268. $task_type = $task->task_type;
  269. if ($task_type && method_exists(self::class, $task_type)) {
  270. try {
  271. $start_time = User::getCleanTime($uid);
  272. return self::$task_type($task_id, $uid, $start_time, $task->number);
  273. } catch (\Exception $e) {
  274. return self::setErrorInfo($e->getMessage());
  275. }
  276. }
  277. return self::setErrorInfo('没有此任务');
  278. }
  279. /**
  280. * 设置任务显示条件
  281. * @param string $alert 表别名
  282. * @param object $model 模型实例
  283. * @return object
  284. * */
  285. public static function visibleWhere($alert = '', $model = null)
  286. {
  287. $model = $model === null ? new self() : $model;
  288. if ($alert) $model = $model->alias($alert);
  289. $alert = $alert ? $alert . '.' : '';
  290. return $model->where("{$alert}is_show", 1);
  291. }
  292. /**
  293. * 获取等级会员任务列表
  294. * @param int $level_id 会员等级id
  295. * @param int $uid 用户id
  296. * @param null $level
  297. * @param int $expire
  298. * @return array
  299. * @throws DataNotFoundException
  300. * @throws DbException
  301. * @throws ModelNotFoundException
  302. */
  303. public static function getTashList($level_id, $uid = 0, $level = null, $expire = 1400)
  304. {
  305. $level_id = is_string($level_id) ? (int)$level_id : $level_id;
  306. $list = self::visibleWhere()->where('level_id', $level_id)->field('name,real_name,task_type,illustrate,number,id')->order('sort desc')->select();
  307. $list = count($list) ? $list->toArray() : [];
  308. if ($uid == 0) return $list;
  309. if ($level === null) $level = SystemUserLevel::getLevelInfo($uid);
  310. //获取下一个vip的id
  311. $LeveId = SystemUserLevel::getNextLevelId($level['id'], SystemUserLevel::setWhere()->where('grade', '>', $level ? $level['grade'] : 0)->min('grade'));
  312. $is_clear = SystemUserLevel::getClear($level['id']);
  313. if ($is_clear == false && $LeveId == $level_id) $is_clear = true;
  314. $reach_count = self::getTaskComplete($level_id, $uid, true);
  315. return [
  316. 'list' => $list,
  317. 'reach_count' => $reach_count,
  318. 'task' => self::tidyTask($list, $uid, $is_clear, User::getCleanTime($uid)),
  319. ];
  320. }
  321. /**
  322. * 获取未完成任务的详细值
  323. * @param array $item 任务
  324. * @param int $uid 用户id
  325. * @param int $startTime 开始时间
  326. * @return array
  327. * */
  328. protected static function set_task_type($item, $uid, $startTime = 0)
  329. {
  330. $task = ['task_type_title' => '', 'new_number' => 0, 'speed' => 0, 'finish' => 0];
  331. $task_type = $item['task_type'];
  332. switch ($task_type) {
  333. case 'SatisfactionIntegral':
  334. case 'ConsumptionAmount':
  335. case 'ConsumptionFrequency':
  336. case 'CumulativeAttendance':
  337. case 'SharingTimes':
  338. case 'InviteGoodFriends':
  339. case 'InviteGoodFriendsLevel':
  340. try {
  341. list($task_type_title, $num, $isComplete) = self::$task_type($item['id'], $uid, $startTime, $item['number']);
  342. if ($isComplete) {
  343. $task['finish'] = 1;
  344. $task['speed'] = 100;
  345. $task['speed'] = $item['number'];
  346. $task['new_number'] = $item['number'];
  347. } else {
  348. $numdata = bcsub($item['number'], $num, 0);
  349. $task['task_type_title'] = str_replace('{$num}', $numdata, $task_type_title);
  350. $task['speed'] = bcdiv($num, $item['number'], 2);
  351. $task['speed'] = bcmul($task['speed'], 100, 0);
  352. $task['new_number'] = $num;
  353. }
  354. } catch (\Exception $e) {
  355. }
  356. break;
  357. }
  358. return [$task['new_number'], $task['speed'], $task['task_type_title'], $task['finish']];
  359. }
  360. /**
  361. * 设置任务完成状态,已被使用
  362. * @param int $level_id 会员id
  363. * @param int $uid 用户id
  364. * @return Boolean
  365. * */
  366. public static function setTarkStatus($level_id, $uid)
  367. {
  368. $taskIds = self::visibleWhere()->where('level_id', $level_id)->column('id', 'id');
  369. if (!count($taskIds)) return true;
  370. return UserTaskFinish::where('uid', $uid)->where('task_id', 'in', $taskIds)->update(['status' => 1]);
  371. }
  372. /**
  373. * 检查当前等级是否完成全部任务
  374. * @param int $level_id 会员id
  375. * @param int $uid 用户uid
  376. * @return boolean
  377. * */
  378. public static function getTaskComplete($level_id, $uid, $isCount = false)
  379. {
  380. $taskIds = self::visibleWhere()->where('level_id', $level_id)->column('id', 'id');
  381. $taskIdsCount = count($taskIds);
  382. //如果当前会员没有任务默认为直接升级为下一等级
  383. if ($taskIdsCount) {
  384. if ($isCount) {
  385. return UserTaskFinish::group('task_id')->where('uid', $uid)->where('task_id', 'in', $taskIds)->count();
  386. } else {
  387. $finishCount = UserTaskFinish::group('task_id')->where('status', $isCount ? 1 : 0)->where('uid', $uid)->where('task_id', 'in', implode(',', $taskIds))->count();
  388. }
  389. //如果当前任务有完成其一的,查询当前完成的任务数量,如果有任务完成则达成当前vip
  390. if (self::visibleWhere()->where('id', 'in', implode(',', $taskIds))->where('is_must', 0)->count() && $finishCount) {
  391. return true;
  392. }
  393. return $finishCount >= $taskIdsCount;
  394. }
  395. if ($isCount) return 0;
  396. //如果没有设置任务当前等级无需购买则返回false
  397. if (SystemUserLevel::be(['id' => $level_id, 'is_pay' => 0])) return false;
  398. return true;
  399. }
  400. /**
  401. * 设置任务内容完成情况
  402. * @param array $task 任务列表
  403. * @param int $uid 用户id
  404. * @热图图呢 array
  405. * */
  406. public static function tidyTask($task, $uid, $is_clear, $startTime)
  407. {
  408. if (!is_array($task)) return $task;
  409. foreach ($task as &$item) {
  410. //如果已完成该任务进度直接为100
  411. if (UserTaskFinish::where('uid', $uid)->where('task_id', $item['id'])->count()) {
  412. $item['new_number'] = $item['number'];
  413. $item['speed'] = 100;
  414. $item['finish'] = 1;
  415. $item['task_type_title'] = '';
  416. } else {
  417. // if($is_clear){
  418. list($new_number, $speed, $task_type_title, $finish) = self::set_task_type($item, $uid, $startTime);
  419. $item['new_number'] = $new_number;
  420. $item['speed'] = $speed;
  421. $item['task_type_title'] = $task_type_title;
  422. $item['finish'] = $finish;
  423. // }else {
  424. // list($new_number, $speed, $task_type_title, $finish) = self::set_task_type($item,-1,time()+86400);
  425. // $item['new_number'] = $new_number;
  426. // $item['speed'] = $speed;
  427. // $item['task_type_title'] = $task_type_title;
  428. // $item['finish'] = $finish;
  429. // }
  430. }
  431. }
  432. return $task;
  433. }
  434. }