Important.php 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546
  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 Important extends Model
  8. {
  9. public function add(){
  10. $data = [
  11. 'id' => input('id/d',0),
  12. 'title' => input('title','','trim'),
  13. 'source' => input('source','','trim'),
  14. 'proof' => input('proof','','trim'),
  15. 'end_time' => input('endTime','','trim'),
  16. 'org_id' => input('orgId/d',0),
  17. 'superior_opinion' => input('superiorOpinion','','trim'),
  18. 'superior_opinion_files' => input('superiorOpinionFiles','','trim'),
  19. 'file' => input('file','','trim'),
  20. // 'bus_type' => input('busType/d',0),
  21. // 'bus_id' => input('busId/d',0),
  22. 'deps' => input('deps','','trim'),
  23. 'cbusers' => input('cbusers','','trim'),
  24. 'degree' => input('degree/d',0),
  25. 'transfer' => input('transfer/d',0),
  26. 'transfer_id' => input('transferId/d',0),
  27. 'remark' => input('remark','','trim')
  28. ];
  29. if($data['id'] > 0){
  30. $result = validate('Important')->check($data,[],'edit');
  31. if(true !== $result){
  32. HelpHander::error(validate('Important')->getError());
  33. }
  34. unset($data['transfer']);
  35. unset($data['transfer_id']);
  36. }else{
  37. $result = validate('Important')->check($data,[],'add');
  38. if(true !== $result){
  39. HelpHander::error(validate('Important')->getError());
  40. }
  41. if($data['transfer_id'] > 0){
  42. $iid = Db::name('important')->where('transfer_id',$data['transfer_id'])->where('del',0)->value('id');
  43. if($iid){
  44. HelpHander::error('该文件已转重要事项');
  45. }
  46. }
  47. }
  48. $id = $data['id'];
  49. unset($data['id']);
  50. Db::startTrans();
  51. try{
  52. if($id > 0){
  53. $data['update_time'] = date('Y-m-d H:i:s');
  54. $ret = $this->allowField(true)->save($data,['id'=>$id]);
  55. if(!$ret){
  56. \exception('操作失败');
  57. }
  58. $cbusers = json_decode($data['cbusers'],true);
  59. foreach ($cbusers as $k=>$v){
  60. $sdata = [
  61. 'user_id' => $v['id'],
  62. 'content' => '',
  63. 'end_time' => $data['end_time'],
  64. 'org_id' => $data['org_id'],
  65. 'pid' => 0,
  66. 'status' => 1, // 默认已同意
  67. 'important_id' => $id,
  68. 'create_time' => date('Y-m-d H:i:s')
  69. ];
  70. // 检查记录是否已存在
  71. $ret = Db::name('important_record')
  72. ->where('user_id',$v['id'])
  73. ->where('important_id',$id)
  74. ->where('pid',0)
  75. ->where('org_id',$sdata['org_id'])
  76. ->find();
  77. if(!$ret){ // 不存在新增
  78. $rid = Db::name('important_record')->insertGetId($sdata);
  79. if(!$rid){
  80. \exception('操作失败');
  81. }
  82. model('Message')->add(3,$rid,14,$sdata['user_id'],$sdata['org_id'],'有一项重要事项需要您办理.');
  83. }
  84. }
  85. // 添加关联关系
  86. Db::name('important_user')->where('important_id',$id)->delete();
  87. }else{
  88. $data['create_time'] = date('Y-m-d H:i:s');
  89. // 获取最新的一个编号
  90. $data['sn'] = $this->createNewSn($data['org_id']);
  91. $ret = $this->allowField(true)->save($data);
  92. if(!$ret){
  93. \exception('操作失败');
  94. }
  95. $id = $this->id;
  96. $cbusers = json_decode($data['cbusers'],true);
  97. foreach ($cbusers as $k=>$v){
  98. $sdata = [
  99. 'user_id' => $v['id'],
  100. 'content' => '',
  101. 'end_time' => $data['end_time'],
  102. 'org_id' => $data['org_id'],
  103. 'pid' => 0,
  104. 'status' => 1, // 默认已同意
  105. 'important_id' => $this->id,
  106. 'create_time' => date('Y-m-d H:i:s')
  107. ];
  108. $rid = Db::name('important_record')->insertGetId($sdata);
  109. if(!$rid){
  110. \exception('操作失败');
  111. }
  112. model('Message')->add(3,$rid,14,$sdata['user_id'],$sdata['org_id'],'有一项重要事项需要您办理.');
  113. }
  114. }
  115. // 添加关联关系
  116. $cbarr = [];
  117. foreach ($cbusers as $k=>$v){
  118. $cbarr[] = [
  119. 'type' => 2,
  120. 'bus_id' => $v['id'],
  121. 'important_id' => $id,
  122. ];
  123. }
  124. if($data['deps']){
  125. $deps = json_decode($data['deps'],true);
  126. $deps = $deps?$deps:[];
  127. foreach ($deps as $k=>$v){
  128. $cbarr[] = [
  129. 'type' => 0,
  130. 'bus_id' => $v['id'],
  131. 'important_id' => $id,
  132. ];
  133. }
  134. }
  135. if($cbarr){
  136. Db::name('important_user')->insertAll($cbarr);
  137. }
  138. Db::commit();
  139. }catch (Exception $e){
  140. Db::rollback();
  141. HelpHander::error($e->getMessage());
  142. }
  143. return true;
  144. }
  145. private function createNewSn($orgId){
  146. $newsn = '';
  147. $sn = Db::name('important')->where('del',0)->where('org_id',$orgId)->order('id desc')->value('sn');
  148. $curY = date('y');
  149. if(!$sn){
  150. $newsn = $curY.'0001';
  151. }else{
  152. $y = substr($sn,0,2);
  153. if($curY != $y){
  154. $newsn = $curY.'0001';
  155. }else{
  156. $newsn = $sn+1;
  157. }
  158. }
  159. return $newsn;
  160. }
  161. public function info($id){
  162. $info = $this->where('id',$id)->find();
  163. if(!$info){
  164. HelpHander::error('数据不存在');
  165. }
  166. $info = $info->toArray();
  167. $cbusers = $info['cbusers']?json_decode($info['cbusers'],true):[];
  168. $names = [];
  169. foreach ($cbusers as $kk=>$vv){
  170. if($vv['type'] == 0){ // 部门
  171. $names[] = Db::name('dep')->where('id',$vv['id'])->value('name');
  172. }else{ // 用户
  173. $names[] = Db::name('user_info')->where('user_id',$vv['id'])->value('name');
  174. }
  175. }
  176. $info['bus_name'] = implode(',',$names);
  177. $deps = $info['deps']?json_decode($info['deps'],true):[];
  178. $dnames = [];
  179. foreach ($deps as $kk=>$vv){
  180. if($vv['type'] == 0){ // 部门
  181. $dnames[] = Db::name('dep')->where('id',$vv['id'])->value('name');
  182. }else{ // 用户
  183. $dnames[] = Db::name('user_info')->where('user_id',$vv['id'])->value('name');
  184. }
  185. }
  186. $info['deps_name'] = implode(',',$dnames);
  187. $info['file'] = $info['file']?json_decode($info['file'],true):[];
  188. $info['superior_opinion_files'] = $info['superior_opinion_files']?json_decode($info['superior_opinion_files'],true):[];
  189. // 办理进展
  190. $anns = Db::name('important_record_annotation')
  191. ->alias('ira')
  192. ->join('important_record ir','ir.id = ira.important_record_id')
  193. ->join('user_info ui','ui.user_id = ira.user_id')
  194. ->where('ir.important_id',$id)
  195. ->where('ira.del',0)
  196. ->field('ira.content,ui.name,ira.files')
  197. ->order('ira.id asc')
  198. ->select();
  199. $info['anns'] = $anns?$anns:[];
  200. return $info;
  201. }
  202. public function lists($orgId){
  203. $page = input('page/d',1);
  204. $size = input('size/d',10);
  205. $title = input('title','','trim');
  206. $enable = input('enable/d',-1);
  207. $transfer = input('transfer/d',-1);
  208. $degree = input('degree/d',-1);
  209. $dep = input('dep','','trim');
  210. $user = input('user','','trim');
  211. $source = input('source','','trim');
  212. $proof = input('proof','','trim');
  213. $sn = input('sn','','trim');
  214. $prop = input('prop','','trim');
  215. $order = input('order','','trim');
  216. if($prop == 'transferId'){
  217. $prop = 'transfer_id';
  218. }else if($prop == 'endTime'){
  219. $prop = 'end_time';
  220. }else if($prop == 'createTime'){
  221. $prop = 'create_time';
  222. }else if($prop == 'enable'){
  223. $prop = 'status';
  224. }
  225. if($prop && $order == 'ascending'){
  226. $orderby = $prop.' asc, id desc';
  227. }else if($prop && $order == 'descending'){
  228. $orderby = $prop.' desc, id desc';
  229. }else{
  230. $orderby = 'id desc';
  231. }
  232. $map[] = ['del','=',0];
  233. $map[] = ['org_id','=',$orgId];
  234. if($title != ''){
  235. $map[] = ['title','like','%'.$title.'%'];
  236. }
  237. if($source != ''){
  238. $map[] = ['source','like','%'.$source.'%'];
  239. }
  240. if($proof != ''){
  241. $map[] = ['proof','like','%'.$proof.'%'];
  242. }
  243. if($sn != ''){
  244. $map[] = ['sn','like','%'.$sn.'%'];
  245. }
  246. if($enable >= 0){
  247. $map[] = ['status','=',$enable];
  248. }
  249. if($transfer >= 0){
  250. $map[] = ['transfer','=',$transfer];
  251. }
  252. if($degree >= 0){
  253. $map[] = ['degree','=',$degree];
  254. }
  255. $ids = [];
  256. if($dep){
  257. $ids1 = Db::name('important_user')
  258. ->alias('iu')
  259. ->join('dep d','d.id = iu.bus_id')
  260. ->where('iu.type',0)
  261. ->where('d.name','like','%'.$dep.'%')
  262. ->column('iu.important_id');
  263. $ids1 = $ids1?$ids1:[];
  264. $ids = array_merge($ids,$ids1);
  265. }
  266. if($user){
  267. $ids2 = Db::name('important_user')
  268. ->alias('iu')
  269. ->join('user_info d','d.user_id = iu.bus_id')
  270. ->where('iu.type',2)
  271. ->where('d.name','like','%'.$user.'%')
  272. ->column('iu.important_id');
  273. $ids2 = $ids2?$ids2:[];
  274. $ids = array_merge($ids,$ids2);
  275. }
  276. $ids = array_unique($ids);
  277. if($ids){
  278. $map[] = ['id','in',$ids];
  279. }
  280. $lists = $this
  281. ->where($map)
  282. ->page($page,$size)
  283. ->order($orderby)
  284. ->select();
  285. $lists = $lists?$lists->toArray():[];
  286. foreach ($lists as $k=>$v){
  287. $cbusers = $v['cbusers']?json_decode($v['cbusers'],true):[];
  288. $names = [];
  289. foreach ($cbusers as $kk=>$vv){
  290. if($vv['type'] == 0){ // 部门
  291. $names[] = Db::name('dep')->where('id',$vv['id'])->value('name');
  292. }else{ // 用户
  293. $names[] = Db::name('user_info')->where('user_id',$vv['id'])->value('name');
  294. }
  295. }
  296. $lists[$k]['bus_name'] = implode(',',$names);
  297. $curBus = Db::name('important_record')
  298. ->alias('ir')
  299. ->join('user_info ui','ui.user_id = ir.user_id')
  300. ->where('ir.important_id',$v['id'])
  301. ->whereIn('ir.status',[0,1])
  302. ->column('ui.name');
  303. $lists[$k]['cur_bus_name'] = $curBus?implode(',',$curBus):'';
  304. $deps = $v['deps']?json_decode($v['deps'],true):[];
  305. $dnames = [];
  306. foreach ($deps as $kk=>$vv){
  307. if($vv['type'] == 0){ // 部门
  308. $dnames[] = Db::name('dep')->where('id',$vv['id'])->value('name');
  309. }else{ // 用户
  310. $dnames[] = Db::name('user_info')->where('user_id',$vv['id'])->value('name');
  311. }
  312. }
  313. $lists[$k]['deps_name'] = implode(',',$dnames);
  314. }
  315. $total = $this->where($map)->count();
  316. $data = [
  317. 'total' => $total,
  318. 'list' => $lists
  319. ];
  320. return $data;
  321. }
  322. public function listExport($orgId){
  323. $title = input('title','','trim');
  324. $enable = input('enable/d',-1);
  325. $transfer = input('transfer/d',-1);
  326. $degree = input('degree/d',-1);
  327. $dep = input('dep','','trim');
  328. $user = input('user','','trim');
  329. $source = input('source','','trim');
  330. $proof = input('proof','','trim');
  331. $sn = input('sn','','trim');
  332. $prop = input('prop','','trim');
  333. $order = input('order','','trim');
  334. if($prop && $order == 'ascending'){
  335. $orderby = $prop.' asc, id desc';
  336. }else if($prop && $order == 'descending'){
  337. $orderby = $prop.' desc, id desc';
  338. }else{
  339. $orderby = 'id desc';
  340. }
  341. $map[] = ['del','=',0];
  342. $map[] = ['org_id','=',$orgId];
  343. if($title != ''){
  344. $map[] = ['title','like','%'.$title.'%'];
  345. }
  346. if($source != ''){
  347. $map[] = ['source','like','%'.$source.'%'];
  348. }
  349. if($proof != ''){
  350. $map[] = ['proof','like','%'.$proof.'%'];
  351. }
  352. if($sn != ''){
  353. $map[] = ['sn','like','%'.$sn.'%'];
  354. }
  355. if($enable >= 0){
  356. $map[] = ['status','=',$enable];
  357. }
  358. if($transfer >= 0){
  359. $map[] = ['transfer','=',$transfer];
  360. }
  361. if($degree >= 0){
  362. $map[] = ['degree','=',$degree];
  363. }
  364. $ids = [];
  365. if($dep){
  366. $ids1 = Db::name('important_user')
  367. ->alias('iu')
  368. ->join('dep d','d.id = iu.bus_id')
  369. ->where('iu.type',0)
  370. ->where('d.name','like','%'.$dep.'%')
  371. ->column('iu.important_id');
  372. $ids1 = $ids1?$ids1:[];
  373. $ids = array_merge($ids,$ids1);
  374. }
  375. if($user){
  376. $ids2 = Db::name('important_user')
  377. ->alias('iu')
  378. ->join('user_info d','d.user_id = iu.bus_id')
  379. ->where('iu.type',2)
  380. ->where('d.name','like','%'.$user.'%')
  381. ->column('iu.important_id');
  382. $ids2 = $ids2?$ids2:[];
  383. $ids = array_merge($ids,$ids2);
  384. }
  385. $ids = array_unique($ids);
  386. if($ids){
  387. $map[] = ['id','in',$ids];
  388. }
  389. $lists = $this
  390. ->where($map)
  391. ->order($orderby)
  392. ->select();
  393. $lists = $lists?$lists->toArray():[];
  394. foreach ($lists as $k=>$v){
  395. $cbusers = $v['cbusers']?json_decode($v['cbusers'],true):[];
  396. $names = [];
  397. foreach ($cbusers as $kk=>$vv){
  398. if($vv['type'] == 0){ // 部门
  399. $names[] = Db::name('dep')->where('id',$vv['id'])->value('name');
  400. }else{ // 用户
  401. $names[] = Db::name('user_info')->where('user_id',$vv['id'])->value('name');
  402. }
  403. }
  404. $lists[$k]['bus_name'] = implode(',',$names);
  405. $curBus = Db::name('important_record')
  406. ->alias('ir')
  407. ->join('user_info ui','ui.user_id = ir.user_id')
  408. ->where('ir.important_id',$v['id'])
  409. ->whereIn('ir.status',[0,1])
  410. ->column('ui.name');
  411. $lists[$k]['cur_bus_name'] = $curBus?implode(',',$curBus):'';
  412. $deps = $v['deps']?json_decode($v['deps'],true):[];
  413. $dnames = [];
  414. foreach ($deps as $kk=>$vv){
  415. if($vv['type'] == 0){ // 部门
  416. $dnames[] = Db::name('dep')->where('id',$vv['id'])->value('name');
  417. }else{ // 用户
  418. $dnames[] = Db::name('user_info')->where('user_id',$vv['id'])->value('name');
  419. }
  420. }
  421. $lists[$k]['deps_name'] = implode(',',$dnames);
  422. $lists[$k]['status_text'] = '';
  423. if($v['status'] == 1){
  424. $lists[$k]['status_text'] = '已办结';
  425. }else if($v['status'] == 0){
  426. $lists[$k]['status_text'] = '进行中';
  427. }
  428. $lists[$k]['degree_text'] = '';
  429. if($v['degree'] == 1){
  430. $lists[$k]['degree_text'] = '加急';
  431. }else if($v['degree'] == 0){
  432. $lists[$k]['degree_text'] = '普通';
  433. }
  434. $lists[$k]['transfer_text'] = '';
  435. if($v['transfer'] == 1){
  436. $lists[$k]['transfer_text'] = '文件';
  437. }else if($v['transfer'] == 0){
  438. $lists[$k]['transfer_text'] = '会议';
  439. }
  440. $ranns = [];
  441. if($v['remark']){
  442. $ranns[] = $v['remark'];
  443. }
  444. // 办理进展
  445. $anns = Db::name('important_record_annotation')
  446. ->alias('ira')
  447. ->join('important_record ir','ir.id = ira.important_record_id')
  448. ->join('user_info ui','ui.user_id = ira.user_id')
  449. ->where('ir.important_id',$v['id'])
  450. ->where('ira.del',0)
  451. ->field('ira.content,ui.name,ira.files')
  452. ->order('ira.id asc')
  453. ->select();
  454. $anns = $anns?$anns:[];
  455. foreach ($anns as $key=>$val){
  456. $ranns[] = $val['name'].':'.$val['content'];
  457. }
  458. $lists[$k]['anns'] = implode(';',$ranns);
  459. }
  460. $columns[] = ["title" => "序号","key" => "sn"];
  461. $columns[] = ["title" => "文件传阅序号","key" => "transferId"];
  462. $columns[] = ["title" => "状态","key" => "statusText"];
  463. $columns[] = ["title" => "来文单位","key" => "source"];
  464. $columns[] = ["title" => "文号/期号","key" => "proof"];
  465. $columns[] = ["title" => "文件名称/议题内容","key" => "title"];
  466. $columns[] = ["title" => "领导批示","key" => "superiorOpinion"];
  467. $columns[] = ["title" => "承办处室","key" => "depsName"];
  468. $columns[] = ["title" => "承办人","key" => "busName"];
  469. $columns[] = ["title" => "当前承办人","key" => "curBusName"];
  470. $columns[] = ["title" => "紧急程度","key" => "degreeText"];
  471. $columns[] = ["title" => "类型","key" => "transferText"];
  472. $columns[] = ["title" => "截止日期","key" => "endTime"];
  473. $columns[] = ["title" => "时间","key" => "createTime"];
  474. $columns[] = ["title" => "办理进度","key" => "anns"];
  475. $data = [
  476. 'columns' => $columns,
  477. 'list' => $lists
  478. ];
  479. return $data;
  480. }
  481. public function del($id){
  482. $ret = $this->where('id',$id)->setField('del',1);
  483. if(!$ret){
  484. HelpHander::error('删除失败');
  485. }
  486. // TODO::删除相关系统消息
  487. return true;
  488. }
  489. // 办结
  490. public function finish($id){
  491. $info = $this->info($id);
  492. if($info['status'] != 0){
  493. HelpHander::error('已办结');
  494. }
  495. $count = Db::name('important_record')->where('important_id',$id)->count();
  496. if($count <= 0){
  497. HelpHander::error('未派发任何人员,不能执行此操作');
  498. }
  499. // 检查记录中是否有人未办结
  500. $count = Db::name('important_record')->where('important_id',$id)->where('status','in',[0,1,2])->count();
  501. if($count > 0){
  502. HelpHander::error('有人未完成,不能执行此操作');
  503. }
  504. $ret = Db::name('important')->where('id',$id)->update(['status' => 1,'finish_time' => date('Y-m-d H:i:s')]);
  505. if(!$ret){
  506. HelpHander::error('操作失败');
  507. }
  508. return true;
  509. }
  510. }