package com.dxhy.erp.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.dxhy.api.model.db.TDxZzsDkdjWspzEntity;
import com.dxhy.common.constant.ConfigureConstant;
import com.dxhy.common.constant.RedisConstant;
import com.dxhy.common.util.JsonUtils;
import com.dxhy.common.util.MathUtil;
import com.dxhy.common.util.pojo.GlobalInfo;
import com.dxhy.common.util.redis.RedisUtils;
import com.dxhy.common.utils.DateUtils;
import com.dxhy.erp.dao.AuthRecordDao;
import com.dxhy.erp.entity.AuthRecord;
import com.dxhy.erp.enums.GlobalReturnEnums;
import com.dxhy.erp.service.ErpZzsDkdjWspzService;
import com.dxhy.erp.utils.CommonUtils;
import com.dxhy.jxpt.baseService.dao.ZzsDkdjWspzDao;
import com.dxhy.jxpt.baseService.service.ZzsDkdjWspzService;
import com.dxhy.pojo.obj.erp.*;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.*;

/**
 * @author Allen
 * @version 1.0.0
 * @ClassName ErpZzsDkdjWspzServiceImpl.java
 * @Description 对外接口增值税代扣代缴完税凭证业务
 * @createTime 2024年03月18日 20:42:00
 */
@Service
@Slf4j
public class ErpZzsDkdjWspzServiceImpl implements ErpZzsDkdjWspzService {
    private static final String LOG_MSG = "对外接口增值税代扣代缴完税凭证业务：";

    private static final Map<String, Integer> batchMap = new HashMap<>();
    
    @Resource
    private RedisUtils redisUtils;
    
    @Autowired
    private ZzsDkdjWspzService zzsDkdjWspzService;

    @Autowired
    private ZzsDkdjWspzDao zzsDkdjWspzDao;

    @Autowired
    private AuthRecordDao authRecordDao;

    /**
     * @return
     * @description 增值税代扣代缴完税凭证采集
     * @author Allen
     * @date 2024/3/18 20:58
     */
    @Override
    public String zzsDkdjWspzCj(GlobalInfo globalInfo, Map<String, String> requestParam) {
        log.info("{}  采集业务处理开始", LOG_MSG);
        try {
            String content = requestParam.get("data");
            log.info("{} 采集业务报文：{}", LOG_MSG, content);
            ZzsDkdjWspzCjReq req = JsonUtils.getInstance().fromJson(content, ZzsDkdjWspzCjReq.class);
            //参数校验
            Map<String, String> validMap = checkCjParam(req);
            if (validMap != null) {
                log.info("{} 采集业务入参校验失败：{}", LOG_MSG, validMap);
                return CommonUtils.packageReturnMsg(globalInfo, null, validMap.get("returnCode"), validMap.get("returnMsg"));
            }
            //每页数量设置
            if (req.getPageCount() == 0) {
                req.setPageCount(200);
            }
            //批次信息校验
            String redisKey = RedisConstant.BAtCH_CJ+RedisConstant.ZZS_DKDJ_WSPZ_CJ+req.getBatchNo()+"_"+req.getBuyerTaxNo();
            String batchMsg = redisUtils.get(redisKey);
            validMap = CommonUtils.checkBatchMsg(batchMsg,req.getPageNum());
            if (validMap != null) {
                log.info("{} 批次信息校验失败：{}", LOG_MSG, validMap);
                return CommonUtils.packageReturnMsg(globalInfo, null, validMap.get("returnCode"), validMap.get("returnMsg"));
            }

            int pageNum = req.getPageNum();
            long totleCount = 0;
            ZzsDkdjWspzCjResp zdwr = new ZzsDkdjWspzCjResp();
            if (StringUtils.isBlank(batchMsg)) {
                log.info("{} 缓存中不存在当前批次信息，开始执行初始采集，批次号：{}", LOG_MSG, req.getBatchNo());
                //查询总数量
                totleCount = zzsDkdjWspzDao.selectCountByParam(req);
                if (totleCount == 0) {
                    log.info("{} 批次号：{} 未查询到数据", LOG_MSG, req.getBatchNo());
                    zdwr.setWspzs(null);
                    zdwr.setPageNum(1);
                    zdwr.setTotleCount(0);
                    return CommonUtils.packageReturnMsg(globalInfo, JsonUtils.getInstance().toJsonString(zdwr), GlobalReturnEnums.SUCCESS);
                }
                //计算总页数
                int totlePage = totleCount % req.getPageCount() == 0 ? req.getPageCount() : req.getPageCount() + 1;

                log.info("{} 批次号：{}  总数量：{}，总页数：{}", LOG_MSG, req.getBatchNo(),totleCount,totlePage);
                if (totlePage > 1) {
                    log.info("{} 批次号：{}  总页数大于1，缓存批次信息", LOG_MSG, req.getBatchNo());
                    //缓存批次信息
                    RedisBatchInfo ri = new RedisBatchInfo();
                    ri.setTotleCount(totleCount);
                    ri.setTotlePage(totlePage);
                    ri.setParam(content);
                    redisUtils.set(redisKey,JsonUtils.getInstance().toJsonString(ri));
                }
            } else {
                log.info("{} 缓存中存在当前批次信息，使用批次条件采集，批次号：{}", LOG_MSG, req.getBatchNo());
                //获取查询条件
                RedisBatchInfo ri = JsonUtils.getInstance().fromJson(batchMsg, RedisBatchInfo.class);
                req = JsonUtils.getInstance().fromJson(ri.getParam(), ZzsDkdjWspzCjReq.class);
                req.setPageNum(pageNum);
                totleCount = ri.getTotleCount();
                if (pageNum == ri.getTotlePage()) {
                    log.info("{} 当前查询为末次查询，清除批次信息，批次号：{}", LOG_MSG, req.getBatchNo());
                    redisUtils.delete(redisKey);
                }
            }
            zdwr.setTotleCount(totleCount);
            zdwr.setPageNum(pageNum);
            List<ZzsDkdjWspzCjItem> itemList = new ArrayList<>();
            //执行查询操作
            //设置查询的开始页数
            int selStartNum;
            try {
                selStartNum = Integer.valueOf(MathUtil.multi(MathUtil.sub(pageNum+"","1"),req.getPageCount()+""));
                if(MathUtil.compare(selStartNum+"", "0")<0){
                    selStartNum = 0;
                }
            } catch (Exception e) {
                log.info("所传页数不正确");
                selStartNum = Integer.valueOf(totleCount+"");
            }
            req.setPageNum(selStartNum);

            List<TDxZzsDkdjWspzEntity> wspzList = zzsDkdjWspzDao.selectByParam(req);
            wspzList.stream().forEach(wspz ->{
                ZzsDkdjWspzCjItem zdwci = new ZzsDkdjWspzCjItem();
                zdwci.setBdklx(wspz.getBdklx());
                zdwci.setBkjnsrmc(wspz.getBkjnsrmc());
                zdwci.setBkjnsrsbh(wspz.getBkjnsrsbh());
                zdwci.setBdkyy(wspz.getBdkyy());
                zdwci.setCjsj(DateUtils.dateToStrLong(wspz.getCreateTime()));
                zdwci.setJsje(wspz.getJsje().toString());
                zdwci.setKjywrmc(wspz.getKjywrmc());
                zdwci.setKjywrsbh(wspz.getKjywrsbh());
                zdwci.setRzsj(wspz.getRzsj());
                zdwci.setYxse(wspz.getYxse().toString());
                zdwci.setSjje(wspz.getSjje().toString());
                zdwci.setTfrq(DateUtils.dateToStrLong(wspz.getTfrq()));
                zdwci.setDkdjwspzh(wspz.getDkdjwspzh());
                zdwci.setRzssq(wspz.getRzssq());
                zdwci.setRzlx(!wspz.getRzzt().equals("0")?wspz.getRzzt():null);
                zdwci.setRzzt(wspz.getRzzt().equals("0")?"0":"1");
                itemList.add(zdwci);
            });
            zdwr.setWspzs(itemList);
            return CommonUtils.packageReturnMsg(globalInfo, JsonUtils.getInstance().toJsonString(zdwr), GlobalReturnEnums.SUCCESS);
        } catch (Exception e) {
            log.info("{} 业务处理异常：{}", LOG_MSG, e);
            return CommonUtils.packageReturnMsg(globalInfo, null, GlobalReturnEnums.FAILED);
        }
    }

    /**
     * @return
     * @description 采集参数校验
     * @author Allen
     * @date 2024/3/19 11:07
     */
    private Map<String, String> checkCjParam(ZzsDkdjWspzCjReq req) {
        log.info("{} 对外接口增值税代扣代缴完税凭证采集参数校验。。。", LOG_MSG);
        if (req == null) {
            log.info("{} 增值税代扣代缴完税凭证采集参数校验失败，参数为空", LOG_MSG);
            return CommonUtils.packageValidResult(GlobalReturnEnums.PARAM_NULL);
        }

        if (StringUtils.isBlank(req.getBuyerTaxNo())) {
            log.info("{} 扣缴义务人识别号不能为空", LOG_MSG);
            return CommonUtils.packageValidResult(GlobalReturnEnums.ZZS_DKDJ_WSPZ_TAXNO_NULL);
        }

        if (StringUtils.isBlank(req.getBatchNo())) {
            log.info("{} 批次号不能为空", LOG_MSG);
            return CommonUtils.packageValidResult(GlobalReturnEnums.PARAM_PCLSH_NULL);
        }

        if (req.getPageNum() < ConfigureConstant.INT_1) {
            log.info("{} 当前页码不能小于1", LOG_MSG);
            return CommonUtils.packageValidResult(GlobalReturnEnums.PARAM_PAGEN_NUM_ERROR);
        }

        if (req.getPageNum() > ConfigureConstant.INT_1000) {
            log.info("{} 每页数量超过最大支持数量" + ConfigureConstant.INT_1000, LOG_MSG);
            return CommonUtils.packageValidResult(GlobalReturnEnums.PARAM_PAGEN_NUM_ERROR);
        }

        if (StringUtils.isNotBlank(req.getDkdjwspzbh())) {
            if (StringUtils.isNotBlank(req.getCreateDateBegin()) || StringUtils.isNotBlank(req.getCreateDateEnd())) {
                log.info("{} 单张查询，不允许传递时间条件", LOG_MSG);
                return CommonUtils.packageValidResult(GlobalReturnEnums.ZZS_DKDJ_WSPZ_PARAM_ERROR_1);
            }
        }
        if (StringUtils.isNotBlank(req.getCreateDateBegin()) && StringUtils.isNotBlank(req.getCreateDateEnd())) {
            if (StringUtils.isNotBlank(req.getDkdjwspzbh())) {
                log.info("{} 批量查询，不允许传递凭证编号", LOG_MSG);
                return CommonUtils.packageValidResult(GlobalReturnEnums.ZZS_DKDJ_WSPZ_PARAM_ERROR_2);
            }
        }
//        if (StringUtils.isNotBlank(req.getCreateDateBegin()) || StringUtils.isNotBlank(req.getCreateDateEnd())) {
//            log.info("{} 批量查询，采集时间条件必传", LOG_MSG);
//            return CommonUtils.packageValidResult(GlobalReturnEnums.ZZS_DKDJ_WSPZ_PARAM_ERROR_3);
//        }
        if ((StringUtils.isBlank(req.getCreateDateBegin()) || StringUtils.isBlank(req.getCreateDateEnd())) && StringUtils.isBlank(req.getDkdjwspzbh())) {
            log.info("{} 采集时间条件和凭证编号条件不能同时为空", LOG_MSG);
            return CommonUtils.packageValidResult(GlobalReturnEnums.ZZS_DKDJ_WSPZ_PARAM_ERROR_4);
        }
        return null;
    }

    /**
     * @description  增值税代扣代缴完税凭证认证申请
     * @author  Allen
     * @date    2024/3/20 14:17
     * @return
    */
    @Override
    public String zzsDkdjWspzRzsq(GlobalInfo globalInfo, Map<String, String> requestParam) {
        log.info("{} 认证申请业务处理开始", LOG_MSG);
        try {
            String content = requestParam.get("data");
            log.info("{} 认证申请业务报文：{}", LOG_MSG, content);
            ZzsDkdjWspzRzsqReq req = JsonUtils.getInstance().fromJson(content, ZzsDkdjWspzRzsqReq.class);
            //参数校验
            Map<String, String> validMap = checkRzsqParam(req);
            if (validMap != null) {
                log.info("{} 认证申请业务入参校验失败：{}", LOG_MSG, validMap);
                return CommonUtils.packageReturnMsg(globalInfo, null, validMap.get("returnCode"), validMap.get("returnMsg"));
            }

            //保存申请批次信息
            List<AuthRecord> abis = buildAuthBatchInfo(req);
            authRecordDao.insertAuthRecord(abis,ConfigureConstant.STRING_3);

            //处理票据状态
            String rzlx = null;
            if (ConfigureConstant.STRING_1.equals(req.getRzlx())) {
                rzlx = "01";
            }
            if (ConfigureConstant.STRING_4.equals(req.getRzlx())) {
                rzlx = "03";
            }
            if (ConfigureConstant.STRING_5.equals(req.getRzlx())) {
                rzlx = "02";
            }
            if (ConfigureConstant.STRING_6.equals(req.getRzlx())) {
                rzlx = "04";
            }
            for (String wspz : req.getWspzs()) {
                TDxZzsDkdjWspzEntity tzdw = zzsDkdjWspzService.selectByWspzbh(wspz);
                if (ConfigureConstant.STRING_1.equals(req.getRzlx())||ConfigureConstant.STRING_4.equals(req.getRzlx())) {
                    //抵扣处理
                    if (ConfigureConstant.STRING_0.equals(tzdw.getRzzt())
                            &&!ConfigureConstant.STRING_1.equals(tzdw.getRzclzt())
                            &&!ConfigureConstant.STRING_2.equals(tzdw.getRzclzt())
                            &&!ConfigureConstant.STRING_4.equals(tzdw.getRzclzt())) {
                        log.info("{} 认证申请业务抵扣处理，凭证编号：{}", LOG_MSG, tzdw.getDkdjwspzh());
                        //更新底账库票据状态
                        tzdw.setRzclzt("1");
                        tzdw.setRzssq(req.getRzssq());
                        tzdw.setCzr(req.getCzr());
                        tzdw.setCzsj(new Date());
                        tzdw.setRzlx(rzlx);
                        tzdw.setBdklx(req.getBdklx());
                        tzdw.setBdkyy(req.getBdkyy());
                    }
                }
                if (ConfigureConstant.STRING_5.equals(req.getRzlx())) {
                    //撤销抵扣处理
                    if (ConfigureConstant.STRING_1.equals(tzdw.getRzzt())
                            &&!ConfigureConstant.STRING_5.equals(tzdw.getRzclzt())
                            &&!ConfigureConstant.STRING_6.equals(tzdw.getRzclzt())
                            &&!ConfigureConstant.STRING_8.equals(tzdw.getRzclzt())) {
                        log.info("{} 认证申请业务撤销抵扣处理，凭证编号：{}", LOG_MSG, tzdw.getDkdjwspzh());
                        //更新底账库票据状态
                        tzdw.setRzclzt("5");
                        tzdw.setRzssq(req.getRzssq());
                        tzdw.setCzr(req.getCzr());
                        tzdw.setCzsj(new Date());
                        tzdw.setRzlx(rzlx);
                    }
                }
                if (ConfigureConstant.STRING_6.equals(req.getRzlx())) {
                    //撤销不抵扣处理
                    if (ConfigureConstant.STRING_2.equals(tzdw.getRzzt())
                            &&!ConfigureConstant.STRING_5.equals(tzdw.getRzclzt())
                            &&!ConfigureConstant.STRING_6.equals(tzdw.getRzclzt())
                            &&!ConfigureConstant.STRING_8.equals(tzdw.getRzclzt())) {
                        log.info("{} 认证申请业务撤销不抵扣处理处理，凭证编号：{}", LOG_MSG, tzdw.getDkdjwspzh());
                        //更新底账库票据状态
                        tzdw.setRzclzt("5");
                        tzdw.setRzssq(req.getRzssq());
                        tzdw.setCzr(req.getCzr());
                        tzdw.setCzsj(new Date());
                        tzdw.setRzlx(rzlx);
                    }
                }
                tzdw.setUpdateTime(new Date());
                log.info("{} 认证申请业务，更新完税凭证编号底账表", LOG_MSG);
                zzsDkdjWspzService.updateByWspzh(tzdw);
            }

            log.info("{} 认证申请处理完成，批次号：{}", LOG_MSG,req.getBatchNo());
            return CommonUtils.packageReturnMsg(globalInfo, null, GlobalReturnEnums.SUCCESS);
        } catch (Exception e) {
            log.info("{} 业务处理异常：{}", LOG_MSG,e);
            return CommonUtils.packageReturnMsg(globalInfo, null, GlobalReturnEnums.FAILED);
        }
    }

    private List<AuthRecord> buildAuthBatchInfo(ZzsDkdjWspzRzsqReq req) {
        List<AuthRecord> list = new ArrayList<>();
        req.getWspzs().stream().forEach(wspz->{
            AuthRecord ar = new AuthRecord();
            ar.setBatchNo(req.getBatchNo());
            ar.setRzlx(req.getRzlx());
            ar.setBuyerTaxNo(req.getBuyerTaxNo());
            ar.setApplyTaxPeriod(req.getRzssq());
            ar.setDkdjwspzh(wspz);
            list.add(ar);
        });
        return list;
    }

    /**
     * @description  参数校验
     * @author  Allen
     * @date    2024/3/20 14:38
     * @return
    */
    private Map<String, String> checkRzsqParam(ZzsDkdjWspzRzsqReq req) {

        if (req == null) {
            log.info("{} 增值税代扣代缴完税凭证认证申请参数校验失败，参数为空", LOG_MSG);
            return CommonUtils.packageValidResult(GlobalReturnEnums.PARAM_NULL);
        }

        if (StringUtils.isBlank(req.getBuyerTaxNo())) {
            log.info("{} 扣缴义务人识别号不能为空", LOG_MSG);
            return CommonUtils.packageValidResult(GlobalReturnEnums.ZZS_DKDJ_WSPZ_TAXNO_NULL);
        }

        //批次号验证
        if (StringUtils.isBlank(req.getBatchNo())) {
            log.info("{} 批次号不能为空", LOG_MSG);
            return CommonUtils.packageValidResult(GlobalReturnEnums.PARAM_PCLSH_NULL);
        }

        if (authRecordDao.selectCountRecord(req.getBatchNo(),req.getBuyerTaxNo(),ConfigureConstant.STRING_3) > 0) {
            log.info("{} 批次号已存在", LOG_MSG);
            return CommonUtils.packageValidResult(GlobalReturnEnums.PARAM_PCH_ERROR);
        }

        //认证类型校验
        if (StringUtils.isBlank(req.getRzlx())) {
            log.info("{} 增值税代扣代缴完税凭证认证类型不能为空", LOG_MSG);
            return CommonUtils.packageValidResult(GlobalReturnEnums.ZZS_DKDJ_WSPZ_RZLX_NULL);
        }

        if (!ConfigureConstant.STRING_1.equals(req.getRzlx())
            &&!ConfigureConstant.STRING_4.equals(req.getRzlx())
            &&!ConfigureConstant.STRING_5.equals(req.getRzlx())
            &&!ConfigureConstant.STRING_6.equals(req.getRzlx())) {
            log.info("{} 认证类型只能为1、4、5、6", LOG_MSG);
            return CommonUtils.packageValidResult(GlobalReturnEnums.ZZS_DKDJ_WSPZ_RZLX_ERROR);
        }

        //不抵扣类型，不抵扣原因校验
        if (StringUtils.isNotBlank(req.getRzlx()) && ConfigureConstant.STRING_4.equals(req.getRzlx())) {
            if (StringUtils.isBlank(req.getBdklx())) {
                log.info("{} 认证类型为不抵扣时，不抵扣类型不能为空", LOG_MSG);
                return CommonUtils.packageValidResult(GlobalReturnEnums.ZZS_DKDJ_WSPZ_BDKLX_NULL);
            }

            if (!ConfigureConstant.STRING_1.equals(req.getBdklx())
                    &&!ConfigureConstant.STRING_2.equals(req.getBdklx())
                    &&!ConfigureConstant.STRING_3.equals(req.getBdklx())
                    &&!ConfigureConstant.STRING_4.equals(req.getBdklx())
                    &&!ConfigureConstant.STRING_5.equals(req.getBdklx())) {
                log.info("{} 不抵扣类型只能为1、2、3、4、5", LOG_MSG);
                return CommonUtils.packageValidResult(GlobalReturnEnums.ZZS_DKDJ_WSPZ_BDKLX_ERROR);
            }

            if (ConfigureConstant.STRING_5.equals(req.getBdklx())) {
                if (StringUtils.isBlank(req.getBdkyy())) {
                    log.info("{} 不抵扣为5时传不抵扣原因必传", LOG_MSG);
                    return CommonUtils.packageValidResult(GlobalReturnEnums.ZZS_DKDJ_WSPZ_BDKYY_ERROR);
                }
            }
        }

        //属期格式校验
        if (StringUtils.isBlank(req.getRzssq())) {
            log.info("{} 属期不能为空",LOG_MSG);
            return CommonUtils.packageValidResult(GlobalReturnEnums.PARAM_SQ_NULL);
        }

        if (!CommonUtils.validMonth(req.getRzssq())) {
            log.info("{} 属期格式必须满足“YYYYMM”",LOG_MSG);
            return CommonUtils.packageValidResult(GlobalReturnEnums.PARAM_SQ_FORMAT_ERROR);
        }

        if (StringUtils.isBlank(req.getCzr())) {
            log.info("{} 认证操作人不能为空",LOG_MSG);
            return CommonUtils.packageValidResult(GlobalReturnEnums.ZZS_DKDJ_WSPZ_CZR_NULL);
        }

        //认证明细校验
        if (req.getWspzs() == null || req.getWspzs().size() == 0) {
            log.info("{} 认证明细为空",LOG_MSG);
            return CommonUtils.packageValidResult(GlobalReturnEnums.ZZS_DKDJ_WSPZ_MX_NULL);
        }

        if (req.getWspzs().size() > 500) {
            log.info("{} 认证明细数量不能超过500",LOG_MSG);
            return CommonUtils.packageValidResult(GlobalReturnEnums.ZZS_DKDJ_WSPZ_MX_ERROR);
        }

        for (String wspz : req.getWspzs()) {
            if (zzsDkdjWspzService.selectByWspzbh(wspz) == null) {
                log.info("{} 底账库不存在增值税代扣代缴完税凭证，凭证编号：{}",LOG_MSG,wspz);
                return CommonUtils.packageValidResult(GlobalReturnEnums.ZZS_DKDJ_WSPZ_NOT_EXIST,req.getWspzs().indexOf(wspz));
            }
        }
        return null;
    }

    /**
     * @description  增值税代扣代缴完税凭证认证结果获取
     * @author  Allen
     * @date    2024/3/20 14:17
     * @return
    */
    @Override
    public String zzsDkdjWspzRzjghq(GlobalInfo globalInfo, Map<String, String> requestParam) {
        log.info("{} 认证结果获取业务处理开始", LOG_MSG);
        try {
            String content = requestParam.get("data");
            log.info("{} 认证结果获取业务报文：{}", LOG_MSG, content);
            ZzsDkdjWspzRzjghqReq req = JsonUtils.getInstance().fromJson(content, ZzsDkdjWspzRzjghqReq.class);
            //参数校验
            Map<String, String> validMap = checkRzjghqParam(req);
            if (validMap != null) {
                log.info("{} 认证结果获取业务入参校验失败：{}", LOG_MSG, validMap);
                return CommonUtils.packageReturnMsg(globalInfo, null, validMap.get("returnCode"), validMap.get("returnMsg"));
            }
            //判断批次处理状态
            //查询批次信息
            List<AuthRecord> authRecords = authRecordDao.selectList(new QueryWrapper<AuthRecord>()
                    .eq("batch_no", req.getBatchNo())
                    .eq("buyer_tax_no", req.getBuyerTaxNo()));
            String batchStatus = ConfigureConstant.STRING_Y;
            for (AuthRecord ar : authRecords) {
                TDxZzsDkdjWspzEntity wspz = zzsDkdjWspzService.selectByWspzbh(ar.getDkdjwspzh());
                if (ConfigureConstant.STRING_0.equals(wspz.getRzclzt())
                    ||ConfigureConstant.STRING_1.equals(wspz.getRzclzt())
                    ||ConfigureConstant.STRING_2.equals(wspz.getRzclzt())
                    ||ConfigureConstant.STRING_5.equals(wspz.getRzclzt())
                    ||ConfigureConstant.STRING_6.equals(wspz.getRzclzt())) {
                    log.info("{} 凭证编号：{} 的票据尚未处理完成", LOG_MSG, wspz.getDkdjwspzh());
                    batchStatus = ConfigureConstant.STRING_N;
                    break;
                }
            }
            //返回数据处理
            ZzsDkdjWspzRzjghqResp jghqResp = new ZzsDkdjWspzRzjghqResp();
            List<ZzsDkdjWspzRzjghqRespItem> itemList = new ArrayList<>();
            jghqResp.setBatchState(batchStatus);
            //批次未处理完返回
            if (ConfigureConstant.STRING_N.equals(batchStatus)) {
                log.info("{} 批次号：{} 该批次处理完成", LOG_MSG, req.getBatchNo());
                return CommonUtils.packageReturnMsg(globalInfo, JsonUtils.getInstance().toJsonString(jghqResp), GlobalReturnEnums.SUCCESS);
            }
            //批次处理完返回
            for (AuthRecord ar : authRecords) {
                TDxZzsDkdjWspzEntity wspz = zzsDkdjWspzService.selectByWspzbh(ar.getDkdjwspzh());
                ZzsDkdjWspzRzjghqRespItem respItem = new ZzsDkdjWspzRzjghqRespItem();
                respItem.setDkdjwspzh(wspz.getDkdjwspzh());
                respItem.setRzssq(wspz.getRzssq());
                respItem.setRzzt(wspz.getRzzt());
                respItem.setRzlx(ar.getRzlx());
                if (ConfigureConstant.STRING_3.equals(wspz.getRzclzt())||ConfigureConstant.STRING_7.equals(wspz.getRzclzt())) {
                    respItem.setRzcljg(ConfigureConstant.STRING_2);
                }
                if (ConfigureConstant.STRING_4.equals(wspz.getRzclzt())||ConfigureConstant.STRING_8.equals(wspz.getRzclzt())) {
                    respItem.setRzcljg(ConfigureConstant.STRING_1);
                }
                respItem.setRzsj(wspz.getRzsj());
                respItem.setRzsbyy(wspz.getRzsbyy());
                respItem.setBdklx(wspz.getBdklx());
                respItem.setBdkyy(wspz.getBdkyy());
                itemList.add(respItem);
            }
            jghqResp.setWspzs(itemList);
            log.info("{} 结果获取完成，批次号：{}", LOG_MSG,req.getBatchNo());
            return CommonUtils.packageReturnMsg(globalInfo, JsonUtils.getInstance().toJsonString(jghqResp), GlobalReturnEnums.SUCCESS);
        } catch (Exception e) {
            log.info("{} 业务处理异常：{}", LOG_MSG,e);
            return CommonUtils.packageReturnMsg(globalInfo, null, GlobalReturnEnums.FAILED);
        }
    }

    private Map<String, String> checkRzjghqParam(ZzsDkdjWspzRzjghqReq req) {
        if (req == null) {
            log.info("{} 增值税代扣代缴完税凭证认证结果获取参数校验失败，参数为空", LOG_MSG);
            return CommonUtils.packageValidResult(GlobalReturnEnums.PARAM_NULL);
        }

        if (StringUtils.isBlank(req.getBuyerTaxNo())) {
            log.info("{} 扣缴义务人识别号不能为空", LOG_MSG);
            return CommonUtils.packageValidResult(GlobalReturnEnums.ZZS_DKDJ_WSPZ_TAXNO_NULL);
        }

        //批次号验证
        if (StringUtils.isBlank(req.getBatchNo())) {
            log.info("{} 批次号不能为空", LOG_MSG);
            return CommonUtils.packageValidResult(GlobalReturnEnums.PARAM_PCLSH_NULL);
        }

        if (authRecordDao.selectCountRecord(req.getBatchNo(),req.getBuyerTaxNo(),ConfigureConstant.STRING_3) == 0) {
            log.info("{} 批次号不存在", LOG_MSG);
            return CommonUtils.packageValidResult(GlobalReturnEnums.PARAM_PCH_NO_EXIST);
        }
        return null;
    }
}
