123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181 |
- <?php
- // +----------------------------------------------------------------------
- // | LARAVEL8.0 框架 [ LARAVEL ][ RXThinkCMF ]
- // +----------------------------------------------------------------------
- // | 版权所有 2017~2021 LARAVEL研发中心
- // +----------------------------------------------------------------------
- // | 官方网站: http://www.laravel.cn
- // +----------------------------------------------------------------------
- // | Author: laravel开发员 <laravel.qq.com>
- // +----------------------------------------------------------------------
- namespace App\Services\Api;
- use App\Models\TaskLogModel;
- use App\Models\TaskModel;
- use App\Services\BaseService;
- use App\Services\RedisService;
- /**
- * 任务管理-服务类
- * @author laravel开发员
- * @since 2020/11/11
- * @package App\Services\Api
- */
- class TaskService extends BaseService
- {
- // 静态对象
- protected static $instance = null;
- /**
- * 构造函数
- * @author laravel开发员
- * @since 2020/11/11
- * GoodsService constructor.
- */
- public function __construct()
- {
- $this->model = new TaskModel();
- }
- /**
- * 静态入口
- * @return static|null
- */
- public static function make()
- {
- if (!self::$instance) {
- self::$instance = (new static());
- }
- return self::$instance;
- }
- /**
- * 每日任务
- * @param $params
- * @param int $pageSize
- * @return array
- */
- public function getTaskList($userId, $type=1)
- {
- $datas = $this->model->where(['type'=>$type,'status'=>1,'mark'=>1])
- ->select(['id','name','type','num','power','sort','check_type','scene'])
- ->orderBy('sort','desc')
- ->orderBy('id','desc')
- ->get();
- $datas = $datas? $datas->toArray() : [];
- $power = 0;
- $num = 0;
- $complete = 0;
- if($datas){
- foreach ($datas as &$item){
- $item['status'] = 1;
- $item['log_num'] = 0;
- if($power<=0){
- $power = $item['power'];
- }
- $logNum = $this->getLogNum($userId, $item['id'], $item['type']);
- // 次数
- if($item['check_type'] == 1 && $item['num']<=$logNum){
- $item['status'] = 2;
- $item['log_num'] = $logNum;
- $complete++;
- }
- // 时长
- else if($item['check_type'] == 2 && $logNum>0){
- $item['status'] = 2;
- $item['log_num'] = $logNum;
- $complete++;
- }
- }
- unset($item);
- }
- return ['power'=>$power,'complete'=>$complete,'num'=>count($datas), 'list'=> $datas];
- }
- /**
- * 任务完成情况
- * @param $userId
- * @param $taskId
- * @param int $type
- * @return array|mixed
- */
- public function getLogNum($userId, $taskId, $type=1)
- {
- $cacheKey = "caches:task:check_{$userId}_{$taskId}_{$type}";
- $data = RedisService::get($cacheKey);
- if($data){
- return $data;
- }
- $data = TaskLogModel::where(['task_id'=> $taskId,'user_id'=> $userId,'status'=>1,'mark'=>1])
- ->where(function($query) use($type){
- // 每日任务
- if($type == 1){
- $query->where('date','=', date('Y-m-d'));
- }
- })->value('num');
- if($data){
- RedisService::set($cacheKey, $data, rand(3,5));
- }
- return $data;
- }
- /**
- * 状态设置
- * @return bool
- */
- public function status()
- {
- $id = request()->post('id', 0);
- $status = request()->post('status', 1);
- if ($id && !$this->model->where(['id' => $id, 'mark' => 1])->value('id')) {
- $this->error = 2981;
- return false;
- }
- if($this->model->where(['id'=> $id,'mark'=>1])->update(['status'=>$status, 'update_time'=> time()])){
- $this->error = 1002;
- return true;
- }
- $this->error = 1003;
- return true;
- }
- /**
- * 删除
- * @return bool
- */
- public function delete()
- {
- // 参数
- $param = request()->all();
- $id = getter($param, "id");
- if (empty($id)) {
- $this->error = 2014;
- return false;
- }
- if(!$this->model->where(['id'=> $id])->value('id')){
- $this->error = 1039;
- return false;
- }
- if($this->model->where(['id'=> $id])->update(['mark'=>0,'update_time'=>time()])){
- $this->model->where(['mark'=> 0])->where('update_time','<=', time() - 3 * 86400)->delete();
- $this->error = 1025;
- return true;
- }else{
- $this->error = 1026;
- return false;
- }
- }
- }
|