package com.dxhy.customs.service.impl;

import java.math.BigDecimal;
import java.text.SimpleDateFormat;
import java.time.LocalDate;
import java.util.*;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import javax.annotation.Resource;

import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.dxhy.common.constant.DbConstant;
import com.dxhy.common.constant.DictConstant;
import com.dxhy.common.datasource.config.DynamicContextHolder;
import com.dxhy.common.enums.RzztEnum;
import com.dxhy.common.service.CustomsLogService;
import com.dxhy.common.service.DictdetaServcice;
import com.dxhy.common.service.TabulateRedisService;
import com.dxhy.common.service.impl.MpBaseServiceImpl;
import com.dxhy.common.util.AmountFormatUtil;
import com.dxhy.common.util.MathUtil;
import com.dxhy.common.utils.DateUtils;
import com.dxhy.common.vo.Business;
import com.dxhy.common.vo.CustomsLog;
import com.dxhy.common.vo.UserInfo;
import com.dxhy.customs.constants.QsTypeEnum;
import com.dxhy.customs.dao.CustomsRecordDao;
import com.dxhy.customs.entity.TdxCustomsRecord;
import com.dxhy.customs.entity.TdxDkCount;
import com.dxhy.customs.entity.TdxTaxCurrent;
import com.dxhy.customs.model.CustomsRecordImportVO;
import com.dxhy.customs.model.ResultData;
import com.dxhy.customs.model.TdxCustomsRecordModelDTO;
import com.dxhy.customs.service.CustomsDkCountService;
import com.dxhy.customs.service.CustomsRecordDkrzService;
import com.dxhy.customs.service.CustomsTaxCurrentService;
import com.dxhy.customs.utils.DateUtil;
import com.dxhy.customs.utils.ObjectUtil;

import cn.hutool.core.codec.Base64;
import lombok.extern.slf4j.Slf4j;

/**
 * @author dxhy
 */
@SuppressWarnings("AlibabaMethodTooLong")
@Service("customsRecordDkrzService")
@Slf4j
public class CustomsRecordDkrzServiceImpl extends MpBaseServiceImpl<CustomsRecordDao, TdxCustomsRecord>
    implements CustomsRecordDkrzService {

    /**
     * 判断小数点后2位的数字的正则表达式
     */
    private static final Pattern PATTERN_AMOUNT = Pattern.compile("^(([1-9]{1}\\d*)|([0]{1}))(\\.(\\d){0,2})?$");
    @Resource
    private CustomsTaxCurrentService customsTaxCurrentService;
    @Resource
    private CustomsDkCountService customsDkCountService;
    @Resource
    private CustomsLogService customsLogService;
    @Resource
    private TabulateRedisService tabulateRedisService;
    @Resource
    private DictdetaServcice dictdetaServcice;

    @Value("${dkse.limit}")
    private boolean limit;

    /**
     * 抵扣认证 - 缴款书手工认证-列表查询
     *
     * @param page
     * @param dto
     * @param userInfo
     * @return java.lang.String
     * @author 陈刚扬
     * @date 2020/8/10
     */
    @Override
    public String querySgByPage(Page page, TdxCustomsRecordModelDTO dto, UserInfo userInfo) throws Exception {
        DynamicContextHolder
            .push(StringUtils.defaultString(userInfo.getDbName(), "business") + DbConstant.BUSINESS_READ);
        dto.setParam(page, userInfo);

        ResultData<Object> resultData = new ResultData<>();
        JSONObject tabulateMenu = tabulateRedisService.getTabulateMenu(dto.getCompany(), dto.getMenuId());
        resultData.putTabulateMenu(tabulateMenu);

        if (dto.getAccountPeriod() != null && !"".equals(dto.getAccountPeriod())) {
            String accountPeriod = dto.getAccountPeriod().substring(0,7);
            dto.setAccountPeriod(accountPeriod);
            log.info("accountPeriod:{}",accountPeriod);
        }

        Map mapTj = null;
        Page<Map<String, Object>> result = null;
        if (StringUtils.isNotBlank(dto.getGfsh()) || dto.getGfTaxNo().size() > 0) {
            if (dto.getCurr() == 1) {
                mapTj = this.baseMapper.querySgBySum(dto);
            }

            result = this.baseMapper.querySgByPage(page, dto);
            if (ResultData.isEmpty(result)) {
                SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
                // List<DictdetaModel> lyModels = dictdetaServcice.selectDictdeta("jksSources");
                // List<DictdetaModel> qsztModels = dictdetaServcice.selectDictdeta("qsStatus");
                // List<DictdetaModel> qsfsModels = dictdetaServcice.selectDictdeta("qsType");
                for (int i = 0; i < page.getRecords().size(); i++) {
                    Map<String, Object> map = result.getRecords().get(i);

                    String qsStatus = (String)map.get("qsStatus");
                    String dataSources = (String)map.get("dataSources");
                    map.put("id", String.valueOf(map.get("id")));
                    map.put("xh", String
                        .valueOf((((page.getCurrent() == 0 ? 1 : page.getCurrent()) - 1) * page.getSize()) + i + 1));
                    if (map.get("fillInDate") != null) {
                        map.put("fillInDate", map.get("fillInDate"));
                    }

                    // 字典转换
                    map.put("dataSources", dictdetaServcice.queryDictName(DictConstant.JKSCJFS, dataSources));
                    map.put("qsStatus", dictdetaServcice.queryDictName(DictConstant.QS_STATUS, qsStatus));
                    String qsType = (String)map.get("qsType");
                    map.put("qsType", dictdetaServcice.queryDictName(DictConstant.QS_TYPE, qsType));
                    if ("1".equals(map.get("inAccountStatus"))){
                        map.put("inAccountStatus","已入账");
                    }else if ("0".equals(map.get("inAccountStatus"))){
                        map.put("inAccountStatus","未入账");
                    }

                    if (map.get("qsDate") != null) {
                        map.put("qsDate", map.get("qsDate"));
                    }

//                    map.put("qsType", QsTypeEnum.getVal(ObjectUtil.fixNull(map.get("qsType"))));
                    map.put("taxAmount", AmountFormatUtil.fmtMicrometer(MathUtil.round((String)map.get("taxAmount"))));
                    map.put("yxTaxAmount", AmountFormatUtil.fmtMicrometer(MathUtil.round((String)map.get("yxTaxAmount"))));
                    map.put("upgradeStatus", "1");
                    map.put("select", true);

                    TdxDkCount tDxDkCount = null;
                    TdxTaxCurrent tDxTaxCurrent = null;
                    List<TdxTaxCurrent> tDxTaxCurrents = customsTaxCurrentService
                        .list(Wrappers.<TdxTaxCurrent>query().lambda().eq(TdxTaxCurrent::getTaxno, map.get("gfTaxNo")));

                    if (tDxTaxCurrents == null || tDxTaxCurrents.size() == 0) {
                        map.put("select", false);
                        continue;
                    }
                    tDxTaxCurrent = tDxTaxCurrents.get(0);
                    List<TdxDkCount> tDxDkCounts = customsDkCountService
                        .list(Wrappers.<TdxDkCount>query().lambda().eq(TdxDkCount::getTaxno, map.get("gfTaxNo"))
                            .eq(TdxDkCount::getSkssq, tDxTaxCurrent.getCurrentTaxPeriod()));

                    if (tDxDkCounts == null || tDxDkCounts.size() == 0) {
                        map.put("select", false);
                        continue;
                    }

                    tDxDkCount = tDxDkCounts.get(0);

                    if (StringUtils.isBlank(tDxTaxCurrent.getCurrentTaxPeriod())) {
                        map.put("select", false);
                        continue;
                    }

                    if (!"0".equals(map.get("glzt"))) {
                        map.put("select", false);
                        continue;
                    }

                    String applyCheck = (String)map.get("applyCheck");
                    String checkStatus = (String)map.get("checkStatus");
                    String auditStatus = (String)map.get("auditStatus");
                    // 采集数据
                    if ("0".equals(dataSources)) {
                        if (!"0".equals(map.get("entryDiscrepancy"))) {
                            map.put("select", false);
                            continue;
                        }
                    } else {
                        // 录入数据
                        if ("0".equals(applyCheck) && "1".equals(auditStatus)) {

                        } else if ("1".equals(applyCheck) && "0".equals(checkStatus)) {

                        } else {
                            map.put("select", false);
                            continue;
                        }
                    }

                    String fillInDate = (String)map.get("fillInDate");
                    if (!DateUtil.isInTwoDate(tDxTaxCurrent.getSelectStartDate(), tDxTaxCurrent.getSelectEndDate(),
                        fillInDate)) {
                        map.put("select", false);
                        continue;
                    }

                    if (!"0".equals(tDxDkCount.getTjStatus()) && !"3".equals(tDxDkCount.getTjStatus())) {
                        map.put("select", false);
                        continue;
                    }

                    if (!"1".equals(qsStatus) && "1".equals(userInfo.getQsStatus())) {
                        map.put("select", false);
                    }
                }
            }
        } else {
            resultData.setDataList(new ArrayList<>());
            return resultData.toJsonBase();
        }
        return resultData.getTjData(mapTj).getPageData(result).toJsonBase();
    }

    /**
     * 缴款书调教认证抵扣、不抵扣
     *
     * @param dto
     *            实体
     * @param userInfo
     *            用户信息
     * @return java.lang.String
     * @author 陈刚扬
     * @date 2020/8/10
     */
    @Override
    public String approveSg(TdxCustomsRecordModelDTO dto, UserInfo userInfo) {

        Map<String, Object> resultMap = new HashMap<>(16);

        long total = 0;
        BigDecimal hjse = new BigDecimal(0);
        BigDecimal hjyxse = new BigDecimal(0);
        String message = "";

        boolean flag = false;
        boolean flag1 = false;
        boolean flag2 = false;
        boolean flag3 = false;

        List<String> sb1List = new ArrayList<>();
        List<String> sb2List = new ArrayList<>();
        List<String> sb3List = new ArrayList<>();

        StringBuilder sb = new StringBuilder();
        StringBuilder sb1 = new StringBuilder();
        StringBuilder sb2 = new StringBuilder();
        StringBuilder sb3 = new StringBuilder();

        sb.append("以下缴款书号码的抵扣税款金额大于税款金额不可勾选认证： 发票号码\n ");
        sb1.append("以下税号已被锁定不可勾选认证： 税号\n");
        sb2.append("以下税号已申请抵扣统计，请撤销统计后重新操作：税号  \n ");
        sb3.append("以下税号本月发票可认证总税额超限：税号\n");

        // 1.查询数据
        DynamicContextHolder
            .push(StringUtils.defaultString(userInfo.getDbName(), "business") + DbConstant.BUSINESS_READ);
        List<TdxCustomsRecord> records = new ArrayList<>();
        // 2.校验
        List<String> dkList = new ArrayList<>();
        // 抵扣限额合计
        BigDecimal dkTaxAmount = new BigDecimal(0);
        List<CustomsLog> logList = new ArrayList<>();
        for (String idstr : dto.getIds()) {

            if (idstr == null) {
                continue;
            }

            String[] arr = idstr.split(":");
            Long id = Long.parseLong(arr[0]);
            // 有效税额
            String yxTaxAmount = arr[1];

            if (yxTaxAmount != null) {
                yxTaxAmount = yxTaxAmount.replaceAll(",", "");
                hjyxse = hjyxse.add(BigDecimal.valueOf(Double.parseDouble(yxTaxAmount)));
            }

            TdxCustomsRecord record = this.baseMapper.selectById(id);
            if (record != null) {

                TdxTaxCurrent tDxTaxCurrent = null;
                TdxDkCount tDxDkCount = null;
                List<TdxTaxCurrent> tDxTaxCurrents = customsTaxCurrentService
                    .list(Wrappers.<TdxTaxCurrent>query().lambda().eq(TdxTaxCurrent::getTaxno, record.getGfTaxNo()));

                tDxTaxCurrent = tDxTaxCurrents.get(0);

                List<TdxDkCount> tDxDkCounts = customsDkCountService
                    .list(Wrappers.<TdxDkCount>query().lambda().eq(TdxDkCount::getTaxno, record.getGfTaxNo())
                        .eq(TdxDkCount::getSkssq, tDxTaxCurrent.getCurrentTaxPeriod()));

                tDxDkCount = tDxDkCounts.get(0);
                CustomsLog customsLog = new CustomsLog();

                // 认证抵扣
                if ("1".equals(dto.getRzlx())) {
                    if (Double.parseDouble(yxTaxAmount) < 0
                        || Double.parseDouble(yxTaxAmount) > Double.parseDouble(record.getTaxAmount())) {
                        sb.append(record.getCustomsCode()).append(" \n ");
                        flag = true;
                        continue;
                    }
                    if ("1".equals(tDxTaxCurrent.getLatchStatus())) {
                        if (!sb1List.contains(record.getGfTaxNo())) {
                            sb1.append(record.getGfTaxNo()).append(" \n ");
                            sb1List.add(record.getGfTaxNo());
                        }
                        flag1 = true;
                        continue;
                    }
                    if (!"0".equals(tDxDkCount.getTjStatus()) && !"3".equals(tDxDkCount.getTjStatus())) {
                        if (!sb2List.contains(record.getGfTaxNo())) {
                            sb2.append(record.getGfTaxNo()).append(" \n ");
                            sb2List.add(record.getGfTaxNo());
                        }
                        flag2 = true;
                        continue;
                    }

                    // 判断抵扣限额
                    if (limit && tDxTaxCurrent.getLimitDkse() != null
                        && tDxTaxCurrent.getLimitDkse().compareTo(BigDecimal.ZERO) != 0) {
                        if (!dkList.contains(record.getGfTaxNo())) {

                            Map<String, Object> paramMap = new HashMap<>(16);
                            paramMap.put("gfsh", record.getGfTaxNo());
                            paramMap.put("skssq", tDxTaxCurrent.getCurrentTaxPeriod());
                            TdxCustomsRecord dkInvoice = super.baseMapper.selectDkse(paramMap);

                            if (dkInvoice != null && dkInvoice.getYxTaxAmount() != null) {
                                dkTaxAmount =
                                    dkTaxAmount.add(BigDecimal.valueOf(Double.parseDouble(dkInvoice.getYxTaxAmount())));
                            }
                            dkTaxAmount = dkTaxAmount.add(BigDecimal.valueOf(Double.parseDouble(yxTaxAmount)));

                            if (dkTaxAmount.compareTo(tDxTaxCurrent.getLimitDkse()) > 0) {
                                dkList.add(record.getGfTaxNo());

                                if (!sb3List.contains(record.getGfTaxNo())) {
                                    sb3.append(record.getGfTaxNo()).append(" \n ");
                                    sb3List.add(record.getGfTaxNo());
                                }
                                flag3 = true;
                                continue;
                            }

                        }
                    }

                    record.setBdkStatus("1");
                    record.setRzlx("1");
                    record.setRzhBelongDate(tDxTaxCurrent.getCurrentTaxPeriod());
                    record.setAuthStatus("2");
                    record.setConfirmDate(DateUtils.getSqlDate());
                    record.setConfirmUser(userInfo.getUsername());
                    record.setCxrzStatus("0");
                    record.setCxrzDate(null);
                    record.setRzhType(dto.getRzhType());
                    record.setYxTaxAmount(yxTaxAmount);
                    records.add(record);
                    customsLog.setCustomsCode(record.getCustomsCode());
                    customsLog.setType("2");
                    customsLog.setCreateDate(new Date());
                    customsLog.setInputName(userInfo.getLoginname());
                    customsLog.setInputStatus("14");
                    logList.add(customsLog);
                } else if ("2".equals(dto.getRzlx())) {
                    log.info("认证不抵扣操作");
                    if ("1".equals(tDxTaxCurrent.getLatchStatus())) {
                        if (!sb1List.contains(record.getGfTaxNo())) {
                            sb1.append(record.getGfTaxNo()).append(" \n ");
                            sb1List.add(record.getGfTaxNo());
                        }
                        flag1 = true;
                        continue;
                    }
                    if (!"0".equals(tDxDkCount.getTjStatus()) && !"3".equals(tDxDkCount.getTjStatus())) {
                        if (!sb2List.contains(record.getGfTaxNo())) {
                            sb2.append(record.getGfTaxNo()).append(" \n ");
                            sb2List.add(record.getGfTaxNo());
                        }
                        flag2 = true;
                        continue;
                    }

                    record.setRzlx("4");
                    record.setBdkStatus("2");
                    record.setRzhBelongDate(tDxTaxCurrent.getCurrentTaxPeriod());
                    record.setAuthStatus("2");
                    record.setConfirmDate(DateUtils.getSqlDate());
                    record.setConfirmUser(userInfo.getUsername());
                    record.setRzhType(dto.getRzhType());
                    record.setCxbdkDate(null);
                    record.setCxbdkStatus("0");
                    record.setYxTaxAmount(record.getTaxAmount());
                    //不抵扣类型
                    record.setBdkMessage(dto.getBdkMessage());
                    //不抵扣原因
                    if(StringUtils.isNotBlank(dto.getBdkYy())){
                        record.setBdkYy(dto.getBdkYy());
                    }
                    records.add(record);
                    customsLog.setCustomsCode(record.getCustomsCode());
                    customsLog.setType("5");
                    customsLog.setCreateDate(new Date());
                    customsLog.setInputName(userInfo.getLoginname());
                    customsLog.setInputStatus("7");
                    logList.add(customsLog);
                }
            }
        }
        // 3.更新数据
        DynamicContextHolder
            .push(StringUtils.defaultString(userInfo.getDbName(), "business") + DbConstant.BUSINESS_WRITE);

        for (TdxCustomsRecord record : records) {
            total++;
            hjse = hjse.add(new BigDecimal(record.getTaxAmount()));
            this.baseMapper.updateById(record);
        }

        if (flag) {
            message = sb.toString();
        }
        if (flag1) {
            message = message + sb1.toString() + "如需勾选认证，请到企业税务信息菜单解除锁定";
        }
        if (flag2) {
            message = message + sb2.toString();
        }
        if (flag3) {
            message = message + sb3.toString();
        }

        resultMap.put("total", total);
        resultMap.put("hjse",
            AmountFormatUtil.fmtMicrometer(hjse.setScale(2, BigDecimal.ROUND_HALF_UP).toPlainString()));
        resultMap.put("hjyxse",
            AmountFormatUtil.fmtMicrometer(hjyxse.setScale(2, BigDecimal.ROUND_HALF_UP).toPlainString()));
        resultMap.put("message", message);
        if (logList.size() > 0) {
            customsLogService.toCustomsLog(logList);
        }
        return Base64.encode(JSONObject.toJSONString(resultMap));
    }

    @Override
    public String importCert(Map<String, Object> pramsMap, UserInfo userInfo) throws Exception {

        JSONObject json =
            tabulateRedisService.getTabulateMenu(pramsMap.get("company").toString(), pramsMap.get("menuId").toString());

        DynamicContextHolder.push(pramsMap.get("dbName") + DbConstant.BUSINESS_READ);
        // 需要设置返回列表标题时调用

        List<Object> returnList = new ArrayList<>();
        String datalist = (String)pramsMap.get("datalist");
        List<String> list = (List<String>)pramsMap.get("gfsh");
        // 首先把字符串转成 JSONArray 对象
        JSONArray jsonArray = JSONArray.parseArray(datalist);
        // 总数量
        int cgsl = 0;
        // 有问题的数量
        int sbsl = 0;
        long hjsl = 0;
        BigDecimal hjse = new BigDecimal(0);
        BigDecimal hjyxse = new BigDecimal(0);
        List<String> uuidList = new ArrayList<>();
        List<String> dkList = new ArrayList<>();
        for (int i = 0; i < jsonArray.size(); i++) {
            hjsl++;
            JSONObject jsonObject = jsonArray.getJSONObject(i);
            String jkdwsh = jsonObject.getString("gfTaxNo");
            String jkshm = jsonObject.getString("customsCode");
            String tfrq = jsonObject.getString("fillInDate");
            String skje = jsonObject.getString("taxAmount");
//            String yxskje = jsonObject.getString("yxTaxAmount");
            CustomsRecordImportVO rzlb = new CustomsRecordImportVO();
            rzlb.setSelect(true);
            rzlb.setXh(String.valueOf(i + 1));
            rzlb.setBkrzts("");
            rzlb.setHgjkshm(jkshm);
            rzlb.setJkdwsh(jkdwsh);
            rzlb.setTfrq(tfrq);
            rzlb.setSkje(skje);
//            rzlb.setDkskje(yxskje);

            if (!(StringUtils.isNotBlank(jkdwsh))) {
                rzlb.setBkrzts("缴款书单位税号不能为空");
                rzlb.setSelect(false);
                returnList.add(rzlb);
                sbsl = sbsl + 1;
                continue;
            }

            if (StringUtils.isBlank(jkshm) || ObjectUtil.isExistLowwer(jkshm)) {
                rzlb.setBkrzts("缴款书号码不能为空或格式错误");
                rzlb.setSelect(false);
                returnList.add(rzlb);
                sbsl = sbsl + 1;
                continue;
            }

            if (StringUtils.isBlank(tfrq)) {
                rzlb.setBkrzts("填发日期不能为空");
                rzlb.setSelect(false);
                returnList.add(rzlb);
                sbsl = sbsl + 1;
                continue;
            }

            SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
            try {
                sdf.parse(tfrq);
            } catch (Exception e1) {
                rzlb.setBkrzts("填发日期格式错误");
                rzlb.setSelect(false);
                returnList.add(rzlb);
                sbsl = sbsl + 1;
                continue;
            }

            if (StringUtils.isBlank(skje)) {
                rzlb.setBkrzts("税款金额不能为空");
                rzlb.setSelect(false);
                returnList.add(rzlb);
                sbsl = sbsl + 1;
                continue;
            } else {
                Matcher match = PATTERN_AMOUNT.matcher(skje);
                if (!match.matches()) {
                    rzlb.setBkrzts("税款金额格式错误");
                    rzlb.setSelect(false);
                    returnList.add(rzlb);
                    sbsl = sbsl + 1;
                    continue;
                }

                if (Double.parseDouble(skje) <= 0) {
                    rzlb.setBkrzts("税款金额不能小于0");
                    rzlb.setSelect(false);
                    returnList.add(rzlb);
                    sbsl = sbsl + 1;
                    continue;
                }

                skje = MathUtil.round(skje);
                rzlb.setSkje(AmountFormatUtil.fmtMicrometer(skje));
            }

//            if (StringUtils.isBlank(yxskje)) {
//                rzlb.setBkrzts("抵扣税款金额不能为空");
//                rzlb.setSelect(false);
//                returnList.add(rzlb);
//                sbsl = sbsl + 1;
//                continue;
//            } else {
//                Matcher match = PATTERN_AMOUNT.matcher(yxskje);
//                if (!match.matches()) {
//                    rzlb.setBkrzts("抵扣税款金额格式错误");
//                    rzlb.setSelect(false);
//                    returnList.add(rzlb);
//                    sbsl = sbsl + 1;
//                    continue;
//                }
//                if (Double.parseDouble(yxskje) <= 0) {
//                    rzlb.setBkrzts("抵扣税款金额不能小于0");
//                    rzlb.setSelect(false);
//                    returnList.add(rzlb);
//                    sbsl = sbsl + 1;
//                    continue;
//                }
//                yxskje = MathUtil.round(yxskje);
//                rzlb.setDkskje(AmountFormatUtil.fmtMicrometer(yxskje));
//            }


            if (uuidList.contains(jkshm)) {
                rzlb.setBkrzts("重复缴款书");
                rzlb.setSelect(false);
                returnList.add(rzlb);
                sbsl = sbsl + 1;
                continue;
            } else {
                uuidList.add(jkshm);
            }
            String yxskje = "";
            QueryWrapper<TdxCustomsRecord> queryRecord = new QueryWrapper<>();
            queryRecord.eq("customs_code", jkshm);
            TdxCustomsRecord customRecord = this.baseMapper.selectOne(queryRecord);

            if (customRecord != null) {
                if (list == null || list.size() == 0 || !list.contains(customRecord.getGfTaxNo())) {
                    rzlb.setBkrzts("无该缴款书购方税号权限");
                    rzlb.setSelect(false);
                    returnList.add(rzlb);
                    sbsl = sbsl + 1;
                    continue;
                }

                if (StringUtils.isNotBlank(customRecord.getYxTaxAmount())) {
//                    if (Double.parseDouble(yxskje) <= 0) {
//                        rzlb.setBkrzts("抵扣税款金额不能小于0");
//                        rzlb.setSelect(false);
//                        returnList.add(rzlb);
//                        sbsl = sbsl + 1;
//                        continue;
//                    }
//                    if (Double.parseDouble(yxskje) > Double.parseDouble(skje)) {
//                        rzlb.setBkrzts("抵扣税额应小于等于税额");
//                        rzlb.setSelect(false);
//                        returnList.add(rzlb);
//                        sbsl = sbsl + 1;
//                        continue;
//                    }
                    yxskje = customRecord.getYxTaxAmount();
                    rzlb.setDkskje(AmountFormatUtil.fmtMicrometer(yxskje));
                }

                rzlb.setJkdwmc(customRecord.getGfName());
                rzlb.setId(customRecord.getId());
                // rzlb.setLy(DataSourceEnum.getVal(ObjectUtil.fixNull(customRecord.getDataSources())));
                rzlb.setLy(dictdetaServcice.queryDictName(DictConstant.JKSCJFS, customRecord.getDataSources()));
                // rzlb.setQszt(QSignStatusEnum.getVal(ObjectUtil.fixNull(customRecord.getQsStatus())));
                rzlb.setQszt(dictdetaServcice.queryDictName(DictConstant.QS_STATUS, customRecord.getQsStatus()));
                rzlb.setQsrq(customRecord.getQsDate() == null ? "" : sdf.format(customRecord.getQsDate()));
                // rzlb.setQsfs(QsTypeEnum.getVal(ObjectUtil.fixNull(customRecord.getQsType())));
                rzlb.setQsfs(dictdetaServcice.queryDictName(DictConstant.QS_TYPE, customRecord.getQsType()));
                rzlb.setQsr(customRecord.getQsUser());
                if (!"0".equals(customRecord.getDataSources())) {
                    rzlb.setBkrzts("该缴款书信息为录入信息，请等待采集结果");
                    rzlb.setSelect(false);
                    returnList.add(rzlb);
                    sbsl = sbsl + 1;
                    continue;
                }
                List<TdxTaxCurrent> tDxTaxCurrents = customsTaxCurrentService.list(
                    Wrappers.<TdxTaxCurrent>query().lambda().eq(TdxTaxCurrent::getTaxno, customRecord.getGfTaxNo()));
                TdxTaxCurrent current = tDxTaxCurrents.get(0);

                if (current == null || StringUtils.isBlank(current.getCurrentTaxPeriod())) {
                    rzlb.setBkrzts("无该缴款书税号属期信息");
                    rzlb.setSelect(false);
                    returnList.add(rzlb);
                    sbsl = sbsl + 1;
                    continue;
                }

                Map<String, Object> curMap = new HashMap<>(16);
                List<TdxDkCount> tDxDkCounts = customsDkCountService
                    .list(Wrappers.<TdxDkCount>query().lambda().eq(TdxDkCount::getTaxno, customRecord.getGfTaxNo())
                        .eq(TdxDkCount::getSkssq, current.getCurrentTaxPeriod()));
                TdxDkCount cur = tDxDkCounts.get(0);

                String skssq = cur.getSkssq();
                String year = skssq.substring(0, 4);
                String mon = skssq.substring(4);
                LocalDate localDate = LocalDate.of(Integer.parseInt(year), Integer.parseInt(mon), 01);
                LocalDate localDate1 = localDate.plusMonths(1);
                LocalDate localDate2 = LocalDate.now();
                if (!"0".equals(cur.getTjStatus()) && !"3".equals(cur.getTjStatus())) {
                    rzlb.setBkrzts("该缴款书的购方税号已申请抵扣统计");
                    rzlb.setSelect(false);
                    returnList.add(rzlb);
                    sbsl = sbsl + 1;
                    continue;
                }
                SimpleDateFormat sdf2 = new SimpleDateFormat("yyyy-MM-dd");
                String fillInDate = sdf2.format(customRecord.getFillInDate());
                if (!tfrq.equals(fillInDate)) {
                    rzlb.setBkrzts("填发日期与底账数据不一致");
                    rzlb.setSelect(false);
                    returnList.add(rzlb);
                    sbsl = sbsl + 1;
                    continue;
                }

                if (Double.parseDouble(customRecord.getTaxAmount()) != Double.parseDouble(skje)) {
                    rzlb.setBkrzts("税款金额与底账数据不一致");
                    rzlb.setSelect(false);
                    returnList.add(rzlb);
                    sbsl = sbsl + 1;
                    continue;
                }

                if (!jkdwsh.equals(customRecord.getGfTaxNo())) {
                    rzlb.setBkrzts("缴款单位税号与底账数据不一致");
                    rzlb.setSelect(false);
                    returnList.add(rzlb);
                    sbsl = sbsl + 1;
                    continue;
                }

                rzlb.setDqskssq(current.getCurrentTaxPeriod());

                if ("1".equals(customRecord.getRzhYesorno())) {
                    rzlb.setBkrzts("该缴款书已认证抵扣");
                    rzlb.setSelect(false);
                    returnList.add(rzlb);
                    sbsl = sbsl + 1;
                    continue;
                }

                if (!"17".equals(customRecord.getDataType())) {
                    rzlb.setBkrzts("该缴款书不满足数据类型为海关缴款书");
                    rzlb.setSelect(false);
                    returnList.add(rzlb);
                    sbsl = sbsl + 1;
                    continue;
                }

                if (!"0".equals(customRecord.getExportMark())) {
                    rzlb.setBkrzts("该缴款书不满足出口转内销标识为否");
                    rzlb.setSelect(false);
                    returnList.add(rzlb);
                    sbsl = sbsl + 1;
                    continue;
                }

                if (!"1".equals(customRecord.getQsStatus()) && "1".equals(userInfo.getQsStatus())) {
                    rzlb.setBkrzts("该缴款书不满足签收状态为已签收");
                    rzlb.setSelect(false);
                    returnList.add(rzlb);
                    sbsl = sbsl + 1;
                    continue;
                }

                if (!"0".equals(customRecord.getAuthStatus()) && !"5".equals(customRecord.getAuthStatus())
                    && !"6".equals(customRecord.getAuthStatus())) {
                    if ("1".equals(customRecord.getBdkStatus())) {
                        rzlb.setBkrzts("已提交认证抵扣");
                        rzlb.setSelect(false);
                        returnList.add(rzlb);
                        sbsl = sbsl + 1;
                        continue;
                    } else if ("2".equals(customRecord.getBdkStatus())) {
                        rzlb.setBkrzts("已提交认证不抵扣");
                        rzlb.setSelect(false);
                        returnList.add(rzlb);
                        sbsl = sbsl + 1;
                        continue;
                    } else if ("3".equals(customRecord.getBdkStatus())) {
                        rzlb.setBkrzts("已提交退税认证");
                        rzlb.setSelect(false);
                        returnList.add(rzlb);
                        sbsl = sbsl + 1;
                        continue;
                    }
                }

                if (!"0".equals(customRecord.getGlzt())) {
                    rzlb.setBkrzts("该缴款书不满足管理状态为正常");
                    rzlb.setSelect(false);
                    returnList.add(rzlb);
                    sbsl = sbsl + 1;
                    continue;
                }

                SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");
                if (!DateUtil.isInTwoDate(current.getSelectStartDate(), current.getSelectEndDate(),
                    simpleDateFormat.format(customRecord.getFillInDate()))) {
                    rzlb.setBkrzts("该缴款书不满足填发日期在符合勾选范围内");
                    rzlb.setSelect(false);
                    returnList.add(rzlb);
                    sbsl = sbsl + 1;
                    continue;
                }

                // 判断抵扣限额
                if (limit && current.getLimitDkse() != null && (current.getLimitDkse().compareTo(BigDecimal.ZERO) != 0)) {
                    if (!dkList.contains(customRecord.getGfTaxNo())) {

                        Map<String, Object> paramMap = new HashMap<>(16);
                        paramMap.put("gfsh", customRecord.getGfTaxNo());
                        paramMap.put("skssq", current.getCurrentTaxPeriod());
                        TdxCustomsRecord dkInvoice = super.baseMapper.selectDkse(paramMap);
                        String dkTaxAmount = "0";
                        if (dkInvoice != null && dkInvoice.getYxTaxAmount() != null) {
                            dkTaxAmount = dkInvoice.getYxTaxAmount();
                        }
                        String dkse = MathUtil.add(dkTaxAmount, yxskje);
                        if (new BigDecimal(yxskje).compareTo(current.getLimitDkse()) > 0) {
                            dkList.add(customRecord.getGfTaxNo());

                            rzlb.setBkrzts("该缴款书税号本月缴款书可认证总税额超限");
                            rzlb.setSelect(false);
                            returnList.add(rzlb);
                            sbsl = sbsl + 1;
                            continue;
                        }

                    }
                }

                // 采集数据
                if ("0".equals(customRecord.getDataSources())) {
                    if (!"0".equals(customRecord.getEntryDiscrepancy())) {
                        rzlb.setBkrzts("该缴款书不满足采集数据未录入不符项");
                        rzlb.setSelect(false);
                        returnList.add(rzlb);
                        sbsl = sbsl + 1;
                        continue;
                    }
                } else {// 录入数据
                    if ("0".equals(customRecord.getApplyCheck()) && "1".equals(customRecord.getAuditStatus())) {
                    } else if ("1".equals(customRecord.getApplyCheck()) && "0".equals(customRecord.getCheckStatus())) {
                    } else {
                        rzlb.setBkrzts("缴款书状态异常");
                        rzlb.setSelect(false);
                        returnList.add(rzlb);
                        sbsl = sbsl + 1;
                        continue;
                    }
                }

                if (current.getSelectStartDate() != null && customRecord.getFillInDate() != null) {
                    if (customRecord.getFillInDate()
                        .compareTo(DateUtils.strToDatePeriod(current.getSelectStartDate())) < 0) {
                        rzlb.setBkrzts("缴款书逾期");
                        rzlb.setSelect(false);
                        returnList.add(rzlb);
                        sbsl = sbsl + 1;
                        continue;
                    }
                }
                if (StringUtils.isNotBlank(current.getSelectEndDate())) {
                    if (customRecord.getFillInDate()
                        .compareTo(DateUtils.strToDatePeriod(current.getSelectEndDate())) > 0) {
                        rzlb.setBkrzts("缴款书未到期");
                        rzlb.setSelect(false);
                        returnList.add(rzlb);
                        sbsl = sbsl + 1;
                        continue;
                    }
                }

                if ("1".equals(current.getLatchStatus())) {
                    rzlb.setBkrzts("该缴款书的购方税号已锁定");
                    rzlb.setSelect(false);
                    returnList.add(rzlb);
                    sbsl = sbsl + 1;
                    continue;
                }

                returnList.add(rzlb);
                cgsl = cgsl + 1;
            } else {
                rzlb.setBkrzts("无底账信息");
                rzlb.setSelect(false);
                returnList.add(rzlb);
                sbsl = sbsl + 1;
                continue;
            }

            hjse = hjse.add(BigDecimal.valueOf(Double.parseDouble(skje)));
            if(StringUtils.isNotBlank(yxskje)){
                hjyxse = hjyxse.add(BigDecimal.valueOf(Double.parseDouble(yxskje)));
            }
        }
        json.put("cgsl", cgsl);
        json.put("sbsl", sbsl);
        json.put("hjsl", hjsl);
        json.put("hjse", hjse);
        json.put("hjyxse", hjyxse);
        json.put("datalist", returnList);

        return super.responseResult(json);
    }

    @Override
    public String queryBdkByPage(Page page, TdxCustomsRecordModelDTO dto, UserInfo userInfo) throws Exception {
        DynamicContextHolder
            .push(StringUtils.defaultString(userInfo.getDbName(), "business") + DbConstant.BUSINESS_READ);
        dto.setParam(page, userInfo);

        ResultData<Object> resultData = new ResultData<>();
        JSONObject tabulateMenu = tabulateRedisService.getTabulateMenu(userInfo.getCompany(), dto.getMenuId());
        resultData.putTabulateMenu(tabulateMenu);

        Map mapTj = null;
        Page<Map<String, Object>> result = null;
        if (StringUtils.isNotBlank(dto.getGfsh()) || dto.getGfTaxNo().size() > 0) {
            // 合计信息,仅第一页时需要统计，减少数据库压力
            if (dto.getCurr() == 1) {
                mapTj = this.baseMapper.queryBdkBySum(dto);
            }
            result = this.baseMapper.queryBdkByPage(page, dto);
            if (ResultData.isEmpty(result)) {

                SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
                for (int i = 0; i < page.getRecords().size(); i++) {
                    Map<String, Object> map = result.getRecords().get(i);

                    String authStatus = (String)map.get("authStatus");
                    String cxbdkStatus = (String)map.get("cxbdkStatus");
                    String qsStatus = (String)map.get("qsStatus");
                    String dataSources = (String)map.get("dataSources");
                    String bdkStatus = (String)map.get("bdkStatus");
                    String bdkMessage = (String)map.get("bdkMessage");
                    map.put("id", String.valueOf(map.get("id")));
                    map.put("xh", String
                        .valueOf((((page.getCurrent() == 0 ? 1 : page.getCurrent()) - 1) * page.getSize()) + i + 1));
                    if (map.get("fillInDate") != null) {
                        map.put("fillInDate", map.get("fillInDate"));
                    }
                    if (map.get("qsDate") != null) {
                        map.put("qsDate", map.get("qsDate"));
                    }

                    // 字典转换
                    map.put("dataSources", dictdetaServcice.queryDictName(DictConstant.JKSCJFS, dataSources));
                    map.put("qsStatus", dictdetaServcice.queryDictName(DictConstant.QS_STATUS, qsStatus));
                    String qsType = (String)map.get("qsType");
                    map.put("qsType", dictdetaServcice.queryDictName(DictConstant.QS_TYPE, qsType));
                    map.put("authStatus", dictdetaServcice.queryDictName(DictConstant.BDK_AUTH_STATUS, authStatus));
                    map.put("cxbdkStatus", dictdetaServcice.queryDictName(DictConstant.BDK_UNDO_STATUS, cxbdkStatus));

                    map.put("bdkMessage", dictdetaServcice.queryDictName(DictConstant.bdkMessage, bdkMessage));

                    map.put("taxAmount", AmountFormatUtil.fmtMicrometer(MathUtil.round((String)map.get("taxAmount"))));
                    map.put("yxTaxAmount",
                        AmountFormatUtil.fmtMicrometer(MathUtil.round((String)map.get("yxTaxAmount"))));

                    map.put("select", true);

                    TdxDkCount tDxDkCount = null;
                    TdxTaxCurrent tDxTaxCurrent = null;
                    List<TdxTaxCurrent> tDxTaxCurrents = customsTaxCurrentService
                        .list(Wrappers.<TdxTaxCurrent>query().lambda().eq(TdxTaxCurrent::getTaxno, map.get("gfTaxNo")));

                    if (tDxTaxCurrents == null || tDxTaxCurrents.size() == 0) {
                        map.put("select", false);
                        continue;
                    }
                    tDxTaxCurrent = tDxTaxCurrents.get(0);
                    List<TdxDkCount> tDxDkCounts = customsDkCountService
                        .list(Wrappers.<TdxDkCount>query().lambda().eq(TdxDkCount::getTaxno, map.get("gfTaxNo"))
                            .eq(TdxDkCount::getSkssq, tDxTaxCurrent.getCurrentTaxPeriod()));

                    if (tDxDkCounts == null || tDxDkCounts.size() == 0) {
                        map.put("select", false);
                        continue;
                    }

                    tDxDkCount = tDxDkCounts.get(0);

                    if (!"0".equals(map.get("glzt"))) {
                        map.put("select", false);
                        continue;
                    }

                    String applyCheck = (String)map.get("applyCheck");
                    String checkStatus = (String)map.get("checkStatus");
                    String auditStatus = (String)map.get("auditStatus");
                    // 采集数据
                    if ("0".equals(dataSources)) {
                        if (!"0".equals(map.get("entryDiscrepancy"))) {
                            map.put("select", false);
                            continue;
                        }
                    } else {
                        // 录入数据
                        if ("0".equals(applyCheck) && "1".equals(auditStatus)) {

                        } else if ("1".equals(applyCheck) && "0".equals(checkStatus)) {

                        } else {
                            map.put("select", false);
                            continue;
                        }
                    }

                    if ("0".equals(dto.getSfDkStatus())) {
                        // 认证不抵扣
                        if (!"0".equals(authStatus) && !"4".equals(authStatus) && !"5".equals(authStatus)
                            && !"6".equals(authStatus)) {
                            map.put("select", false);
                            continue;
                        }
                    } else if ("4".equals(dto.getSfDkStatus())) {
                        // 页面中有撤销不抵扣按钮
                        String rzhBelongDate = (String)map.get("rzhBelongDate");
                        if (!"0".equals(cxbdkStatus) && !"4".equals(cxbdkStatus) && !"5".equals(cxbdkStatus)) {
                            map.put("select", false);
                            continue;
                        }
                        if (!"2".equals(bdkStatus)) {
                            map.put("select", false);
                            continue;
                        }
                        if (rzhBelongDate != null && !rzhBelongDate.equals(tDxTaxCurrent.getCurrentTaxPeriod())) {
                            map.put("select", false);
                            continue;
                        }
                    }

                    SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");
                    String fillInDate = (String)map.get("fillInDate");
                    if (!DateUtil.isInTwoDate(tDxTaxCurrent.getSelectStartDate(), tDxTaxCurrent.getSelectEndDate(),
                        fillInDate)) {
                        map.put("select", false);
                        continue;
                    }

                    if (!"0".equals(tDxDkCount.getTjStatus()) && !"3".equals(tDxDkCount.getTjStatus())) {
                        map.put("select", false);
                        continue;
                    }

                    if (!"1".equals(qsStatus) && "1".equals(userInfo.getQsStatus())) {
                        map.put("select", false);
                    }
                }
            }
        } else {
            resultData.setDataList(new ArrayList<>());
            return resultData.toJsonBase();
        }

        return resultData.getTjData(mapTj).getPageData(result).toJsonBase();
    }

    @Override
    public String approveCxrz(TdxCustomsRecordModelDTO dto, UserInfo userInfo) {

        Map resultMap = new HashMap(16);

        long total = 0;
        BigDecimal hjje = new BigDecimal(0);
        BigDecimal hjse = new BigDecimal(0);
        BigDecimal hjyxse = new BigDecimal(0);
        String message = "";

        boolean flag1 = false;
        boolean flag2 = false;
        boolean flag3 = false;

        List<String> sb1List = new ArrayList<>();
        List<String> sb2List = new ArrayList<>();

        StringBuilder sb1 = new StringBuilder();
        StringBuilder sb2 = new StringBuilder();
        StringBuilder sb3 = new StringBuilder();

        sb1.append("以下税号已被锁定不可勾选认证：税号 \n");
        sb2.append("以下税号已申请抵扣统计，请撤销统计后重新操作：税号\n ");
        sb3.append("以下缴款书的税款所属期不是当前属期：发票\n ");

        // 1.查询数据
        DynamicContextHolder
            .push(StringUtils.defaultString(userInfo.getDbName(), "business") + DbConstant.BUSINESS_READ);
        List<TdxCustomsRecord> records = new ArrayList<>();
        // 2.校验
        List<CustomsLog> logList = new ArrayList<>();
        for (String idstr : dto.getIds()) {

            Long id = Long.parseLong(idstr);

            TdxCustomsRecord record = this.baseMapper.selectById(id);
            if (record != null) {

                TdxTaxCurrent tDxTaxCurrent;
                TdxDkCount tDxDkCount;
                List<TdxTaxCurrent> tDxTaxCurrents = customsTaxCurrentService
                    .list(Wrappers.<TdxTaxCurrent>query().lambda().eq(TdxTaxCurrent::getTaxno, record.getGfTaxNo()));

                tDxTaxCurrent = tDxTaxCurrents.get(0);

                List<TdxDkCount> tDxDkCounts = customsDkCountService
                    .list(Wrappers.<TdxDkCount>query().lambda().eq(TdxDkCount::getTaxno, record.getGfTaxNo())
                        .eq(TdxDkCount::getSkssq, tDxTaxCurrent.getCurrentTaxPeriod()));

                tDxDkCount = tDxDkCounts.get(0);

                if ("1".equals(tDxTaxCurrent.getLatchStatus())) {
                    if (!sb1List.contains(record.getGfTaxNo())) {
                        sb1.append(record.getGfTaxNo()).append(" \n ");
                        sb1List.add(record.getGfTaxNo());
                    }
                    flag1 = true;
                    continue;
                }

                if (!"0".equals(tDxDkCount.getTjStatus()) && !"3".equals(tDxDkCount.getTjStatus())) {
                    if (!sb2List.contains(record.getGfTaxNo())) {
                        sb2.append(record.getGfTaxNo()).append(" \n ");
                        sb2List.add(record.getGfTaxNo());
                    }
                    flag2 = true;
                    continue;
                }

                if (!record.getRzhBelongDate().equals(tDxTaxCurrent.getCurrentTaxPeriod())) {
                    sb3.append(record.getGfTaxNo()).append(" \n ");
                    flag3 = true;
                    continue;
                }
                CustomsLog customsLog = new CustomsLog();
                // 撤销抵扣
                if ("1".equals(record.getBdkStatus())) {
                    record.setRzlx("5");
                    record.setCxrzCurrentPeriod(tDxTaxCurrent.getCurrentTaxPeriod());
                    record.setCxrzDate(new Date());
                    record.setCxrzStatus("1");
                    customsLog.setCustomsCode(record.getCustomsCode());
                    customsLog.setType("2");
                    customsLog.setCreateDate(new Date());
                    customsLog.setInputName(userInfo.getLoginname());
                    customsLog.setInputStatus("15");
                    logList.add(customsLog);
                } else if ("2".equals(record.getBdkStatus())) {
                    // 撤销不抵扣
                    record.setBdkCurrentPeriod(tDxTaxCurrent.getCurrentTaxPeriod());
                    record.setRzlx("6");
                    record.setCxbdkDate(new Date());
                    record.setCxbdkStatus("1");
                    customsLog.setCustomsCode(record.getCustomsCode());
                    customsLog.setType("5");
                    customsLog.setCreateDate(new Date());
                    customsLog.setInputName(userInfo.getLoginname());
                    customsLog.setInputStatus("10");
                    logList.add(customsLog);
                }
                records.add(record);
            }
        }
        // 3.更新数据
        DynamicContextHolder
            .push(StringUtils.defaultString(userInfo.getDbName(), "business") + DbConstant.BUSINESS_WRITE);
        for (TdxCustomsRecord record : records) {
            total++;
            hjse = hjse.add(new BigDecimal(record.getTaxAmount()));
            hjyxse = hjyxse.add(new BigDecimal(record.getYxTaxAmount()));
            this.baseMapper.updateById(record);
        }
        if (flag1) {
            message = message + sb1.toString() + "如需勾选认证，请到企业税务信息菜单解除锁定";
        }
        if (flag2) {
            message = message + sb2.toString();
        }
        if (flag3) {
            message = message + sb3.toString();
        }
        resultMap.put("total", total);
        resultMap.put("hjse", hjse);
        resultMap.put("message", message);
        resultMap.put("hjje", hjje);
        resultMap.put("hjdk", hjyxse);
        if (logList.size() > 0) {
            customsLogService.toCustomsLog(logList);
        }
        return Base64.encode(JSONObject.toJSONString(resultMap));
    }

    @Override
    public String queryRzcxByPage(Page page, TdxCustomsRecordModelDTO dto, UserInfo userInfo) throws Exception {
        DynamicContextHolder
            .push(StringUtils.defaultString(userInfo.getDbName(), "business") + DbConstant.BUSINESS_READ);
        dto.setParam(page, userInfo);
        List<Business> businesses = userInfo.getBusiness();


        ResultData<Object> resultData = new ResultData<>();
        JSONObject tabulateMenu = tabulateRedisService.getTabulateMenu(userInfo.getCompany(), dto.getMenuId());
        resultData.putTabulateMenu(tabulateMenu);

        if (dto.getAccountPeriod() != null && !"".equals(dto.getAccountPeriod())) {
            String accountPeriod = dto.getAccountPeriod().substring(0,7);
            dto.setAccountPeriod(accountPeriod);
            log.info("accountPeriod:{}",accountPeriod);
        }
        Map mapTj = null;
        Page<Map<String, Object>> result = null;
        if (StringUtils.isNotBlank(dto.getGfsh()) || dto.getGfTaxNo().size() > 0) {
            dto.setBusiness(businesses);
            if(StringUtils.isNotBlank(dto.getConfirmUser())){
                List<String> confirmUserList = Arrays.asList(dto.getConfirmUser().split(","));
                dto.setConfirmUserList(confirmUserList);
            }
            // 合计信息,仅第一页时需要统计，减少数据库压力
            if (dto.getCurr() == 1) {
                mapTj = this.baseMapper.queryRzcxBySum(dto);
            }
            result = this.baseMapper.queryRzcxByPage(page, dto);
            if (ResultData.isEmpty(result)) {
                SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
                for (int i = 0; i < page.getRecords().size(); i++) {
                    Map<String, Object> map = result.getRecords().get(i);

                    String authStatus = (String)map.get("authStatus");
                    String cxrzStatus = (String)map.get("cxrzStatus");
                    String bdkStatus = (String)map.get("bdkStatus");
                    String rzhYesorno = (String)map.get("rzhYesorno");

                    map.put("xh", String
                        .valueOf((((page.getCurrent() == 0 ? 1 : page.getCurrent()) - 1) * page.getSize()) + i + 1));
                    if (map.get("fillInDate") != null) {
                        map.put("fillInDate", map.get("fillInDate"));
                    }
                    // 字典值转换
                    String dataSources = (String)map.get("dataSources");
                    map.put("dataSources", dictdetaServcice.queryDictName(DictConstant.JKSCJFS, dataSources));
                    String qsStatus = (String)map.get("qsStatus");
                    map.put("qsStatus", dictdetaServcice.queryDictName(DictConstant.QS_STATUS, qsStatus));
                    String qsType = (String)map.get("qsType");
                    map.put("qsType", dictdetaServcice.queryDictName(DictConstant.QS_TYPE, qsType));
                    String glzt = (String)map.get("glzt");
                    map.put("glzt", dictdetaServcice.queryDictName(DictConstant.GLZT, glzt));
                    // 认证状态
                    map.put("rzhYesorno", dictdetaServcice.queryDictName(DictConstant.DEDU_STATUS, rzhYesorno));
                    // 认证处理状态
                    map.put("authStatus", dictdetaServcice.queryDictName(DictConstant.AUTH_STATUS_ALL, authStatus));
                    // 不抵扣状态
                    map.put("bdkStatus", dictdetaServcice.queryDictName(DictConstant.BDK_STATUS, bdkStatus));
                    map.put("cxrzStatus", dictdetaServcice.queryDictName(DictConstant.CXRZ_STATUS, cxrzStatus));
                    //入账状态
                    if ("1".equals(map.get("inAccountStatus"))){
                        map.put("inAccountStatus","已入账");
                    }else if ("0".equals(map.get("inAccountStatus"))) {
                        map.put("inAccountStatus","未入账");
                    }

                    if (map.get("qsDate") != null) {
                        map.put("qsDate", map.get("qsDate"));
                    }
                    if (map.get("rzhDate") != null) {
                        map.put("rzhDate", map.get("rzhDate"));
                    }

                    map.put("taxAmount", AmountFormatUtil.fmtMicrometer(MathUtil.round((String)map.get("taxAmount"))));
                    map.put("yxTaxAmount",
                        AmountFormatUtil.fmtMicrometer(MathUtil.round((String)map.get("yxTaxAmount"))));
                    map.put("select", true);

                    TdxDkCount tDxDkCount = null;
                    TdxTaxCurrent tDxTaxCurrent = null;
                    List<TdxTaxCurrent> tDxTaxCurrents = customsTaxCurrentService
                        .list(Wrappers.<TdxTaxCurrent>query().lambda().eq(TdxTaxCurrent::getTaxno, map.get("gfTaxNo")));

                    if (tDxTaxCurrents == null || tDxTaxCurrents.size() == 0) {
                        map.put("select", false);
                        continue;
                    }
                    tDxTaxCurrent = tDxTaxCurrents.get(0);
                    List<TdxDkCount> tDxDkCounts = customsDkCountService
                        .list(Wrappers.<TdxDkCount>query().lambda().eq(TdxDkCount::getTaxno, map.get("gfTaxNo"))
                            .eq(TdxDkCount::getSkssq, tDxTaxCurrent.getCurrentTaxPeriod()));

                    if (tDxDkCounts == null || tDxDkCounts.size() == 0) {
                        map.put("select", false);
                        continue;
                    }

                    tDxDkCount = tDxDkCounts.get(0);

                    if (!"0".equals(tDxDkCount.getTjStatus()) && !"3".equals(tDxDkCount.getTjStatus())) {
                        map.put("select", false);
                        continue;
                    }

                    if (!"1".equals(bdkStatus)) {
                        map.put("select", false);
                        continue;
                    }
                    if (!"1".equals(rzhYesorno)) {
                        map.put("select", false);
                        continue;
                    }
                    if (!map.get("rzhBelongDate").equals(tDxTaxCurrent.getCurrentTaxPeriod())) {
                        map.put("select", false);
                        continue;
                    }
                    if (!"0".equals(cxrzStatus) && !"5".equals(cxrzStatus)) {
                        map.put("select", false);
                    }
                }
            }
        } else {
            resultData.setDataList(new ArrayList<>());
            return resultData.toJsonBase();
        }

        return resultData.getTjData(mapTj).getPageData(result).toJsonBase();
    }

    private String getRzzt(String rzhYesorno) {
        String rzzt = "";
        if (RzztEnum.WRZ.getRzztDm().equals(rzhYesorno)) {
            rzzt = RzztEnum.WRZ.getRzztMc(RzztEnum.WRZ.getIndex());
        } else if (RzztEnum.YRZ.getRzztDm().equals(rzhYesorno)) {
            rzzt = RzztEnum.YRZ.getRzztMc(RzztEnum.YRZ.getIndex());
        } else if (RzztEnum.YRZBDK.getRzztDm().equals(rzhYesorno)) {
            rzzt = RzztEnum.YRZBDK.getRzztMc(RzztEnum.YRZBDK.getIndex());
        } else if (RzztEnum.TSRZ.getRzztDm().equals(rzhYesorno)) {
            rzzt = RzztEnum.TSRZ.getRzztMc(RzztEnum.TSRZ.getIndex());
        }
        return rzzt;
    }
}
