PairService.php 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444
  1. <?php
  2. namespace App\Http\Services\V1;
  3. use App\Generated\Exceptions\ConflictException;
  4. use App\Generated\Exceptions\NotFoundException;
  5. use App\Generated\Exceptions\UnprocessableEntityException;
  6. use App\Generated\V1\Messages\Pair\ActivityLastMessage;
  7. use App\Generated\V1\Messages\Pair\GetActivityConductStageIdMessage;
  8. use App\Generated\V1\Messages\Pair\ActivityRecentMessage;
  9. use App\Generated\V1\Messages\Pair\ActivityInfoMessage;
  10. use App\Generated\V1\Messages\Pair\LastenrollMessage;
  11. use App\Generated\V1\Messages\Pair\GetActivityListMessage;
  12. use App\Generated\V1\Messages\Pair\GetAloneGroupMessage;
  13. use App\Generated\V1\Messages\Pair\RemoveKeepCpMessage;
  14. use App\Generated\V1\Messages\Pair\KeepCpMessage;
  15. use App\Generated\V1\Messages\Pair\GetKeepRoomMessage;
  16. use App\Generated\V1\Messages\Pair\EnrollProMessage;
  17. use App\Generated\V1\Messages\Pair\EnrollFreeMessage;
  18. use App\Generated\V1\Messages\Pair\GetAllTagsMessage;
  19. use App\Generated\V1\Messages\Pair\AddScoreByFlowerMessage;
  20. use App\Generated\V1\Messages\Pair\RefundMessage;
  21. use App\Generated\V1\Messages\Pair\EnrollNextMessage;
  22. use App\Generated\V1\Messages\Pair\ConfirmBeAloneMessage;
  23. use App\Generated\V1\Messages\Pair\OperationalLogMessage;
  24. use App\Generated\V1\Messages\Pair\RegretsMessage;
  25. use App\Generated\V1\Messages\Pair\ConfirmCpMessage;
  26. use App\Generated\Exceptions\CustomErrorMessageException;
  27. use App\Http\Controllers\Core\Auth;
  28. use App\Models\Fpdx\GroupMemberModel;
  29. use App\Models\Fpdx\OperationalLogModel;
  30. use Illuminate\Support\Facades\DB;
  31. use App\Models\TagModel;
  32. use App\Models\Fpdx\ActivityModel;
  33. use App\Models\Fpdx\RoomModel;
  34. use App\Models\User\UserModel;
  35. use App\Models\Fpdx\GroupModel;
  36. use App\Models\Fpdx\PairModel;
  37. use Illuminate\Database\Eloquent\Collection;
  38. use Illuminate\Database\Eloquent\ModelNotFoundException;
  39. use Tymon\JWTAuth\Exceptions\JWTException;
  40. use App\Managers\OrderManager;
  41. use App\Managers\PairManager;
  42. class PairService
  43. {
  44. protected $pairManager;
  45. protected $orderManager;
  46. public function __construct(PairManager $pairManager, OrderManager $orderManager)
  47. {
  48. $this->pairManager = $pairManager;
  49. $this->orderManager = $orderManager;
  50. }
  51. public function activityLast(ActivityLastMessage $message)
  52. {
  53. $activityType = $message->getActivityType() ?: '72h';
  54. $activity = ActivityModel::where(function ($query) use ($activityType) {
  55. if ($activityType == 'qbj') {
  56. $query->where('qbj_stage_id', '>', 0);
  57. }
  58. })->orderBy('stage_id', 'desc')->first();
  59. try {
  60. $uid = Auth::auth();
  61. $first_pair = PairModel::where('uid', $uid)->where(function ($query) {
  62. /** @var PairModel $query */
  63. $query->whereNotNull('assoc_id')
  64. ->orWhere('last_assoc', '>', 0);
  65. })->first();
  66. $activity->first_pair = $first_pair ? false : true;
  67. } catch (JWTException $e) {
  68. }
  69. $message->setResponse($activity);
  70. }
  71. /**
  72. * 获取某人最近一期的活动状态
  73. *
  74. * @param ActivityRecentMessage $message
  75. */
  76. public function activityRecent(ActivityRecentMessage $message)
  77. {
  78. $uid = Auth::auth();
  79. $data = $this->pairManager->getRecentState($uid);
  80. $message->setResponse($data);
  81. }
  82. public function activityInfo(ActivityInfoMessage $message)
  83. {
  84. $stage_id = $message->getStageId();
  85. $uid = Auth::auth();
  86. $data = $this->pairManager->getActivityInfo($uid, $stage_id);
  87. $message->setResponse($data);
  88. }
  89. public function getActivityConductStageId(GetActivityConductStageIdMessage $message)
  90. {
  91. $ings = ActivityModel::getActivitys();
  92. $data = ActivityModel::findOrFail($ings['ing']);
  93. $message->setResponse($data);
  94. }
  95. public function getAllTags(GetAllTagsMessage $message)
  96. {
  97. $datas = TagModel::get()->toArray();
  98. $result = array();
  99. foreach ($datas as $data) {
  100. $tmp = array(
  101. 'id' => (string)$data['id'],
  102. 'name' => $data['name'],
  103. 'group' => (string)$data['group']
  104. );
  105. $result[$data['group']][$data['id']] = $tmp;
  106. }
  107. $message->setResponse($result);
  108. }
  109. public function lastenroll(LastenrollMessage $message)
  110. {
  111. $ings = ActivityModel::getActivitys();
  112. /** @var Collection $pairs */
  113. $pairs = PairModel::where([
  114. ['stage_id', $ings['next']],
  115. ['state', '>', 100]
  116. ])->take(30)->orderBy('id', 'desc')
  117. ->get(['id', 'sex', 'stage_id', 'create_time', 'uid']);
  118. $users = UserModel::whereIn('uid', $pairs->pluck('uid'))->get([
  119. 'uid',
  120. 'headimgurl',
  121. 'nickname',
  122. 'sex',
  123. 'address',
  124. 'home'
  125. ]);
  126. foreach ($pairs as &$pair) {
  127. /** @var UserModel $tmp */
  128. $tmp = $users->where('uid', $pair->uid) ?? [];
  129. foreach ($tmp as $v) {
  130. $pair->user = $v;
  131. }
  132. }
  133. $message->setResponse($pairs);
  134. }
  135. public function getActivityList(GetActivityListMessage $message)
  136. {
  137. $uid = Auth::auth();
  138. $lists = DB::table("kdgx_partner_charge_activity")
  139. ->join(
  140. "kdgx_partner_charge_pair",
  141. "kdgx_partner_charge_activity.stage_id",
  142. "kdgx_partner_charge_pair.stage_id"
  143. )
  144. ->where([['kdgx_partner_charge_pair.uid', $uid], ['kdgx_partner_charge_pair.hidden_at', 0]])
  145. ->orderBy("kdgx_partner_charge_pair.stage_id", "desc")->get();
  146. foreach ($lists as &$list) {
  147. try {
  148. $pair = PairModel::find($list->id);
  149. $progress = $pair->progress();
  150. $list->progress = $progress;
  151. } catch (\Exception $e) {
  152. app('sentry')->captureException($e);
  153. }
  154. $userInfo = DB::table("kdgx_partner_charge_pair")
  155. ->join("kdgx_partner_charge_user", "kdgx_partner_charge_pair.uid", "kdgx_partner_charge_user.uid")
  156. ->where([
  157. ['kdgx_partner_charge_pair.uid', $uid],
  158. ['kdgx_partner_charge_pair.stage_id', $list->stage_id]
  159. ])
  160. ->first();
  161. if (is_null($userInfo->assoc_id)) {
  162. $list->pair_info = ['user_info' => $userInfo];
  163. } else {
  164. $partnerInfo = DB::table("kdgx_partner_charge_pair")
  165. ->join("kdgx_partner_charge_user", "kdgx_partner_charge_pair.uid", "kdgx_partner_charge_user.uid")
  166. ->where('kdgx_partner_charge_pair.id', $userInfo->assoc_id)
  167. ->first();
  168. try {
  169. $roomInfo = RoomModel::findOrFail($partnerInfo->room_id);
  170. $roomInfo->online = null;
  171. $roomInfo->unread_msg = 0;
  172. } catch (ModelNotFoundException $exception) {
  173. $roomInfo = [];
  174. }
  175. $list->pair_info = ['user_info' => $userInfo, 'partner_info' => $partnerInfo, 'room_info' => $roomInfo];
  176. }
  177. }
  178. $message->setResponse($lists);
  179. }
  180. public function getKeepRoom(GetKeepRoomMessage $message)
  181. {
  182. $uid = Auth::auth();
  183. $room_id = $this->pairManager->getKeepRoom($uid);
  184. $room = RoomModel::find($room_id);
  185. $message->setResponse($room);
  186. }
  187. public function keepCp(KeepCpMessage $message)
  188. {
  189. $uid = Auth::auth();
  190. $room_id = $message->getRoomId();
  191. $this->pairManager->keepRoom($uid, $room_id);
  192. }
  193. public function removeKeepCp(RemoveKeepCpMessage $message)
  194. {
  195. $uid = Auth::auth();
  196. $room_id = $message->getRoomId();
  197. $this->pairManager->removeKeepRoom($uid, $room_id);
  198. }
  199. public function getAloneGroup(GetAloneGroupMessage $message)
  200. {
  201. $uid = Auth::auth();
  202. $stageId = $message->getStageId();
  203. $activityType = $message->getActivityType();
  204. $groups = GroupMemberModel::where('uid', $uid)
  205. ->when($stageId, function ($query) use ($stageId) {
  206. return $query->where('stage_id', $stageId);
  207. })->when($activityType, function ($query) use ($activityType) {
  208. return $query->where('activity_type', $activityType);
  209. })->get();
  210. foreach ($groups as $group) {
  211. $group->group = GroupModel::find($group->group_id);
  212. }
  213. $message->setResponse($group);
  214. }
  215. /**
  216. * 付费报名
  217. * @param EnrollProMessage $message
  218. * @throws ConflictException
  219. * @throws CustomErrorMessageException
  220. * @throws NotFoundException
  221. * @throws UnprocessableEntityException
  222. * @throws \GuzzleHttp\Exception\GuzzleException
  223. */
  224. public function enrollPro(EnrollProMessage $message)
  225. {
  226. $stageId = $message->getStageId();
  227. $mediaId = $message->getMediaId() ?: config('miniprogram.public_id');
  228. $agent = $message->getRequest()->header('user-agent');
  229. $couponId = $message->getCouponId();
  230. $device = 'other';
  231. if (
  232. strpos($agent, 'macintosh')
  233. || strpos($agent, 'iPod')
  234. || strpos($agent, 'iPhone')
  235. || strpos($agent, 'iPad')
  236. ) {
  237. $device = "ios";
  238. }
  239. if (strpos($agent, 'Android')) {
  240. $device = 'android';
  241. }
  242. $uid = Auth::auth();
  243. $user = UserModel::find($uid);
  244. if (!in_array($user->sex, [1, 2])) {
  245. throw new UnprocessableEntityException('请选择性别');
  246. }
  247. if (!in_array($user->sxo, [1, 2])) {
  248. throw new UnprocessableEntityException('请选择性取向');
  249. }
  250. $activity = ActivityModel::enrolling()->first();
  251. if (!$activity) {
  252. throw new NotFoundException("活动不存在");
  253. }
  254. if ($activity->stage_id !== $stageId) {
  255. throw new NotFoundException('活动报名已截止报名');
  256. }
  257. $pair = PairModel::where('uid', $uid)->where('stage_id', $stageId)->first();
  258. if ($pair) {
  259. throw new ConflictException("您已经报名本期活动,请勿重复报名哦");
  260. }
  261. if (isset($pair->order_id) && $pair->order_id > 0) {
  262. throw new CustomErrorMessageException('一次活动只能付费一次');
  263. }
  264. $attach = [
  265. 'uid' => $uid,
  266. 'stage_id' => $stageId,
  267. 'media_id' => $mediaId,
  268. 'state' => 103,
  269. 'device' => $device,
  270. ];
  271. $orderId = $this->orderManager->unifyOrderByPair($uid, $attach, $couponId);
  272. $message->setResponse(['order_id' => $orderId]);
  273. }
  274. public function enrollFree(EnrollFreeMessage $message)
  275. {
  276. $mediaId = $message->getMediaId() ?: config('miniprogram.public_id');
  277. $state = $message->getState() ?: 2;
  278. $score = $message->getScore();
  279. $stageId = $message->getStageId();
  280. $allowPhoto = $message->getAllowPhoto();
  281. $activityType = $message->getActivityType();
  282. $agent = $message->getRequest()->header('user-agent');
  283. $device = 'other';
  284. if (
  285. strpos($agent, 'macintosh')
  286. || strpos($agent, 'iPod')
  287. || strpos($agent, 'iPhone')
  288. || strpos($agent, 'iPad')
  289. ) {
  290. $device = "ios";
  291. }
  292. if (strpos($agent, 'Android')) {
  293. $device = 'android';
  294. }
  295. $uid = Auth::auth();
  296. $user = UserModel::find($uid);
  297. if (!in_array($user->sex, [1, 2])) {
  298. throw new CustomErrorMessageException('请先选择性别');
  299. }
  300. if ($user->sex == 2) {
  301. $state = 102;
  302. }
  303. $attribute = [
  304. 'stage_id' => $stageId,
  305. 'media_id' => $mediaId,
  306. 'uid' => $uid,
  307. 'sex' => $user->sex,
  308. 'state' => $state,
  309. 'data' => $device,
  310. 'add_score' => $score,
  311. 'allow_photo' => $allowPhoto,
  312. 'activity_type' => $activityType,
  313. ];
  314. $pair = PairModel::create($attribute);
  315. $message->setResponse($pair);
  316. }
  317. public function addScoreByFlower(AddScoreByFlowerMessage $message)
  318. {
  319. $uid = Auth::auth();
  320. $pairId = $message->getPairId();
  321. $flowers = [
  322. 'red_flower' => $message->getRedFlower(),
  323. 'gold_flower' => $message->getGoldFlower(),
  324. ];
  325. $ps = new \App\Services\Pair\PairService();
  326. $ps->addScoreByFlower($pairId, $flowers);
  327. }
  328. public function refund(RefundMessage $message)
  329. {
  330. $uid = Auth::auth();
  331. $pairId = $message->getPairId();
  332. $pair = PairModel::findOrFail($pairId);
  333. if ($pair->uid != $uid) {
  334. throw new CustomErrorMessageException('无权限');
  335. }
  336. $this->pairManager->refund($pair);
  337. }
  338. public function enrollNext(EnrollNextMessage $message)
  339. {
  340. $uid = Auth::auth();
  341. $stageId = $message->getLastStageId();
  342. $agent = $message->getRequest()->header('user-agent');
  343. $os = 'other';
  344. if (
  345. strpos($agent, 'macintosh')
  346. || strpos($agent, 'iPod')
  347. || strpos($agent, 'iPhone')
  348. || strpos($agent, 'iPad')
  349. ) {
  350. $os = "ios";
  351. }
  352. if (strpos($agent, 'Android')) {
  353. $os = 'android';
  354. }
  355. $data = [
  356. 'allow_photo' => $message->getAllowPhoto(),
  357. ];
  358. $pairService = new \App\Services\Pair\PairService();
  359. $pairId = $pairService->enrollNext($stageId, $uid, $data, $os);
  360. $message->setResponse([
  361. 'pair_id' => $pairId
  362. ]);
  363. }
  364. public function confirmBeAlone(ConfirmBeAloneMessage $message)
  365. {
  366. $uid = Auth::auth();
  367. $stageId = $message->getStageId();
  368. $groupId = $this->pairManager->getAloneGroup($uid, $stageId);
  369. $group = GroupModel::find($groupId);
  370. $message->setResponse($group);
  371. }
  372. public function operationalLog(OperationalLogMessage $message)
  373. {
  374. $uid = Auth::auth();
  375. $stageId = $message->getStageId();
  376. $type = $message->getType();
  377. $content = $message->getContent();
  378. OperationalLogModel::create([
  379. 'uid' => $uid,
  380. 'stage_id' => $stageId,
  381. 'type' => $type,
  382. 'content' => $content
  383. ]);
  384. }
  385. public function regrets(RegretsMessage $message)
  386. {
  387. $pairId = $message->getPairId();
  388. $ps = new \App\Services\Pair\PairService();
  389. $ps->goBackConfirm($pairId);
  390. }
  391. public function confirmCp(ConfirmCpMessage $message)
  392. {
  393. $uid = Auth::auth();
  394. $confirm = $message->getConfirm();
  395. $pairId = $message->getPairId();
  396. $pairService = new \App\Services\Pair\PairService();
  397. $pairService->confirmPair($pairId, $confirm);
  398. }
  399. }