PairService.php 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765
  1. <?php
  2. namespace App\Services\Pair;
  3. use App\Exceptions\AlertException;
  4. use App\Exceptions\DBException;
  5. use App\Http\Controllers\Core\Auth;
  6. use App\Models\Deed\DeedModel;
  7. use App\Models\Fpdx\ActivityModel;
  8. use App\Models\Fpdx\GroupModel;
  9. use App\Models\Fpdx\PairModel;
  10. use App\Models\Fpdx\RoomModel;
  11. use App\Models\Fpdx\WxkfModel;
  12. use App\Models\TicketModel;
  13. use App\Models\User\AuthKey;
  14. use App\Models\User\UserModel;
  15. use App\Services\App\KfService;
  16. use App\Services\App\ProfileService;
  17. use App\Services\Pay\OrderService;
  18. use App\Services\Service;
  19. use Illuminate\Support\Facades\DB;
  20. use Illuminate\Support\Facades\Redis;
  21. class PairService extends Service
  22. {
  23. /**
  24. * 通过小花提升成功率
  25. * @param int $pair_id
  26. * @param array $flowers
  27. * @return bool
  28. * @throws AlertException
  29. * @throws DBException
  30. */
  31. public function addScoreByFlower(int $pair_id, array $flowers)
  32. {
  33. $pair = PairModel::findOrFail($pair_id);
  34. if ($pair->state > 200) {
  35. throw new AlertException("报名时间已结束", 402);
  36. }
  37. $user = UserModel::findOrFail($pair->uid);
  38. if (
  39. $user->red_flower < $flowers['red_flower']
  40. || $user->gold_flower < $flowers['gold_flower']
  41. ) {
  42. throw new AlertException("余额不足", 403);
  43. }
  44. $flower = $flowers['red_flower'] + $flowers['gold_flower'];
  45. if ($flower * 10 + $pair->score > 90) {
  46. throw new AlertException("超过上限", 404);
  47. }
  48. $pair->score = $pair->score + $flower * 10;
  49. if ($pair->score >= 60 && $pair->state < 100) {
  50. $pair->state = $pair->state + 100;
  51. // 通知
  52. try {
  53. // 系统消息
  54. $notice = new \App\Services\Pair\NoticeService();
  55. $notice->inviteToSuccess($pair->uid);
  56. } catch (\Exception $e) {
  57. app('sentry')->captureException($e);
  58. }
  59. }
  60. if ($pair->save()) {
  61. UserModel::where('uid', $pair->uid)->update([
  62. 'red_flower' => $user->red_flower - $flowers['red_flower'],
  63. 'gold_flower' => $user->gold_flower - $flowers['gold_flower'],
  64. ]);
  65. \DB::table('kdgx_partner_charge_pay_logs')->insert([
  66. 'uid' => $pair->uid,
  67. 'create_at' => time(),
  68. 'type' => 13,
  69. 'red_flower' => 0 - $flowers['red_flower'],
  70. 'gold_flower' => 0 - $flowers['gold_flower'],
  71. 'remark' => "72小时提升匹配成功率",
  72. ]);
  73. return true;
  74. } else {
  75. throw new DBException();
  76. }
  77. }
  78. /**
  79. * 通过超级会员卡提升成功率
  80. * @param int $pair_id
  81. * @return bool
  82. * @throws AlertException
  83. */
  84. public function addScoreBySupvip(int $pair_id)
  85. {
  86. $pair = PairModel::findOrFail($pair_id);
  87. if ($pair->state > 200) {
  88. throw new AlertException("报名时间已结束", 402);
  89. }
  90. $ticket = TicketModel::where(array(
  91. ['uid', $pair->uid],
  92. ['validity_time', '>', time()],
  93. ['type', 3],
  94. ['state', 0],
  95. ))->orderBy('validity_time', 'asc')->first();
  96. if (collect($ticket)->isEmpty()) {
  97. throw new AlertException("本月已经没有次数了");
  98. }
  99. if ($pair->score >= 92) {
  100. throw new AlertException("超过上限", 404);
  101. }
  102. $ticket->state = 1;
  103. $ticket->save();
  104. if ($pair->state < 100) {
  105. $pair->state = $pair->state + 100;
  106. // 通知
  107. try {
  108. $notice = new \App\Services\Pair\NoticeService();
  109. $notice->inviteToSuccess($pair->uid);
  110. } catch (\Exception $e) {
  111. }
  112. }
  113. $pair->score = 92;
  114. $pair->save();
  115. return true;
  116. }
  117. /**
  118. * 不/同意匹配结果
  119. * @param int $pair_id 报名记录
  120. * @param int $confirm 1同意;-1不同意
  121. * @return bool
  122. * @throws AlertException
  123. * @throws DBException
  124. */
  125. public function confirmPair(int $pair_id, int $confirm)
  126. {
  127. $pair = PairModel::findOrFail($pair_id);
  128. $uid = Auth::auth();
  129. if ($pair->uid != $uid) {
  130. throw new AlertException("无权限", 101);
  131. }
  132. if (
  133. $pair->state < 400
  134. || $pair->state > 499
  135. || empty($pair->assoc_id)
  136. || $pair->room_id > 0
  137. ) {
  138. throw new AlertException("不在活动签到时间!");
  139. }
  140. $activity = ActivityModel::findOrFail($pair->stage_id);
  141. if (time() > $activity->activity_time + 3600 * 10) {
  142. throw new AlertException("不在活动签到时间");
  143. }
  144. if ($pair->confirm != 0) {
  145. throw new AlertException("恭喜!CP关系确认成功,匹配对象确认CP关系后将会解锁活动群", 102);
  146. }
  147. switch ($confirm) {
  148. case 1:
  149. $other = PairModel::findOrFail($pair->assoc_id);
  150. $pair->confirm = time();
  151. if (0 < $other->confirm) {
  152. DB::beginTransaction();
  153. try {
  154. $room = RoomModel::create([
  155. 'stage_id' => $pair->stage_id,
  156. 'member' => "{$pair->uid},{$other->uid}",
  157. 'create_time' => time(),
  158. 'type' => 1,
  159. ]);
  160. $pair->room_id = $room->room_id;
  161. $other->room_id = $room->room_id;
  162. $pair->save() && $other->save();
  163. DB::commit();
  164. } catch (\Exception $exception) {
  165. DB::rollBack();
  166. throw new DBException($exception->getMessage());
  167. }
  168. } else {
  169. $pair->save();
  170. }
  171. break;
  172. case -1:
  173. $pair->confirm = 0 - time();
  174. $pair->save();
  175. break;
  176. default:
  177. throw new AlertException("错误的操作逻辑");
  178. }
  179. // 发送通知
  180. try {
  181. $other = PairModel::findOrFail($pair->assoc_id);
  182. $noticeService = new NoticeService();
  183. // pair同意 => 单方确认(通知对方)
  184. if (0 < $pair->confirm && $other->confirm == 0) {
  185. $noticeService->unilateralConfirm($other->uid, $other->id);
  186. }
  187. // pair不同意 => 单方不同意(通知对方)
  188. if (0 > $pair->confirm && $other->confirm == 0) {
  189. $noticeService->unilateralAgain($other->uid, $other->id);
  190. }
  191. // pair同意;other同意 => 双方同意 (通知双方)
  192. if (0 < $pair->confirm && 0 < $other->confirm) {
  193. $noticeService->unlockGroup($other->uid, $other->id);
  194. $noticeService->unlockGroup($pair->uid, $pair->id);
  195. }
  196. } catch (\Exception $exception) {
  197. }
  198. return true;
  199. }
  200. /**
  201. * 匹配成功-72-签到反悔
  202. * @param int $pair_id
  203. * @return bool
  204. * @throws AlertException
  205. */
  206. public function goBackConfirm(int $pair_id)
  207. {
  208. $pair = PairModel::findOrFail($pair_id);
  209. $uid = Auth::auth();
  210. if ($pair->uid != $uid) {
  211. throw new AlertException("无权限");
  212. }
  213. if (0 == $pair->confirm || abs($pair->confirm) + 3600 <= time()) {
  214. throw new AlertException("当前不可反悔");
  215. }
  216. $activity = ActivityModel::findOrFail($pair->stage_id);
  217. if (time() >= $activity->activity_time + 3600 * 10) {
  218. throw new AlertException("当前时间不可反悔");
  219. }
  220. if ($pair->goback_at > 0) {
  221. throw new AlertException("反悔次数已用完");
  222. }
  223. $pair->confirm = 0;
  224. $pair->goback_at = time();
  225. if ($pair->room_id > 0) {
  226. try {
  227. DB::beginTransaction();
  228. $pair->save();
  229. RoomModel::where('room_id', $pair->room_id)->delete();
  230. PairModel::where('room_id', $pair->room_id)->update(['room_id' => 0]);
  231. DB::commit();
  232. } catch (\Exception $exception) {
  233. DB::rollBack();
  234. throw $exception;
  235. }
  236. } else {
  237. $pair->save();
  238. }
  239. Redis::sadd("fpdx:pair:{$pair->stage_id}:gobackconfirm", $pair->uid);
  240. // 通知
  241. try {
  242. $other = PairModel::findOrFail($pair->assoc_id);
  243. $noticeService = new NoticeService();
  244. $noticeService->regretConfirm($other->uid);
  245. } catch (\Exception $exception) {
  246. }
  247. return true;
  248. }
  249. /**
  250. * 获取某人最近一期的状态
  251. * @param int $uid
  252. * @param int $stage_id
  253. * @return array
  254. */
  255. public function getActivityInfo(int $uid, int $stage_id)
  256. {
  257. $next = PairModel::where([
  258. ['uid', $uid],
  259. ['stage_id', $stage_id],
  260. ])->first();
  261. if (collect($next)->isEmpty()) {
  262. return array();
  263. } else {
  264. $result = $next->toArray();
  265. $activity = ActivityModel::find($next->stage_id, [
  266. 'stage_id',
  267. 'activity_name',
  268. 'activity_time',
  269. 'close_time',
  270. 'signbegin_time',
  271. 'signend_time',
  272. 'open_time',
  273. 'rematch_begin_at',
  274. 'rematched_at',
  275. ]);
  276. $result['activity'] = $activity;
  277. $result = array_merge($result, $activity->toArray());
  278. $result['pair_info'] = array();
  279. $user = UserModel::find($uid);
  280. try {
  281. $progress = $next->progress();
  282. $result['progress'] = $progress;
  283. } catch (\Exception $e) {
  284. app('sentry')->captureException($e);
  285. }
  286. $result['pair_info']['user_info'] = array_merge($next->toArray(), $user->toArray());
  287. if (empty($result['assoc_id'])) {
  288. return $result;
  289. }
  290. $result['pair_info']['room_info'] = RoomModel::find($result['room_id']) ?? [];
  291. $result['pair_info']['group'] = GroupModel::find($result['group_id']);
  292. $partner_pair = PairModel::find($result['assoc_id']);
  293. $partner_user = UserModel::find($partner_pair->uid);
  294. $result['pair_info']['partner_info'] = array_merge($partner_pair->toArray(), $partner_user->toArray());
  295. return $result;
  296. }
  297. }
  298. // 获取某人最近一期的状态
  299. public function getRecentState(int $uid)
  300. {
  301. $next = PairModel::where('uid', $uid)->OrderBy('stage_id', 'desc')->first();
  302. if (collect($next)->isEmpty()) {
  303. return [];
  304. } else {
  305. $result = $next->toArray();
  306. $activity = ActivityModel::findOrFail($next->stage_id, [
  307. 'activity_name',
  308. 'activity_time',
  309. 'close_time',
  310. 'signbegin_time',
  311. 'signend_time',
  312. 'open_time',
  313. 'rematched_at',
  314. 'qbj_stage_id',
  315. ]);
  316. $result = array_merge($result, $activity->toArray());
  317. try {
  318. $progress = $next->progress();
  319. $result['progress'] = $progress;
  320. } catch (\Exception $e) {
  321. app('sentry')->captureException($e);
  322. }
  323. $result['pair_info'] = array();
  324. $result['activity'] = $activity;
  325. if (empty($result['assoc_id'])) {
  326. return $result;
  327. }
  328. $result['pair_info']['room_info'] = RoomModel::find($result['room_id']) ?? [];
  329. $partner_pair = PairModel::findOrFail($result['assoc_id']);
  330. $partner_user = UserModel::findOrFail($partner_pair->uid);
  331. $result['pair_info']['partner_info'] = array_merge($partner_pair->toArray(), $partner_user->toArray());
  332. $user = UserModel::findOrFail($uid);
  333. $result['pair_info']['user_info'] = array_merge($next->toArray(), $user->toArray());
  334. return $result;
  335. }
  336. }
  337. /**
  338. * 结算本期|报名下一期
  339. * @param int $stageId
  340. * @param int $uid
  341. * @param array $enroll
  342. * @param string $os
  343. * @return int
  344. * @throws AlertException
  345. */
  346. public function enrollNext(int $stageId, int $uid, array $enroll, string $os)
  347. {
  348. $data = PairModel::where([
  349. ['uid', $uid],
  350. ['stage_id', $stageId],
  351. ])->first();
  352. if (collect($data)->isEmpty() || 'qbj' == $data->activity_type) {
  353. throw new AlertException("错误的期数", 103);
  354. }
  355. $isture = false; // 是否自动报名下一期: 否
  356. $canenroll = false; // 是否结算: 否
  357. // 匹配失败
  358. if ($data->state >= 300 && $data->state < 400) {
  359. $isture = true; // 是否结算:是
  360. $canenroll = true; // 自动报名下一期:是
  361. $state = 700 + intval($data->state) % 100; // 本期状态 => 70x
  362. $next_state = 100 + intval($data->state) % 100; // 自动报名成功
  363. $order_id = $data->order_id; // 携带报名费用
  364. $pay = $data->pay; // 携带报名费用
  365. $score = $data->score; // 携带报名分数
  366. $add_score = $data->add_score; // 携带报名分数
  367. }
  368. // 不满意
  369. if ($data->state >= 500 && $data->state < 600) {
  370. $isture = true; // 是否结算:是
  371. $canenroll = true; // 自动报名下一期:是
  372. $state = 700 + intval($data->state) % 100; // 本期状态 => 70x
  373. $next_state = 100 + intval($data->state) % 100; // 自动报名成功
  374. $order_id = $data->order_id; // 携带报名费用
  375. $pay = $data->pay; // 携带报名费用
  376. $score = $data->score; // 携带报名分数
  377. $add_score = $data->add_score;
  378. }
  379. // 报名失败
  380. if ($data->state >= 900 && $data->state < 999) {
  381. $isture = true; // 是否结算:是
  382. $canenroll = false; // 自动报名下一期:否
  383. $state = 1000 + intval($data->state) % 100;// 本期状态 => 100x
  384. $next_state = 0; // 自动报名成功: 否
  385. $order_id = 0; // 携带报名费用: 否
  386. $pay = 0;
  387. $score = 0; // 携带报名分数: 否
  388. $add_score = $data->add_score;
  389. }
  390. // 自己未操作对方重配
  391. if ($data->state >= 800 && $data->state < 900 && $data->confirm == 0 && empty($data->assoc_id)) {
  392. $isture = true; // 是否结算:是
  393. $canenroll = false; // 自动报名下一期: 否
  394. $state = 700 + intval($data->state) % 100; // 本期状态 => 70x
  395. $next_state = 0; // 自动报名成功: 否
  396. $order_id = 0; // 携带报名费用: 否
  397. $pay = 0;
  398. $score = 0; // 携带报名分数: 否
  399. $add_score = $data->add_score;
  400. }
  401. // 重配失败
  402. if (
  403. (($data->state >= 400
  404. && $data->state < 500)
  405. || ($data->state >= 800
  406. && $data->state < 900))
  407. && empty($data->assoc_id)
  408. && $data->last_assoc > 0
  409. && $data->confirm != 0
  410. ) {
  411. $isture = true; // 是否结算:是
  412. $canenroll = true; // 自动报名下一期:是
  413. $state = 700 + intval($data->state) % 100; // 本期状态 => 70x
  414. $next_state = 100 + intval($data->state) % 100; // 自动报名成功: 是
  415. $order_id = 0; // 携带报名费用: 否
  416. $pay = 0;
  417. $score = $data->score; // 携带报名分数: 是
  418. $add_score = $data->add_score;
  419. }
  420. if (!$isture) {
  421. throw new AlertException("不可报名下一期", 100);
  422. }
  423. $pair_id = 0;
  424. if ($canenroll) {
  425. $ings = ActivityModel::getActivitys();
  426. $next = PairModel::where([
  427. ['uid', $uid],
  428. ['stage_id', $ings['next']],
  429. ])->first();
  430. if (!collect($next)->isEmpty()) {
  431. throw new AlertException("你已报名下一期", 100);
  432. }
  433. $user = UserModel::findOrFail($uid);
  434. if (!in_array($user->sex, [1, 2])) {
  435. throw new AlertException("请补全性别", 100);
  436. }
  437. $tmp = [
  438. 'uid' => $data->uid,
  439. 'stage_id' => $ings['next'],
  440. 'state' => $next_state,
  441. 'order_id' => $order_id,
  442. 'pay' => $pay,
  443. 'score' => $score,
  444. 'add_score' => $add_score,
  445. 'data' => $os,
  446. 'sex' => $user->sex,
  447. 'create_time' => time(),
  448. 'media_id' => 'gh_b598cb7474d8',
  449. ];
  450. $pair = array_merge($tmp, $enroll);
  451. DB::beginTransaction();
  452. try {
  453. $save = array(
  454. 'state' => $state,
  455. );
  456. if ($order_id > 0) {
  457. $save['pay'] = 0;
  458. }
  459. DB::table("kdgx_partner_charge_pair")->where('id', $data->id)->update($save);
  460. $pair_id = DB::table("kdgx_partner_charge_pair")->insertGetId($pair);
  461. DB::commit();
  462. try {
  463. // 系统消息
  464. $notice = new \App\Services\Pair\NoticeService();
  465. $notice->inviteToSuccess($pair->uid);
  466. } catch (\Exception $e) {
  467. }
  468. } catch (\Exception $exception) {
  469. DB::rollBack();
  470. throw $exception;
  471. }
  472. } else {
  473. DB::table("kdgx_partner_charge_pair")->where('id', $data->id)->update(['state' => $state]);
  474. }
  475. return $pair_id;
  476. }
  477. /**
  478. * 免费报名
  479. * @param int $uid
  480. * @param int $stage_id
  481. * @param array $data
  482. * @return mixed
  483. * @throws AlertException
  484. */
  485. public function freeEnroll(int $uid, int $stage_id, array $data)
  486. {
  487. $pm = new PairModel();
  488. if ($pm->where([['uid', $uid], ['stage_id', $stage_id]])->exists()) {
  489. throw new AlertException("你已经报名了本期活动", 101);
  490. }
  491. $user = UserModel::findOrFail($uid);
  492. if (!in_array($user->sex, [1, 2])) {
  493. throw new AlertException("请补全性别", 102);
  494. }
  495. if ($user->sex == 2 && $data['state'] == 3) {
  496. $data['state'] = 103;
  497. }
  498. $data = array_merge(
  499. ['uid' => $uid, 'stage_id' => $stage_id, 'sex' => $user->sex, 'create_time' => time()],
  500. $data
  501. );
  502. DB::beginTransaction();
  503. try {
  504. $pair_id = DB::table('kdgx_partner_charge_pair')->insertGetId($data);
  505. DB::commit();
  506. if ($data['state'] > 100) {
  507. // 通知
  508. try {
  509. // 系统消息
  510. $notice = new \App\Services\Pair\NoticeService();
  511. $notice->inviteToSuccess($uid);
  512. } catch (\Exception $e) {
  513. app('sentry')->captureException($e);
  514. }
  515. }
  516. return $pair_id;
  517. } catch (\Exception $e) {
  518. DB::rollBack();
  519. throw $e;
  520. }
  521. }
  522. /**
  523. * 超级会员报名
  524. * @param int $uid
  525. * @param int $stage_id
  526. * @param array $data
  527. * @return int
  528. * @throws AlertException
  529. */
  530. public function supvipEnroll(int $uid, int $stage_id, array $data)
  531. {
  532. $pm = new PairModel();
  533. if ($pm->where([['uid', $uid], ['stage_id', $stage_id]])->exists()) {
  534. throw new AlertException("你已经报名了本期活动", 101);
  535. }
  536. $user = UserModel::findOrFail($uid);
  537. if (!in_array($user->sex, [1, 2])) {
  538. throw new AlertException("请补全性别", 102);
  539. }
  540. $ticket = TicketModel::where(array(
  541. ['uid', $uid],
  542. ['validity_time', '>', time()],
  543. ['type', 3],
  544. ['state', 0],
  545. ))->orderBy('validity_time', 'asc')->first();
  546. if (collect($ticket)->isEmpty()) {
  547. throw new AlertException("本月已经没有次数了");
  548. }
  549. $data = array_merge(
  550. ['uid' => $uid, 'stage_id' => $stage_id, 'sex' => $user->sex, 'create_time' => time()],
  551. $data
  552. );
  553. DB::beginTransaction();
  554. try {
  555. $pair_id = DB::table('kdgx_partner_charge_pair')->insertGetId($data);
  556. $ticket->state = 1;
  557. $ticket->save();
  558. DB::commit();
  559. // 通知
  560. try {
  561. // 系统消息
  562. $notice = new \App\Services\Pair\NoticeService();
  563. $notice->inviteToSuccess($uid);
  564. } catch (\Exception $e) {
  565. app('sentry')->captureException($e);
  566. }
  567. return $pair_id;
  568. } catch (\Exception $exception) {
  569. DB::rollBack();
  570. throw $exception;
  571. }
  572. }
  573. /**
  574. * 获取某人是否可以使用微信kf
  575. * @param int $uid
  576. * @return bool
  577. * @throws \App\Exceptions\ApiException
  578. */
  579. public function isopenwxkf(int $uid): bool
  580. {
  581. $pf = new ProfileService();
  582. $js = $pf->get('fpdx_open_status');
  583. if (empty($js)) {
  584. return false;
  585. } else {
  586. $conf = json_decode($js, true);
  587. if (!isset($conf['fpdx_zskf']) || $conf['fpdx_zskf'] == 0) {
  588. return false;
  589. }
  590. }
  591. $user = UserModel::findOrFail($uid);
  592. $wxm = new WxkfModel();
  593. if (empty($user->wxkf)) {
  594. $kfs = new KfService();
  595. $wxid = $kfs->hashWxkf($user->uid);
  596. if ($wxid) {
  597. return true;
  598. }
  599. return false;
  600. } else {
  601. if (
  602. $auth = AuthKey::where([
  603. ['uid', $user->uid],
  604. ['auth_type', 'wxid'],
  605. ])->exists()
  606. ) {
  607. return false;
  608. }
  609. $wxkf = $wxm->where('wxid', $user->wxkf)->first();
  610. if (collect($wxkf)->isEmpty()) {
  611. return false;
  612. }
  613. if ("活动" == $wxkf->type && $wxkf->state == 1) {
  614. return true;
  615. } else {
  616. return false;
  617. }
  618. }
  619. }
  620. public function keepRoom($uid)
  621. {
  622. $keep_rooms = Redis::smembers("keep_rooms");
  623. $room_id = PairModel::where('uid', $uid)->whereIn("room_id", $keep_rooms)->value('room_id');
  624. return $room_id;
  625. }
  626. /**
  627. * 保持CP关系
  628. * @param $uid
  629. * @param $room_id
  630. * @throws AlertException
  631. */
  632. public function keep($uid, $room_id)
  633. {
  634. // 判断有没有保持CP的报名信息
  635. $pair = PairModel::where(['uid' => $uid, 'keep' => 1])->first();
  636. if ($pair) {
  637. //
  638. if (RoomModel::where('room_id', $pair->room_id)->where('keep', 1)->first()) {
  639. throw new AlertException("只能跟一个CP保持关系", 403);
  640. } else {
  641. $this->unKeep($uid, $pair->room_id);
  642. }
  643. }
  644. $pair = PairModel::where(['uid' => $uid, 'room_id' => $room_id])->first();
  645. if (!$pair) {
  646. throw new AlertException("不存在", 404);
  647. }
  648. $activity = ActivityModel::findOrFail($pair->stage_id);
  649. if ($activity->open_time > time() && $activity->close_time <= time()) {
  650. throw new AlertException("只能在活动期间保持关系", 410);
  651. }
  652. $pair->keep = 1;
  653. $pair->save();
  654. $other = PairModel::find($pair->assoc_id);
  655. if ($other->keep == 1) {
  656. Redis::sadd("keep_rooms", $room_id);
  657. }
  658. }
  659. /**
  660. * 取消关系
  661. * @param $uid
  662. * @param $room_id
  663. */
  664. public function unKeep($uid, $room_id)
  665. {
  666. $pair = PairModel::where(['uid' => $uid, 'room_id' => $room_id])->first();
  667. $pair->keep = 0;
  668. $pair->save();
  669. Redis::srem("keep_rooms", $room_id);
  670. }
  671. /**
  672. * 删除报名纪录
  673. * @param $uid
  674. * @return bool
  675. * @throws AlertException
  676. * @throws \App\Exceptions\ApiException
  677. */
  678. public function deletePair($uid)
  679. {
  680. // 检验当前活动
  681. $stage_id = ActivityModel::where([
  682. ['signend_time', '<', time()],
  683. ['close_time', '>', time()],
  684. ])->value('stage_id');
  685. if (PairModel::where('uid', $uid)->where('stage_id', $stage_id)->where('state', '<', 600)->first()) {
  686. throw new AlertException("正在活动中不能删除报名信息", 409);
  687. }
  688. // 检验最近一期活动
  689. $stages = ActivityModel::getActivitys();
  690. $pair = PairModel::where([
  691. ['uid', $uid],
  692. ['stage_id', $stages['next']],
  693. ])->whereBetween('state', [0, 299])->whereNull('assoc_id')->first();
  694. if (collect($pair)->isEmpty()) {
  695. return true;
  696. // throw new AlertException('未能自动索引到报名纪录,请联系管理员手动处理(每周三的下午12:30-16:00[视情况调整时间]为预匹配时间,请稍后重试)',204);
  697. }
  698. // 退款
  699. if ($pair->order_id > 0 && $pair->pay > 0) {
  700. $orderService = new OrderService();
  701. $orderService->refund($pair->order_id, $pair->pay * 100, "72小时CP退款", true);
  702. }
  703. PairModel::where('id', $pair->id)->delete();
  704. DeedModel::where([
  705. ['type', 1],
  706. ['uid', $pair->uid],
  707. ['ack', $pair->id],
  708. ])->delete();
  709. return true;
  710. }
  711. }