MeetingService.php 7.4 KB

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