123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279 |
- <?php
- namespace app\common\model;
- use app\hander\HelpHander;
- use think\Db;
- use think\Exception;
- use think\Model;
- class Document extends Model
- {
- public function add(){
- $data = [
- 'id' => input('id/d',0),
- 'title' => input('title','','trim'),
- 'org_id' => input('orgId/d',0),
- 'user_id' => input('userId/d',0),
- 'dep_id' => input('depId/d',0),
- 'cate_id' => input('cateId/d',0),
- 'source_file' => input('sourceFile','','trim'),
- 'size' => input('size/d',0), // 文件大小
- 'respon_user_id' => input('responUserId/d',0),
- 'page' => input('page','','trim'),
- 'duration' => input('duration','','trim'),
- 'auth' => input('auth/d',1),
- 'auths' => input('auths','','trim'),
- 'keyword' => input('keyword','','trim'),
- 'remark' => input('remark','','trim'),
- ];
- $result = validate('Document')->check($data,[],'');
- if(true !== $result){
- HelpHander::error(validate('Document')->getError());
- }
- $id = $data['id'];
- unset($data['id']);
- if($data['auth'] != 3){
- $data['auths'] = '';
- }
- $auths = json_decode($data['auths'],true);
- unset($data['auths']);
- Db::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('操作失败');
- }
- Db::name('document_auth')->where('document_id',$id)->delete();
- }else{
- $data['create_time'] = date('Y-m-d H:i:s');
- $max = Db::name('document')
- ->where('org_id',$data['org_id'])
- ->where('create_time','>=',date('Y-m-d').' 00:00:00')
- ->max('sn');
- $sn = $max ? $max + 1 : date('Ymd').'00001';
- $data['sn'] = $sn;
- $ret = $this->allowField(true)->save($data);
- if(!$ret){
- \exception('操作失败');
- }
- $id = $this->id;
- }
- if($data['auth'] === 3){
- $auths = $this->formatAuths($auths,$data['org_id'],$id);
- if($auths){
- Db::name('document_auth')->insertAll($auths);
- }
- }
- Db::commit();
- }catch (Exception $e){
- Db::rollback();
- HelpHander::error($e->getMessage());
- }
- return true;
- }
- private function formatAuths($auths,$orgId,$doucmentId){
- $newauths = [];
- if(!empty($auths)){
- foreach ($auths as $k=>$v){
- $newauths[] = [
- 'type' => $v['type'],
- 'bus_id' => $v['busId'],
- 'org_id' => $orgId,
- 'document_id' => $doucmentId,
- ];
- }
- }
- return $newauths;
- }
- public function info($id){
- $info = Db::name('Document')->where('id',$id)->find();
- if(!$info){
- HelpHander::error('数据不存在');
- }
- $info['cateName'] = Db::name('document_cate')->where('id',$info['cate_id'])->value('name');
- $info['depName'] = '';
- $info['responUserName'] = '';
- if($info['dep_id']){
- $info['depName'] = Db::name('dep')->where('id',$info['dep_id'])->value('name');
- }
- if($info['respon_user_id']){
- $info['responUserName'] = Db::name('user_info')->where('user_id',$info['respon_user_id'])->value('name');
- }
- $info['userName'] = Db::name('user_info')->where('user_id',$info['user_id'])->value('name');
- $documentAuths = Db::name('document_auth')->where('document_id',$id)->select();
- $info['documentAuths'] = $documentAuths?$documentAuths:[];
- return $info;
- }
- public function lists($page,$size,$cateId,$title,$name,$startTime,$endTime,$userId,$orgId){
- // 获取用户的部门及所有上级部门
- $depids = model('Dep')->getUserDepIds($userId,$orgId);
- $map1 = [
- ['type','=',2],
- ['org_id','=',$orgId]
- ];
- $map2 = [
- ['type','=',0],
- ['bus_id','=',$userId]
- ];
- $map3 = [
- ['type','=',1],
- ['bus_id','in',$depids]
- ];
- $documentIds = Db::name('document_auth')->whereOr([$map1, $map2, $map3])->column('document_id');
- if($documentIds){
- $m1[] = ['d.id','in',$documentIds];
- }
- if($cateId > 0){
- $m1[] = $m2[] = $m3[] = ['d.cate_id','=',$cateId];
- }
- if($title){
- $m1[] = $m2[] = $m3[] = ['d.title','like','%'.$title.'%'];
- }
- if($name){
- $m1[] = $m2[] = $m3[] = ['ui.name','like','%'.$name.'%'];
- }
- if($startTime && $endTime){
- $m1[] = $m2[] = $m3[] = ['d.create_time','>=',$startTime];
- $m1[] = $m2[] = $m3[] = ['d.create_time','<=',$endTime];
- }
- $m1[] = ['d.del','=',0];
- $m1[] = ['d.auth','=',3];
- $m2[] = ['d.del','=',0];
- $m2[] = ['d.user_id','=',$userId];
- $m3[] = ['d.del','=',0];
- $m3[] = ['d.auth','=',2];
- $lists = Db::name('document')
- ->alias('d')
- ->join('user_info ui','ui.user_id = d.user_id')
- ->field('d.*,ui.name as userName')
- ->whereOr([$m1,$m2,$m3])
- ->page($page,$size)
- ->order('d.id desc')
- ->select();
- $lists = $lists?$lists:[];
- foreach ($lists as $k=>$v){
- $lists[$k]['depName'] = '';
- $lists[$k]['cateName'] = '';
- $lists[$k]['responUserName'] = '';
- if($v['dep_id']){
- $lists[$k]['depName'] = Db::name('dep')->where('id',$v['dep_id'])->value('name');
- }
- if($v['cate_id']){
- $lists[$k]['cateName'] = Db::name('document_cate')->where('id',$v['cate_id'])->value('name');
- }
- if($v['respon_user_id']){
- $lists[$k]['responUserName'] = Db::name('user_info')->where('user_id',$v['respon_user_id'])->value('name');
- }
- }
- $total = Db::name('document')
- ->alias('d')
- ->join('user_info ui','ui.user_id = d.user_id')
- ->whereOr([$m1,$m2,$m3])
- ->count();
- $data = [
- 'total' => $total,
- 'list' => $lists?$lists:[]
- ];
- return $data;
- }
- public function getAppList($title,$userId,$orgId,$cateId=0){
- // 获取用户的部门及所有上级部门
- $depids = model('Dep')->getUserDepIds($userId,$orgId);
- $map1 = [
- ['type','=',2],
- ['org_id','=',$orgId]
- ];
- $map2 = [
- ['type','=',0],
- ['bus_id','=',$userId]
- ];
- $map3 = [
- ['type','=',1],
- ['bus_id','in',$depids]
- ];
- $documentIds = Db::name('document_auth')->whereOr([$map1, $map2, $map3])->column('document_id');
- if($documentIds){
- $m1[] = ['id','in',$documentIds];
- }
- if($title){
- $m1[] = $m2[] = $m3[] = ['title','like','%'.$title.'%'];
- }
- $m1[] = ['del','=',0];
- $m1[] = ['auth','=',3];
- $m2[] = ['del','=',0];
- $m2[] = ['user_id','=',$userId];
- $m3[] = ['del','=',0];
- $m3[] = ['auth','=',2];
- $lists = Db::name('document')
- ->field('id,title as name,source_file,file')
- ->whereOr([$m1,$m2,$m3])
- ->order('id asc')
- ->select();
- $lists = $lists?$lists:[];
- foreach ($lists as $k=>$v){
- $lists[$k]['type'] = 1;
- }
- return $lists;
- }
- public function queryDocumentByCate($cateId,$title,$userId,$orgId){
- if($title){ // 搜索
- $lists = $this->getAppList($title,$userId,$orgId);
- } else { // 子分类及文档
- // 先获取子分类
- $list1 = model('DocumentCate')->getSubList($cateId,$orgId);
- $list1 = $list1?$list1:[];
- //获取分类下文档
- $list2 = $this->getAppList('',$userId,$orgId,$cateId);
- $list1 = $list1?$list1:[];
- $lists = array_merge($list1,$list2);
- }
- return $lists;
- }
- public function del($id,$userId){
- $info = Db::name('document')->where('id',$id)->where('del',0)->find();
- if(!$info){
- HelpHander::error('文档不存在');
- }
- if($info['user_id'] != $userId){
- HelpHander::error('不能修改他人文档');
- }
- $ret = $this->where('id',$id)->setField('del',1);
- if(!$ret){
- HelpHander::error('删除失败');
- }
- return true;
- }
- public function updateViews($id){
- Db::name('document')->where('id',$id)->setInc('views',1);
- return true;
- }
- }
|