TaskTempService.php 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233
  1. <?php
  2. namespace App\Module\Game\Services;
  3. use App\Module\Game\Dtos\TaskChangeTempDto;
  4. use App\Module\Game\Logics\TaskTemp;
  5. use Illuminate\Support\Facades\Log;
  6. /**
  7. * 任务暂存服务类
  8. *
  9. * 提供任务暂存相关的服务方法,用于外部调用
  10. */
  11. class TaskTempService
  12. {
  13. /**
  14. * 获取用户的任务变更数据
  15. *
  16. * @param int $userId 用户ID
  17. * @return array 任务变更数据列表
  18. */
  19. public static function getUserTaskChanges(int $userId): array
  20. {
  21. try {
  22. return TaskTemp::getUserTaskChanges($userId);
  23. } catch (\Exception $e) {
  24. Log::error('获取用户任务变更数据失败', [
  25. 'user_id' => $userId,
  26. 'error' => $e->getMessage()
  27. ]);
  28. return [];
  29. }
  30. }
  31. /**
  32. * 获取用户指定任务的变更数据
  33. *
  34. * @param int $userId 用户ID
  35. * @param int $taskId 任务ID
  36. * @return TaskChangeTempDto|null 任务变更数据
  37. */
  38. public static function getUserTaskChange(int $userId, int $taskId): ?TaskChangeTempDto
  39. {
  40. try {
  41. return TaskTemp::getUserTaskChange($userId, $taskId);
  42. } catch (\Exception $e) {
  43. Log::error('获取用户指定任务变更数据失败', [
  44. 'user_id' => $userId,
  45. 'task_id' => $taskId,
  46. 'error' => $e->getMessage()
  47. ]);
  48. return null;
  49. }
  50. }
  51. /**
  52. * 记录任务进度更新
  53. *
  54. * @param int $userId 用户ID
  55. * @param int $taskId 任务ID
  56. * @param string $taskName 任务名称
  57. * @param int $progress 进度百分比
  58. * @param array $progressDetails 进度详情
  59. * @return bool 是否成功
  60. */
  61. public static function recordTaskProgressUpdate(int $userId, int $taskId, string $taskName, int $progress, array $progressDetails = []): bool
  62. {
  63. try {
  64. TaskTemp::handleTaskProgressUpdated($userId, $taskId, $taskName, $progress, $progressDetails);
  65. return true;
  66. } catch (\Exception $e) {
  67. Log::error('记录任务进度更新失败', [
  68. 'user_id' => $userId,
  69. 'task_id' => $taskId,
  70. 'progress' => $progress,
  71. 'error' => $e->getMessage()
  72. ]);
  73. return false;
  74. }
  75. }
  76. /**
  77. * 清理用户的任务变更数据
  78. *
  79. * @param int $userId 用户ID
  80. * @return bool 是否成功
  81. */
  82. public static function clearUserTaskChanges(int $userId): bool
  83. {
  84. try {
  85. TaskTemp::clearUserTaskChanges($userId);
  86. return true;
  87. } catch (\Exception $e) {
  88. Log::error('清理用户任务变更数据失败', [
  89. 'user_id' => $userId,
  90. 'error' => $e->getMessage()
  91. ]);
  92. return false;
  93. }
  94. }
  95. /**
  96. * 清理用户指定任务的变更数据
  97. *
  98. * @param int $userId 用户ID
  99. * @param int $taskId 任务ID
  100. * @return bool 是否成功
  101. */
  102. public static function clearUserTaskChange(int $userId, int $taskId): bool
  103. {
  104. try {
  105. TaskTemp::clearUserTaskChange($userId, $taskId);
  106. return true;
  107. } catch (\Exception $e) {
  108. Log::error('清理用户指定任务变更数据失败', [
  109. 'user_id' => $userId,
  110. 'task_id' => $taskId,
  111. 'error' => $e->getMessage()
  112. ]);
  113. return false;
  114. }
  115. }
  116. /**
  117. * 获取用户任务变更统计信息
  118. *
  119. * @param int $userId 用户ID
  120. * @return array 统计信息
  121. */
  122. public static function getUserTaskChangeStats(int $userId): array
  123. {
  124. try {
  125. $changes = TaskTemp::getUserTaskChanges($userId);
  126. $stats = [
  127. 'total_changes' => count($changes),
  128. 'progress_updates' => 0,
  129. 'completed_tasks' => 0,
  130. 'reward_claims' => 0,
  131. 'latest_update' => null,
  132. ];
  133. foreach ($changes as $change) {
  134. switch ($change->changeType) {
  135. case 'progress_updated':
  136. $stats['progress_updates']++;
  137. break;
  138. case 'completed':
  139. $stats['completed_tasks']++;
  140. break;
  141. case 'reward_claimed':
  142. $stats['reward_claims']++;
  143. break;
  144. }
  145. if ($stats['latest_update'] === null || $change->updatedAt > $stats['latest_update']) {
  146. $stats['latest_update'] = $change->updatedAt;
  147. }
  148. }
  149. return $stats;
  150. } catch (\Exception $e) {
  151. Log::error('获取用户任务变更统计信息失败', [
  152. 'user_id' => $userId,
  153. 'error' => $e->getMessage()
  154. ]);
  155. return [
  156. 'total_changes' => 0,
  157. 'progress_updates' => 0,
  158. 'completed_tasks' => 0,
  159. 'reward_claims' => 0,
  160. 'latest_update' => null,
  161. ];
  162. }
  163. }
  164. /**
  165. * 检查用户是否有任务变更数据
  166. *
  167. * @param int $userId 用户ID
  168. * @return bool 是否有变更数据
  169. */
  170. public static function hasUserTaskChanges(int $userId): bool
  171. {
  172. try {
  173. $changes = TaskTemp::getUserTaskChanges($userId);
  174. return !empty($changes);
  175. } catch (\Exception $e) {
  176. Log::error('检查用户任务变更数据失败', [
  177. 'user_id' => $userId,
  178. 'error' => $e->getMessage()
  179. ]);
  180. return false;
  181. }
  182. }
  183. /**
  184. * 获取用户最近的任务变更
  185. *
  186. * @param int $userId 用户ID
  187. * @param int $limit 限制数量
  188. * @return array 最近的任务变更
  189. */
  190. public static function getUserRecentTaskChanges(int $userId, int $limit = 10): array
  191. {
  192. try {
  193. $changes = TaskTemp::getUserTaskChanges($userId);
  194. // 按更新时间倒序排序
  195. usort($changes, function ($a, $b) {
  196. return $b->updatedAt - $a->updatedAt;
  197. });
  198. // 限制数量
  199. return array_slice($changes, 0, $limit);
  200. } catch (\Exception $e) {
  201. Log::error('获取用户最近任务变更失败', [
  202. 'user_id' => $userId,
  203. 'limit' => $limit,
  204. 'error' => $e->getMessage()
  205. ]);
  206. return [];
  207. }
  208. }
  209. }