123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951 |
- <?php
- namespace app\common\model;
- use function PHPSTORM_META\map;
- use think\Db;
- use think\Exception;
- class WasteRecord extends Base {
- public $table = 'waste_record';
- protected $validateName = 'WasteRecord';
- public $status = [
- '已收取',
- '已交接',
- '已转运',
- '医废异常',
- '未转运',
- ];
- /**
- * 医废记录详情
- *
- * @author wst
- * @date 2021/9/8 17:32
- * @return array
- */
- public function info($id,$orgId){
- $info = $this->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;
- }
- }
|