| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289 |
- <?php
- // +----------------------------------------------------------------------
- // | LARAVEL8.0 框架 [ LARAVEL ][ RXThinkCMF ]
- // +----------------------------------------------------------------------
- // | 版权所有 2017~2021 LARAVEL研发中心
- // +----------------------------------------------------------------------
- // | 官方网站: http://www.laravel.cn
- // +----------------------------------------------------------------------
- // | Author: laravel开发员 <laravel.qq.com>
- // +----------------------------------------------------------------------
- namespace App\Services\Api;
- use App\Models\CityModel;
- use App\Models\MeetingModel;
- use App\Models\MeetingRecordsModel;
- use App\Models\SupervisorsModel;
- use App\Models\UserModel;
- use App\Services\BaseService;
- use App\Services\MpService;
- use App\Services\RedisService;
- /**
- * 会议-服务类
- * @author laravel开发员
- * @since 2020/11/11
- * @package App\Services\Api
- */
- class MeetingService extends BaseService
- {
- // 静态对象
- protected static $instance = null;
- /**
- * 构造函数
- * @author laravel开发员
- * @since 2020/11/11
- */
- public function __construct()
- {
- $this->model = new MeetingModel();
- }
- /**
- * 静态入口
- */
- public static function make()
- {
- if (!self::$instance) {
- self::$instance = new static();
- }
- return self::$instance;
- }
- /**
- * 信息
- * @param int $id
- * @param int $userId
- * @return array|mixed
- */
- public function getInfo($id,$userId=0,$type=1)
- {
- $cacheKey = "caches:meeting:info_{$id}_{$userId}_{$type}";
- $info = RedisService::get($cacheKey);
- if($info){
- return $info;
- }
- $info = $this->model->with(['member','company'])
- ->where(['id'=>$id,'status'=>1,'mark'=>1])
- ->withCount(['records'])
- ->first();
- $info = $info? $info->toArray() : [];
- if($info){
- if($type == 2){
- $info['qrcode'] = MpService::make()->getMiniQrcode('pagesSub/pages/meeting/books','books&id='.$id);
- $info['qrcode'] = $info['qrcode']? get_image_url($info['qrcode']):'';
- }else if($type == 3){
- $info['is_book'] = MeetingRecordsModel::where(['user_id'=>$userId,'meeting_id'=>$id,'mark'=>1])->value('id');
- }else{
- $areaIds = [];
- if($info['city_id']){
- $areaIds[] = $info['city_id'];
- }
- if($info['district_id']){
- $areaIds[] = $info['district_id'];
- }
- $areas = CityModel::whereIn('citycode', $areaIds)->pluck('name');
- $areas = $areas?$areas->toArray() :[];
- $info['area'] = $areas? implode('/',$areas) : '';
- $stores = '';
- if($info['store_ids']){
- $stores = UserModel::whereIn('id', $info['store_ids'])
- ->select(['id','avatar','realname','nickname as company','mobile','department','position','status'])
- ->where(['mark'=>1])
- ->get();;
- }
- $info['stores'] = $stores?$stores->toArray() : [];
- $supervisors = '';
- if($info['supervisor_ids']){
- $supervisors = SupervisorsModel::whereIn('id', $info['supervisor_ids'])
- ->select(['id','name','avatar','mobile','company','occupation','department','position','status'])
- ->where(['mark'=>1])
- ->get();
- }
- $info['supervisors'] = $supervisors?$supervisors->toArray() :[];
- $info['store_count'] = count($info['stores']);
- $info['supervisor_count'] =count($info['supervisors']);
- }
- RedisService::set($cacheKey, $info, rand(5,10));
- }
- return $info;
- }
- /**
- * @param $params
- * @param int $pageSize
- * @return array
- */
- public function getDataList($params, $pageSize = 15)
- {
- $query = $this->getQuery($params);
- $list = $query->select(['meetings.*'])
- ->withCount(['records'])
- ->orderBy('meetings.sort','desc')
- ->orderBy('meetings.id','desc')
- ->paginate($pageSize > 0 ? $pageSize : 9999999);
- $list = $list? $list->toArray() :[];
- return [
- 'pageSize'=> $pageSize,
- 'total'=>isset($list['total'])? $list['total'] : 0,
- 'list'=> isset($list['data'])? $list['data'] : []
- ];
- }
- /**
- * 查询
- * @param $params
- * @return mixed
- */
- public function getQuery($params)
- {
- $where = ['meetings.status'=>0,'meetings.mark' => 1];
- $status = isset($params['status']) && $params['status']? intval($params['status']) : 1;
- if($status>0){
- $where['meetings.status'] = $status;
- }else{
- unset($where['meetings.status']);
- }
- return $this->model->with(['member','company'])
- ->from('meetings')
- ->leftJoin('meetings_records','meetings_records.meeting_id','meetings.id')
- ->where($where)
- ->where(function ($query) use($params){
- $keyword = isset($params['keyword'])? $params['keyword'] : '';
- if($keyword){
- $query->where('meetings.title','like',"%{$keyword}%");
- }
- $type = isset($params['type'])? $params['type'] : 0;
- $userId = isset($params['user_id'])?$params['user_id'] : 0;
- // 我组织的
- if($type==1){
- $query->where('meetings.user_id', $userId);
- }else if($type == 2){
- $query->where('meetings_records.user_id', $userId)->where('meetings_records.mark',1);
- }
- });
- }
- /**
- * 签到
- * @param $userId
- * @param $params
- * @return array|false
- */
- public function books($userId, $params)
- {
- $meetingId = isset($params['id']) ? intval($params['id']) : 0;
- if ($meetingId<=0) {
- $this->error = '会议参数错误,请返回重试~';
- return false;
- }
- $cacheKey = "caches:meetingBooks:{$userId}_{$meetingId}";
- if (RedisService::get($cacheKey)) {
- $this->error = '请不要重复签到';
- return false;
- }
- $nowDate = date('Y-m-d H:i:s');
- $info = $this->model->where(['id'=>$meetingId])->first();
- $startAt = isset($info['start_at'])?$info['start_at'] : '';
- $endAt = isset($info['end_at'])?$info['end_at'] : '';
- $orderDay = isset($info['order_day']) && $info['order_day']>1?intval($info['order_day']) : 1; // 订单关联天数
- if($startAt && $nowDate < $startAt){
- RedisService::clear($cacheKey);
- $this->error = '会议尚未开始,无法签到';
- return false;
- }
- if($endAt && $nowDate > $endAt){
- RedisService::clear($cacheKey);
- $this->error = '会议已结束,无法签到';
- return false;
- }
- $record = MeetingRecordsModel::where(['meeting_id'=> $meetingId,'user_id'=>$userId,'mark'=>1])->first();
- if($record){
- RedisService::clear($cacheKey);
- $this->error = '您已经完成签到';
- return false;
- }
- $otherRecord = MeetingRecordsModel::with(['meeting'])->where(['user_id'=>$userId,'mark'=>1])->first();
- $meeting = isset($otherRecord['meeting'])?$otherRecord['meeting']:[];
- $meetingStartAt = isset($meeting['start_at'])?$meeting['start_at'] : '';
- $meetingEndAt = isset($meeting['end_at'])?$meeting['end_at'] : '';
- if($otherRecord && $meetingStartAt <= $nowDate && $nowDate <= $meetingEndAt){
- RedisService::clear($cacheKey);
- $this->error = '您有未完成的会议,请完成后再签到';
- return false;
- }
- $data = [
- 'user_id' => $userId,
- 'meeting_id' => $meetingId,
- 'order_count' => 0,
- 'order_total' => 0,
- 'expired_at' => date('Y-m-d H:i:s', time() + $orderDay * 86400),
- 'create_time' => time(),
- 'update_time' => time(),
- 'mark' => 1,
- ];
- if(!$id = MeetingRecordsModel::insertGetId($data)){
- RedisService::clear($cacheKey);
- $this->error = '签到失败';
- return false;
- }
- $this->error = '签到成功';
- RedisService::set($cacheKey, $data, 30);
- return ['id' => $id];
- }
- /**
- * 会议报名记录
- * @param $params
- * @param int $pageSize
- * @return array
- */
- public function records($params,$pageSize=20)
- {
- $meetingId = isset($params['meeting_id'])?$params['meeting_id']: 0;
- $list = MeetingRecordsModel::with(['member'])
- ->from('meetings_records as a')
- ->leftJoin('member as b','b.id','=','a.user_id')
- ->where(function($query) use($params){
- $keyword = isset($params['keyword'])? trim($params['keyword']) : '';
- if($keyword){
- $query->where('b.mobile','like',"%{$keyword}%")
- ->orWhere('b.nickname','like',"%{$keyword}%")
- ->orWhere('b.realname','like',"%{$keyword}%");
- }
- })
- ->where('b.id','>',0)
- ->where(['a.meeting_id'=>$meetingId,'a.mark'=>1])
- ->select(['a.*'])
- ->orderBy('a.id','desc')
- ->paginate($pageSize > 0 ? $pageSize : 9999999);
- $list = $list? $list->toArray() :[];
- return [
- 'pageSize'=> $pageSize,
- 'total'=>isset($list['total'])? $list['total'] : 0,
- 'list'=> isset($list['data'])? $list['data'] : []
- ];
- }
- }
|