DeedService.php 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484
  1. <?php
  2. namespace App\Services\Deed;
  3. use App\Exceptions\AlertException;
  4. use App\Exceptions\DBException;
  5. use App\Models\Deed\DeedCommentModel;
  6. use App\Models\Deed\DeedModel;
  7. use App\Models\Deed\InvitationCardModel;
  8. use App\Models\Fpdx\ActivityModel;
  9. use App\Models\Fpdx\PairModel;
  10. use App\Models\Goodnight\RoomMemberModel as GoodnightRoomMemberModel;
  11. use App\Models\Gteam\EnterModel;
  12. use App\Models\Gteam\RoomMemberModel as GameRoomMemberModel;
  13. use App\Models\User\UserModel;
  14. use App\Services\Goodnight\EnterService;
  15. use App\Services\Service;
  16. class DeedService extends Service
  17. {
  18. /**
  19. * 签订同意契约
  20. * @param int $uid
  21. * @param string $sign
  22. * @param int $deed_id
  23. * @return mixed
  24. * @throws AlertException
  25. * @throws DBException
  26. */
  27. public function signDeed(int $uid, string $sign, int $deed_id)
  28. {
  29. $deed = DeedModel::findOrFail($deed_id);
  30. if ($deed->state != 1) {
  31. throw new AlertException("契约已失效", 103);
  32. }
  33. \DB::beginTransaction();
  34. try {
  35. \DB::table('kdgx_fpdx_deed')->where('id', $deed_id)->update(['number' => date('YmdHis', time()) . uniqid()]);
  36. $tdeed_id = \DB::table('kdgx_fpdx_deed')->insertGetId([
  37. 'type' => $deed->type,
  38. 'uid' => $uid,
  39. 'sign' => $sign,
  40. 'ack' => $deed->ack,
  41. 'number' => $deed->number,
  42. 'state' => 1,
  43. ]);
  44. \DB::commit();
  45. return $tdeed_id;
  46. } catch (\Exception $e) {
  47. \DB::rollBack();
  48. throw new DBException($e->getMessage(), 501);
  49. }
  50. }
  51. /**
  52. * 创建契约
  53. * @param int $uid 签定契约的用户
  54. * @param array $signs 签名数组
  55. * @param string $type 活动类型 0:交换微信|1:72小时|2:开黑王者|3:开黑吃鸡
  56. * @param string|null $ack 活动处理标识,用于定位某人报名某期某活动
  57. * @param int $way 参加活动的方式 0:报名|1:邀请
  58. * @param int $way_id 方式关联字段 way为报名时=报名id|way为邀请时=邀请id
  59. * @return mixed
  60. * @throws AlertException
  61. */
  62. public function createDeed(int $uid, array $signs, string $type, $ack, int $way, int $way_id)
  63. {
  64. if (!in_array($way, [0, 1])) {
  65. throw new AlertException("参加活动的方式参数异常", 501);
  66. }
  67. $dm = new DeedModel();
  68. $deed = $dm->where([
  69. ['uid', $uid],
  70. ['way', $way],
  71. ['way_id', $way_id],
  72. ])->first();
  73. if (collect($deed)->isEmpty()) {
  74. $data = array(
  75. 'created_at' => time(),
  76. 'updated_at' => time(),
  77. 'uid' => $uid,
  78. 'type' => $type,
  79. 'ack' => $ack,
  80. 'sign' => $signs['sign'],
  81. 'sign_content' => $signs['sign_content'],
  82. 'state' => 1,
  83. 'way' => $way,
  84. 'way_id' => $way_id,
  85. );
  86. if ($way == 1) {
  87. $data['sign_at'] = time();
  88. $data['publish_at'] = time() + 86400;
  89. $data['confirm_at'] = time() + 86400;
  90. $data['end_at'] = time() + 86400;
  91. } else {
  92. switch ($type) {
  93. case 0:
  94. $data['sign_at'] = time();
  95. $data['publish_at'] = time() + 86400;
  96. $data['confirm_at'] = time() + 86400;
  97. $data['end_at'] = time() + 86400;
  98. break;
  99. case 1:
  100. $pair = PairModel::findOrFail($ack);
  101. $activity = ActivityModel::findOrFail($pair->stage_id);
  102. $data['sign_at'] = time();
  103. $data['publish_at'] = $activity->open_time;
  104. $data['confirm_at'] = $activity->rematched_at;
  105. $data['end_at'] = $activity->close_time;
  106. break;
  107. case 2:
  108. case 3:
  109. $enter = EnterModel::findOrFail($ack);
  110. $acty = $enter->activity;
  111. $data['sign_at'] = time();
  112. $data['publish_at'] = $acty->match_end_at;
  113. $data['confirm_at'] = $acty->match_end_at;
  114. $data['end_at'] = $acty->match_end_at + 3 * 3600;
  115. break;
  116. case 4:
  117. $es = new EnterService();
  118. $acty = $es->getActivity("报名");
  119. $data['sign_at'] = time();
  120. $data['publish_at'] = $acty->showed_at;
  121. $data['confirm_at'] = $acty->showed_at;
  122. $data['end_at'] = $acty->end_at;
  123. break;
  124. }
  125. }
  126. $deed_id = \DB::table("kdgx_fpdx_deed")->insertGetId($data);
  127. return $deed_id;
  128. }
  129. return $deed->id;
  130. }
  131. /**
  132. * 生成契约编号
  133. * @param int $type
  134. * @param int $ack
  135. * @param int $isack
  136. * @return string
  137. * @throws AlertException
  138. * @throws DBException
  139. */
  140. public function signTwoDeed(int $type, $ack, $isack)
  141. {
  142. $deed = DeedModel::where([['type', $type], ['ack', $ack]])->first();
  143. $isdeed = DeedModel::where([['type', $type], ['ack', $isack]])->first();
  144. if (collect($deed)->isEmpty() || collect($isdeed)->isEmpty()) {
  145. throw new AlertException("不存在的契约", 404);
  146. }
  147. if ($deed->type != $isdeed->type) {
  148. throw new AlertException("契约类型不同", 101);
  149. }
  150. if (!in_array($deed->state, [1, 2]) || !in_array($isdeed->state, [1, 2])) {
  151. throw new AlertException("契约已失效", 103);
  152. }
  153. $number = bin2hex(\openssl_random_pseudo_bytes(3)) . rand(1, 9);
  154. $deed->number = $number;
  155. $deed->state = 3;
  156. $isdeed->number = $number;
  157. $isdeed->state = 3;
  158. if ($deed->save() && $isdeed->save()) {
  159. return $number;
  160. }
  161. throw new DBException("数据库异常", 501);
  162. }
  163. /**
  164. * 生成契约编号
  165. * @param int $deed_id
  166. * @param int $isdeed_id
  167. * @return string
  168. * @throws AlertException
  169. * @throws DBException
  170. */
  171. public function signTwoDeedByid(int $deed_id, int $isdeed_id)
  172. {
  173. $deed = DeedModel::findOrFail($deed_id);
  174. $isdeed = DeedModel::findOrFail($isdeed_id);
  175. if ($deed->type != $isdeed->type) {
  176. throw new AlertException("契约类型不同", 101);
  177. }
  178. if (!in_array($deed->state, [1, 2]) || !in_array($isdeed->state, [1, 2])) {
  179. throw new AlertException("契约已失效", 103);
  180. }
  181. switch ($deed->type) {
  182. case 0:
  183. $deed->end_at = time() + 86400;
  184. $isdeed->end_at = time() + 86400;
  185. break;
  186. }
  187. $number = bin2hex(\openssl_random_pseudo_bytes(3)) . rand(1, 9);
  188. $deed->number = $number;
  189. $deed->state = 3;
  190. $isdeed->number = $number;
  191. $isdeed->state = 3;
  192. if ($deed->save() && $isdeed->save()) {
  193. return $number;
  194. }
  195. throw new DBException("数据库异常", 501);
  196. }
  197. /**
  198. * 获取契约信息
  199. * @param int $deed_id
  200. * @return \Illuminate\Database\Eloquent\Collection|\Illuminate\Database\Eloquent\Model
  201. */
  202. public function get(int $deed_id)
  203. {
  204. $deed = DeedModel::findOrFail($deed_id);
  205. if (!empty($deed->number)) {
  206. $other = DeedModel::where([['number', $deed->number], ['id', '!=', $deed_id]])->first();
  207. $ouser = UserModel::findOrFail(
  208. $other->uid,
  209. ['nickname', 'headimgurl', 'qq', 'weixin', 'partner_id', 'wx_auth', 'age', 'work_state']
  210. );
  211. $ouser->deed_cnt = DeedModel::where([
  212. ['uid', $other->uid],
  213. ['end_at', '<', time()],
  214. ])->whereNotNull('number')->count();
  215. $other->user = $ouser;
  216. $deed->other = $other;
  217. $deed->comment = DeedCommentModel::where([
  218. ['number', $deed->number],
  219. ['comment_uid', $other->uid],
  220. ])->first(['speed', 'level']) ?? null;
  221. }
  222. return $deed;
  223. }
  224. public function getByTUA(int $type, int $uid, $ack)
  225. {
  226. if (is_null($ack)) {
  227. throw new AlertException("没有找到契约", 401);
  228. }
  229. /** @var DeedModel|null $deed */
  230. $deed = DeedModel::where([
  231. ['type', $type],
  232. ['uid', $uid],
  233. ['ack', $ack],
  234. ])->first();
  235. if (collect($deed)->isEmpty()) {
  236. throw new AlertException("没有找到契约", 404);
  237. }
  238. if (!empty($deed->number)) {
  239. $other = DeedModel::where([['number', $deed->number], ['id', '!=', $deed->id]])->first();
  240. if (collect($other)->isEmpty()) {
  241. $other = null;
  242. } else {
  243. $ouser = UserModel::findOrFail($other->uid, ['nickname', 'headimgurl', 'qq', 'weixin', 'partner_id']);
  244. $ouser->deed_cnt = DeedModel::where('uid', $other->uid)->count();
  245. $other->user = $ouser;
  246. $deed->comment = DeedCommentModel::where([
  247. ['number', $deed->number],
  248. ['comment_uid', $other->uid],
  249. ])->first(['speed', 'level']) ?? null;
  250. }
  251. $deed->other = $other;
  252. }
  253. return $deed;
  254. }
  255. public function getByTUW(int $type, int $uid, $way, $way_id)
  256. {
  257. $deed = DeedModel::where([
  258. ['type', $type],
  259. ['uid', $uid],
  260. ['way', $way],
  261. ['way_id', $way_id],
  262. ])->first();
  263. if (collect($deed)->isEmpty()) {
  264. throw new AlertException("没有找到契约", 404);
  265. }
  266. if (!empty($deed->number)) {
  267. $other = DeedModel::where([['number', $deed->number], ['id', '!=', $deed->id]])->first();
  268. if (collect($other)->isEmpty()) {
  269. $other = null;
  270. } else {
  271. $ouser = UserModel::findOrFail($other->uid, ['nickname', 'headimgurl', 'qq', 'weixin', 'partner_id']);
  272. $ouser->deed_cnt = DeedModel::where('uid', $other->uid)->count();
  273. $other->user = $ouser;
  274. $deed->comment = DeedCommentModel::where([
  275. ['number', $deed->number],
  276. ['comment_uid', $other->uid],
  277. ])->first(['speed', 'level']) ?? null;
  278. }
  279. $deed->other = $other;
  280. }
  281. return $deed;
  282. }
  283. /**
  284. * 进行中的契约
  285. * @param int $uid
  286. * @param array $pages
  287. * @return array
  288. */
  289. public function ings(int $uid, array $pages)
  290. {
  291. $total = DeedModel::where([['uid', $uid], ['end_at', '>', time()]])->whereIn('state', [0, 1, 2, 3])->count();
  292. $deeds = DeedModel::where([['uid', $uid], ['end_at', '>', time()]])->whereIn('state', [0, 1, 2, 3])->get();
  293. foreach ($deeds as &$deed) {
  294. try {
  295. switch ($deed->type) {
  296. case 0:
  297. $invita = InvitationCardModel::findOrFail($deed->way_id);
  298. if ($invita->uid == $uid) {
  299. $duid = $invita->invite_uid;
  300. } else {
  301. $duid = $invita->uid;
  302. }
  303. $user = UserModel::findOrFail(
  304. $duid,
  305. ['uid', 'headimgurl', 'nickname', 'sex', 'qq', 'weixin', 'partner_id']
  306. );
  307. $user->deed_cnt = DeedModel::where('uid', $user->uid)->count();
  308. $deed->user = $user;
  309. break;
  310. case 1:
  311. $user = array(
  312. 'headimgurl' => "https://oss.pocketuniversity.cn/media/2018-12-03/5c04a96d88148.png",
  313. );
  314. $pair = PairModel::findOrFail($deed->ack);
  315. if (!empty($pair->assoc_id)) {
  316. $assoc = PairModel::findOrFail($pair->assoc_id);
  317. $user = UserModel::findOrFail(
  318. $assoc->uid,
  319. ['uid', 'headimgurl', 'nickname', 'sex', 'qq', 'weixin', 'partner_id']
  320. );
  321. $user->deed_cnt = DeedModel::where('uid', $user->uid)->count();
  322. }
  323. $deed->user = $user;
  324. break;
  325. case 2:
  326. case 3:
  327. $duid = 0;
  328. if (!empty($deed->number)) {
  329. $enter = EnterModel::findOrFail($deed->ack);
  330. $member = GameRoomMemberModel::where([
  331. ['room_id', $enter->room_id],
  332. ['uid', '!=', $uid],
  333. ])->firstOrFail();
  334. $duid = $member->uid;
  335. } else {
  336. if ($deed->way == 1) {
  337. $invita = InvitationCardModel::findOrFail($deed->way_id);
  338. if ($invita->uid == $uid) {
  339. $duid = $invita->invite_uid;
  340. } else {
  341. $duid = $invita->uid;
  342. }
  343. }
  344. }
  345. if ($duid == 0) {
  346. if ($deed->type == 2) {
  347. $user = array(
  348. 'headimgurl' => "https://oss.pocketuniversity.cn/media/2018-12-10/5c0e5153b3cbe.png",
  349. );
  350. } else {
  351. $user = array(
  352. 'headimgurl' => "https://oss.pocketuniversity.cn/media/2018-12-10/5c0e515249448.png",
  353. );
  354. }
  355. } else {
  356. $user = UserModel::findOrFail(
  357. $duid,
  358. ['uid', 'headimgurl', 'nickname', 'sex', 'qq', 'weixin']
  359. );
  360. $user->deed_cnt = DeedModel::where('uid', $user->uid)->count();
  361. }
  362. $deed->user = $user;
  363. break;
  364. case 4:
  365. $duid = 0;
  366. if (!empty($deed->number)) {
  367. $enter = \App\Models\Goodnight\EnterModel::findOrFail($deed->ack);
  368. $member = GoodnightRoomMemberModel::where([
  369. ['room_id', $enter->room_id],
  370. ['uid', '!=', $uid],
  371. ])->firstOrFail();
  372. $duid = $member->uid;
  373. } else {
  374. if ($deed->way == 1) {
  375. $invita = InvitationCardModel::findOrFail($deed->way_id);
  376. if ($invita->uid == $uid) {
  377. $duid = $invita->invite_uid;
  378. } else {
  379. $duid = $invita->uid;
  380. }
  381. }
  382. }
  383. if ($duid == 0) {
  384. $user = array(
  385. 'headimgurl' => "https://oss.pocketuniversity.cn/media/2018-12-03/5c04a96d8b26b.png",
  386. );
  387. } else {
  388. $user = UserModel::findOrFail(
  389. $duid,
  390. ['uid', 'headimgurl', 'nickname', 'sex', 'qq', 'weixin']
  391. );
  392. $user->deed_cnt = DeedModel::where('uid', $user->uid)->count();
  393. }
  394. $deed->user = $user;
  395. break;
  396. }
  397. } catch (\Exception $e) {
  398. app('sentry')->captureException($e);
  399. continue;
  400. }
  401. }
  402. return [
  403. 'total' => $total,
  404. 'page' => $pages['page'],
  405. 'limit' => $pages['limit'],
  406. 'list' => $deeds,
  407. ];
  408. }
  409. /**
  410. * 已完成的契约
  411. * @param int $uid
  412. * @param array $pages
  413. * @return array
  414. */
  415. public function succs(int $uid, array $pages)
  416. {
  417. $total = DeedModel::where('uid', $uid)->where(function ($query) {
  418. $query->whereIn('state', [-1, 4, 5])->orWhere('end_at', '<', time());
  419. $query->whereIn('state', [-1, 4, 5])->orWhere('end_at', '<', time());
  420. $query->whereIn('state', [-1, 4, 5])->orWhere('end_at', '<', time());
  421. })->count();
  422. $deeds = DeedModel::where('uid', $uid)->where(function ($query) {
  423. $query->whereIn('state', [-1, 4, 5])->orWhere('end_at', '<', time());
  424. })->skip(($pages['page'] - 1) * $pages['limit'])->take($pages['limit'])->get();
  425. foreach ($deeds as &$deed) {
  426. /** @var DeedModel $deed */
  427. if (!empty($deed->number)) {
  428. $other = DeedModel::where([
  429. ['number', $deed->number],
  430. ['id', '!=', $deed->id],
  431. ])->first();
  432. /** @var UserModel $ouser */
  433. $ouser = UserModel::findOrFail(
  434. $other->uid,
  435. ['uid', 'nickname', 'headimgurl', 'qq', 'weixin', 'partner_id']
  436. );
  437. $ouser->deed_cnt = DeedModel::where('uid', $other->uid)->count();
  438. $deed->user = $ouser;
  439. $deed->comment = DeedCommentModel::where([
  440. ['number', $deed->number],
  441. ['comment_uid', $ouser->uid],
  442. ])->first(['speed', 'level']) ?? null;
  443. }
  444. switch ($deed->type) {
  445. case 0:
  446. case 1:
  447. case 2:
  448. case 3:
  449. case 4:
  450. break;
  451. }
  452. }
  453. return [
  454. 'total' => $total,
  455. 'page' => $pages['page'],
  456. 'limit' => $pages['limit'],
  457. 'list' => $deeds,
  458. ];
  459. }
  460. }