input('id/d',0), 'work_type_id' => input('type/d',0), 'content' => input('content','','trim'), 'files' => input('files','','trim'), 'create_time' => $curTime, ]; $result = validate('WorkNews')->check($data,[],''); if(true !== $result){ HelpHander::error(validate('WorkNews')->getError()); } Db::startTrans(); try{ $id = $data['id']; $sdata = [ 'work_type_id' => $data['work_type_id'], 'files' => $data['files'] ]; if($id > 0){ Db::name('work_news_item')->where('work_news_id',$id)->delete(); $sdata['update_time'] = date('Y-m-d H:i:s'); $workReportId = Db::name('work_news')->where('id',$id)->update($sdata); if(!$workReportId){ \exception('提交失败'); } }else{ $sdata['status'] = 1; $sdata['org_id'] = input('orgId/d',0); $sdata['user_id'] = input('userId/d',0); $sdata['create_time'] = date('Y-m-d H:i:s'); $sdata['receiver'] = check_exp_imp(input('receiver','','trim')); $sdata['auditor'] = input('auditor/d',0); if($sdata['auditor'] > 0){ $sdata['audit_status'] = 0; }else{ $sdata['audit_status'] = 1; } $workReportId = Db::name('work_news')->insertGetId($sdata); if(!$workReportId){ \exception('提交失败'); } $id = $workReportId; $type = 0; if($sdata['auditor'] > 0){ $receiver = [$sdata['auditor']]; $type = 1; }else{ // 添加接收人记录及消息 $receiver = explode(',',$sdata['receiver']); } $username = Db::name('user_info')->where('user_id',$sdata['user_id'])->value('name'); foreach ($receiver as $k=>$v){ $d = [ 'org_id' => $sdata['org_id'], 'user_id' => $v, 'create_time' => $curTime, 'is_read' => 0, 'work_news_id' => $workReportId, 'type' => $type ]; $wrrid = Db::name('work_news_receiver')->insertGetId($d); if(!$wrrid){ \exception('提交失败'); } $typename = Db::name('work_news_type')->where('id',$data['work_type_id'])->value('title'); $context = $username.'提交的'.$typename.'工作动态'; $ret = model('Message')->add(3,$wrrid,13,$v,$sdata['org_id'],$context); if(!$ret){ \exception('提交失败'); } } } $content = json_decode($data['content'],true); if($content){ foreach ($content as $k=>$v){ if($v['content'] || $v['date']){ Db::name('work_news_item')->insert([ 'work_news_id' => $id, 'content' => $v['content'], 'date' => empty($v['date'])?null:$v['date'] ]); } } } Db::commit(); }catch (Exception $e){ Db::rollback(); HelpHander::error($e->getMessage()); } return true; } public function info($id){ $info = Db::name('work_news') ->where('id',$id) ->where('status',1) ->field('id,user_id,work_type_id,create_time,files,audit_status') ->find(); if(!$info){ HelpHander::error('数据不存在'); } $info['typeName'] = Db::name('work_news_type')->where('id',$info['work_type_id'])->value('title'); $info['userName'] = Db::name('user_info')->where('user_id',$info['user_id'])->value('name'); $content = Db::name('work_news_item')->where('work_news_id',$id)->field('content,date')->select(); $info['content'] = json_encode($content); $receiver = Db::name('work_news_receiver') ->alias('wnr') ->join('user_info ui','ui.user_id = wnr.user_id') ->where('wnr.work_news_id',$info['id']) ->column('ui.name'); $info['receiver'] = $receiver?implode(',',$receiver):''; return $info; } public function receiveInfo($id){ $info = Db::name('work_news_receiver') ->alias('wrr') ->join('work_news wr','wr.id = wrr.work_news_id') ->where('wrr.id',$id) ->where('wr.status',1) ->field('wr.id,wr.audit_status,wr.user_id,wr.files,wr.work_type_id,wrr.create_time,wrr.is_read,wrr.read_time,wrr.user_id as receiver') ->find(); if(!$info){ HelpHander::error('数据不存在'); } $info['typeName'] = Db::name('work_news_type')->where('id',$info['work_type_id'])->value('title'); $info['userName'] = Db::name('user_info')->where('user_id',$info['user_id'])->value('name'); if($info['is_read'] == 0){ Db::name('work_news_receiver')->where('id',$id)->update(['is_read'=>1,'read_time'=>date('Y-m-d H:i:s')]); $msgid = Db::name('message') ->where('status',0) ->where('type',7) ->where('user_id',$info['receiver']) ->where('bus_type',12) ->where('bus_id',$id) ->value('id'); if($msgid){ model('Message')->updataStatus($msgid,$info['receiver']); // 对应标记为已读消息 } } unset($info['receiver']); $content = Db::name('work_news_item')->where('work_news_id',$info['id'])->field('content,date')->select(); $info['content'] = json_encode($content); $receiver = Db::name('work_news_receiver') ->alias('wnr') ->join('user_info ui','ui.user_id = wnr.user_id') ->where('wnr.work_news_id',$info['id']) ->column('ui.name'); $info['receiver'] = $receiver?implode(',',$receiver):''; return $info; } // 我提交的工作汇报 public function lists($page,$size,$userId,$orgId){ $map[] = ['org_id','=',$orgId]; $map[] = ['user_id','=',$userId]; $map[] = ['status','=',1]; $lists = $this ->where($map) ->page($page,$size) ->order('id desc') ->field('id,user_id,work_type_id,create_time,status,audit_status') ->select(); $lists = $lists?$lists->toArray():[]; foreach ($lists as $k=>$v){ $lists[$k]['typeName'] = Db::name('work_news_type')->where('id',$v['work_type_id'])->value('title'); $lists[$k]['userName'] = Db::name('user_info')->where('user_id',$v['user_id'])->value('name'); //是否可以撤回 $count = Db::name('work_news_receiver')->where('work_news_id',$v['id'])->where('is_read',1)->count(); $lists[$k]['is_read'] = $count > 0?1:0; } $total = $this->where($map)->count(); $data = [ 'total' => $total, 'list' => $lists ]; return $data; } // 我收到的工作汇报 public function receiveList($page,$size,$userId,$orgId){ $map[] = ['wrr.org_id','=',$orgId]; $map[] = ['wrr.user_id','=',$userId]; $map[] = ['wr.status','=',1]; $lists = Db::name('work_news_receiver') ->alias('wrr') ->join('work_news wr','wr.id = wrr.work_news_id') ->where($map) ->field('wr.user_id,wr.audit_status,wrr.type,wr.work_type_id,wrr.create_time,wrr.work_news_id,wrr.is_read,wrr.id') ->page($page,$size) ->order('wrr.id desc') ->select(); $lists = $lists?$lists:[]; foreach ($lists as $k=>$v){ $lists[$k]['typeName'] = Db::name('work_news_type')->where('id',$v['work_type_id'])->value('title'); $lists[$k]['userName'] = Db::name('user_info')->where('user_id',$v['user_id'])->value('name'); } $total = Db::name('work_news_receiver') ->alias('wrr') ->join('work_news wr','wr.id = wrr.work_news_id') ->where($map) ->count(); $data = [ 'total' => $total, 'list' => $lists ]; return $data; } public function myReceiver($userId,$orgId){ $receiver = Db::name('work_news') ->where('org_id',$orgId) ->where('user_id',$userId) ->order('id desc') ->value('receiver'); $receiver = '100006,100135'; // 暂时默认两人 if(!$receiver){ return []; } $receiver = explode(',',$receiver); $lists = Db::name('user') ->alias('u') ->join('user_info ui','ui.user_id = u.id') ->where('u.id','in',$receiver) ->where('u.del',0) ->where('u.enable',1) ->field('u.id,ui.name') ->select(); $lists = $lists?$lists:[]; foreach ($lists as $k=>$v){ $jobs = Db::name('user_job') ->alias('uj') ->join('job j','j.id = uj.job_id') ->where('uj.user_id',$v['id']) ->where('j.del',0) ->where('j.enable',1) ->field('j.*') ->select(); $lists[$k]['listJob'] = $jobs?$jobs:[]; } return $lists; } public function auditor($userId,$orgId){ $receiver = Db::name('work_news') ->where('org_id',$orgId) ->where('user_id',$userId) ->order('id desc') ->value('auditor'); if(!$receiver){ return []; } $lists = Db::name('user') ->alias('u') ->join('user_info ui','ui.user_id = u.id') ->where('u.id',$receiver) ->where('u.del',0) ->where('u.enable',1) ->field('u.id,ui.name') ->select(); $lists = $lists?$lists:[]; foreach ($lists as $k=>$v){ $jobs = Db::name('user_job') ->alias('uj') ->join('job j','j.id = uj.job_id') ->where('uj.user_id',$v['id']) ->where('j.del',0) ->where('j.enable',1) ->field('j.*') ->select(); $lists[$k]['listJob'] = $jobs?$jobs:[]; } return $lists; } // 获取工作汇报未读数量 public function unReadNums($userId,$orgId){ $count = Db::name('work_news_receiver') ->where('is_read',0) ->where('user_id',$userId) ->where('org_id',$orgId) ->count(); return $count; } public function cancel($id,$userId){ $info = Db::name('work_news')->where('id',$id)->where('user_id',$userId)->find(); if(!$info){ HelpHander::error('记录不存在'); } if($info['status'] == 0){ HelpHander::error('记录已撤销'); } //是否可以撤回 $count = Db::name('work_news_receiver')->where('work_news_id',$id)->where('is_read',1)->count(); if($count > 0){ HelpHander::error('上级已阅读,无法撤销'); } Db::startTrans(); try{ $res = Db::name('work_news')->where('id',$id)->update(['status' => 0,'update_time' => date('Y-m-d H:i:s')]); if(!$res){ \exception('操作失败'); } // 删除上级记录 $lists = Db::name('work_news_receiver')->where('work_news_id',$id)->select(); foreach ($lists as $k=>$v){ Db::name('work_news_receiver')->where('id',$v['id'])->delete(); Db::name('message')->where('type',3)->where('bus_id',$v['id'])->where('bus_type',13)->delete(); //原数据type=7 } Db::commit(); }catch (Exception $e){ Db::rollback(); HelpHander::error('操作失败'); } return true; } public function itemLists($page,$size,$userId,$orgId,$content,$workTypeId,$name){ if($content){ $map[] = ['wi.content','like','%'.$content.'%']; $map2[] = ['wi.content','like','%'.$content.'%']; } if($workTypeId > 0){ $map[] = ['wr.work_type_id','=',$workTypeId]; $map2[] = ['wr.work_type_id','=',$workTypeId]; } if($name){ $map[] = ['ui.name','like','%'.$name.'%']; $map2[] = ['ui.name','like','%'.$name.'%']; } $map[] = ['wrr.org_id','=',$orgId]; $map[] = ['wrr.user_id','=',$userId]; $map[] = ['wr.status','=',1]; $map[] = ['wr.audit_status','=',1]; $map2[] = ['wrr.org_id','=',$orgId]; $map2[] = ['wr.user_id','=',$userId]; $map2[] = ['wr.status','=',1]; $map2[] = ['wr.audit_status','=',1]; $lists = Db::name('work_news_item') ->alias('wi') ->join('work_news wr','wr.id = wi.work_news_id') ->join('user_info ui','ui.user_id = wr.user_id') ->join('work_news_receiver wrr','wrr.work_news_id = wr.id','left') ->whereOr([ $map, $map2 ]) ->field('wr.id,wr.user_id,wr.work_type_id,wr.create_time,wi.content,wi.date,wi.id as item_id') ->page($page,$size) ->group('wi.id') ->order('wi.id desc') ->select(); $lists = $lists?$lists:[]; foreach ($lists as $k=>$v){ $lists[$k]['typeName'] = Db::name('work_news_type')->where('id',$v['work_type_id'])->value('title'); $lists[$k]['userName'] = Db::name('user_info')->where('user_id',$v['user_id'])->value('name'); $lists[$k]['label_id'] = 0; $lists[$k]['label_name'] = ''; $label = Db::name('work_news_item_label')->where('user_id',$userId)->where('item_id',$v['item_id'])->find(); if($label){ $lists[$k]['label_id'] = $label['id']; if($label['label'] == 1){ $lists[$k]['label_name'] = '留存'; }else if($label['label'] == 2){ $lists[$k]['label_name'] = '一般'; }else{ $lists[$k]['label_name'] = '重要'; } } } $total = Db::name('work_news_item') ->alias('wi') ->join('work_news wr','wr.id = wi.work_news_id') ->join('user_info ui','ui.user_id = ui.user_id') ->join('work_news_receiver wrr','wrr.work_news_id = wr.id','left') ->whereOr([ $map, $map2 ]) ->group('wi.id') ->count(); $data = [ 'total' => $total, 'list' => $lists ]; return $data; } public function itemAll($userId,$orgId,$content,$workTypeId,$name){ if($content){ $map[] = ['wi.content','like','%'.$content.'%']; $map2[] = ['wi.content','like','%'.$content.'%']; } if($workTypeId > 0){ $map[] = ['wr.work_type_id','=',$workTypeId]; $map2[] = ['wr.work_type_id','=',$workTypeId]; } if($name){ $map[] = ['ui.name','like','%'.$name.'%']; $map2[] = ['ui.name','like','%'.$name.'%']; } $map[] = ['wrr.org_id','=',$orgId]; $map[] = ['wrr.user_id','=',$userId]; $map[] = ['wr.status','=',1]; $map[] = ['wr.audit_status','=',1]; $map2[] = ['wrr.org_id','=',$orgId]; $map2[] = ['wr.user_id','=',$userId]; $map2[] = ['wr.status','=',1]; $map2[] = ['wr.audit_status','=',1]; $lists = Db::name('work_news_item') ->alias('wi') ->join('work_news wr','wr.id = wi.work_news_id') ->join('user_info ui','ui.user_id = ui.user_id') ->join('work_news_receiver wrr','wrr.work_news_id = wr.id','left') ->whereOr([ $map, $map2 ]) ->field('wr.id,wr.user_id,wr.work_type_id,wr.create_time,wi.content,wi.date,wi.id as item_id') ->group('wi.id') ->order('wi.id desc') ->select(); $lists = $lists?$lists:[]; foreach ($lists as $k=>$v){ $lists[$k]['typeName'] = Db::name('work_news_type')->where('id',$v['work_type_id'])->value('title'); $lists[$k]['userName'] = Db::name('user_info')->where('user_id',$v['user_id'])->value('name'); $lists[$k]['label_id'] = 0; $lists[$k]['label_name'] = ''; $label = Db::name('work_news_item_label')->where('user_id',$userId)->where('item_id',$v['item_id'])->find(); if($label){ $lists[$k]['label_id'] = $label['id']; if($label['label'] == 1){ $lists[$k]['label_name'] = '留存'; }else if($label['label'] == 2){ $lists[$k]['label_name'] = '一般'; }else{ $lists[$k]['label_name'] = '重要'; } } } $columns = [ ["title" => "姓名","key" => "userName"], ["title" => "类型","key" => "typeName"], ["title" => "工作内容","key" => "content"], ["title" => "完成时间","key" => "date"], ["title" => "标注","key" => "labelName"], ]; $data = [ 'columns' => $columns, 'list' => $lists ]; return $data; } public function allItemLists($page,$size,$userId,$orgId,$content,$workTypeId,$name,$label){ if($content){ $map[] = ['wi.content','like','%'.$content.'%']; } if($workTypeId > 0){ $map[] = ['wr.work_type_id','=',$workTypeId]; } if($name){ $map[] = ['ui.name','like','%'.$name.'%']; } if($label > 0){ $ids = Db::name('work_news_item_label')->where('label',$label)->where('user_id',$userId)->column('item_id'); if($ids){ $map[] = ['wi.id','in',$ids]; } } $map[] = ['wrr.org_id','=',$orgId]; $map[] = ['wr.status','=',1]; $map[] = ['wr.audit_status','=',1]; $lists = Db::name('work_news_item') ->alias('wi') ->join('work_news wr','wr.id = wi.work_news_id') ->join('user_info ui','ui.user_id = wr.user_id') ->join('work_news_receiver wrr','wrr.work_news_id = wr.id','left') ->where($map) ->field('wr.id,wr.user_id,wr.work_type_id,wr.create_time,wi.content,wi.date,wi.id as item_id') ->page($page,$size) ->order('wi.id desc') ->group('wi.id') ->select(); $lists = $lists?$lists:[]; foreach ($lists as $k=>$v){ $lists[$k]['typeName'] = Db::name('work_news_type')->where('id',$v['work_type_id'])->value('title'); $lists[$k]['userName'] = Db::name('user_info')->where('user_id',$v['user_id'])->value('name'); $lists[$k]['label_id'] = 0; $lists[$k]['label_name'] = ''; $label = Db::name('work_news_item_label')->where('user_id',$userId)->where('item_id',$v['item_id'])->find(); if($label){ $lists[$k]['label_id'] = $label['id']; if($label['label'] == 1){ $lists[$k]['label_name'] = '留存'; }else if($label['label'] == 2){ $lists[$k]['label_name'] = '一般'; }else{ $lists[$k]['label_name'] = '重要'; } } } $total = Db::name('work_news_item') ->alias('wi') ->join('work_news wr','wr.id = wi.work_news_id') ->join('user_info ui','ui.user_id = wr.user_id') ->join('work_news_receiver wrr','wrr.work_news_id = wr.id','left') ->where($map) ->group('wi.id') ->count(); $data = [ 'total' => $total, 'list' => $lists ]; return $data; } public function allItemAll($userId,$orgId,$content,$workTypeId,$name,$label){ if($content){ $map[] = ['wi.content','like','%'.$content.'%']; } if($workTypeId > 0){ $map[] = ['wr.work_type_id','=',$workTypeId]; } if($name){ $map[] = ['ui.name','like','%'.$name.'%']; } if($label > 0){ $ids = Db::name('work_news_item_label')->where('label',$label)->where('user_id',$userId)->column('item_id'); if($ids){ $map[] = ['wi.id','in',$ids]; } } $map[] = ['wrr.org_id','=',$orgId]; $map[] = ['wr.status','=',1]; $map[] = ['wr.audit_status','=',1]; $lists = Db::name('work_news_item') ->alias('wi') ->join('work_news wr','wr.id = wi.work_news_id') ->join('user_info ui','ui.user_id = wr.user_id') ->join('work_news_receiver wrr','wrr.work_news_id = wr.id','left') ->where($map) ->field('wr.id,wr.user_id,wr.work_type_id,wr.create_time,wi.content,wi.date,wi.id as item_id') ->order('wi.id desc') ->group('wi.id') ->select(); $lists = $lists?$lists:[]; foreach ($lists as $k=>$v){ $lists[$k]['typeName'] = Db::name('work_news_type')->where('id',$v['work_type_id'])->value('title'); $lists[$k]['userName'] = Db::name('user_info')->where('user_id',$v['user_id'])->value('name'); $lists[$k]['label_id'] = 0; $lists[$k]['label_name'] = ''; $label = Db::name('work_news_item_label')->where('user_id',$userId)->where('item_id',$v['item_id'])->find(); if($label){ $lists[$k]['label_id'] = $label['id']; if($label['label'] == 1){ $lists[$k]['label_name'] = '留存'; }else if($label['label'] == 2){ $lists[$k]['label_name'] = '一般'; }else{ $lists[$k]['label_name'] = '重要'; } } } $columns = [ ["title" => "姓名","key" => "userName"], ["title" => "类型","key" => "typeName"], ["title" => "工作内容","key" => "content"], ["title" => "完成时间","key" => "date"], ["title" => "标注","key" => "labelName"], ]; $data = [ 'columns' => $columns, 'list' => $lists ]; return $data; } public function addLabel($itemId,$type,$userId){ $data = [ 'item_id' => $itemId, 'label' => $type, 'user_id' => $userId ]; $ret = Db::name('work_news_item_label')->insertGetId($data); if(!$ret){ HelpHander::error('操作失败'); } return true; } public function delLabel($id,$userId){ $ret = Db::name('work_news_item_label')->where('id',$id)->where('user_id',$userId)->delete(); if(!$ret){ HelpHander::error('操作失败'); } return true; } public function unapply($userId){ $lists = Db::name('work_news_receiver') ->alias('wnr') ->join('work_news wn','wn.id = wnr.work_news_id') ->join('user_info ui','ui.user_id = wn.user_id') ->join('work_news_type unt','unt.id = wn.work_type_id') ->where('wnr.user_id',$userId) ->where('wnr.is_read',0) ->where('wn.status',1) ->field('wnr.id,ui.name,unt.title,wnr.create_time') ->select(); $lists = $lists?$lists:[]; foreach ($lists as $k=>$v){ $lists[$k]['title'] = $v['name']."提交的".$v['title']; unset($lists[$k]['name']); } return $lists; } public function agree($id){ $info = Db::name('work_news')->where('id',$id)->find(); if(!$info){ HelpHander::error('记录不存在'); } if($info['audit_status'] != 0){ HelpHander::error('记录已审核'); } $curTime = date('Y-m-d H:i:s'); Db::startTrans(); try{ $res = Db::name('work_news')->where('id',$id)->update(['audit_status' => 1,'audit_time' => $curTime]); if(!$res){ \exception('操作失败'); } $receiver = $info['receiver']?explode(',',$info['receiver']):[]; $username = Db::name('user_info')->where('user_id',$info['user_id'])->value('name'); foreach ($receiver as $k=>$v){ $d = [ 'org_id' => $info['org_id'], 'user_id' => $v, 'create_time' => $curTime, 'is_read' => 0, 'work_news_id' => $id, 'type' => 0 ]; $wrrid = Db::name('work_news_receiver')->insertGetId($d); if(!$wrrid){ \exception('提交失败'); } $typename = Db::name('work_news_type')->where('id',$info['work_type_id'])->value('title'); $context = $username.'提交的'.$typename.'工作动态'; $ret = model('Message')->add(3,$wrrid,13,$v,$info['org_id'],$context); if(!$ret){ \exception('提交失败'); } } Db::commit(); }catch (Exception $e){ Db::rollback(); HelpHander::error('操作失败'); } return true; } public function disagree($id){ $info = Db::name('work_news')->where('id',$id)->find(); if(!$info){ HelpHander::error('记录不存在'); } if($info['audit_status'] != 0){ HelpHander::error('记录已审核'); } $curTime = date('Y-m-d H:i:s'); Db::startTrans(); try{ $res = Db::name('work_news')->where('id',$id)->update(['audit_status' => 2,'audit_time' => $curTime]); if(!$res){ \exception('操作失败'); } Db::commit(); }catch (Exception $e){ Db::rollback(); HelpHander::error('操作失败'); } return true; } }