MeetingService.php 9.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288
  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'])
  63. ->where(['id'=>$id,'status'=>1,'mark'=>1])
  64. ->withCount(['records'])
  65. ->first();
  66. $info = $info? $info->toArray() : [];
  67. if($info){
  68. if($type == 2){
  69. $info['qrcode'] = MpService::make()->getMiniQrcode('pagesSub/pages/meeting/books','meet&id='.$id);
  70. $info['qrcode'] = $info['qrcode']? get_image_url($info['qrcode']):'';
  71. }else if($type == 3){
  72. $info['is_book'] = MeetingRecordsModel::where(['user_id'=>$userId,'meeting_id'=>$id,'mark'=>1])->value('id');
  73. }else{
  74. $areaIds = [];
  75. if($info['city_id']){
  76. $areaIds[] = $info['city_id'];
  77. }
  78. if($info['district_id']){
  79. $areaIds[] = $info['district_id'];
  80. }
  81. $areas = CityModel::whereIn('citycode', $areaIds)->pluck('name');
  82. $areas = $areas?$areas->toArray() :[];
  83. $info['area'] = $areas? implode('/',$areas) : '';
  84. $stores = '';
  85. if($info['store_ids']){
  86. $stores = UserModel::whereIn('id', $info['store_ids'])
  87. ->select(['id','avatar','realname','nickname as company','mobile','department','position','status'])
  88. ->where(['mark'=>1])
  89. ->get();;
  90. }
  91. $info['stores'] = $stores?$stores->toArray() : [];
  92. $supervisors = '';
  93. if($info['supervisor_ids']){
  94. $supervisors = SupervisorsModel::whereIn('id', $info['supervisor_ids'])
  95. ->select(['id','name','avatar','mobile','company','occupation','department','position','status'])
  96. ->where(['mark'=>1])
  97. ->get();
  98. }
  99. $info['supervisors'] = $supervisors?$supervisors->toArray() :[];
  100. $info['guest_count'] = count($info['stores']) + count($info['supervisors']);
  101. }
  102. RedisService::set($cacheKey, $info, rand(5,10));
  103. }
  104. return $info;
  105. }
  106. /**
  107. * @param $params
  108. * @param int $pageSize
  109. * @return array
  110. */
  111. public function getDataList($params, $pageSize = 15)
  112. {
  113. $query = $this->getQuery($params);
  114. $list = $query->select(['meetings.*'])
  115. ->withCount(['records'])
  116. ->orderBy('meetings.sort','desc')
  117. ->orderBy('meetings.id','desc')
  118. ->paginate($pageSize > 0 ? $pageSize : 9999999);
  119. $list = $list? $list->toArray() :[];
  120. return [
  121. 'pageSize'=> $pageSize,
  122. 'total'=>isset($list['total'])? $list['total'] : 0,
  123. 'list'=> isset($list['data'])? $list['data'] : []
  124. ];
  125. }
  126. /**
  127. * 查询
  128. * @param $params
  129. * @return mixed
  130. */
  131. public function getQuery($params)
  132. {
  133. $where = ['meetings.status'=>0,'meetings.mark' => 1];
  134. $status = isset($params['status']) && $params['status']? intval($params['status']) : 1;
  135. if($status>0){
  136. $where['meetings.status'] = $status;
  137. }else{
  138. unset($where['meetings.status']);
  139. }
  140. return $this->model->with(['member'])
  141. ->from('meetings')
  142. ->leftJoin('meetings_records','meetings_records.meeting_id','meetings.id')
  143. ->where($where)
  144. ->where(function ($query) use($params){
  145. $keyword = isset($params['keyword'])? $params['keyword'] : '';
  146. if($keyword){
  147. $query->where('meetings.title','like',"%{$keyword}%");
  148. }
  149. $type = isset($params['type'])? $params['type'] : 0;
  150. $userId = isset($params['user_id'])?$params['user_id'] : 0;
  151. // 我组织的
  152. if($type==1){
  153. $query->where('meetings.user_id', $userId);
  154. }else if($type == 2){
  155. $query->where('meetings_records.user_id', $userId)->where('meetings_records.mark',1);
  156. }
  157. });
  158. }
  159. /**
  160. * 签到
  161. * @param $userId
  162. * @param $params
  163. * @return array|false
  164. */
  165. public function books($userId, $params)
  166. {
  167. $meetingId = isset($params['id']) ? intval($params['id']) : 0;
  168. if ($meetingId<=0) {
  169. $this->error = '会议参数错误,请返回重试~';
  170. return false;
  171. }
  172. $cacheKey = "caches:meetingBooks:{$userId}_{$meetingId}";
  173. if (RedisService::get($cacheKey)) {
  174. $this->error = '请不要重复签到';
  175. return false;
  176. }
  177. $nowDate = date('Y-m-d H:i:s');
  178. $info = $this->model->where(['id'=>$meetingId])->first();
  179. $startAt = isset($info['start_at'])?$info['start_at'] : '';
  180. $endAt = isset($info['end_at'])?$info['end_at'] : '';
  181. $orderDay = isset($info['order_day']) && $info['order_day']>1?intval($info['order_day']) : 1; // 订单关联天数
  182. if($startAt && $nowDate < $startAt){
  183. RedisService::clear($cacheKey);
  184. $this->error = '会议尚未开始,无法签到';
  185. return false;
  186. }
  187. if($endAt && $nowDate > $endAt){
  188. RedisService::clear($cacheKey);
  189. $this->error = '会议已结束,无法签到';
  190. return false;
  191. }
  192. $record = MeetingRecordsModel::where(['meeting_id'=> $meetingId,'user_id'=>$userId,'mark'=>1])->first();
  193. if($record){
  194. RedisService::clear($cacheKey);
  195. $this->error = '您已经完成签到';
  196. return false;
  197. }
  198. $otherRecord = MeetingRecordsModel::with(['meeting'])->where(['user_id'=>$userId,'mark'=>1])->first();
  199. $meeting = isset($otherRecord['meeting'])?$otherRecord['meeting']:[];
  200. $meetingStartAt = isset($meeting['start_at'])?$meeting['start_at'] : '';
  201. $meetingEndAt = isset($meeting['end_at'])?$meeting['end_at'] : '';
  202. if($otherRecord && $meetingStartAt <= $nowDate && $nowDate <= $meetingEndAt){
  203. RedisService::clear($cacheKey);
  204. $this->error = '您有未完成的会议,请完成后再签到';
  205. return false;
  206. }
  207. $data = [
  208. 'user_id' => $userId,
  209. 'meeting_id' => $meetingId,
  210. 'order_count' => 0,
  211. 'order_total' => 0,
  212. 'expired_at' => date('Y-m-d H:i:s', time() + $orderDay * 86400),
  213. 'create_time' => time(),
  214. 'update_time' => time(),
  215. 'mark' => 1,
  216. ];
  217. if(!$id = MeetingRecordsModel::insertGetId($data)){
  218. RedisService::clear($cacheKey);
  219. $this->error = '签到失败';
  220. return false;
  221. }
  222. $this->error = '签到成功';
  223. RedisService::set($cacheKey, $data, 30);
  224. return ['id' => $id];
  225. }
  226. /**
  227. * 会议报名记录
  228. * @param $params
  229. * @param int $pageSize
  230. * @return array
  231. */
  232. public function records($params,$pageSize=20)
  233. {
  234. $meetingId = isset($params['meeting_id'])?$params['meeting_id']: 0;
  235. $list = MeetingRecordsModel::with(['member'])
  236. ->from('meetings_records as a')
  237. ->leftJoin('member as b','b.id','=','a.user_id')
  238. ->where(function($query) use($params){
  239. $keyword = isset($params['keyword'])? trim($params['keyword']) : '';
  240. if($keyword){
  241. $query->where('b.mobile','like',"%{$keyword}%")
  242. ->orWhere('b.nickname','like',"%{$keyword}%")
  243. ->orWhere('b.realname','like',"%{$keyword}%");
  244. }
  245. })
  246. ->where('b.id','>',0)
  247. ->where(['a.meeting_id'=>$meetingId,'a.mark'=>1])
  248. ->select(['a.*'])
  249. ->orderBy('a.id','desc')
  250. ->paginate($pageSize > 0 ? $pageSize : 9999999);
  251. $list = $list? $list->toArray() :[];
  252. return [
  253. 'pageSize'=> $pageSize,
  254. 'total'=>isset($list['total'])? $list['total'] : 0,
  255. 'list'=> isset($list['data'])? $list['data'] : []
  256. ];
  257. }
  258. }