package com.dxhy.extend.service.pool;

import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.dxhy.common.constant.DictConstant;
import com.dxhy.common.service.DictdetaServcice;
import com.dxhy.extend.dao.SysAuthDao;
import com.dxhy.extend.entity.*;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

import com.alibaba.fastjson.JSONObject;
import com.dxhy.common.constant.DbConstant;
import com.dxhy.common.datasource.config.DynamicContextHolder;
import com.dxhy.common.enums.*;
import com.dxhy.common.service.impl.MpBaseServiceImpl;
import com.dxhy.common.util.AmountFormatUtil;
import com.dxhy.common.util.MathUtil;
import com.dxhy.extend.dao.ExtInvoicePoolDao;
import com.dxhy.extend.util.InvoiceTypeUtils;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;

import javax.annotation.Resource;

/**
 * @author dxhy
 */
@Slf4j
@SuppressWarnings({"AlibabaUndefineMagicConstant", "AlibabaMethodTooLong"})
@Service
public class ExtInvoicePoolServiceImpl extends MpBaseServiceImpl<ExtInvoicePoolDao, ExtAddTaxInvoice>
    implements ExtInvoicePoolService {
    @Value("${dxhy.datasource.dxhy_admin:dxhy_admin}")
    private String dxhyAdmin;
    @Value("${dxhy.datasource.dxhy_tertiary:dxhy_tertiary}")
    private String dxhyTertiary;
    @Value("${dxhy.datasource.dxhy_detail:dxhy_detail}")
    private String dxhyDetail;

    @Resource
    private DictdetaServcice dictdetaServcice;

    @Resource
    private SysAuthDao sysAuthDao;

    @Override
    public String listAddTax(Map<String, Object> pramsMap, int curr, int size) {
        long enter = System.currentTimeMillis();
        JSONObject json = new JSONObject();
        DynamicContextHolder.push(pramsMap.get("dbName") + DbConstant.BUSINESS_READ);
        try {
            String[] titlelist = {"序号", "发票类型", "发票代码", "发票号码", "发票状态", "开票日期", "销方名称", "销方税号", "金额", "税额", "有效税额",
                "价税合计", "认证状态", "认证类型", "认证日期", "认证方式", "认证人", "采集状态", "底账库采集时间", "采集来源", "签收方式", "签收状态", "签收日期", "签收人",
                "入账月份", "凭证号", "过账时间", "影像号", "账期","是否末次摊销", "摊销金额", "剩余金额", "业务单号", "入池时间","报账单号","报账人","入账状态","所属组织"};

            String[] titleprop = {"xh", "invoiceType", "invoiceCode", "invoiceNo", "invoiceStatus", "invoiceDate",
                "xfmc", "xfsh", "invoiceAmount", "taxAmount", "yxse", "totalAmount", "rzzt", "rzlx", "rzrq", "rzfs",
                "rzr", "collectStatus", "collectDate", "collectFrom", "qsfs", "qszt", "qsrq", "qsr", "inAccountMonth",
                 "snVoucherNumber","postingTime","imageNumber","accountPeriod", "amortizeStatus", "amortizeAmount", "remainAmortizeAmount",
                "documentNumber", "createDate","bzdh","bzr","inAccountStatus","compCode"};
            // 需要设置返回列表标题时调用
            json = super.setTitleList(titlelist, titleprop);
            PageHelper.startPage(curr, size);
            // 合计信息,仅第一页时需要统计，减少数据库压力
            if (curr == 1) {
                String hjsl = "0";
                String hjje = "0";
                String hjse = "0";
                List<ExtAddTaxInvoice> hjList = super.baseMapper.selectAddTaxHj(pramsMap);
                if (hjList.size() > 0) {
                    ExtAddTaxInvoice hjxx = hjList.get(0);
                    hjsl = hjxx.getId();
                    if (!"0".equals(hjsl)) {
                        hjje = AmountFormatUtil.fmtMicrometer(MathUtil.round(hjxx.getInvoiceAmount()));
                        hjse = AmountFormatUtil.fmtMicrometer(MathUtil.round(hjxx.getTaxAmount()));
                    }
                }

                json.put("hjsl", hjsl);
                json.put("hjje", hjje);
                json.put("hjse", hjse);
                log.info("进项增值税票池-查询首页合计信息耗时:{}",System.currentTimeMillis()-enter);
            }
            PageHelper.startPage(curr, size);
            List<ExtAddTaxInvoice> pageList = super.baseMapper.listAddTaxInvoice(pramsMap);
            PageInfo<ExtAddTaxInvoice> pageInfo = new PageInfo<>(pageList);
            log.info("进项增值税票池-查询列表信息耗时:{}",System.currentTimeMillis()-enter);
            // 用于页面返回list对象
            if (pageList != null && pageList.size() > 0) {

                int start = (int)pageInfo.getStartRow();
                for (int i = 0; i < pageList.size(); i++) {
                    pageList.get(i).setXh(String.valueOf(i + start));
                    pageList.get(i).setInvoiceAmount(
                        AmountFormatUtil.fmtMicrometer(MathUtil.round(pageList.get(i).getInvoiceAmount())));
                    pageList.get(i).setTotalAmount(
                        AmountFormatUtil.fmtMicrometer(MathUtil.round(pageList.get(i).getTotalAmount())));
                    if (!"01".equals(pageList.get(i).getInvoiceType()) && !"03".equals(pageList.get(i).getInvoiceType())
                        && !"08".equals(pageList.get(i).getInvoiceType())
                        && !"14".equals(pageList.get(i).getInvoiceType())
                        && !"31".equals(pageList.get(i).getInvoiceType())) {
                        pageList.get(i).setYxse("--");
                    } else {
                        if (pageList.get(i).getYxse() != null && !"".equals(pageList.get(i).getYxse())) {
                            pageList.get(i)
                                .setYxse(AmountFormatUtil.fmtMicrometer(MathUtil.round(pageList.get(i).getYxse())));
                        } else {
                            pageList.get(i).setYxse(
                                AmountFormatUtil.fmtMicrometer(MathUtil.round(pageList.get(i).getTaxAmount())));
                        }
                    }
                    pageList.get(i)
                        .setTaxAmount(AmountFormatUtil.fmtMicrometer(MathUtil.round(pageList.get(i).getTaxAmount())));
                    if (pageList.get(i).getAmortizeAmount() != null
                        && !"".equals(pageList.get(i).getAmortizeAmount())) {
                        pageList.get(i).setAmortizeAmount(
                            AmountFormatUtil.fmtMicrometer(MathUtil.round(pageList.get(i).getAmortizeAmount())));
                    } else {
                        pageList.get(i).setAmortizeAmount("--");
                    }
                    if (pageList.get(i).getRemainAmortizeAmount() != null
                        && !"".equals(pageList.get(i).getRemainAmortizeAmount())) {
                        pageList.get(i).setRemainAmortizeAmount(
                            AmountFormatUtil.fmtMicrometer(MathUtil.round(pageList.get(i).getRemainAmortizeAmount())));
                    } else {
                        pageList.get(i).setRemainAmortizeAmount("--");
                    }
                    for (FpztEnum e : FpztEnum.values()) {
                        if (e.getFpztDm().equals(pageList.get(i).getInvoiceStatus())) {
                            pageList.get(i).setInvoiceStatus(e.getFpztMc(e.getIndex()));
                        }
                    }
                    for (FpqsjgEnum e : FpqsjgEnum.values()) {
                        if (e.getQsjgDm().equals(pageList.get(i).getQszt())) {
                            pageList.get(i).setQszt(e.getQsjgMc(e.getIndex()));
                        }
                    }
                    for (RzztEnum e : RzztEnum.values()) {
                        if (e.getRzztDm().equals(pageList.get(i).getRzzt())) {
                            pageList.get(i).setRzzt(e.getRzztMc(e.getIndex()));
                        }
                    }
                    for (QsfsEnum e : QsfsEnum.values()) {
                        if (e.getQsfsDm().equals(pageList.get(i).getQsfs())) {
                            pageList.get(i).setQsfs(e.getQsfsMc(e.getIndex()));
                        }
                    }
                    if ("电票上传签收".equals(pageList.get(i).getQsfs())) {
                        pageList.get(i).setQsfs("电票签收");
                    }
                    if ("接口推送".equals(pageList.get(i).getQsfs())) {
                        pageList.get(i).setQsfs("接口签收");
                    }
                    if ("1".equals(pageList.get(i).getRzlx())) {
                        pageList.get(i).setRzlx("认证抵扣");
                    } else if ("2".equals(pageList.get(i).getRzlx())) {
                        pageList.get(i).setRzlx("--");
                    } else if ("3".equals(pageList.get(i).getRzlx())) {
                        pageList.get(i).setRzlx("认证代办退税");
                    } else if ("4".equals(pageList.get(i).getRzlx())) {
                        pageList.get(i).setRzlx("认证不抵扣");
                    } else {
                        pageList.get(i).setRzlx("");
                    }
                    if ("1".equals(pageList.get(i).getInAccountStatus())) {
                        pageList.get(i).setInAccountStatus("已入账");
                    } else {
                        pageList.get(i).setInAccountStatus("未入账");
                    }
//                    else {
//                        pageList.get(i).setInAccountStatus("未入账");
//                    }

                    if ("0".equals(pageList.get(i).getCollectFrom())) {
                        pageList.get(i).setCollectFrom("进项采集");
                    } else if ("1".equals(pageList.get(i).getCollectFrom())) {
                        pageList.get(i).setCollectFrom("票据采集");
                    }
                    //报账单号字典值转换
//                    pageList.get(i).setInAccountStatus(dictdetaServcice.queryDictName(DictConstant.INACCOUNT_STATUS,pageList.get(i).getInAccountStatus()));
//                    log.info("入账状态====>{}",pageList.get(i).getInAccountStatus());
                    pageList.get(i).setInvoiceType(InvoiceTypeUtils.invoiceTypeName(pageList.get(i).getInvoiceType()));
                    pageList.get(i).setQsrq(pageList.get(i).getQsrq() == null ? "" : pageList.get(i).getQsrq());
                    pageList.get(i).setRzrq(pageList.get(i).getRzrq() == null ? "" : pageList.get(i).getRzrq());
                    pageList.get(i).setScanId(pageList.get(i).getScanId() == null ? "" : pageList.get(i).getScanId());
                    pageList.get(i).setInAccountMonth(
                        pageList.get(i).getInAccountMonth() == null ? "" : pageList.get(i).getInAccountMonth());
                    if ("0".equals(pageList.get(i).getAmortizeStatus())) {
                        pageList.get(i).setAmortizeStatus("否");
                    } else if ("1".equals(pageList.get(i).getAmortizeStatus())) {
                        pageList.get(i).setAmortizeStatus("是");
                    }
                    for (RzfsEnum e : RzfsEnum.values()) {
                        if (e.getRzfsDm().equals(pageList.get(i).getRzfs())) {
                            pageList.get(i).setRzfs(e.getRzfsMc(e.getIndex()));
                        }
                    }
                    if ("0".equals(pageList.get(i).getCollectStatus())) {
                        pageList.get(i).setCollectStatus("未采集");
                    } else if ("1".equals(pageList.get(i).getCollectStatus())) {
                        pageList.get(i).setCollectStatus("已采集");
                    }
                }
            }

            // 设置记录总数
            json.put("total", pageInfo.getTotal());
            List<ExtAddTaxInvoice> resultList = pageInfo.getList();
            json.put("datalist", resultList);
            log.info("进项增值税票池-合计耗时:{}",System.currentTimeMillis()-enter);
        } catch (Exception e) {
            // 统一设置操作异常标识
            json = super.errorResult(e);
        }
        return super.responseResult(json);
    }

    @Override
    public Object listOther(Map<String, Object> pramsMap, int curr, int size) {
        long enter = System.currentTimeMillis();
        JSONObject json = new JSONObject();
        DynamicContextHolder.push(pramsMap.get("dbName") + DbConstant.BUSINESS_READ);
        try {
            String[] titlelist =
                {"序号", "发票类型", "发票代码", "发票号码", "开票日期", "金额", "签收状态", "签收日期", "签收方式", "签收人", "业务单号"
                        , "入池时间","凭证号", "过账时间", "影像号", "账期","报账单号","报账人","入账状态","所属组织"};
            String[] titleprop = {"xh", "invoiceTypeString", "invoiceCode", "invoiceNo", "invoiceDate", "invoiceAmount",
                "qszt", "qsrq", "qsfs", "qsr", "businessDocumentNumber", "createDate","snVoucherNumber", "postingTime",
                    "imageNumber","accountPeriod","bzdh","bzr","inAccountStatus","compCode"};
            // 需要设置返回列表标题时调用
            json = super.setTitleList(titlelist, titleprop);
            PageHelper.startPage(curr, size);
            // 合计信息,仅第一页时需要统计，减少数据库压力
            if (curr == 1) {
                String hjsl = "0";
                String hjje = "0";
                String hjse = "0";
                List<ExtOtherInvoice> hjList = super.baseMapper.selectOtherHj(pramsMap);
                if (hjList.size() > 0) {
                    ExtOtherInvoice hjxx = hjList.get(0);
                    hjsl = hjxx.getId();
                    if (!"0".equals(hjsl)) {
                        hjje = AmountFormatUtil.fmtMicrometer(MathUtil.round(hjxx.getInvoiceAmount()));
                        hjse = AmountFormatUtil.fmtMicrometer(MathUtil.round(hjxx.getTaxAmount()));
                    }
                }

                json.put("hjsl", hjsl);
                json.put("hjje", hjje);
                json.put("hjse", hjse);
                log.info("其他客票-查询首页合计信息耗时:{}",System.currentTimeMillis()-enter);
            }
            PageHelper.startPage(curr, size);
            log.info("开始进行分页查询");
            List<ExtOtherInvoice> pageList = super.baseMapper.listOtherInvoice(pramsMap);
            log.info("查询结果为 {}",pageList);
            PageInfo<ExtOtherInvoice> pageInfo = new PageInfo<>(pageList);
            log.info("其他客票-查询列表信息耗时:{}",System.currentTimeMillis()-enter);
            // 用于页面返回list对象
            if (pageList != null && pageList.size() > 0) {
                int start = (int)pageInfo.getStartRow();
                for (int i = 0; i < pageList.size(); i++) {
                    pageList.get(i).setXh(String.valueOf(i + start));
                    pageList.get(i).setInvoiceAmount(
                        AmountFormatUtil.fmtMicrometer(MathUtil.round(pageList.get(i).getInvoiceAmount())));
                    for (FpqsjgEnum e : FpqsjgEnum.values()) {
                        if (e.getQsjgDm().equals(pageList.get(i).getQszt())) {
                            pageList.get(i).setQszt(e.getQsjgMc(e.getIndex()));
                        }
                    }
                    for (QsfsEnum e : QsfsEnum.values()) {
                        if (e.getQsfsDm().equals(pageList.get(i).getQsfs())) {
                            pageList.get(i).setQsfs(e.getQsfsMc(e.getIndex()));
                        }
                    }
                    if ("电票上传签收".equals(pageList.get(i).getQsfs())) {
                        pageList.get(i).setQsfs("电票签收");
                    }
                    if ("接口推送".equals(pageList.get(i).getQsfs())) {
                        pageList.get(i).setQsfs("接口签收");
                    }
                    pageList.get(i).setQsrq(pageList.get(i).getQsrq() == null ? "" : pageList.get(i).getQsrq());
                    pageList.get(i).setScanId(pageList.get(i).getScanId() == null ? "" : pageList.get(i).getScanId());
                    pageList.get(i)
                        .setInvoiceTypeString(InvoiceTypeUtils.invoiceTypeName(pageList.get(i).getInvoiceType()));

                    if ("1".equals(pageList.get(i).getInAccountStatus())){
                        pageList.get(i).setInAccountStatus("已入账");
                    }else {
                        pageList.get(i).setInAccountStatus("未入账");
                    }


//                    log.info("inAccountStatus=====>{}",pageList.get(i).getInAccountStatus());
//                    pageList.get(i).setInAccountStatus(dictdetaServcice.queryDictName(DictConstant.INACCOUNT_STATUS,
//                            pageList.get(i).getInAccountStatus()));

                }

            }

            // 设置记录总数
            json.put("total", pageInfo.getTotal());
            List<ExtOtherInvoice> resultList = pageInfo.getList();
            json.put("datalist", resultList);
            log.info("其他客票-合计耗时:{}",System.currentTimeMillis()-enter);
        } catch (Exception e) {
            // 统一设置操作异常标识
            json = super.errorResult(e);
        }

        return super.responseResult(json);
    }

    @Override
    public Object listSaleInvoice(Map<String, Object> pramsMap, int curr, int size) {
        JSONObject json = new JSONObject();
        DynamicContextHolder.push(pramsMap.get("dbName") + DbConstant.BUSINESS_READ);
        try {
            String[] titlelist = {"序号", "发票类型", "发票代码", "发票号码", "发票状态", "开票日期", "购方名称", "购方税号", "金额", "税额", "价税合计",
                "清单标志", "备注", "优惠政策类型", "采集状态", "采集时间", "采集来源", "签收方式", "对接系统", "签收状态", "签收日期", "签收人", "入账月份",
                "凭证号", "业务单号", "入池时间","影像号","账期","过账日期","报账单号","报账人","入账状态","所属组织"};

            String[] titleprop = {"xh", "invoiceType", "invoiceCode", "invoiceNo", "invoiceStatus", "invoiceDate",
                "gfmc", "gfsh", "invoiceAmount", "taxAmount", "totalAmount", "listStatus", "remark", "incentiveFlag",
                "collectStatus", "collectDate", "collectFrom", "qsfs", "systemName", "qszt", "qsrq", "qsr",
                "inAccountMonth", "snVoucherNumber", "documentNumber", "createDate","imageNumber","accountPeriod"
            ,"postingTime","bzdh","bzr","inAccountStatus","compCode"};
            // 需要设置返回列表标题时调用
            json = super.setTitleList(titlelist, titleprop);
            PageHelper.startPage(curr, size);
            pramsMap.put("dxhyDetail", dxhyDetail);
            // 合计信息,仅第一页时需要统计，减少数据库压力
            if (curr == 1) {
                String hjsl = "0";
                String hjje = "0";
                String hjse = "0";
                List<ExtAddTaxInvoice> hjList = super.baseMapper.selectSaleHj(pramsMap);
                if (hjList.size() > 0) {
                    ExtAddTaxInvoice hjxx = hjList.get(0);
                    hjsl = hjxx.getId();
                    if (!"0".equals(hjsl)) {
                        hjje = AmountFormatUtil.fmtMicrometer(MathUtil.round(hjxx.getInvoiceAmount()));
                        hjse = AmountFormatUtil.fmtMicrometer(MathUtil.round(hjxx.getTaxAmount()));
                    }
                }

                json.put("hjsl", hjsl);
                json.put("hjje", hjje);
                json.put("hjse", hjse);
            }
            PageHelper.startPage(curr, size);
            List<ExtAddTaxInvoice> pageList = super.baseMapper.listSaleInvoice(pramsMap);
            PageInfo<ExtAddTaxInvoice> pageInfo = new PageInfo<>(pageList);
            // 用于页面返回list对象
            if (pageList.size() > 0) {

                int start = (int)pageInfo.getStartRow();
                for (int i = 0; i < pageList.size(); i++) {
                    pageList.get(i).setXh(String.valueOf(i + start));
                    pageList.get(i).setInvoiceAmount(
                        AmountFormatUtil.fmtMicrometer(MathUtil.round(pageList.get(i).getInvoiceAmount())));
                    if (pageList.get(i).getYxse() != null || !"".equals(pageList.get(i).getYxse())) {
                        pageList.get(i)
                            .setYxse(AmountFormatUtil.fmtMicrometer(MathUtil.round(pageList.get(i).getYxse())));
                    }
                    pageList.get(i).setTotalAmount(
                        AmountFormatUtil.fmtMicrometer(MathUtil.round(pageList.get(i).getTotalAmount())));
                    pageList.get(i)
                        .setTaxAmount(AmountFormatUtil.fmtMicrometer(MathUtil.round(pageList.get(i).getTaxAmount())));
                    for (FpztEnum e : FpztEnum.values()) {
                        if (e.getFpztDm().equals(pageList.get(i).getInvoiceStatus())) {
                            pageList.get(i).setInvoiceStatus(e.getFpztMc(e.getIndex()));
                        }
                    }
                    for (FpqsjgEnum e : FpqsjgEnum.values()) {
                        if (e.getQsjgDm().equals(pageList.get(i).getQszt())) {
                            pageList.get(i).setQszt(e.getQsjgMc(e.getIndex()));
                        }
                    }
                    for (RzztEnum e : RzztEnum.values()) {
                        if (e.getRzztDm().equals(pageList.get(i).getRzzt())) {
                            pageList.get(i).setRzzt(e.getRzztMc(e.getIndex()));
                        }
                    }
                    for (QsfsEnum e : QsfsEnum.values()) {
                        if (e.getQsfsDm().equals(pageList.get(i).getQsfs())) {
                            pageList.get(i).setQsfs(e.getQsfsMc(e.getIndex()));
                        }
                    }
                    if ("电票上传签收".equals(pageList.get(i).getQsfs())) {
                        pageList.get(i).setQsfs("电票签收");
                    }
                    if ("接口推送".equals(pageList.get(i).getQsfs())) {
                        pageList.get(i).setQsfs("接口签收");
                    }
                    if ("1".equals(pageList.get(i).getRzlx())) {
                        pageList.get(i).setRzlx("认证抵扣");
                    } else if ("2".equals(pageList.get(i).getRzlx())) {
                        pageList.get(i).setRzlx("认证退税");
                    } else if ("3".equals(pageList.get(i).getRzlx())) {
                        pageList.get(i).setRzlx("认证代办退税");
                    } else if ("4".equals(pageList.get(i).getRzlx())) {
                        pageList.get(i).setRzlx("认证不抵扣");
                    } else {
                        pageList.get(i).setRzlx("");
                    }
                    if ("0".equals(pageList.get(i).getInAccountStatus())) {
                        pageList.get(i).setInAccountStatus("未入账");
                    } else if ("1".equals(pageList.get(i).getInAccountStatus())) {
                        pageList.get(i).setInAccountStatus("已入账");
                    } else {
                        pageList.get(i).setInAccountStatus("未入账");
                    }
                    if ("0".equals(pageList.get(i).getListStatus())) {
                        pageList.get(i).setListStatus("否");
                    } else if ("1".equals(pageList.get(i).getListStatus())) {
                        pageList.get(i).setListStatus("是");
                    } else {
                        pageList.get(i).setListStatus("");
                    }
                    pageList.get(i).setCollectFrom("票据采集");
                    if ("0".equals(pageList.get(i).getCollectStatus())) {
                        pageList.get(i).setCollectStatus("未采集");
                    } else if ("1".equals(pageList.get(i).getCollectStatus())) {
                        pageList.get(i).setCollectStatus("已采集");
                    }
                    pageList.get(i).setQsrq(pageList.get(i).getQsrq() == null ? "" : pageList.get(i).getQsrq());
                    pageList.get(i).setInAccountMonth(
                        pageList.get(i).getInAccountMonth() == null ? "" : pageList.get(i).getInAccountMonth());
                    pageList.get(i).setScanId(pageList.get(i).getScanId() == null ? "" : pageList.get(i).getScanId());
                    pageList.get(i).setInvoiceType(InvoiceTypeUtils.invoiceTypeName(pageList.get(i).getInvoiceType()));
//                    pageList.get(i).setInAccountStatus(dictdetaServcice.queryDictName(DictConstant.INACCOUNT_STATUS,
//                            pageList.get(i).getInAccountStatus()));
                }
            }

            // 设置记录总数
            json.put("total", pageInfo.getTotal());
            List<ExtAddTaxInvoice> resultList = pageInfo.getList();
            json.put("datalist", resultList);
        } catch (Exception e) {
            // 统一设置操作异常标识
            json = super.errorResult(e);
        }
        return super.responseResult(json);
    }
    @Override
    public Object selectPullRecord(PullRecordReq pullRecordReq) {
        JSONObject json = new JSONObject();
        try{
            String[] titlelist = {"公司名称", "会计凭证号码", "会计年度",
                    "账期", "过账日期","报账单号","结算单","报账单状态","借贷标识","科目编码"
                    ,"科目描述","供应商名称","供应商编码","客户编码","客户名称","物料编码","数量","单位",
                    "税码","税率","本位币借方金额","本位币贷方金额","销售订单","销售订单行","矿别编号","矿别","最后接收时间"};
            String[] titleProp = {"companyName","belnr","gjahr",
                    "monat","budat","zbzdh","zjsdh","zbzdzt","zdrcrk","racct",
                    "txt50","zname","lifnr","kunnr","zname2","matnr","msl","runit",
                    "mwskz","kbetr","zhslj","zhsld","kdauf","kdpos","kbbh","ktype","lastReceiveTime"};
            json = super.setTitleList(titlelist, titleProp);
            DynamicContextHolder.push("business_business_read");
            PageHelper.startPage(pullRecordReq.getCurr(),pullRecordReq.getSize());
            List<PullRecordModel> recordList=super.baseMapper.getPullRecord(pullRecordReq);
            PageInfo<PullRecordModel> pageInfo = new PageInfo<>(recordList);
            json.put("total",pageInfo.getTotal());
            json.put("dataList",pageInfo.getList());

        }catch (Exception e){
            e.printStackTrace();
            json = super.errorResult(e);
        }
        return super.responseResult(json);
    }

    /**
     * 摊销记录
     *
     * @param pramsMap
     *            查询条件
     * @return 返回
     */
    @Override
    public Object amortizeRecord(Map<String, Object> pramsMap) {
        JSONObject json = new JSONObject();
        DynamicContextHolder.push(pramsMap.get("dbName") + DbConstant.BUSINESS_READ);
        try {
            String[] titlelist = {"序号", "业务单号", "已摊销金额", "剩余金额", "报销人"};
            String[] titleprop = {"xh", "amortizeDocumentNumber", "amortizeAmount", "remainAmortizeAmount", "bxr"};
            // 需要设置返回列表标题时调用
            json = super.setTitleList(titlelist, titleprop);
            List<AmortizeRecordEntity> pageList = super.baseMapper.listAmortizeRecord(pramsMap);
            PageInfo<AmortizeRecordEntity> pageInfo = new PageInfo<>(pageList);
            // 用于页面返回list对象
            if (pageList != null && pageList.size() > 0) {
                for (int i = 0; i < pageList.size(); i++) {
                    pageList.get(i).setXh(String.valueOf(i + 1));
                    pageList.get(i).setAmortizeAmount(
                        AmountFormatUtil.fmtMicrometer(MathUtil.round(pageList.get(i).getAmortizeAmount())));
                    pageList.get(i).setRemainAmortizeAmount(
                        AmountFormatUtil.fmtMicrometer(MathUtil.round(pageList.get(i).getRemainAmortizeAmount())));

                }
            }
            json.put("datalist", pageList);
        } catch (Exception e) {
            // 统一设置操作异常标识
            json = super.errorResult(e);
        }
        return super.responseResult(json);
    }

    @Override
    public List<String> getGfshList(List<String> gfshList, List<String> companyCodes) {
        DynamicContextHolder.push(DbConstant.BASICS_READ);
        List<SysDeptEntity> taxnoList = sysAuthDao.queryTaxnoList();
        List<SysDeptEntity> parentIdList = sysAuthDao.queryParentIdList();

        Map<String, String> taxnoMap = taxnoList.stream().collect(Collectors.toMap(SysDeptEntity::getDeptId, SysDeptEntity::getTaxno));
        Map<String, String> parentIdMap = parentIdList.stream().collect(Collectors.toMap(SysDeptEntity::getDeptId, e -> e.getParentId() == null ? "" : e.getParentId()));

        log.info("taxnoMap={},parentIdMap={}", taxnoMap, parentIdMap);

        String gfsh = "";
        for (String companyCode : companyCodes) {
            QueryWrapper<SysDeptEntity> wrapper = new QueryWrapper<>();
            wrapper.eq("org_code", companyCode);
            List<SysDeptEntity> sysDeptEntityList = sysAuthDao.selectList(wrapper);
            SysDeptEntity sysDeptEntity = sysDeptEntityList.get(0);
            if (sysDeptEntity != null) {
                String dId = sysDeptEntity.getDeptId();
                log.info("获取deptId为:{}", dId);
                if (StringUtils.isNotEmpty(dId)) {
                    gfsh = getGfsh(dId, taxnoMap, parentIdMap);
                    if (StringUtils.isNotEmpty(gfsh)) {
                        gfshList.add(gfsh);
                    }
                }
            }
        }
        log.info("购方税号list为：{}", gfshList);


        return gfshList.stream().distinct().collect(Collectors.toList());
    }

    @Override
    public Object incentiveFlagList(String dbName) {
        JSONObject json = new JSONObject();
        DynamicContextHolder.push(dbName + DbConstant.DETAIL_READ);
        List<String> incentiveFlagList = super.baseMapper.listIncentiveFlag();
        json.put("incentiveFlagList", incentiveFlagList);
        return super.responseResult(json);
    }

    private String getGfsh(String deptId, Map<String, String> taxnoMap, Map<String, String> parentIdMap) {
        String taxno = "";
        if (StringUtils.isNotEmpty(deptId)) {
            log.info("deptId={}", deptId);
            taxno = taxnoMap.get(deptId);
            log.info("taxno={}", taxno);

            if (StringUtils.isEmpty(taxno)) {
                deptId = parentIdMap.get(deptId);
                log.info("获取map中的deptId:{}", deptId);
                taxno = getGfsh(deptId, taxnoMap, parentIdMap);
            }
        }
        return taxno;
    }


}
