Notice.php 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394
  1. <?php
  2. namespace app\common\model;
  3. use app\hander\HelpHander;
  4. use GatewayClient\Gateway;
  5. use think\Db;
  6. use think\Exception;
  7. use think\Model;
  8. class Notice extends Model
  9. {
  10. public function add($userId,$orgId){
  11. $data = [
  12. 'id' => input('id/d',0),
  13. 'title' => input('title','','trim'),
  14. 'content' => input('content','','trim'),
  15. 'attachment' => input('attachment','','trim'),
  16. 'auths' => input('auths','','trim'),
  17. 'enable' => input('enable/d',0),
  18. 'top' => input('top/d',0),
  19. 'user_id' => $userId,
  20. 'org_id' => $orgId
  21. ];
  22. $logdata = json_encode($data);
  23. $result = validate('Notice')->check($data,[],'');
  24. if(true !== $result){
  25. HelpHander::error(validate('Notice')->getError());
  26. }
  27. $id = $data['id'];
  28. unset($data['id']);
  29. $this->startTrans();
  30. try{
  31. if($id > 0){
  32. unset($data['user_id']);
  33. $data['update_time'] = date('Y-m-d H:i:s');
  34. $ret = $this->allowField(true)->save($data,['id'=>$id]);
  35. if(!$ret){
  36. \exception('操作失败');
  37. }
  38. }else{
  39. $data['create_time'] = date('Y-m-d H:i:s');
  40. $ret = $this->allowField(true)->save($data);
  41. if(!$ret){
  42. \exception('操作失败');
  43. }
  44. $id = $this->id;
  45. }
  46. // 检查是否已发布过
  47. $ret = Db::name('notice_log')->where('notice_id',$id)->find();
  48. if($data['enable'] == 1 && !$ret){ // 创建发布记录
  49. $res = $this->createNoticeLog($id,$data['title'],$orgId,$data['auths']);
  50. if(!$res){
  51. \exception('记录添加失败');
  52. }
  53. }
  54. $this->commit();
  55. }catch (Exception $e){
  56. $this->rollback();
  57. HelpHander::error('操作失败');
  58. }
  59. if($id > 0){
  60. $content = '修改通知公告';
  61. }else{
  62. $content = '添加通知公告';
  63. }
  64. model('ActionLog')->add(17,$content,0,$logdata);
  65. return true;
  66. }
  67. /**
  68. * 创建公告记录
  69. * @param $noticeId
  70. * @param $orgId
  71. * @param $auths
  72. * @return bool
  73. * @throws \Exception
  74. */
  75. private function createNoticeLog($noticeId,$title,$orgId,$auths){
  76. try{
  77. $userids = model('User')->getUserByAuths($orgId,$auths);
  78. $arr = [];
  79. foreach ($userids as $k=>$v){
  80. $arr[] = [
  81. 'user_id' => $v,
  82. 'notice_id' => $noticeId,
  83. 'status' => 0
  84. ];
  85. if(count($arr) == 500){
  86. $ret = Db::name('notice_log')->insertAll($arr);
  87. if($ret != count($arr)){
  88. \exception('记录添加失败');
  89. }
  90. $arr = [];
  91. }
  92. // TODO::当人数过多时需要优化,推送及短信都是比较耗时的
  93. $res = model('Message')->add(1,$noticeId,1,$v,$orgId,$title);
  94. if(!$res){
  95. \exception('消息发送失败');
  96. }
  97. }
  98. if($arr){ // 不足500条的处理
  99. $ret = Db::name('notice_log')->insertAll($arr);
  100. if($ret != count($arr)){
  101. \exception('记录添加失败');
  102. }
  103. }
  104. }catch (Exception $e){
  105. return false;
  106. }
  107. return true;
  108. }
  109. private function createNoticeLog_old($noticeId,$orgId,$auths){
  110. try{
  111. $userids = model('User')->getUserByAuths($orgId,$auths);
  112. $arr = [];
  113. $message = [];
  114. foreach ($userids as $k=>$v){
  115. $arr[] = [
  116. 'user_id' => $v,
  117. 'notice_id' => $noticeId,
  118. 'status' => 0
  119. ];
  120. if(count($arr) == 500){
  121. $ret = Db::name('notice_log')->insertAll($arr);
  122. if($ret != count($arr)){
  123. \exception('记录添加失败');
  124. }
  125. }
  126. }
  127. if($arr){
  128. $ret = Db::name('notice_log')->insertAll($arr);
  129. if($ret != count($arr)){
  130. \exception('记录添加失败');
  131. }
  132. }
  133. }catch (Exception $e){
  134. return false;
  135. }
  136. return true;
  137. }
  138. public function info($id){
  139. $info = $this->where('id',$id)->find();
  140. if(!$info){
  141. HelpHander::error('数据不存在');
  142. }
  143. return $info->toArray();
  144. }
  145. public function lists($page,$size,$orgId,$title,$enable,$userId,$type){ // type 0=全部 1=自己
  146. $map[] = ['org_id','=',$orgId];
  147. if($title != ''){
  148. $map[] = ['title','like','%'.$title.'%'];
  149. }
  150. if($type == 1){
  151. $map[] = ['user_id','=',$userId];
  152. }
  153. if(in_array($enable,[0,1])){
  154. $map[] = ['enable','=',$enable];
  155. }
  156. $lists = $this
  157. ->where($map)
  158. ->page($page,$size)
  159. ->order('id desc')
  160. ->select();
  161. $total = $this->where($map)->count();
  162. $data = [
  163. 'total' => $total,
  164. 'list' => $lists?$lists->toArray():[]
  165. ];
  166. return $data;
  167. }
  168. /**
  169. * 获取用户公告
  170. * @param $page
  171. * @param $size
  172. * @param $userId
  173. * @param $status
  174. * @return array
  175. */
  176. public function userLists($page,$size,$userId,$status){
  177. $map[] = ['nl.user_id','=',$userId];
  178. if(in_array($status,[0,1])){
  179. $map[] = ['nl.status','=',$status];
  180. }
  181. $lists = Db::name('notice_log')
  182. ->alias('nl')
  183. ->join('notice n','n.id = nl.notice_id')
  184. ->where($map)
  185. ->page($page,$size)
  186. ->order('n.top desc,nl.id desc')
  187. ->field('nl.status,n.*')
  188. ->select();
  189. $total = Db::name('notice_log')->alias('nl')->join('notice n','n.id = nl.notice_id')->where($map)->count();
  190. $data = [
  191. 'total' => $total,
  192. 'list' => $lists?$lists:[]
  193. ];
  194. return $data;
  195. }
  196. public function recentNoticeList($userId){
  197. $map[] = ['nl.user_id','=',$userId];
  198. $lists = Db::name('notice_log')
  199. ->alias('nl')
  200. ->join('notice n','n.id = nl.notice_id')
  201. ->where($map)
  202. ->order('n.top desc,nl.id desc')
  203. ->field('n.top,nl.status,n.id,n.title,n.create_time')
  204. ->limit(5)
  205. ->select();
  206. return $lists?$lists:[];
  207. }
  208. /**
  209. * 根据公告id获取查看结果
  210. * @param $page
  211. * @param $size
  212. * @param $noticeId
  213. * @param $status
  214. * @return array
  215. */
  216. public function noticeLog($page,$size,$noticeId,$status){
  217. $map[] = ['nl.notice_id','=',$noticeId];
  218. if(in_array($status,[0,1])){
  219. $map[] = ['nl.status','=',$status];
  220. }
  221. $lists = Db::name('notice_log')
  222. ->alias('nl')
  223. ->join('user_info u','u.user_id = nl.user_id')
  224. ->where($map)
  225. ->page($page,$size)
  226. ->order('nl.id desc')
  227. ->field('nl.id,nl.user_id,nl.read_time,nl.status,u.name as user_name')
  228. ->select();
  229. $total = Db::name('notice_log')->alias('nl')->join('user_info u','u.user_id = nl.user_id')->where($map)->count();
  230. $data = [
  231. 'total' => $total,
  232. 'list' => $lists?$lists:[]
  233. ];
  234. return $data;
  235. }
  236. public function del($id){
  237. Db::startTrans();
  238. try{
  239. $ret = $this->where('id',$id)->delete();
  240. if(!$ret){
  241. \exception('操作失败');
  242. }
  243. // 删除记录
  244. Db::name('notice_log')->where('notice_id',$id)->delete();
  245. // 删除消息
  246. Db::name('message')->where('bus_id',$id)->where('type',1)->delete();
  247. $logdata = json_encode(['id' => $id]);
  248. model('ActionLog')->add(17,'删除通知公告',0,$logdata);
  249. Db::commit();
  250. }catch (Exception $e){
  251. Db::rollback();
  252. HelpHander::error('操作失败');
  253. }
  254. return true;
  255. }
  256. // 未读状态改为已读
  257. public function changeStatus($id,$userId){
  258. Db::startTrans();
  259. try{
  260. Db::name('message')
  261. ->where('user_id',$userId)
  262. ->where('bus_type',1)
  263. ->where('type',1)
  264. ->where('bus_id',$id)
  265. ->setField('status',1);
  266. $ret = Db::name('notice_log')
  267. ->where('notice_id',$id)
  268. ->where('user_id',$userId)
  269. ->update(['status'=>1,'read_time'=>date('Y-m-d H:i:s')]);
  270. if(!$ret){
  271. \exception('操作失败');
  272. }
  273. Db::commit();
  274. }catch (Exception $e){
  275. Db::rollback();
  276. HelpHander::error('操作失败');
  277. }
  278. // websocket推送
  279. Gateway::$registerAddress = config('app.gateway_register_ip');
  280. if(Gateway::isUidOnline($userId)){ // 在线发送消息
  281. Gateway::sendToUid($userId, json_encode(['type'=>'msgcount','data'=>[],'msg'=>'']));
  282. }
  283. return true;
  284. }
  285. // 全部已读
  286. public function setAllNoticeRead($userId,$orgId){
  287. $ids = Db::name('notice_log')
  288. ->alias('nl')
  289. ->join('notice n','n.id = nl.notice_id')
  290. ->where('nl.user_id',$userId)
  291. ->where('n.org_id',$orgId)
  292. ->where('nl.status',0)
  293. ->column('n.id');
  294. if($ids){
  295. Db::startTrans();
  296. try{
  297. Db::name('notice_log')
  298. ->where('user_id',$userId)
  299. ->where('notice_id','in',$ids)
  300. ->update(['status'=>1,'read_time'=>date('Y-m-d H:i:s')]);
  301. Db::name('message')
  302. ->where('org_id',$orgId)
  303. ->where('user_id',$userId)
  304. ->where('status',0)
  305. ->where('bus_type',1)
  306. ->where('bus_id','in',$ids)
  307. ->where('type',1)
  308. ->setField('status',1);
  309. Db::commit();
  310. }catch (Exception $e){
  311. Db::rollback();
  312. HelpHander::error('操作失败');
  313. }
  314. }
  315. // websocket推送
  316. Gateway::$registerAddress = config('app.gateway_register_ip');
  317. if(Gateway::isUidOnline($userId)){ // 在线发送消息
  318. Gateway::sendToUid($userId, json_encode(['type'=>'msgcount','data'=>[],'msg'=>'']));
  319. }
  320. return true;
  321. }
  322. public function queryAppDetail($id){
  323. $info = Db::name('notice')
  324. ->where('id',$id)
  325. ->where('enable',1)
  326. ->field('id,title,content,org_id,create_time,attachment')
  327. ->find();
  328. if(!$info){
  329. HelpHander::error('记录不存在');
  330. }
  331. $info['orgName'] = Db::name('org')->where('id',$info['org_id'])->value('name');
  332. $info['readNum'] = Db::name('notice_log')->where('notice_id',$id)->where('status',1)->count();
  333. $info['unReadNum'] = Db::name('notice_log')->where('notice_id',$id)->where('status',0)->count();
  334. unset($info['org_id']);
  335. return $info;
  336. }
  337. // 用户最新一条公告数据
  338. public function queryUserLastNotice($userId,$orgId){
  339. $info = Db::name('notice')
  340. ->alias('n')
  341. ->join('notice_log nl','nl.notice_id = n.id')
  342. ->where('nl.user_id',$userId)
  343. ->where('n.org_id',$orgId)
  344. ->where('n.enable',1)
  345. ->field('n.id,n.title,n.content,n.org_id,n.create_time,n.attachment')
  346. ->find();
  347. if($info){
  348. $info['readNum'] = Db::name('notice_log')->where('notice_id',$info['id'])->where('status',1)->count();
  349. $info['unReadNum'] = Db::name('notice_log')->where('notice_id',$info['id'])->where('status',0)->count();
  350. }
  351. return $info;
  352. }
  353. }