WorkReport.php 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298
  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 WorkReport extends Model
  8. {
  9. public function add(){
  10. $curTime = date('Y-m-d H:i:s');
  11. $data = [
  12. 'org_id' => input('orgId/d',0),
  13. 'user_id' => input('userId/d',0),
  14. 'work_type_id' => input('type/d',0),
  15. 'content' => htmlspecialchars(input('content','','')),
  16. 'receiver' => input('receiver','','trim'),
  17. 'files' => input('files','','trim'),
  18. 'create_time' => $curTime,
  19. 'month' => input('month','','trim'),
  20. 'work1' => htmlspecialchars(input('work1','','trim')),
  21. 'work2' => htmlspecialchars(input('work2','','trim')),
  22. 'points' => input('points','','trim'),
  23. 'zx_total_sr' => input('zxTotalSr/f',0),
  24. 'xh_total_sr' => input('xhTotalSr/f',0),
  25. 'zx_total_zc' => input('zxTotalZc/f',0),
  26. 'xh_total_zc' => input('xhTotalZc/f',0),
  27. 'zx_total_gz' => input('zxTotalGz/f',0),
  28. 'xh_total_gz' => input('xhTotalGz/f',0),
  29. 'zx_total_yy' => input('zxTotalYy/f',0),
  30. 'xh_total_yy' => input('xhTotalYy/f',0),
  31. ];
  32. $data['receiver'] = check_exp_imp($data['receiver']);
  33. $data['points'] = check_exp_imp($data['points']);
  34. $result = validate('WorkReport')->check($data,[],'');
  35. if(true !== $result){
  36. HelpHander::error(validate('WorkReport')->getError());
  37. }
  38. Db::startTrans();
  39. try{
  40. $sdata = [
  41. 'org_id' => $data['org_id'],
  42. 'user_id' => $data['user_id'],
  43. 'work_type_id' => $data['work_type_id'],
  44. 'content' => $data['content'],
  45. 'receiver' => $data['receiver'],
  46. 'create_time' => $data['create_time'],
  47. 'files' => $data['files']
  48. ];
  49. $workReportId = Db::name('work_report')->insertGetId($sdata);
  50. if(!$workReportId){
  51. \exception('提交失败');
  52. }
  53. if($data['work_type_id'] == 1){
  54. $edata = [
  55. 'work_report_id' => $workReportId,
  56. 'month' => $data['month'],
  57. 'zx_total_sr' => $data['zx_total_sr'],
  58. 'xh_total_sr' => $data['xh_total_sr'],
  59. 'zx_total_zc' => $data['zx_total_zc'],
  60. 'xh_total_zc' => $data['xh_total_zc'],
  61. 'zx_total_gz' => $data['zx_total_gz'],
  62. 'xh_total_gz' => $data['xh_total_gz'],
  63. 'zx_total_yy' => $data['zx_total_yy'],
  64. 'xh_total_yy' => $data['xh_total_yy'],
  65. ];
  66. $ret = Db::name('work_report_finance')->insertGetId($edata);
  67. if(!$ret){
  68. \exception('提交失败');
  69. }
  70. } else if($data['work_type_id'] == 2){
  71. $edata = [
  72. 'work_report_id' => $workReportId,
  73. 'month' => $data['month'],
  74. 'work1' => $data['work1'],
  75. 'work2' => $data['work2'],
  76. ];
  77. $ret = Db::name('work_report_summary')->insertGetId($edata);
  78. if(!$ret){
  79. \exception('提交失败');
  80. }
  81. } else if($data['work_type_id'] == 3){
  82. $edata = [
  83. 'work_report_id' => $workReportId,
  84. 'month' => $data['month'],
  85. 'points' => $data['points'],
  86. ];
  87. $ret = Db::name('work_report_point')->insertGetId($edata);
  88. if(!$ret){
  89. \exception('提交失败');
  90. }
  91. }
  92. // 添加接收人记录及消息
  93. $receiver = explode(',',$data['receiver']);
  94. $username = Db::name('user_info')->where('user_id',$data['user_id'])->value('name');
  95. foreach ($receiver as $k=>$v){
  96. $d = [
  97. 'org_id' => $data['org_id'],
  98. 'user_id' => $v,
  99. 'create_time' => $curTime,
  100. 'is_read' => 0,
  101. 'work_report_id' => $workReportId
  102. ];
  103. $wrrid = Db::name('work_report_receiver')->insertGetId($d);
  104. if(!$wrrid){
  105. \exception('提交失败');
  106. }
  107. $typename = Db::name('work_type')->where('id',$data['work_type_id'])->value('title');
  108. $context = $username.'提交的'.$typename;
  109. $ret = model('Message')->add(5,$wrrid,6,$v,$data['org_id'],$context);
  110. if(!$ret){
  111. \exception('提交失败');
  112. }
  113. }
  114. Db::commit();
  115. }catch (Exception $e){
  116. Db::rollback();
  117. HelpHander::error($e->getMessage());
  118. }
  119. return true;
  120. }
  121. public function info($id){
  122. $info = Db::name('WorkReport')
  123. ->where('id',$id)
  124. ->field('id,user_id,work_type_id,content,create_time,files')
  125. ->find();
  126. if(!$info){
  127. HelpHander::error('数据不存在');
  128. }
  129. $info['typeName'] = Db::name('work_type')->where('id',$info['work_type_id'])->value('title');
  130. $info['userName'] = Db::name('user_info')->where('user_id',$info['user_id'])->value('name');
  131. if($info['work_type_id'] == 1){
  132. $ext = Db::name('work_report_finance')->where('work_report_id',$info['id'])->find();
  133. $info['finance'] = $ext;
  134. }else if($info['work_type_id'] == 2){
  135. $ext = Db::name('work_report_summary')->where('work_report_id',$info['id'])->find();
  136. $info['summary'] = $ext;
  137. }else if($info['work_type_id'] == 3){
  138. $ext = Db::name('work_report_point')->where('work_report_id',$info['id'])->find();
  139. $list = Db::name('work_point')->where('id','in',$ext['points'])->field('id,title')->order('id desc')->select();
  140. $ext['points'] = $list?$list:[];
  141. $info['point'] = $ext;
  142. }
  143. return $info;
  144. }
  145. public function receiveInfo($id){
  146. $info = Db::name('work_report_receiver')
  147. ->alias('wrr')
  148. ->join('work_report wr','wr.id = wrr.work_report_id')
  149. ->where('wrr.id',$id)
  150. ->field('wr.id,wr.user_id,wr.files,wr.work_type_id,wr.content,wrr.create_time,wrr.is_read,wrr.read_time,wrr.user_id as receiver')
  151. ->find();
  152. if(!$info){
  153. HelpHander::error('数据不存在');
  154. }
  155. $info['typeName'] = Db::name('work_type')->where('id',$info['work_type_id'])->value('title');
  156. $info['userName'] = Db::name('user_info')->where('user_id',$info['user_id'])->value('name');
  157. if($info['work_type_id'] == 1){
  158. $ext = Db::name('work_report_finance')->where('work_report_id',$info['id'])->find();
  159. $info['finance'] = $ext;
  160. }else if($info['work_type_id'] == 2){
  161. $ext = Db::name('work_report_summary')->where('work_report_id',$info['id'])->find();
  162. $info['summary'] = $ext;
  163. }else if($info['work_type_id'] == 3){
  164. $ext = Db::name('work_report_point')->where('work_report_id',$info['id'])->find();
  165. $list = Db::name('work_point')->where('id','in',$ext['points'])->field('id,title')->order('id desc')->select();
  166. $ext['points'] = $list?$list:[];
  167. $info['point'] = $ext;
  168. }
  169. if($info['is_read'] == 0){
  170. Db::name('work_report_receiver')->where('id',$id)->update(['is_read'=>1,'read_time'=>date('Y-m-d H:i:s')]);
  171. $msgid = Db::name('message')
  172. ->where('status',0)
  173. ->where('type',5)
  174. ->where('user_id',$info['receiver'])
  175. ->where('bus_type',6)
  176. ->where('bus_id',$id)
  177. ->value('id');
  178. if($msgid){
  179. model('Message')->updataStatus($msgid,$info['receiver']); // 对应标记为已读消息
  180. }
  181. }
  182. unset($info['receiver']);
  183. return $info;
  184. }
  185. // 我提交的工作汇报
  186. public function lists($page,$size,$userId,$orgId){
  187. $map[] = ['org_id','=',$orgId];
  188. $map[] = ['user_id','=',$userId];
  189. $lists = $this
  190. ->where($map)
  191. ->page($page,$size)
  192. ->order('id desc')
  193. ->field('id,user_id,work_type_id,create_time')
  194. ->select();
  195. $lists = $lists?$lists->toArray():[];
  196. foreach ($lists as $k=>$v){
  197. $lists[$k]['typeName'] = Db::name('work_type')->where('id',$v['work_type_id'])->value('title');
  198. $lists[$k]['userName'] = Db::name('user_info')->where('user_id',$v['user_id'])->value('name');
  199. }
  200. $total = $this->where($map)->count();
  201. $data = [
  202. 'total' => $total,
  203. 'list' => $lists
  204. ];
  205. return $data;
  206. }
  207. // 我收到的工作汇报
  208. public function receiveList($page,$size,$userId,$orgId){
  209. $map[] = ['wrr.org_id','=',$orgId];
  210. $map[] = ['wrr.user_id','=',$userId];
  211. $lists = Db::name('work_report_receiver')
  212. ->alias('wrr')
  213. ->join('work_report wr','wr.id = wrr.work_report_id')
  214. ->where($map)
  215. ->field('wr.user_id,wr.work_type_id,wrr.create_time,wrr.work_report_id,wrr.is_read,wrr.id')
  216. ->page($page,$size)
  217. ->order('wrr.id desc')
  218. ->select();
  219. $lists = $lists?$lists:[];
  220. foreach ($lists as $k=>$v){
  221. $lists[$k]['typeName'] = Db::name('work_type')->where('id',$v['work_type_id'])->value('title');
  222. $lists[$k]['userName'] = Db::name('user_info')->where('user_id',$v['user_id'])->value('name');
  223. }
  224. $total = Db::name('work_report_receiver')
  225. ->alias('wrr')
  226. ->join('work_report wr','wr.id = wrr.work_report_id')
  227. ->where($map)
  228. ->count();
  229. $data = [
  230. 'total' => $total,
  231. 'list' => $lists
  232. ];
  233. return $data;
  234. }
  235. public function myReceiver($userId,$orgId){
  236. $receiver = Db::name('work_report')
  237. ->where('org_id',$orgId)
  238. ->where('user_id',$userId)
  239. ->order('id desc')
  240. ->value('receiver');
  241. if(!$receiver){
  242. return [];
  243. }
  244. $receiver = explode(',',$receiver);
  245. $lists = Db::name('user')
  246. ->alias('u')
  247. ->join('user_info ui','ui.user_id = u.id')
  248. ->where('u.id','in',$receiver)
  249. ->where('u.del',0)
  250. ->where('u.enable',1)
  251. ->field('u.id,ui.name')
  252. ->select();
  253. $lists = $lists?$lists:[];
  254. foreach ($lists as $k=>$v){
  255. $jobs = Db::name('user_job')
  256. ->alias('uj')
  257. ->join('job j','j.id = uj.job_id')
  258. ->where('uj.user_id',$v['id'])
  259. ->where('j.del',0)
  260. ->where('j.enable',1)
  261. ->field('j.*')
  262. ->select();
  263. $lists[$k]['listJob'] = $jobs?$jobs:[];
  264. }
  265. return $lists;
  266. }
  267. // 获取工作汇报未读数量
  268. public function unReadNums($userId,$orgId){
  269. $count = Db::name('work_record_receiver')
  270. ->where('is_read',0)
  271. ->where('user_id',$userId)
  272. ->where('org_id',$orgId)
  273. ->count();
  274. return $count;
  275. }
  276. }