package com.dxhy.erp.service.impl;

import java.nio.charset.StandardCharsets;
import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.codec.binary.Base64;
import org.apache.commons.lang.StringUtils;
import org.springframework.stereotype.Service;

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.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.SysDeptDao;
import com.dxhy.erp.dao.TdxTaxCurrentDao;
import com.dxhy.erp.entity.SysDeptEntity;
import com.dxhy.erp.entity.TdxTaxCurrent;
import com.dxhy.erp.model.ReturnStateInfo;
import com.dxhy.erp.model.TaxCurrentReq;
import com.dxhy.erp.model.TaxCurrentRes;
import com.dxhy.erp.model.auth.InterfaceCode;
import com.dxhy.erp.service.TaxCurrentService;

import lombok.extern.slf4j.Slf4j;

/**
 * @author dxhy
 */
@Service("taxCurrentService")
@Slf4j
public class TaxCurrentServiceImpl extends MpBaseServiceImpl<TdxTaxCurrentDao, TdxTaxCurrent>
    implements TaxCurrentService {
    @Resource
    private SysDeptDao sysDeptDao;

    @Override
    public String getTaxCurrent(GlobalInfo globalInfo, Map<String, String> map, HttpServletRequest request,
        HttpServletResponse response, Authorize authorize) {
        ReturnStateInfo run = new ReturnStateInfo();
        InterfaceCode interfaceCode = new InterfaceCode();
        TaxCurrentReq data;
        try {
            data = JSONObject.parseObject(map.get("data"), TaxCurrentReq.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.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;
        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);
        QueryWrapper<TdxTaxCurrent> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("taxno", data.getBuyerTaxNo());
        TdxTaxCurrent taxCurrent = this.baseMapper.selectOne(queryWrapper);
        String resultData = "";
        if (taxCurrent != null) {
            TaxCurrentRes res = new TaxCurrentRes();
            res.setCurTaxno(taxCurrent.getTaxno());
            res.setOldTaxno(taxCurrent.getOldTaxNo());
            res.setCompName(taxCurrent.getTaxname());
            res.setIncomeMoth(taxCurrent.getCurrentTaxPeriod());
            res.setSelectStartDate(taxCurrent.getSelectStartDate());
            res.setSelectEndDate(taxCurrent.getSelectEndDate());
            res.setIncomeEndDate(taxCurrent.getOperationEndDate());
            res.setCompLevel(taxCurrent.getCreditRating());
            res.setAppPeriod(taxCurrent.getDeclarePeriod());
            resultData = JSON.toJSONString(res);
        }
        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);

    }

}
