TaskService.php 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177
  1. <?php
  2. // +----------------------------------------------------------------------
  3. // | LARAVEL8.0 框架 [ LARAVEL ][ RXThinkCMF ]
  4. // +----------------------------------------------------------------------
  5. // | 版权所有 2017~2021 LARAVEL研发中心
  6. // +----------------------------------------------------------------------
  7. // | 官方网站: http://www.laravel.cn
  8. // +----------------------------------------------------------------------
  9. // | Author: laravel开发员 <laravel.qq.com>
  10. // +----------------------------------------------------------------------
  11. namespace App\Services\Api;
  12. use App\Models\TaskLogModel;
  13. use App\Models\TaskModel;
  14. use App\Services\BaseService;
  15. use App\Services\RedisService;
  16. /**
  17. * 任务管理-服务类
  18. * @author laravel开发员
  19. * @since 2020/11/11
  20. * @package App\Services\Api
  21. */
  22. class TaskService extends BaseService
  23. {
  24. // 静态对象
  25. protected static $instance = null;
  26. /**
  27. * 构造函数
  28. * @author laravel开发员
  29. * @since 2020/11/11
  30. * GoodsService constructor.
  31. */
  32. public function __construct()
  33. {
  34. $this->model = new TaskModel();
  35. }
  36. /**
  37. * 静态入口
  38. * @return static|null
  39. */
  40. public static function make()
  41. {
  42. if (!self::$instance) {
  43. self::$instance = (new static());
  44. }
  45. return self::$instance;
  46. }
  47. /**
  48. * 每日任务
  49. * @param $params
  50. * @param int $pageSize
  51. * @return array
  52. */
  53. public function getTaskList($userId, $type=1)
  54. {
  55. $datas = $this->model->where(['type'=>$type,'status'=>1,'mark'=>1])
  56. ->select(['id','name','type','num','power','sort','check_type','scene'])
  57. ->orderBy('sort','desc')
  58. ->orderBy('id','desc')
  59. ->get();
  60. $datas = $datas? $datas->toArray() : [];
  61. $power = 0;
  62. if($datas){
  63. foreach ($datas as &$item){
  64. $item['status'] = 1;
  65. $item['log_num'] = 0;
  66. if($power<=0){
  67. $power = $item['power'];
  68. }
  69. $logNum = $this->getLogNum($item['id'], $userId, $item['type']);
  70. // 次数
  71. if($item['check_type'] == 1 && $item['num']<=$logNum){
  72. $item['status'] = 2;
  73. $item['log_num'] = $logNum;
  74. }
  75. // 时长
  76. else if($item['check_type'] == 2 && $logNum>0){
  77. $item['status'] = 2;
  78. $item['log_num'] = $logNum;
  79. }
  80. }
  81. unset($item);
  82. }
  83. return ['power'=>$power, 'list'=> $datas];
  84. }
  85. /**
  86. * 任务完成情况
  87. * @param $userId
  88. * @param $taskId
  89. * @param int $type
  90. * @return array|mixed
  91. */
  92. public function getLogNum($userId, $taskId, $type=1)
  93. {
  94. $cacheKey = "caches:task:check_{$userId}_{$taskId}_{$type}";
  95. $data = RedisService::get($cacheKey);
  96. if($data){
  97. return $data;
  98. }
  99. $data = TaskLogModel::where(['task_id'=> $taskId,'user_id'=> $userId,'status'=>1,'mark'=>1])
  100. ->where(function($query) use($type){
  101. // 每日任务
  102. if($type == 1){
  103. $query->where('date','=', date('Y-m-d'));
  104. }
  105. })->value('num');
  106. if($data){
  107. RedisService::set($cacheKey, $data, rand(3,5));
  108. }
  109. return $data;
  110. }
  111. /**
  112. * 状态设置
  113. * @return bool
  114. */
  115. public function status()
  116. {
  117. $id = request()->post('id', 0);
  118. $status = request()->post('status', 1);
  119. if ($id && !$this->model->where(['id' => $id, 'mark' => 1])->value('id')) {
  120. $this->error = 2981;
  121. return false;
  122. }
  123. if($this->model->where(['id'=> $id,'mark'=>1])->update(['status'=>$status, 'update_time'=> time()])){
  124. $this->error = 1002;
  125. return true;
  126. }
  127. $this->error = 1003;
  128. return true;
  129. }
  130. /**
  131. * 删除
  132. * @return bool
  133. */
  134. public function delete()
  135. {
  136. // 参数
  137. $param = request()->all();
  138. $id = getter($param, "id");
  139. if (empty($id)) {
  140. $this->error = 2014;
  141. return false;
  142. }
  143. if(!$this->model->where(['id'=> $id])->value('id')){
  144. $this->error = 1039;
  145. return false;
  146. }
  147. if($this->model->where(['id'=> $id])->update(['mark'=>0,'update_time'=>time()])){
  148. $this->model->where(['mark'=> 0])->where('update_time','<=', time() - 3 * 86400)->delete();
  149. $this->error = 1025;
  150. return true;
  151. }else{
  152. $this->error = 1026;
  153. return false;
  154. }
  155. }
  156. }