123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394 |
- <?php
- namespace app\common\model;
- use app\hander\HelpHander;
- use GatewayClient\Gateway;
- use think\Db;
- use think\Exception;
- use think\Model;
- class Notice extends Model
- {
- public function add($userId,$orgId){
- $data = [
- 'id' => input('id/d',0),
- 'title' => input('title','','trim'),
- 'content' => input('content','','trim'),
- 'attachment' => input('attachment','','trim'),
- 'auths' => input('auths','','trim'),
- 'enable' => input('enable/d',0),
- 'top' => input('top/d',0),
- 'user_id' => $userId,
- 'org_id' => $orgId
- ];
- $logdata = json_encode($data);
- $result = validate('Notice')->check($data,[],'');
- if(true !== $result){
- HelpHander::error(validate('Notice')->getError());
- }
- $id = $data['id'];
- unset($data['id']);
- $this->startTrans();
- try{
- if($id > 0){
- unset($data['user_id']);
- $data['update_time'] = date('Y-m-d H:i:s');
- $ret = $this->allowField(true)->save($data,['id'=>$id]);
- if(!$ret){
- \exception('操作失败');
- }
- }else{
- $data['create_time'] = date('Y-m-d H:i:s');
- $ret = $this->allowField(true)->save($data);
- if(!$ret){
- \exception('操作失败');
- }
- $id = $this->id;
- }
- // 检查是否已发布过
- $ret = Db::name('notice_log')->where('notice_id',$id)->find();
- if($data['enable'] == 1 && !$ret){ // 创建发布记录
- $res = $this->createNoticeLog($id,$data['title'],$orgId,$data['auths']);
- if(!$res){
- \exception('记录添加失败');
- }
- }
- $this->commit();
- }catch (Exception $e){
- $this->rollback();
- HelpHander::error('操作失败');
- }
- if($id > 0){
- $content = '修改通知公告';
- }else{
- $content = '添加通知公告';
- }
- model('ActionLog')->add(17,$content,0,$logdata);
- return true;
- }
- /**
- * 创建公告记录
- * @param $noticeId
- * @param $orgId
- * @param $auths
- * @return bool
- * @throws \Exception
- */
- private function createNoticeLog($noticeId,$title,$orgId,$auths){
- try{
- $userids = model('User')->getUserByAuths($orgId,$auths);
- $arr = [];
- foreach ($userids as $k=>$v){
- $arr[] = [
- 'user_id' => $v,
- 'notice_id' => $noticeId,
- 'status' => 0
- ];
- if(count($arr) == 500){
- $ret = Db::name('notice_log')->insertAll($arr);
- if($ret != count($arr)){
- \exception('记录添加失败');
- }
- $arr = [];
- }
- // TODO::当人数过多时需要优化,推送及短信都是比较耗时的
- $res = model('Message')->add(1,$noticeId,1,$v,$orgId,$title);
- if(!$res){
- \exception('消息发送失败');
- }
- }
- if($arr){ // 不足500条的处理
- $ret = Db::name('notice_log')->insertAll($arr);
- if($ret != count($arr)){
- \exception('记录添加失败');
- }
- }
- }catch (Exception $e){
- return false;
- }
- return true;
- }
- private function createNoticeLog_old($noticeId,$orgId,$auths){
- try{
- $userids = model('User')->getUserByAuths($orgId,$auths);
- $arr = [];
- $message = [];
- foreach ($userids as $k=>$v){
- $arr[] = [
- 'user_id' => $v,
- 'notice_id' => $noticeId,
- 'status' => 0
- ];
- if(count($arr) == 500){
- $ret = Db::name('notice_log')->insertAll($arr);
- if($ret != count($arr)){
- \exception('记录添加失败');
- }
- }
- }
- if($arr){
- $ret = Db::name('notice_log')->insertAll($arr);
- if($ret != count($arr)){
- \exception('记录添加失败');
- }
- }
- }catch (Exception $e){
- return false;
- }
- return true;
- }
- public function info($id){
- $info = $this->where('id',$id)->find();
- if(!$info){
- HelpHander::error('数据不存在');
- }
- return $info->toArray();
- }
- public function lists($page,$size,$orgId,$title,$enable,$userId,$type){ // type 0=全部 1=自己
- $map[] = ['org_id','=',$orgId];
- if($title != ''){
- $map[] = ['title','like','%'.$title.'%'];
- }
- if($type == 1){
- $map[] = ['user_id','=',$userId];
- }
- if(in_array($enable,[0,1])){
- $map[] = ['enable','=',$enable];
- }
- $lists = $this
- ->where($map)
- ->page($page,$size)
- ->order('id desc')
- ->select();
- $total = $this->where($map)->count();
- $data = [
- 'total' => $total,
- 'list' => $lists?$lists->toArray():[]
- ];
- return $data;
- }
- /**
- * 获取用户公告
- * @param $page
- * @param $size
- * @param $userId
- * @param $status
- * @return array
- */
- public function userLists($page,$size,$userId,$status){
- $map[] = ['nl.user_id','=',$userId];
- if(in_array($status,[0,1])){
- $map[] = ['nl.status','=',$status];
- }
- $lists = Db::name('notice_log')
- ->alias('nl')
- ->join('notice n','n.id = nl.notice_id')
- ->where($map)
- ->page($page,$size)
- ->order('n.top desc,nl.id desc')
- ->field('nl.status,n.*')
- ->select();
- $total = Db::name('notice_log')->alias('nl')->join('notice n','n.id = nl.notice_id')->where($map)->count();
- $data = [
- 'total' => $total,
- 'list' => $lists?$lists:[]
- ];
- return $data;
- }
- public function recentNoticeList($userId){
- $map[] = ['nl.user_id','=',$userId];
- $lists = Db::name('notice_log')
- ->alias('nl')
- ->join('notice n','n.id = nl.notice_id')
- ->where($map)
- ->order('n.top desc,nl.id desc')
- ->field('n.top,nl.status,n.id,n.title,n.create_time')
- ->limit(5)
- ->select();
- return $lists?$lists:[];
- }
- /**
- * 根据公告id获取查看结果
- * @param $page
- * @param $size
- * @param $noticeId
- * @param $status
- * @return array
- */
- public function noticeLog($page,$size,$noticeId,$status){
- $map[] = ['nl.notice_id','=',$noticeId];
- if(in_array($status,[0,1])){
- $map[] = ['nl.status','=',$status];
- }
- $lists = Db::name('notice_log')
- ->alias('nl')
- ->join('user_info u','u.user_id = nl.user_id')
- ->where($map)
- ->page($page,$size)
- ->order('nl.id desc')
- ->field('nl.id,nl.user_id,nl.read_time,nl.status,u.name as user_name')
- ->select();
- $total = Db::name('notice_log')->alias('nl')->join('user_info u','u.user_id = nl.user_id')->where($map)->count();
- $data = [
- 'total' => $total,
- 'list' => $lists?$lists:[]
- ];
- return $data;
- }
- public function del($id){
- Db::startTrans();
- try{
- $ret = $this->where('id',$id)->delete();
- if(!$ret){
- \exception('操作失败');
- }
- // 删除记录
- Db::name('notice_log')->where('notice_id',$id)->delete();
- // 删除消息
- Db::name('message')->where('bus_id',$id)->where('type',1)->delete();
- $logdata = json_encode(['id' => $id]);
- model('ActionLog')->add(17,'删除通知公告',0,$logdata);
- Db::commit();
- }catch (Exception $e){
- Db::rollback();
- HelpHander::error('操作失败');
- }
- return true;
- }
- // 未读状态改为已读
- public function changeStatus($id,$userId){
- Db::startTrans();
- try{
- Db::name('message')
- ->where('user_id',$userId)
- ->where('bus_type',1)
- ->where('type',1)
- ->where('bus_id',$id)
- ->setField('status',1);
- $ret = Db::name('notice_log')
- ->where('notice_id',$id)
- ->where('user_id',$userId)
- ->update(['status'=>1,'read_time'=>date('Y-m-d H:i:s')]);
- if(!$ret){
- \exception('操作失败');
- }
- Db::commit();
- }catch (Exception $e){
- Db::rollback();
- HelpHander::error('操作失败');
- }
- // websocket推送
- Gateway::$registerAddress = config('app.gateway_register_ip');
- if(Gateway::isUidOnline($userId)){ // 在线发送消息
- Gateway::sendToUid($userId, json_encode(['type'=>'msgcount','data'=>[],'msg'=>'']));
- }
- return true;
- }
- // 全部已读
- public function setAllNoticeRead($userId,$orgId){
- $ids = Db::name('notice_log')
- ->alias('nl')
- ->join('notice n','n.id = nl.notice_id')
- ->where('nl.user_id',$userId)
- ->where('n.org_id',$orgId)
- ->where('nl.status',0)
- ->column('n.id');
- if($ids){
- Db::startTrans();
- try{
- Db::name('notice_log')
- ->where('user_id',$userId)
- ->where('notice_id','in',$ids)
- ->update(['status'=>1,'read_time'=>date('Y-m-d H:i:s')]);
- Db::name('message')
- ->where('org_id',$orgId)
- ->where('user_id',$userId)
- ->where('status',0)
- ->where('bus_type',1)
- ->where('bus_id','in',$ids)
- ->where('type',1)
- ->setField('status',1);
- Db::commit();
- }catch (Exception $e){
- Db::rollback();
- HelpHander::error('操作失败');
- }
- }
- // websocket推送
- Gateway::$registerAddress = config('app.gateway_register_ip');
- if(Gateway::isUidOnline($userId)){ // 在线发送消息
- Gateway::sendToUid($userId, json_encode(['type'=>'msgcount','data'=>[],'msg'=>'']));
- }
- return true;
- }
- public function queryAppDetail($id){
- $info = Db::name('notice')
- ->where('id',$id)
- ->where('enable',1)
- ->field('id,title,content,org_id,create_time,attachment')
- ->find();
- if(!$info){
- HelpHander::error('记录不存在');
- }
- $info['orgName'] = Db::name('org')->where('id',$info['org_id'])->value('name');
- $info['readNum'] = Db::name('notice_log')->where('notice_id',$id)->where('status',1)->count();
- $info['unReadNum'] = Db::name('notice_log')->where('notice_id',$id)->where('status',0)->count();
- unset($info['org_id']);
- return $info;
- }
- // 用户最新一条公告数据
- public function queryUserLastNotice($userId,$orgId){
- $info = Db::name('notice')
- ->alias('n')
- ->join('notice_log nl','nl.notice_id = n.id')
- ->where('nl.user_id',$userId)
- ->where('n.org_id',$orgId)
- ->where('n.enable',1)
- ->field('n.id,n.title,n.content,n.org_id,n.create_time,n.attachment')
- ->find();
- if($info){
- $info['readNum'] = Db::name('notice_log')->where('notice_id',$info['id'])->where('status',1)->count();
- $info['unReadNum'] = Db::name('notice_log')->where('notice_id',$info['id'])->where('status',0)->count();
- }
- return $info;
- }
- }
|