package com.dxhy.erp.service.impl;

import com.alibaba.fastjson.JSONObject;
import com.alibaba.fastjson.serializer.SerializerFeature;
import com.dxhy.common.constant.DbConstant;
import com.dxhy.common.datasource.config.DynamicContextHolder;
import com.dxhy.erp.dao.AllTaxesDao;
import com.dxhy.erp.entity.alltax.*;
import com.dxhy.erp.service.AllTaxesService;
import com.dxhy.erp.utils.IdUtils;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.math.BigDecimal;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.List;

/**
 * @author luxiaoxu
 */
@SuppressWarnings({"AlibabaUndefineMagicConstant", "AlibabaMethodTooLong"})
@Service("allTaxesService")
@Slf4j
public class AllTaxesServiceImpl implements AllTaxesService {
    @Resource
    private AllTaxesDao allTaxesDao;
    @Value("${dxhy.datasource.dxhy_detail:dxhy_detail}")
    private String dxhyDetail;

    @Override
    public String getInvoice(String params, TaxesRequestInfo req) throws UnsupportedEncodingException, IOException {
        AllTaxReturnInfo returnInfo = new AllTaxReturnInfo();
        try {
            String type = req.getInformType();
            String keyword = req.getKeyword();
            // 类型为空，失败 类型为2，关键字为空 失败。
            log.info("线程{},通知类型{}", Thread.currentThread().getId(), type);
            if (StringUtils.isEmpty(type)) {
                returnInfo.setCode("0001");
                returnInfo.setMessage("通知失败");
                return JSONObject.toJSONString(returnInfo, SerializerFeature.WriteMapNullValue);
            } else {
                if ("2".equals(type) && StringUtils.isEmpty(keyword)) {
                    returnInfo.setCode("0001");
                    returnInfo.setMessage("通知失败");
                    return JSONObject.toJSONString(returnInfo, SerializerFeature.WriteMapNullValue);
                }
                returnInfo.setCode("0000");
                returnInfo.setMessage("通知成功");
                return JSONObject.toJSONString(returnInfo, SerializerFeature.WriteMapNullValue);
            }
        } catch (Exception e) {
            returnInfo.setCode("0001");
            returnInfo.setMessage("通知失败");
            return JSONObject.toJSONString(returnInfo, SerializerFeature.WriteMapNullValue);
        }
    }

    @Override
    public String getInvoiceSign(TaxesRequestSign req) throws UnsupportedEncodingException, IOException {
        // 完成标识
        int flag = 1;
        ReturnSign returnSign = new ReturnSign();
        List<Params> taxnoList = req.getParam();
        List<SignInfo> signList = new ArrayList<SignInfo>();
        String majorIntoWhereDb = "";
        String majorIntoWhereTable = "";
        String detailIntoWhereDb = "";
        String detailIntoWhereTable = "";
        try {
            if ("1".equals(req.getInformType())) {
                for (Params params : taxnoList) {
                    SignInfo signInfo = new SignInfo();
                    DynamicContextHolder.push(DbConstant.BASICS_READ);
                    JSONObject deptInfo = allTaxesDao.getBlocInfoBytaxNo(params.getTaxNumber());
                    log.info("线程{},获取完成标识税号{}", Thread.currentThread().getId(), params.getTaxNumber());
                    if (deptInfo == null) {
                        flag = 2;
                        signInfo.setCompleteFlag(flag);
                        signInfo.setTaxNumber(params.getTaxNumber());
                        signInfo.setLegalizeBelongDate(params.getLegalizeBelongDate());
                        signInfo.setConfirmType(params.getConfirmType());
                        signInfo.setDeptName(params.getDeptName());
                        signList.add(signInfo);
                    } else {
                        // 主体数据入库标识
                        String dbName = deptInfo.getString("db_name");
                        // 明细数据入库标识
                        DynamicContextHolder.push(dbName + DbConstant.BUSINESS_READ);
                        // try {
                        int count = allTaxesDao.getCountSign(params.getTaxNumber(), params.getLegalizeBelongDate(),
                            params.getDeptName(), params.getConfirmType());
                        // 若count大于0,则返回 已完成标识、否则为未完成
                        if (count == 2) {
                            flag = 2;
                            signInfo.setCompleteFlag(flag);
                            signInfo.setTaxNumber(params.getTaxNumber());
                            signInfo.setLegalizeBelongDate(params.getLegalizeBelongDate());
                            signInfo.setConfirmType(params.getConfirmType());
                            signInfo.setDeptName(params.getDeptName());
                        } else {
                            flag = 1;
                            signInfo.setCompleteFlag(flag);
                            signInfo.setTaxNumber(params.getTaxNumber());
                            signInfo.setLegalizeBelongDate(params.getLegalizeBelongDate());
                            signInfo.setConfirmType(params.getConfirmType());
                            signInfo.setDeptName(params.getDeptName());
                            JSONObject jsonNorDate = allTaxesDao.listCounts(params.getTaxNumber(),
                                params.getLegalizeBelongDate(), "1", params.getDeptName(), params.getConfirmType());
                            JSONObject jsonAbnorDate = allTaxesDao.listCounts(params.getTaxNumber(),
                                params.getLegalizeBelongDate(), "2", params.getDeptName(), params.getConfirmType());
                            if (jsonNorDate == null || jsonAbnorDate == null) {
                                TaxesRequestInfo taxesRequestInfo = new TaxesRequestInfo();
                                List<Params> list = new ArrayList<>();
                                list.add(params);
                                taxesRequestInfo.setInformType("1");
                                taxesRequestInfo.setParam(list);
                                this.count(taxesRequestInfo);
                            }
                        }

                        signList.add(signInfo);
                    }
                }
            } else if ("2".equals(req.getInformType())) {
                for (Params params : taxnoList) {
                    SignInfo signInfo = new SignInfo();
                    DynamicContextHolder.push(DbConstant.BASICS_READ);
                    JSONObject deptInfo = allTaxesDao.getBlocInfoBytaxNo(params.getTaxNumber());
                    if (deptInfo == null) {
                        flag = 2;
                        signInfo.setCompleteFlag(flag);
                        signInfo.setTaxNumber(params.getTaxNumber());
                        signInfo.setLegalizeBelongDate(params.getLegalizeBelongDate());
                        signInfo.setConfirmType(params.getConfirmType());
                        signInfo.setDeptName(params.getDeptName());
                        signList.add(signInfo);
                    } else {
                        // 主体数据入库标识
                        String dbName = deptInfo.getString("db_name");
                        DynamicContextHolder.push(dbName + DbConstant.BUSINESS_READ);
                        try {
                            int count = allTaxesDao.getCountInvoice(params.getTaxNumber(),
                                params.getLegalizeBelongDate(), params.getDeptName(), params.getConfirmType());
                            // 若count大于0,则返回 已完成标识、否则为未完成
                            if (count > 0) {
                                flag = 2;
                                signInfo.setCompleteFlag(flag);
                                signInfo.setTaxNumber(params.getTaxNumber());
                                signInfo.setLegalizeBelongDate(params.getLegalizeBelongDate());
                                signInfo.setConfirmType(params.getConfirmType());
                                signInfo.setDeptName(params.getDeptName());
                            } else {
                                flag = 1;
                                signInfo.setCompleteFlag(flag);
                                signInfo.setTaxNumber(params.getTaxNumber());
                                signInfo.setLegalizeBelongDate(params.getLegalizeBelongDate());
                                signInfo.setConfirmType(params.getConfirmType());
                                signInfo.setDeptName(params.getDeptName());

                            }
                        } catch (Exception e) {
                            flag = 3;
                            signInfo.setCompleteFlag(flag);
                            signInfo.setTaxNumber(params.getTaxNumber());
                            signInfo.setLegalizeBelongDate(params.getLegalizeBelongDate());
                            signInfo.setConfirmType(params.getConfirmType());
                            signInfo.setDeptName(params.getDeptName());
                        }
                        signList.add(signInfo);
                    }

                }
            } else {
                return null;
            }
            returnSign.setInformType(req.getInformType());
            returnSign.setCode("0000");
            returnSign.setMessage("获取成功");
            returnSign.setParam(signList);
            return JSONObject.toJSONString(returnSign, SerializerFeature.WriteMapNullValue);
        } catch (Exception e) {
            e.printStackTrace();
            returnSign.setInformType(req.getInformType());
            returnSign.setCode("0001");
            returnSign.setMessage("获取失败");
            returnSign.setParam(null);
            return JSONObject.toJSONString(returnSign, SerializerFeature.WriteMapNullValue);
        }
    }

    @Override
    public void count(TaxesRequestInfo req) {
        String dbName = "";
        String majorIntoWhereTable = "";
        String detailIntoWhereDb = "";
        String detailIntoWhereTable = "";

        // 汇总
        try {
            if ("1".equals(req.getInformType())) {
                List<Params> taxnoList = req.getParam();
                for (Params params : taxnoList) {
                    DynamicContextHolder.push(DbConstant.BASICS_READ);
                    JSONObject deptInfo = allTaxesDao.getBlocInfoBytaxNo(params.getTaxNumber());
                    log.info("线程{}汇总业务开始{}", Thread.currentThread().getId(), params.getTaxNumber());
                    if (deptInfo == null) {
                        log.info("线程{}税号未配置{}", Thread.currentThread().getId(), params.getTaxNumber());
                        continue;

                    }
                    dbName = deptInfo.getString("db_name");
                    DynamicContextHolder.push(dbName + DbConstant.BUSINESS_READ);
                    List<String> monthList = new ArrayList();
                    SummaryData normalcount = new SummaryData();
                    SummaryData abnormalcount = new SummaryData();
                    // 判断是否为季度，包含Q
                    if (params.getLegalizeBelongDate().indexOf("Q") < 0) {
                        monthList.add(params.getLegalizeBelongDate());
                        // 查当前属期认证发票份数(正常)
                        normalcount = allTaxesDao.getOneCount(params.getTaxNumber(), monthList, params.getDeptName(),
                            params.getConfirmType());
                        // 查当前属期认证发票份数(异常)
                        abnormalcount = allTaxesDao.getTwoCount(params.getTaxNumber(), monthList, params.getDeptName(),
                            params.getConfirmType());
                        log.info("线程{},税号{},正常份数{},异常份数{}", Thread.currentThread().getId(), params.getTaxNumber(),
                            normalcount, abnormalcount);
                    } else {
                        // 截取季度的 年属性
                        String allYear =
                            params.getLegalizeBelongDate().substring(0, params.getLegalizeBelongDate().indexOf("Q"));
                        // 截取季度的 月属性
                        String allMonth = params.getLegalizeBelongDate().substring(
                            params.getLegalizeBelongDate().indexOf("Q") + 1, params.getLegalizeBelongDate().length());
                        if ("1".equals(allMonth)) {
                            monthList.add(allYear.concat("01"));
                            monthList.add(allYear.concat("02"));
                            monthList.add(allYear.concat("03"));
                        }
                        if ("2".equals(allMonth)) {
                            monthList.add(allYear.concat("04"));
                            monthList.add(allYear.concat("05"));
                            monthList.add(allYear.concat("06"));
                        }
                        if ("3".equals(allMonth)) {
                            monthList.add(allYear.concat("07"));
                            monthList.add(allYear.concat("08"));
                            monthList.add(allYear.concat("09"));
                        }
                        if ("4".equals(allMonth)) {
                            monthList.add(allYear.concat("10"));
                            monthList.add(allYear.concat("11"));
                            monthList.add(allYear.concat("12"));
                        }
                        // 查季度认证发票份数(正常)
                        normalcount = allTaxesDao.getOneCount(params.getTaxNumber(), monthList, params.getDeptName(),
                            params.getConfirmType());
                        // 查季度认证发票份数(异常)
                        abnormalcount = allTaxesDao.getTwoCount(params.getTaxNumber(), monthList, params.getDeptName(),
                            params.getConfirmType());
                    }

                    String sign = "1";
                    int count = allTaxesDao.getAllCount(params.getTaxNumber(), params.getLegalizeBelongDate(),
                        params.getDeptName(), params.getConfirmType());
                    DynamicContextHolder.push(dbName + DbConstant.BUSINESS_WRITE);
                    if (count > 0) {
                        allTaxesDao.deleteCount(params.getTaxNumber(), params.getLegalizeBelongDate(),
                            params.getDeptName(), params.getConfirmType());
                        log.info("线程{}汇总表数据删除{}", Thread.currentThread().getId(), params.getTaxNumber());
                    }
                    normalcount.setStatus("1");

                    int normalSign = allTaxesDao.insertCount(normalcount, IdUtils.nextId(), params, sign,
                        params.getDeptName(), params.getConfirmType());
                    log.info("线程{},正常数据汇总完成{},参数{}", Thread.currentThread().getId(), normalSign,
                        params.getTaxNumber() + params.getLegalizeBelongDate());
                    abnormalcount.setStatus("2");
                    int abnormalSign = allTaxesDao.insertCount(abnormalcount, IdUtils.nextId(), params, sign,
                        params.getDeptName(), params.getConfirmType());
                    log.info("线程{},异常数据汇总完成{},参数{}", Thread.currentThread().getId(), abnormalSign,
                        params.getTaxNumber() + params.getLegalizeBelongDate());
                }
            }

            // 查询关键字发票
            if ("2".equals(req.getInformType())) {
                List<Params> taxnoList = req.getParam();
                for (Params params : taxnoList) {
                    DynamicContextHolder.push(DbConstant.BASICS_READ);
                    JSONObject deptInfo = allTaxesDao.getBlocInfoBytaxNo(params.getTaxNumber());
                    log.info("线程{}查询关键字发票开始{}", Thread.currentThread().getId(), params.getTaxNumber());
                    if (deptInfo == null) {
                        continue;
                    }
                    dbName = deptInfo.getString("db_name");
                    DynamicContextHolder.push(dbName + DbConstant.BUSINESS_READ);
                    List<AllTaxesInfo> allList = new ArrayList<AllTaxesInfo>();
                    List<CompInputInvoice> comList = new ArrayList<CompInputInvoice>();
                    allList = allTaxesDao.getAllList(req.getKeyword(), params.getTaxNumber(),
                        params.getLegalizeBelongDate(), params.getDeptName(), params.getConfirmType(), dxhyDetail);
                    int c = allTaxesDao.getCountAdvertising(params.getTaxNumber(), params.getLegalizeBelongDate(),
                        params.getDeptName(), params.getConfirmType());
                    DynamicContextHolder.push(dbName + DbConstant.BUSINESS_WRITE);
                    if (c > 0) {
                        // 删除现有记录
                        allTaxesDao.delAllTaxesInvoice(req.getKeyword(), params.getTaxNumber(),
                            params.getLegalizeBelongDate(), params.getDeptName(), params.getConfirmType());
                    }
                    if (allList != null && allList.size() > 0) {
                        for (AllTaxesInfo list : allList) {
                            CompInputInvoice com = this.toEntity(list, req.getKeyword());
                            comList.add(com);
                        }
                        // 插入记录表
                        String sign = "1";
                        allTaxesDao.insertAllTaxesInvoice(comList, sign, params.getTaxNumber(),
                            params.getLegalizeBelongDate(), params.getDeptName(), params.getConfirmType());
                    } else {
                        // 插入记录表
                        String sign = "1";
                        allTaxesDao.insertAllTaxesInvoiceNoData(sign, params.getTaxNumber(),
                            params.getLegalizeBelongDate(), req.getKeyword(), params.getDeptName(),
                            params.getConfirmType());
                    }
                }
            }

        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    private CompInputInvoice toEntity(AllTaxesInfo list, String keyword) throws ParseException {
        CompInputInvoice com = new CompInputInvoice();
        com.setKeyword(keyword);
        if (StringUtils.isEmpty(list.getInvoiceType())) {
            com.setInvoiceType("");
        } else {
            com.setInvoiceType(exchangeInvoiceType(list.getInvoiceStatus(), list.getSumAmount()));
        }
        com.setInvoiceCode(list.getInvoiceCode());
        com.setInvoiceNo(list.getInvoiceNo());
        com.setInvoiceDate(new SimpleDateFormat("yyyy-MM-dd").format(list.getInvoiceDate()));
        if (list.getCancelDate() == null) {
            com.setCancelDate("");
        } else {
            com.setCancelDate(new SimpleDateFormat("yyyy-MM-dd").format(list.getCancelDate()));
        }
        com.setSalerTaxNo(list.getSalerTaxNo());
        com.setSalerName(list.getSalerName());
        com.setSalerAddressPhone(list.getSalerAddressPhone());
        com.setBuyerTaxNo(list.getBuyerTaxNo());
        if (StringUtils.isEmpty(list.getBuyerName())) {
            com.setBuyerName("");
        } else {
            com.setBuyerName(list.getBuyerName());
        }
        if (StringUtils.isEmpty(list.getBuyerAddressPhone())) {
            com.setBuyerAddressPhone("");
        } else {
            com.setBuyerAddressPhone(list.getBuyerAddressPhone());
        }
        com.setSumAmount(list.getSumAmount());
        com.setSumTaxAmount(list.getSumTaxAmount());
        if (list.getSumPriceTax() == null) {
            com.setSumPriceTax(null);
        } else {
            com.setSumPriceTax(list.getSumPriceTax());
        }
        if (StringUtils.isEmpty(list.getBlueInvoiceCode())) {
            com.setBlueInvoiceCode("");
        } else {
            com.setBlueInvoiceCode(list.getBlueInvoiceCode());
        }
        if (StringUtils.isEmpty(list.getBlueInvoiceNo())) {
            com.setBlueInvoiceNo("");
        } else {
            com.setBlueInvoiceNo(list.getBlueInvoiceNo());
        }
        if (StringUtils.isEmpty(list.getReplaceTaxpayerCode())) {
            com.setReplaceTaxpayerCode("");
        } else {
            com.setReplaceTaxpayerCode(list.getReplaceTaxpayerCode());
        }
        if (StringUtils.isEmpty(list.getReplaceCompanyName())) {
            com.setReplaceCompanyName("");
        } else {
            com.setReplaceCompanyName(list.getReplaceCompanyName());
        }
        if (list.getAuthDate() == null) {
            com.setAuthDate("");
        } else {
            com.setAuthDate(new SimpleDateFormat("yyyy-MM-dd").format(list.getAuthDate()));
        }
        com.setAuthPeriod(
            new SimpleDateFormat("yyyy-MM").format(new SimpleDateFormat("yyyyMM").parse(list.getAuthPeriod())));
        com.setInvoiceKindCode(exchangeInvoiceKindCode(list.getInvoiceType()));
        if (StringUtils.isEmpty(list.getEquipmentNo())) {
            com.setEquipmentNo("");
        } else {
            com.setEquipmentNo(list.getEquipmentNo());
        }
        com.setCancelMark(String.valueOf(list.getCancelMark()));
        com.setAuthState(String.valueOf(list.getAuthState()));
        if (StringUtils.isEmpty(list.getDrawer())) {
            com.setDrawer("");
        } else {
            com.setDrawer(list.getDrawer());
        }
        if (StringUtils.isEmpty(list.getPayee())) {
            com.setPayee("");
        } else {
            com.setPayee(list.getPayee());
        }
        if (StringUtils.isEmpty(list.getReviewer())) {
            com.setReviewer("");
        } else {
            com.setReviewer(list.getReviewer());
        }
        if (StringUtils.isEmpty(list.getRemark())) {
            com.setRemark("");
        } else {
            com.setRemark(list.getRemark());
        }
        return com;
    }

    private String exchangeInvoiceKindCode(String invoiceType) {
        if ("01".equals(invoiceType)) {
            return "0";
        } else if ("04".equals(invoiceType)) {
            return "2";
        } else if ("11".equals(invoiceType)) {
            return "41";
        } else if ("10".equals(invoiceType)) {
            return "51";
        }
        return "";
    }

    private String exchangeInvoiceType(String invoiceStatus, BigDecimal sumAmount) {
        if (sumAmount.compareTo(new BigDecimal("0")) > 0 && !"2".equals(invoiceStatus)) {
            return "0";
        } else if (sumAmount.compareTo(new BigDecimal("0")) < 0 && !"2".equals(invoiceStatus)) {
            return "1";
        } else if (sumAmount.compareTo(new BigDecimal("0")) > 0 && "2".equals(invoiceStatus)) {
            return "3";
        } else if (sumAmount.compareTo(new BigDecimal("0")) < 0 && "2".equals(invoiceStatus)) {
            return "4";
        }
        return null;
    }

    @Override
    public String getInvoiceByKey(ReqInvoiceInfo req) throws UnsupportedEncodingException, IOException {
        List<CompInputInvoice2> list = null;
        ReturnInvoiceInfo returnInfo = new ReturnInvoiceInfo();
        List<RetcomInvoice> relist = new ArrayList<RetcomInvoice>();
        if (req.getCurrPage() == null || req.getPageSize() == null) {
            returnInfo.setCode("0001");
            returnInfo.setMessage("拉取失败");
            if (req.getCurrPage() == null) {
                returnInfo.setCurrPage(0);
            } else {
                returnInfo.setCurrPage(req.getCurrPage());
            }
            if (req.getPageSize() == null) {
                returnInfo.setPageSize(0);
            } else {
                returnInfo.setPageSize(req.getPageSize());
            }
            returnInfo.setTotalPage(0);
            returnInfo.setTotalPage(0);
            returnInfo.setData(null);
            return JSONObject.toJSONString(returnInfo, SerializerFeature.WriteMapNullValue);
        }
        int count = 0;
        try {
            String dbName = "";
            DynamicContextHolder.push(DbConstant.BASICS_READ);
            JSONObject deptInfo = allTaxesDao.getBlocInfoBytaxNo(req.getTaxNumber());
            if (deptInfo != null) {
                // 主体数据入库标识
                dbName = deptInfo.getString("db_name");
                DynamicContextHolder.push(dbName + DbConstant.BUSINESS_READ);
                count = allTaxesDao.getInvoiceCount(req.getCurrPage(), exchangeBelongDate(req.getLegalizeBelongDate()),
                    req.getPageSize(), req.getTaxNumber(), req.getDeptName(), req.getConfirmType());
                if (count > 0) {
                    list = allTaxesDao.getInvoiceList((req.getCurrPage() - 1) * req.getPageSize(),
                        exchangeBelongDate(req.getLegalizeBelongDate()), req.getPageSize(), req.getTaxNumber(),
                        req.getDeptName(), req.getConfirmType());
                    for (CompInputInvoice2 info : list) {
                        RetcomInvoice retcomInvoice = new RetcomInvoice();
                        String uuid = info.getInvoiceCode() + info.getInvoiceNo();
                        List<CompInputInvoiceDetail> detailList = allTaxesDao.getInvoiceDetailList(uuid, dxhyDetail);
                        retcomInvoice.setCompInputInvoice(info);
                        retcomInvoice.setCompInputInvoiceDetail(detailList);
                        relist.add(retcomInvoice);
                    }
                }
            }

            int recount = count;
            int repagesize = req.getPageSize();
            int retotalpage = recount % repagesize == 0 ? (recount / repagesize) : (recount / repagesize) + 1;
            returnInfo.setCode("0000");
            returnInfo.setMessage("拉取成功");
            returnInfo.setCurrPage(req.getCurrPage());
            returnInfo.setPageSize(req.getPageSize());
            returnInfo.setTotalPage(retotalpage);
            returnInfo.setTotalCount(count);
            returnInfo.setData(relist);
            return JSONObject.toJSONString(returnInfo, SerializerFeature.WriteMapNullValue);
        } catch (Exception e) {
            e.printStackTrace();
            returnInfo.setCode("0001");
            returnInfo.setMessage("拉取失败");
            returnInfo.setCurrPage(req.getCurrPage());
            returnInfo.setPageSize(req.getPageSize());
            returnInfo.setTotalPage(0);
            returnInfo.setTotalPage(0);
            returnInfo.setData(null);
            return JSONObject.toJSONString(returnInfo, SerializerFeature.WriteMapNullValue);
        }
    }

    private String exchangeBelongDate(String legalizeBelongDate) throws ParseException {
        return new SimpleDateFormat("yyyy-MM").format(new SimpleDateFormat("yyyyMM").parse(legalizeBelongDate));
    }

    @Override
    public String getInvoiceCount(List<ReqCountInfo> reqList) throws UnsupportedEncodingException, IOException {
        ReturnCount returnInfo = new ReturnCount();
        List<Data> listData = new ArrayList<Data>();
        String dbName = "";
        try {
            for (ReqCountInfo info : reqList) {
                List<SummaryData> list = new ArrayList<SummaryData>();
                DynamicContextHolder.push(DbConstant.BASICS_READ);
                JSONObject deptInfo = allTaxesDao.getBlocInfoBytaxNo(info.getTaxNumber());
                Data data = new Data();
                if (deptInfo != null) {
                    // 主体数据入库标识
                    dbName = deptInfo.getString("db_name");

                    DynamicContextHolder.push(dbName + DbConstant.BUSINESS_READ);
                    list = allTaxesDao.getOneCountData(info.getLegalizeBelongDate(), info.getTaxNumber(),
                        info.getDeptName(), info.getConfirmType());
                    if (list != null) {
                        data.setSummaryData(list);
                        data.setTaxNumber(info.getTaxNumber());
                        data.setLegalizeBelongDate(info.getLegalizeBelongDate());
                        data.setDeptName(info.getDeptName());
                        data.setConfirmType(info.getConfirmType());
                        listData.add(data);
                    }
                } else {
                    data.setSummaryData(list);
                    data.setTaxNumber(info.getTaxNumber());
                    data.setLegalizeBelongDate(info.getLegalizeBelongDate());
                    data.setDeptName(info.getDeptName());
                    data.setConfirmType(info.getConfirmType());
                    listData.add(data);
                }

            }
            returnInfo.setData(listData);
            returnInfo.setCode("0000");
            returnInfo.setMessage("拉取成功");
            return JSONObject.toJSONString(returnInfo, SerializerFeature.WriteMapNullValue);
        } catch (Exception e) {
            e.printStackTrace();
            returnInfo.setCode("0001");
            returnInfo.setMessage("拉取失败");
            returnInfo.setData(null);
            return JSONObject.toJSONString(returnInfo, SerializerFeature.WriteMapNullValue);
        }
    }

}
