MeetingService.php 7.6 KB

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