SocialFarmService.php 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394
  1. <?php
  2. namespace App\Module\SocialFarm\Services;
  3. use App\Module\SocialFarm\Logics\StealLogic;
  4. use App\Module\SocialFarm\Logics\VisitLogic;
  5. use App\Module\SocialFarm\Logics\HelpLogic;
  6. use App\Module\SocialFarm\Logics\PermissionLogic;
  7. use App\Module\SocialFarm\Models\SocialFarmSetting;
  8. use App\Module\SocialFarm\Enums\STEAL_STATUS;
  9. use App\Module\SocialFarm\Enums\VISIT_TYPE;
  10. use App\Module\SocialFarm\Enums\HELP_TYPE;
  11. use Illuminate\Support\Facades\DB;
  12. use Illuminate\Support\Facades\Log;
  13. /**
  14. * 社交农场主服务类
  15. *
  16. * 提供社交农场功能的统一对外接口
  17. */
  18. class SocialFarmService
  19. {
  20. /**
  21. * 访问好友农场
  22. *
  23. * @param int $visitorId 访问者ID
  24. * @param int $ownerId 农场主ID
  25. * @param int $visitType 访问类型
  26. * @return array 访问结果
  27. */
  28. public static function visitFriendFarm(int $visitorId, int $ownerId, int $visitType = VISIT_TYPE::NORMAL->value): array
  29. {
  30. try {
  31. // 验证访问权限
  32. $permissionCheck = PermissionLogic::checkVisitPermission($visitorId, $ownerId);
  33. if (!$permissionCheck['allowed']) {
  34. return [
  35. 'success' => false,
  36. 'code' => 'NO_PERMISSION',
  37. 'message' => $permissionCheck['reason']
  38. ];
  39. }
  40. // 执行访问逻辑
  41. $visitResult = VisitLogic::recordVisit($visitorId, $ownerId, $visitType);
  42. // 获取农场信息
  43. $farmInfo = static::getFriendFarmInfo($ownerId, $visitorId);
  44. return [
  45. 'success' => true,
  46. 'data' => [
  47. 'visit_id' => $visitResult['visit_id'],
  48. 'farm_info' => $farmInfo,
  49. 'permissions' => $permissionCheck['permissions']
  50. ]
  51. ];
  52. } catch (\Exception $e) {
  53. Log::error('访问好友农场失败', [
  54. 'visitor_id' => $visitorId,
  55. 'owner_id' => $ownerId,
  56. 'error' => $e->getMessage()
  57. ]);
  58. return [
  59. 'success' => false,
  60. 'code' => 'VISIT_FAILED',
  61. 'message' => '访问失败,请稍后重试'
  62. ];
  63. }
  64. }
  65. /**
  66. * 偷菜操作
  67. *
  68. * @param int $stealerId 偷菜者ID
  69. * @param int $ownerId 农场主ID
  70. * @param int $landId 土地ID
  71. * @return array 偷菜结果
  72. */
  73. public static function stealCrop(int $stealerId, int $ownerId, int $landId): array
  74. {
  75. try {
  76. // 开启事务
  77. return DB::transaction(function () use ($stealerId, $ownerId, $landId) {
  78. // 验证偷菜条件
  79. $canSteal = StealLogic::canSteal($stealerId, $ownerId, $landId);
  80. if (!$canSteal['allowed']) {
  81. return [
  82. 'success' => false,
  83. 'code' => $canSteal['code'],
  84. 'message' => $canSteal['reason']
  85. ];
  86. }
  87. // 执行偷菜
  88. $stealResult = StealLogic::executeSteal($stealerId, $ownerId, $landId);
  89. // 记录访问(如果还没有记录)
  90. VisitLogic::recordVisit($stealerId, $ownerId, VISIT_TYPE::STEAL_VISIT->value);
  91. return [
  92. 'success' => true,
  93. 'data' => $stealResult
  94. ];
  95. });
  96. } catch (\Exception $e) {
  97. Log::error('偷菜操作失败', [
  98. 'stealer_id' => $stealerId,
  99. 'owner_id' => $ownerId,
  100. 'land_id' => $landId,
  101. 'error' => $e->getMessage()
  102. ]);
  103. return [
  104. 'success' => false,
  105. 'code' => 'STEAL_FAILED',
  106. 'message' => '偷菜失败,请稍后重试'
  107. ];
  108. }
  109. }
  110. /**
  111. * 互助操作
  112. *
  113. * @param int $helperId 帮助者ID
  114. * @param int $ownerId 农场主ID
  115. * @param int $landId 土地ID
  116. * @param int $helpType 帮助类型
  117. * @return array 互助结果
  118. */
  119. public static function helpFriend(int $helperId, int $ownerId, int $landId, int $helpType): array
  120. {
  121. try {
  122. // 开启事务
  123. return DB::transaction(function () use ($helperId, $ownerId, $landId, $helpType) {
  124. // 验证互助条件
  125. $canHelp = HelpLogic::canHelp($helperId, $ownerId, $landId, $helpType);
  126. if (!$canHelp['allowed']) {
  127. return [
  128. 'success' => false,
  129. 'code' => $canHelp['code'],
  130. 'message' => $canHelp['reason']
  131. ];
  132. }
  133. // 执行互助
  134. $helpResult = HelpLogic::executeHelp($helperId, $ownerId, $landId, $helpType);
  135. // 记录访问(如果还没有记录)
  136. VisitLogic::recordVisit($helperId, $ownerId, VISIT_TYPE::HELP_VISIT->value);
  137. return [
  138. 'success' => true,
  139. 'data' => $helpResult
  140. ];
  141. });
  142. } catch (\Exception $e) {
  143. Log::error('互助操作失败', [
  144. 'helper_id' => $helperId,
  145. 'owner_id' => $ownerId,
  146. 'land_id' => $landId,
  147. 'help_type' => $helpType,
  148. 'error' => $e->getMessage()
  149. ]);
  150. return [
  151. 'success' => false,
  152. 'code' => 'HELP_FAILED',
  153. 'message' => '互助失败,请稍后重试'
  154. ];
  155. }
  156. }
  157. /**
  158. * 获取可偷菜的好友列表
  159. *
  160. * @param int $userId 用户ID
  161. * @return array 好友列表
  162. */
  163. public static function getStealableFriends(int $userId): array
  164. {
  165. try {
  166. // 获取好友列表
  167. $friends = \App\Module\Friend\Services\FriendService::getFriendList($userId);
  168. $stealableFriends = [];
  169. foreach ($friends['items'] as $friend) {
  170. $friendId = $friend['user_id'];
  171. // 检查是否允许偷菜
  172. $permission = PermissionLogic::checkStealPermission($userId, $friendId);
  173. if (!$permission['allowed']) {
  174. continue;
  175. }
  176. // 获取可偷菜的作物数量
  177. $stealableCount = StealLogic::getStealableCropCount($friendId);
  178. if ($stealableCount > 0) {
  179. $friend['stealable_count'] = $stealableCount;
  180. $friend['last_visit_time'] = VisitLogic::getLastVisitTime($userId, $friendId);
  181. $stealableFriends[] = $friend;
  182. }
  183. }
  184. return [
  185. 'success' => true,
  186. 'data' => $stealableFriends
  187. ];
  188. } catch (\Exception $e) {
  189. Log::error('获取可偷菜好友列表失败', [
  190. 'user_id' => $userId,
  191. 'error' => $e->getMessage()
  192. ]);
  193. return [
  194. 'success' => false,
  195. 'message' => '获取好友列表失败'
  196. ];
  197. }
  198. }
  199. /**
  200. * 获取好友农场信息
  201. *
  202. * @param int $ownerId 农场主ID
  203. * @param int $visitorId 访问者ID
  204. * @return array 农场信息
  205. */
  206. public static function getFriendFarmInfo(int $ownerId, int $visitorId): array
  207. {
  208. try {
  209. // 获取农场基础信息
  210. $farmInfo = \App\Module\Farm\Services\FarmService::getFarmInfo($ownerId);
  211. // 获取土地信息(包含作物状态)
  212. $lands = \App\Module\Farm\Services\LandService::getUserLands($ownerId);
  213. // 为每块土地添加社交相关信息
  214. foreach ($lands as &$land) {
  215. if ($land['crop']) {
  216. // 检查是否可偷菜
  217. $canSteal = StealLogic::canStealCrop($visitorId, $ownerId, $land['id']);
  218. $land['can_steal'] = $canSteal['allowed'];
  219. $land['steal_reason'] = $canSteal['reason'] ?? '';
  220. // 检查是否可互助
  221. $canHelp = HelpLogic::getAvailableHelpTypes($visitorId, $ownerId, $land['id']);
  222. $land['help_types'] = $canHelp;
  223. }
  224. }
  225. return [
  226. 'farm_info' => $farmInfo,
  227. 'lands' => $lands,
  228. 'owner_settings' => static::getUserSocialSettings($ownerId)
  229. ];
  230. } catch (\Exception $e) {
  231. Log::error('获取好友农场信息失败', [
  232. 'owner_id' => $ownerId,
  233. 'visitor_id' => $visitorId,
  234. 'error' => $e->getMessage()
  235. ]);
  236. return [];
  237. }
  238. }
  239. /**
  240. * 获取用户社交设置
  241. *
  242. * @param int $userId 用户ID
  243. * @return array 社交设置
  244. */
  245. public static function getUserSocialSettings(int $userId): array
  246. {
  247. $settings = SocialFarmSetting::where('user_id', $userId)->first();
  248. if (!$settings) {
  249. // 创建默认设置
  250. $settings = SocialFarmSetting::create(['user_id' => $userId]);
  251. }
  252. return [
  253. 'allow_steal' => $settings->allow_steal,
  254. 'allow_help' => $settings->allow_help,
  255. 'allow_visit' => $settings->allow_visit,
  256. 'steal_protection_hours' => $settings->steal_protection_hours,
  257. 'daily_steal_limit' => $settings->daily_steal_limit,
  258. 'daily_help_limit' => $settings->daily_help_limit,
  259. 'notification_enabled' => $settings->notification_enabled,
  260. 'friend_only' => $settings->friend_only,
  261. ];
  262. }
  263. /**
  264. * 更新用户社交设置
  265. *
  266. * @param int $userId 用户ID
  267. * @param array $settings 设置数据
  268. * @return array 更新结果
  269. */
  270. public static function updateUserSocialSettings(int $userId, array $settings): array
  271. {
  272. try {
  273. $userSettings = SocialFarmSetting::where('user_id', $userId)->first();
  274. if (!$userSettings) {
  275. $userSettings = new SocialFarmSetting(['user_id' => $userId]);
  276. }
  277. // 更新设置
  278. $allowedFields = [
  279. 'allow_steal', 'allow_help', 'allow_visit',
  280. 'steal_protection_hours', 'daily_steal_limit', 'daily_help_limit',
  281. 'notification_enabled', 'auto_revenge', 'friend_only'
  282. ];
  283. foreach ($allowedFields as $field) {
  284. if (isset($settings[$field])) {
  285. $userSettings->$field = $settings[$field];
  286. }
  287. }
  288. $userSettings->save();
  289. return [
  290. 'success' => true,
  291. 'data' => $userSettings->toArray()
  292. ];
  293. } catch (\Exception $e) {
  294. Log::error('更新用户社交设置失败', [
  295. 'user_id' => $userId,
  296. 'settings' => $settings,
  297. 'error' => $e->getMessage()
  298. ]);
  299. return [
  300. 'success' => false,
  301. 'message' => '设置更新失败'
  302. ];
  303. }
  304. }
  305. /**
  306. * 获取用户社交统计
  307. *
  308. * @param int $userId 用户ID
  309. * @param string $date 统计日期(可选,默认今天)
  310. * @return array 统计数据
  311. */
  312. public static function getUserSocialStats(int $userId, string $date = null): array
  313. {
  314. try {
  315. $date = $date ?: date('Y-m-d');
  316. $stats = \App\Module\SocialFarm\Models\SocialFarmStats::where('user_id', $userId)
  317. ->where('stat_date', $date)
  318. ->first();
  319. if (!$stats) {
  320. return [
  321. 'steal_count' => 0,
  322. 'stolen_count' => 0,
  323. 'help_count' => 0,
  324. 'helped_count' => 0,
  325. 'visit_count' => 0,
  326. 'visited_count' => 0,
  327. 'steal_items_gained' => 0,
  328. 'steal_items_lost' => 0,
  329. 'help_rewards_gained' => 0,
  330. 'total_exp_gained' => 0,
  331. ];
  332. }
  333. return $stats->toArray();
  334. } catch (\Exception $e) {
  335. Log::error('获取用户社交统计失败', [
  336. 'user_id' => $userId,
  337. 'date' => $date,
  338. 'error' => $e->getMessage()
  339. ]);
  340. return [];
  341. }
  342. }
  343. }