TaskService.php 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181
  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. $num = 0;
  63. $complete = 0;
  64. if($datas){
  65. foreach ($datas as &$item){
  66. $item['status'] = 1;
  67. $item['log_num'] = 0;
  68. if($power<=0){
  69. $power = $item['power'];
  70. }
  71. $logNum = $this->getLogNum($userId, $item['id'], $item['type']);
  72. // 次数
  73. if($item['check_type'] == 1 && $item['num']<=$logNum){
  74. $item['status'] = 2;
  75. $item['log_num'] = $logNum;
  76. $complete++;
  77. }
  78. // 时长
  79. else if($item['check_type'] == 2 && $logNum>0){
  80. $item['status'] = 2;
  81. $item['log_num'] = $logNum;
  82. $complete++;
  83. }
  84. }
  85. unset($item);
  86. }
  87. return ['power'=>$power,'complete'=>$complete,'num'=>count($datas), 'list'=> $datas];
  88. }
  89. /**
  90. * 任务完成情况
  91. * @param $userId
  92. * @param $taskId
  93. * @param int $type
  94. * @return array|mixed
  95. */
  96. public function getLogNum($userId, $taskId, $type=1)
  97. {
  98. $cacheKey = "caches:task:check_{$userId}_{$taskId}_{$type}";
  99. $data = RedisService::get($cacheKey);
  100. if($data){
  101. return $data;
  102. }
  103. $data = TaskLogModel::where(['task_id'=> $taskId,'user_id'=> $userId,'status'=>1,'mark'=>1])
  104. ->where(function($query) use($type){
  105. // 每日任务
  106. if($type == 1){
  107. $query->where('date','=', date('Y-m-d'));
  108. }
  109. })->value('num');
  110. if($data){
  111. RedisService::set($cacheKey, $data, rand(3,5));
  112. }
  113. return $data;
  114. }
  115. /**
  116. * 状态设置
  117. * @return bool
  118. */
  119. public function status()
  120. {
  121. $id = request()->post('id', 0);
  122. $status = request()->post('status', 1);
  123. if ($id && !$this->model->where(['id' => $id, 'mark' => 1])->value('id')) {
  124. $this->error = 2981;
  125. return false;
  126. }
  127. if($this->model->where(['id'=> $id,'mark'=>1])->update(['status'=>$status, 'update_time'=> time()])){
  128. $this->error = 1002;
  129. return true;
  130. }
  131. $this->error = 1003;
  132. return true;
  133. }
  134. /**
  135. * 删除
  136. * @return bool
  137. */
  138. public function delete()
  139. {
  140. // 参数
  141. $param = request()->all();
  142. $id = getter($param, "id");
  143. if (empty($id)) {
  144. $this->error = 2014;
  145. return false;
  146. }
  147. if(!$this->model->where(['id'=> $id])->value('id')){
  148. $this->error = 1039;
  149. return false;
  150. }
  151. if($this->model->where(['id'=> $id])->update(['mark'=>0,'update_time'=>time()])){
  152. $this->model->where(['mark'=> 0])->where('update_time','<=', time() - 3 * 86400)->delete();
  153. $this->error = 1025;
  154. return true;
  155. }else{
  156. $this->error = 1026;
  157. return false;
  158. }
  159. }
  160. }