WorkNews.php 27 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723
  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 WorkNews extends Model
  8. {
  9. public function add(){
  10. $curTime = date('Y-m-d H:i:s');
  11. $data = [
  12. 'id' => input('id/d',0),
  13. 'work_type_id' => input('type/d',0),
  14. 'content' => input('content','','trim'),
  15. 'files' => input('files','','trim'),
  16. 'create_time' => $curTime,
  17. ];
  18. $result = validate('WorkNews')->check($data,[],'');
  19. if(true !== $result){
  20. HelpHander::error(validate('WorkNews')->getError());
  21. }
  22. Db::startTrans();
  23. try{
  24. $id = $data['id'];
  25. $sdata = [
  26. 'work_type_id' => $data['work_type_id'],
  27. 'files' => $data['files']
  28. ];
  29. if($id > 0){
  30. Db::name('work_news_item')->where('work_news_id',$id)->delete();
  31. $sdata['update_time'] = date('Y-m-d H:i:s');
  32. $workReportId = Db::name('work_news')->where('id',$id)->update($sdata);
  33. if(!$workReportId){
  34. \exception('提交失败');
  35. }
  36. }else{
  37. $sdata['status'] = 1;
  38. $sdata['org_id'] = input('orgId/d',0);
  39. $sdata['user_id'] = input('userId/d',0);
  40. $sdata['create_time'] = date('Y-m-d H:i:s');
  41. $sdata['receiver'] = check_exp_imp(input('receiver','','trim'));
  42. $sdata['auditor'] = input('auditor/d',0);
  43. if($sdata['auditor'] > 0){
  44. $sdata['audit_status'] = 0;
  45. }else{
  46. $sdata['audit_status'] = 1;
  47. }
  48. $workReportId = Db::name('work_news')->insertGetId($sdata);
  49. if(!$workReportId){
  50. \exception('提交失败');
  51. }
  52. $id = $workReportId;
  53. $type = 0;
  54. if($sdata['auditor'] > 0){
  55. $receiver = [$sdata['auditor']];
  56. $type = 1;
  57. }else{
  58. // 添加接收人记录及消息
  59. $receiver = explode(',',$sdata['receiver']);
  60. }
  61. $username = Db::name('user_info')->where('user_id',$sdata['user_id'])->value('name');
  62. foreach ($receiver as $k=>$v){
  63. $d = [
  64. 'org_id' => $sdata['org_id'],
  65. 'user_id' => $v,
  66. 'create_time' => $curTime,
  67. 'is_read' => 0,
  68. 'work_news_id' => $workReportId,
  69. 'type' => $type
  70. ];
  71. $wrrid = Db::name('work_news_receiver')->insertGetId($d);
  72. if(!$wrrid){
  73. \exception('提交失败');
  74. }
  75. $typename = Db::name('work_news_type')->where('id',$data['work_type_id'])->value('title');
  76. $context = $username.'提交的'.$typename.'工作动态';
  77. $ret = model('Message')->add(3,$wrrid,13,$v,$sdata['org_id'],$context);
  78. if(!$ret){
  79. \exception('提交失败');
  80. }
  81. }
  82. }
  83. $content = json_decode($data['content'],true);
  84. if($content){
  85. foreach ($content as $k=>$v){
  86. if($v['content'] || $v['date']){
  87. Db::name('work_news_item')->insert([
  88. 'work_news_id' => $id,
  89. 'content' => $v['content'],
  90. 'date' => empty($v['date'])?null:$v['date']
  91. ]);
  92. }
  93. }
  94. }
  95. Db::commit();
  96. }catch (Exception $e){
  97. Db::rollback();
  98. HelpHander::error($e->getMessage());
  99. }
  100. return true;
  101. }
  102. public function info($id){
  103. $info = Db::name('work_news')
  104. ->where('id',$id)
  105. ->where('status',1)
  106. ->field('id,user_id,work_type_id,create_time,files,audit_status')
  107. ->find();
  108. if(!$info){
  109. HelpHander::error('数据不存在');
  110. }
  111. $info['typeName'] = Db::name('work_news_type')->where('id',$info['work_type_id'])->value('title');
  112. $info['userName'] = Db::name('user_info')->where('user_id',$info['user_id'])->value('name');
  113. $content = Db::name('work_news_item')->where('work_news_id',$id)->field('content,date')->select();
  114. $info['content'] = json_encode($content);
  115. $receiver = Db::name('work_news_receiver')
  116. ->alias('wnr')
  117. ->join('user_info ui','ui.user_id = wnr.user_id')
  118. ->where('wnr.work_news_id',$info['id'])
  119. ->column('ui.name');
  120. $info['receiver'] = $receiver?implode(',',$receiver):'';
  121. return $info;
  122. }
  123. public function receiveInfo($id){
  124. $info = Db::name('work_news_receiver')
  125. ->alias('wrr')
  126. ->join('work_news wr','wr.id = wrr.work_news_id')
  127. ->where('wrr.id',$id)
  128. ->where('wr.status',1)
  129. ->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')
  130. ->find();
  131. if(!$info){
  132. HelpHander::error('数据不存在');
  133. }
  134. $info['typeName'] = Db::name('work_news_type')->where('id',$info['work_type_id'])->value('title');
  135. $info['userName'] = Db::name('user_info')->where('user_id',$info['user_id'])->value('name');
  136. if($info['is_read'] == 0){
  137. Db::name('work_news_receiver')->where('id',$id)->update(['is_read'=>1,'read_time'=>date('Y-m-d H:i:s')]);
  138. $msgid = Db::name('message')
  139. ->where('status',0)
  140. ->where('type',7)
  141. ->where('user_id',$info['receiver'])
  142. ->where('bus_type',12)
  143. ->where('bus_id',$id)
  144. ->value('id');
  145. if($msgid){
  146. model('Message')->updataStatus($msgid,$info['receiver']); // 对应标记为已读消息
  147. }
  148. }
  149. unset($info['receiver']);
  150. $content = Db::name('work_news_item')->where('work_news_id',$info['id'])->field('content,date')->select();
  151. $info['content'] = json_encode($content);
  152. $receiver = Db::name('work_news_receiver')
  153. ->alias('wnr')
  154. ->join('user_info ui','ui.user_id = wnr.user_id')
  155. ->where('wnr.work_news_id',$info['id'])
  156. ->column('ui.name');
  157. $info['receiver'] = $receiver?implode(',',$receiver):'';
  158. return $info;
  159. }
  160. // 我提交的工作汇报
  161. public function lists($page,$size,$userId,$orgId){
  162. $map[] = ['org_id','=',$orgId];
  163. $map[] = ['user_id','=',$userId];
  164. $map[] = ['status','=',1];
  165. $lists = $this
  166. ->where($map)
  167. ->page($page,$size)
  168. ->order('id desc')
  169. ->field('id,user_id,work_type_id,create_time,status,audit_status')
  170. ->select();
  171. $lists = $lists?$lists->toArray():[];
  172. foreach ($lists as $k=>$v){
  173. $lists[$k]['typeName'] = Db::name('work_news_type')->where('id',$v['work_type_id'])->value('title');
  174. $lists[$k]['userName'] = Db::name('user_info')->where('user_id',$v['user_id'])->value('name');
  175. //是否可以撤回
  176. $count = Db::name('work_news_receiver')->where('work_news_id',$v['id'])->where('is_read',1)->count();
  177. $lists[$k]['is_read'] = $count > 0?1:0;
  178. }
  179. $total = $this->where($map)->count();
  180. $data = [
  181. 'total' => $total,
  182. 'list' => $lists
  183. ];
  184. return $data;
  185. }
  186. // 我收到的工作汇报
  187. public function receiveList($page,$size,$userId,$orgId){
  188. $map[] = ['wrr.org_id','=',$orgId];
  189. $map[] = ['wrr.user_id','=',$userId];
  190. $map[] = ['wr.status','=',1];
  191. $lists = Db::name('work_news_receiver')
  192. ->alias('wrr')
  193. ->join('work_news wr','wr.id = wrr.work_news_id')
  194. ->where($map)
  195. ->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')
  196. ->page($page,$size)
  197. ->order('wrr.id desc')
  198. ->select();
  199. $lists = $lists?$lists:[];
  200. foreach ($lists as $k=>$v){
  201. $lists[$k]['typeName'] = Db::name('work_news_type')->where('id',$v['work_type_id'])->value('title');
  202. $lists[$k]['userName'] = Db::name('user_info')->where('user_id',$v['user_id'])->value('name');
  203. }
  204. $total = Db::name('work_news_receiver')
  205. ->alias('wrr')
  206. ->join('work_news wr','wr.id = wrr.work_news_id')
  207. ->where($map)
  208. ->count();
  209. $data = [
  210. 'total' => $total,
  211. 'list' => $lists
  212. ];
  213. return $data;
  214. }
  215. public function myReceiver($userId,$orgId){
  216. $receiver = Db::name('work_news')
  217. ->where('org_id',$orgId)
  218. ->where('user_id',$userId)
  219. ->order('id desc')
  220. ->value('receiver');
  221. $receiver = '100006,100135'; // 暂时默认两人
  222. if(!$receiver){
  223. return [];
  224. }
  225. $receiver = explode(',',$receiver);
  226. $lists = Db::name('user')
  227. ->alias('u')
  228. ->join('user_info ui','ui.user_id = u.id')
  229. ->where('u.id','in',$receiver)
  230. ->where('u.del',0)
  231. ->where('u.enable',1)
  232. ->field('u.id,ui.name')
  233. ->select();
  234. $lists = $lists?$lists:[];
  235. foreach ($lists as $k=>$v){
  236. $jobs = Db::name('user_job')
  237. ->alias('uj')
  238. ->join('job j','j.id = uj.job_id')
  239. ->where('uj.user_id',$v['id'])
  240. ->where('j.del',0)
  241. ->where('j.enable',1)
  242. ->field('j.*')
  243. ->select();
  244. $lists[$k]['listJob'] = $jobs?$jobs:[];
  245. }
  246. return $lists;
  247. }
  248. public function auditor($userId,$orgId){
  249. $receiver = Db::name('work_news')
  250. ->where('org_id',$orgId)
  251. ->where('user_id',$userId)
  252. ->order('id desc')
  253. ->value('auditor');
  254. if(!$receiver){
  255. return [];
  256. }
  257. $lists = Db::name('user')
  258. ->alias('u')
  259. ->join('user_info ui','ui.user_id = u.id')
  260. ->where('u.id',$receiver)
  261. ->where('u.del',0)
  262. ->where('u.enable',1)
  263. ->field('u.id,ui.name')
  264. ->select();
  265. $lists = $lists?$lists:[];
  266. foreach ($lists as $k=>$v){
  267. $jobs = Db::name('user_job')
  268. ->alias('uj')
  269. ->join('job j','j.id = uj.job_id')
  270. ->where('uj.user_id',$v['id'])
  271. ->where('j.del',0)
  272. ->where('j.enable',1)
  273. ->field('j.*')
  274. ->select();
  275. $lists[$k]['listJob'] = $jobs?$jobs:[];
  276. }
  277. return $lists;
  278. }
  279. // 获取工作汇报未读数量
  280. public function unReadNums($userId,$orgId){
  281. $count = Db::name('work_news_receiver')
  282. ->where('is_read',0)
  283. ->where('user_id',$userId)
  284. ->where('org_id',$orgId)
  285. ->count();
  286. return $count;
  287. }
  288. public function cancel($id,$userId){
  289. $info = Db::name('work_news')->where('id',$id)->where('user_id',$userId)->find();
  290. if(!$info){
  291. HelpHander::error('记录不存在');
  292. }
  293. if($info['status'] == 0){
  294. HelpHander::error('记录已撤销');
  295. }
  296. //是否可以撤回
  297. $count = Db::name('work_news_receiver')->where('work_news_id',$id)->where('is_read',1)->count();
  298. if($count > 0){
  299. HelpHander::error('上级已阅读,无法撤销');
  300. }
  301. Db::startTrans();
  302. try{
  303. $res = Db::name('work_news')->where('id',$id)->update(['status' => 0,'update_time' => date('Y-m-d H:i:s')]);
  304. if(!$res){
  305. \exception('操作失败');
  306. }
  307. // 删除上级记录
  308. $lists = Db::name('work_news_receiver')->where('work_news_id',$id)->select();
  309. foreach ($lists as $k=>$v){
  310. Db::name('work_news_receiver')->where('id',$v['id'])->delete();
  311. Db::name('message')->where('type',3)->where('bus_id',$v['id'])->where('bus_type',13)->delete(); //原数据type=7
  312. }
  313. Db::commit();
  314. }catch (Exception $e){
  315. Db::rollback();
  316. HelpHander::error('操作失败');
  317. }
  318. return true;
  319. }
  320. public function itemLists($page,$size,$userId,$orgId,$content,$workTypeId,$name){
  321. if($content){
  322. $map[] = ['wi.content','like','%'.$content.'%'];
  323. $map2[] = ['wi.content','like','%'.$content.'%'];
  324. }
  325. if($workTypeId > 0){
  326. $map[] = ['wr.work_type_id','=',$workTypeId];
  327. $map2[] = ['wr.work_type_id','=',$workTypeId];
  328. }
  329. if($name){
  330. $map[] = ['ui.name','like','%'.$name.'%'];
  331. $map2[] = ['ui.name','like','%'.$name.'%'];
  332. }
  333. $map[] = ['wrr.org_id','=',$orgId];
  334. $map[] = ['wrr.user_id','=',$userId];
  335. $map[] = ['wr.status','=',1];
  336. $map[] = ['wr.audit_status','=',1];
  337. $map2[] = ['wrr.org_id','=',$orgId];
  338. $map2[] = ['wr.user_id','=',$userId];
  339. $map2[] = ['wr.status','=',1];
  340. $map2[] = ['wr.audit_status','=',1];
  341. $lists = Db::name('work_news_item')
  342. ->alias('wi')
  343. ->join('work_news wr','wr.id = wi.work_news_id')
  344. ->join('user_info ui','ui.user_id = wr.user_id')
  345. ->join('work_news_receiver wrr','wrr.work_news_id = wr.id','left')
  346. ->whereOr([ $map, $map2 ])
  347. ->field('wr.id,wr.user_id,wr.work_type_id,wr.create_time,wi.content,wi.date,wi.id as item_id')
  348. ->page($page,$size)
  349. ->group('wi.id')
  350. ->order('wi.id desc')
  351. ->select();
  352. $lists = $lists?$lists:[];
  353. foreach ($lists as $k=>$v){
  354. $lists[$k]['typeName'] = Db::name('work_news_type')->where('id',$v['work_type_id'])->value('title');
  355. $lists[$k]['userName'] = Db::name('user_info')->where('user_id',$v['user_id'])->value('name');
  356. $lists[$k]['label_id'] = 0;
  357. $lists[$k]['label_name'] = '';
  358. $label = Db::name('work_news_item_label')->where('user_id',$userId)->where('item_id',$v['item_id'])->find();
  359. if($label){
  360. $lists[$k]['label_id'] = $label['id'];
  361. if($label['label'] == 1){
  362. $lists[$k]['label_name'] = '留存';
  363. }else if($label['label'] == 2){
  364. $lists[$k]['label_name'] = '一般';
  365. }else{
  366. $lists[$k]['label_name'] = '重要';
  367. }
  368. }
  369. }
  370. $total = Db::name('work_news_item')
  371. ->alias('wi')
  372. ->join('work_news wr','wr.id = wi.work_news_id')
  373. ->join('user_info ui','ui.user_id = ui.user_id')
  374. ->join('work_news_receiver wrr','wrr.work_news_id = wr.id','left')
  375. ->whereOr([ $map, $map2 ])
  376. ->group('wi.id')
  377. ->count();
  378. $data = [
  379. 'total' => $total,
  380. 'list' => $lists
  381. ];
  382. return $data;
  383. }
  384. public function itemAll($userId,$orgId,$content,$workTypeId,$name){
  385. if($content){
  386. $map[] = ['wi.content','like','%'.$content.'%'];
  387. $map2[] = ['wi.content','like','%'.$content.'%'];
  388. }
  389. if($workTypeId > 0){
  390. $map[] = ['wr.work_type_id','=',$workTypeId];
  391. $map2[] = ['wr.work_type_id','=',$workTypeId];
  392. }
  393. if($name){
  394. $map[] = ['ui.name','like','%'.$name.'%'];
  395. $map2[] = ['ui.name','like','%'.$name.'%'];
  396. }
  397. $map[] = ['wrr.org_id','=',$orgId];
  398. $map[] = ['wrr.user_id','=',$userId];
  399. $map[] = ['wr.status','=',1];
  400. $map[] = ['wr.audit_status','=',1];
  401. $map2[] = ['wrr.org_id','=',$orgId];
  402. $map2[] = ['wr.user_id','=',$userId];
  403. $map2[] = ['wr.status','=',1];
  404. $map2[] = ['wr.audit_status','=',1];
  405. $lists = Db::name('work_news_item')
  406. ->alias('wi')
  407. ->join('work_news wr','wr.id = wi.work_news_id')
  408. ->join('user_info ui','ui.user_id = ui.user_id')
  409. ->join('work_news_receiver wrr','wrr.work_news_id = wr.id','left')
  410. ->whereOr([ $map, $map2 ])
  411. ->field('wr.id,wr.user_id,wr.work_type_id,wr.create_time,wi.content,wi.date,wi.id as item_id')
  412. ->group('wi.id')
  413. ->order('wi.id desc')
  414. ->select();
  415. $lists = $lists?$lists:[];
  416. foreach ($lists as $k=>$v){
  417. $lists[$k]['typeName'] = Db::name('work_news_type')->where('id',$v['work_type_id'])->value('title');
  418. $lists[$k]['userName'] = Db::name('user_info')->where('user_id',$v['user_id'])->value('name');
  419. $lists[$k]['label_id'] = 0;
  420. $lists[$k]['label_name'] = '';
  421. $label = Db::name('work_news_item_label')->where('user_id',$userId)->where('item_id',$v['item_id'])->find();
  422. if($label){
  423. $lists[$k]['label_id'] = $label['id'];
  424. if($label['label'] == 1){
  425. $lists[$k]['label_name'] = '留存';
  426. }else if($label['label'] == 2){
  427. $lists[$k]['label_name'] = '一般';
  428. }else{
  429. $lists[$k]['label_name'] = '重要';
  430. }
  431. }
  432. }
  433. $columns = [
  434. ["title" => "姓名","key" => "userName"],
  435. ["title" => "类型","key" => "typeName"],
  436. ["title" => "工作内容","key" => "content"],
  437. ["title" => "完成时间","key" => "date"],
  438. ["title" => "标注","key" => "labelName"],
  439. ];
  440. $data = [
  441. 'columns' => $columns,
  442. 'list' => $lists
  443. ];
  444. return $data;
  445. }
  446. public function allItemLists($page,$size,$userId,$orgId,$content,$workTypeId,$name,$label){
  447. if($content){
  448. $map[] = ['wi.content','like','%'.$content.'%'];
  449. }
  450. if($workTypeId > 0){
  451. $map[] = ['wr.work_type_id','=',$workTypeId];
  452. }
  453. if($name){
  454. $map[] = ['ui.name','like','%'.$name.'%'];
  455. }
  456. if($label > 0){
  457. $ids = Db::name('work_news_item_label')->where('label',$label)->where('user_id',$userId)->column('item_id');
  458. if($ids){
  459. $map[] = ['wi.id','in',$ids];
  460. }
  461. }
  462. $map[] = ['wrr.org_id','=',$orgId];
  463. $map[] = ['wr.status','=',1];
  464. $map[] = ['wr.audit_status','=',1];
  465. $lists = Db::name('work_news_item')
  466. ->alias('wi')
  467. ->join('work_news wr','wr.id = wi.work_news_id')
  468. ->join('user_info ui','ui.user_id = wr.user_id')
  469. ->join('work_news_receiver wrr','wrr.work_news_id = wr.id','left')
  470. ->where($map)
  471. ->field('wr.id,wr.user_id,wr.work_type_id,wr.create_time,wi.content,wi.date,wi.id as item_id')
  472. ->page($page,$size)
  473. ->order('wi.id desc')
  474. ->group('wi.id')
  475. ->select();
  476. $lists = $lists?$lists:[];
  477. foreach ($lists as $k=>$v){
  478. $lists[$k]['typeName'] = Db::name('work_news_type')->where('id',$v['work_type_id'])->value('title');
  479. $lists[$k]['userName'] = Db::name('user_info')->where('user_id',$v['user_id'])->value('name');
  480. $lists[$k]['label_id'] = 0;
  481. $lists[$k]['label_name'] = '';
  482. $label = Db::name('work_news_item_label')->where('user_id',$userId)->where('item_id',$v['item_id'])->find();
  483. if($label){
  484. $lists[$k]['label_id'] = $label['id'];
  485. if($label['label'] == 1){
  486. $lists[$k]['label_name'] = '留存';
  487. }else if($label['label'] == 2){
  488. $lists[$k]['label_name'] = '一般';
  489. }else{
  490. $lists[$k]['label_name'] = '重要';
  491. }
  492. }
  493. }
  494. $total = Db::name('work_news_item')
  495. ->alias('wi')
  496. ->join('work_news wr','wr.id = wi.work_news_id')
  497. ->join('user_info ui','ui.user_id = wr.user_id')
  498. ->join('work_news_receiver wrr','wrr.work_news_id = wr.id','left')
  499. ->where($map)
  500. ->group('wi.id')
  501. ->count();
  502. $data = [
  503. 'total' => $total,
  504. 'list' => $lists
  505. ];
  506. return $data;
  507. }
  508. public function allItemAll($userId,$orgId,$content,$workTypeId,$name,$label){
  509. if($content){
  510. $map[] = ['wi.content','like','%'.$content.'%'];
  511. }
  512. if($workTypeId > 0){
  513. $map[] = ['wr.work_type_id','=',$workTypeId];
  514. }
  515. if($name){
  516. $map[] = ['ui.name','like','%'.$name.'%'];
  517. }
  518. if($label > 0){
  519. $ids = Db::name('work_news_item_label')->where('label',$label)->where('user_id',$userId)->column('item_id');
  520. if($ids){
  521. $map[] = ['wi.id','in',$ids];
  522. }
  523. }
  524. $map[] = ['wrr.org_id','=',$orgId];
  525. $map[] = ['wr.status','=',1];
  526. $map[] = ['wr.audit_status','=',1];
  527. $lists = Db::name('work_news_item')
  528. ->alias('wi')
  529. ->join('work_news wr','wr.id = wi.work_news_id')
  530. ->join('user_info ui','ui.user_id = wr.user_id')
  531. ->join('work_news_receiver wrr','wrr.work_news_id = wr.id','left')
  532. ->where($map)
  533. ->field('wr.id,wr.user_id,wr.work_type_id,wr.create_time,wi.content,wi.date,wi.id as item_id')
  534. ->order('wi.id desc')
  535. ->group('wi.id')
  536. ->select();
  537. $lists = $lists?$lists:[];
  538. foreach ($lists as $k=>$v){
  539. $lists[$k]['typeName'] = Db::name('work_news_type')->where('id',$v['work_type_id'])->value('title');
  540. $lists[$k]['userName'] = Db::name('user_info')->where('user_id',$v['user_id'])->value('name');
  541. $lists[$k]['label_id'] = 0;
  542. $lists[$k]['label_name'] = '';
  543. $label = Db::name('work_news_item_label')->where('user_id',$userId)->where('item_id',$v['item_id'])->find();
  544. if($label){
  545. $lists[$k]['label_id'] = $label['id'];
  546. if($label['label'] == 1){
  547. $lists[$k]['label_name'] = '留存';
  548. }else if($label['label'] == 2){
  549. $lists[$k]['label_name'] = '一般';
  550. }else{
  551. $lists[$k]['label_name'] = '重要';
  552. }
  553. }
  554. }
  555. $columns = [
  556. ["title" => "姓名","key" => "userName"],
  557. ["title" => "类型","key" => "typeName"],
  558. ["title" => "工作内容","key" => "content"],
  559. ["title" => "完成时间","key" => "date"],
  560. ["title" => "标注","key" => "labelName"],
  561. ];
  562. $data = [
  563. 'columns' => $columns,
  564. 'list' => $lists
  565. ];
  566. return $data;
  567. }
  568. public function addLabel($itemId,$type,$userId){
  569. $data = [
  570. 'item_id' => $itemId,
  571. 'label' => $type,
  572. 'user_id' => $userId
  573. ];
  574. $ret = Db::name('work_news_item_label')->insertGetId($data);
  575. if(!$ret){
  576. HelpHander::error('操作失败');
  577. }
  578. return true;
  579. }
  580. public function delLabel($id,$userId){
  581. $ret = Db::name('work_news_item_label')->where('id',$id)->where('user_id',$userId)->delete();
  582. if(!$ret){
  583. HelpHander::error('操作失败');
  584. }
  585. return true;
  586. }
  587. public function unapply($userId){
  588. $lists = Db::name('work_news_receiver')
  589. ->alias('wnr')
  590. ->join('work_news wn','wn.id = wnr.work_news_id')
  591. ->join('user_info ui','ui.user_id = wn.user_id')
  592. ->join('work_news_type unt','unt.id = wn.work_type_id')
  593. ->where('wnr.user_id',$userId)
  594. ->where('wnr.is_read',0)
  595. ->where('wn.status',1)
  596. ->field('wnr.id,ui.name,unt.title,wnr.create_time')
  597. ->select();
  598. $lists = $lists?$lists:[];
  599. foreach ($lists as $k=>$v){
  600. $lists[$k]['title'] = $v['name']."提交的".$v['title'];
  601. unset($lists[$k]['name']);
  602. }
  603. return $lists;
  604. }
  605. public function agree($id){
  606. $info = Db::name('work_news')->where('id',$id)->find();
  607. if(!$info){
  608. HelpHander::error('记录不存在');
  609. }
  610. if($info['audit_status'] != 0){
  611. HelpHander::error('记录已审核');
  612. }
  613. $curTime = date('Y-m-d H:i:s');
  614. Db::startTrans();
  615. try{
  616. $res = Db::name('work_news')->where('id',$id)->update(['audit_status' => 1,'audit_time' => $curTime]);
  617. if(!$res){
  618. \exception('操作失败');
  619. }
  620. $receiver = $info['receiver']?explode(',',$info['receiver']):[];
  621. $username = Db::name('user_info')->where('user_id',$info['user_id'])->value('name');
  622. foreach ($receiver as $k=>$v){
  623. $d = [
  624. 'org_id' => $info['org_id'],
  625. 'user_id' => $v,
  626. 'create_time' => $curTime,
  627. 'is_read' => 0,
  628. 'work_news_id' => $id,
  629. 'type' => 0
  630. ];
  631. $wrrid = Db::name('work_news_receiver')->insertGetId($d);
  632. if(!$wrrid){
  633. \exception('提交失败');
  634. }
  635. $typename = Db::name('work_news_type')->where('id',$info['work_type_id'])->value('title');
  636. $context = $username.'提交的'.$typename.'工作动态';
  637. $ret = model('Message')->add(3,$wrrid,13,$v,$info['org_id'],$context);
  638. if(!$ret){
  639. \exception('提交失败');
  640. }
  641. }
  642. Db::commit();
  643. }catch (Exception $e){
  644. Db::rollback();
  645. HelpHander::error('操作失败');
  646. }
  647. return true;
  648. }
  649. public function disagree($id){
  650. $info = Db::name('work_news')->where('id',$id)->find();
  651. if(!$info){
  652. HelpHander::error('记录不存在');
  653. }
  654. if($info['audit_status'] != 0){
  655. HelpHander::error('记录已审核');
  656. }
  657. $curTime = date('Y-m-d H:i:s');
  658. Db::startTrans();
  659. try{
  660. $res = Db::name('work_news')->where('id',$id)->update(['audit_status' => 2,'audit_time' => $curTime]);
  661. if(!$res){
  662. \exception('操作失败');
  663. }
  664. Db::commit();
  665. }catch (Exception $e){
  666. Db::rollback();
  667. HelpHander::error('操作失败');
  668. }
  669. return true;
  670. }
  671. }