<?php
/**
 * @todo 核放单添加
 */
use Customs\Dg\Hmzb\InvtSb\Xml;
use Customs\Dg\Hmzb\InvtSb\PassportHead;
use Customs\Dg\Hmzb\InvtSb\PassportList;
use Customs\Dg\Hmzb\InvtSb\PassportAcmp;
use Customs\Dg\Hmzb\InvtSb\Send;
use App\models\Logistic\SendXml\InvSb;
class Process_InvtSb
{
    protected $headInfo;
    protected $bodyInfo;
    protected $default;

    /**
     * Note:验证表头信息
     * User: Yao
     * Date: 2020/8/6
     * Time: 17:54
     * @param $headInfo
     * @return array
     */
    public function validateHeadInfo($headInfo,$detail) {
        $error = [];

        if (empty($headInfo['rltNo'])) {
            if($headInfo['passportTypecd'] !=6){
                $error[] = '申报单号不能为空';
            }
        } else {
            $hzRows = explode("/",$headInfo['rltNo']);
            switch($headInfo['bindTypecd']){
                case "1":
                    if(count($hzRows)<=1){
                        //一车多票要看是否搞了多票
                        $error[] = "一车多票必须是大于2个核注清单";
                    }
                    foreach($hzRows as $k=>$hzvalue){
                        $hzinfo = Service_InvtSbInvtlist::getByField($hzvalue,"RltInvtNo");
                        if(!empty($hzinfo)){
                            $error[] = "核注清单{$hzvalue}已经关联核放单:".$hzinfo["invt_sb"];
                        }
                        //查看核注清单是否能申请
                        $invtInfo = Service_InvtList::getByField($hzvalue,'BondInvtNo');
                        if(empty($invtInfo)){
                            $error[] = "找不到单号{$hzvalue}的核注清单";
                        }else{
                            if(!in_array($invtInfo['zb_status'],[4,5])){
                                $error[] = "核注清单必须是{$hzvalue}必须是预核扣/未核扣才能做核放单";
                            }
                            if($headInfo['ioTypecd']!=$invtInfo['ImpexpMarkcd']){
                                $error[] = "核放单的进出标志和核注清单的进出标志不一致";
                            }
                        }
                    }
                    break;
                case "2":
                    //一车一票
                    if(count($hzRows)!==1){
                        //一车多票要看是否搞了多票
                        $error[] = "一车一票只能有一个核注清单";
                    }
                    //print_r($hzRows);
                    foreach($hzRows as $k=>$hzvalue){
                        $hzinfo = Service_InvtSbInvtlist::getByField($hzvalue,"RltInvtNo");
                        if(!empty($hzinfo)){
                            $error[] = "核注清单{$hzvalue}已经关联核放单:".$hzinfo["invt_sb"];
                        }
                        //查看核注清单是否能申请
                        $invtInfo = Service_InvtList::getByField($hzvalue,'BondInvtNo');
                        if(empty($invtInfo)){
                            $error[] = "找不到单号{$hzvalue}的核注清单";
                        }else{
                            if(!in_array($invtInfo['zb_status'],[4,5])){
                                $error[] = "核注清单必须是{$hzvalue}必须是预核扣/未核扣才能做核放单";
                            }
                            if($headInfo['ioTypecd']!=$invtInfo['ImpexpMarkcd']){
                                $error[] = "核放单的进出标志和核注清单的进出标志不一致";
                            }
                        }
                    }
                    break;
                case "3":
                    //一票多车(不校验是否关联)
                    foreach($hzRows as $k=>$hzvalue){
                        //查看核注清单是否能申请
                        $invtInfo = Service_InvtList::getByField($hzvalue,'BondInvtNo');
                        if(empty($invtInfo)){
                            $error[] = "找不到单号{$hzvalue}的核注清单";
                        }else{
                            if(!in_array($invtInfo['zb_status'],[4,5])){
                                $error[] = "核注清单必须是{$hzvalue}必须是预核扣/未核扣才能做核放单";
                            }
                            if($headInfo['ioTypecd']!=$invtInfo['ImpexpMarkcd']){
                                $error[] = "核放单的进出标志和核注清单的进出标志不一致";
                            }
                        }
                    }
                    break;
            }
        }
        if(!empty($detail) && $headInfo['bindTypecd']!='3'){
            $error[] = "不是一票多车核放单商品项不能填写";
        }
        if(empty($headInfo['sb_mode'])){
            $error[]='请选择核放单模式';
        }else{
            if($headInfo['sb_mode'] == 1 || $headInfo['sb_mode'] == 2){
                if(empty($headInfo['del_no'])){
                    throw new Exception('请填写非金二核放单号');
                }
            }
        }

        if (empty($headInfo['bindTypecd']) && $headInfo['bindTypecd'] != '') {
            $error[] = '绑定类型不能为空';
        } else {
            if (!in_array($headInfo['bindTypecd'], array_keys(Service_InvtSb::getbindTypecd()))) {
                $error[] = '绑定类型错误';
            }
        }
        if (empty($headInfo['passportTypecd']) && $headInfo['passportTypecd'] != '') {
            $error[] = '核放单类型不能为空';
        } else {
            if (!in_array($headInfo['passportTypecd'], array_keys(Service_InvtSb::getpassportTypecd()))) {
                $error[] = '核放单类型错误';
            }
        }
        if (empty($headInfo['rltTbTypecd']) && $headInfo['rltTbTypecd'] != '') {
            if($headInfo['passportTypecd']!=6){//空车进区可以为空
                $error[] = '关联单据类型不能为空';
            }
        } else {
            if (!in_array($headInfo['rltTbTypecd'], array_keys(Service_InvtSb::getrltTbTypecd()))) {
                $error[] = '关联单据类型错误';
            }
        }

        if (empty($headInfo['ioTypecd']) && $headInfo['ioTypecd'] != '') {
            $error[] = '进出标志为空';
        } else {
            if (!in_array($headInfo['ioTypecd'], array_keys(Service_InvtSb::getioTypecd()))) {
                $error[] = '进出标志为错误';
            }
        }
        if (empty($headInfo['masterCuscd'])) {
            $error[] = '主管关区代码为空';
        }
        if (empty($headInfo['areainEtpsno']) && $headInfo['areainEtpsno']!='') {
            $error[] = '区内企业代码为空';
        }
        if (empty($headInfo['areainEtpsNm'])) {
            $error[] = '区内企业名称为空';
        }
        if (empty($headInfo['areainEtpsSccd'])) {
            $error[] = '区内企业信用代码为空';
        }
        if (empty($headInfo['vehicleNo'])) {
            $error[] = '承运车车牌号必填';
        }
        if (empty($headInfo['vehicleWt'])) {
            $error[] = '车自重必填';
        } else {
            if(!is_numeric($headInfo['vehicleWt'])){
                $error[] = '车自重必须是数字';
            }
        }
        if (empty($headInfo['vehicleFrameWt'])) {
            $error[] = '车架重为空';
        } else {
            if(!is_numeric($headInfo['vehicleFrameWt'])){
                $error[] = '车架重必须是数字';
            }
        }
        if (empty($headInfo['totalGrossWt'])) {
            $error[] = '货物总毛重为空';
        } else {
            if(!is_numeric($headInfo['vehicleFrameWt'])){
                $error[] = '货物总毛重必须是数字';
            }
        }

        return $error;
    }
    /**
     * Note:验证body主体
     * User: Yao
     * Date: 2020/8/10
     * Time: 17:52
     * @param $bodyInfo
     */
    public function validateBodyInfo($bodyInfo){
        $error = [];
        $warnError =[];
        foreach ($bodyInfo as $key => $body) {
            $lineError = [];
            $linWarnError=[];
            if (empty($body['DclQty'])) {
                $lineError[] = '申报数量为空';
            }else{
                if(!preg_match('/^[1-9]\d*$/',$body['DclQty'])){
                    $lineError[] = '申报数量必须为正整数';
                }
            }
            if(empty($body['Gdecd'])){
                $lineError[] = '海关编码为空';
            }
            if(empty($body['GdsNm'])){
                $lineError[] = '海关品名为空';
            }
            if(empty($body['DclUnitcd'])){
                $lineError[] = '申报单位为空';
            }
            if(empty($body['GdsMtno'])){
                $lineError[] = '商品料号为空';
            }
        }
        return [
            'error'=>$error,
            'warn_error'=>$warnError,
        ];
    }

    /**
     * Note:添加核放单数据
     * User: Yao
     * Date: 2020/8/8
     * Time: 11:06
     * @param $headInfo(核放单表头)
     * @param $bodyInfo(核放单对应核注信息)
     * @param $detail(核放单对应的商品信息,一票多车时候需要)
     * @return string 企业内部订单号
     * @throws Exception
     */
    public function doSave($headInfo, $bodyInfo,$detail=[]) {
        $code = Common_GetNumber::getCode('INVSB', 'DGST', 'F');
        //var_dump($code);
        $companyInfo = Process_InvtSb::getCompanyInfo();
        $code =$companyInfo['area_code'].date("YmdHis").substr($code,-4);
        // $code ='Z5225'.$headInfo['ioTypecd'].date("YmdHis").substr($code,-4);
        /*var_dump($code);
        exit;*/
        $save = [
            'SeqNo'=>'',
            'PassportNo' => '',
            'sb_mode'=>$headInfo['sb_mode'],
            'PassportTypecd'=>$headInfo['passportTypecd'],
            'MasterCuscd' => $headInfo['masterCuscd'],//主管关区代码
            'DclTypecd' => "1",//
            'IoTypecd' => $headInfo['ioTypecd'],//进出标志代码
            'BindTypecd' => $headInfo['bindTypecd'], //关联单证类型代码
            'RltTbTypecd' => $headInfo['rltTbTypecd'],//关联单据类型
            'RltNo' => $headInfo['rltNo'], //关联单证编号
            'AreainOriactNo' => '', //区内账册编号
            'AreainEtpsno'=>$headInfo['areainEtpsno'],//区内企业编码
            'AreainEtpsNm' => $headInfo['areainEtpsNm'], //区内企业编码
            'AreainEtpsSccd' => $headInfo['areainEtpsSccd'], //区内企业社会信用代码
            'VehicleNo' => $headInfo['vehicleNo'], //承运车车牌号
            'VehicleIcNo' => $headInfo['vehicleIcNo'], //IC卡号
            'ContainerNo' => $headInfo['containerNo'], //集装箱号
            'VehicleWt'=>$headInfo['vehicleWt'], //车自重
            'VehicleFrameNo'=>$headInfo['vehicleFrameNo'], //车架号
            'VehicleFrameWt' =>$headInfo['vehicleFrameWt'], //车架重
            'ContainerType'=>$headInfo['containerType'], //集装箱箱型
            'ContainerWt'=>$headInfo['containerWt'], //集装箱重
            'TotalWt'=>$headInfo['totalWt'],//总重量
            'TotalGrossWt'=>$headInfo['totalGrossWt'],//货物总毛重
            'TotalNetWt'=>$headInfo['totalNetWt'],//货物总净重
            'DclErConc'=>$headInfo['DclErConc'],//申请人及联系方式
            'DclTime'=>date("Y-m-d H:i:s",time()),//申报时间
            'DclEtpsno'=>$headInfo['DclEtpsno']?:'',//申报企业编号
            'DclEtpsNm'=>$headInfo['DclEtpsNm'], //申报企业名称
            'DclEtpsSccd'=>$headInfo['DclEtpsSccd'], //申报企业社会信用代码
            'InputCode'=>$headInfo['InputCode'], //录入单位代码
            'InputSccd'=>$headInfo['InputSccd']?:'', //录入单位社会信用代码
            'InputName'=>$headInfo['InputName'], //录入单位名称
            'EtpsPreentNo'=>$code, //企业内部编号
            'Rmk'=>$headInfo['rmk'],
            'del_no'=>$headInfo['del_no']?:'',
            'upload_file_id'=>$headInfo['upload_file_id']?:0,
            'Col1'=>'',
            'Col2'=>'',
            'Col3'=>'',
            'Col4'=>'',
        ];
        $is_id = Service_InvtSb::add($save);
        if (!$is_id) {
            throw  new Exception('保存核放表头信息失败');
        }
        //从汇单创建的更新下汇单和核放单的关系
        if($headInfo['im_code']){
            $invtManifest = Service_InvtManifiest::getByField($headInfo['im_code'],'im_code');
            Service_InvtManifestLog::add([
                'im_id'=>$invtManifest['im_id'],
                'operater_id'=>Service_User::getUserId(),
                'add_time'=>date("Y-m-d H:i:s"),
                'content'=>'生成核放单：'.$code,
                'status_from'=>$invtManifest['im_status'],
                'status_to'=>$invtManifest['im_status'],
                'ip'=>Common_Common::getRealIp(),
            ]);
        }
        $RltInvtNo = "";
        if(!empty($bodyInfo)){
            //核放单对应的核注清单
            foreach($bodyInfo as $k=>$value){
                $saveList = array(
                    'is_id'=>$is_id,
                    'EtpsInnerInvtNo'=>$code,
                    'RltInvtNo'=>$value,
                    'add_time'=>date("Y-m-d H:i:s",time())
                );
                $RltInvtNo = $value;
                //print_r($saveList);
                if(!Service_InvtSbInvtlist::add($saveList)){
                    throw new Exception("核放单和核注清单关联失败");
                }
            }
        }
        if(!empty($detail)){

            //核放单对应的商品(一票多车时候使用)
            $index = 1;
            foreach ($detail as $key => $value) {
                $saveBody = [
                    'is_id' => $is_id,
                    'EtpsInnerInvtNo' => $code,
                    'RltInvtNo' => $RltInvtNo,
                    'SeqNo' => '',
                    //'PassportNo' => $value['PassportNo'],
                    'PassportSeqNo' => $index,
                    'GdsMtno' => $value['GdsMtno'],
                    'Gdecd' => $value['Gdecd'],
                    'GdsNm' => $value['GdsNm'],
                    'GrossWt' => $value['GrossWt'],
                    'NetWt' => $value['NetWt'],
                    'RltGdsSeqno' => $value['RltGdsSeqno'],
                    'DclUnitcd' => $value['DclUnitcd'],
                    'DclQty'=>$value['DclQty'],
                    'Rmk' => $value['Rmk'],
                ];
                if(!Service_InvtSbDetail::add($saveBody)){
                    throw new Exception("核放单表体商品项添加失败");
                }
                $index++;
            }
        }

        $log = [
            'is_id' => $is_id,
            'is_node' => '添加核放单',
            'ls_text' => "表头".json_encode($headInfo)."核注表体:".json_encode($bodyInfo)."核放商品表体:".json_encode($detail),
            'add_time' => date("Y-m-d H:i:s",time()),
            //'status_to' => '1',
        ];
        Service_InvtSbLog::add($log);
        return $code;
    }
    /**
     * Note:清单取消
     * User: Yao
     * Date: 2020/8/10
     * Time: 11:56
     * @param $invList
     * @return bool
     * @throws Exception
     */
    public function concel($invtSbList) {
        $invSbListDetail = Service_InvtSbDetail::getByCondition([
            'is_id' => $invtSbList['is_id']
        ]);

        $invtSbList = Service_InvtSb::getByFieldLock($invtSbList['is_id']);
        if (in_array($invtSbList['status'],[2,3])) {
            throw new Exception('核放单申报和过卡状态无法取消');
        }
        //删除核放单
        Service_InvtSb::delete($invtSbList['is_id'],'is_id');
        if(!empty($invSbListDetail)){
            Service_InvtSbDetail::delete($invtSbList['is_id'],'is_id');
        }
        $sbInvtLists = Service_InvtSbInvtlist::getByCondition([
            'is_id' => $invtSbList['is_id']
        ]);
        if(!empty($sbInvtLists)){
            foreach ($sbInvtLists as $k=>$sbl){
                $RltInvtNo = $sbl['RltInvtNo'];
                if(empty($RltInvtNo)){
                    throw new Exception("核注单号为空");
                }else{
                    $invt = Service_InvtList::getByField($RltInvtNo,'BondInvtNo');
                    if(empty($invt)){
                        throw new Exception("核注单号{$RltInvtNo}不存在");
                    }else{
                        if($invt['status']=='4'||$invt['zb_status']=='1'){
                            throw new Exception("核注单号{$RltInvtNo}已经完成或者核扣,不能在删核放单重做");
                        }
                    }
                }
            }
            Service_InvtSbInvtlist::delete($invtSbList['is_id'],'is_id');
        }
        $log = [
            'is_id' => $invtSbList['is_id'],
            'is_node' => '删除核放单'.$invtSbList['is_id'].'-->'.$invtSbList['EtpsPreentNo'],
            'add_time' => date("Y-m-d H:i:s"),
            'ls_text' => json_encode($invtSbList),
        ];
        Service_InvtSbLog::add($log);
        return true;
    }
    /**
     * Note:核放单手工放行
     * User: Yao
     * Date: 2021/10/22
     * Time: 11:54
     * @param $invtSbList
     * @return bool
     * @throws Exception
     */
    public function pass($invtSbList) {
        $invtSbList = Service_InvtSb::getByFieldLock($invtSbList['is_id']);
        if (in_array($invtSbList['status'],['-1','0','4'])) {
            throw new Exception('请检查核放单状态是否正常');
        }
        $update=[
            'is_status'=>2,
        ];
        Service_InvtSb::update($update,$invtSbList['is_id']);
        $log=[
            'is_id'=>$invtSbList['is_id'],
            'is_node'=>'手工放行',
            'add_time'=>date("Y-m-d H:i:s"),
            'ls_text'=>"获取回执：审批通过",
            'file_path'=>''
        ];
        Service_InvtSbLog::add($log);
        return true;
    }
    /**
     * Note:获取申报的企业相关信息
     * User: Yao
     * Date: 2021/6/4
     * Time: 14:56
     * @return array
     */
    public static function getCompanyInfo(){
        return [
            'company_name'=>'',
            'area_code'=>"",//区内企业代码
            'area_credit_code'=>'',//企业信用代码
            'apply_name'=>'',//申请人姓名
        ];
    }

    /**
     * Note:
     * User: Yao
     * Date: 2021/6/7
     * Time: 18:07
     * @param $invtSbList 核注清单表数据
     * @param int $directApplay 是否直接申报 0 否，1是
     */
    public function send($invtSbList,$directApplay = 0){
        $return =[
            'state'=>0,
            'message'=>'fail'
        ];
        // return [
        //     'state'=>1,
        //     'message'=>'发送综保成功'
        // ];
        $xml = new Xml();
        $xml->setDelcareFlag($directApplay);
        $invHead = new PassportHead();
        $invHead->setFromArray($invtSbList);

        //核放单和和核注单的关联关系
        $invSbListBody = Service_InvtSbInvtlist::getByCondition([
            'is_id'=>$invtSbList['is_id']
        ]);
        //print_r($invSbListBody);

        $xml->setPassportHead($invHead);

        if(!empty($invSbListBody)){
            foreach ($invSbListBody as $body){
                $passportAcmpType = new PassportAcmp();
                $passportAcmpType->setFromArray([
                    'RtlBillTypecd'=>$invtSbList['RltTbTypecd'],
                    'RtlBillNo'=>$body['RltInvtNo'],
                ]);
                $xml->addPassportAcmp($passportAcmpType);
            }
        }

        //一票多车情况看核放单有商品表体
        $invSbDetail = Service_InvtSbDetail::getByCondition([
            'is_id'=>$invtSbList['is_id'],
            //'ios_status_neq'=>4
        ]);
        /*print_r($invSbDetail);
        exit;*/
        if(!empty($invSbDetail)){
            foreach ($invSbDetail as $sbdetail){
                $passportListType= new PassportList();
                $passportListType->setFromArray($sbdetail);
                $xml->addPassportList($passportListType);
            }
        }
        /*var_dump($xml);
        exit;*/
        $xmlString = $xml->toXml();

        $sendApi = new Send();
        $result = $sendApi->send($xmlString);
        if($result['state']=='1'){
            $upRow = array(
                'is_status'=>'1'
            );
            Service_InvtSb::update($upRow,$invtSbList['is_id']);
        }
        $return['state'] = $result['state'];
        $return['message'] =$result['message'];
        return $return;
    }
    /**
     * Note:核放单申报回执
     * User: Yao
     * Date: 2021/6/18
     * Time: 18:02
     * @param $dataArray
     * @param $filePath
     */
    public function parseCommonResponse($dataArray,$filePath){
        $commonResponse = $dataArray['RetBizData']['CommonResponeMessage'];
        $etpsNo = $commonResponse['EtpsPreentNo'];
        $seqNo = $commonResponse['SeqNo'];
        $sbInfo = Service_InvtSb::getByField($etpsNo,'EtpsPreentNo');
        if(empty($sbInfo)){
            throw new Exception('核放单信息不存在');
        }
        $update=[
            'SeqNo'=>$seqNo,
        ];
        Service_InvtSb::update($update,$sbInfo['is_id']);
        $log=[
            'is_id'=>$sbInfo['is_id'],
            'is_node'=>'调用成功回执：'.$seqNo,
            'add_time'=>date("Y-m-d H:i:s"),
            'ls_text'=>"调用成功回执：".$seqNo,
            'file_path'=>str_replace(APPLICATION_PATH,'',$filePath)
        ];
        Service_InvtSbLog::add($log);
    }
    /**
     * Note:核放单211解析,审核通过回执
     * User: Yao
     * Date: 2021/6/18
     * Time: 17:54
     * @param $dataArray
     */
    public function parseInvSas211($dataArray,$filePath){
            $sas211 = $dataArray['DataInfo']['BussinessData']['SAS221'];
            $apprResult = $sas211['HdeApprResult'];
            $typeCd = $apprResult['typecd'];
            $manageResult = $apprResult['manageResult'];
            $seqNo = $apprResult['etpsPreentNo'];
            $passportNo= $sas211['SasPassportBsc']['passportNo'];
            $sbInfo = Service_InvtSb::getByField($seqNo,'SeqNo');
            if(empty($sbInfo)){
                throw new Exception('核放单信息不存在');
            }
            if($typeCd==1 && $manageResult ==1){
                $update=[
                    'PassportNo'=>$passportNo,
                    'is_status'=>2,
                ];
                Service_InvtSb::update($update,$sbInfo['is_id']);
                $log=[
                    'is_id'=>$sbInfo['is_id'],
                    'is_node'=>'审批通过',
                    'add_time'=>date("Y-m-d H:i:s"),
                    'ls_text'=>"获取回执：审批通过",
                    'file_path'=>str_replace(APPLICATION_PATH,'',$filePath)
                ];
                if($sbInfo['PassportTypecd'] == 6) {
                    $ruqu = $sbInfo['IoTypecd'] =='I'?'入区':'出区';
                    $content = "# 车牌:" . $sbInfo['VehicleNo'] . '空车'.$ruqu.PHP_EOL;
                    $content .= '# <font color="#dd0000">审批通过</font>' . date("Y-m-d H:i:s", time()) . PHP_EOL;
                    $title = $sbInfo['VehicleNo'] . $ruqu.'审批通过';
                    Service_HandOverSend::add([
                        'hob_code' => $sbInfo['batch_code'],
                        'add_time' => date("Y-m-d H:i:s"),
                        'send_info' => json_encode([
                            'title' => $title,
                            'content' => $content,
                        ]),
                        'feedback_method' => 'diding_send',
                    ]);
                }
                Service_InvtSbLog::add($log);

            }else{
                $update=[];
                if(empty($sbInfo['PassportNo']) && !empty($passportNo)){
                    $update['PassportNo']=$passportNo;
                }
                if($typeCd==1 && $manageResult ==3){
                    $update['is_status']=4;
                    if($sbInfo['batch_code']){
                        $content ="# 车牌:".$sbInfo['VehicleNo'].'<font color="#dd0000">审批退单</font>'.date("Y-m-d H:i:s",time()).PHP_EOL;
                        $content .="*  无法出区，请联系关务同事处理".PHP_EOL;
                        $title =$sbInfo['VehicleNo'].'审批退单';
                        Service_HandOverSend::add([
                            'hob_code'=>$sbInfo['batch_code'],
                            'add_time'=>date("Y-m-d H:i:s"),
                            'send_info'=>json_encode([
                                'title'=>$title,
                                'content'=>$content,
                            ]),
                            'feedback_method'=>'diding_send',
                        ]);
                    }
                }
                if(!empty($update)){
                    Service_InvtSb::update($update,$sbInfo['is_id']);
                }
                $zbStatus = Service_InvtSb::getZbStatus();
                $log=[
                    'is_id'=>$sbInfo['is_id'],
                    'is_node'=>'回执状态：'.$zbStatus[$manageResult],
                    'add_time'=>date("Y-m-d H:i:s"),
                    'ls_text'=>"获取回执：".$zbStatus[$manageResult],
                    'file_path'=>str_replace(APPLICATION_PATH,'',$filePath)
                ];
                Service_InvtSbLog::add($log);
            }
    }
    /**
     * Note:过卡回执
     * User: Yao
     * Date: 2021/6/18
     * Time: 18:38
     * @param $dataArray
     * @param $filePath
     * @throws Exception
     */
    public function parseInvSas223($dataArray,$filePath){
        $sas211 = $dataArray['DataInfo']['BussinessData']['SAS223'];
        $apprResult = $sas211['HdeApprResult'];
        $manageResult = $apprResult['manageResult'];
        $etpPreNo= $apprResult['businessId'];
        $sbInfo = Service_InvtSb::getByField($etpPreNo,'PassportNo');
        if(empty($sbInfo)){
            throw new Exception('核放单信息不存在');
        }
        if($manageResult ==1){
            $update=[
                'is_status'=>3,
            ];
            Service_InvtSb::update($update,$sbInfo['is_id']);
            if($sbInfo['PassportTypecd'] == 6) {
                $ruqu = $sbInfo['IoTypecd'] =='I'?'入区':'出区';
                $content = "# 车牌:" . $sbInfo['VehicleNo'] . '空车'.$ruqu.PHP_EOL;
                $content .= '# 已过卡' . date("Y-m-d H:i:s", time()) . PHP_EOL;
                $title = $sbInfo['VehicleNo'] . $ruqu.'已过卡';
                Service_HandOverSend::add([
                    'hob_code' => $sbInfo['batch_code'],
                    'add_time' => date("Y-m-d H:i:s"),
                    'send_info' => json_encode([
                        'title' => $title,
                        'content' => $content,
                    ]),
                    'feedback_method' => 'diding_send',
                ]);
            }
            $log=[
                'is_id'=>$sbInfo['is_id'],
                'is_node'=>'已过卡',
                'add_time'=>date("Y-m-d H:i:s"),
                'ls_text'=>"已过卡",
                'file_path'=>str_replace(APPLICATION_PATH,'',$filePath)
            ];
            Service_InvtSbLog::add($log);
        }else{
            $zbStatus = Service_InvtSb::getZbStatus();
            $log=[
                'is_id'=>$sbInfo['is_id'],
                'is_node'=>'回执状态：'.$manageResult,
                'add_time'=>date("Y-m-d H:i:s"),
                'ls_text'=>"获取回执：".$manageResult,
                'file_path'=>str_replace(APPLICATION_PATH,'',$filePath)
            ];
            Service_InvtSbLog::add($log);
        }
    }
}
