MeetingService.php 9.9 KB

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