getOne($id,$orgId); if($info){ $info['real_name'] = Db::name('user') ->where('id',$info['user_id']) ->value('real_name'); $info['waste_device'] = Db::name('address') ->where('id',$info['waste_device_id']) ->value('title'); $info['cate'] = Db::name('waste_type') ->where('id',$info['cateid']) ->value('title'); $info['weight'] =round($info['weight']/1000,2); $info['status'] =$this->status[$info['status']]; $rlist = Db::name('waste_record_user') ->alias('waste_record_user') ->join('user user','user.id = waste_record_user.user_id') ->where('waste_record_user.waste_record_id',$info['id']) ->field('waste_record_user.*,user.real_name') ->order('status', 'asc') ->select(); $info['rlist'] = $rlist?$rlist:array(); $info = json_decode(json_encode($info),true); $cate = Db::name('waste_type')->where('id',$info['cateid'])->find(); if($cate['type'] == 1 && $info['num'] > 0){ $tnum = round($info['weight'] / 1000, 2).'kg'.'('.$info['num'].$cate['unit'].')'; } $info['tum'] = $tnum; } return $info; } //转运 public function finish($id,$user_id){ $this->startTrans(); try{ $ret = $this ->where('id', $id) ->update(['status'=>2]); if(!$ret){ throw new Exception('更新状态失败'); } $ret2 = Db::name('waste_record_user')->insert(array( 'waste_record_id' => $id, 'user_id' => $user_id, 'status' => 2, 'create_time' => getTime() )); if(!$ret2){ throw new exception('保存操作记录失败'); } $this->commit(); return true; }catch (exception $e){ $this->rollback(); $this->error=$e->getMessage(); return false; } } public function getOne($id,$orgId=0){ $info = $this ->where('id',$id) ->where('org_id',$orgId) ->whereOr('serial_number',$id) ->find() ->toArray(); return $info; } /*----------------api----------------*/ public function recordInfo($serialNum,$orgId){ $map[] = ['serial_number','=',$serialNum]; $map[] = ['org_id','=',$orgId]; $map[] = ['del','=',0]; $info = $this->field('id,serial_number,org_id,waste_device_id,user_id,cateid,weight,status,create_time,reason,is_print,weight2,num') ->where($map)->find(); $info = $info?$info->toArray():[]; if(!$info){ $this->error = '记录不存在'; return false; } $org = Db::name('org') ->where('id',$info['org_id']) ->field('name')->find(); $info['org_name'] = $org?$org['name']:''; $user = Db::name('user') ->where('id',$info['user_id']) ->field('real_name')->find(); $info['real_name'] = $user?$user['real_name']:''; $wasteDevice = Db::name('address') ->where('id',$info['waste_device_id']) ->field('title') ->find(); $info['title'] = $wasteDevice?$wasteDevice['title']:''; $info['cate_id'] = $info['cateid']; $info['cate_name'] =Db::name('waste_type')->where('id',$info['cateid'])->value('title'); $info['unit'] =Db::name('waste_type')->where('id',$info['cateid'])->value('unit'); unset($info['cateid']); $lists = Db::name('waste_record_user') ->alias('wru') ->where('wru.waste_record_id',$info['id']) ->join('user u','u.id = wru.user_id') ->field('u.real_name,wru.status,wru.create_time') ->order('wru.id asc') ->select(); $data = [ 'type' => 1, // 1=医废记录详情 2=医废包详情 'info' => $info, 'status_list' => $lists?$lists:[] ]; return $data; } public function queryIsPrint($id){ $ret=$this ->where('id',$id) ->field('is_print') ->find()->toArray(); return $ret; } public function wasteRecordList($orgId,$status,$createTime,$page,$size){ $offset= ($page - 1) * $size; $starttime=$createTime.' 00:00:00'; $endtime=$createTime.' 23:59:59'; $where[] = ['create_time','between',[$starttime,$endtime]]; $list=$this ->field('waste_device_id,cateid,create_time,is_print,serial_number,id,weight,num') ->where([ 'org_id'=>$orgId, 'status'=>$status, 'del'=>0, ]) ->where($where) ->order('id desc') ->limit($offset,$size) ->select() ->toArray(); $ret['list']=$list; foreach ($list as $k=>$v){ $title=Db::name('address') ->where([ 'id'=>$v['waste_device_id'], 'del'=>0, 'enable'=>1, ])->value('title'); $ret['list'][$k]['title']= $title; $cate = Db::name('waste_type') ->where('id',$v['cateid']) ->value('title'); $ret['list'][$k]['cate_title']= $cate; $ret['list'][$k]['unit']= Db::name('waste_type') ->where('id',$v['cateid']) ->value('unit'); } return $ret; } public function unTransportList($orgId,$createTime){ $where = []; if($createTime){ $starttime=$createTime.' 00:00:00'; $endtime=$createTime.' 23:59:59'; $where[] = ['create_time','between',[$starttime,$endtime]]; } $list=$this ->field('waste_device_id,cateid,create_time,is_print,serial_number,id,weight,num') ->where('org_id',$orgId) ->where('del',0) ->where($where) ->where('status','in',[0,1]) ->order('id','desc') ->select() ->toArray(); $ret['list']=$list; foreach ($list as $k=>$v){ $title=Db::name('address') ->where([ 'id'=>$v['waste_device_id'], 'del'=>0, 'enable'=>1, ])->value('title'); $ret['list'][$k]['title']= $title; $cate =Db::name('waste_type') ->where('id',$v['cateid']) ->value('title'); $ret['list'][$k]['cate_title']= $cate; $ret['list'][$k]['unit']= Db::name('waste_type') ->where('id',$v['cateid']) ->value('unit'); } return $ret; } public function saveRecord($data){ $curtime = getTime(); $data['serial_number'] = get_unique_id(); // if(!$data['create_time']){ // $data['create_time'] = $curtime; // } $data['create_time'] = $curtime; $data['create_yyyy'] = date('Y',strtotime($data['create_time'])); $data['create_yyyymm'] = date('Ym',strtotime($data['create_time'])); $data['create_yyyymmdd'] = date('Ymd',strtotime($data['create_time'])); $this->startTrans(); try{ $waste_record_id = $this->insertGetId($data); if (!$waste_record_id) { throw new Exception('保存记录失败'); } $sdata = [ 'status' => 0, 'create_time' => $data['create_time'], 'user_id' => $data['user_id'], 'waste_record_id' => $waste_record_id, ]; $res = Db::name('waste_record_user')->insert($sdata); if (!$res) { throw new Exception('保存操作记录失败'); } $this->commit(); return $waste_record_id; }catch (Exception $e){ $this->error = $e->getMessage(); $this->rollback(); return false; } } public function saveStatus($data){ $info = $this-> where('serial_number',$data['serialNumber']) ->find(); if(!$info){ $this->error = '记录不存在'; return false; } $curTime = date('Y-m-d H:i:s'); $this->startTrans(); try{ $rdata = [ 'weight2' => isset($data['weight2'])?$data['weight2']:0, 'status' => $data['status'], 'reason' => isset($data['reason'])?$data['reason']:'', 'package_id' => 0 // 有任何变化,都删除与包的关系 ]; $ret = $this ->where('id',$info['id']) ->update( $rdata); if (!$ret) { \exception('操作失败1'); } $sdata = [ 'status' => $data['status'], 'create_time' => $curTime, 'user_id' => $data['userId'], 'waste_record_id' => $info['id'], ]; $res = Db::name('waste_record_user') ->insert($sdata); if (!$res) { \exception('操作失败'); } // 如果在保内,检查包内是否还有可以记录,没有则删除包记录 if($info['package_id'] > 0){ $count = $this ->where('package_id',$info['package_id']) ->where('id','<>',$info['id']) ->count(); if($count == 0){ $res = Db::name('waste_package')-> where('id',$info['package_id']) ->update(['del' => 1]); if (!$res) { \exception('操作失败'); } } } $this->commit(); }catch (Exception $e){ $this->error = '操作失败'.$e->getMessage(); $this->rollback(); return false; } return true; } public function lists($orgId,$depId,$date,$page,$size,$userId){ $offset= ($page - 1) * $size; $map[]= ['wr.org_id','=',$orgId]; $map[]= ['wr.sign','=',0]; $map[]= ['wr.del','=',0]; $map1[]= ['wr.user_id','=',$userId]; if($depId){ $map1[]= ['wd.dep_id','=',$depId]; } if($date){ $date = date('Ymd',strtotime($date)); $map[]= ['wr.create_yyyymmdd','=',$date]; } $list = $this ->alias('wr') ->join('address wd','wd.id = wr.waste_device_id') ->where(function ($query) use ($map1) { $query->whereOr($map1); })->where($map)->field('wr.waste_device_id,wr.cateid,wr.create_time,wr.is_print,wr.serial_number,wr.id,wr.weight,wr.sign,wr.num') ->order('wr.id','desc') ->limit($offset,$size)->select(); $list = $list?$list->toArray():[]; foreach ($list as $k=>$v){ $title=Db::name('address') ->where('id',$v['waste_device_id']) ->value('title'); $list[$k]['waste_device_name'] = $title; $cate = Db::name('waste_type')->where('id',$v['cateid'])->value('title'); $list[$k]['cate_title']= $cate?$cate:''; $list[$k]['unit']= Db::name('waste_type')->where('id',$v['cateid'])->value('unit'); } $ret['list']=$list?$list:[]; return $ret; } public function savePackage($post){ $curTime =getTime(); $data = [ 'org_id' => $post['org_id'], 'user_id' => $post['user_id'], 'ids' => $post['ids'], 'create_time' => $curTime, ]; // 检查是否符合要求 $count = Db::name('waste_record') ->where('del',0) ->where('status',0) ->where('package_id',0) ->where('user_id',$data['user_id']) ->where('id','in',$data['ids']) ->count(); if($count != count($data['ids'])){ $this->error = '有不符合要求的医废记录'; return false; } $this->startTrans(); try{ $pData = [ 'org_id' => $post['org_id'], 'user_id' => $post['user_id'], 'record' => implode(',',$post['ids']), 'create_time' => $curTime, 'sn' => get_unique_id2() ]; $id= Db::name('waste_package') ->insertGetId($pData); if (!$id) { exception('操作失败'); } $sData = [ 'package_id' => $id, ]; $res = $this->where('id','in',$data['ids']) ->update( $sData); if (!$res) { exception('操作失败'); } $this->commit(); }catch (Exception $e){ $this->error = $e->getMessage(); $this->rollback(); return false; } return $id; } public function packInfo($serialNum,$orgId,$platform=''){ $map[] = ['sn','=',$serialNum]; $map[] = ['org_id','=',$orgId]; $map[] = ['del','=',0]; $info = Db::name('waste_package') ->where($map) ->field('id,sn,org_id,user_id,status,create_time,jj_time,zy_time,jj_user_id,zy_user_id') ->find(); if(!$info){ $this->error = '记录不存在'; return false; } $org = Db::name('org') ->where('id',$info['org_id']) ->field('name')->find(); $info['org_name'] = $org?$org['name']:''; $user = Db::name('user') ->where('id',$info['user_id']) ->field('real_name')->find(); $info['real_name'] = $user?$user['real_name']:''; $info['jj_time'] = $info['jj_time']?$info['jj_time']:''; $info['zy_time'] = $info['zy_time']?$info['zy_time']:''; if($info['jj_user_id'] > 0){ $user = Db::name('user') ->where('id',$info['jj_user_id']) ->field('real_name')->find(); $info['jj_real_name'] = $user?$user['real_name']:''; } if($info['zy_user_id'] > 0){ $user = Db::name('user') ->where('id',$info['zy_user_id']) ->field('real_name')->find(); $info['zy_real_name'] = $user?$user['real_name']:''; } $weight =$this ->where('package_id',$info['id']) ->sum('weight'); // $info['weight'] = $weight?round($weight/1000,2):0; $info['kg'] = $weight?round($weight/1000,2):0; if($platform == 'iOS'){ $info['weight'] = $weight; }else{ $info['weight'] = $weight?round($weight/1000,2):0; } $count = $this->where('package_id',$info['id']) ->count(); $info['count'] = $count?$count:0; $record = Db::name('waste_record') ->alias('wr') ->where('wr.del',0) ->where('wr.package_id',$info['id']) ->field('wr.waste_device_id,wr.cateid,wr.create_time,wr.is_print,wr.serial_number,wr.id,wr.weight,wr.sign,wr.num') ->order('wr.id desc') ->select(); foreach ($record as $k=>$v){ $title=Db::name('address') ->field('title') ->where('id',$v['waste_device_id']) ->find(); $record[$k]['title'] = $title?$title['title']:''; $record[$k]['cate_name'] =Db::name('waste_type') ->where('id',$v['cateid'])->value('title'); // $record[$k]['weight'] = $v['weight']?round($v['weight']/1000,2):0; $record[$k]['weight'] = $v['weight']?$v['weight']:0; $record[$k]['kg'] = $v['weight']?round($v['weight']/1000,2):0; $cate = Db::name('waste_type')->where('id',$v['cateid'])->find(); $record[$k]['unit'] = $cate ? $cate['unit']:''; $tnum = round($v['weight'] / 1000, 2).'kg'; if($v['num'] >0){ $tnum = round($v['weight'] / 1000, 2).'kg'.'('.$v['num'].$cate['unit'].')'; } $record[$k]['tum'] = $tnum; } $data = [ 'type' => 2, 'pinfo' => $info, 'record' => $record?$record:[] ]; return $data; } public function packagesLists($orgId,$page,$size,$userId){ $offset= ($page - 1) * $size; $map[] = ['org_id','=',$orgId]; $map[] = ['user_id','=',$userId]; $map[] = ['del','=',0]; $list = Db::name('waste_package') ->field('id,sn,org_id,user_id,status,create_time') ->order('id desc') ->where($map) ->limit($offset,$size) ->select(); foreach ($list as $k=>$v){ $user = Db::name('user') ->where('id',$v['user_id']) ->field('real_name') ->find(); $list[$k]['real_name'] = $user?$user['real_name']:''; $weight = $this->where('package_id',$v['id']) ->sum('weight'); $list[$k]['weight'] = $weight?$weight:0; $count = $this->where('package_id',$v['id']) ->count(); $list[$k]['count'] = $count?$count:0; } return $list?$list:[]; } public function delpackage($orgId,$userId,$id,$recordId){ $info = $this ->where('del',0) ->where('org_id',$orgId) ->where('package_id',$id) ->where('id',$recordId) ->find(); if(!$info){ $this->error = '记录不存在'; return false; } $this->startTrans(); try{ $res = $this->where('id',$recordId) ->update(['package_id' => 0]); if (!$res) { exception('操作失败'); } // 如果在保内,检查包内是否还有可以记录,没有则删除包记录 if($info['package_id'] > 0){ $count = $this ->where('package_id',$id) ->where('id','<>',$recordId) ->count(); if($count == 0){ $res = Db::name('waste_package') ->where('id',$id) ->update(['del' => 1]); if (!$res) { exception('操作失败'); } } } $this->commit(); return true; }catch (Exception $e){ $this->error = '操作失败'.$e->getmessage(); $this->rollback(); return false; } } // 交接 public function jjpackage($orgId,$userId,$id){ $info = Db::name('waste_package') ->where('del',0) ->where('org_id',$orgId) ->where('id',$id) ->find(); if(!$info){ $this->error = '记录不存在'; return false; } if($info['status'] != 0){ $this->error = '无权限操作'; return false; } $lists = $this ->where('package_id',$id) ->where('del',0) ->select(); $lists = $lists?$lists->toArray():[]; if(count($lists) == 0){ $this->error = '无可操作的医废记录'; return false; } $ids = []; foreach ($lists as $k=>$v){ $ids[] = $v['id']; } $this->startTrans(); try{ $rData = [ 'jj_time' => getTime(), 'status' => 1, 'jj_user_id' => $userId, 'jj_record' => implode(',',$ids) ]; $ret = Db::name('waste_package') ->where('id',$id) ->update($rData); if (!$ret) { exception('操作失败1'); } foreach ($lists as $k=>$v){ $rrData = [ 'weight2' => 0, 'status' => 1, 'reason' => '', ]; $ret = $this ->where('id',$v['id']) ->update($rrData); if (!$ret) { exception('操作失败'); } $sData = [ 'status' => 1, 'create_time' => getTime(), 'user_id' => $userId, 'waste_record_id' => $v['id'], ]; $res = Db::name('waste_record_user') ->insert($sData); if (!$res) { exception('操作失败'); } } $this->commit(); return true; }catch (Exception $e){ $this->error = '操作失败'.$e->getmessage(); $this->rollback(); return false; } } // 转运 public function zypackage($orgId,$userId,$id){ $info = Db::name('waste_package') ->where('del',0) ->where('org_id',$orgId) ->where('id',$id) ->find(); if(!$info){ $this->error = '记录不存在'; return false; } if($info['status'] != 1){ $this->error = '无权限操作'; return false; } $lists = $this ->where('package_id',$id) ->where('del',0) ->select(); $lists = $lists?$lists->toArray():[]; if(count($lists) == 0){ $this->error = '无可操作的医废记录'; return false; } $ids = []; foreach ($lists as $k=>$v){ $ids[] = $v['id']; } $this->startTrans(); try{ $rData = [ 'zy_time' => date('Y-m-d H:i:s'), 'status' => 2, 'zy_user_id' => $userId, 'zy_record' => implode(',',$ids) ]; $ret = Db::name('waste_package') ->where('id',$id) ->update($rData); if (!$ret) { throw new exception('操作失败'); } foreach ($lists as $k=>$v){ $rrdata = [ 'weight2' => 0, 'status' => 2, 'reason' => '', ]; $ret = $this ->where('id',$v['id']) ->update($rrdata); if (!$ret) { exception('操作失败'); } $sdata = [ 'status' => 2, 'create_time' => getTime(), 'user_id' => $userId, 'waste_record_id' => $v['id'], ]; $res = Db::name('waste_record_user') ->insert($sdata); if (!$res) { exception('操作失败'); } } $this->commit(); return true; }catch (Exception $e){ $this->error = '操作失败'.$e->getmessage(); $this->rollback(); return false; } } public function yflist($orgId,$userId){ $map[] = ['wr.org_id','=',$orgId]; $map[] = ['wr.user_id','=',$userId]; $map[] = ['wr.package_id','=',0]; $map[] = ['wr.del','=',0]; $map[] = ['wr.status','=',0]; $list=$this->alias('wr') ->where($map) ->field('wr.waste_device_id,wr.cateid,wr.create_time,wr.is_print,wr.serial_number,wr.id,wr.weight,wr.sign,wr.num') ->order('wr.id desc') ->select(); $list = $list?$list->toArray():[]; foreach ($list as $k=>$v){ $title=Db::name('address') ->field('title') ->where('id',$v['waste_device_id']) ->find(); $list[$k]['title'] = $title?$title['title']:''; $list[$k]['cate_name'] =Db::name('waste_type')->where('id',$v['cateid'])->value('title'); $list[$k]['unit'] =Db::name('waste_type')->where('id',$v['cateid'])->value('unit'); } return $list?$list:[]; } public function printList($orgId,$userId){ $title = input('title',''); if($title!=''){ $type = 7; $map1[]=['','exp',Db::raw("FIND_IN_SET($type,types)")]; $address = Db::name('address') ->where('del',0) ->where('enable',1) ->where('org_id',$orgId) ->where('title','like','%'.$title.'%') ->where($map1) ->column('id'); if($address){ $map[] = ['wr.waste_device_id','in',$address]; }else{ $map[] = ['wr.waste_device_id','=',-1]; } } $map[] = ['wr.org_id','=',$orgId]; $map[] = ['wr.user_id','=',$userId]; $map[] = ['wr.del','=',0]; $map[] = ['wr.status','=',0]; $map[] = ['wr.create_yyyymmdd','=',date('Ymd')]; $list=$this->alias('wr') ->where($map) ->field('wr.waste_device_id,wr.cateid,wr.create_time,wr.is_print,wr.serial_number,wr.id,wr.weight,wr.sign,wr.num') ->order('wr.id desc') ->limit(1000) ->select(); $list = $list?$list->toArray():[]; foreach ($list as $k=>$v){ $title=Db::name('address') ->field('title') ->where('id',$v['waste_device_id']) ->find(); $list[$k]['title'] = $title?$title['title']:''; $list[$k]['cate_name'] =Db::name('waste_type')->where('id',$v['cateid'])->value('title'); $list[$k]['unit'] =Db::name('waste_type')->where('id',$v['cateid'])->value('unit'); } return $list?$list:[]; } public function recordLists($orgId,$wasteDeviceId,$start,$end,$page,$size,$userId){ $offset= ($page - 1) * $size; $map[] = ['wr.org_id','=',$orgId]; $map[] = ['wr.del','=',0]; if($wasteDeviceId>0){ $map[] = ['wr.waste_device_id','=',$wasteDeviceId]; } if($start){ $date = date('Ymd',strtotime($start)); $map[] = ['wr.create_yyyymmdd','>=',$date]; } if($end){ $date = date('Ymd',strtotime($end)); $map[] = ['wr.create_yyyymmdd','<=',$date]; } $list=db('waste_record') ->alias('wr') ->field('wr.waste_device_id,wr.status,wr.cateid,wr.create_time,wr.is_print,wr.serial_number,wr.id,wr.weight,wr.sign,wr.num') ->order('wr.id','desc') ->where($map) ->limit($offset,$size) ->select(); foreach ($list as $k=>$v){ $title= Db::name('address') ->field('title') ->where('id',$v['waste_device_id']) ->find(); $list[$k]['title'] = $title?$title['title']:''; $list[$k]['cate_title'] = $this->getTableField('waste_type',['id'=>$v['cateid']],'title'); $list[$k]['unit'] = $this->getTableField('waste_type',['id'=>$v['cateid']],'unit'); } $ret=$list?$list:[]; return $ret; } public function getByDepDayList($wasteDeviceId,$orgId){ $typeList = model('WasteType')->getList($orgId); $str =''; foreach ($typeList as $k=>$v){ $sum = Db::name('waste_record') ->where('create_yyyymmdd',date('Ymd')) ->where('org_id',$orgId) ->where('waste_device_id',$wasteDeviceId) ->where('cateid',$v['id']) ->where('del',0) ->sum('weight'); if($sum >0){ $count = ($sum/1000).'kg'; $str.=$v['title'].':'.$count.';'; } } if($str!=''){ $s = explode(';',$str); unset($s[count($s)-1]); $str = implode(';',$s); } return $str; } public function print($ids,$orgId,$userId){ $waste_device_id = []; $id = explode(',',$ids); $total = 0; $cate = []; foreach ($id as $k=>$v){ $info = Db::name('waste_record') ->where('id',$v) ->find(); if(empty($info)){ $this->error='记录id'.$v.'不存在'; return false; } if($info['user_id']!=$userId){ $this->error='记录id'.$v.'收取人不是自己'; return false; } $cateInfo = Db::name('waste_type') ->where('id',$info['cateid']) ->find(); $cate[] = [ 'id'=>$info['cateid'], 'title'=>$cateInfo['title'], 'num'=>$info['weight'], 'count'=>1, ]; $total+=$info['weight']; $waste_device_id[] = $info['waste_device_id']; } $aa = []; foreach ($cate as $k=>$v){ if(isset($aa[$v['id']])){ $aa[$v['id']]['num'] = $aa[$v['id']]['num']+$v['num']; $aa[$v['id']]['count'] = $aa[$v['id']]['count']+1; }else{ $aa[$v['id']] = $v; } } $ab = []; foreach ($aa as $k=>$v){ $kg = sprintf("%.2f",$v['num']/1000); $ab[]=$v['title'].':'.$v['count'].' '.$kg.'kg'; } if(count(array_unique($waste_device_id)) >1){ $this->error='只能打印同一个科室下的记录'; return false; } $address = Db::name('address') ->where('id',$waste_device_id[0]) ->find(); if(empty($address['user'])){ $this->error='当前科室未绑定负责人'; return false; } $amount = $total>0?sprintf("%.2f",$total/1000):"0"; $total_desc = $amount.'kg,'.count($id).'袋'; $a = [ 'org_id'=>$orgId, 'waste_record_id'=>$ids, 'address'=>$address['title'], 'address_user'=>$address['user'], 'create_time'=>getTime(), 'total'=>$total_desc, 'user_id'=>$userId, 'desc'=>implode(',',$ab), ]; $r = Db::name('waste_print_log') ->insertGetId($a); $a['org_name'] = Db::name('org') ->where('id',$a['org_id']) ->value('name'); $a['user_name'] = Db::name('user') ->where('id',$userId) ->value('real_name'); unset($a['user_id']); return $a; } }