MeetingService.php 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512
  1. <?php
  2. // +----------------------------------------------------------------------
  3. // | LARAVEL8.0 框架 [ LARAVEL ][ RXThinkCMF ]
  4. // +----------------------------------------------------------------------
  5. // | 版权所有 2017~2021 LARAVEL研发中心
  6. // +----------------------------------------------------------------------
  7. // | 官方网站: http://www.laravel.cn
  8. // +----------------------------------------------------------------------
  9. // | Author: laravel开发员 <laravel.qq.com>
  10. // +----------------------------------------------------------------------
  11. namespace App\Services\Api;
  12. use App\Models\CityModel;
  13. use App\Models\MeetingModel;
  14. use App\Models\MeetingRecordsModel;
  15. use App\Models\SupervisorsModel;
  16. use App\Models\UserModel;
  17. use App\Services\BaseService;
  18. use App\Services\MpService;
  19. use App\Services\RedisService;
  20. /**
  21. * 会议-服务类
  22. * @author laravel开发员
  23. * @since 2020/11/11
  24. * @package App\Services\Api
  25. */
  26. class MeetingService extends BaseService
  27. {
  28. // 静态对象
  29. protected static $instance = null;
  30. /**
  31. * 构造函数
  32. * @author laravel开发员
  33. * @since 2020/11/11
  34. */
  35. public function __construct()
  36. {
  37. $this->model = new MeetingModel();
  38. }
  39. /**
  40. * 静态入口
  41. */
  42. public static function make()
  43. {
  44. if (!self::$instance) {
  45. self::$instance = new static();
  46. }
  47. return self::$instance;
  48. }
  49. /**
  50. * 信息
  51. * @param int $id
  52. * @param int $userId
  53. * @return array|mixed
  54. */
  55. public function getInfo($id, $userId = 0, $type = 1)
  56. {
  57. $cacheKey = "caches:meeting:info_{$id}_{$userId}_{$type}";
  58. $info = RedisService::get($cacheKey);
  59. if ($info) {
  60. return $info;
  61. }
  62. $info = $this->model->with(['member', 'company'])
  63. ->where(['id' => $id, 'status' => 1, 'mark' => 1])
  64. ->first();
  65. $info = $info ? $info->toArray() : [];
  66. if ($info) {
  67. $info['fee'] = floatval($info['fee']);
  68. if ($type == 2) {
  69. $info['qrcode'] = MpService::make()->getMiniQrcode('pagesSub/pages/meeting/books', $id);
  70. $info['qrcode'] = $info['qrcode'] ? get_image_url($info['qrcode']) : '';
  71. } else if ($type == 3) {
  72. $bookInfo = MeetingRecordsModel::where(['user_id' => $userId, 'meeting_id' => $id, 'mark' => 1])
  73. ->select(['id', 'is_sign', 'money', 'is_claim', 'status'])
  74. ->first();
  75. $bookInfo = $bookInfo ? $bookInfo->toArray() : [];
  76. $info['is_book'] = $bookInfo ? 1 : 0;
  77. $info['is_sign'] = isset($bookInfo['is_sign']) ? $bookInfo['is_sign'] : 0;
  78. $info['is_claim'] = isset($bookInfo['is_claim']) ? $bookInfo['is_claim'] : 0;
  79. $info['is_fee'] = isset($bookInfo['status']) ? $bookInfo['status'] : 0;
  80. $info['pay_fee'] = isset($bookInfo['money']) ? floatval($bookInfo['money']) : 0;
  81. } else {
  82. $areaIds = [];
  83. if ($info['city_id']) {
  84. $areaIds[] = $info['city_id'];
  85. }
  86. if ($info['district_id']) {
  87. $areaIds[] = $info['district_id'];
  88. }
  89. $areas = CityModel::whereIn('citycode', $areaIds)->pluck('name');
  90. $areas = $areas ? $areas->toArray() : [];
  91. $info['area'] = $areas ? implode('/', $areas) : '';
  92. // 企业
  93. $stores = '';
  94. if($info['store_ids']){
  95. $stores = UserModel::whereIn('id', $info['store_ids'])
  96. ->select(['id','avatar','realname','nickname as company','mobile','department','position','status'])
  97. ->where(['mark'=>1])
  98. ->get();;
  99. }
  100. $info['stores'] = $stores?$stores->toArray() : [];
  101. $info['store_count'] = count($info['stores']);
  102. // 邀请报名人数
  103. $info['records_count']= (int)MeetingRecordsModel::from('meetings_records as a')
  104. ->leftJoin('member as b','b.id','=','a.user_id')
  105. ->where(['a.meeting_id'=>$id,'b.parent_id'=>$userId,'a.status'=>1])
  106. ->count('a.id');
  107. /*
  108. $supervisors = '';
  109. if($info['supervisor_ids']){
  110. $supervisors = SupervisorsModel::whereIn('id', $info['supervisor_ids'])
  111. ->select(['id','name','avatar','mobile','company','occupation','department','position','status'])
  112. ->where(['mark'=>1])
  113. ->get();
  114. }
  115. $info['supervisors'] = $supervisors?$supervisors->toArray() :[];
  116. $info['supervisor_count'] =count($info['supervisors']);
  117. */
  118. $bookInfo = MeetingRecordsModel::where(['user_id' => $userId, 'meeting_id' => $id, 'mark' => 1])
  119. ->select(['id', 'is_sign', 'money', 'realname', 'mobile', 'is_claim', 'status'])
  120. ->first();
  121. $bookInfo = $bookInfo ? $bookInfo->toArray() : [];
  122. $info['is_book'] = $bookInfo ? 1 : 0;
  123. $info['is_sign'] = isset($bookInfo['is_sign']) ? $bookInfo['is_sign'] : 0;
  124. $info['is_claim'] = isset($bookInfo['is_claim']) ? $bookInfo['is_claim'] : 0;
  125. $info['is_fee'] = isset($bookInfo['status']) ? $bookInfo['status'] : 0;
  126. $info['pay_fee'] = isset($bookInfo['money']) ? floatval($bookInfo['money']) : 0;
  127. $info['book_realname'] = isset($bookInfo['realname']) ? trim($bookInfo['realname']) : '';
  128. $info['book_mobile'] = isset($bookInfo['mobile']) ? trim($bookInfo['mobile']) : '';
  129. }
  130. RedisService::set($cacheKey, $info, rand(5, 10));
  131. }
  132. return $info;
  133. }
  134. /**
  135. * @param $params
  136. * @param int $pageSize
  137. * @return array
  138. */
  139. public function getDataList($params, $pageSize = 15)
  140. {
  141. $query = $this->getQuery($params);
  142. $list = $query->select(['meetings.*'])
  143. ->withCount(['records'])
  144. ->groupBy('meetings.id')
  145. ->orderBy('meetings.sort', 'desc')
  146. ->orderBy('meetings.id', 'desc')
  147. ->paginate($pageSize > 0 ? $pageSize : 9999999);
  148. $list = $list ? $list->toArray() : [];
  149. return [
  150. 'pageSize' => $pageSize,
  151. 'total' => isset($list['total']) ? $list['total'] : 0,
  152. 'list' => isset($list['data']) ? $list['data'] : []
  153. ];
  154. }
  155. /**
  156. * 查询
  157. * @param $params
  158. * @return mixed
  159. */
  160. public function getQuery($params)
  161. {
  162. $where = ['meetings.status' => 0, 'meetings.mark' => 1];
  163. $status = isset($params['status']) && $params['status'] ? intval($params['status']) : 1;
  164. if ($status > 0) {
  165. $where['meetings.status'] = $status;
  166. } else {
  167. unset($where['meetings.status']);
  168. }
  169. return $this->model->with(['member', 'company','province','city','district'])
  170. ->from('meetings')
  171. ->leftJoin('meetings_records', 'meetings_records.meeting_id', 'meetings.id')
  172. ->where($where)
  173. ->where(function ($query) use ($params) {
  174. $keyword = isset($params['keyword']) ? $params['keyword'] : '';
  175. if ($keyword) {
  176. $query->where('meetings.title', 'like', "%{$keyword}%");
  177. }
  178. $type = isset($params['type']) ? $params['type'] : 0;
  179. $userId = isset($params['user_id']) ? $params['user_id'] : 0;
  180. // 我组织的
  181. if ($type == 1) {
  182. $query->where('meetings.user_id', $userId);
  183. } else if ($type == 2) {
  184. $query->where('meetings_records.user_id', $userId)->where('meetings_records.mark', 1);
  185. }
  186. });
  187. }
  188. /**
  189. * 报名
  190. * @param $userId
  191. * @param $params
  192. * @return array|false
  193. */
  194. public function books($userId, $params)
  195. {
  196. $meetingId = isset($params['id']) ? intval($params['id']) : 0;
  197. $realname = isset($params['realname']) ? trim($params['realname']) : '';
  198. $mobile = isset($params['mobile']) ? trim($params['mobile']) : '';
  199. if ($meetingId <= 0) {
  200. $this->error = '会议参数错误,请返回重试~';
  201. return false;
  202. }
  203. $cacheKey = "caches:meetingBooks:{$userId}_{$meetingId}";
  204. if (RedisService::get($cacheKey)) {
  205. $this->error = '请不要重复报名';
  206. return false;
  207. }
  208. $nowDate = date('Y-m-d H:i:s');
  209. $info = $this->model->where(['id' => $meetingId])->first();
  210. $startAt = isset($info['start_at']) ? $info['start_at'] : '';
  211. $endAt = isset($info['end_at']) ? $info['end_at'] : '';
  212. $fee = isset($info['fee']) ? $info['fee'] : 0;
  213. $orderDay = isset($info['order_day']) && $info['order_day'] > 1 ? intval($info['order_day']) : 1; // 订单关联天数
  214. if ($startAt && $nowDate < $startAt) {
  215. RedisService::clear($cacheKey);
  216. $this->error = '会议尚未开始,无法报名';
  217. return false;
  218. }
  219. if ($endAt && $nowDate > $endAt) {
  220. RedisService::clear($cacheKey);
  221. $this->error = '会议已结束,无法报名';
  222. return false;
  223. }
  224. $record = MeetingRecordsModel::where(['meeting_id' => $meetingId, 'user_id' => $userId, 'mark' => 1])->first();
  225. $status = isset($record['status']) ? $record['status'] : 0;
  226. if ($record) {
  227. RedisService::clear($cacheKey);
  228. $this->error = $status == 1 ? '您已经完成报名' : '您已经完成报名,请等候付费确认';
  229. return false;
  230. }
  231. $otherRecord = MeetingRecordsModel::with(['meeting'])->where(['user_id' => $userId, 'mark' => 1])->first();
  232. $meeting = isset($otherRecord['meeting']) ? $otherRecord['meeting'] : [];
  233. $meetingStartAt = isset($meeting['start_at']) ? $meeting['start_at'] : '';
  234. $meetingEndAt = isset($meeting['end_at']) ? $meeting['end_at'] : '';
  235. if ($otherRecord && $meetingStartAt <= $nowDate && $nowDate <= $meetingEndAt) {
  236. RedisService::clear($cacheKey);
  237. $this->error = '您有未完成的会议,请完成后再报名';
  238. return false;
  239. }
  240. $data = [
  241. 'user_id' => $userId,
  242. 'meeting_id' => $meetingId,
  243. 'realname' => $realname,
  244. 'mobile' => $mobile,
  245. 'order_count' => 0,
  246. 'order_total' => 0,
  247. 'is_sign' => 2,
  248. 'is_claim' => 2,
  249. 'status' => $fee <= 0 ? 1 : 2,
  250. 'create_time' => time(),
  251. 'update_time' => time(),
  252. 'mark' => 1,
  253. ];
  254. if (!$id = MeetingRecordsModel::insertGetId($data)) {
  255. RedisService::clear($cacheKey);
  256. $this->error = '报名失败';
  257. return false;
  258. }
  259. $this->error = $fee > 0 ? '报名成功,请等候付费确认' : '报名成功';
  260. RedisService::set($cacheKey, $data, 30);
  261. RedisService::keyDel("caches:meeting:info_{$meetingId}_{$userId}*");
  262. return ['id' => $id];
  263. }
  264. /**
  265. * 签到
  266. * @param $userId
  267. * @param $params
  268. * @return array|false
  269. */
  270. public function sign($userId, $params)
  271. {
  272. $meetingId = isset($params['id']) ? intval($params['id']) : 0;
  273. if ($meetingId <= 0) {
  274. $this->error = '会议参数错误,请返回重试~';
  275. return false;
  276. }
  277. $cacheKey = "caches:meetingSign:{$userId}_{$meetingId}";
  278. if (RedisService::get($cacheKey)) {
  279. $this->error = '请不要重复签到';
  280. return false;
  281. }
  282. $nowDate = date('Y-m-d H:i:s');
  283. $info = $this->model->where(['id' => $meetingId])->first();
  284. $startAt = isset($info['start_at']) ? $info['start_at'] : '';
  285. $endAt = isset($info['end_at']) ? $info['end_at'] : '';
  286. $orderDay = isset($info['order_day']) && $info['order_day'] > 1 ? intval($info['order_day']) : 1; // 订单关联天数
  287. if ($startAt && $nowDate < $startAt) {
  288. RedisService::clear($cacheKey);
  289. $this->error = '会议尚未开始,无法签到';
  290. return false;
  291. }
  292. if ($endAt && $nowDate > $endAt) {
  293. RedisService::clear($cacheKey);
  294. $this->error = '会议已结束,无法签到';
  295. return false;
  296. }
  297. $record = MeetingRecordsModel::where(['meeting_id' => $meetingId, 'user_id' => $userId, 'mark' => 1])->first();
  298. $recordId = isset($record['id']) ? $record['id'] : 0;
  299. $isSign = isset($record['is_sign']) ? $record['is_sign'] : 0;
  300. $bookStatus = isset($record['status']) ? $record['status'] : 0;
  301. if (empty($record) || $recordId <= 0) {
  302. RedisService::clear($cacheKey);
  303. $this->error = '抱歉,请先完成报名';
  304. return false;
  305. }
  306. if ($bookStatus != 1) {
  307. RedisService::clear($cacheKey);
  308. $this->error = '抱歉,请先完成报名付费确认';
  309. return false;
  310. }
  311. if ($isSign == 1) {
  312. RedisService::clear($cacheKey);
  313. $this->error = '您已经完成签到';
  314. return false;
  315. }
  316. $otherRecord = MeetingRecordsModel::with(['meeting'])->where(['user_id' => $userId, 'mark' => 1])->first();
  317. $meeting = isset($otherRecord['meeting']) ? $otherRecord['meeting'] : [];
  318. $meetingStartAt = isset($meeting['start_at']) ? $meeting['start_at'] : '';
  319. $meetingEndAt = isset($meeting['end_at']) ? $meeting['end_at'] : '';
  320. if ($otherRecord && $meetingStartAt <= $nowDate && $nowDate <= $meetingEndAt) {
  321. RedisService::clear($cacheKey);
  322. $this->error = '您有未完成的会议,请完成后再签到';
  323. return false;
  324. }
  325. $data = [
  326. 'is_sign' => 1,
  327. 'sign_at' => date('Y-m-d H:i:s'),
  328. 'expired_at' => date('Y-m-d H:i:s', time() + $orderDay * 86400),
  329. 'create_time' => time(),
  330. 'update_time' => time(),
  331. 'mark' => 1,
  332. ];
  333. MeetingRecordsModel::where(['id' => $recordId])->update($data);
  334. $this->error = '签到成功';
  335. RedisService::set($cacheKey, $data, 30);
  336. RedisService::keyDel("caches:meeting:info_{$meetingId}_{$userId}*");
  337. return ['id' => $recordId];
  338. }
  339. /**
  340. * 领取物料
  341. * @param $userId
  342. * @param $params
  343. * @return array|false
  344. */
  345. public function claim($userId, $params)
  346. {
  347. $meetingId = isset($params['id']) ? intval($params['id']) : 0;
  348. if ($meetingId <= 0) {
  349. $this->error = '会议参数错误,请返回重试~';
  350. return false;
  351. }
  352. $cacheKey = "caches:meetingClaim:{$userId}_{$meetingId}";
  353. if (RedisService::get($cacheKey)) {
  354. $this->error = '请不要重复领取';
  355. return false;
  356. }
  357. $nowDate = date('Y-m-d H:i:s');
  358. $info = $this->model->where(['id' => $meetingId])->first();
  359. $startAt = isset($info['start_at']) ? $info['start_at'] : '';
  360. $endAt = isset($info['end_at']) ? $info['end_at'] : '';
  361. $orderDay = isset($info['order_day']) && $info['order_day'] > 1 ? intval($info['order_day']) : 1; // 订单关联天数
  362. if ($startAt && $nowDate < $startAt) {
  363. RedisService::clear($cacheKey);
  364. $this->error = '会议尚未开始,无法签到';
  365. return false;
  366. }
  367. if ($endAt && $nowDate > $endAt) {
  368. RedisService::clear($cacheKey);
  369. $this->error = '会议已结束,无法签到';
  370. return false;
  371. }
  372. $record = MeetingRecordsModel::where(['meeting_id' => $meetingId, 'user_id' => $userId, 'mark' => 1])->first();
  373. $recordId = isset($record['id']) ? $record['id'] : 0;
  374. $isSign = isset($record['is_sign']) ? $record['is_sign'] : 0;
  375. $isClaim = isset($record['is_claim']) ? $record['is_claim'] : 0;
  376. $bookStatus = isset($record['status']) ? $record['status'] : 0;
  377. if (empty($record) || $recordId <= 0) {
  378. RedisService::clear($cacheKey);
  379. $this->error = '抱歉,请先完成报名';
  380. return false;
  381. }
  382. if ($bookStatus != 1) {
  383. RedisService::clear($cacheKey);
  384. $this->error = '抱歉,请先完成报名付费确认';
  385. return false;
  386. }
  387. if ($isSign != 1) {
  388. RedisService::clear($cacheKey);
  389. $this->error = '请先完成签到';
  390. return false;
  391. }
  392. if ($isClaim == 1) {
  393. RedisService::clear($cacheKey);
  394. $this->error = '您已经领取过物料';
  395. return false;
  396. }
  397. $data = [
  398. 'is_claim' => 1,
  399. 'claim_at' => date('Y-m-d H:i:s'),
  400. 'create_time' => time(),
  401. 'update_time' => time(),
  402. 'mark' => 1,
  403. ];
  404. MeetingRecordsModel::where(['id' => $recordId])->update($data);
  405. $this->error = '领取成功';
  406. RedisService::set($cacheKey, $data, 30);
  407. RedisService::keyDel("caches:meeting:info_{$meetingId}_{$userId}*");
  408. return ['id' => $recordId];
  409. }
  410. /**
  411. * 会议报名签到记录
  412. * @param $params
  413. * @param int $pageSize
  414. * @return array
  415. */
  416. public function records($params, $pageSize = 20, $userId = 0)
  417. {
  418. $meetingId = isset($params['meeting_id']) ? $params['meeting_id'] : 0;
  419. $where = ['b.parent_id' => $userId, 'a.meeting_id' => $meetingId, 'a.mark' => 1];
  420. $query = MeetingRecordsModel::with(['member'])
  421. ->from('meetings_records as a')
  422. ->leftJoin('member as b', 'b.id', '=', 'a.user_id')
  423. ->where(function ($query) use ($params) {
  424. $keyword = isset($params['keyword']) ? trim($params['keyword']) : '';
  425. if ($keyword) {
  426. $query->where('b.mobile', 'like', "%{$keyword}%")
  427. ->orWhere('b.nickname', 'like', "%{$keyword}%")
  428. ->orWhere('b.realname', 'like', "%{$keyword}%");
  429. }
  430. })
  431. ->where('b.id', '>', 0);
  432. $query1 = clone $query;
  433. $list = $query1->where($where)->select(['a.*'])
  434. ->orderBy('a.id', 'desc')
  435. ->paginate($pageSize > 0 ? $pageSize : 9999999);
  436. $list = $list ? $list->toArray() : [];
  437. $query2 = clone $query;
  438. unset($where['b.parent_id']);
  439. // var_dump($where);
  440. $total = $query2->where($where)->count('a.id');
  441. return [
  442. 'pageSize' => $pageSize,
  443. 'count' => isset($list['total']) ? $list['total'] : 0,
  444. 'total' => $total,
  445. 'list' => isset($list['data']) ? $list['data'] : []
  446. ];
  447. }
  448. }