package com.dxhy.core.tab.service.impl;

import com.alibaba.fastjson.JSON;
import com.dxhy.common.constant.DbConstant;
import com.dxhy.common.datasource.config.DynamicContextHolder;
import com.dxhy.core.tab.dao.TabDao;
import com.dxhy.core.tab.entity.*;
import com.dxhy.core.tab.service.TabService;
import lombok.extern.slf4j.Slf4j;
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.text.NumberFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.List;

/**
 * @author user
 */
@SuppressWarnings({"AlibabaUndefineMagicConstant", "AlibabaMethodTooLong", "AlibabaLowerCamelCaseVariableNaming",
    "DuplicatedCode"})
@Service("tabService")
@Slf4j

public class TabServiceImpl implements TabService {

    @Resource
    private TabDao tabDao;

    @Override
    public Object queryCjTabInfo(RequestInfo req, HttpServletRequest request, HttpServletResponse response) {

        ReturnInfo returnInfo = new ReturnInfo();
        List<CollectTabInvoice> cjList = new ArrayList<>();
        CollectTabInvoice col0;
        CollectTabInvoice col1;
        CollectTabInvoice col2;
        CollectTabInvoice col3;
        CollectTabInvoice col4;
        CollectTabInvoice col5;
        CollectTabInvoice col6;
        String invoiceAmount = "";

        try {
            String gname = this.getName(req.getTaxpayerCode());
            DynamicContextHolder.push("business" + DbConstant.BUSINESS_READ);
            // 取六个月
            List<String> result = this.getMonth();
            // 取两个月，查询出汇总数据，计算月环比
            col0 = tabDao.getReInvoice(req.getTaxpayerCode(), result.get(0));

            if (col0 != null) {

                col0.setGfName(gname);
                col0.setGfTaxno(req.getTaxpayerCode());
            }
            col1 = tabDao.getReInvoice(req.getTaxpayerCode(), result.get(1));
            if (col1 != null) {

                col1.setGfName(gname);
                col1.setGfTaxno(req.getTaxpayerCode());
            }
            col2 = tabDao.getReInvoice(req.getTaxpayerCode(), result.get(2));
            if (col2 != null) {

                col2.setGfName(gname);
                col2.setGfTaxno(req.getTaxpayerCode());
            }
            col3 = tabDao.getReInvoice(req.getTaxpayerCode(), result.get(3));
            if (col3 != null) {

                col3.setGfName(gname);
                col3.setGfTaxno(req.getTaxpayerCode());
            }
            col4 = tabDao.getReInvoice(req.getTaxpayerCode(), result.get(4));
            if (col4 != null) {

                col4.setGfName(gname);
                col4.setGfTaxno(req.getTaxpayerCode());
            }
            col5 = tabDao.getReInvoice(req.getTaxpayerCode(), result.get(5));
            if (col5 != null) {

                col5.setGfName(gname);
                col5.setGfTaxno(req.getTaxpayerCode());
            }
            col6 = tabDao.getReInvoice(req.getTaxpayerCode(), result.get(6));
            if (col6 != null) {

                col6.setGfName(gname);
                col6.setGfTaxno(req.getTaxpayerCode());
                invoiceAmount = col6.getInvoiceAmount();
            }

            if (col0 != null && col1 != null) {

                if (!"0".equals(col0.getInvoiceNum()) && !"0".equals(col1.getInvoiceNum())) {
                    String mon0 = this.monthBasis(col0.getInvoiceAmount(), col1.getInvoiceAmount());
                    col1.setMonthPercent(mon0);
                } else if ("0".equals(col0.getInvoiceNum())) {
                    col1.setCollectMonth(result.get(1));
                    col1.setMonthPercent("-");
                } else {
                    col1.setCollectMonth(result.get(1));
                    col1.setMonthPercent("0");
                }
            }
            if (col1 != null && col2 != null) {

                if (!"0".equals(col1.getInvoiceNum()) && !"0".equals(col2.getInvoiceNum())) {
                    String mon1 = this.monthBasis(col1.getInvoiceAmount(), col2.getInvoiceAmount());
                    col2.setMonthPercent(mon1);
                } else if ("0".equals(col1.getInvoiceNum())) {
                    col2.setCollectMonth(result.get(2));
                    col2.setMonthPercent("-");
                } else {
                    col2.setCollectMonth(result.get(2));
                    col2.setMonthPercent("0");
                }
            }
            if (col2 != null && col3 != null) {

                if (!"0".equals(col2.getInvoiceNum()) && !"0".equals(col3.getInvoiceNum())) {
                    String mon2 = this.monthBasis(col2.getInvoiceAmount(), col3.getInvoiceAmount());
                    col3.setMonthPercent(mon2);
                } else if ("0".equals(col2.getInvoiceNum())) {
                    col3.setCollectMonth(result.get(3));
                    col3.setMonthPercent("-");
                } else {
                    col3.setCollectMonth(result.get(3));
                    col3.setMonthPercent("0");
                }
            }
            if (col3 != null && col4 != null) {

                if (!"0".equals(col3.getInvoiceNum()) && !"0".equals(col4.getInvoiceNum())) {
                    String mon4 = this.monthBasis(col3.getInvoiceAmount(), col4.getInvoiceAmount());
                    col4.setMonthPercent(mon4);
                } else if ("0".equals(col3.getInvoiceNum())) {
                    col4.setCollectMonth(result.get(4));
                    col4.setMonthPercent("-");
                } else {
                    col4.setCollectMonth(result.get(4));
                    col4.setMonthPercent("0");
                }
            }
            if (col4 != null && col5 != null) {

                if (!"0".equals(col4.getInvoiceNum()) && !"0".equals(col5.getInvoiceNum())) {
                    String mon5 = this.monthBasis(col4.getInvoiceAmount(), col5.getInvoiceAmount());
                    col5.setMonthPercent(mon5);
                } else if ("0".equals(col5.getInvoiceNum())) {
                    col5.setCollectMonth(result.get(5));
                    col5.setMonthPercent("-");
                } else {
                    col5.setCollectMonth(result.get(5));
                    col5.setMonthPercent("0");
                }
            }
            if (col5 != null && col6 != null) {

                if (!"0".equals(col5.getInvoiceNum()) && !"0".equals(col6.getInvoiceNum())) {
                    String mon6 = this.monthBasis(col5.getInvoiceAmount(), col6.getInvoiceAmount());
                    col6.setMonthPercent(mon6);
                } else if ("0".equals(col5.getInvoiceNum())) {
                    col6.setCollectMonth(result.get(6));
                    col6.setMonthPercent("-");
                } else {
                    col6.setCollectMonth(result.get(6));
                    col6.setMonthPercent("0");
                }
            }
            if (col6 != null) {
                cjList.add(col6);
            }
            if (col5 != null) {
                cjList.add(col5);
            }
            if (col4 != null) {
                cjList.add(col4);
            }
            if (col3 != null) {
                cjList.add(col3);
            }
            if (col2 != null) {
                cjList.add(col2);
            }
            if (col1 != null) {
                cjList.add(col1);
            }

            if ("1".equals(req.getDataType())) {
                returnInfo.setData(invoiceAmount);
            }
            if ("2".equals(req.getDataType())) {
                returnInfo.setContent(cjList);
            }
            if ("3".equals(req.getDataType())) {
                returnInfo.setData(invoiceAmount);
                returnInfo.setContent(cjList);
            }

            returnInfo.setCode("0000");
            returnInfo.setMessage("处理成功");

            return JSON.toJSON(returnInfo);
        } catch (Exception e) {
            log.info("接口调用异常：{}",e);
            returnInfo.setCode("9999");
            returnInfo.setMessage("异常");
            return JSON.toJSON(returnInfo);

        }

    }

    @Override
    public Object queryRzTabInfo(RequestInfo req, HttpServletRequest request, HttpServletResponse response) {
        DynamicContextHolder.push("business" + DbConstant.BUSINESS_READ);
        ReturnInfoRz returnInfo = new ReturnInfoRz();
        RzTwo rzTwo = new RzTwo();
        List<InvoiceRz> rzList;
        List<RzInfo> rzInfoList = new ArrayList<>();

        try {
            String taxDqsq = tabDao.getSq(req.getTaxpayerCode());
            List<RzInfo> rzInfo = tabDao.getRzDq(req.getTaxpayerCode(), taxDqsq);
            String allAmount = tabDao.getAllAmount(req.getTaxpayerCode(), taxDqsq);
            for (RzInfo auth : rzInfo) {
                RzInfo reAuth = new RzInfo();
                if (auth.getName() != null) {
                    if ("01".equals(auth.getName())) {
                        reAuth.setName("增值税专用发票");
                        String a = this.typeBasis(auth.getValue(), allAmount);
                        reAuth.setValue(auth.getValue());
                        reAuth.setPercent(a);
                    }
                    if ("03".equals(auth.getName())) {
                        reAuth.setName("机动车统一销售发票");
                        String a = this.typeBasis(auth.getValue(), allAmount);
                        reAuth.setValue(auth.getValue());
                        reAuth.setPercent(a);
                    }
                    if ("14".equals(auth.getName())) {
                        reAuth.setName("通行费发票");
                        String a = this.typeBasis(auth.getValue(), allAmount);
                        reAuth.setValue(auth.getValue());
                        reAuth.setPercent(a);
                    }
                    if ("08".equals(auth.getName())) {
                        reAuth.setName("增值税电子专用发票");
                        String a = this.typeBasis(auth.getValue(), allAmount);
                        reAuth.setValue(auth.getValue());
                        reAuth.setPercent(a);
                    }
                    if ("31".equals(auth.getName())) {
                        reAuth.setName("电子发票（增值税专用发票）");
                        String a = this.typeBasis(auth.getValue(), allAmount);
                        reAuth.setValue(auth.getValue());
                        reAuth.setPercent(a);
                    }
                    if ("283".equals(auth.getName())) {
                        reAuth.setName("电子发票（铁路电子客票）");
                        String a = this.typeBasis(auth.getValue(), allAmount);
                        reAuth.setValue(auth.getValue());
                        reAuth.setPercent(a);
                    }
                    if ("161".equals(auth.getName())) {
                        reAuth.setName("电子发票（航空运输电子客票行程单）");
                        String a = this.typeBasis(auth.getValue(), allAmount);
                        reAuth.setValue(auth.getValue());
                        reAuth.setPercent(a);
                    }
                    if ("183".equals(auth.getName())) {
                        reAuth.setName("机动车销售电子统一发票");
                        String a = this.typeBasis(auth.getValue(), allAmount);
                        reAuth.setValue(auth.getValue());
                        reAuth.setPercent(a);
                    }
                    if ("185".equals(auth.getName())) {
                        reAuth.setName("数电票（增值税专用发票)");
                        String a = this.typeBasis(auth.getValue(), allAmount);
                        reAuth.setValue(auth.getValue());
                        reAuth.setPercent(a);
                    }
                    if ("32".equals(auth.getName())) {
                        reAuth.setName("电子发票（普通发票）");
                        String a = this.typeBasis(auth.getValue(), allAmount);
                        reAuth.setValue(auth.getValue());
                        reAuth.setPercent(a);
                    }
                } else {
                    reAuth.setName("");
                    reAuth.setPercent("");
                    reAuth.setValue("");
                }

                rzInfoList.add(reAuth);
            }

            Calendar c = Calendar.getInstance();
            c.add(Calendar.MONTH, -5);
            String beforeSix = c.get(Calendar.YEAR) + "-" + c.get(Calendar.MONTH);
            List<String> result = new ArrayList<>();
            // 格式化为年月
            SimpleDateFormat sdf = new SimpleDateFormat("yyyyMM");
            Calendar min = Calendar.getInstance();
            Calendar max = Calendar.getInstance();
            min.setTime(sdf.parse(beforeSix));
            min.set(min.get(Calendar.YEAR), min.get(Calendar.MONTH), 1);
            max.setTime(sdf.parse(sdf.format(new Date())));
            max.set(max.get(Calendar.YEAR), max.get(Calendar.MONTH), 2);
            while (min.before(max)) {
                result.add(sdf.format(min.getTime()));
                min.add(Calendar.MONTH, 1);
            }
            result.remove(0);
            rzList = tabDao.getRzList(req.getTaxpayerCode(), result);
            rzTwo.setRzInfoNow(rzInfoList);
            rzTwo.setSixRzDate(rzList);
            returnInfo.setData(allAmount);
            returnInfo.setContent(rzTwo);
            returnInfo.setCode("0000");
            returnInfo.setMessage("处理成功");
            return JSON.toJSON(returnInfo);
        } catch (Exception e) {
            log.info("接口调用异常：{}",e);
            returnInfo.setCode("9999");
            returnInfo.setMessage("异常");

            return JSON.toJSON(returnInfo);

        }
    }

    @Override
    public Object queryQsTabInfo(RequestInfo req, HttpServletRequest request, HttpServletResponse response) {
        // 返回信息
        ReturnInfo returnInfo = new ReturnInfo();
        // 签收信息
        List<InvoiceScan> scanList;
        try {
            String gname = this.getName(req.getTaxpayerCode());
            DynamicContextHolder.push("business" + DbConstant.BUSINESS_READ);
            // 取六个月
            List<String> result = this.getMonth();
            scanList = tabDao.getQsList(req.getTaxpayerCode(), result);
            for (InvoiceScan scan : scanList) {
                scan.setGfName(gname);
                scan.setGfTaxno(req.getTaxpayerCode());
            }
            if ("1".equals(req.getDataType())) {
                returnInfo.setData(scanList.get(0).getInvoiceAmount());
            }
            if ("2".equals(req.getDataType())) {
                returnInfo.setContent(scanList);
            }
            if ("3".equals(req.getDataType())) {
                returnInfo.setData(scanList.get(0).getInvoiceAmount());
                returnInfo.setContent(scanList);
            }
            returnInfo.setCode("0000");
            returnInfo.setMessage("处理成功");
            return JSON.toJSON(returnInfo);
        } catch (Exception e) {
            log.info("接口调用异常：{}",e);
            returnInfo.setCode("9999");
            returnInfo.setMessage("异常");
            response.setCharacterEncoding("UTF-8");
            response.setContentType("application/json; charset=utf-8");
            return JSON.toJSON(returnInfo);
        }
    }

    @Override
    public Object queryYqTabInfo(RequestInfo req, HttpServletRequest request, HttpServletResponse response) {
        // 返回信息
        ReturnInfo returnInfo = new ReturnInfo();
        List<LaterInvoice> laterList = null;
        try {
            String gname = this.getName(req.getTaxpayerCode());
            DynamicContextHolder.push("business" + DbConstant.BUSINESS_READ);
            laterList = tabDao.getLaterInvoice(req.getTaxpayerCode());
            int a = tabDao.getCount(req.getTaxpayerCode());
            for (LaterInvoice laterInvoice : laterList) {
                laterInvoice.setGfName(gname);
                laterInvoice.setGfTaxno(req.getTaxpayerCode());
            }
            if ("1".equals(req.getDataType())) {
                returnInfo.setData(String.valueOf(a));
            }
            if ("2".equals(req.getDataType())) {
                returnInfo.setContent(laterList);
            }
            if ("3".equals(req.getDataType())) {
                returnInfo.setData(String.valueOf(a));
                returnInfo.setContent(laterList);
            }
            returnInfo.setCode("0000");
            returnInfo.setMessage("处理成功");
            return JSON.toJSON(returnInfo);
        } catch (Exception e) {
            log.info("接口调用异常：{}",e);
            returnInfo.setCode("9999");
            returnInfo.setMessage("异常");
            response.setCharacterEncoding("UTF-8");
            response.setContentType("application/json; charset=utf-8");
            return JSON.toJSONString(returnInfo);
        }
    }

    @Override
    public String queryYcTabInfo(RequestInfo req, HttpServletRequest request, HttpServletResponse response) {
        DynamicContextHolder.push("business" + DbConstant.BUSINESS_READ);
        // 返回信息
        ReturnInfo returnInfo = new ReturnInfo();
        List<AbnormalInvoice> abnormalList = null;
        try {
            abnormalList = tabDao.getAbnormalInvoice(req.getTaxpayerCode());
            if ("1".equals(req.getDataType())) {
                returnInfo.setData(String.valueOf(abnormalList.size()));
            }
            if ("2".equals(req.getDataType())) {
                returnInfo.setContent(abnormalList);
            }
            if ("3".equals(req.getDataType())) {
                returnInfo.setData(String.valueOf(abnormalList.size()));
                returnInfo.setContent(abnormalList);
            }
            returnInfo.setCode("0000");
            returnInfo.setMessage("处理成功");
            return JSON.toJSONString(returnInfo);
        } catch (Exception e) {
            log.info("接口调用异常：{}",e);
            returnInfo.setCode("9999");
            returnInfo.setMessage("异常");
            response.setCharacterEncoding("UTF-8");
            response.setContentType("application/json; charset=utf-8");
            return JSON.toJSONString(returnInfo);
        }
    }

    @Override
    public String queryPuPiaoTabInfo(RequestInfo req, HttpServletRequest request, HttpServletResponse response) {
        DynamicContextHolder.push("business" + DbConstant.BUSINESS_READ);
        // 返回信息
        ReturnInfo returnInfo = new ReturnInfo();
        List<PuInvoice> puInvoiceList = null;
        try {
            Calendar cale = Calendar.getInstance();
            int year = cale.get(Calendar.YEAR);
            puInvoiceList = tabDao.getPuInvoice(req.getTaxpayerCode(), year);
            if ("1".equals(req.getDataType())) {
                returnInfo.setData(String.valueOf(puInvoiceList.size()));
            }
            if ("2".equals(req.getDataType())) {
                returnInfo.setContent(puInvoiceList);
            }
            if ("3".equals(req.getDataType())) {
                returnInfo.setData(String.valueOf(puInvoiceList.size()));
                returnInfo.setContent(puInvoiceList);
            }
            returnInfo.setCode("0000");
            returnInfo.setMessage("处理成功");
            return JSON.toJSONString(returnInfo);
        } catch (Exception e) {
            log.info("接口调用异常：{}",e);
            returnInfo.setCode("9999");
            returnInfo.setMessage("异常");
            response.setCharacterEncoding("UTF-8");
            response.setContentType("application/json; charset=utf-8");
            return JSON.toJSONString(returnInfo);
        }
    }

    public List<String> getMonth() throws ParseException {
        Calendar c = Calendar.getInstance();
        c.add(Calendar.MONTH, -5);
        // 六个月前
        String beforeSix = c.get(Calendar.YEAR) + "-" + c.get(Calendar.MONTH);
        List<String> result = new ArrayList<>();
        // 格式化为年月
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM");
        Calendar min = Calendar.getInstance();
        Calendar max = Calendar.getInstance();
        min.setTime(sdf.parse(beforeSix));
        min.set(min.get(Calendar.YEAR), min.get(Calendar.MONTH), 1);
        max.setTime(sdf.parse(sdf.format(new Date())));
        max.set(max.get(Calendar.YEAR), max.get(Calendar.MONTH), 2);
        while (min.before(max)) {
            result.add(sdf.format(min.getTime()));
            min.add(Calendar.MONTH, 1);
        }
        return result;
    }

    /**
     * 计算月环比
     *
     * @author 陈刚扬
     * @date 2020/10/30
     * @param montha
     *            环比
     * @param monthb
     *            环比
     * @return java.lang.String
     */
    public String monthBasis(String montha, String monthb) {
        NumberFormat numberFormat = NumberFormat.getInstance();
        numberFormat.setMaximumFractionDigits(2);
        double a = Double.parseDouble(montha);
        double b = Double.parseDouble(monthb);
        String c = numberFormat.format((a - b) / b * 100);
        return c + "%";
    }

    /**
     * 计算占比
     *
     * @author 陈刚扬
     * @date 2020/10/30
     * @param typea
     * @param typeb
     * @return java.lang.String
     */
    public String typeBasis(String typea, String typeb) {
        NumberFormat numberFormat = NumberFormat.getInstance();
        numberFormat.setMaximumFractionDigits(2);
        double a = Double.parseDouble(typea);
        double b = Double.parseDouble(typeb);
        String c = numberFormat.format(a / b * 100);
        return c + "%";
    }

    @Override
    public Object queryCjTaxTabInfo(RequestInfo req, HttpServletRequest request, HttpServletResponse response) {
        // 返回信息
        ReturnInfo returnInfo = new ReturnInfo();
        // 采集信息
        List<CollecTaxTabInvoice> cjList = new ArrayList<>();
        CollecTaxTabInvoice col0;
        CollecTaxTabInvoice col1;
        CollecTaxTabInvoice col2;
        CollecTaxTabInvoice col3;
        CollecTaxTabInvoice col4;
        CollecTaxTabInvoice col5;
        CollecTaxTabInvoice col6;
        String taxAmount = "";
        try {
            String gname = this.getName(req.getTaxpayerCode());
            DynamicContextHolder.push("business" + DbConstant.BUSINESS_READ);
            // 取六个月
            List<String> result = this.getMonth();
            // 取两个月，查询出汇总数据，计算月环比
            col0 = tabDao.getReTaxInvoice(req.getTaxpayerCode(), result.get(0));
            if (col0 != null) {

                col0.setGfName(gname);
                col0.setGfTaxno(req.getTaxpayerCode());
            }
            col1 = tabDao.getReTaxInvoice(req.getTaxpayerCode(), result.get(1));
            if (col1 != null) {

                col1.setGfName(gname);
                col1.setGfTaxno(req.getTaxpayerCode());
            }
            col2 = tabDao.getReTaxInvoice(req.getTaxpayerCode(), result.get(2));
            if (col2 != null) {

                col2.setGfName(gname);
                col2.setGfTaxno(req.getTaxpayerCode());
            }
            col3 = tabDao.getReTaxInvoice(req.getTaxpayerCode(), result.get(3));
            if (col3 != null) {

                col3.setGfName(gname);
                col3.setGfTaxno(req.getTaxpayerCode());
            }
            col4 = tabDao.getReTaxInvoice(req.getTaxpayerCode(), result.get(4));
            if (col4 != null) {

                col4.setGfName(gname);
                col4.setGfTaxno(req.getTaxpayerCode());
            }
            col5 = tabDao.getReTaxInvoice(req.getTaxpayerCode(), result.get(5));
            if (col5 != null) {

                col5.setGfName(gname);
                col5.setGfTaxno(req.getTaxpayerCode());
            }
            col6 = tabDao.getReTaxInvoice(req.getTaxpayerCode(), result.get(6));
            if (col6 != null) {

                col6.setGfName(gname);
                col6.setGfTaxno(req.getTaxpayerCode());
                taxAmount = col6.getTaxAmount();
            }

            if (col0 != null && col1 != null) {

                if (!"0".equals(col0.getInvoiceNum()) && !"0".equals(col1.getInvoiceNum())) {
                    String taxmon0 = this.monthBasis(col0.getTaxAmount(), col1.getTaxAmount());
                    col1.setTaxmonthPercent(taxmon0);
                } else if ("0".equals(col0.getInvoiceNum())) {
                    col1.setCollectMonth(result.get(1));
                    col1.setTaxmonthPercent("-");
                } else {
                    col1.setCollectMonth(result.get(1));
                    col1.setTaxmonthPercent("0");
                }
            }

            if (col1 != null && col2 != null) {

                if (!"0".equals(col1.getInvoiceNum()) && !"0".equals(col2.getInvoiceNum())) {
                    String taxmon1 = this.monthBasis(col1.getTaxAmount(), col2.getTaxAmount());
                    col2.setTaxmonthPercent(taxmon1);
                } else if ("0".equals(col1.getInvoiceNum())) {
                    col2.setCollectMonth(result.get(2));
                    col2.setTaxmonthPercent("-");
                } else {
                    col2.setCollectMonth(result.get(2));
                    col2.setTaxmonthPercent("0");
                }
            }
            if (col2 != null && col3 != null) {

                if (!"0".equals(col2.getInvoiceNum()) && !"0".equals(col3.getInvoiceNum())) {
                    String taxmon2 = this.monthBasis(col2.getTaxAmount(), col3.getTaxAmount());
                    col3.setTaxmonthPercent(taxmon2);
                } else if ("0".equals(col2.getInvoiceNum())) {
                    col3.setCollectMonth(result.get(3));
                    col3.setTaxmonthPercent("-");
                } else {
                    col3.setCollectMonth(result.get(3));
                    col3.setTaxmonthPercent("0");
                }
            }
            if (col3 != null && col4 != null) {

                if (!"0".equals(col3.getInvoiceNum()) && !"0".equals(col4.getInvoiceNum())) {
                    String taxmon4 = this.monthBasis(col3.getTaxAmount(), col4.getTaxAmount());
                    col4.setTaxmonthPercent(taxmon4);
                } else if ("0".equals(col3.getInvoiceNum())) {
                    col4.setCollectMonth(result.get(4));
                    col4.setTaxmonthPercent("-");
                } else {
                    col4.setCollectMonth(result.get(4));
                    col4.setTaxmonthPercent("0");
                }
            }
            if (col4 != null && col5 != null) {

                if (!"0".equals(col4.getInvoiceNum()) && !"0".equals(col5.getInvoiceNum())) {
                    String taxmon5 = this.monthBasis(col4.getTaxAmount(), col5.getTaxAmount());
                    col5.setTaxmonthPercent(taxmon5);
                } else if ("0".equals(col5.getInvoiceNum())) {
                    col5.setCollectMonth(result.get(5));
                    col5.setTaxmonthPercent("-");
                } else {
                    col5.setCollectMonth(result.get(5));
                    col5.setTaxmonthPercent("0");
                }
            }
            if (col5 != null && col6 != null) {

                if (!"0".equals(col5.getInvoiceNum()) && !"0".equals(col6.getInvoiceNum())) {
                    String taxmon6 = this.monthBasis(col5.getTaxAmount(), col6.getTaxAmount());
                    col6.setTaxmonthPercent(taxmon6);
                } else if ("0".equals(col5.getInvoiceNum())) {
                    col6.setCollectMonth(result.get(6));
                    col6.setTaxmonthPercent("-");
                } else {
                    col6.setCollectMonth(result.get(6));
                    col6.setTaxmonthPercent("0");
                }
            }
            if (col6 != null) {
                cjList.add(col6);
            }
            if (col5 != null) {
                cjList.add(col5);
            }
            if (col4 != null) {
                cjList.add(col4);
            }
            if (col3 != null) {
                cjList.add(col3);
            }
            if (col2 != null) {
                cjList.add(col2);
            }
            if (col1 != null) {
                cjList.add(col1);
            }
            if ("1".equals(req.getDataType())) {
                returnInfo.setData(taxAmount);
            }
            if ("2".equals(req.getDataType())) {
                returnInfo.setContent(cjList);
            }
            if ("3".equals(req.getDataType())) {
                returnInfo.setData(taxAmount);
                returnInfo.setContent(cjList);
            }

            returnInfo.setCode("0000");
            returnInfo.setMessage("处理成功");

            return JSON.toJSON(returnInfo);
        } catch (Exception e) {
            log.info("接口调用异常：{}",e);
            returnInfo.setCode("9999");
            returnInfo.setMessage("异常");
            return JSON.toJSON(returnInfo);
        }

    }

    @Override
    public Object queryQsTaxTabInfo(RequestInfo req, HttpServletRequest request, HttpServletResponse response) {
        // 返回信息
        ReturnInfo returnInfo = new ReturnInfo();
        // 签收信息
        List<InvoiceScan> scanList = null;
        try {
            String gname = this.getName(req.getTaxpayerCode());
            // 取六个月
            List<String> result = this.getMonth();
            DynamicContextHolder.push("business" + DbConstant.BUSINESS_READ);
            scanList = tabDao.getQsList(req.getTaxpayerCode(), result);
            if (scanList!=null && scanList.size()>0) {
                for (InvoiceScan scan : scanList) {
                    scan.setGfName(gname);
                    scan.setGfTaxno(req.getTaxpayerCode());
                }
                if ("1".equals(req.getDataType())) {
                    returnInfo.setData(scanList.get(0).getTaxAmount());
                }
                if ("2".equals(req.getDataType())) {
                    returnInfo.setContent(scanList);
                }
                if ("3".equals(req.getDataType())) {
                    returnInfo.setData(scanList.get(0).getTaxAmount());
                    returnInfo.setContent(scanList);
                }
            }

            returnInfo.setCode("0000");
            returnInfo.setMessage("处理成功");
            return JSON.toJSON(returnInfo);
        } catch (Exception e) {
            log.info("接口调用异常：{}",e);
            returnInfo.setCode("9999");
            returnInfo.setMessage("异常");
            response.setCharacterEncoding("UTF-8");
            response.setContentType("application/json; charset=utf-8");
            return JSON.toJSON(returnInfo);
        }
    }

    public String getName(String taxno) {
        DynamicContextHolder.push(DbConstant.BASICS_READ);
        return tabDao.getName(taxno);
    }
}
