Document.php 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279
  1. <?php
  2. namespace app\common\model;
  3. use app\hander\HelpHander;
  4. use think\Db;
  5. use think\Exception;
  6. use think\Model;
  7. class Document extends Model
  8. {
  9. public function add(){
  10. $data = [
  11. 'id' => input('id/d',0),
  12. 'title' => input('title','','trim'),
  13. 'org_id' => input('orgId/d',0),
  14. 'user_id' => input('userId/d',0),
  15. 'dep_id' => input('depId/d',0),
  16. 'cate_id' => input('cateId/d',0),
  17. 'source_file' => input('sourceFile','','trim'),
  18. 'size' => input('size/d',0), // 文件大小
  19. 'respon_user_id' => input('responUserId/d',0),
  20. 'page' => input('page','','trim'),
  21. 'duration' => input('duration','','trim'),
  22. 'auth' => input('auth/d',1),
  23. 'auths' => input('auths','','trim'),
  24. 'keyword' => input('keyword','','trim'),
  25. 'remark' => input('remark','','trim'),
  26. ];
  27. $result = validate('Document')->check($data,[],'');
  28. if(true !== $result){
  29. HelpHander::error(validate('Document')->getError());
  30. }
  31. $id = $data['id'];
  32. unset($data['id']);
  33. if($data['auth'] != 3){
  34. $data['auths'] = '';
  35. }
  36. $auths = json_decode($data['auths'],true);
  37. unset($data['auths']);
  38. Db::startTrans();
  39. try{
  40. if($id > 0){
  41. unset($data['user_id']);
  42. $data['update_time'] = date('Y-m-d H:i:s');
  43. $ret = $this->allowField(true)->save($data,['id'=>$id]);
  44. if(!$ret){
  45. \exception('操作失败');
  46. }
  47. Db::name('document_auth')->where('document_id',$id)->delete();
  48. }else{
  49. $data['create_time'] = date('Y-m-d H:i:s');
  50. $max = Db::name('document')
  51. ->where('org_id',$data['org_id'])
  52. ->where('create_time','>=',date('Y-m-d').' 00:00:00')
  53. ->max('sn');
  54. $sn = $max ? $max + 1 : date('Ymd').'00001';
  55. $data['sn'] = $sn;
  56. $ret = $this->allowField(true)->save($data);
  57. if(!$ret){
  58. \exception('操作失败');
  59. }
  60. $id = $this->id;
  61. }
  62. if($data['auth'] === 3){
  63. $auths = $this->formatAuths($auths,$data['org_id'],$id);
  64. if($auths){
  65. Db::name('document_auth')->insertAll($auths);
  66. }
  67. }
  68. Db::commit();
  69. }catch (Exception $e){
  70. Db::rollback();
  71. HelpHander::error($e->getMessage());
  72. }
  73. return true;
  74. }
  75. private function formatAuths($auths,$orgId,$doucmentId){
  76. $newauths = [];
  77. if(!empty($auths)){
  78. foreach ($auths as $k=>$v){
  79. $newauths[] = [
  80. 'type' => $v['type'],
  81. 'bus_id' => $v['busId'],
  82. 'org_id' => $orgId,
  83. 'document_id' => $doucmentId,
  84. ];
  85. }
  86. }
  87. return $newauths;
  88. }
  89. public function info($id){
  90. $info = Db::name('Document')->where('id',$id)->find();
  91. if(!$info){
  92. HelpHander::error('数据不存在');
  93. }
  94. $info['cateName'] = Db::name('document_cate')->where('id',$info['cate_id'])->value('name');
  95. $info['depName'] = '';
  96. $info['responUserName'] = '';
  97. if($info['dep_id']){
  98. $info['depName'] = Db::name('dep')->where('id',$info['dep_id'])->value('name');
  99. }
  100. if($info['respon_user_id']){
  101. $info['responUserName'] = Db::name('user_info')->where('user_id',$info['respon_user_id'])->value('name');
  102. }
  103. $info['userName'] = Db::name('user_info')->where('user_id',$info['user_id'])->value('name');
  104. $documentAuths = Db::name('document_auth')->where('document_id',$id)->select();
  105. $info['documentAuths'] = $documentAuths?$documentAuths:[];
  106. return $info;
  107. }
  108. public function lists($page,$size,$cateId,$title,$name,$startTime,$endTime,$userId,$orgId){
  109. // 获取用户的部门及所有上级部门
  110. $depids = model('Dep')->getUserDepIds($userId,$orgId);
  111. $map1 = [
  112. ['type','=',2],
  113. ['org_id','=',$orgId]
  114. ];
  115. $map2 = [
  116. ['type','=',0],
  117. ['bus_id','=',$userId]
  118. ];
  119. $map3 = [
  120. ['type','=',1],
  121. ['bus_id','in',$depids]
  122. ];
  123. $documentIds = Db::name('document_auth')->whereOr([$map1, $map2, $map3])->column('document_id');
  124. if($documentIds){
  125. $m1[] = ['d.id','in',$documentIds];
  126. }
  127. if($cateId > 0){
  128. $m1[] = $m2[] = $m3[] = ['d.cate_id','=',$cateId];
  129. }
  130. if($title){
  131. $m1[] = $m2[] = $m3[] = ['d.title','like','%'.$title.'%'];
  132. }
  133. if($name){
  134. $m1[] = $m2[] = $m3[] = ['ui.name','like','%'.$name.'%'];
  135. }
  136. if($startTime && $endTime){
  137. $m1[] = $m2[] = $m3[] = ['d.create_time','>=',$startTime];
  138. $m1[] = $m2[] = $m3[] = ['d.create_time','<=',$endTime];
  139. }
  140. $m1[] = ['d.del','=',0];
  141. $m1[] = ['d.auth','=',3];
  142. $m2[] = ['d.del','=',0];
  143. $m2[] = ['d.user_id','=',$userId];
  144. $m3[] = ['d.del','=',0];
  145. $m3[] = ['d.auth','=',2];
  146. $lists = Db::name('document')
  147. ->alias('d')
  148. ->join('user_info ui','ui.user_id = d.user_id')
  149. ->field('d.*,ui.name as userName')
  150. ->whereOr([$m1,$m2,$m3])
  151. ->page($page,$size)
  152. ->order('d.id desc')
  153. ->select();
  154. $lists = $lists?$lists:[];
  155. foreach ($lists as $k=>$v){
  156. $lists[$k]['depName'] = '';
  157. $lists[$k]['cateName'] = '';
  158. $lists[$k]['responUserName'] = '';
  159. if($v['dep_id']){
  160. $lists[$k]['depName'] = Db::name('dep')->where('id',$v['dep_id'])->value('name');
  161. }
  162. if($v['cate_id']){
  163. $lists[$k]['cateName'] = Db::name('document_cate')->where('id',$v['cate_id'])->value('name');
  164. }
  165. if($v['respon_user_id']){
  166. $lists[$k]['responUserName'] = Db::name('user_info')->where('user_id',$v['respon_user_id'])->value('name');
  167. }
  168. }
  169. $total = Db::name('document')
  170. ->alias('d')
  171. ->join('user_info ui','ui.user_id = d.user_id')
  172. ->whereOr([$m1,$m2,$m3])
  173. ->count();
  174. $data = [
  175. 'total' => $total,
  176. 'list' => $lists?$lists:[]
  177. ];
  178. return $data;
  179. }
  180. public function getAppList($title,$userId,$orgId,$cateId=0){
  181. // 获取用户的部门及所有上级部门
  182. $depids = model('Dep')->getUserDepIds($userId,$orgId);
  183. $map1 = [
  184. ['type','=',2],
  185. ['org_id','=',$orgId]
  186. ];
  187. $map2 = [
  188. ['type','=',0],
  189. ['bus_id','=',$userId]
  190. ];
  191. $map3 = [
  192. ['type','=',1],
  193. ['bus_id','in',$depids]
  194. ];
  195. $documentIds = Db::name('document_auth')->whereOr([$map1, $map2, $map3])->column('document_id');
  196. if($documentIds){
  197. $m1[] = ['id','in',$documentIds];
  198. }
  199. if($title){
  200. $m1[] = $m2[] = $m3[] = ['title','like','%'.$title.'%'];
  201. }
  202. $m1[] = ['del','=',0];
  203. $m1[] = ['auth','=',3];
  204. $m2[] = ['del','=',0];
  205. $m2[] = ['user_id','=',$userId];
  206. $m3[] = ['del','=',0];
  207. $m3[] = ['auth','=',2];
  208. $lists = Db::name('document')
  209. ->field('id,title as name,source_file,file')
  210. ->whereOr([$m1,$m2,$m3])
  211. ->order('id asc')
  212. ->select();
  213. $lists = $lists?$lists:[];
  214. foreach ($lists as $k=>$v){
  215. $lists[$k]['type'] = 1;
  216. }
  217. return $lists;
  218. }
  219. public function queryDocumentByCate($cateId,$title,$userId,$orgId){
  220. if($title){ // 搜索
  221. $lists = $this->getAppList($title,$userId,$orgId);
  222. } else { // 子分类及文档
  223. // 先获取子分类
  224. $list1 = model('DocumentCate')->getSubList($cateId,$orgId);
  225. $list1 = $list1?$list1:[];
  226. //获取分类下文档
  227. $list2 = $this->getAppList('',$userId,$orgId,$cateId);
  228. $list1 = $list1?$list1:[];
  229. $lists = array_merge($list1,$list2);
  230. }
  231. return $lists;
  232. }
  233. public function del($id,$userId){
  234. $info = Db::name('document')->where('id',$id)->where('del',0)->find();
  235. if(!$info){
  236. HelpHander::error('文档不存在');
  237. }
  238. if($info['user_id'] != $userId){
  239. HelpHander::error('不能修改他人文档');
  240. }
  241. $ret = $this->where('id',$id)->setField('del',1);
  242. if(!$ret){
  243. HelpHander::error('删除失败');
  244. }
  245. return true;
  246. }
  247. public function updateViews($id){
  248. Db::name('document')->where('id',$id)->setInc('views',1);
  249. return true;
  250. }
  251. }