package com.dxhy.erp.service.impl;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.dxhy.common.constant.DbConstant;
import com.dxhy.common.datasource.config.DynamicContextHolder;
import com.dxhy.common.enums.DataSourceEnum;
import com.dxhy.common.enums.ReturnCodeEnum;
import com.dxhy.common.service.impl.MpBaseServiceImpl;
import com.dxhy.common.util.AmountFormatUtil;
import com.dxhy.common.util.MathUtil;
import com.dxhy.common.util.pojo.Authorize;
import com.dxhy.common.util.pojo.GlobalInfo;
import com.dxhy.common.utils.DateUtils;
import com.dxhy.erp.dao.AuthRecordDao;
import com.dxhy.erp.dao.RiskStatisticsDao;
import com.dxhy.erp.dao.SysDeptDao;
import com.dxhy.erp.dao.TdxCustomsRecordDao;
import com.dxhy.erp.entity.*;
import com.dxhy.erp.model.*;
import com.dxhy.erp.model.auth.InterfaceCode;
import com.dxhy.erp.service.CustomspcService;
import com.dxhy.erp.service.RiskStatisticsService;
import com.dxhy.erp.service.TdxUploadRecordService;
import com.dxhy.erp.utils.CheckUtils;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.codec.binary.Base64;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Value;
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.nio.charset.StandardCharsets;
import java.text.DecimalFormat;
import java.text.SimpleDateFormat;
import java.util.*;

/**
 * @author dxhy
 */
@SuppressWarnings({"AlibabaUndefineMagicConstant", "AlibabaMethodTooLong", "AlibabaAvoidComplexCondition"})
@Service("riskStatisticsService")
@Slf4j
public class RiskStatisticsImpl extends MpBaseServiceImpl<RiskStatisticsDao, RiskStatisticsModel>
    implements RiskStatisticsService {

    @Resource
    private SysDeptDao sysDeptDao;

    @Resource
    private RiskStatisticsDao riskStatisticsDao;

    @Override
    public String getRiskStatistics(GlobalInfo globalInfo, Map<String, String> map, HttpServletRequest request,
        HttpServletResponse response, Authorize authorize) {
        ReturnStateInfo run = new ReturnStateInfo();
        RiskStatisticsModel model = null;
        InterfaceCode interfaceCode = new InterfaceCode();
        RiskStatisticsPo data;
        try {
            data = JSON.parseObject(map.get("data"), RiskStatisticsPo.class);
            run = getCheck(data);
            if (run.getReturnCode() != null || run.getReturnMessage() != null) {
                interfaceCode.setGlobalInfo(globalInfo);
                interfaceCode.setReturnStateInfo(run);
                return JSON.toJSONString(interfaceCode);
            }

            DynamicContextHolder.push(DbConstant.BASICS_READ);
            SysDeptEntity deptEntity = sysDeptDao.getOrg(data.getTaxNo());
            String dbName;
            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);
            }
            String resultData = "";
            DynamicContextHolder.push(dbName + DbConstant.BUSINESS_READ);
            // 获取数据
            if ("1".equals(data.getDataType())) {
                model = riskStatisticsDao.getJxData(data);
            } else if ("2".equals(data.getDataType())) {
                model = riskStatisticsDao.getXxData(data);
            }
            model.setInvoiceAmountSum(MathUtil.round(model.getInvoiceAmountSum()));
            model.setTaxAmountSum(MathUtil.round(model.getTaxAmountSum()));
            model.setTotalAmountSum(MathUtil.round(model.getTotalAmountSum()));
            resultData = JSON.toJSONString(model);
            run.setReturnCode(ReturnCodeEnum.CLCG.getFhzDm());
            run.setReturnMessage(ReturnCodeEnum.CLCG.getFhzMc(ReturnCodeEnum.CLCG.getIndex()));
            interfaceCode.setReturnStateInfo(run);
            interfaceCode.setGlobalInfo(globalInfo);
            interfaceCode.setData(Base64.encodeBase64String(resultData.getBytes(StandardCharsets.UTF_8)));
            return JSON.toJSONString(interfaceCode);
        } catch (Exception e) {
            e.printStackTrace();
            run.setReturnCode(ReturnCodeEnum.CLSB.getFhzDm());
            run.setReturnMessage(ReturnCodeEnum.CLSB.getFhzMc(ReturnCodeEnum.CLSB.getIndex()));
            interfaceCode.setGlobalInfo(globalInfo);
            interfaceCode.setReturnStateInfo(run);
            return JSON.toJSONString(interfaceCode);
        }
    }

    public ReturnStateInfo getCheck(RiskStatisticsPo data) {
        ReturnStateInfo run = new ReturnStateInfo();

        if (CheckUtils.isNullOrEmpty(data.getTaxNo())) {
            run.setReturnCode(ReturnCodeEnum.SHBNWK.getFhzDm());
            run.setReturnMessage(ReturnCodeEnum.SHBNWK.getFhzMc(ReturnCodeEnum.SHBNWK.getIndex()));
        }

        if (CheckUtils.isNullOrEmpty(data.getDataType())) {
            run.setReturnCode(ReturnCodeEnum.QQBWGSCW.getFhzDm());
            run.setReturnMessage(ReturnCodeEnum.QQBWGSCW.getFhzMc(ReturnCodeEnum.QQBWGSCW.getIndex()));
        }
        if (CheckUtils.isNullOrEmpty(data.getInvoiceMonth()) || (!CheckUtils.stringLength(data.getInvoiceMonth(), 6))) {
            run.setReturnCode(ReturnCodeEnum.QQBWGSCW.getFhzDm());
            run.setReturnMessage(ReturnCodeEnum.QQBWGSCW.getFhzMc(ReturnCodeEnum.QQBWGSCW.getIndex()));
        }
        if (CheckUtils.isNullOrEmpty(data.getInvoiceType())) {
            run.setReturnCode(ReturnCodeEnum.FPLXWK.getFhzDm());
            run.setReturnMessage(ReturnCodeEnum.FPLXWK.getFhzMc(ReturnCodeEnum.FPLXWK.getIndex()));
        }
        return run;
    }

}
