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.toolkit.Wrappers;
import com.dxhy.api.model.config.sys.SysEnterpriseConfigDto;
import com.dxhy.common.constant.DbConstant;
import com.dxhy.common.datasource.config.DynamicContextHolder;
import com.dxhy.common.enums.ReturnCodeEnum;
import com.dxhy.common.service.impl.MpBaseServiceImpl;
import com.dxhy.common.util.pojo.Authorize;
import com.dxhy.common.util.pojo.GlobalInfo;
import com.dxhy.erp.dao.SysEnterpriseConfigErpDao;
import com.dxhy.jxpt.baseService.config.service.SysEnterpriseConfigService;
import com.dxhy.jxpt.baseService.ncpInvoice.dao.TdxDkCountDao;
import com.dxhy.erp.dao.TdxStateRecordDao;
import com.dxhy.api.model.db.TDxDkCountEntity;
import com.dxhy.api.model.db.TDxDkCountDetailEntity;
import com.dxhy.api.model.db.TDxStateRecordEntity;
import com.dxhy.erp.model.ReturnStateInfo;
import com.dxhy.erp.model.StateDataReq;
import com.dxhy.erp.model.StateDataRes;
import com.dxhy.erp.model.auth.InterfaceCode;
import com.dxhy.erp.service.ApplyStatService;
import com.dxhy.erp.utils.CheckUtils;
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.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.List;
import java.util.Map;

/**
 * @author dxhy
 */
@SuppressWarnings({"AlibabaUndefineMagicConstant", "AlibabaMethodTooLong"})
@Service("applyStatService")
@Slf4j
public class ApplyStatServiceImpl extends MpBaseServiceImpl<TdxDkCountDao, TDxDkCountEntity> implements ApplyStatService {
    @Autowired
    private SysEnterpriseConfigErpDao enterpriseConfigErpDao;
    @Autowired
    private TdxDkCountDao tDxDkCountDao;
    @Autowired
    private SysEnterpriseConfigService sysEnterpriseConfigService;
    @Autowired
    private TdxStateRecordDao stateRecordDao;

    @Override
    public String applyStat(GlobalInfo globalInfo, Map<String, String> map, HttpServletRequest request,
        HttpServletResponse response, Authorize authorize) {
        ReturnStateInfo run = new ReturnStateInfo();
        InterfaceCode interfaceCode = new InterfaceCode();
        StateDataReq data = null;
        try {
            data = JSONObject.parseObject(map.get("data"), StateDataReq.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);
        }
        run = checkData(data);
        if (run.getReturnCode() != null) {
            interfaceCode.setGlobalInfo(globalInfo);
            interfaceCode.setReturnStateInfo(run);
            return JSON.toJSONString(interfaceCode);
        }
        DynamicContextHolder.push(DbConstant.BASICS_READ);
        SysEnterpriseConfigDto deptEntity = enterpriseConfigErpDao.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);
        List<TDxStateRecordEntity> records =
            stateRecordDao.selectList(Wrappers.<TDxStateRecordEntity>query().eq("batch_no", data.getBatchNo()));
        if (records != null && records.size() > 0) {
            ReturnStateInfo runInfo = new ReturnStateInfo();
            runInfo.setReturnCode("2099");
            runInfo.setReturnMessage("批次号已存在");
            interfaceCode.setGlobalInfo(globalInfo);
            interfaceCode.setReturnStateInfo(runInfo);
            return JSON.toJSONString(interfaceCode);
        }
        TDxDkCountEntity dkCount = tDxDkCountDao.selectCountByTaxNo(data.getBuyerTaxNo());
        if (dkCount == null) {
            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);
        }
        if ("0".equals(data.getStatisticalType())) {
            if ("0".equals(dkCount.getTjStatus()) || "3".equals(dkCount.getTjStatus())) {
                dkCount.setApplyStatus("0");
                dkCount.setTjStatus("1");
                dkCount.setApplyType("0");
                dkCount.setCxtjStatus("0");
                dkCount.setCxqsStatus("0");
            }
        } else if ("1".equals(data.getStatisticalType())) {
            if ("2".equals(dkCount.getTjStatus())) {
                if ("0".equals(dkCount.getCxtjStatus()) || "3".equals(dkCount.getCxtjStatus())) {
                    dkCount.setCxtjStatus("1");
                    dkCount.setApplyType("1");
                    dkCount.setApplyStatus("0");
                }
            } else {
                ReturnStateInfo runInfo = new ReturnStateInfo();
                runInfo.setReturnCode(ReturnCodeEnum.SHWTJ.getFhzDm());
                runInfo.setReturnMessage(ReturnCodeEnum.SHWTJ.getFhzMc(ReturnCodeEnum.SHWTJ.getIndex()));
                interfaceCode.setGlobalInfo(globalInfo);
                interfaceCode.setReturnStateInfo(runInfo);
                return JSON.toJSONString(interfaceCode);
            }
        }
        DynamicContextHolder.push(dbName + DbConstant.BUSINESS_WRITE);
        TDxStateRecordEntity stateRecord = buildStateRecord(data, dkCount);
        stateRecordDao.insert(stateRecord);
        tDxDkCountDao.updateDkCount(dkCount);
        run.setReturnCode(ReturnCodeEnum.CLCG.getFhzDm());
        run.setReturnMessage(ReturnCodeEnum.CLCG.getFhzMc(ReturnCodeEnum.CLCG.getIndex()));
        interfaceCode.setGlobalInfo(globalInfo);
        interfaceCode.setReturnStateInfo(run);
        return JSON.toJSONString(interfaceCode);
    }

    @Override
    public String statResult(GlobalInfo globalInfo, Map<String, String> map, HttpServletRequest request,
        HttpServletResponse response, Authorize authorize) {
        ReturnStateInfo run = new ReturnStateInfo();
        InterfaceCode interfaceCode = new InterfaceCode();
        StateDataReq data = null;
        try {
            data = JSONObject.parseObject(map.get("data"), StateDataReq.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())) {
            run.setReturnCode(ReturnCodeEnum.PCHBNWK.getFhzDm());
            run.setReturnMessage(ReturnCodeEnum.PCHBNWK.getFhzMc(ReturnCodeEnum.PCHBNWK.getIndex()));
            interfaceCode.setGlobalInfo(globalInfo);
            interfaceCode.setReturnStateInfo(run);
            return JSON.toJSONString(interfaceCode);
        }
        DynamicContextHolder.push(DbConstant.BASICS_READ);
        SysEnterpriseConfigDto deptEntity = enterpriseConfigErpDao.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);
        TDxStateRecordEntity stateRecord = tDxDkCountDao.selectStateRecord(data.getBatchNo());
        if (stateRecord == null) {
            run.setReturnCode(ReturnCodeEnum.NRYCY.getFhzDm());
            run.setReturnMessage(ReturnCodeEnum.NRYCY.getFhzMc(ReturnCodeEnum.NRYCY.getIndex()));
            interfaceCode.setReturnStateInfo(run);
            interfaceCode.setGlobalInfo(globalInfo);
            return JSON.toJSONString(interfaceCode);
        }
        TDxDkCountEntity dkCount = tDxDkCountDao.selectStateResult(stateRecord.getBuyerTaxNo(), stateRecord.getTjMonth());
        StateDataRes stateDataRes = new StateDataRes();
        if ("0".equals(stateRecord.getApplyType())) {
            if ("2".equals(dkCount.getTjStatus())) {
                stateDataRes.setBatchNo(data.getBatchNo());
                stateDataRes.setBuyerTaxNo(stateRecord.getBuyerTaxNo());
                stateDataRes.setApplicationResult("1");
                stateDataRes.setStatisticalType("0");
                stateDataRes.setStatisticalMonth(stateRecord.getTjMonth());
                stateDataRes.setStatisticalTime(dkCount.getTjDate());
                //正常统计结果 type=1 正常  type=0  不正常(乐企或者大企业)
                List<TDxDkCountDetailEntity> normalList = tDxDkCountDao.selectCountDetail(dkCount.getTaxno(), dkCount.getSkssq(),"1");
                String stateResult = buildStateResult(normalList);
                stateDataRes.setStatisticalResult(stateResult);
                //异常
                List<TDxDkCountDetailEntity> errList = tDxDkCountDao.selectCountDetail(dkCount.getTaxno(), dkCount.getSkssq(),"0");
                String errResult = buildStateResult(errList);
                stateDataRes.setErrResult(errResult);
            } else if ("3".equals(dkCount.getTjStatus())) {
                stateDataRes.setBatchNo(data.getBatchNo());
                stateDataRes.setBuyerTaxNo(stateRecord.getBuyerTaxNo());
                stateDataRes.setApplicationResult(dkCount.getApplyMsg());
                stateDataRes.setStatisticalType("0");
                stateDataRes.setStatisticalMonth(stateRecord.getTjMonth());
                stateDataRes.setStatisticalTime("");
                stateDataRes.setStatisticalResult("");
            }
        } else if ("1".equals(stateRecord.getApplyType())) {
            if ("2".equals(dkCount.getCxtjStatus())) {
                stateDataRes.setBatchNo(data.getBatchNo());
                stateDataRes.setBuyerTaxNo(stateRecord.getBuyerTaxNo());
                stateDataRes.setApplicationResult("1");
                stateDataRes.setStatisticalType("1");
                stateDataRes.setStatisticalMonth(stateRecord.getTjMonth());
                stateDataRes.setStatisticalTime("");
                stateDataRes.setStatisticalResult("");
            } else if ("3".equals(dkCount.getCxtjStatus())) {
                stateDataRes.setBatchNo(data.getBatchNo());
                stateDataRes.setBuyerTaxNo(stateRecord.getBuyerTaxNo());
                stateDataRes.setApplicationResult(dkCount.getApplyMsg());
                stateDataRes.setStatisticalType("1");
                stateDataRes.setStatisticalMonth(stateRecord.getTjMonth());
                stateDataRes.setStatisticalTime(dkCount.getTjDate());
                //正常统计结果 type=1 正常  type=0  不正常(乐企或者大企业)
                List<TDxDkCountDetailEntity> normalList = tDxDkCountDao.selectCountDetail(dkCount.getTaxno(), dkCount.getSkssq(),"1");
                String stateResult = buildStateResult(normalList);
                stateDataRes.setStatisticalResult(stateResult);
                //异常
                List<TDxDkCountDetailEntity> errList = tDxDkCountDao.selectCountDetail(dkCount.getTaxno(), dkCount.getSkssq(),"0");
                String errResult = buildStateResult(errList);
                stateDataRes.setErrResult(errResult);
            }
        }
        if (stateDataRes.getBatchNo() != null) {
            String result = JSON.toJSONString(stateDataRes);
            interfaceCode.setData(Base64.encode(result));
        }
        run.setReturnCode(ReturnCodeEnum.CLCG.getFhzDm());
        run.setReturnMessage(ReturnCodeEnum.CLCG.getFhzMc(ReturnCodeEnum.CLCG.getIndex()));
        interfaceCode.setReturnStateInfo(run);
        interfaceCode.setGlobalInfo(globalInfo);
        return JSON.toJSONString(interfaceCode);
    }

    @Override
    public String applyConfirm(GlobalInfo globalInfo, Map<String, String> map, HttpServletRequest request,
        HttpServletResponse response, Authorize authorize) {
        ReturnStateInfo run = new ReturnStateInfo();
        InterfaceCode interfaceCode = new InterfaceCode();
        StateDataReq data = null;
        try {
            data = JSONObject.parseObject(map.get("data"), StateDataReq.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);
        }
        run = checkConfirmData(data);
        if (run.getReturnCode() != null) {
            interfaceCode.setGlobalInfo(globalInfo);
            interfaceCode.setReturnStateInfo(run);
            return JSON.toJSONString(interfaceCode);
        }
        DynamicContextHolder.push(DbConstant.BASICS_READ);
        SysEnterpriseConfigDto deptEntity = enterpriseConfigErpDao.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);
        List<TDxStateRecordEntity> records =
            stateRecordDao.selectList(Wrappers.<TDxStateRecordEntity>query().eq("batch_no", data.getBatchNo()));
        if (records != null && records.size() > 0) {
            ReturnStateInfo runInfo = new ReturnStateInfo();
            runInfo.setReturnCode("2099");
            runInfo.setReturnMessage("批次号已存在");
            interfaceCode.setGlobalInfo(globalInfo);
            interfaceCode.setReturnStateInfo(runInfo);
            return JSON.toJSONString(interfaceCode);
        }
        TDxDkCountEntity dkCount = tDxDkCountDao.selectCountByTaxNo(data.getBuyerTaxNo());
        if (dkCount == null) {
            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);
        }
        if (!"2".equals(dkCount.getTjStatus())) {
            ReturnStateInfo runInfo = new ReturnStateInfo();
            runInfo.setReturnCode(ReturnCodeEnum.SHWTJ.getFhzDm());
            runInfo.setReturnMessage(ReturnCodeEnum.SHWTJ.getFhzMc(ReturnCodeEnum.SHWTJ.getIndex()));
            interfaceCode.setGlobalInfo(globalInfo);
            interfaceCode.setReturnStateInfo(runInfo);
            return JSON.toJSONString(interfaceCode);
        }
        if (!data.getStatisticalTime().equals(dkCount.getTjDate())) {
            ReturnStateInfo runInfo = new ReturnStateInfo();
            runInfo.setReturnCode(ReturnCodeEnum.TJSJBF.getFhzDm());
            runInfo.setReturnMessage(ReturnCodeEnum.TJSJBF.getFhzMc(ReturnCodeEnum.TJSJBF.getIndex()));
            interfaceCode.setGlobalInfo(globalInfo);
            interfaceCode.setReturnStateInfo(runInfo);
            return JSON.toJSONString(interfaceCode);
        }
        if ("0".equals(data.getConfirmType())) {
            if (StringUtils.isNotBlank(data.getPassword()) && !data.getPassword().equals(deptEntity.getPassword())) {
                DynamicContextHolder.push(DbConstant.BASICS_WRITE);
                enterpriseConfigErpDao.updatePassword(data.getBuyerTaxNo(), data.getPassword());
            }
            if ("0".equals(dkCount.getQsStatus()) || "3".equals(dkCount.getQsStatus())) {
                dkCount.setApplyStatus("0");
                dkCount.setQsStatus("1");
                dkCount.setApplyType("0");
                dkCount.setCxtjStatus("0");
                dkCount.setCxqsStatus("0");
            }
        } else if ("1".equals(data.getConfirmType())) {
            if (StringUtils.isNotBlank(data.getPassword()) && !data.getPassword().equals(deptEntity.getPassword())) {
                DynamicContextHolder.push(DbConstant.BASICS_WRITE);
                enterpriseConfigErpDao.updatePassword(data.getBuyerTaxNo(), data.getPassword());
            }
            if ("2".equals(dkCount.getQsStatus())) {
                if ("0".equals(dkCount.getCxqsStatus()) || "3".equals(dkCount.getCxqsStatus())) {
                    dkCount.setCxqsStatus("1");
                    dkCount.setApplyType("1");
                    dkCount.setApplyStatus("0");
                }
            } else {
                ReturnStateInfo runInfo = new ReturnStateInfo();
                runInfo.setReturnCode(ReturnCodeEnum.SHWQR.getFhzDm());
                runInfo.setReturnMessage(ReturnCodeEnum.SHWQR.getFhzMc(ReturnCodeEnum.SHWQR.getIndex()));
                interfaceCode.setGlobalInfo(globalInfo);
                interfaceCode.setReturnStateInfo(runInfo);
                return JSON.toJSONString(interfaceCode);
            }
        }
        DynamicContextHolder.push(dbName + DbConstant.BUSINESS_WRITE);
        TDxStateRecordEntity stateRecord = buildConfirmRecord(data, dkCount);
        stateRecordDao.insert(stateRecord);
        tDxDkCountDao.updateDkCount(dkCount);
        run.setReturnCode(ReturnCodeEnum.CLCG.getFhzDm());
        run.setReturnMessage(ReturnCodeEnum.CLCG.getFhzMc(ReturnCodeEnum.CLCG.getIndex()));
        interfaceCode.setGlobalInfo(globalInfo);
        interfaceCode.setReturnStateInfo(run);
        return JSON.toJSONString(interfaceCode);

    }

    @Override
    public String confirmResult(GlobalInfo globalInfo, Map<String, String> map, HttpServletRequest request,
        HttpServletResponse response, Authorize authorize) {
        ReturnStateInfo run = new ReturnStateInfo();
        InterfaceCode interfaceCode = new InterfaceCode();
        StateDataReq data = null;
        try {
            data = JSONObject.parseObject(map.get("data"), StateDataReq.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())) {
            run.setReturnCode(ReturnCodeEnum.PCHBNWK.getFhzDm());
            run.setReturnMessage(ReturnCodeEnum.PCHBNWK.getFhzMc(ReturnCodeEnum.PCHBNWK.getIndex()));
            interfaceCode.setGlobalInfo(globalInfo);
            interfaceCode.setReturnStateInfo(run);
            return JSON.toJSONString(interfaceCode);
        }
        DynamicContextHolder.push(DbConstant.BASICS_READ);
        SysEnterpriseConfigDto deptEntity = enterpriseConfigErpDao.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);
        TDxStateRecordEntity stateRecord = tDxDkCountDao.selectStateRecord(data.getBatchNo());
        if (stateRecord == null) {
            run.setReturnCode(ReturnCodeEnum.NRYCY.getFhzDm());
            run.setReturnMessage(ReturnCodeEnum.NRYCY.getFhzMc(ReturnCodeEnum.NRYCY.getIndex()));
            interfaceCode.setReturnStateInfo(run);
            interfaceCode.setGlobalInfo(globalInfo);
            return JSON.toJSONString(interfaceCode);
        }
        TDxDkCountEntity dkCount = tDxDkCountDao.selectStateResult(stateRecord.getBuyerTaxNo(), stateRecord.getTjMonth());
        StateDataRes stateDataRes = new StateDataRes();
        if ("0".equals(stateRecord.getApplyType())) {
            if ("2".equals(dkCount.getQsStatus())) {
                stateDataRes.setBatchNo(data.getBatchNo());
                stateDataRes.setBuyerTaxNo(stateRecord.getBuyerTaxNo());
                stateDataRes.setApplicationResult("1");
                stateDataRes.setConfirmStatus("0");
                stateDataRes.setStatisticalMonth(stateRecord.getTjMonth());
                stateDataRes.setConfirmType("0");
            } else if ("3".equals(dkCount.getQsStatus())) {
                stateDataRes.setBatchNo(data.getBatchNo());
                stateDataRes.setBuyerTaxNo(stateRecord.getBuyerTaxNo());
                stateDataRes.setApplicationResult(dkCount.getApplyMsg());
                stateDataRes.setConfirmStatus("1");
                stateDataRes.setStatisticalMonth(stateRecord.getTjMonth());
                stateDataRes.setConfirmType("0");
            }
        } else if ("1".equals(stateRecord.getApplyType())) {
            if ("2".equals(dkCount.getCxqsStatus())) {
                stateDataRes.setBatchNo(data.getBatchNo());
                stateDataRes.setBuyerTaxNo(stateRecord.getBuyerTaxNo());
                stateDataRes.setApplicationResult("1");
                stateDataRes.setConfirmStatus("0");
                stateDataRes.setStatisticalMonth(stateRecord.getTjMonth());
                stateDataRes.setConfirmType("1");
            } else if ("3".equals(dkCount.getCxqsStatus())) {
                stateDataRes.setBatchNo(data.getBatchNo());
                stateDataRes.setBuyerTaxNo(stateRecord.getBuyerTaxNo());
                stateDataRes.setApplicationResult(dkCount.getApplyMsg());
                stateDataRes.setConfirmStatus("1");
                stateDataRes.setStatisticalMonth(stateRecord.getTjMonth());
                stateDataRes.setConfirmType("1");
            }
        }
        if (stateDataRes.getBatchNo() != null) {
            String result = JSON.toJSONString(stateDataRes);
            interfaceCode.setData(Base64.encode(result));
        }
        run.setReturnCode(ReturnCodeEnum.CLCG.getFhzDm());
        run.setReturnMessage(ReturnCodeEnum.CLCG.getFhzMc(ReturnCodeEnum.CLCG.getIndex()));
        interfaceCode.setReturnStateInfo(run);
        interfaceCode.setGlobalInfo(globalInfo);
        return JSON.toJSONString(interfaceCode);
    }

    public String buildStateResult(List<TDxDkCountDetailEntity> list) {
        StringBuilder sb = new StringBuilder();
        if (list != null) {
            for (TDxDkCountDetailEntity detail : list) {
                sb.append(detail.getInvoiceType()).append("=").append(detail.getDkInvoiceCount()).append("=")
                    .append(detail.getDkAmountCount()).append("=").append(detail.getDkTaxAmountCount()).append("=")
                    .append(detail.getBdkInvoiceCount()).append("=").append(detail.getBdkAmountCount()).append("=")
                    .append(detail.getBdkTaxAmountCount()).append(";");
            }
        } else {
            sb.append("01=0=0=0=0=0=0;03=0=0=0=0=0=0;08=0=0=0=0=0=0;14=0=0=0=0=0=0;17=0=-=0=0=-=0;24=0=0=0=0=0=0;"
                + "30=0=-=0=0=-=0;80=0=0=0=0=0=0;99=0=0=0=0=0=0;");
        }
        return sb.toString();
    }

    public TDxStateRecordEntity buildStateRecord(StateDataReq data, TDxDkCountEntity dkCount) {
        TDxStateRecordEntity record = new TDxStateRecordEntity();
        record.setApplyType(data.getStatisticalType());
        record.setBatchNo(data.getBatchNo());
        record.setTjMonth(dkCount.getSkssq());
        record.setBuyerTaxNo(data.getBuyerTaxNo());
        record.setStateType("0");
        record.setStateTime(data.getStatisticalTime());
        return record;
    }

    public TDxStateRecordEntity buildConfirmRecord(StateDataReq data, TDxDkCountEntity dkCount) {
        TDxStateRecordEntity record = new TDxStateRecordEntity();
        record.setApplyType(data.getConfirmType());
        record.setBatchNo(data.getBatchNo());
        record.setTjMonth(dkCount.getSkssq());
        record.setBuyerTaxNo(data.getBuyerTaxNo());
        record.setStateType("1");
        record.setStateTime(data.getStatisticalTime());
        return record;
    }

    public ReturnStateInfo checkData(StateDataReq data) {
        ReturnStateInfo run = new ReturnStateInfo();
        if (StringUtils.isBlank(data.getBatchNo()) || (!CheckUtils.stringLength(data.getBatchNo(), 32))) {
            run.setReturnCode(ReturnCodeEnum.PCHBNWK.getFhzDm());
            run.setReturnMessage(ReturnCodeEnum.PCHBNWK.getFhzMc(ReturnCodeEnum.PCHBNWK.getIndex()));
            return run;
        }
        if (StringUtils.isBlank(data.getBuyerTaxNo())) {
            run.setReturnCode(ReturnCodeEnum.SHBNWK.getFhzDm());
            run.setReturnMessage(ReturnCodeEnum.SHBNWK.getFhzMc(ReturnCodeEnum.SHBNWK.getIndex()));
            return run;
        }
        if (StringUtils.isBlank(data.getStatisticalType())) {
            run.setReturnCode(ReturnCodeEnum.TJLXWK.getFhzDm());
            run.setReturnMessage(ReturnCodeEnum.TJLXWK.getFhzMc(ReturnCodeEnum.TJLXWK.getIndex()));
            return run;
        }
        return run;
    }

    public ReturnStateInfo checkConfirmData(StateDataReq data) {
        ReturnStateInfo run = new ReturnStateInfo();
        if (StringUtils.isBlank(data.getBatchNo()) || (!CheckUtils.stringLength(data.getBatchNo(), 32))) {
            run.setReturnCode(ReturnCodeEnum.PCHBNWK.getFhzDm());
            run.setReturnMessage(ReturnCodeEnum.PCHBNWK.getFhzMc(ReturnCodeEnum.PCHBNWK.getIndex()));
            return run;
        }
        if (StringUtils.isBlank(data.getBuyerTaxNo())) {
            run.setReturnCode(ReturnCodeEnum.SHBNWK.getFhzDm());
            run.setReturnMessage(ReturnCodeEnum.SHBNWK.getFhzMc(ReturnCodeEnum.SHBNWK.getIndex()));
            return run;
        }
        if (StringUtils.isBlank(data.getStatisticalTime())) {
            run.setReturnCode(ReturnCodeEnum.TJSJWK.getFhzDm());
            run.setReturnMessage(ReturnCodeEnum.TJSJWK.getFhzMc(ReturnCodeEnum.TJSJWK.getIndex()));
            return run;
        }
        if (StringUtils.isBlank(data.getConfirmType())) {
            run.setReturnCode(ReturnCodeEnum.QRLXWK.getFhzDm());
            run.setReturnMessage(ReturnCodeEnum.QRLXWK.getFhzMc(ReturnCodeEnum.QRLXWK.getIndex()));
            return run;
        }
        String enterpriseJrfs = sysEnterpriseConfigService.getEnterpriseJrfs(data.getBuyerTaxNo());
        if ("0".equals(enterpriseJrfs)&&StringUtils.isBlank(data.getPassword())) {
            run.setReturnCode(ReturnCodeEnum.YWMMBNWK.getFhzDm());
            run.setReturnMessage(ReturnCodeEnum.YWMMBNWK.getFhzMc(ReturnCodeEnum.YWMMBNWK.getIndex()));
            return run;
        }
        return run;
    }
}
