<?php
namespace app\api\controller\v1;
use app\api\controller\Base;
use app\hander\HelpHander;
use think\App;
use think\Db;

class WasteRecord extends Base
{
    public function __construct(App $app = null) {
        parent::__construct($app);
        $this->model = new \app\common\model\WasteRecord();
    }
    public function printRecord (){
        $id=input('id/d');
        db('waste_record')->where('id',$id)
            ->update(['is_print' =>1]);
        HelpHander::success([],'操作成功');
    }
    public function queryIsPrint(){
        $id=input('id/d');
        $ret=$this->model->queryIsPrint($id);
        if($ret){
            HelpHander::success($ret);
        }else{
            HelpHander::error('暂无数据');
        }
    }
    public function queryWasteRecordList(){
        $orgId=input('orgId/d');
        $status=input('status');
        $createTime=trim(input('createTime'));
        $page=input('page/d',1);
        $size=input('size/d',10);
        $ret=$this->model->wasteRecordList($orgId,$status,$createTime,$page,$size);
        if($ret){
            HelpHander::success($ret);
        }else{
            HelpHander::error('暂无数据');
        }
    }
    public function queryUnTransportList(){
        $orgId=input('orgId/d');
        $createTime=trim(input('createTime'));
        $ret=$this->model->unTransportList($orgId,$createTime);
        if($ret){
            HelpHander::success($ret);
        }else{
            HelpHander::error('暂无数据');
        }
    }

    // 保存医废记录
    public function saveRecord(){
        $data = [
            'org_id' => input('orgId/d'),
            'user_id' => $this->userId,
            'waste_device_id' => input('wasteDeviceId'),
            'cateid' =>input('cateId'),
            'weight' => input('weight'),
            'status' => 0,
            'is_print' => 0
        ];
        if(!$data['waste_device_id']){
            HelpHander::error('未选择收取科室');
        }
        if(!$data['cateid']){
            HelpHander::error('类型不能为空');
        }

        if($data['weight'] <= 0){
            HelpHander::error('称重必须大于0');
        }
        $waste_record_id = $this->model->saverecord($data);
        if(!$waste_record_id){
            HelpHander::error($this->model->getError());
        }
        $info = $this->model->getOne($waste_record_id,$data['org_id']);
        if(!$info){
            HelpHander::error('操作失败');
        }
        $info['wasteDeviceName'] = Db::name('address')
            ->where('id',$info['waste_device_id'])
            ->value('title');
        $info['cateId']=$info['cateid'];
        $a = formatArray(['id','create_time',
            'serial_number','wasteDeviceName','cateId','is_print'],$info);
        HelpHander::success($a,'操作成功');
    }

    public function save(){
       $json = [
           'serialNumber'=>input('serialNumber'),
           'weight2'=>input('weight2'),
           'status'=>input('status'),
           'reason'=>input('reason'),
           'userId'=>$this->userId,
       ];
        if(!in_array($json['status'],[1,2,3])){
            HelpHander::error('参数错误');
        }
        $ret = $this->model->saveStatus($json);
        if($ret){
            HelpHander::success([],'操作成功');
        }else{
            HelpHander::error('操作失败'.$this->model->getError());
        }

    }

    public function detail(){
        $serialNum = input('serialNum');
        $orgId = $this->orgId;
        if(strlen($serialNum) == 14){ // 包编号
            $ret = $this->model->packInfo($serialNum,$orgId);
        }else{
            $ret = $this->model->recordInfo($serialNum,$orgId);
        }
        if($ret){
            HelpHander::success($ret,'操作成功');
        }else{
            HelpHander::error('操作失败');
        }

    }

    public function lists(){
        $depId = input('depId');
        $date = input('date');
        $orgId = $this->orgId;
        $page=input('page/d',1);
        $size=input('size/d',10);
        $ret=$this->model->lists($orgId,$depId,$date,$page,$size,$this->userId);
        HelpHander::success($ret);
    }

    public function sign(){
        $path = input('path');
        $wasterecordid = input('wasteRecordId');
        if(!$path || !$wasterecordid){
            HelpHander::error('参数错误');
        }
        $ids = explode(',',$wasterecordid);

        $succ = 0;
        foreach ($ids as $k=>$v){
            $info = $this->model->getOne($v,$this->orgId);
            if(!$info){
                HelpHander::error($v.'记录不存在');
            }
            if($info['sign'] == 1){
                continue;
            }
            $res = Db::name('waste_record')
                ->where('id',$v)
                ->update([
                    'sign' => 1,
                    'sign_path' => $path,
                    'sign_user_id' => $this->userId,
                    'sign_time' => getTime()
                ]);
            if($res){
                $succ++;

            }
        }

        if($succ==count($ids)){
            HelpHander::success('操作成功');
        }else{
            HelpHander::error('操作失败');
        }
    }


    // 保存医废打包
    public function savePackage(){
        $data = [
            'org_id' =>$this->orgId,
            'user_id' => $this->userId,
            'ids' =>input('ids'),
        ];
        $ids = explode(',',$data['ids']);
        $nids = [];
        foreach ($ids as $k=>$v){
            if($v && !in_array($v,$nids)){
                $nids[] = $v;
            }
        }
        $data['ids'] = $nids;
        if(!$data['ids']){
            HelpHander::error('未选择医废记录');
        }
        $ret = $this->model->savePackage($data);
        if(!$ret){
            HelpHander::error($this->model->getError());
        }

        HelpHander::success(['id' => $ret],'操作成功');
    }

    // 可使用的打包医废记录
    public function yflist(){
        $ret=$this->model->yflist($this->orgId,$this->userId);
        HelpHander::success($ret);
    }

    // 删除包内医废记录
    public function delpackage(){
        $id = input('id'); // 包id
        $recordId = input('recordId'); // 记录id
        $ret=$this->model->delpackage($this->orgId,$this->userId,$id,$recordId);
        if(!$ret){
            HelpHander::error($this->model->getError());
        }

        HelpHander::success([],'操作成功');
    }

    // 交接包
    public function jjpackage(){
        $id = input('id');
        $ret=$this->model->jjpackage($this->orgId,$this->userId,$id);
        if(!$ret){
            HelpHander::error($this->model->getError());
        }

        HelpHander::success([],'操作成功');
    }

    // 转运包
    public function zypackage(){
        $id = input('id');
        $ret=$this->model->zypackage($this->orgId,$this->userId,$id);
        if(!$ret){
            HelpHander::error($this->model->getError());
        }

        HelpHander::success([],'操作成功');
    }

    public function packages(){
        $page = input('page',1);
        $size = input('size',10);
        $ret=$this->model->packagesLists($this->orgId,$page,$size,$this->userId);
        HelpHander::success($ret);
    }
    public function recordLists(){
        $wasteDeviceId = input('wasteDeviceId/d',0);
        $start = input('start','');
        $end = input('end','');
        $orgId = $this->orgId;
        $page = input('page',1);
        $size = input('size',10);
        $ret=$this->model->recordLists($orgId,$wasteDeviceId,$start,$end,$page,$size,$this->userId);
        HelpHander::success($ret);
    }
    public function getByDepDayList(){
        $wasteDeviceId = input('wasteDeviceId/d',0);
        $str=$this->model->getByDepDayList($wasteDeviceId,$this->orgId);
        HelpHander::success(['str'=>$str]);

    }
    public function printList(){
        $ret=$this->model->printList($this->orgId,$this->userId);
        HelpHander::success($ret);
    }
    public function print(){
        $ids = input('ids','');
        if(empty($ids)){
            HelpHander::error('参数错误');
        }
        $ret = $this->model->print($ids,$this->orgId,$this->userId);
        $ret?HelpHander::success($ret):HelpHander::error($this->model->getError());
    }
}