TaskService.php 12 KB

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