123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546 |
- <?php
- namespace app\common\model;
- use app\hander\HelpHander;
- use think\Db;
- use think\Exception;
- use think\Model;
- class Important extends Model
- {
- public function add(){
- $data = [
- 'id' => input('id/d',0),
- 'title' => input('title','','trim'),
- 'source' => input('source','','trim'),
- 'proof' => input('proof','','trim'),
- 'end_time' => input('endTime','','trim'),
- 'org_id' => input('orgId/d',0),
- 'superior_opinion' => input('superiorOpinion','','trim'),
- 'superior_opinion_files' => input('superiorOpinionFiles','','trim'),
- 'file' => input('file','','trim'),
- // 'bus_type' => input('busType/d',0),
- // 'bus_id' => input('busId/d',0),
- 'deps' => input('deps','','trim'),
- 'cbusers' => input('cbusers','','trim'),
- 'degree' => input('degree/d',0),
- 'transfer' => input('transfer/d',0),
- 'transfer_id' => input('transferId/d',0),
- 'remark' => input('remark','','trim')
- ];
- if($data['id'] > 0){
- $result = validate('Important')->check($data,[],'edit');
- if(true !== $result){
- HelpHander::error(validate('Important')->getError());
- }
- unset($data['transfer']);
- unset($data['transfer_id']);
- }else{
- $result = validate('Important')->check($data,[],'add');
- if(true !== $result){
- HelpHander::error(validate('Important')->getError());
- }
- if($data['transfer_id'] > 0){
- $iid = Db::name('important')->where('transfer_id',$data['transfer_id'])->where('del',0)->value('id');
- if($iid){
- HelpHander::error('该文件已转重要事项');
- }
- }
- }
- $id = $data['id'];
- unset($data['id']);
- Db::startTrans();
- try{
- if($id > 0){
- $data['update_time'] = date('Y-m-d H:i:s');
- $ret = $this->allowField(true)->save($data,['id'=>$id]);
- if(!$ret){
- \exception('操作失败');
- }
- $cbusers = json_decode($data['cbusers'],true);
- foreach ($cbusers as $k=>$v){
- $sdata = [
- 'user_id' => $v['id'],
- 'content' => '',
- 'end_time' => $data['end_time'],
- 'org_id' => $data['org_id'],
- 'pid' => 0,
- 'status' => 1, // 默认已同意
- 'important_id' => $id,
- 'create_time' => date('Y-m-d H:i:s')
- ];
- // 检查记录是否已存在
- $ret = Db::name('important_record')
- ->where('user_id',$v['id'])
- ->where('important_id',$id)
- ->where('pid',0)
- ->where('org_id',$sdata['org_id'])
- ->find();
- if(!$ret){ // 不存在新增
- $rid = Db::name('important_record')->insertGetId($sdata);
- if(!$rid){
- \exception('操作失败');
- }
- model('Message')->add(3,$rid,14,$sdata['user_id'],$sdata['org_id'],'有一项重要事项需要您办理.');
- }
- }
- // 添加关联关系
- Db::name('important_user')->where('important_id',$id)->delete();
- }else{
- $data['create_time'] = date('Y-m-d H:i:s');
- // 获取最新的一个编号
- $data['sn'] = $this->createNewSn($data['org_id']);
- $ret = $this->allowField(true)->save($data);
- if(!$ret){
- \exception('操作失败');
- }
- $id = $this->id;
- $cbusers = json_decode($data['cbusers'],true);
- foreach ($cbusers as $k=>$v){
- $sdata = [
- 'user_id' => $v['id'],
- 'content' => '',
- 'end_time' => $data['end_time'],
- 'org_id' => $data['org_id'],
- 'pid' => 0,
- 'status' => 1, // 默认已同意
- 'important_id' => $this->id,
- 'create_time' => date('Y-m-d H:i:s')
- ];
- $rid = Db::name('important_record')->insertGetId($sdata);
- if(!$rid){
- \exception('操作失败');
- }
- model('Message')->add(3,$rid,14,$sdata['user_id'],$sdata['org_id'],'有一项重要事项需要您办理.');
- }
- }
- // 添加关联关系
- $cbarr = [];
- foreach ($cbusers as $k=>$v){
- $cbarr[] = [
- 'type' => 2,
- 'bus_id' => $v['id'],
- 'important_id' => $id,
- ];
- }
- if($data['deps']){
- $deps = json_decode($data['deps'],true);
- $deps = $deps?$deps:[];
- foreach ($deps as $k=>$v){
- $cbarr[] = [
- 'type' => 0,
- 'bus_id' => $v['id'],
- 'important_id' => $id,
- ];
- }
- }
- if($cbarr){
- Db::name('important_user')->insertAll($cbarr);
- }
- Db::commit();
- }catch (Exception $e){
- Db::rollback();
- HelpHander::error($e->getMessage());
- }
- return true;
- }
- private function createNewSn($orgId){
- $newsn = '';
- $sn = Db::name('important')->where('del',0)->where('org_id',$orgId)->order('id desc')->value('sn');
- $curY = date('y');
- if(!$sn){
- $newsn = $curY.'0001';
- }else{
- $y = substr($sn,0,2);
- if($curY != $y){
- $newsn = $curY.'0001';
- }else{
- $newsn = $sn+1;
- }
- }
- return $newsn;
- }
- public function info($id){
- $info = $this->where('id',$id)->find();
- if(!$info){
- HelpHander::error('数据不存在');
- }
- $info = $info->toArray();
- $cbusers = $info['cbusers']?json_decode($info['cbusers'],true):[];
- $names = [];
- foreach ($cbusers as $kk=>$vv){
- if($vv['type'] == 0){ // 部门
- $names[] = Db::name('dep')->where('id',$vv['id'])->value('name');
- }else{ // 用户
- $names[] = Db::name('user_info')->where('user_id',$vv['id'])->value('name');
- }
- }
- $info['bus_name'] = implode(',',$names);
- $deps = $info['deps']?json_decode($info['deps'],true):[];
- $dnames = [];
- foreach ($deps as $kk=>$vv){
- if($vv['type'] == 0){ // 部门
- $dnames[] = Db::name('dep')->where('id',$vv['id'])->value('name');
- }else{ // 用户
- $dnames[] = Db::name('user_info')->where('user_id',$vv['id'])->value('name');
- }
- }
- $info['deps_name'] = implode(',',$dnames);
- $info['file'] = $info['file']?json_decode($info['file'],true):[];
- $info['superior_opinion_files'] = $info['superior_opinion_files']?json_decode($info['superior_opinion_files'],true):[];
- // 办理进展
- $anns = Db::name('important_record_annotation')
- ->alias('ira')
- ->join('important_record ir','ir.id = ira.important_record_id')
- ->join('user_info ui','ui.user_id = ira.user_id')
- ->where('ir.important_id',$id)
- ->where('ira.del',0)
- ->field('ira.content,ui.name,ira.files')
- ->order('ira.id asc')
- ->select();
- $info['anns'] = $anns?$anns:[];
- return $info;
- }
- public function lists($orgId){
- $page = input('page/d',1);
- $size = input('size/d',10);
- $title = input('title','','trim');
- $enable = input('enable/d',-1);
- $transfer = input('transfer/d',-1);
- $degree = input('degree/d',-1);
- $dep = input('dep','','trim');
- $user = input('user','','trim');
- $source = input('source','','trim');
- $proof = input('proof','','trim');
- $sn = input('sn','','trim');
- $prop = input('prop','','trim');
- $order = input('order','','trim');
- if($prop == 'transferId'){
- $prop = 'transfer_id';
- }else if($prop == 'endTime'){
- $prop = 'end_time';
- }else if($prop == 'createTime'){
- $prop = 'create_time';
- }else if($prop == 'enable'){
- $prop = 'status';
- }
- if($prop && $order == 'ascending'){
- $orderby = $prop.' asc, id desc';
- }else if($prop && $order == 'descending'){
- $orderby = $prop.' desc, id desc';
- }else{
- $orderby = 'id desc';
- }
- $map[] = ['del','=',0];
- $map[] = ['org_id','=',$orgId];
- if($title != ''){
- $map[] = ['title','like','%'.$title.'%'];
- }
- if($source != ''){
- $map[] = ['source','like','%'.$source.'%'];
- }
- if($proof != ''){
- $map[] = ['proof','like','%'.$proof.'%'];
- }
- if($sn != ''){
- $map[] = ['sn','like','%'.$sn.'%'];
- }
- if($enable >= 0){
- $map[] = ['status','=',$enable];
- }
- if($transfer >= 0){
- $map[] = ['transfer','=',$transfer];
- }
- if($degree >= 0){
- $map[] = ['degree','=',$degree];
- }
- $ids = [];
- if($dep){
- $ids1 = Db::name('important_user')
- ->alias('iu')
- ->join('dep d','d.id = iu.bus_id')
- ->where('iu.type',0)
- ->where('d.name','like','%'.$dep.'%')
- ->column('iu.important_id');
- $ids1 = $ids1?$ids1:[];
- $ids = array_merge($ids,$ids1);
- }
- if($user){
- $ids2 = Db::name('important_user')
- ->alias('iu')
- ->join('user_info d','d.user_id = iu.bus_id')
- ->where('iu.type',2)
- ->where('d.name','like','%'.$user.'%')
- ->column('iu.important_id');
- $ids2 = $ids2?$ids2:[];
- $ids = array_merge($ids,$ids2);
- }
- $ids = array_unique($ids);
- if($ids){
- $map[] = ['id','in',$ids];
- }
- $lists = $this
- ->where($map)
- ->page($page,$size)
- ->order($orderby)
- ->select();
- $lists = $lists?$lists->toArray():[];
- foreach ($lists as $k=>$v){
- $cbusers = $v['cbusers']?json_decode($v['cbusers'],true):[];
- $names = [];
- foreach ($cbusers as $kk=>$vv){
- if($vv['type'] == 0){ // 部门
- $names[] = Db::name('dep')->where('id',$vv['id'])->value('name');
- }else{ // 用户
- $names[] = Db::name('user_info')->where('user_id',$vv['id'])->value('name');
- }
- }
- $lists[$k]['bus_name'] = implode(',',$names);
- $curBus = Db::name('important_record')
- ->alias('ir')
- ->join('user_info ui','ui.user_id = ir.user_id')
- ->where('ir.important_id',$v['id'])
- ->whereIn('ir.status',[0,1])
- ->column('ui.name');
- $lists[$k]['cur_bus_name'] = $curBus?implode(',',$curBus):'';
- $deps = $v['deps']?json_decode($v['deps'],true):[];
- $dnames = [];
- foreach ($deps as $kk=>$vv){
- if($vv['type'] == 0){ // 部门
- $dnames[] = Db::name('dep')->where('id',$vv['id'])->value('name');
- }else{ // 用户
- $dnames[] = Db::name('user_info')->where('user_id',$vv['id'])->value('name');
- }
- }
- $lists[$k]['deps_name'] = implode(',',$dnames);
- }
- $total = $this->where($map)->count();
- $data = [
- 'total' => $total,
- 'list' => $lists
- ];
- return $data;
- }
- public function listExport($orgId){
- $title = input('title','','trim');
- $enable = input('enable/d',-1);
- $transfer = input('transfer/d',-1);
- $degree = input('degree/d',-1);
- $dep = input('dep','','trim');
- $user = input('user','','trim');
- $source = input('source','','trim');
- $proof = input('proof','','trim');
- $sn = input('sn','','trim');
- $prop = input('prop','','trim');
- $order = input('order','','trim');
- if($prop && $order == 'ascending'){
- $orderby = $prop.' asc, id desc';
- }else if($prop && $order == 'descending'){
- $orderby = $prop.' desc, id desc';
- }else{
- $orderby = 'id desc';
- }
- $map[] = ['del','=',0];
- $map[] = ['org_id','=',$orgId];
- if($title != ''){
- $map[] = ['title','like','%'.$title.'%'];
- }
- if($source != ''){
- $map[] = ['source','like','%'.$source.'%'];
- }
- if($proof != ''){
- $map[] = ['proof','like','%'.$proof.'%'];
- }
- if($sn != ''){
- $map[] = ['sn','like','%'.$sn.'%'];
- }
- if($enable >= 0){
- $map[] = ['status','=',$enable];
- }
- if($transfer >= 0){
- $map[] = ['transfer','=',$transfer];
- }
- if($degree >= 0){
- $map[] = ['degree','=',$degree];
- }
- $ids = [];
- if($dep){
- $ids1 = Db::name('important_user')
- ->alias('iu')
- ->join('dep d','d.id = iu.bus_id')
- ->where('iu.type',0)
- ->where('d.name','like','%'.$dep.'%')
- ->column('iu.important_id');
- $ids1 = $ids1?$ids1:[];
- $ids = array_merge($ids,$ids1);
- }
- if($user){
- $ids2 = Db::name('important_user')
- ->alias('iu')
- ->join('user_info d','d.user_id = iu.bus_id')
- ->where('iu.type',2)
- ->where('d.name','like','%'.$user.'%')
- ->column('iu.important_id');
- $ids2 = $ids2?$ids2:[];
- $ids = array_merge($ids,$ids2);
- }
- $ids = array_unique($ids);
- if($ids){
- $map[] = ['id','in',$ids];
- }
- $lists = $this
- ->where($map)
- ->order($orderby)
- ->select();
- $lists = $lists?$lists->toArray():[];
- foreach ($lists as $k=>$v){
- $cbusers = $v['cbusers']?json_decode($v['cbusers'],true):[];
- $names = [];
- foreach ($cbusers as $kk=>$vv){
- if($vv['type'] == 0){ // 部门
- $names[] = Db::name('dep')->where('id',$vv['id'])->value('name');
- }else{ // 用户
- $names[] = Db::name('user_info')->where('user_id',$vv['id'])->value('name');
- }
- }
- $lists[$k]['bus_name'] = implode(',',$names);
- $curBus = Db::name('important_record')
- ->alias('ir')
- ->join('user_info ui','ui.user_id = ir.user_id')
- ->where('ir.important_id',$v['id'])
- ->whereIn('ir.status',[0,1])
- ->column('ui.name');
- $lists[$k]['cur_bus_name'] = $curBus?implode(',',$curBus):'';
- $deps = $v['deps']?json_decode($v['deps'],true):[];
- $dnames = [];
- foreach ($deps as $kk=>$vv){
- if($vv['type'] == 0){ // 部门
- $dnames[] = Db::name('dep')->where('id',$vv['id'])->value('name');
- }else{ // 用户
- $dnames[] = Db::name('user_info')->where('user_id',$vv['id'])->value('name');
- }
- }
- $lists[$k]['deps_name'] = implode(',',$dnames);
- $lists[$k]['status_text'] = '';
- if($v['status'] == 1){
- $lists[$k]['status_text'] = '已办结';
- }else if($v['status'] == 0){
- $lists[$k]['status_text'] = '进行中';
- }
- $lists[$k]['degree_text'] = '';
- if($v['degree'] == 1){
- $lists[$k]['degree_text'] = '加急';
- }else if($v['degree'] == 0){
- $lists[$k]['degree_text'] = '普通';
- }
- $lists[$k]['transfer_text'] = '';
- if($v['transfer'] == 1){
- $lists[$k]['transfer_text'] = '文件';
- }else if($v['transfer'] == 0){
- $lists[$k]['transfer_text'] = '会议';
- }
- $ranns = [];
- if($v['remark']){
- $ranns[] = $v['remark'];
- }
- // 办理进展
- $anns = Db::name('important_record_annotation')
- ->alias('ira')
- ->join('important_record ir','ir.id = ira.important_record_id')
- ->join('user_info ui','ui.user_id = ira.user_id')
- ->where('ir.important_id',$v['id'])
- ->where('ira.del',0)
- ->field('ira.content,ui.name,ira.files')
- ->order('ira.id asc')
- ->select();
- $anns = $anns?$anns:[];
- foreach ($anns as $key=>$val){
- $ranns[] = $val['name'].':'.$val['content'];
- }
- $lists[$k]['anns'] = implode(';',$ranns);
- }
- $columns[] = ["title" => "序号","key" => "sn"];
- $columns[] = ["title" => "文件传阅序号","key" => "transferId"];
- $columns[] = ["title" => "状态","key" => "statusText"];
- $columns[] = ["title" => "来文单位","key" => "source"];
- $columns[] = ["title" => "文号/期号","key" => "proof"];
- $columns[] = ["title" => "文件名称/议题内容","key" => "title"];
- $columns[] = ["title" => "领导批示","key" => "superiorOpinion"];
- $columns[] = ["title" => "承办处室","key" => "depsName"];
- $columns[] = ["title" => "承办人","key" => "busName"];
- $columns[] = ["title" => "当前承办人","key" => "curBusName"];
- $columns[] = ["title" => "紧急程度","key" => "degreeText"];
- $columns[] = ["title" => "类型","key" => "transferText"];
- $columns[] = ["title" => "截止日期","key" => "endTime"];
- $columns[] = ["title" => "时间","key" => "createTime"];
- $columns[] = ["title" => "办理进度","key" => "anns"];
- $data = [
- 'columns' => $columns,
- 'list' => $lists
- ];
- return $data;
- }
- public function del($id){
- $ret = $this->where('id',$id)->setField('del',1);
- if(!$ret){
- HelpHander::error('删除失败');
- }
- // TODO::删除相关系统消息
- return true;
- }
- // 办结
- public function finish($id){
- $info = $this->info($id);
- if($info['status'] != 0){
- HelpHander::error('已办结');
- }
- $count = Db::name('important_record')->where('important_id',$id)->count();
- if($count <= 0){
- HelpHander::error('未派发任何人员,不能执行此操作');
- }
- // 检查记录中是否有人未办结
- $count = Db::name('important_record')->where('important_id',$id)->where('status','in',[0,1,2])->count();
- if($count > 0){
- HelpHander::error('有人未完成,不能执行此操作');
- }
- $ret = Db::name('important')->where('id',$id)->update(['status' => 1,'finish_time' => date('Y-m-d H:i:s')]);
- if(!$ret){
- HelpHander::error('操作失败');
- }
- return true;
- }
- }
|