MeetingService.php 7.5 KB

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