TaskService.php 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361
  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\AccountLogModel;
  13. use App\Models\TaskLogModel;
  14. use App\Models\TaskModel;
  15. use App\Services\BaseService;
  16. use App\Services\RedisService;
  17. use Illuminate\Support\Facades\DB;
  18. /**
  19. * 任务管理-服务类
  20. * @author laravel开发员
  21. * @since 2020/11/11
  22. * @package App\Services\Api
  23. */
  24. class TaskService extends BaseService
  25. {
  26. // 静态对象
  27. protected static $instance = null;
  28. /**
  29. * 构造函数
  30. * @author laravel开发员
  31. * @since 2020/11/11
  32. * GoodsService constructor.
  33. */
  34. public function __construct()
  35. {
  36. $this->model = new TaskModel();
  37. }
  38. /**
  39. * 静态入口
  40. * @return static|null
  41. */
  42. public static function make()
  43. {
  44. if (!self::$instance) {
  45. self::$instance = (new static());
  46. }
  47. return self::$instance;
  48. }
  49. /**
  50. * 每日任务
  51. * @param $params
  52. * @param int $pageSize
  53. * @return array
  54. */
  55. public function getTaskList($userId, $type=1)
  56. {
  57. $datas = $this->model->where(['type'=>$type,'status'=>1,'mark'=>1])
  58. ->select(['id','name','type','num','power','sort','check_type','scene'])
  59. ->orderBy('sort','desc')
  60. ->orderBy('id','desc')
  61. ->get();
  62. $datas = $datas? $datas->toArray() : [];
  63. $power = 0;
  64. $num = 0;
  65. $complete = 0;
  66. if($datas){
  67. foreach ($datas as &$item){
  68. $item['status'] = 1;
  69. if($power<=0){
  70. $power = $item['power'];
  71. }
  72. $logNum = $this->getLogNum($userId, $item['id'], $item['type']);
  73. // 次数
  74. $item['log_num'] = intval($logNum);
  75. if($item['check_type'] == 1 && $item['num']<=$logNum){
  76. $item['status'] = 2;
  77. $item['log_num'] = intval($item['num']);
  78. $complete++;
  79. }
  80. // 时长
  81. else if($item['check_type'] == 2 && $logNum>0 && $item['num'] <= ($logNum/60)){
  82. $item['status'] = 2;
  83. $item['log_num'] = 1;
  84. $complete++;
  85. }
  86. }
  87. unset($item);
  88. }
  89. return ['power'=>$power,'complete'=>$complete,'num'=>count($datas), 'list'=> $datas];
  90. }
  91. /**
  92. * 任务完成情况
  93. * @param $userId
  94. * @param $taskId
  95. * @param int $type
  96. * @return array|mixed
  97. */
  98. public function getLogNum($userId, $taskId, $type=1)
  99. {
  100. $cacheKey = "caches:task:check_{$userId}_{$taskId}_{$type}";
  101. $data = RedisService::get($cacheKey);
  102. if($data){
  103. return $data;
  104. }
  105. $data = TaskLogModel::where(['task_id'=> $taskId,'user_id'=> $userId,'status'=>1,'mark'=>1])
  106. ->where(function($query) use($type){
  107. // 每日任务
  108. if($type == 1){
  109. $query->where('date','=', date('Y-m-d'));
  110. }
  111. })->value('num');
  112. if($data){
  113. RedisService::set($cacheKey, $data, rand(3,5));
  114. }
  115. return $data;
  116. }
  117. /**
  118. * 获取对应场景下的任务列表
  119. * @param $scene 场景
  120. * @return array|mixed
  121. */
  122. public function getTaskByScene($scene)
  123. {
  124. $cacheKey = "caches:task:scene_{$scene}";
  125. $datas = RedisService::get($cacheKey);
  126. if($datas){
  127. return $datas;
  128. }
  129. $where = ['scene'=> $scene,'status'=>1,'mark'=>1];
  130. $datas = $this->model->where($where)
  131. ->select(['id','name','type','num','power','sort','check_type','scene'])
  132. ->orderBy('sort','desc')
  133. ->orderBy('id','desc')
  134. ->get();
  135. $datas = $datas? $datas->toArray() : [];
  136. if($datas)
  137. {
  138. RedisService::set($cacheKey, $datas, rand(300,600));
  139. }
  140. return $datas;
  141. }
  142. /**
  143. * 获取对应类型的任务列表
  144. * @param $type 类型:1-每日任务,2-新手任务
  145. * @return array|mixed
  146. */
  147. public function getTaskByType($type)
  148. {
  149. $cacheKey = "caches:task:type_{$type}";
  150. $datas = RedisService::get($cacheKey);
  151. if($datas){
  152. return $datas;
  153. }
  154. $datas = $this->model->where(['type'=> $type,'status'=>1,'mark'=>1])
  155. ->select(['id','name','type','num','power','sort','check_type','scene'])
  156. ->orderBy('sort','desc')
  157. ->orderBy('id','desc')
  158. ->get();
  159. $datas = $datas? $datas->toArray() : [];
  160. if($datas)
  161. {
  162. RedisService::set($cacheKey, $datas, rand(300,600));
  163. }
  164. return $datas;
  165. }
  166. /**
  167. * 验证更新任务完成记录数据
  168. * @param $userId 用户ID
  169. * @param $taskId 任务ID
  170. * @param int $type 任务类型:1-每日,2-新手
  171. * @param int $checkType 任务验证类型:1-次数,2-时长
  172. * @param int $sourceId 任务完成对象来源ID
  173. * @return false
  174. */
  175. public function updateLogData($userId, $taskId, $type=1, $checkType=1, $sourceId=0, $time=0)
  176. {
  177. $cacheKey = "caches:task:scene_update_{$userId}_{$taskId}_{$type}";
  178. if(RedisService::get($cacheKey)){
  179. return false;
  180. }
  181. // 是否已经完成
  182. $checkData = TaskLogModel::where(['task_id'=> $taskId,'user_id'=> $userId,'is_complete'=>1,'status'=>1,'mark'=>1])
  183. ->where(function($query) use($type){
  184. // 每日任务
  185. if($type == 1){
  186. $query->where('date','=', date('Y-m-d'));
  187. }
  188. })->select(['id','task_id','user_id'])->orderBy('id','desc')->first();
  189. $checkData = $checkData? $checkData->toArray() : [];
  190. if($checkData){
  191. RedisService::set($cacheKey, $checkData, rand(300,600));
  192. return false;
  193. }
  194. // 清除缓存
  195. RedisService::clear("caches:task:check_{$userId}_{$taskId}_{$type}");
  196. // 清除数据
  197. TaskLogModel::from('task_log as a')->leftJoin('task as b','b.id','=','a.task_id')
  198. ->where(['a.user_id'=> $userId,'b.type'=>1,'a.status'=>1,'a.mark'=>1])
  199. ->where('a.date','<=', date('Y-m-d', time() - 7*86400))
  200. ->delete();
  201. $logId = TaskLogModel::where(['task_id'=> $taskId,'user_id'=> $userId,'status'=>1,'mark'=>1])
  202. ->where(function($query) use($type){
  203. // 每日任务
  204. if($type == 1){
  205. $query->where('date','=', date('Y-m-d'));
  206. }
  207. })->value('id');
  208. if($logId){
  209. if($checkType == 1){
  210. $updateData = ['num'=> DB::raw('num + 1'),'update_time'=>time()];
  211. }else{
  212. $updateData = ['num'=> $time>0? $time: 1,'update_time'=>time()];
  213. }
  214. return TaskLogModel::where(['id'=> $logId])->update($updateData);
  215. }else{
  216. return TaskLogModel::insert([
  217. 'user_id'=> $userId,
  218. 'task_id'=> $taskId,
  219. 'source_id'=> $sourceId,
  220. 'num'=> $checkType==1? 1 : $time,
  221. 'date'=> date('Y-m-d'),
  222. 'create_time'=> time(),
  223. 'update_time'=> time(),
  224. 'status'=>1,
  225. 'mark'=>1,
  226. ]);
  227. }
  228. }
  229. /**
  230. * 验证更新任务完成状态,并结算
  231. * @param $userId 用户ID
  232. * @param $scene 场景
  233. * @param int $sourceId 来源ID
  234. * @return false
  235. */
  236. public function updateTask($userId, $scene, $sourceId=0,$time=0)
  237. {
  238. if(RedisService::get("caches:task:lock_{$userId}_{$scene}_{$sourceId}")){
  239. return false;
  240. }
  241. $taskList = $this->getTaskByScene($scene);
  242. RedisService::set("caches:task:lock_{$userId}_{$scene}_{$sourceId}", true, rand(2,3));
  243. if($taskList){
  244. $hasDay = false;
  245. $completeIds = [];
  246. foreach ($taskList as $item){
  247. $item['status'] = 1;
  248. $taskId = isset($item['id'])? $item['id'] : 0;
  249. $checkType = isset($item['check_type'])? $item['check_type'] : 1;
  250. $type = isset($item['type'])? $item['type'] : 1;
  251. $num = isset($item['num'])? $item['num'] : 1;
  252. $taskName = isset($item['name']) && $item['name']? $item['name'] : '任务';
  253. $power = isset($item['power'])? floatval($item['power']) : 0;
  254. // 验证更新完成记录,若无数据更新则跳过
  255. $errorKey = "caches:task:scene_error:{$taskId}_{$userId}";
  256. if(!$this->updateLogData($userId, $taskId, $type, $checkType, $sourceId, $time)){
  257. RedisService::set("{$errorKey}_error", ['info'=> $item,'user_id'=> $userId,'source_id'=> $sourceId,'error'=>'该任务已完成'], rand(300, 600));
  258. continue;
  259. }
  260. // 获取验证数据
  261. $logNum = $this->getLogNum($userId, $taskId, $type);
  262. // 次数
  263. if($item['check_type'] == 1 && $num<=$logNum){
  264. $item['status'] = 2;
  265. $item['log_num'] = $logNum;
  266. }
  267. // 时长
  268. else if($item['check_type'] == 2 && $logNum>0 && $num <= ($logNum/60)){
  269. $item['status'] = 2;
  270. $item['log_num'] = 1;
  271. }
  272. // 新手任务奖励
  273. if($type == 2 && $item['status'] == 2) {
  274. FinanceService::make()->settleTaskPower($userId, $power, 22, $taskId, "{$taskName}任务奖励");
  275. $completeIds[] = $taskId;
  276. }else if($type == 1 && $item['status'] == 2){
  277. $completeIds[] = $taskId;
  278. $hasDay = true;
  279. }
  280. }
  281. // 更新完成
  282. if($completeIds){
  283. TaskLogModel::whereIn('task_id',$completeIds)->update(['is_complete'=>1,'update_time'=>time()]);
  284. }
  285. // 当前有每日任务,处理
  286. if($hasDay){
  287. $dayTaskList = $this->getTaskByType(1);
  288. if($dayTaskList){
  289. $power = 0;
  290. $completeCount = 0;
  291. $ids = [];
  292. foreach ($dayTaskList as $item){
  293. $taskId = isset($item['id'])? $item['id'] : 0;
  294. $type = isset($item['type'])? $item['type'] : 1;
  295. $num = isset($item['num'])? $item['num'] : 1;
  296. // 获取验证数据
  297. $logNum = $this->getLogNum($userId, $taskId, $type);
  298. // 次数
  299. if($item['check_type'] == 1 && $num<=$logNum){
  300. $completeCount++;
  301. }
  302. // 时长
  303. else if($item['check_type'] == 2 && $logNum>0 && $num <= ($logNum/60)){
  304. $completeCount++;
  305. }
  306. if($power<=0){
  307. $power = isset($item['power'])? $item['power'] : 0;
  308. }
  309. $ids[] = $taskId;
  310. }
  311. // 所有任务完成
  312. RedisService::set("caches:task:day_count_{$userId}",['list'=> $dayTaskList,'count'=> $completeCount], 600);
  313. if($completeCount == count($dayTaskList)){
  314. if(!AccountLogModel::where(['user_id'=> $userId,'source_id'=>'d'.date('Ymd'),'coin_type'=>3,'status'=>1,'mark'=>1])->value('id')){
  315. FinanceService::make()->settleTaskPower($userId, $power, 21,'d'.date('Ymd'), "每日任务奖励");
  316. }
  317. // 今日任务更新完成
  318. if($ids){
  319. TaskLogModel::whereIn('task_id',$ids)->where('date','>=',date('Y-m-d'))->update(['is_complete'=>1,'update_time'=>time()]);
  320. }
  321. }
  322. }
  323. }
  324. }
  325. }
  326. }