package com.dxhy.erp.service.impl;

import cn.hutool.core.codec.Base64;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.dxhy.common.constant.DbConstant;
import com.dxhy.common.datasource.config.DynamicContextHolder;
import com.dxhy.common.enums.FplxEnum;
import com.dxhy.common.enums.ReturnCodeEnum;
import com.dxhy.common.service.impl.MpBaseServiceImpl;
import com.dxhy.common.util.DateOrderUtil;
import com.dxhy.common.util.StringHelper;
import com.dxhy.common.util.pojo.Authorize;
import com.dxhy.common.util.pojo.GlobalInfo;
import com.dxhy.erp.dao.*;
import com.dxhy.erp.entity.*;
import com.dxhy.erp.model.*;
import com.dxhy.erp.model.auth.InterfaceCode;
import com.dxhy.erp.service.InvoiceAuthService;
import com.dxhy.erp.service.TDxInvoiceReimburseService;
import com.dxhy.erp.utils.CheckUtils;
import com.dxhy.erp.utils.DateUtil;
import com.google.common.collect.Maps;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.StringUtils;
import org.joda.time.DateTime;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.math.BigDecimal;
import java.text.DecimalFormat;
import java.util.*;
import java.util.stream.Collectors;

/**
 * @author dxhy
 */
@SuppressWarnings({"AlibabaMethodTooLong", "AlibabaUndefineMagicConstant"})
@Service
@Slf4j
public class InvoiceAuthServiceImpl extends MpBaseServiceImpl<RecordInvoiceDao, RecordInvoice>
        implements InvoiceAuthService {

    @Resource
    AuthRecordDao authRecordDao;
    @Resource
    SysDeptDao sysDeptDao;
    @Resource
    TdxRecordInvoiceDao tDxrecordInvoiceDao;
    @Resource
    TDxInvoiceReimburseService tDxInvoiceReimburseService;
    @Resource
    TdxSaleRecordInvoiceDao tdxSaleRecordInvoiceDao;

    @Override
    public String applyInvoiceAuth(GlobalInfo globalInfo, Map<String, String> map, HttpServletRequest request,
                                   HttpServletResponse response, Authorize authorize) {
        ReturnStateInfo run = new ReturnStateInfo();
        InterfaceCode interfaceCode = new InterfaceCode();
        AuthDateReq data = null;
        try {
            data = JSONObject.parseObject(map.get("data"), AuthDateReq.class);
        } catch (Exception e) {
            run.setReturnCode(ReturnCodeEnum.QQBWGSCW.getFhzDm());
            run.setReturnMessage(ReturnCodeEnum.QQBWGSCW.getFhzMc(ReturnCodeEnum.QQBWGSCW.getIndex()));
            interfaceCode.setGlobalInfo(globalInfo);
            interfaceCode.setReturnStateInfo(run);
            return JSON.toJSONString(interfaceCode);
        }
        if (StringUtils.isBlank(data.getBatchNo()) || (!CheckUtils.stringLength(data.getBatchNo(), 32))) {
            run.setReturnCode(ReturnCodeEnum.PCHBNWK.getFhzDm());
            run.setReturnMessage(ReturnCodeEnum.PCHBNWK.getFhzMc(ReturnCodeEnum.PCHBNWK.getIndex()));
            interfaceCode.setGlobalInfo(globalInfo);
            interfaceCode.setReturnStateInfo(run);
            return JSON.toJSONString(interfaceCode);
        }
        if (StringUtils.isBlank(data.getApplyRzlx())) {
            run.setReturnCode(ReturnCodeEnum.RZLXBNWK.getFhzDm());
            run.setReturnMessage(ReturnCodeEnum.RZLXBNWK.getFhzMc(ReturnCodeEnum.RZLXBNWK.getIndex()));
            interfaceCode.setGlobalInfo(globalInfo);
            interfaceCode.setReturnStateInfo(run);
            return JSON.toJSONString(interfaceCode);
        }
        List<Map<String, Object>> authStateList = (List<Map<String, Object>>) data.getInvoices();
        run = getCheckData(authStateList, data.getContentRows());
        if (run.getReturnCode() != null || run.getReturnMessage() != null) {
            interfaceCode.setReturnStateInfo(run);
            interfaceCode.setGlobalInfo(globalInfo);
            return JSON.toJSONString(interfaceCode);
        }
        DynamicContextHolder.push(DbConstant.BASICS_READ);
        SysDeptEntity deptEntity = sysDeptDao.getOrg(data.getBuyerTaxNo());
        String dbName = null;
        if (deptEntity != null && deptEntity.getDbName() != null) {
            dbName = deptEntity.getDbName();
        } else {
            ReturnStateInfo runInfo = new ReturnStateInfo();
            runInfo.setReturnCode(ReturnCodeEnum.JTSHBF.getFhzDm());
            runInfo.setReturnMessage(ReturnCodeEnum.JTSHBF.getFhzMc(ReturnCodeEnum.JTSHBF.getIndex()));
            interfaceCode.setGlobalInfo(globalInfo);
            interfaceCode.setReturnStateInfo(runInfo);
            return JSON.toJSONString(interfaceCode);
        }

        DynamicContextHolder.push("business" + DbConstant.BUSINESS_READ);
        int resultNum = authRecordDao.selectCountRecord(data.getBatchNo());
        if (resultNum > 0) {
            ReturnStateInfo runInfo = new ReturnStateInfo();
            runInfo.setReturnCode("2099");
            runInfo.setReturnMessage("批次号已存在");
            interfaceCode.setGlobalInfo(globalInfo);
            interfaceCode.setReturnStateInfo(runInfo);
            return JSON.toJSONString(interfaceCode);
        }
        List<AuthRecord> authRecordList = bulidAuthRecord(authStateList, data.getBatchNo(), data.getApplyRzlx());
        // 申请认证记录保存
        DynamicContextHolder.push(dbName + DbConstant.BUSINESS_WRITE);
        authRecordDao.insertAuthRecord(authRecordList);
        if (StringUtils.isBlank(data.getApplyRzlx())) {
            data.setApplyRzlx("1");
        }
        for (Map<String, Object> authState : authStateList) {
            DynamicContextHolder.push(dbName + DbConstant.BUSINESS_READ);
            TdxRecordInvoice recordInvoice = tDxrecordInvoiceDao.getRecordInvoice((String) authState.get("buyerTaxNo"),
                    (String) authState.get("invoiceCode"), (String) authState.get("invoiceNo"));
            DynamicContextHolder.push(dbName + DbConstant.BUSINESS_WRITE);
            if (recordInvoice != null) {
                if ("01".equals(recordInvoice.getInvoiceType()) || "03".equals(recordInvoice.getInvoiceType())
                        || "08".equals(recordInvoice.getInvoiceType()) || "14".equals(recordInvoice.getInvoiceType())
                        || "31".equals(recordInvoice.getInvoiceType())) {
                    if ("5".equals(data.getApplyRzlx())) {
                        if (!"4".equals(recordInvoice.getCxrzStatus()) && !"2".equals(recordInvoice.getCxrzStatus())
                                && !"1".equals(recordInvoice.getCxrzStatus())) {
                            String invoiceCode = StringUtils.isBlank((String) authState.get("invoiceCode")) ? "" : (String) authState.get("invoiceCode");
                            tDxrecordInvoiceDao.updateCxrz((String) authState.get("applyTaxPeriod"),
                                    invoiceCode + (String) authState.get("invoiceNo"));
                        }
                    } else if ("6".equals(data.getApplyRzlx())) {
                        if (!"4".equals(recordInvoice.getCxbdkStatus()) && !"2".equals(recordInvoice.getCxbdkStatus())
                                && !"1".equals(recordInvoice.getCxrzStatus())) {
                            String invoiceCode = StringUtils.isBlank((String) authState.get("invoiceCode")) ? "" : (String) authState.get("invoiceCode");
                            tDxrecordInvoiceDao.updateCxbdk((String) authState.get("applyTaxPeriod"),
                                    invoiceCode + (String) authState.get("invoiceNo"));
                        }
                    } else {
                        if (!"4".equals(recordInvoice.getAuthStatus()) && !"3".equals(recordInvoice.getAuthStatus())
                                && !"2".equals(recordInvoice.getAuthStatus())) {
                            if ("1".equals(data.getApplyRzlx())) {
                                tDxrecordInvoiceDao.updateRz(authState, data.getApplyRzlx(), "1",
                                        recordInvoice.getUuid());
                            } else if ("4".equals(data.getApplyRzlx())) {
                                tDxrecordInvoiceDao.updateRz(authState, data.getApplyRzlx(), "2",
                                        recordInvoice.getUuid());
                            } else {
                                tDxrecordInvoiceDao.updateRz(authState, data.getApplyRzlx(), "99",
                                        recordInvoice.getUuid());
                            }
                        }
                    }
                }
            }
        }
        if (run.getReturnCode() == null) {
            run.setReturnCode(ReturnCodeEnum.CLCG.getFhzDm());
            run.setReturnMessage(ReturnCodeEnum.CLCG.getFhzMc(ReturnCodeEnum.CLCG.getIndex()));
        }
        interfaceCode.setReturnStateInfo(run);
        interfaceCode.setGlobalInfo(globalInfo);
        return JSON.toJSONString(interfaceCode);
    }

    public List<AuthRecord> bulidAuthRecord(List<Map<String, Object>> list, String batchNo, String rzlx) {
        List<AuthRecord> authRecordList = new ArrayList<>();
        for (Map<String, Object> map : list) {
            AuthRecord authRecord = new AuthRecord();
            authRecord.setBatchNo(batchNo);
            authRecord.setInvoiceCode((String) map.get("invoiceCode"));
            authRecord.setInvoiceNo((String) map.get("invoiceNo"));
            authRecord.setUuid(map.get("invoiceCode") + (String) map.get("invoiceNo"));
            if (StringUtils.isBlank(rzlx)) {
                authRecord.setRzlx("1");
            } else {
                authRecord.setRzlx(rzlx);
            }
            authRecord.setApplyTaxPeriod((String) map.get("applyTaxPeriod"));
            authRecord.setBuyerTaxNo((String) map.get("buyerTaxNo"));
            authRecord.setYxse((String) map.get("taxCredit"));
            authRecord.setCreateDate(new Date());
            authRecordList.add(authRecord);
        }
        return authRecordList;
    }

    @Override
    public String getAuthResult(GlobalInfo globalInfo, Map<String, String> map, HttpServletRequest request,
                                HttpServletResponse response, Authorize authorize) {
        ReturnStateInfo run = new ReturnStateInfo();
        InterfaceCode interfaceCode = new InterfaceCode();
        AuthDateReq data = null;
        try {
            data = JSONObject.parseObject(map.get("data"), AuthDateReq.class);
        } catch (Exception e) {
            run.setReturnCode(ReturnCodeEnum.QQBWGSCW.getFhzDm());
            run.setReturnMessage(ReturnCodeEnum.QQBWGSCW.getFhzMc(ReturnCodeEnum.QQBWGSCW.getIndex()));
            interfaceCode.setGlobalInfo(globalInfo);
            interfaceCode.setReturnStateInfo(run);
            return JSON.toJSONString(interfaceCode);
        }
        if (StringUtils.isBlank(data.getBatchNo()) || (!CheckUtils.stringLength(data.getBatchNo(), 32))) {
            run.setReturnCode(ReturnCodeEnum.PCHBNWK.getFhzDm());
            run.setReturnMessage(ReturnCodeEnum.PCHBNWK.getFhzMc(ReturnCodeEnum.PCHBNWK.getIndex()));
            interfaceCode.setGlobalInfo(globalInfo);
            interfaceCode.setReturnStateInfo(run);
            return JSON.toJSONString(interfaceCode);
        }
        if (StringUtils.isBlank(data.getBuyerTaxNo())) {
            run.setReturnCode(ReturnCodeEnum.SHBNWK.getFhzDm());
            run.setReturnMessage(ReturnCodeEnum.SHBNWK.getFhzMc(ReturnCodeEnum.SHBNWK.getIndex()));
            interfaceCode.setGlobalInfo(globalInfo);
            interfaceCode.setReturnStateInfo(run);
            return JSON.toJSONString(interfaceCode);
        }
        DynamicContextHolder.push(DbConstant.BASICS_READ);
        SysDeptEntity deptEntity = sysDeptDao.getOrg(data.getBuyerTaxNo());
        String dbName = null;
        if (deptEntity != null && deptEntity.getDbName() != null) {
            dbName = deptEntity.getDbName();
        } else {
            ReturnStateInfo runInfo = new ReturnStateInfo();
            runInfo.setReturnCode(ReturnCodeEnum.JTSHBF.getFhzDm());
            runInfo.setReturnMessage(ReturnCodeEnum.JTSHBF.getFhzMc(ReturnCodeEnum.JTSHBF.getIndex()));
            interfaceCode.setGlobalInfo(globalInfo);
            interfaceCode.setReturnStateInfo(runInfo);
            return JSON.toJSONString(interfaceCode);
        }
        DynamicContextHolder.push(dbName + DbConstant.BUSINESS_READ);
        int resultNum = authRecordDao.selectCountRecord(data.getBatchNo());
        List<AuthStatus> authStatuses = tDxrecordInvoiceDao.selectAuthResult(data.getBatchNo());
        boolean r = authStatuses != null && authStatuses.size() > 0;
        if (resultNum == 0 || r) {
            data.setContentRows(0);
            data.setBatchState("N");
            String resultData = JSON.toJSONString(data);
            run.setReturnCode(ReturnCodeEnum.CLCG.getFhzDm());
            run.setReturnMessage(ReturnCodeEnum.CLCG.getFhzMc(ReturnCodeEnum.CLCG.getIndex()));
            interfaceCode.setReturnStateInfo(run);
            interfaceCode.setGlobalInfo(globalInfo);

            interfaceCode.setData(Base64.encode(resultData));

            return JSON.toJSONString(interfaceCode);
        }
        List<AuthStatus> authStatusList = tDxrecordInvoiceDao.selectAuthResultBack(data.getBatchNo());
        DecimalFormat df = new DecimalFormat("#0.00");
        for (AuthStatus authStatus : authStatusList) {
            if ("0".equals(authStatus.getAuthState())) {
                authStatus.setFailureReason("未认证");
            }
            if (authStatus.getAuthState() == null) {
                authStatus.setAuthState("5");
                authStatus.setLegalizeState("0");
                authStatus.setFailureReason("无此底账");
            }
            if (authStatus.getLegalizeDate() == null) {
                authStatus.setLegalizeDate("");
            }
            if (authStatus.getFailureReason() == null) {
                authStatus.setFailureReason("");
            }
            if (authStatus.getLegalizeBelongDate() == null) {
                authStatus.setLegalizeBelongDate("");
            }
            if (authStatus.getManagementStatus() == null) {
                authStatus.setManagementStatus("");
            }
            if (authStatus.getInformationSources() == null) {
                authStatus.setInformationSources("");
            }
            if (authStatus.getOverdueCheckMark() == null) {
                authStatus.setOverdueCheckMark("");
            }
            if ("1".equals(authStatus.getRzlx()) || "4".equals(authStatus.getRzlx())) {
                authStatus.setTaxCredit(df.format(new BigDecimal(authStatus.getTaxCredit())));
            } else {
                authStatus.setTaxCredit("");
            }
            if ("5".equals(authStatus.getRzlx())) {
                authStatus.setAuthState(authStatus.getCxrzStatus());
            }
            if (StringUtils.isBlank(authStatus.getInvoiceCode())) {
                authStatus.setInvoiceCode("");
            }
            authStatus.setTaxCredit("");
            if ("1".equals(authStatus.getRzlx()) || "4".equals(authStatus.getRzlx())) {
                if (StringUtils.isNotBlank(authStatus.getTaxCredit())) {
                    authStatus.setTaxCredit(df.format(new BigDecimal(authStatus.getTaxCredit())));
                }
            }
            if ("5".equals(authStatus.getRzlx())) {
                authStatus.setAuthState(authStatus.getCxrzStatus());
            }
            if ("6".equals(authStatus.getRzlx())) {
                authStatus.setAuthState(authStatus.getCxbdkStatus());
            }
        }
        data.setBatchState("Y");
        data.setContentRows(authStatusList.size());
        data.setInvoices(authStatusList);
        String resultData = JSON.toJSONString(data);
        run.setReturnCode(ReturnCodeEnum.CLCG.getFhzDm());
        run.setReturnMessage(ReturnCodeEnum.CLCG.getFhzMc(ReturnCodeEnum.CLCG.getIndex()));
        interfaceCode.setReturnStateInfo(run);
        interfaceCode.setGlobalInfo(globalInfo);

        interfaceCode.setData(Base64.encode(resultData));

        return JSON.toJSONString(interfaceCode);
    }

    @Override
    public EsOutPutResp receiveErpInvoiceMessage(IsInput isInput) {
        EsOutPutResp es = new EsOutPutResp();
        es.setBSKEY(isInput.getBSKEY());
        es.setSAPKEY(isInput.getSAPKEY());
        es.setZTYPE("S");
        ZDATARESP zdataresp = new ZDATARESP();
        StringBuilder promsg = new StringBuilder();
        String zdata2 = isInput.getZDATA();
        List<ZDATA> zdata = JSON.parseArray(zdata2, ZDATA.class);
        AuthDateReq authDateReq = new AuthDateReq();
        authDateReq.setBatchNo(isInput.getSAPKEY());
        authDateReq.setApplyRzlx("1");
        StringBuilder errorMsg = new StringBuilder();
        DynamicContextHolder.push("business" + DbConstant.BUSINESS_READ);
        List<String> fplxList = new ArrayList<>();
        fplxList.add("01");
        fplxList.add("03");
        fplxList.add("04");
        fplxList.add("08");
        fplxList.add("10");
        fplxList.add("11");
        fplxList.add("14");
        fplxList.add("31");
        fplxList.add("32");
        for (ZDATA zdata1 : zdata) {
            log.info("zdata1.getZFPHM()===>{}", zdata1.getZFPHM());
            String collect = fplxList.stream().collect(Collectors.joining(","));
            if (!collect.contains(convertFpzldm(zdata1.getZFPLX()))) {
                log.info("非增值税先不处理：{}", JSON.toJSONString(zdata1));
                continue;
            }
            String fphm = zdata1.getZFPHM();
            if (org.apache.commons.lang3.StringUtils.isNotBlank(fphm) && fphm.length() > 8 && !("26".equals(zdata1.getZFPLX()) || "27".equals(zdata1.getZFPLX()))) {
                fphm = fphm.replace(zdata1.getZFPDM(), "");
                log.info("非全电但是号码");
            }
            log.info("发票号码{}", fphm);
            String uuid = zdata1.getZFPDM() + fphm;
            zdata1.setZFPLX(convertFpzldm(zdata1.getZFPLX()));
            TdxRecordInvoice recordInvoiceByUuid = tDxrecordInvoiceDao.getRecordInvoiceByUuid(uuid);
            //查询票池
            QueryWrapper<TDxInvoiceReimburse> queryWrapper = new QueryWrapper<>();
            queryWrapper.eq("uuid", uuid);
            TDxInvoiceReimburse tDxInvoiceReimburse = tDxInvoiceReimburseService.getBaseMapper().selectOne(queryWrapper);
            //没法区分来源是不是销项发票
            TdxSaleRecordInvoice tdxSaleRecordInvoice = tdxSaleRecordInvoiceDao.selectByUuid(uuid);
            if (recordInvoiceByUuid == null && tDxInvoiceReimburse == null && tdxSaleRecordInvoice == null) {
                errorMsg.append("发票号码:" + zdata1.getZFPHM());
            }
        }
        if (errorMsg.length() > 1) {
            es.setZMESSAGE(errorMsg.toString() + "不存在");
            es.setZTYPE("E");
            return es;
        }
        log.info("zdata={}",zdata);
        for (ZDATA zdata1 : zdata) {
            //成功或者失败标识
            boolean flag = false;
            log.info("zdata1.getZFPHM():{}", zdata1.getZFPHM());
            String fphm = zdata1.getZFPHM();
            if (org.apache.commons.lang3.StringUtils.isNotBlank(fphm) && fphm.length() > 8 && !("26".equals(zdata1.getZFPLX()) || "27".equals(zdata1.getZFPLX()))) {
                fphm = fphm.replace(zdata1.getZFPDM(), "");
                log.info("非全电但是号码");
            }
            log.info("发票号码{}", fphm);
            String uuid = zdata1.getZFPDM() + fphm;
            //销项标识
            String xxbs = zdata1.getXXBS();
            DynamicContextHolder.push("business" + DbConstant.BUSINESS_READ);
            if (fplxList.contains(zdata1.getZFPLX()) && !"X".equalsIgnoreCase(xxbs)) {
                TdxRecordInvoice recordInvoiceByUuid = tDxrecordInvoiceDao.getRecordInvoiceByUuid(uuid);
                log.info("查询的发票为:{}",recordInvoiceByUuid);
                if (recordInvoiceByUuid != null) {
                    UpdateWrapper updateWrapper = new UpdateWrapper();
                    updateWrapper.eq("uuid", uuid);
                    //凭证号
                    updateWrapper.set("sn_voucher_number", zdata1.getBELNR());
                    //凭证过账日期
                    updateWrapper.set("posting_time", zdata1.getBUDAT());
                    //账期
                    updateWrapper.set("account_period", zdata1.getBUDAT().substring(0, 7));
                    //报账单号
                    updateWrapper.set("bzdh", zdata1.getZBZDH());
                    //入账状态
                    updateWrapper.set("inaccount_status", zdata1.getZYWZT());
                    updateWrapper.set("comp_code", zdata1.getZGSDM());
                    updateWrapper.set("bzr", zdata1.getZJBRT());
                    DynamicContextHolder.push("business" + DbConstant.BUSINESS_WRITE);
                    int update = tDxrecordInvoiceDao.update(null, updateWrapper);
                    if (update > 0) {
                        DynamicContextHolder.push(DbConstant.BASICS_READ);
                        SysDeptEntity org = sysDeptDao.getOrg(recordInvoiceByUuid.getGfTaxNo());
                        zdata1.setZFPLX(convertFpzldm(zdata1.getZFPLX()));
                        //如果设置的自动认证走申请认证逻辑
                        if (org != null && "1".equals(org.getAutoRz()) && ("01".equals(zdata1.getZFPLX()) || "03".equals(zdata1.getZFPLX())
                                || "08".equals(zdata1.getZFPLX()) || "14".equals(zdata1.getZFPLX())
                                || "31".equals(zdata1.getZFPLX())) && StringUtils.isNotBlank(zdata1.getBUDAT())) {
                            if (!"4".equals(recordInvoiceByUuid.getAuthStatus()) && !"3".equals(recordInvoiceByUuid.getAuthStatus())
                                    && !"2".equals(recordInvoiceByUuid.getAuthStatus())) {
                                Map<String, Object> authState = new HashMap<>();
                                authState.put("rz_user", zdata1.getZJBRT());
                                authState.put("applyTaxPeriod", zdata1.getBUDAT().substring(0, 7).replace("-", ""));
                                DynamicContextHolder.push("business" + DbConstant.BUSINESS_WRITE);
                                tDxrecordInvoiceDao.updateRz(authState, "1", "1", recordInvoiceByUuid.getUuid());
                            }
                        }
                    }
                }
            } else {
                UpdateWrapper updateWrapper = convertRecordInvoice(zdata1, uuid);
                DynamicContextHolder.push("business" + DbConstant.BUSINESS_WRITE);
                tDxInvoiceReimburseService.getBaseMapper().update(null, updateWrapper);
            }
            if ("X".equalsIgnoreCase(xxbs)){
                DynamicContextHolder.push("business"+DbConstant.BUSINESS_WRITE);
                UpdateWrapper updateWrapper = convertRecordInvoice(zdata1,uuid);
                tdxSaleRecordInvoiceDao.update(null,updateWrapper);
            }
            zdataresp.setPROMSG(promsg.toString());
            zdataresp.setPROTYP("S");
            es.setZMESSAGE("处理成功");
            es.setZdata(zdataresp);
        }
        return es;
    }

    public UpdateWrapper convertRecordInvoice(ZDATA zdata, String uuid) {
        UpdateWrapper updateWrapper = new UpdateWrapper();
        updateWrapper.set("sn_voucher_number", zdata.getBELNR());
        try {
            DateTime parse = DateTime.parse(zdata.getBUDAT());
            updateWrapper.set("posting_time", zdata.getBUDAT());
            //账期
            updateWrapper.set("account_period", zdata.getBUDAT().substring(0, 7));
        } catch (Exception e) {
            log.error("时间转换异常：{}", e);
        }
        updateWrapper.set("bzdh", zdata.getZBZDH());
        updateWrapper.set("inaccount_status", zdata.getZYWZT());
        updateWrapper.set("comp_code", zdata.getZGSDM());
        updateWrapper.set("bzr", zdata.getZJBRT());
        updateWrapper.eq("uuid", uuid);
        return updateWrapper;
    }

    public ReturnStateInfo getCheckData(List<Map<String, Object>> authStateList, Integer contentRows) {
        ReturnStateInfo run = new ReturnStateInfo();
        if (contentRows == null || authStateList.size() != contentRows) {
            run.setReturnCode(ReturnCodeEnum.NRYCY.getFhzDm());
            run.setReturnMessage(ReturnCodeEnum.NRYCY.getFhzMc(ReturnCodeEnum.NRYCY.getIndex()));
            return run;
        }

        for (Map<String, Object> map : authStateList) {
//            if (StringUtils.isBlank((String)map.get("invoiceCode")) || StringUtils.isBlank((String)map.get("invoiceNo"))
//                || ((String)map.get("invoiceCode")).length() > 12 || ((String)map.get("invoiceNo")).length() > 10) {
//                run.setReturnCode(ReturnCodeEnum.DMHMBF.getFhzDm());
//                run.setReturnMessage(ReturnCodeEnum.DMHMBF.getFhzMc(ReturnCodeEnum.DMHMBF.getIndex()));
//                return run;
//            }
            boolean r =
                    !(CheckUtils.stringLength((String) map.get("invoiceNo"), 8) || CheckUtils.stringLength((String) map.get("invoiceNo"), 10)
                            || CheckUtils.stringLength((String) map.get("invoiceNo"), 18) || CheckUtils.stringLength((String) map.get("invoiceNo"), 20));
            if (CheckUtils.isNullOrEmpty((String) map.get("invoiceNo")) || r) {
                run.setReturnCode(ReturnCodeEnum.DMHMBF.getFhzDm());
                run.setReturnMessage(ReturnCodeEnum.DMHMBF.getFhzMc(ReturnCodeEnum.DMHMBF.getIndex()));
            }
            if (StringUtils.isBlank((String) map.get("applyTaxPeriod"))
                    || ((String) map.get("applyTaxPeriod")).length() != 6) {
                run.setReturnCode(ReturnCodeEnum.SKSSQBF.getFhzDm());
                run.setReturnMessage(ReturnCodeEnum.SKSSQBF.getFhzMc(ReturnCodeEnum.SKSSQBF.getIndex()));
                return run;
            }
            if (StringUtils.isBlank((String) map.get("buyerTaxNo"))) {
                run.setReturnCode(ReturnCodeEnum.SHBNWK.getFhzDm());
                run.setReturnMessage(ReturnCodeEnum.SHBNWK.getFhzMc(ReturnCodeEnum.SHBNWK.getIndex()));
                return run;
            }
            if (StringUtils.isBlank((String) map.get("rz_user"))) {
                run.setReturnCode(ReturnCodeEnum.RZRBNWK.getFhzDm());
                run.setReturnMessage(ReturnCodeEnum.RZRBNWK.getFhzMc(ReturnCodeEnum.RZRBNWK.getIndex()));
                return run;
            }

//            if (StringUtils.isBlank((String)map.get("taxCredit"))) {
//                run.setReturnCode(ReturnCodeEnum.DKSEBNWK.getFhzDm());
//                run.setReturnMessage(ReturnCodeEnum.DKSEBNWK.getFhzMc(ReturnCodeEnum.DKSEBNWK.getIndex()));
//                return run;
//            }
        }
        return run;
    }
    public String convertFpzldm(String fpzldm) {
        if ("10".equals(fpzldm)) {
            fpzldm = FplxEnum.ZP.getFplxDm();
        } else if ("11".equals(fpzldm)) {
            fpzldm = FplxEnum.PP.getFplxDm();
        } else if ("12".equals(fpzldm)) {
            fpzldm = FplxEnum.DZP.getFplxDm();
        } else if ("13".equals(fpzldm)) {
            fpzldm = FplxEnum.JSP.getFplxDm();
        } else if ("14".equals(fpzldm)) {
            fpzldm = FplxEnum.JDC.getFplxDm();
        } else if ("15".equals(fpzldm)) {
            fpzldm = FplxEnum.ESC.getFplxDm();
        } else if ("24".equals(fpzldm)) {
            fpzldm = FplxEnum.TXF.getFplxDm();
        } else if ("25".equals(fpzldm)) {
            fpzldm = FplxEnum.DZZP.getFplxDm();
        } else if ("26".equals(fpzldm)) {
            fpzldm = FplxEnum.QDZZP.getFplxDm();
        } else if ("27".equals(fpzldm)) {
            fpzldm = FplxEnum.QDPP.getFplxDm();
        }
        return fpzldm;
    }

    public static void main(String[] args) {

        Date date = DateUtil.strToDate("0000-00-00");
        DateTime parse1 = DateTime.parse("2012-12-01");
        Date date11 = parse1.toDate();
        HashMap<Object, Object> map = Maps.newHashMap();
        map.put("1", "3");
        try {
            DateTime parse = DateTime.parse("0000-00-00");
            map.put("2", "3");
            Date date1 = parse.toDate();
            String s = parse.toString("yyyy-MM-dd ");
            System.out.println("");
        } catch (Exception e) {
//            throw new RuntimeException(e);
        }
        map.put("3", "3");
        System.out.println();
//        String d="2023";
//        String substring = d.substring(0, 7);
//        System.out.println(substring);

    }


}
