| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512 |
- <?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])
- ->first();
- $info = $info ? $info->toArray() : [];
- if ($info) {
- $info['fee'] = floatval($info['fee']);
- if ($type == 2) {
- $info['qrcode'] = MpService::make()->getMiniQrcode('pagesSub/pages/meeting/books', $id);
- $info['qrcode'] = $info['qrcode'] ? get_image_url($info['qrcode']) : '';
- } else if ($type == 3) {
- $bookInfo = MeetingRecordsModel::where(['user_id' => $userId, 'meeting_id' => $id, 'mark' => 1])
- ->select(['id', 'is_sign', 'money', 'is_claim', 'status'])
- ->first();
- $bookInfo = $bookInfo ? $bookInfo->toArray() : [];
- $info['is_book'] = $bookInfo ? 1 : 0;
- $info['is_sign'] = isset($bookInfo['is_sign']) ? $bookInfo['is_sign'] : 0;
- $info['is_claim'] = isset($bookInfo['is_claim']) ? $bookInfo['is_claim'] : 0;
- $info['is_fee'] = isset($bookInfo['status']) ? $bookInfo['status'] : 0;
- $info['pay_fee'] = isset($bookInfo['money']) ? floatval($bookInfo['money']) : 0;
- } 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() : [];
- $info['store_count'] = count($info['stores']);
- // 邀请报名人数
- $info['records_count']= (int)MeetingRecordsModel::from('meetings_records as a')
- ->leftJoin('member as b','b.id','=','a.user_id')
- ->where(['a.meeting_id'=>$id,'b.parent_id'=>$userId,'a.status'=>1])
- ->count('a.id');
- /*
- $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['supervisor_count'] =count($info['supervisors']);
- */
- $bookInfo = MeetingRecordsModel::where(['user_id' => $userId, 'meeting_id' => $id, 'mark' => 1])
- ->select(['id', 'is_sign', 'money', 'realname', 'mobile', 'is_claim', 'status'])
- ->first();
- $bookInfo = $bookInfo ? $bookInfo->toArray() : [];
- $info['is_book'] = $bookInfo ? 1 : 0;
- $info['is_sign'] = isset($bookInfo['is_sign']) ? $bookInfo['is_sign'] : 0;
- $info['is_claim'] = isset($bookInfo['is_claim']) ? $bookInfo['is_claim'] : 0;
- $info['is_fee'] = isset($bookInfo['status']) ? $bookInfo['status'] : 0;
- $info['pay_fee'] = isset($bookInfo['money']) ? floatval($bookInfo['money']) : 0;
- $info['book_realname'] = isset($bookInfo['realname']) ? trim($bookInfo['realname']) : '';
- $info['book_mobile'] = isset($bookInfo['mobile']) ? trim($bookInfo['mobile']) : '';
- }
- 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'])
- ->groupBy('meetings.id')
- ->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','province','city','district'])
- ->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;
- $realname = isset($params['realname']) ? trim($params['realname']) : '';
- $mobile = isset($params['mobile']) ? trim($params['mobile']) : '';
- 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'] : '';
- $fee = isset($info['fee']) ? $info['fee'] : 0;
- $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();
- $status = isset($record['status']) ? $record['status'] : 0;
- if ($record) {
- RedisService::clear($cacheKey);
- $this->error = $status == 1 ? '您已经完成报名' : '您已经完成报名,请等候付费确认';
- 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,
- 'realname' => $realname,
- 'mobile' => $mobile,
- 'order_count' => 0,
- 'order_total' => 0,
- 'is_sign' => 2,
- 'is_claim' => 2,
- 'status' => $fee <= 0 ? 1 : 2,
- 'create_time' => time(),
- 'update_time' => time(),
- 'mark' => 1,
- ];
- if (!$id = MeetingRecordsModel::insertGetId($data)) {
- RedisService::clear($cacheKey);
- $this->error = '报名失败';
- return false;
- }
- $this->error = $fee > 0 ? '报名成功,请等候付费确认' : '报名成功';
- RedisService::set($cacheKey, $data, 30);
- RedisService::keyDel("caches:meeting:info_{$meetingId}_{$userId}*");
- return ['id' => $id];
- }
- /**
- * 签到
- * @param $userId
- * @param $params
- * @return array|false
- */
- public function sign($userId, $params)
- {
- $meetingId = isset($params['id']) ? intval($params['id']) : 0;
- if ($meetingId <= 0) {
- $this->error = '会议参数错误,请返回重试~';
- return false;
- }
- $cacheKey = "caches:meetingSign:{$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();
- $recordId = isset($record['id']) ? $record['id'] : 0;
- $isSign = isset($record['is_sign']) ? $record['is_sign'] : 0;
- $bookStatus = isset($record['status']) ? $record['status'] : 0;
- if (empty($record) || $recordId <= 0) {
- RedisService::clear($cacheKey);
- $this->error = '抱歉,请先完成报名';
- return false;
- }
- if ($bookStatus != 1) {
- RedisService::clear($cacheKey);
- $this->error = '抱歉,请先完成报名付费确认';
- return false;
- }
- if ($isSign == 1) {
- 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 = [
- 'is_sign' => 1,
- 'sign_at' => date('Y-m-d H:i:s'),
- 'expired_at' => date('Y-m-d H:i:s', time() + $orderDay * 86400),
- 'create_time' => time(),
- 'update_time' => time(),
- 'mark' => 1,
- ];
- MeetingRecordsModel::where(['id' => $recordId])->update($data);
- $this->error = '签到成功';
- RedisService::set($cacheKey, $data, 30);
- RedisService::keyDel("caches:meeting:info_{$meetingId}_{$userId}*");
- return ['id' => $recordId];
- }
- /**
- * 领取物料
- * @param $userId
- * @param $params
- * @return array|false
- */
- public function claim($userId, $params)
- {
- $meetingId = isset($params['id']) ? intval($params['id']) : 0;
- if ($meetingId <= 0) {
- $this->error = '会议参数错误,请返回重试~';
- return false;
- }
- $cacheKey = "caches:meetingClaim:{$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();
- $recordId = isset($record['id']) ? $record['id'] : 0;
- $isSign = isset($record['is_sign']) ? $record['is_sign'] : 0;
- $isClaim = isset($record['is_claim']) ? $record['is_claim'] : 0;
- $bookStatus = isset($record['status']) ? $record['status'] : 0;
- if (empty($record) || $recordId <= 0) {
- RedisService::clear($cacheKey);
- $this->error = '抱歉,请先完成报名';
- return false;
- }
- if ($bookStatus != 1) {
- RedisService::clear($cacheKey);
- $this->error = '抱歉,请先完成报名付费确认';
- return false;
- }
- if ($isSign != 1) {
- RedisService::clear($cacheKey);
- $this->error = '请先完成签到';
- return false;
- }
- if ($isClaim == 1) {
- RedisService::clear($cacheKey);
- $this->error = '您已经领取过物料';
- return false;
- }
- $data = [
- 'is_claim' => 1,
- 'claim_at' => date('Y-m-d H:i:s'),
- 'create_time' => time(),
- 'update_time' => time(),
- 'mark' => 1,
- ];
- MeetingRecordsModel::where(['id' => $recordId])->update($data);
- $this->error = '领取成功';
- RedisService::set($cacheKey, $data, 30);
- RedisService::keyDel("caches:meeting:info_{$meetingId}_{$userId}*");
- return ['id' => $recordId];
- }
- /**
- * 会议报名签到记录
- * @param $params
- * @param int $pageSize
- * @return array
- */
- public function records($params, $pageSize = 20, $userId = 0)
- {
- $meetingId = isset($params['meeting_id']) ? $params['meeting_id'] : 0;
- $where = ['b.parent_id' => $userId, 'a.meeting_id' => $meetingId, 'a.mark' => 1];
- $query = 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);
- $query1 = clone $query;
- $list = $query1->where($where)->select(['a.*'])
- ->orderBy('a.id', 'desc')
- ->paginate($pageSize > 0 ? $pageSize : 9999999);
- $list = $list ? $list->toArray() : [];
- $query2 = clone $query;
- unset($where['b.parent_id']);
- // var_dump($where);
- $total = $query2->where($where)->count('a.id');
- return [
- 'pageSize' => $pageSize,
- 'count' => isset($list['total']) ? $list['total'] : 0,
- 'total' => $total,
- 'list' => isset($list['data']) ? $list['data'] : []
- ];
- }
- }
|