MeetingService.php 7.3 KB

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