SystemUserTask.php 18 KB

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