package com.dxhy.base.service.tdxrecordinvoice;

import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.dxhy.base.dao.BaseTDxRecordInvoiceDao;
import com.dxhy.base.dao.BaseTDxTaxCurrentDao;
import com.dxhy.base.entity.BaseTDxDkCount;
import com.dxhy.base.entity.BaseTDxRecordInvoice;
import com.dxhy.base.entity.BaseTDxTaxCurrent;
import com.dxhy.base.model.BaseSgrzFplbRespVO;
import com.dxhy.common.constant.DbConstant;
import com.dxhy.common.datasource.config.DynamicContextHolder;
import com.dxhy.common.enums.FpqsjgEnum;
import com.dxhy.common.enums.QsfsEnum;
import com.dxhy.common.service.InvoiceLogService;
import com.dxhy.common.service.impl.MpBaseServiceImpl;
import com.dxhy.common.util.AmountFormatUtil;
import com.dxhy.common.util.MathUtil;
import com.dxhy.common.util.StringHelper;
import com.dxhy.common.utils.DateUtils;
import com.dxhy.common.vo.InvoiceLog;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.ObjectUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.math.BigDecimal;
import java.time.LocalDate;
import java.util.*;

/**
 *
 *
 * 发票认证查询业务处理类
 *
 * @author zhanghong
 * @date 2019年4月26日
 *
 */
@SuppressWarnings({"AlibabaUndefineMagicConstant", "AlibabaMethodTooLong", "AlibabaLowerCamelCaseVariableNaming",
    "AlibabaClassNamingShouldBeCamel", "AlibabaCollectionInitShouldAssignCapacity"})
@Service("baseIRzTDxRecordInvoiceService")
@Slf4j
public class BaseRzTDxRecordInvoiceServiceImpl extends MpBaseServiceImpl<BaseTDxRecordInvoiceDao, BaseTDxRecordInvoice>
    implements BaseIRzTDxRecordInvoiceService {

    @Resource
    private BaseTDxTaxCurrentDao baseTDxTaxCurrentDao;
    @Resource
    private InvoiceLogService invoiceLogService;


    @Override
    public String selectByCxtj(Map<String, Object> pramsMap, int curr, int size, int count) {

        String[] titlelist =
            {"序号", "发票代码", "发票号码", "开票日期", "购方名称", "销方名称", "金额", "税额", "签收日期", "签收状态", "签收人", "签收方式", "抵扣税额", "业务单元"};
        String[] titleprop =
            {"xh", "fpdm", "fphm", "kprq", "gfmc", "xfmc", "je", "se", "qsrq", "qszt", "qsr", "qsfs", "dkse", "ywdy"};
        String zjsl = "0";
        String zjje = "0";
        String zjse = "0";

        DynamicContextHolder.push(pramsMap.get("dbName") + DbConstant.BUSINESS_READ);

        // 需要设置返回列表标题时调用
        JSONObject json = super.setTitleList(titlelist, titleprop);

        Page<BaseTDxRecordInvoice> page = new Page<>(curr, size);

        QueryWrapper<BaseTDxRecordInvoice> queryWrapper = new QueryWrapper<>();
        queryWrapper.in("auth_status", "0", "1", "5");
        queryWrapper.in("invoice_type", "01", "03", "14");
        queryWrapper.gt("invoice_amount", "0");
        queryWrapper.ge("tax_amount", "0");
        queryWrapper.ne("rzlx", "4");
        queryWrapper.eq("rzh_yesorno", "0");

        List<String> list = (List<String>)pramsMap.get("gfsh");
        String[] values = list.toArray(new String[0]);
        queryWrapper.in("gf_tax_no", values);

        QueryWrapper<BaseTDxTaxCurrent> currentWrapper = new QueryWrapper<>();

        currentWrapper.in("taxno", values);

        BaseTDxTaxCurrent current = baseTDxTaxCurrentDao.selectOne(currentWrapper);
        if (current != null) {
            json.put("datalist", new ArrayList<>());
            // 设置记录总数
            json.put("total", 0);
            json.put("zjsl", "0");
            json.put("zjje", "0");
            json.put("zjse", "0");
            return super.responseResult(json);
        }
        queryWrapper.between("invoice_date", current.getSelectStartDate(), current.getSelectEndDate());
        if (pramsMap.containsKey("xfmc") && !StringHelper.isBlank(String.valueOf(pramsMap.get("xfmc")))) {
            queryWrapper.like("xf_name", "concat(" + pramsMap.get("xfmc") + ",'%')");
        }
        if (pramsMap.containsKey("fplx") && !StringHelper.isBlank(String.valueOf(pramsMap.get("fplx")))) {
            queryWrapper.eq("invoice_type", pramsMap.get("fplx"));
        }
        queryWrapper.between("invoice_date", pramsMap.get("ksrq"), pramsMap.get("jsrq"));
        if (pramsMap.containsKey("qszt") && !StringHelper.isBlank(String.valueOf(pramsMap.get("qszt")))) {
            queryWrapper.eq("qs_status", pramsMap.get("qszt"));
        }
        if (pramsMap.containsKey("fphm") && !StringHelper.isBlank(String.valueOf(pramsMap.get("fphm")))) {
            queryWrapper.eq("invoice_no", pramsMap.get("fphm"));
        }
        if (pramsMap.containsKey("ywdy") && !StringHelper.isBlank(String.valueOf(pramsMap.get("ywdy")))) {
            queryWrapper.eq("dept_id", pramsMap.get("ywdy"));
        }
        if (pramsMap.containsKey("qsfs") && !StringHelper.isBlank(String.valueOf(pramsMap.get("qsfs")))) {
            queryWrapper.eq("qs_type", pramsMap.get("qsfs"));
        }
        if (pramsMap.containsKey("qsrqq") && !StringHelper.isBlank(String.valueOf(pramsMap.get("qsrqq")))
            && pramsMap.containsKey("qsrqz") && !StringHelper.isBlank(String.valueOf(pramsMap.get("qsrqz")))) {
            queryWrapper.between("qs_date", pramsMap.get("qsrqq"), pramsMap.get("qsrqz"));
        }

        IPage<BaseTDxRecordInvoice> pageInfo = super.page(page, queryWrapper);
        List<BaseTDxRecordInvoice> resultList = pageInfo.getRecords();
        List<Object> returnList = new ArrayList<>();

        if (resultList != null && resultList.size() > 0) {

            int c = (int)pageInfo.getCurrent();
            int s = (int)pageInfo.getSize();
            int xh = (c - 1) * s;
            for (int i = 0; i < resultList.size(); i++) {
                zjje = MathUtil.add(zjje, resultList.get(i).getInvoiceAmount().toPlainString());
                zjse = MathUtil.add(zjse, resultList.get(i).getTaxAmount().toPlainString());
                BaseSgrzFplbRespVO fplb = this.convertToFplbView(resultList.get(i), xh + 1 + i);
                returnList.add(fplb);
            }

        }
        json.put("datalist", returnList);
        // 设置记录总数
        json.put("total", pageInfo.getTotal());
        zjje = AmountFormatUtil.fmtMicrometer(MathUtil.round(zjje));
        zjse = AmountFormatUtil.fmtMicrometer(MathUtil.round(zjse));
        zjsl = String.valueOf(pageInfo.getTotal());
        json.put("zjsl", zjsl);
        json.put("zjje", zjje);
        json.put("zjse", zjse);
        return super.responseResult(json);
    }

    private BaseSgrzFplbRespVO convertToFplbView(BaseTDxRecordInvoice tDxRecordInvoice, int i) {
        BaseSgrzFplbRespVO fpxx = new BaseSgrzFplbRespVO();
        fpxx.setId(tDxRecordInvoice.getId().toString());
        fpxx.setXh(String.valueOf(i));
        fpxx.setUuid(tDxRecordInvoice.getUuid());
        fpxx.setFpdm(tDxRecordInvoice.getInvoiceCode());
        fpxx.setFphm(tDxRecordInvoice.getInvoiceNo());
        fpxx.setGfmc(tDxRecordInvoice.getGfName());
        if (tDxRecordInvoice.getInvoiceAmount() != null) {
            fpxx.setJe(
                AmountFormatUtil.fmtMicrometer(MathUtil.round(tDxRecordInvoice.getInvoiceAmount().toPlainString())));
        } else {
            fpxx.setJe("0");
        }
        if (tDxRecordInvoice.getInvoiceDate() != null) {
            fpxx.setKprq(DateUtils.dateToStr(tDxRecordInvoice.getInvoiceDate()));
        } else {
            fpxx.setKprq("");
        }
        fpxx.setQsfs(QSFS_MAP.get(tDxRecordInvoice.getRzhType()));
        fpxx.setQsr(tDxRecordInvoice.getQsName());
        if (tDxRecordInvoice.getQsDate() != null) {
            fpxx.setQsrq(DateUtils.dateToStr(tDxRecordInvoice.getQsDate()));
        } else {
            fpxx.setQsrq("");
        }
        fpxx.setQszt(QSZT_MAP.get(tDxRecordInvoice.getQsStatus()));
        if (tDxRecordInvoice.getTaxAmount() != null) {
            fpxx.setSe(AmountFormatUtil.fmtMicrometer(MathUtil.round(tDxRecordInvoice.getTaxAmount().toPlainString())));
        } else {
            fpxx.setSe("0");
        }
        fpxx.setXfmc(tDxRecordInvoice.getXfName());
        if (tDxRecordInvoice.getDkTaxAmount() != null) {
            fpxx.setDkse(
                AmountFormatUtil.fmtMicrometer(MathUtil.round(tDxRecordInvoice.getDkTaxAmount().toPlainString())));
        } else {
            fpxx.setDkse(
                AmountFormatUtil.fmtMicrometer(MathUtil.round(tDxRecordInvoice.getTaxAmount().toPlainString())));
        }
        fpxx.setInvoiceType(tDxRecordInvoice.getInvoiceType());
        fpxx.setYwdy(tDxRecordInvoice.getDeptName());
        return fpxx;
    }

    /**
     * 签收方式统一放入map集合中
     */
    public static final Map<String, String> QSFS_MAP = new LinkedHashMap<String, String>() {

        private static final long serialVersionUID = 1L;

        {
            for (QsfsEnum en : QsfsEnum.values()) {
                this.put(en.getQsfsDm(), en.getQsfsMc(en.getIndex()));
            }
        }
    };
    /**
     * 签收状态统一放入map集合中
     */
    public static final Map<String, String> QSZT_MAP = new LinkedHashMap<String, String>() {

        private static final long serialVersionUID = 1L;

        {
            for (FpqsjgEnum en : FpqsjgEnum.values()) {
                this.put(en.getQsjgDm(), en.getQsjgMc(en.getIndex()));
            }
        }
    };

    @Override
    public String identifiByIds(Map<String, Object> pramsMap, int curr, int size, int count) {
        String gxType = (String)pramsMap.get("gxType");
        String dkses = pramsMap.get("dkses").toString();
        List<String> dkArray = new ArrayList(Arrays.asList(dkses.split(",")));
        for (int i = 0; i <dkArray.size() ; i++) {
            if("--".equals(dkArray.get(i))){
                dkArray.set(i, "");
            }
        }

        String uuids = pramsMap.get("uuids").toString();
        String[] uuidArray = uuids.split(",");
        String rzts = "0";
        String hjje = "0.00";
        String hjse = "0.00";
        String hjdk = "0.00";
        JSONObject json = new JSONObject();

        List<String> taxnoList = (List<String>)pramsMap.get("gfsh");

        int cgsl = 0;
        StringBuilder sb = new StringBuilder();
        StringBuilder sb1 = new StringBuilder();
        StringBuilder sb2 = new StringBuilder();
        StringBuilder sb3 = new StringBuilder();
        boolean flag = false;
        boolean flag1 = false;
        boolean flag2 = false;
        boolean flag3 = false;
        sb.append("以下发票号码的抵扣税额大于税额不可勾选认证： \n ");
        sb1.append("以下税号已被锁定不可勾选认证： \n ");
        sb2.append("以下税号已申请抵扣统计，请撤销统计后重新操作： \n ");
        sb3.append("以下税号本月发票可认证总税额超限：\n");
        List<String> taxList = new ArrayList<>();
        List<String> orgList = new ArrayList<>();
        List<String> dkList = new ArrayList<>();
        List<InvoiceLog> logList = new ArrayList<>();
        for (int i = 0; i < uuidArray.length; i++) {
            try {
                QueryWrapper<BaseTDxRecordInvoice> queryWrapper1 = new QueryWrapper<>();
                queryWrapper1.eq("uuid", uuidArray[i]);
                DynamicContextHolder.push(pramsMap.get("dbName") + DbConstant.BUSINESS_READ);
                BaseTDxRecordInvoice recordInvoice = this.baseMapper.selectOne(queryWrapper1);
                Map<String, Object> curMap = new HashMap<>();
                List<String> shList = new ArrayList<>();
                shList.add(recordInvoice.getGfTaxNo());
                curMap.put("gfsh", shList);
                List<BaseTDxDkCount> curList = this.baseMapper.selectDksh(curMap);
                BaseTDxDkCount cur = curList.get(0);
                String skssq = cur.getSkssq();
                String year = skssq.substring(0, 4);
                String mon = skssq.substring(4);
                LocalDate localDate = LocalDate.of(Integer.parseInt(year), Integer.parseInt(mon), 01);
                LocalDate localDate1 = localDate.plusMonths(1);
                LocalDate localDate2 = LocalDate.now();
                if (!"0".equals(cur.getTjStatus()) && !"3".equals(cur.getTjStatus())
                    && !localDate2.isBefore(localDate1)) {
                    if (!orgList.contains(recordInvoice.getGfTaxNo())) {
                        sb2.append(recordInvoice.getGfTaxNo()).append(" \n ");
                        orgList.add(recordInvoice.getGfTaxNo());
                    }
                    flag2 = true;
                    continue;
                }
                //如果是农产品，代扣代缴，不验证此条件
                if("2".equals(recordInvoice.getYwlx())||"4".equals(recordInvoice.getXxly())){

                }else{
                    if (recordInvoice.getTaxAmount().compareTo(new BigDecimal(dkArray.get(i))) == -1) {
                        sb.append(recordInvoice.getInvoiceNo()).append(" \n ");
                        flag = true;
                        continue;
                    }
                }

                QueryWrapper<BaseTDxTaxCurrent> currentWrapper = new QueryWrapper<>();
                currentWrapper.eq("taxno", recordInvoice.getGfTaxNo());
                BaseTDxTaxCurrent current = baseTDxTaxCurrentDao.selectOne(currentWrapper);
                if ("1".equals(current.getLatchStatus())) {
                    if (!taxList.contains(recordInvoice.getGfTaxNo())) {
                        sb1.append(recordInvoice.getGfTaxNo()).append(" \n ");
                        taxList.add(recordInvoice.getGfTaxNo());
                    }
                    flag1 = true;
                    continue;
                }
                // 是否判断抵扣限额
                if (current.getLimitDkse() != null && current.getLimitDkse().compareTo(BigDecimal.ZERO) != 0) {
                    if (!dkList.contains(recordInvoice.getGfTaxNo())) {

                        Map<String, Object> paramMap = new HashMap<>();
                        paramMap.put("gfsh", recordInvoice.getGfTaxNo());
                        paramMap.put("skssq", current.getCurrentTaxPeriod());
                        BaseTDxRecordInvoice dkInvoice = super.baseMapper.selectDkse(paramMap);
                        BigDecimal customsDkse = super.baseMapper.selectCustomsDkse(paramMap);
                        String dkTaxAmount = "0";
                        if (dkInvoice != null && dkInvoice.getDkTaxAmount() != null) {
                            dkTaxAmount = dkInvoice.getDkTaxAmount().toPlainString();
                        }
                        if (customsDkse != null) {
                            dkTaxAmount = new BigDecimal(dkTaxAmount).add(customsDkse).toPlainString();
                        }
                        String dkse = "";
                        if(StringUtils.isNotEmpty(dkArray.get(i))){
                            dkse = MathUtil.add(dkTaxAmount, dkArray.get(i));
                        }else{
                            dkse = MathUtil.add(dkTaxAmount, "0");
                        }
                        if (new BigDecimal(dkse).compareTo(current.getLimitDkse()) > 0) {
                            sb3.append(recordInvoice.getGfTaxNo()).append(" \n ");
                            dkList.add(recordInvoice.getGfTaxNo());
                            flag3 = true;
                            continue;
                        }

                    }
                }
                UpdateWrapper<BaseTDxRecordInvoice> updateWrapper = new UpdateWrapper<>();
                updateWrapper.in("gf_tax_no", taxnoList);
                updateWrapper.eq("uuid", uuidArray[i]);
                BaseTDxRecordInvoice tdxRecordInvoice = new BaseTDxRecordInvoice();
                if(StringUtils.isNotEmpty(dkArray.get(i))){
                    tdxRecordInvoice.setDkTaxAmount(new BigDecimal(dkArray.get(i)));
                }
                tdxRecordInvoice.setSfygx("1");
                tdxRecordInvoice.setRzlx("1");
                tdxRecordInvoice.setAuthStatus("2");
                tdxRecordInvoice.setBdkStatus("1");
                tdxRecordInvoice.setConfirmDate(DateUtils.getSqlDate());
                tdxRecordInvoice.setConfirmUser(pramsMap.get("userName").toString());
                tdxRecordInvoice.setGxUserAccount(pramsMap.get("loginName").toString());
                tdxRecordInvoice.setGxUserName(pramsMap.get("userName").toString());
                tdxRecordInvoice.setGxDate(DateUtils.getSqlDate());
                tdxRecordInvoice.setRzhBelongDate(current.getCurrentTaxPeriod());
                tdxRecordInvoice.setCxrzDate(null);
                tdxRecordInvoice.setCxrzStatus("0");
                if (!"99".equals(gxType)) {
                    tdxRecordInvoice.setGxType(gxType);
                }
                DynamicContextHolder.push(pramsMap.get("dbName") + DbConstant.BUSINESS_WRITE);
                int n = super.baseMapper.update(tdxRecordInvoice, updateWrapper);
                cgsl = cgsl + n;
                QueryWrapper<BaseTDxRecordInvoice> queryWrapper = new QueryWrapper<>();
                queryWrapper.in("gf_tax_no", taxnoList);
                queryWrapper.eq("uuid", uuidArray[i]);
                DynamicContextHolder.push(pramsMap.get("dbName") + DbConstant.BUSINESS_READ);
                List<BaseTDxRecordInvoice> resultList = super.baseMapper.selectList(queryWrapper);
                if(ObjectUtils.isNotEmpty(resultList.get(0).getInvoiceAmount())){
                    hjje = MathUtil.round(MathUtil.add(hjje, resultList.get(0).getInvoiceAmount().toPlainString()));
                }
                if(ObjectUtils.isNotEmpty(resultList.get(0).getTaxAmount())){
                    hjse = MathUtil.round(MathUtil.add(hjse, resultList.get(0).getTaxAmount().toPlainString()));
                }
                if(ObjectUtils.isNotEmpty(resultList.get(0).getDkTaxAmount())){
                    hjdk = MathUtil.round(MathUtil.add(hjdk, resultList.get(0).getDkTaxAmount().toPlainString()));
                }
                InvoiceLog invoiceLog = new InvoiceLog();
                invoiceLog.setInvoiceCode(recordInvoice.getInvoiceCode());
                invoiceLog.setInvoiceNo(recordInvoice.getInvoiceNo());
                invoiceLog.setType("2");
                invoiceLog.setCreateDate(new Date());
                invoiceLog.setInputName((String)pramsMap.get("userName"));
                invoiceLog.setInputStatus("14");
                invoiceLog.setRzType(resultList.get(0).getGxType());
                logList.add(invoiceLog);
            } catch (Exception e) {
               log.info("业务处理异常：{}", e);
            }
        }
        if (logList.size() > 0) {
            invoiceLogService.toInvoiceLog(logList);
        }
        String message = "";
        if (flag) {
            message = sb.toString();
        }
        if (flag1) {
            message = message + sb1.toString() + "如需勾选认证，请到企业税务信息菜单解除锁定";
        }
        if (flag2) {
            message = message + sb2.toString();
        }
        if (flag3) {
            message = message + sb3.toString();
        }
        json.put("message", message);
        rzts = String.valueOf(cgsl);
        json.put("rzts", rzts);
        json.put("hjje", AmountFormatUtil.fmtMicrometer(hjje));
        json.put("hjse", AmountFormatUtil.fmtMicrometer(hjse));
        json.put("hjdk", AmountFormatUtil.fmtMicrometer(hjdk));

        return super.responseResult(json);
    }

    @Override
    public String identifiAllCount(Map<String, Object> pramsMap) {

        String hjsl = "0";
        String hjje = "0.00";
        String hjse = "0.00";

        JSONObject json = new JSONObject();

        DynamicContextHolder.push(pramsMap.get("dbName") + DbConstant.BUSINESS_READ);
        List<BaseTDxRecordInvoice> hjList = super.baseMapper.selectRzhAllHj(pramsMap);
        BaseTDxRecordInvoice hjxx = hjList.get(0);
        hjsl = hjxx.getSfygx();
        if (!"0".equals(hjsl)) {
            hjje = AmountFormatUtil.fmtMicrometer(MathUtil.round(hjxx.getInvoiceAmount().toPlainString()));
            hjse = AmountFormatUtil.fmtMicrometer(MathUtil.round(hjxx.getTaxAmount().toPlainString()));
        }
        json.put("hjsl", AmountFormatUtil.fmtMicrometer(hjsl));
        json.put("hjje", hjje);
        json.put("hjse", hjse);

        return super.responseResult(json);
    }

    @Override
    public String identifiByAll(Map<String, Object> pramsMap) {

        String rzts = "0";
        String hjje = "0.00";
        String hjse = "0.00";
        String hjdk = "0";
        JSONObject json = new JSONObject();

        List<String> list = (List<String>)pramsMap.get("gfsh");
        String[] values = list.toArray(new String[0]);

        int cgsl = 0;

        StringBuilder sb1 = new StringBuilder();
        StringBuilder sb2 = new StringBuilder();

        boolean flag1 = false;
        boolean flag2 = false;

        sb1.append("以下税号已被锁定不可勾选认证： \n ");
        sb2.append("以下税号已申请抵扣统计，请撤销统计后重新操作： \n ");

        List<String> taxList = new ArrayList<>();
        List<String> orgList = new ArrayList<>();
        List<InvoiceLog> logList = new ArrayList<>();
        DynamicContextHolder.push(pramsMap.get("dbName") + DbConstant.BUSINESS_READ);
        List<BaseTDxRecordInvoice> invoiceList = super.baseMapper.selectRzhAll(pramsMap);
        for (BaseTDxRecordInvoice recordInvoice : invoiceList) {
            try {

                Map<String, Object> curMap = new HashMap<>();
                List<String> shList = new ArrayList<>();
                shList.add(recordInvoice.getGfTaxNo());
                curMap.put("gfsh", shList);
                List<BaseTDxDkCount> curList = this.baseMapper.selectDksh(curMap);
                BaseTDxDkCount cur = curList.get(0);
                String skssq = cur.getSkssq();
                String year = skssq.substring(0, 4);
                String mon = skssq.substring(4);
                LocalDate localDate = LocalDate.of(Integer.parseInt(year), Integer.parseInt(mon), 01);
                LocalDate localDate1 = localDate.plusMonths(1);
                LocalDate localDate2 = LocalDate.now();
                if (!"0".equals(cur.getTjStatus()) && !"3".equals(cur.getTjStatus())
                    && !localDate2.isBefore(localDate1)) {
                    if (!orgList.contains(recordInvoice.getGfTaxNo())) {
                        sb2.append(recordInvoice.getGfTaxNo()).append(" \n ");
                        orgList.add(recordInvoice.getGfTaxNo());
                    }
                    flag2 = true;
                    continue;
                }

                QueryWrapper<BaseTDxTaxCurrent> currentWrapper = new QueryWrapper<>();
                currentWrapper.eq("taxno", recordInvoice.getGfTaxNo());
                BaseTDxTaxCurrent current = baseTDxTaxCurrentDao.selectOne(currentWrapper);
                if ("1".equals(current.getLatchStatus())) {
                    if (!taxList.contains(recordInvoice.getGfTaxNo())) {
                        sb1.append(recordInvoice.getGfTaxNo()).append(" \n ");
                        taxList.add(recordInvoice.getGfTaxNo());
                    }
                    flag1 = true;
                    continue;
                }

                UpdateWrapper<BaseTDxRecordInvoice> updateWrapper = new UpdateWrapper<>();
                updateWrapper.in("gf_tax_no", values);
                String  invoiceCode = StringUtils.isBlank(recordInvoice.getInvoiceCode())? "" : recordInvoice.getInvoiceCode();
                updateWrapper.eq("uuid", invoiceCode + recordInvoice.getInvoiceNo());
                BaseTDxRecordInvoice tdxRecordInvoice = new BaseTDxRecordInvoice();
                tdxRecordInvoice.setSfygx("1");
                tdxRecordInvoice.setRzlx("1");
                tdxRecordInvoice.setAuthStatus("2");
                tdxRecordInvoice.setBdkStatus("1");
                tdxRecordInvoice.setConfirmDate(DateUtils.getSqlDate());
                tdxRecordInvoice.setConfirmUser(pramsMap.get("name").toString());
                tdxRecordInvoice.setGxUserAccount(pramsMap.get("loginName").toString());
                tdxRecordInvoice.setGxUserName(pramsMap.get("name").toString());
                tdxRecordInvoice.setGxDate(DateUtils.getSqlDate());
                tdxRecordInvoice.setRzhBelongDate(current.getCurrentTaxPeriod());
                tdxRecordInvoice.setCxrzDate(null);
                tdxRecordInvoice.setCxrzStatus("0");
                DynamicContextHolder.push(pramsMap.get("dbName") + DbConstant.BUSINESS_WRITE);
                int n = super.baseMapper.update(tdxRecordInvoice, updateWrapper);
                cgsl = cgsl + n;
                InvoiceLog invoiceLog = new InvoiceLog();
                invoiceLog.setInvoiceCode(recordInvoice.getInvoiceCode());
                invoiceLog.setInvoiceNo(recordInvoice.getInvoiceNo());
                invoiceLog.setType("2");
                invoiceLog.setCreateDate(new Date());
                invoiceLog.setInputName((String)pramsMap.get("name"));
                invoiceLog.setInputStatus("14");
                logList.add(invoiceLog);

                hjje = MathUtil.round(MathUtil.add(hjje, recordInvoice.getInvoiceAmount().toPlainString()));
                hjse = MathUtil.round(MathUtil.add(hjse, recordInvoice.getTaxAmount().toPlainString()));
                hjdk = MathUtil.round(MathUtil.add(hjdk, recordInvoice.getDkTaxAmount() == null
                    ? recordInvoice.getTaxAmount().toPlainString() : recordInvoice.getDkTaxAmount().toPlainString()));
            } catch (Exception e) {
               log.info("业务处理异常：{}", e);
            }
        }

        if (logList.size() > 0) {
            invoiceLogService.toInvoiceLog(logList);
        }
        String message = "";

        if (flag1) {
            message = message + sb1.toString() + "如需勾选认证，请到企业税务信息菜单解除锁定";
        }
        if (flag2) {
            message = message + sb2.toString();
        }

        json.put("message", message);
        rzts = String.valueOf(cgsl);
        json.put("rzts", rzts);
        json.put("hjje", AmountFormatUtil.fmtMicrometer(hjje));
        json.put("hjse", AmountFormatUtil.fmtMicrometer(hjse));
        json.put("hjdk", AmountFormatUtil.fmtMicrometer(hjdk));

        return super.responseResult(json);
    }

}
