package com.dxhy.jxpt.baseService.ncpInvoice.service.impl;

import cn.hutool.core.util.ObjectUtil;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.dxhy.api.model.ncp.ncpCustoms.NcpJjkcCustomsSqReq;
import com.dxhy.api.model.ncp.ncpCustoms.NcpJjkcCustomsSqReqItem;
import com.dxhy.api.model.ncp.ncpCustoms.TdxCustomsNcpJjkcEntity;
import com.dxhy.api.model.db.TDxDkCountEntity;
import com.dxhy.api.model.rz.rzCustom.JkszhcxRespVO;
import com.dxhy.api.model.custom.TdxCustomsRecord;
import com.dxhy.api.model.db.TDxTaxCurrentEntity;
import com.dxhy.common.constant.DbConstant;
import com.dxhy.common.constant.DictConstant;
import com.dxhy.common.datasource.config.DynamicContextHolder;
import com.dxhy.common.enums.NcpQrlxEnum;
import com.dxhy.common.service.DictdetaServcice;
import com.dxhy.common.service.impl.MpBaseServiceImpl;
import com.dxhy.common.util.AmountFormatUtil;
import com.dxhy.common.util.MathUtil;
import com.dxhy.jxpt.baseService.config.dao.SysEnterpriseConfigDao;
import com.dxhy.jxpt.baseService.ncpInvoice.dao.*;
import com.dxhy.jxpt.baseService.ncpInvoice.service.NcpJjkcCustomsService;
import com.dxhy.jxpt.baseService.rzInvoice.dao.TdxCustomsRecordDao;
import com.dxhy.jxpt.baseService.rzInvoice.dao.BaseServiceTdxInvoiceNcpDao;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.github.pagehelper.util.StringUtil;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import javax.annotation.Resource;
import java.util.*;
import java.util.stream.Collectors;

/**
* @Description 农产品页面功能业务
* @Author wangruwei
* @Date 2023/9/21 16:20
**/
@Service("ncpJjkcCustomsService")
@Slf4j
public class NcpJjkcCustomsServiceImpl extends MpBaseServiceImpl<NcpJjkcCustomsDao, TdxCustomsNcpJjkcEntity> implements NcpJjkcCustomsService {

    @Resource
    private DictdetaServcice dictdetaServcice;

    @Resource
    private NcpRecordInvoiceDao ncpRecordInvoiceDao;

    @Resource
    private BaseServiceTdxInvoiceNcpDao baseServiceTdxInvoiceNcpDao;

    @Resource
    private SysEnterpriseConfigDao sysEnterpriseConfigDao;

    @Resource
    private TdxTaxCurrentDao tdxTaxCurrentDao;

    @Resource
    private NcpJjkcRecordInvoiceDao ncpJjkcRecordInvoiceDao;

    @Resource
    private TdxDkCountDao tdxDkCountDao;

    @Resource
    private NcpJjkcCustomsDao ncpJjkcCustomsDao;

    @Resource
    private TdxCustomsRecordDao tdxCustomsRecordDao;

    /**
     * @Description 农产品海关缴款书加计扣除发票列表
     * @Return java.lang.Object
     * @Author wangruwei
     * @Date 2023/10/20 17:55
     **/
    @Override
    public Object selectByPramsMap(Map<String, Object> pramsMap, int curr, int size) {
        DynamicContextHolder.push(pramsMap.get("dbName") + DbConstant.BUSINESS_READ);

        JSONObject json = new JSONObject();
        try {

            if (CollectionUtils.isEmpty((List<String>)pramsMap.get("gfsh"))) {
                json.put("total", "0");
                json.put("datalist", new ArrayList<>());
                return super.responseResult(json);
            }

            List<String> gfshList = (List<String>) pramsMap.get("gfsh");
            List<String> notInCustomsCodeList = new LinkedList();
            log.info("查询出当前属期，且属于加计扣除状态的发票");
            List<TDxDkCountEntity> dkCountList = new LinkedList();
            for (String gfsh:gfshList) {
                TDxTaxCurrentEntity tdxTaxCurrentEntity = tdxTaxCurrentDao.selectOne(
                        new QueryWrapper<TDxTaxCurrentEntity>().eq("taxno", gfsh));
                if(ObjectUtil.isNotEmpty(tdxTaxCurrentEntity)){
                    //当前税款所属期
                    String currentTaxPeriod = tdxTaxCurrentEntity.getCurrentTaxPeriod();
                    //查找出来不允许查询的发票号码集合
                    List<TdxCustomsNcpJjkcEntity> notInJjkcList = ncpJjkcCustomsDao.selectList(new LambdaQueryWrapper<TdxCustomsNcpJjkcEntity>()
                            .eq(TdxCustomsNcpJjkcEntity::getJjssq, currentTaxPeriod)
                            .eq(TdxCustomsNcpJjkcEntity::getGfTaxNo, gfsh)
                            .notIn(TdxCustomsNcpJjkcEntity::getClzt, "3", "8")
                    );
                    notInCustomsCodeList.addAll(notInJjkcList.stream().map(e->e.getCustomsCode()).collect(Collectors.toList()));

                    log.info("查询所传税号是否已统计成功，用作限制是否可勾选。已统计成功的不允许勾选");
                    TDxDkCountEntity tdxDkCountEntity = tdxDkCountDao.selectOne(new LambdaQueryWrapper<TDxDkCountEntity>()
                            .eq(TDxDkCountEntity::getTaxno,gfsh)
                            .eq(TDxDkCountEntity::getSkssq, currentTaxPeriod));
                    if (ObjectUtil.isNotEmpty(tdxDkCountEntity)) {
                        dkCountList.add(tdxDkCountEntity);
                    }
                }
            }
            if(notInCustomsCodeList.size()>0){
                log.info("列表里排除出去的缴款书号码{}",notInCustomsCodeList);
                pramsMap.put("notInCustomsCodeList", notInCustomsCodeList);
            }
            //筛选税号和是否当前属期 统计中,统计成功。只要此方法筛选出来有数据，则不让勾选
            Map<String, List<TDxDkCountEntity>> tjSucList = dkCountList.stream().filter(e -> "1".equals(e.getTjStatus())||"2".equals(e.getTjStatus())).collect(Collectors.groupingBy(TDxDkCountEntity::getTaxno));


            // 合计信息,仅第一页时需要统计，减少数据库压力
            if (curr == 1) {
                String hjsl = "0";
                String hjse = "0.00";
                String sykjjkcse = "0.00";
                Map<String, Object> hjxx = super.baseMapper.selectHjxx(pramsMap);
                if (hjxx != null) {
                    hjsl = (long)hjxx.get("hjsl") + "";
                    if (!"0".equals(hjsl)) {
                        hjsl = AmountFormatUtil.fmtMicrometer(hjsl);
                        hjse = AmountFormatUtil.fmtMicrometer(MathUtil.round(hjxx.get("hjse") + ""));
                        sykjjkcse = AmountFormatUtil.fmtMicrometer(MathUtil.round(hjxx.get("sykjjkcse") + ""));
                    }
                }
                json.put("hjsl", hjsl);
                json.put("hjse", hjse);
                json.put("sykjjkcse", sykjjkcse);
            }
            PageHelper.startPage(curr, size);
            List<JkszhcxRespVO> pageList = super.baseMapper.selectByPramsMap(pramsMap);
            PageInfo<JkszhcxRespVO> pageInfo = new PageInfo<>(pageList);
            // 用于页面返回list对象
            if (pageList.size() > 0) {
                int start = new Long(pageInfo.getStartRow()).intValue();
                log.info("可勾选条件:统计状态是未统计，统计失败");
                for (int i = 0; i < pageList.size(); i++) {
                    pageList.get(i).setXh(String.valueOf(i + start));
                    if(StringUtils.isNotEmpty(pageList.get(i).getTaxAmount())){
                        pageList.get(i)
                                .setTaxAmount(AmountFormatUtil.fmtMicrometer(MathUtil.round(pageList.get(i).getTaxAmount())));
                    }
                    if(StringUtils.isNotEmpty(pageList.get(i).getYxTaxAmount())){
                        pageList.get(i).setYxTaxAmount(
                                AmountFormatUtil.fmtMicrometer(MathUtil.round(pageList.get(i).getYxTaxAmount())));
                    }
                    if(StringUtils.isNotEmpty(pageList.get(i).getSykjjkcse())){
                        pageList.get(i).setSykjjkcse(
                                AmountFormatUtil.fmtMicrometer(MathUtil.round(pageList.get(i).getSykjjkcse())));
                    }
                    if(StringUtils.isNotEmpty(pageList.get(i).getKjjkcse())){
                        pageList.get(i).setKjjkcse(
                                AmountFormatUtil.fmtMicrometer(MathUtil.round(pageList.get(i).getKjjkcse())));
                    }

                    pageList.get(i)
                            .setSelect(true);

                    //统计状态是已统计，统计中。则不让勾选
                    if(ObjectUtil.isNotEmpty(tjSucList.get(pageList.get(i).getGfTaxNo()))&&tjSucList.get(pageList.get(i).getGfTaxNo()).size()>0){
                        pageList.get(i)
                                .setSelect(false);
                    }

                    // 字典值转换
                }
            }
            // 设置记录总数
            json.put("total", pageInfo.getTotal());
            List<JkszhcxRespVO> resultList = pageInfo.getList();
            json.put("datalist", CollectionUtils.isEmpty(resultList) ? new ArrayList<>() : resultList);
        } catch (Exception e) {
            // 统一设置操作异常标识
            json = super.errorResult(e);
        }
        return super.responseResult(json);
    }

    @Override
    public String ncpJjkcCustomSq(NcpJjkcCustomsSqReq req, String userName) {
        JSONObject json = new JSONObject();
        DynamicContextHolder.push("business" + DbConstant.BUSINESS_READ);
        //筛选出购方税号
        List<String> gfList = req.getCustomsList().stream().map(e -> e.getGfsh()).distinct().collect(Collectors.toList());
        //筛选出属期
        List<TDxTaxCurrentEntity> taxCurrents = tdxTaxCurrentDao.selectList(new LambdaQueryWrapper<TDxTaxCurrentEntity>().in(TDxTaxCurrentEntity::getTaxno, gfList));
        /*//筛选出有当前属期的数据，没有的话，返回错误    当前属期肯定会有
        List<String> haveDqSqList = taxCurrents.stream().map(e -> e.getTaxno()).collect(Collectors.toList());
        gfList.removeAll(haveDqSqList);
        if(gfList.size()>0){
            json.put("code", "9999");
            json.put("msg", "以下税号没有当前属期信息");
            json.put("data", gfList);
            return super.responseResult(json);
        }*/

        //税号和属期分组
        Map<String, List<TDxTaxCurrentEntity>> gfTaxSqMap = taxCurrents.stream().collect(Collectors.groupingBy(e -> e.getTaxno()));
        List<NcpJjkcCustomsSqReqItem> invoiceList = req.getCustomsList();
        if("0".equals(req.getSqType())){
            log.info("0申请全部项目不加计");
            for (NcpJjkcCustomsSqReqItem item:invoiceList) {
                TdxCustomsRecord ncpVo = tdxCustomsRecordDao.selectOne(new LambdaQueryWrapper<TdxCustomsRecord>()
                        .eq(TdxCustomsRecord::getCustomsCode,item.getCustomsCode()));
                String gfCurrent = findGfCurrent(gfTaxSqMap, ncpVo.getGfTaxNo());
                TdxCustomsNcpJjkcEntity jjkcEntity = this.baseMapper.selectOne(new LambdaQueryWrapper<TdxCustomsNcpJjkcEntity>()
                        .eq(TdxCustomsNcpJjkcEntity::getCustomsCode, item.getCustomsCode())
                        .eq(TdxCustomsNcpJjkcEntity::getJjssq, gfCurrent)
                );
                if(ObjectUtil.isEmpty(jjkcEntity)){
                    createCustomsJjkcData(req.getSqType(),ncpVo,gfCurrent,userName);
                }else{
                    log.info("加计扣除表中存在数据(存在则肯定是 3加计失败,8撤销成功)。删除当前数据，重新插入jjkc信息");
                    this.baseMapper.deleteById(jjkcEntity);
                    createCustomsJjkcData(req.getSqType(),ncpVo,gfCurrent,userName);
                }
            }
        }else if("1".equals(req.getSqType())){
            log.info("1申请剩余金额全部加计");
            for (NcpJjkcCustomsSqReqItem item:invoiceList) {
                TdxCustomsRecord ncpVo = tdxCustomsRecordDao.selectOne(new LambdaQueryWrapper<TdxCustomsRecord>()
                        .eq(TdxCustomsRecord::getCustomsCode,item.getCustomsCode()));
                String gfCurrent = findGfCurrent(gfTaxSqMap, ncpVo.getGfTaxNo());
                TdxCustomsNcpJjkcEntity jjkcEntity = this.baseMapper.selectOne(new LambdaQueryWrapper<TdxCustomsNcpJjkcEntity>()
                        .eq(TdxCustomsNcpJjkcEntity::getCustomsCode, item.getCustomsCode())
                        .eq(TdxCustomsNcpJjkcEntity::getJjssq, gfCurrent)
                );
                if(ObjectUtil.isEmpty(jjkcEntity)){
                    createCustomsJjkcData(req.getSqType(),ncpVo,gfCurrent,userName);
                }else{
                    log.info("加计扣除表中存在数据(存在则肯定是 3加计失败,8撤销成功)。删除当前数据，重新插入jjkc信息");
                    this.baseMapper.deleteById(jjkcEntity);
                    createCustomsJjkcData(req.getSqType(),ncpVo,gfCurrent,userName);
                }
            }
        }else if("2".equals(req.getSqType())){
            log.info("2申请剩余金额部分加计");

            //验证本次税额是否小于等于剩余可加计扣除税额
            for (int i = 0; i < invoiceList.size(); i++) {
                invoiceList.get(i).setRowNum(i+1);
            }
            List<NcpJjkcCustomsSqReqItem> errList = invoiceList.stream().filter(e -> MathUtil.compare(e.getBcjjkcse(), e.getSykjjkcse()) > 0).collect(Collectors.toList());
            if(errList.size()>0){
                json.put("code", "9999");
                json.put("msg", "验证失败");
                json.put("errList", errList);
                return super.responseResult(json);
            }

            for (NcpJjkcCustomsSqReqItem item:invoiceList) {
                TdxCustomsRecord ncpVo = tdxCustomsRecordDao.selectOne(new LambdaQueryWrapper<TdxCustomsRecord>()
                        .eq(TdxCustomsRecord::getCustomsCode,item.getCustomsCode()));
                String gfCurrent = findGfCurrent(gfTaxSqMap, ncpVo.getGfTaxNo());
                TdxCustomsNcpJjkcEntity jjkcEntity = this.baseMapper.selectOne(new LambdaQueryWrapper<TdxCustomsNcpJjkcEntity>()
                        .eq(TdxCustomsNcpJjkcEntity::getCustomsCode, item.getCustomsCode())
                        .eq(TdxCustomsNcpJjkcEntity::getJjssq, gfCurrent)
                );
                if(ObjectUtil.isEmpty(jjkcEntity)){
                    createCustomsJjkcBfData(req.getSqType(),ncpVo,gfCurrent,userName,item.getBcjjkcse());
                }else{
                    log.info("加计扣除表中存在数据(存在则肯定是 3加计失败,8撤销成功)。删除当前数据，重新插入jjkc信息");
                    this.baseMapper.deleteById(jjkcEntity);
                    createCustomsJjkcBfData(req.getSqType(),ncpVo,gfCurrent,userName,item.getBcjjkcse());
                }
            }

        }else{
            log.info("没有此类型");
            json.put("code", "9999");
            json.put("msg", "没有此类型");
            return super.responseResult(json);
        }
        json.put("code", "0000");
        json.put("msg", "申请成功");
        return super.responseResult(json);
    }

    private void createCustomsJjkcData(String sqType, TdxCustomsRecord ncpVo, String gfCurrent, String userName) {
        if("0".equals(sqType)){
            TdxCustomsNcpJjkcEntity jjkcInsert = new TdxCustomsNcpJjkcEntity();
            jjkcInsert.setGfTaxNo(ncpVo.getGfTaxNo());
            jjkcInsert.setCustomsCode(ncpVo.getCustomsCode());
            jjkcInsert.setFillInDate(ncpVo.getFillInDate());
            jjkcInsert.setCkznxzmbh(ncpVo.getCkznxzmbh());
            jjkcInsert.setJjssq(gfCurrent);
            jjkcInsert.setBcjjkcse(null);
            jjkcInsert.setSqlx(NcpQrlxEnum.NCP_QR_TYPE_JJKC_SQQBBJJ_20.getKey());
            jjkcInsert.setClzt("1");
            jjkcInsert.setSbyy("");
            jjkcInsert.setCzsj(new Date());
            jjkcInsert.setCzr(userName);
            jjkcInsert.setCreateTime(new Date());
            this.baseMapper.insert(jjkcInsert);
        }else if("1".equals(sqType)){
            TdxCustomsNcpJjkcEntity jjkcInsert = new TdxCustomsNcpJjkcEntity();
            jjkcInsert.setGfTaxNo(ncpVo.getGfTaxNo());
            jjkcInsert.setCustomsCode(ncpVo.getCustomsCode());
            jjkcInsert.setFillInDate(ncpVo.getFillInDate());
            jjkcInsert.setCkznxzmbh(ncpVo.getCkznxzmbh());
            jjkcInsert.setJjssq(gfCurrent);
            jjkcInsert.setBcjjkcse(ncpVo.getSykjjkcse());
            jjkcInsert.setSqlx(NcpQrlxEnum.NCP_QR_TYPE_JJKC_SQJJ_18.getKey());
            jjkcInsert.setClzt("1");
            jjkcInsert.setSbyy("");
            jjkcInsert.setCzsj(new Date());
            jjkcInsert.setCzr(userName);
            jjkcInsert.setCreateTime(new Date());

            this.baseMapper.insert(jjkcInsert);
        }
    }

    /**
     * @Description 创建加计扣除信息,自定义部分扣除
     * @Author wangruwei
     * @Date 2023/11/15 9:44
     **/
    private void createCustomsJjkcBfData(String sqType, TdxCustomsRecord ncpVo, String gfCurrent, String userName, String bcjjkcse) {
        TdxCustomsNcpJjkcEntity jjkcInsert = new TdxCustomsNcpJjkcEntity();
        jjkcInsert.setGfTaxNo(ncpVo.getGfTaxNo());
        jjkcInsert.setCustomsCode(ncpVo.getCustomsCode());
        jjkcInsert.setFillInDate(ncpVo.getFillInDate());
        jjkcInsert.setCkznxzmbh(ncpVo.getCkznxzmbh());
        jjkcInsert.setJjssq(gfCurrent);
        jjkcInsert.setBcjjkcse(bcjjkcse);
        jjkcInsert.setSqlx(NcpQrlxEnum.NCP_QR_TYPE_JJKC_SQJJ_18.getKey());
        jjkcInsert.setClzt("1");
        jjkcInsert.setSbyy("");
        jjkcInsert.setCzsj(new Date());
        jjkcInsert.setCzr(userName);
        jjkcInsert.setCreateTime(new Date());
        this.baseMapper.insert(jjkcInsert);
    }

    /**
    * @Description 农产品缴款书加计结果查询
    * @Author wangruwei
    * @Date 2023/10/21 14:50
    **/
    @Override
    public Object ncpJjkcCustomResultPage(Map<String, Object> pramsMap, int curr, int size) {
        DynamicContextHolder.push(pramsMap.get("dbName") + DbConstant.BUSINESS_READ);

        JSONObject json = new JSONObject();
        try {

            if (CollectionUtils.isEmpty((List<String>)pramsMap.get("gfsh"))) {
                json.put("total", "0");
                json.put("datalist", new ArrayList<>());
                return super.responseResult(json);
            }

            List<String> gfshList = (List<String>) pramsMap.get("gfsh");
            log.info("查询出当前属期，且属于加计扣除状态的发票");
            List<TDxDkCountEntity> dkCountList = new LinkedList();
            //查询出税号和属期对应的关系
            List<TDxTaxCurrentEntity> AllgfCurrentList = new LinkedList();
            for (String gfsh:gfshList) {
                TDxTaxCurrentEntity tdxTaxCurrentEntity = tdxTaxCurrentDao.selectOne(
                        new QueryWrapper<TDxTaxCurrentEntity>().eq("taxno", gfsh));
                if(ObjectUtil.isNotEmpty(tdxTaxCurrentEntity)){
                    AllgfCurrentList.add(tdxTaxCurrentEntity);
                    //当前税款所属期
                    String currentTaxPeriod = tdxTaxCurrentEntity.getCurrentTaxPeriod();

                    log.info("查询所传税号是否已统计成功，用作限制是否可勾选。已统计成功的不允许勾选");
                    TDxDkCountEntity tdxDkCountEntity = tdxDkCountDao.selectOne(new LambdaQueryWrapper<TDxDkCountEntity>()
                            .eq(TDxDkCountEntity::getTaxno,gfsh)
                            .eq(TDxDkCountEntity::getSkssq, currentTaxPeriod));
                    if (ObjectUtil.isNotEmpty(tdxDkCountEntity)) {
                        dkCountList.add(tdxDkCountEntity);
                    }
                }
            }
            //筛选税号和是否当前属期 统计中,统计成功。只要此方法筛选出来有数据，则不让勾选
            Map<String, List<TDxDkCountEntity>> tjSucList = dkCountList.stream().filter(e -> "0".equals(e.getTjStatus())||"3".equals(e.getTjStatus())).collect(Collectors.groupingBy(TDxDkCountEntity::getTaxno));
            //税号对应属期
            Map<String, List<TDxTaxCurrentEntity>> AllgfCurrentMap = AllgfCurrentList.stream().collect(Collectors.groupingBy(TDxTaxCurrentEntity::getTaxno));

            // 合计信息,仅第一页时需要统计，减少数据库压力
            if (curr == 1) {
                String hjsl = "0";
                String hjse = "0.00";
                String bcjjkcse = "0.00";
                Map<String, Object> hjxx = super.baseMapper.selectResultHjxx(pramsMap);
                if (hjxx != null) {
                    hjsl = (long)hjxx.get("hjsl") + "";
                    if (!"0".equals(hjsl)) {
                        hjsl = AmountFormatUtil.fmtMicrometer(hjsl);
                        hjse = AmountFormatUtil.fmtMicrometer(MathUtil.round(hjxx.get("hjse") + ""));
                        bcjjkcse = AmountFormatUtil.fmtMicrometer(MathUtil.round(hjxx.get("bcjjkcse") + ""));
                    }
                }
                json.put("hjsl", hjsl);
                json.put("hjse", hjse);
                json.put("bcjjkcse", bcjjkcse);
            }
            PageHelper.startPage(curr, size);
            List<JkszhcxRespVO> pageList = super.baseMapper.selectResultByPramsMap(pramsMap);
            PageInfo<JkszhcxRespVO> pageInfo = new PageInfo<>(pageList);
            // 用于页面返回list对象
            if (pageList.size() > 0) {
                int start = new Long(pageInfo.getStartRow()).intValue();
                log.info("可勾选条件:统计状态是未统计，统计失败。加计处理状态为申请成功。加计属期等于当前属期");
                for (int i = 0; i < pageList.size(); i++) {
                    pageList.get(i).setXh(String.valueOf(i + start));


                    if(StringUtils.isNotEmpty(pageList.get(i).getTaxAmount())){
                        pageList.get(i)
                                .setTaxAmount(AmountFormatUtil.fmtMicrometer(MathUtil.round(pageList.get(i).getTaxAmount())));
                    }
                    if(StringUtils.isNotEmpty(pageList.get(i).getYxTaxAmount())){
                        pageList.get(i).setYxTaxAmount(
                                AmountFormatUtil.fmtMicrometer(MathUtil.round(pageList.get(i).getYxTaxAmount())));
                    }

                    if(StringUtil.isNotEmpty(pageList.get(i).getKjjkcse())){
                        pageList.get(i).setKjjkcse(
                                AmountFormatUtil.fmtMicrometer(MathUtil.round(pageList.get(i).getKjjkcse())));
                    }

                    if(StringUtil.isNotEmpty(pageList.get(i).getBcjjkcse())){
                        pageList.get(i).setBcjjkcse(
                                AmountFormatUtil.fmtMicrometer(MathUtil.round(pageList.get(i).getBcjjkcse())));
                    }

                    if(StringUtil.isNotEmpty(pageList.get(i).getSykjjkcse())){
                        pageList.get(i).setSykjjkcse(
                                AmountFormatUtil.fmtMicrometer(MathUtil.round(pageList.get(i).getSykjjkcse())));
                    }


                    pageList.get(i)
                            .setSelect(false);
                    List<TDxTaxCurrentEntity> tDxTaxCurrentEntities = AllgfCurrentMap.get(pageList.get(i).getGfTaxNo());
                    String currentSq = "";
                    if(!CollectionUtils.isEmpty(tDxTaxCurrentEntities)&&tDxTaxCurrentEntities.size()>0){
                        currentSq = tDxTaxCurrentEntities.get(0).getCurrentTaxPeriod();
                    }
                    //统计状态是未统计，统计失败
                    if( (
                            (ObjectUtil.isNotEmpty(tjSucList.get(pageList.get(i).getGfTaxNo()))&&tjSucList.get(pageList.get(i).getGfTaxNo()).size()>0)||
                                    ObjectUtil.isEmpty(tjSucList.get(pageList.get(i).getGfTaxNo()))
                        )
                            &&"4".equals(pageList.get(i).getClzt())
                            &&currentSq.equals(pageList.get(i).getJjssq())
                    ){
                        pageList.get(i)
                                .setSelect(true);
                    }

                    // 字典值转换

                    String clzt = pageList.get(i).getClzt();
                    pageList.get(i).setClzt(dictdetaServcice.queryDictName(DictConstant.NCPJJKC_JJCLZT, clzt));

                    String sqlx = pageList.get(i).getSqlx();
                    pageList.get(i).setSqlx(dictdetaServcice.queryDictName(DictConstant.NCPJJKC_SQLX, sqlx));

                }
            }
            // 设置记录总数
            json.put("total", pageInfo.getTotal());
            List<JkszhcxRespVO> resultList = pageInfo.getList();
            json.put("datalist", CollectionUtils.isEmpty(resultList) ? new ArrayList<>() : resultList);
        } catch (Exception e) {
            // 统一设置操作异常标识
            json = super.errorResult(e);
        }
        return super.responseResult(json);
    }

    /**
     * @Description 农产品--缴款书撤销加计
     * @Author wangruwei
     * @Date 2023年10月19日16:57:06
     **/
    @Override
    public String ncpJjkcCustomCx(List<NcpJjkcCustomsSqReqItem> customList, String userName) {
        DynamicContextHolder.push("business" + DbConstant.BUSINESS_READ);
        for (int i = 0; i < customList.size(); i++) {
            NcpJjkcCustomsSqReqItem ncpJjkcCustomsSqReqItem = customList.get(i);
            TdxCustomsNcpJjkcEntity jjkcEntity = this.baseMapper.selectOne(new LambdaQueryWrapper<TdxCustomsNcpJjkcEntity>()
                    .eq(TdxCustomsNcpJjkcEntity::getCustomsCode, ncpJjkcCustomsSqReqItem.getCustomsCode())
                    .eq(TdxCustomsNcpJjkcEntity::getJjssq, ncpJjkcCustomsSqReqItem.getJjssq())
            );
            TdxCustomsNcpJjkcEntity updateJjkc = new TdxCustomsNcpJjkcEntity();
            if(NcpQrlxEnum.NCP_QR_TYPE_JJKC_SQJJ_18.getKey().equals(jjkcEntity.getSqlx())){
                updateJjkc.setSqlx(NcpQrlxEnum.NCP_QR_TYPE_JJKC_CXJJ_19.getKey());
            }
            if(NcpQrlxEnum.NCP_QR_TYPE_JJKC_SQQBBJJ_20.getKey().equals(jjkcEntity.getSqlx())){
                updateJjkc.setSqlx(NcpQrlxEnum.NCP_QR_TYPE_JJKC_CXQBBJJ_21.getKey());
            }
            updateJjkc.setClzt("5");
            this.baseMapper.update(updateJjkc, new LambdaUpdateWrapper<TdxCustomsNcpJjkcEntity>()
                    .eq(TdxCustomsNcpJjkcEntity::getCustomsCode, ncpJjkcCustomsSqReqItem.getCustomsCode())
                    .eq(TdxCustomsNcpJjkcEntity::getJjssq, ncpJjkcCustomsSqReqItem.getJjssq())
            );
        }
        JSONObject json = new JSONObject();
        json.put("code", "0000");
        json.put("msg", "撤销成功");
        return super.responseResult(json);
    }

    private String findGfCurrent(Map<String, List<TDxTaxCurrentEntity>> gfTaxSqMap, String gfsh) {
        if(ObjectUtil.isNotEmpty(gfTaxSqMap.get(gfsh))){
            return gfTaxSqMap.get(gfsh).size() > 0 ? gfTaxSqMap.get(gfsh).get(0).getCurrentTaxPeriod() : "";
        }else{
            return "";
        }
    }

}
