|
@@ -14,6 +14,8 @@ use App\Models\TaskLogModel;
|
|
|
use App\Models\TaskModel;
|
|
|
use App\Services\BaseService;
|
|
|
use App\Services\RedisService;
|
|
|
+use BN\Red;
|
|
|
+use Illuminate\Support\Facades\DB;
|
|
|
|
|
|
/**
|
|
|
* 任务管理-服务类
|
|
@@ -127,55 +129,198 @@ class TaskService extends BaseService
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
- * 状态设置
|
|
|
- * @return bool
|
|
|
+ * 获取对应场景下的任务列表
|
|
|
+ * @param $scene 场景
|
|
|
+ * @return array|mixed
|
|
|
*/
|
|
|
- public function status()
|
|
|
+ public function getTaskByScene($scene)
|
|
|
{
|
|
|
- $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;
|
|
|
+ $cacheKey = "caches:task:scene_{$scene}";
|
|
|
+ $datas = RedisService::get($cacheKey);
|
|
|
+ if($datas){
|
|
|
+ return $datas;
|
|
|
+ }
|
|
|
+ $datas = $this->model->where(['scene'=> $scene,'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() : [];
|
|
|
+ if($datas)
|
|
|
+ {
|
|
|
+ RedisService::set($cacheKey, $datas, rand(300,600));
|
|
|
}
|
|
|
|
|
|
- if($this->model->where(['id'=> $id,'mark'=>1])->update(['status'=>$status, 'update_time'=> time()])){
|
|
|
- $this->error = 1002;
|
|
|
- return true;
|
|
|
+ return $datas;
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 获取对应类型的任务列表
|
|
|
+ * @param $type 类型:1-每日任务,2-新手任务
|
|
|
+ * @return array|mixed
|
|
|
+ */
|
|
|
+ public function getTaskByType($type)
|
|
|
+ {
|
|
|
+ $cacheKey = "caches:task:type_{$type}";
|
|
|
+ $datas = RedisService::get($cacheKey);
|
|
|
+ if($datas){
|
|
|
+ return $datas;
|
|
|
+ }
|
|
|
+ $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() : [];
|
|
|
+ if($datas)
|
|
|
+ {
|
|
|
+ RedisService::set($cacheKey, $datas, rand(300,600));
|
|
|
}
|
|
|
|
|
|
- $this->error = 1003;
|
|
|
- return true;
|
|
|
+ return $datas;
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
- * 删除
|
|
|
- * @return bool
|
|
|
+ * 验证更新任务完成记录数据
|
|
|
+ * @param $userId 用户ID
|
|
|
+ * @param $taskId 任务ID
|
|
|
+ * @param int $type 任务类型:1-每日,2-新手
|
|
|
+ * @param int $checkType 任务验证类型:1-次数,2-时长
|
|
|
+ * @param int $sourceId 任务完成对象来源ID
|
|
|
+ * @return false
|
|
|
*/
|
|
|
- public function delete()
|
|
|
+ public function updateLogData($userId, $taskId, $type=1, $checkType=1, $sourceId=0)
|
|
|
{
|
|
|
- // 参数
|
|
|
- $param = request()->all();
|
|
|
- $id = getter($param, "id");
|
|
|
- if (empty($id)) {
|
|
|
- $this->error = 2014;
|
|
|
+ $cacheKey = "caches:task:scene_update_{$userId}_{$taskId}_{$type}";
|
|
|
+ if(RedisService::get($cacheKey)){
|
|
|
return false;
|
|
|
}
|
|
|
|
|
|
- if(!$this->model->where(['id'=> $id])->value('id')){
|
|
|
- $this->error = 1039;
|
|
|
+ // 是否已经完成
|
|
|
+ $checkData = TaskLogModel::where(['task_id'=> $taskId,'user_id'=> $userId,'is_complete'=>1,'status'=>1,'mark'=>1])
|
|
|
+ ->where(function($query) use($type){
|
|
|
+ // 每日任务
|
|
|
+ if($type == 1){
|
|
|
+ $query->where('date','=', date('Y-m-d'));
|
|
|
+ }
|
|
|
+ })->select(['id','task_id','user_id'])->orderBy('id','desc')->first();
|
|
|
+ if($checkData){
|
|
|
+ RedisService::set($cacheKey, $checkData, rand(300,600));
|
|
|
return false;
|
|
|
}
|
|
|
|
|
|
+ $logId = 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('id');
|
|
|
+ if($logId){
|
|
|
+ if($checkType == 1){
|
|
|
+ $updateData = ['num'=> DB::raw('num + 1'),'update_time'=>time()];
|
|
|
+ }else{
|
|
|
+ $updateData = ['num'=> 1,'update_time'=>time()];
|
|
|
+ }
|
|
|
|
|
|
- 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;
|
|
|
+ return TaskLogModel::where(['id'=> $logId])->update($updateData);
|
|
|
}else{
|
|
|
- $this->error = 1026;
|
|
|
- return false;
|
|
|
+ return TaskLogModel::insert([
|
|
|
+ 'user_id'=> $userId,
|
|
|
+ 'task_id'=> $taskId,
|
|
|
+ 'source_id'=> $sourceId,
|
|
|
+ 'num'=> 1,
|
|
|
+ 'date'=> date('Y-m-d'),
|
|
|
+ 'create_time'=> time(),
|
|
|
+ 'update_time'=> time(),
|
|
|
+ 'status'=>1,
|
|
|
+ 'mark'=>1,
|
|
|
+ ]);
|
|
|
}
|
|
|
}
|
|
|
|
|
|
+ /**
|
|
|
+ * 验证更新任务完成状态,并结算
|
|
|
+ * @param $userId 用户ID
|
|
|
+ * @param $scene 场景
|
|
|
+ * @param int $sourceId 来源ID
|
|
|
+ * @return false
|
|
|
+ */
|
|
|
+ public function updateTask($userId, $scene, $sourceId=0)
|
|
|
+ {
|
|
|
+ $taskList = $this->getTaskByScene($scene);
|
|
|
+ if($taskList){
|
|
|
+ $hasDay = false;
|
|
|
+ foreach ($taskList as $item){
|
|
|
+ $item['status'] = 1;
|
|
|
+ $taskId = isset($item['id'])? $item['id'] : 0;
|
|
|
+ $checkType = isset($item['check_type'])? $item['check_type'] : 1;
|
|
|
+ $type = isset($item['type'])? $item['type'] : 1;
|
|
|
+ $num = isset($item['num'])? $item['num'] : 1;
|
|
|
+ $taskName = isset($item['name']) && $item['name']? $item['name'] : '任务';
|
|
|
+ $power = isset($item['power'])? floatval($item['power']) : 0;
|
|
|
+
|
|
|
+ // 验证更新完成记录,若无数据更新则跳过
|
|
|
+ $errorKey = "caches:task:scene_error:{$taskId}_{$userId}";
|
|
|
+ if(!$this->updateLogData($userId, $taskId, $type, $checkType, $sourceId)){
|
|
|
+ RedisService::set("{$errorKey}_error", ['info'=> $item,'user_id'=> $userId,'source_id'=> $sourceId,'error'=>'该任务已完成'], rand(300, 600));
|
|
|
+ continue;
|
|
|
+ }
|
|
|
+
|
|
|
+ // 获取验证数据
|
|
|
+ $logNum = $this->getLogNum($userId, $taskId, $type);
|
|
|
+ // 次数
|
|
|
+ if($item['check_type'] == 1 && $num<=$logNum){
|
|
|
+ $item['status'] = 2;
|
|
|
+ $item['log_num'] = $logNum;
|
|
|
+ }
|
|
|
+ // 时长
|
|
|
+ else if($item['check_type'] == 2 && $logNum>0){
|
|
|
+ $item['status'] = 2;
|
|
|
+ $item['log_num'] = $logNum;
|
|
|
+ }
|
|
|
+
|
|
|
+ // 新手任务奖励
|
|
|
+ if($type == 2 && $item['status'] == 2) {
|
|
|
+ FinanceService::make()->settleTaskPower($userId, $power, $taskId, "完成{$taskName}任务奖励");
|
|
|
+ }else if($type == 1 && $item['status'] == 2){
|
|
|
+ $hasDay = true;
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ // 当前有每日任务,处理
|
|
|
+ if($hasDay){
|
|
|
+ $dayTaskList = $this->getTaskByType(1);
|
|
|
+ if($dayTaskList){
|
|
|
+ $power = 0;
|
|
|
+ $completeCount = 0;
|
|
|
+ foreach ($dayTaskList as $item){
|
|
|
+ $taskId = isset($item['id'])? $item['id'] : 0;
|
|
|
+ $type = isset($item['type'])? $item['type'] : 1;
|
|
|
+ $num = isset($item['num'])? $item['num'] : 1;
|
|
|
+ // 获取验证数据
|
|
|
+ $logNum = $this->getLogNum($userId, $taskId, $type);
|
|
|
+ // 次数
|
|
|
+ if($item['check_type'] == 1 && $num<=$logNum){
|
|
|
+ $completeCount++;
|
|
|
+ }
|
|
|
+ // 时长
|
|
|
+ else if($item['check_type'] == 2 && $logNum>0){
|
|
|
+ $completeCount++;
|
|
|
+ }
|
|
|
+
|
|
|
+ if($power<=0){
|
|
|
+ $power = isset($item['power'])? $item['power'] : 0;
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ // 所有任务完成
|
|
|
+ if($completeCount == count($dayTaskList)){
|
|
|
+ FinanceService::make()->settleTaskPower($userId, $power, 0, "完成每日任务奖励");
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ }
|
|
|
+ }
|
|
|
}
|