MeetingService.php 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397
  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. ->withCount(['records'])
  65. ->first();
  66. $info = $info? $info->toArray() : [];
  67. if($info){
  68. $info['fee'] = floatval($info['fee']);
  69. if($type == 2){
  70. $info['qrcode'] = MpService::make()->getMiniQrcode('pagesSub/pages/meeting/books',$id);
  71. $info['qrcode'] = $info['qrcode']? get_image_url($info['qrcode']):'';
  72. }else if($type == 3){
  73. $bookInfo = MeetingRecordsModel::where(['user_id'=>$userId,'meeting_id'=>$id,'mark'=>1])
  74. ->select(['id','is_sign','status'])
  75. ->first();
  76. $bookInfo = $bookInfo?$bookInfo->toArray() : [];
  77. $info['is_book'] = $bookInfo?1: 0;
  78. $info['is_sign'] = isset($bookInfo['is_sign'])?$bookInfo['is_sign']: 0;
  79. $info['is_fee'] = isset($bookInfo['status'])?$bookInfo['status']: 0;
  80. }else{
  81. $areaIds = [];
  82. if($info['city_id']){
  83. $areaIds[] = $info['city_id'];
  84. }
  85. if($info['district_id']){
  86. $areaIds[] = $info['district_id'];
  87. }
  88. $areas = CityModel::whereIn('citycode', $areaIds)->pluck('name');
  89. $areas = $areas?$areas->toArray() :[];
  90. $info['area'] = $areas? implode('/',$areas) : '';
  91. $stores = '';
  92. if($info['store_ids']){
  93. $stores = UserModel::whereIn('id', $info['store_ids'])
  94. ->select(['id','avatar','realname','nickname as company','mobile','department','position','status'])
  95. ->where(['mark'=>1])
  96. ->get();;
  97. }
  98. $info['stores'] = $stores?$stores->toArray() : [];
  99. $supervisors = '';
  100. if($info['supervisor_ids']){
  101. $supervisors = SupervisorsModel::whereIn('id', $info['supervisor_ids'])
  102. ->select(['id','name','avatar','mobile','company','occupation','department','position','status'])
  103. ->where(['mark'=>1])
  104. ->get();
  105. }
  106. $info['supervisors'] = $supervisors?$supervisors->toArray() :[];
  107. $info['store_count'] = count($info['stores']);
  108. $info['supervisor_count'] =count($info['supervisors']);
  109. $bookInfo = MeetingRecordsModel::where(['user_id'=>$userId,'meeting_id'=>$id,'mark'=>1])
  110. ->select(['id','is_sign','status'])
  111. ->first();
  112. $bookInfo = $bookInfo?$bookInfo->toArray() : [];
  113. $info['is_book'] = $bookInfo?1: 0;
  114. $info['is_sign'] = isset($bookInfo['is_sign'])?$bookInfo['is_sign']: 0;
  115. $info['is_fee'] = isset($bookInfo['status'])?$bookInfo['status']: 0;
  116. }
  117. RedisService::set($cacheKey, $info, rand(5,10));
  118. }
  119. return $info;
  120. }
  121. /**
  122. * @param $params
  123. * @param int $pageSize
  124. * @return array
  125. */
  126. public function getDataList($params, $pageSize = 15)
  127. {
  128. $query = $this->getQuery($params);
  129. $list = $query->select(['meetings.*'])
  130. ->withCount(['records'])
  131. ->groupBy('meetings.id')
  132. ->orderBy('meetings.sort','desc')
  133. ->orderBy('meetings.id','desc')
  134. ->paginate($pageSize > 0 ? $pageSize : 9999999);
  135. $list = $list? $list->toArray() :[];
  136. return [
  137. 'pageSize'=> $pageSize,
  138. 'total'=>isset($list['total'])? $list['total'] : 0,
  139. 'list'=> isset($list['data'])? $list['data'] : []
  140. ];
  141. }
  142. /**
  143. * 查询
  144. * @param $params
  145. * @return mixed
  146. */
  147. public function getQuery($params)
  148. {
  149. $where = ['meetings.status'=>0,'meetings.mark' => 1];
  150. $status = isset($params['status']) && $params['status']? intval($params['status']) : 1;
  151. if($status>0){
  152. $where['meetings.status'] = $status;
  153. }else{
  154. unset($where['meetings.status']);
  155. }
  156. return $this->model->with(['member','company'])
  157. ->from('meetings')
  158. ->leftJoin('meetings_records','meetings_records.meeting_id','meetings.id')
  159. ->where($where)
  160. ->where(function ($query) use($params){
  161. $keyword = isset($params['keyword'])? $params['keyword'] : '';
  162. if($keyword){
  163. $query->where('meetings.title','like',"%{$keyword}%");
  164. }
  165. $type = isset($params['type'])? $params['type'] : 0;
  166. $userId = isset($params['user_id'])?$params['user_id'] : 0;
  167. // 我组织的
  168. if($type==1){
  169. $query->where('meetings.user_id', $userId);
  170. }else if($type == 2){
  171. $query->where('meetings_records.user_id', $userId)->where('meetings_records.mark',1);
  172. }
  173. });
  174. }
  175. /**
  176. * 报名
  177. * @param $userId
  178. * @param $params
  179. * @return array|false
  180. */
  181. public function books($userId, $params)
  182. {
  183. $meetingId = isset($params['id']) ? intval($params['id']) : 0;
  184. if ($meetingId<=0) {
  185. $this->error = '会议参数错误,请返回重试~';
  186. return false;
  187. }
  188. $cacheKey = "caches:meetingBooks:{$userId}_{$meetingId}";
  189. if (RedisService::get($cacheKey)) {
  190. $this->error = '请不要重复报名';
  191. return false;
  192. }
  193. $nowDate = date('Y-m-d H:i:s');
  194. $info = $this->model->where(['id'=>$meetingId])->first();
  195. $startAt = isset($info['start_at'])?$info['start_at'] : '';
  196. $endAt = isset($info['end_at'])?$info['end_at'] : '';
  197. $fee = isset($info['fee'])?$info['fee'] : 0;
  198. $orderDay = isset($info['order_day']) && $info['order_day']>1?intval($info['order_day']) : 1; // 订单关联天数
  199. if($startAt && $nowDate < $startAt){
  200. RedisService::clear($cacheKey);
  201. $this->error = '会议尚未开始,无法报名';
  202. return false;
  203. }
  204. if($endAt && $nowDate > $endAt){
  205. RedisService::clear($cacheKey);
  206. $this->error = '会议已结束,无法报名';
  207. return false;
  208. }
  209. $record = MeetingRecordsModel::where(['meeting_id'=> $meetingId,'user_id'=>$userId,'mark'=>1])->first();
  210. $status = isset($record['status'])?$record['status']:0;
  211. if($record){
  212. RedisService::clear($cacheKey);
  213. $this->error = $status==1? '您已经完成报名': '您已经完成报名,请等候付费确认';
  214. return false;
  215. }
  216. $otherRecord = MeetingRecordsModel::with(['meeting'])->where(['user_id'=>$userId,'mark'=>1])->first();
  217. $meeting = isset($otherRecord['meeting'])?$otherRecord['meeting']:[];
  218. $meetingStartAt = isset($meeting['start_at'])?$meeting['start_at'] : '';
  219. $meetingEndAt = isset($meeting['end_at'])?$meeting['end_at'] : '';
  220. if($otherRecord && $meetingStartAt <= $nowDate && $nowDate <= $meetingEndAt){
  221. RedisService::clear($cacheKey);
  222. $this->error = '您有未完成的会议,请完成后再报名';
  223. return false;
  224. }
  225. $data = [
  226. 'user_id' => $userId,
  227. 'meeting_id' => $meetingId,
  228. 'order_count' => 0,
  229. 'order_total' => 0,
  230. 'is_sign' => 2,
  231. 'status' => $fee<=0?1:2,
  232. 'create_time' => time(),
  233. 'update_time' => time(),
  234. 'mark' => 1,
  235. ];
  236. if(!$id = MeetingRecordsModel::insertGetId($data)){
  237. RedisService::clear($cacheKey);
  238. $this->error = '报名失败';
  239. return false;
  240. }
  241. $this->error = $fee>0?'报名成功,请等候付费确认':'报名成功';
  242. RedisService::set($cacheKey, $data, 30);
  243. RedisService::keyDel("caches:meeting:info_{$meetingId}_{$userId}*");
  244. return ['id' => $id];
  245. }
  246. /**
  247. * 签到
  248. * @param $userId
  249. * @param $params
  250. * @return array|false
  251. */
  252. public function sign($userId, $params)
  253. {
  254. $meetingId = isset($params['id']) ? intval($params['id']) : 0;
  255. if ($meetingId<=0) {
  256. $this->error = '会议参数错误,请返回重试~';
  257. return false;
  258. }
  259. $cacheKey = "caches:meetingSign:{$userId}_{$meetingId}";
  260. if (RedisService::get($cacheKey)) {
  261. $this->error = '请不要重复签到';
  262. return false;
  263. }
  264. $nowDate = date('Y-m-d H:i:s');
  265. $info = $this->model->where(['id'=>$meetingId])->first();
  266. $startAt = isset($info['start_at'])?$info['start_at'] : '';
  267. $endAt = isset($info['end_at'])?$info['end_at'] : '';
  268. $orderDay = isset($info['order_day']) && $info['order_day']>1?intval($info['order_day']) : 1; // 订单关联天数
  269. if($startAt && $nowDate < $startAt){
  270. RedisService::clear($cacheKey);
  271. $this->error = '会议尚未开始,无法签到';
  272. return false;
  273. }
  274. if($endAt && $nowDate > $endAt){
  275. RedisService::clear($cacheKey);
  276. $this->error = '会议已结束,无法签到';
  277. return false;
  278. }
  279. $record = MeetingRecordsModel::where(['meeting_id'=> $meetingId,'user_id'=>$userId,'mark'=>1])->first();
  280. $recordId = isset($record['id'])?$record['id'] : 0;
  281. $isSign = isset($record['is_sign'])?$record['is_sign'] : 0;
  282. $bookStatus = isset($record['status'])?$record['status'] : 0;
  283. if(empty($record) || $recordId<=0){
  284. RedisService::clear($cacheKey);
  285. $this->error = '抱歉,请先完成报名';
  286. return false;
  287. }
  288. if($bookStatus!=1){
  289. RedisService::clear($cacheKey);
  290. $this->error = '抱歉,请先完成报名付费确认';
  291. return false;
  292. }
  293. if($isSign==1){
  294. RedisService::clear($cacheKey);
  295. $this->error = '您已经完成签到';
  296. return false;
  297. }
  298. $otherRecord = MeetingRecordsModel::with(['meeting'])->where(['user_id'=>$userId,'mark'=>1])->first();
  299. $meeting = isset($otherRecord['meeting'])?$otherRecord['meeting']:[];
  300. $meetingStartAt = isset($meeting['start_at'])?$meeting['start_at'] : '';
  301. $meetingEndAt = isset($meeting['end_at'])?$meeting['end_at'] : '';
  302. if($otherRecord && $meetingStartAt <= $nowDate && $nowDate <= $meetingEndAt){
  303. RedisService::clear($cacheKey);
  304. $this->error = '您有未完成的会议,请完成后再签到';
  305. return false;
  306. }
  307. $data = [
  308. 'is_sign' => 1,
  309. 'sign_at' => date('Y-m-d H:i:s'),
  310. 'expired_at' => date('Y-m-d H:i:s', time() + $orderDay * 86400),
  311. 'create_time' => time(),
  312. 'update_time' => time(),
  313. 'mark' => 1,
  314. ];
  315. MeetingRecordsModel::where(['id'=>$recordId])->update($data);
  316. $this->error = '签到成功';
  317. RedisService::set($cacheKey, $data, 30);
  318. RedisService::keyDel("caches:meeting:info_{$meetingId}_{$userId}*");
  319. return ['id' => $recordId];
  320. }
  321. /**
  322. * 会议报名签到记录
  323. * @param $params
  324. * @param int $pageSize
  325. * @return array
  326. */
  327. public function records($params,$pageSize=20)
  328. {
  329. $meetingId = isset($params['meeting_id'])?$params['meeting_id']: 0;
  330. $list = MeetingRecordsModel::with(['member'])
  331. ->from('meetings_records as a')
  332. ->leftJoin('member as b','b.id','=','a.user_id')
  333. ->where(function($query) use($params){
  334. $keyword = isset($params['keyword'])? trim($params['keyword']) : '';
  335. if($keyword){
  336. $query->where('b.mobile','like',"%{$keyword}%")
  337. ->orWhere('b.nickname','like',"%{$keyword}%")
  338. ->orWhere('b.realname','like',"%{$keyword}%");
  339. }
  340. })
  341. ->where('b.id','>',0)
  342. ->where(['a.meeting_id'=>$meetingId,'a.is_sign'=>1,'a.mark'=>1])
  343. ->select(['a.*'])
  344. ->orderBy('a.id','desc')
  345. ->paginate($pageSize > 0 ? $pageSize : 9999999);
  346. $list = $list? $list->toArray() :[];
  347. return [
  348. 'pageSize'=> $pageSize,
  349. 'total'=>isset($list['total'])? $list['total'] : 0,
  350. 'list'=> isset($list['data'])? $list['data'] : []
  351. ];
  352. }
  353. }