TaskService.php 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353
  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. use BN\Red;
  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'] = $logNum;
  75. if($item['check_type'] == 1 && $item['num']<=$logNum){
  76. $item['status'] = 2;
  77. $item['log_num'] = $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. $logId = TaskLogModel::where(['task_id'=> $taskId,'user_id'=> $userId,'status'=>1,'mark'=>1])
  197. ->where(function($query) use($type){
  198. // 每日任务
  199. if($type == 1){
  200. $query->where('date','=', date('Y-m-d'));
  201. }
  202. })->value('id');
  203. if($logId){
  204. if($checkType == 1){
  205. $updateData = ['num'=> DB::raw('num + 1'),'update_time'=>time()];
  206. }else{
  207. $updateData = ['num'=> $time>0? $time: 1,'update_time'=>time()];
  208. }
  209. return TaskLogModel::where(['id'=> $logId])->update($updateData);
  210. }else{
  211. return TaskLogModel::insert([
  212. 'user_id'=> $userId,
  213. 'task_id'=> $taskId,
  214. 'source_id'=> $sourceId,
  215. 'num'=> $checkType==1? 1 : $time,
  216. 'date'=> date('Y-m-d'),
  217. 'create_time'=> time(),
  218. 'update_time'=> time(),
  219. 'status'=>1,
  220. 'mark'=>1,
  221. ]);
  222. }
  223. }
  224. /**
  225. * 验证更新任务完成状态,并结算
  226. * @param $userId 用户ID
  227. * @param $scene 场景
  228. * @param int $sourceId 来源ID
  229. * @return false
  230. */
  231. public function updateTask($userId, $scene, $sourceId=0,$time=0)
  232. {
  233. if(RedisService::get("caches:task:lock_{$userId}_{$scene}_{$sourceId}")){
  234. return false;
  235. }
  236. $taskList = $this->getTaskByScene($scene);
  237. RedisService::set("caches:task:lock_{$userId}_{$scene}_{$sourceId}", true, rand(2,3));
  238. if($taskList){
  239. $hasDay = false;
  240. $completeIds = [];
  241. foreach ($taskList as $item){
  242. $item['status'] = 1;
  243. $taskId = isset($item['id'])? $item['id'] : 0;
  244. $checkType = isset($item['check_type'])? $item['check_type'] : 1;
  245. $type = isset($item['type'])? $item['type'] : 1;
  246. $num = isset($item['num'])? $item['num'] : 1;
  247. $taskName = isset($item['name']) && $item['name']? $item['name'] : '任务';
  248. $power = isset($item['power'])? floatval($item['power']) : 0;
  249. // 验证更新完成记录,若无数据更新则跳过
  250. $errorKey = "caches:task:scene_error:{$taskId}_{$userId}";
  251. if(!$this->updateLogData($userId, $taskId, $type, $checkType, $sourceId, $time)){
  252. RedisService::set("{$errorKey}_error", ['info'=> $item,'user_id'=> $userId,'source_id'=> $sourceId,'error'=>'该任务已完成'], rand(300, 600));
  253. continue;
  254. }
  255. // 获取验证数据
  256. $logNum = $this->getLogNum($userId, $taskId, $type);
  257. // 次数
  258. if($item['check_type'] == 1 && $num<=$logNum){
  259. $item['status'] = 2;
  260. $item['log_num'] = $logNum;
  261. }
  262. // 时长
  263. else if($item['check_type'] == 2 && $logNum>0 && $num <= ($logNum/60)){
  264. $item['status'] = 2;
  265. $item['log_num'] = 1;
  266. }
  267. // 新手任务奖励
  268. if($type == 2 && $item['status'] == 2) {
  269. FinanceService::make()->settleTaskPower($userId, $power, $taskId, "{$taskName}任务奖励");
  270. $completeIds[] = $taskId;
  271. }else if($type == 1 && $item['status'] == 2){
  272. $completeIds[] = $taskId;
  273. $hasDay = true;
  274. }
  275. }
  276. // 更新完成
  277. if($completeIds){
  278. TaskLogModel::whereIn('task_id',$completeIds)->update(['is_complete'=>1,'update_time'=>time()]);
  279. }
  280. // 当前有每日任务,处理
  281. if($hasDay){
  282. $dayTaskList = $this->getTaskByType(1);
  283. if($dayTaskList){
  284. $power = 0;
  285. $completeCount = 0;
  286. $ids = [];
  287. foreach ($dayTaskList as $item){
  288. $taskId = isset($item['id'])? $item['id'] : 0;
  289. $type = isset($item['type'])? $item['type'] : 1;
  290. $num = isset($item['num'])? $item['num'] : 1;
  291. // 获取验证数据
  292. $logNum = $this->getLogNum($userId, $taskId, $type);
  293. // 次数
  294. if($item['check_type'] == 1 && $num<=$logNum){
  295. $completeCount++;
  296. }
  297. // 时长
  298. else if($item['check_type'] == 2 && $logNum>0 && $num <= ($logNum/60)){
  299. $completeCount++;
  300. }
  301. if($power<=0){
  302. $power = isset($item['power'])? $item['power'] : 0;
  303. }
  304. $ids[] = $taskId;
  305. }
  306. // 所有任务完成
  307. RedisService::set("caches:task:day_count_{$userId}",['list'=> $dayTaskList,'count'=> $completeCount], 600);
  308. if($completeCount == count($dayTaskList)){
  309. FinanceService::make()->settleTaskPower($userId, $power, 0, "每日任务奖励");
  310. // 今日任务更新完成
  311. if($ids){
  312. TaskLogModel::whereIn('task_id',$ids)->where('date','>=',date('Y-m-d'))->update(['is_complete'=>1,'update_time'=>time()]);
  313. }
  314. }
  315. }
  316. }
  317. }
  318. }
  319. }