123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298 |
- <?php
- namespace app\common\model;
- use app\hander\HelpHander;
- use think\Db;
- use think\Exception;
- use think\Model;
- class WorkReport extends Model
- {
- public function add(){
- $curTime = date('Y-m-d H:i:s');
- $data = [
- 'org_id' => input('orgId/d',0),
- 'user_id' => input('userId/d',0),
- 'work_type_id' => input('type/d',0),
- 'content' => htmlspecialchars(input('content','','')),
- 'receiver' => input('receiver','','trim'),
- 'files' => input('files','','trim'),
- 'create_time' => $curTime,
- 'month' => input('month','','trim'),
- 'work1' => htmlspecialchars(input('work1','','trim')),
- 'work2' => htmlspecialchars(input('work2','','trim')),
- 'points' => input('points','','trim'),
- 'zx_total_sr' => input('zxTotalSr/f',0),
- 'xh_total_sr' => input('xhTotalSr/f',0),
- 'zx_total_zc' => input('zxTotalZc/f',0),
- 'xh_total_zc' => input('xhTotalZc/f',0),
- 'zx_total_gz' => input('zxTotalGz/f',0),
- 'xh_total_gz' => input('xhTotalGz/f',0),
- 'zx_total_yy' => input('zxTotalYy/f',0),
- 'xh_total_yy' => input('xhTotalYy/f',0),
- ];
- $data['receiver'] = check_exp_imp($data['receiver']);
- $data['points'] = check_exp_imp($data['points']);
- $result = validate('WorkReport')->check($data,[],'');
- if(true !== $result){
- HelpHander::error(validate('WorkReport')->getError());
- }
- Db::startTrans();
- try{
- $sdata = [
- 'org_id' => $data['org_id'],
- 'user_id' => $data['user_id'],
- 'work_type_id' => $data['work_type_id'],
- 'content' => $data['content'],
- 'receiver' => $data['receiver'],
- 'create_time' => $data['create_time'],
- 'files' => $data['files']
- ];
- $workReportId = Db::name('work_report')->insertGetId($sdata);
- if(!$workReportId){
- \exception('提交失败');
- }
- if($data['work_type_id'] == 1){
- $edata = [
- 'work_report_id' => $workReportId,
- 'month' => $data['month'],
- 'zx_total_sr' => $data['zx_total_sr'],
- 'xh_total_sr' => $data['xh_total_sr'],
- 'zx_total_zc' => $data['zx_total_zc'],
- 'xh_total_zc' => $data['xh_total_zc'],
- 'zx_total_gz' => $data['zx_total_gz'],
- 'xh_total_gz' => $data['xh_total_gz'],
- 'zx_total_yy' => $data['zx_total_yy'],
- 'xh_total_yy' => $data['xh_total_yy'],
- ];
- $ret = Db::name('work_report_finance')->insertGetId($edata);
- if(!$ret){
- \exception('提交失败');
- }
- } else if($data['work_type_id'] == 2){
- $edata = [
- 'work_report_id' => $workReportId,
- 'month' => $data['month'],
- 'work1' => $data['work1'],
- 'work2' => $data['work2'],
- ];
- $ret = Db::name('work_report_summary')->insertGetId($edata);
- if(!$ret){
- \exception('提交失败');
- }
- } else if($data['work_type_id'] == 3){
- $edata = [
- 'work_report_id' => $workReportId,
- 'month' => $data['month'],
- 'points' => $data['points'],
- ];
- $ret = Db::name('work_report_point')->insertGetId($edata);
- if(!$ret){
- \exception('提交失败');
- }
- }
- // 添加接收人记录及消息
- $receiver = explode(',',$data['receiver']);
- $username = Db::name('user_info')->where('user_id',$data['user_id'])->value('name');
- foreach ($receiver as $k=>$v){
- $d = [
- 'org_id' => $data['org_id'],
- 'user_id' => $v,
- 'create_time' => $curTime,
- 'is_read' => 0,
- 'work_report_id' => $workReportId
- ];
- $wrrid = Db::name('work_report_receiver')->insertGetId($d);
- if(!$wrrid){
- \exception('提交失败');
- }
- $typename = Db::name('work_type')->where('id',$data['work_type_id'])->value('title');
- $context = $username.'提交的'.$typename;
- $ret = model('Message')->add(5,$wrrid,6,$v,$data['org_id'],$context);
- if(!$ret){
- \exception('提交失败');
- }
- }
- Db::commit();
- }catch (Exception $e){
- Db::rollback();
- HelpHander::error($e->getMessage());
- }
- return true;
- }
- public function info($id){
- $info = Db::name('WorkReport')
- ->where('id',$id)
- ->field('id,user_id,work_type_id,content,create_time,files')
- ->find();
- if(!$info){
- HelpHander::error('数据不存在');
- }
- $info['typeName'] = Db::name('work_type')->where('id',$info['work_type_id'])->value('title');
- $info['userName'] = Db::name('user_info')->where('user_id',$info['user_id'])->value('name');
- if($info['work_type_id'] == 1){
- $ext = Db::name('work_report_finance')->where('work_report_id',$info['id'])->find();
- $info['finance'] = $ext;
- }else if($info['work_type_id'] == 2){
- $ext = Db::name('work_report_summary')->where('work_report_id',$info['id'])->find();
- $info['summary'] = $ext;
- }else if($info['work_type_id'] == 3){
- $ext = Db::name('work_report_point')->where('work_report_id',$info['id'])->find();
- $list = Db::name('work_point')->where('id','in',$ext['points'])->field('id,title')->order('id desc')->select();
- $ext['points'] = $list?$list:[];
- $info['point'] = $ext;
- }
- return $info;
- }
- public function receiveInfo($id){
- $info = Db::name('work_report_receiver')
- ->alias('wrr')
- ->join('work_report wr','wr.id = wrr.work_report_id')
- ->where('wrr.id',$id)
- ->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')
- ->find();
- if(!$info){
- HelpHander::error('数据不存在');
- }
- $info['typeName'] = Db::name('work_type')->where('id',$info['work_type_id'])->value('title');
- $info['userName'] = Db::name('user_info')->where('user_id',$info['user_id'])->value('name');
- if($info['work_type_id'] == 1){
- $ext = Db::name('work_report_finance')->where('work_report_id',$info['id'])->find();
- $info['finance'] = $ext;
- }else if($info['work_type_id'] == 2){
- $ext = Db::name('work_report_summary')->where('work_report_id',$info['id'])->find();
- $info['summary'] = $ext;
- }else if($info['work_type_id'] == 3){
- $ext = Db::name('work_report_point')->where('work_report_id',$info['id'])->find();
- $list = Db::name('work_point')->where('id','in',$ext['points'])->field('id,title')->order('id desc')->select();
- $ext['points'] = $list?$list:[];
- $info['point'] = $ext;
- }
- if($info['is_read'] == 0){
- Db::name('work_report_receiver')->where('id',$id)->update(['is_read'=>1,'read_time'=>date('Y-m-d H:i:s')]);
- $msgid = Db::name('message')
- ->where('status',0)
- ->where('type',5)
- ->where('user_id',$info['receiver'])
- ->where('bus_type',6)
- ->where('bus_id',$id)
- ->value('id');
- if($msgid){
- model('Message')->updataStatus($msgid,$info['receiver']); // 对应标记为已读消息
- }
- }
- unset($info['receiver']);
- return $info;
- }
- // 我提交的工作汇报
- public function lists($page,$size,$userId,$orgId){
- $map[] = ['org_id','=',$orgId];
- $map[] = ['user_id','=',$userId];
- $lists = $this
- ->where($map)
- ->page($page,$size)
- ->order('id desc')
- ->field('id,user_id,work_type_id,create_time')
- ->select();
- $lists = $lists?$lists->toArray():[];
- foreach ($lists as $k=>$v){
- $lists[$k]['typeName'] = Db::name('work_type')->where('id',$v['work_type_id'])->value('title');
- $lists[$k]['userName'] = Db::name('user_info')->where('user_id',$v['user_id'])->value('name');
- }
- $total = $this->where($map)->count();
- $data = [
- 'total' => $total,
- 'list' => $lists
- ];
- return $data;
- }
- // 我收到的工作汇报
- public function receiveList($page,$size,$userId,$orgId){
- $map[] = ['wrr.org_id','=',$orgId];
- $map[] = ['wrr.user_id','=',$userId];
- $lists = Db::name('work_report_receiver')
- ->alias('wrr')
- ->join('work_report wr','wr.id = wrr.work_report_id')
- ->where($map)
- ->field('wr.user_id,wr.work_type_id,wrr.create_time,wrr.work_report_id,wrr.is_read,wrr.id')
- ->page($page,$size)
- ->order('wrr.id desc')
- ->select();
- $lists = $lists?$lists:[];
- foreach ($lists as $k=>$v){
- $lists[$k]['typeName'] = Db::name('work_type')->where('id',$v['work_type_id'])->value('title');
- $lists[$k]['userName'] = Db::name('user_info')->where('user_id',$v['user_id'])->value('name');
- }
- $total = Db::name('work_report_receiver')
- ->alias('wrr')
- ->join('work_report wr','wr.id = wrr.work_report_id')
- ->where($map)
- ->count();
- $data = [
- 'total' => $total,
- 'list' => $lists
- ];
- return $data;
- }
- public function myReceiver($userId,$orgId){
- $receiver = Db::name('work_report')
- ->where('org_id',$orgId)
- ->where('user_id',$userId)
- ->order('id desc')
- ->value('receiver');
- if(!$receiver){
- return [];
- }
- $receiver = explode(',',$receiver);
- $lists = Db::name('user')
- ->alias('u')
- ->join('user_info ui','ui.user_id = u.id')
- ->where('u.id','in',$receiver)
- ->where('u.del',0)
- ->where('u.enable',1)
- ->field('u.id,ui.name')
- ->select();
- $lists = $lists?$lists:[];
- foreach ($lists as $k=>$v){
- $jobs = Db::name('user_job')
- ->alias('uj')
- ->join('job j','j.id = uj.job_id')
- ->where('uj.user_id',$v['id'])
- ->where('j.del',0)
- ->where('j.enable',1)
- ->field('j.*')
- ->select();
- $lists[$k]['listJob'] = $jobs?$jobs:[];
- }
- return $lists;
- }
- // 获取工作汇报未读数量
- public function unReadNums($userId,$orgId){
- $count = Db::name('work_record_receiver')
- ->where('is_read',0)
- ->where('user_id',$userId)
- ->where('org_id',$orgId)
- ->count();
- return $count;
- }
- }
|