SystemUserTask.php 18 KB

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