package com.css.txw.gxzx.service.lsjr.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.IdUtil;
import cn.hutool.json.JSONUtil;
import com.alibaba.excel.EasyExcel;
import com.alibaba.excel.read.builder.ExcelReaderBuilder;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.css.ggzc.framework.cache.utils.CacheUtils;
import com.css.ggzc.framework.cache.utils.XtcsUtils;
import com.css.ggzc.framework.cache.vo.DmbDTO;
import com.css.ggzc.framework.common.exception.RRException;
import com.css.ggzc.framework.common.exception.ServiceException;
import com.css.ggzc.framework.common.exception.enums.GlobalErrorCodeConstants;
import com.css.ggzc.framework.common.pojo.PageResult;
import com.css.ggzc.framework.common.util.date.DateUtils;
import com.css.ggzc.framework.common.util.gy.GyUtils;
import com.css.ggzc.framework.common.util.json.JsonUtils;
import com.css.ggzc.framework.file.utils.GgzcFileUtils;
import com.css.ggzc.framework.session.SessionUtils;
import com.css.txw.common.util.TxwHttpUtils;
import com.css.txw.gxzx.constants.SjztEnum;
import com.css.txw.gxzx.pojo.domain.TxwGxzxShqkbDO;
import com.css.txw.gxzx.pojo.domain.lsjr.GxzxDkbxsqxx;
import com.css.txw.gxzx.pojo.domain.lsjr.GxzxLsjrcpxx;
import com.css.txw.gxzx.pojo.dto.GxzxLsjrcpImportDto;
import com.css.txw.gxzx.pojo.lsjr.*;
import com.css.txw.gxzx.pojo.vo.GxspReqVO;
import com.css.txw.gxzx.pojo.vo.ShqkxxVO;
import com.css.txw.gxzx.pojo.vo.lsjr.ProductApplyVO;
import com.css.txw.gxzx.service.TxwGxzxShqkbService;
import com.css.txw.gxzx.service.lsjr.GxzxDkbxsqxxService;
import com.css.txw.gxzx.service.lsjr.GxzxLsjrService;
import com.css.txw.gxzx.service.lsjr.GxzxLsjrcpxxService;
import lombok.SneakyThrows;
import lombok.extern.slf4j.Slf4j;
import org.springframework.http.HttpMethod;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import javax.validation.Valid;

import java.io.ByteArrayInputStream;
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.util.*;
import java.util.stream.Collectors;

@Slf4j
@Service
public class GxzxLsjrServiceImpl implements GxzxLsjrService {

    @Resource
    private GxzxDkbxsqxxService dkbxsqxxService;

    @Resource
    private GxzxLsjrcpxxService lsjrcpxxService;

    @Resource
    private TxwGxzxShqkbService txwGxzxShqkbService;

    @Resource
    private TxwHttpUtils txwHttpUtils;

    @Override
    public List<DmbDTO> queryJgList(YhBxjgcxRequestVO requestVO) {
        String type = requestVO.getType();
        String tableName = "xd".equals(type) ? "LSJRYH" : "LSJRBXJG";
        List<DmbDTO> dtoList = CacheUtils.getDmbData(tableName);
        if (GyUtils.isNull(dtoList)) {
            dtoList = new ArrayList<>();
        }
        return dtoList;
    }

    @Override
    public PageResult<GxzxLsjrcpxxDto> queryXdbxcpList(XdbxcpcxRequestVO requestVO) {
        List<GxzxLsjrcpxxDto> list = new ArrayList<>();
        PageResult<GxzxLsjrcpxxDto> pageResult = new PageResult<>();
        pageResult.setTotal(0L);
        Integer pageNum = requestVO.getPageNum();
        Integer pageSize = requestVO.getPageSize();
        String cpmc = requestVO.getCpmc();
        List<String> qyidList = requestVO.getQyidList();
        String type = requestVO.getType();
        String sxjzt = requestVO.getSxjzt();
        String module = requestVO.getModule();// 后台维护时只能看自己发布的，产品查询时不限制本企业
        Page<GxzxLsjrcpxx> page = new Page<>(pageNum, pageSize);
        QueryWrapper<GxzxLsjrcpxx> queryWrapper = new QueryWrapper<>();
        queryWrapper.lambda()
                .eq((!type.contains(",")), GxzxLsjrcpxx::getSshyDm, type)
                .in((type.contains(",")), GxzxLsjrcpxx::getSshyDm, Arrays.asList(type.split(",")))
                .in(GyUtils.isNotNull(qyidList), GxzxLsjrcpxx::getQyuuid, qyidList)
                .like(GyUtils.isNotNull(cpmc), GxzxLsjrcpxx::getCpmc, cpmc)
                .like(GyUtils.isNotNull(requestVO.getJgmc()), GxzxLsjrcpxx::getQymc, requestVO.getJgmc())
                .eq(GyUtils.isNotNull(requestVO.getCarbonType()), GxzxLsjrcpxx::getCarbonType, requestVO.getCarbonType())
                .eq(GyUtils.isNotNull(requestVO.getYwlxDm()), GxzxLsjrcpxx::getYwlxDm, requestVO.getYwlxDm())
                .eq((GyUtils.isNotNull(sxjzt) && "wh".equals(module)), GxzxLsjrcpxx::getSjzt, sxjzt)
                .eq((GyUtils.isNull(sxjzt) && !"wh".equals(module)), GxzxLsjrcpxx::getSjzt, SjztEnum.SJ.getCode())
                .orderByDesc(GxzxLsjrcpxx::getSjzt)
                .orderByDesc(GxzxLsjrcpxx::getLrrq);
        Page<GxzxLsjrcpxx> cpxxPage = lsjrcpxxService.page(page, queryWrapper);
        if (GyUtils.isNotNull(cpxxPage) && GyUtils.isNotNull(cpxxPage.getRecords())) {
            List<GxzxLsjrcpxx> records = cpxxPage.getRecords();
            list = JsonUtils.toList(JsonUtils.toJson(records), GxzxLsjrcpxxDto.class);
            String tableName = "xd".equals(type) ? "LSJR_XDYWLX" : "LSJR_BXYWLX";
            List<DmbDTO> yelxList = CacheUtils.getDmbData(tableName);
            Map<String, String> ywlxMap = yelxList.stream().collect(Collectors.toMap(DmbDTO::getCode, DmbDTO::getCaption));
            List<DmbDTO> carbonTypeList = CacheUtils.getDmbData("CARBON_TYPE");
            Map<String, String> carbonTypeMap = carbonTypeList.stream().collect(Collectors.toMap(DmbDTO::getCode, DmbDTO::getCaption));

            for (GxzxLsjrcpxxDto dto : list) {
                String ywlxDm = dto.getYwlxDm();
                String ywlxmc = ywlxMap.get(ywlxDm);
                dto.setYwlxmc(ywlxmc);
                Integer carbonType = dto.getCarbonType();
                String carbonTypeDesc = carbonTypeMap.get(carbonType + "");
                dto.setCarbonTypeMc(carbonTypeDesc);
                // 设置logo
                String jgid = dto.getQyuuid();
                String sshyDm = dto.getSshyDm();
                tableName = "xd".equals(sshyDm) ? "LSJRYH" : "LSJRBXJG";
                String cplxmc = "xd".equals(sshyDm) ? "信贷" : "保险";
                dto.setCplxmc(cplxmc);
                Map<String, Object> tableData = CacheUtils.getTableData(tableName, jgid);
                if (GyUtils.isNotNull(tableData)) {
                    dto.setYxlogo(GyUtils.isNotNull(tableData.get("bz")) ? tableData.get("bz").toString() : null);
                }
            }
        }
        pageResult.setTotal(cpxxPage.getTotal());
        pageResult.setList(list);
        return pageResult;
    }

    @Override
    public GxzxLsjrcpxxDto queryXdbxcpxq(String cpid) {
        GxzxLsjrcpxx lsjrcpxx = lsjrcpxxService.getById(cpid);
        if (GyUtils.isNull(lsjrcpxx)) {
			return null;
		}
        GxzxLsjrcpxxDto lsjrcpxxDto = JsonUtils.toBean(JsonUtils.toJson(lsjrcpxx), GxzxLsjrcpxxDto.class);
        String sshyDm = lsjrcpxxDto.getSshyDm();
        String tableName = "xd".equals(sshyDm) ? "LSJR_XDYWLX" : "LSJR_BXYWLX";
        String ywlxmc = CacheUtils.dm2mc(tableName, lsjrcpxxDto.getYwlxDm());
        lsjrcpxxDto.setYwlxmc(ywlxmc);
        return lsjrcpxxDto;
    }

    @Override
    public KxtsjResponseDataDto queryKxtsj(KxtsjRequestVO requestVO) {
        KxtsjResponseDataDto responseDataDto = new KxtsjResponseDataDto();
        List<KxtsjRecordsDto> dataList = new ArrayList<>();
        String kxtcxUrl = CacheUtils.getXtcs("KXTCX_URL");
//        String kxtcxUrl = "https://ctn-web-pre.lingshu.net/api/v1/open/carbon-business/page";
        KxtsjResponseDto kxtsjResponseDto = null;
        String responseStr = null;
        try {
            String shxydm = SessionUtils.getShxydm();
            requestVO.setSearch(shxydm);
            txwHttpUtils.createRestTemplateWithProxy();
            responseStr = txwHttpUtils.exchange(kxtcxUrl, HttpMethod.POST, JsonUtils.toJson(requestVO), null, String.class);
            kxtsjResponseDto = JSONUtil.toBean(responseStr, KxtsjResponseDto.class);
        } catch (Exception e) {
            e.printStackTrace();
        }
        if (GyUtils.isNull(kxtsjResponseDto) || GyUtils.isNull(kxtsjResponseDto.getData())) {
            responseDataDto.setPages("1");
            responseDataDto.setSize(requestVO.getPageSize() + "");
            responseDataDto.setTotal("0");
            responseDataDto.setCurrent(requestVO.getCurrentPage() + "");
            responseDataDto.setRecords(dataList);
        } else {
            responseDataDto = kxtsjResponseDto.getData();
            String current = responseDataDto.getCurrent();
            String size = responseDataDto.getSize();
            int start = 1;
            if (GyUtils.isNotNull(current) && !"1".equals(current)) {
                int pageSize = Integer.valueOf(size);
                int pageNum = Integer.valueOf(current);
                start = pageNum * pageSize + 1;
            }
            List<DmbDTO> dtoList = CacheUtils.getDmbData("CARBON_TYPE");
            Map<String, String> typeMap = dtoList.stream().collect(Collectors.toMap(DmbDTO::getCode, DmbDTO::getCaption));
            for (KxtsjRecordsDto dto : responseDataDto.getRecords()) {
                dto.setXh(start);
                dto.setCarbonTypeDesc(typeMap.get(dto.getCarbonType() + ""));
                start++;
            }
        }
        return responseDataDto;
    }

    @SneakyThrows
    @Override
    public String saveDksqxx(GxzxDkbxsqxxDto dto, MultipartFile file) {
        // 生成唯一文件名
        String originalFilename = file.getOriginalFilename();
        String fileExt = originalFilename.substring(originalFilename.lastIndexOf("."));
        String newFilename = UUID.randomUUID() + fileExt;
        // 创建存储目录
        String yearMonth = DateUtils.toDateStrByFormat(new Date(), "yyyyMM");
        String day = DateUtils.toDateStrByFormat(new Date(), "dd");
        String qyuuid = SessionUtils.getQyuuid();
        String qymc = SessionUtils.get("qymc").toString();
        String nsrsbh = SessionUtils.getNsrsbh();
        String bakFilePath = "gxzx" + File.separator + "lsjr" + File.separator + "dksq" + File.separator + qyuuid
                + File.separator + yearMonth + File.separator + day + File.separator + newFilename;
        String uploadPath = GgzcFileUtils.upload(file.getBytes(), bakFilePath, file.getContentType());
        GxzxDkbxsqxx dkbxsqxx = new GxzxDkbxsqxx();
        dkbxsqxx.setSquuid(IdUtil.fastSimpleUUID());
        dkbxsqxx.setSshyDm("xd");
        dkbxsqxx.setFlzl(uploadPath);
        dkbxsqxx.setCpuuid(dto.getCpuuid());
        dkbxsqxx.setKxtsjzl(JsonUtils.toJson(dto.getKxtsjzl()));
        dkbxsqxx.setLxr(dto.getLxr());
        dkbxsqxx.setLxdh(dto.getLxdh());
        dkbxsqxx.setQyuuid(qyuuid);
        dkbxsqxx.setQymc(qymc);
        dkbxsqxx.setNsrsbh(nsrsbh);
        dkbxsqxx.setZt("2");
        dkbxsqxx.setWjmc(originalFilename);
        String sfsh = XtcsUtils.getXtcs("LSJR_SFSH", "Y");
        if ("N".equals(sfsh)) {
            dkbxsqxx.setZt("3");
        }
        dkbxsqxx.setSqdkJe(dto.getSqdkJe());
        dkbxsqxx.setSqdkyt(dto.getSqdkyt());
        dkbxsqxx.setLrruuid(qyuuid);
        dkbxsqxx.setLrrq(new Date());
        dkbxsqxxService.save(dkbxsqxx);
        TxwGxzxShqkbDO shqkbDO = new TxwGxzxShqkbDO();
        shqkbDO.setUuid(IdUtil.fastSimpleUUID());
        shqkbDO.setYwlxDm("dk");
        shqkbDO.setGlid(dkbxsqxx.getSquuid());
        shqkbDO.setLrrq(new Date());
        shqkbDO.setLrrsfid(qyuuid);
        txwGxzxShqkbService.save(shqkbDO);
        return dkbxsqxx.getSquuid();
    }

    @SneakyThrows
    @Override
    public String saveTbsqxx(GxzxDkbxsqxxDto dto, MultipartFile file) {
        // 生成唯一文件名
        String originalFilename = file.getOriginalFilename();
        String fileExt = originalFilename.substring(originalFilename.lastIndexOf("."));
        String newFilename = UUID.randomUUID() + fileExt;
        // 创建存储目录
        String yearMonth = DateUtils.toDateStrByFormat(new Date(), "yyyyMM");
        String day = DateUtils.toDateStrByFormat(new Date(), "dd");
        String qyuuid = SessionUtils.getQyuuid();
        String qymc = SessionUtils.get("qymc").toString();
        String nsrsbh = SessionUtils.getNsrsbh();
        String bakFilePath = "gxzx" + File.separator + "lsjr" + File.separator + "tbsq" + File.separator + qyuuid
                + File.separator + yearMonth + File.separator + day + File.separator + newFilename;
        String uploadPath = GgzcFileUtils.upload(file.getBytes(), bakFilePath, file.getContentType());
        GxzxDkbxsqxx dkbxsqxx = new GxzxDkbxsqxx();
        dkbxsqxx.setSquuid(IdUtil.fastSimpleUUID());
        dkbxsqxx.setSshyDm("bx");
        dkbxsqxx.setFlzl(uploadPath);
        dkbxsqxx.setCpuuid(dto.getCpuuid());
        dkbxsqxx.setKxtsjzl(JsonUtils.toJson(dto.getKxtsjzl()));
        dkbxsqxx.setLxr(dto.getLxr());
        dkbxsqxx.setLxdh(dto.getLxdh());
        dkbxsqxx.setQyuuid(qyuuid);
        dkbxsqxx.setQymc(qymc);
        dkbxsqxx.setNsrsbh(nsrsbh);
        dkbxsqxx.setZt("2");
        dkbxsqxx.setWjmc(originalFilename);
        String sfsh = XtcsUtils.getXtcs("LSJR_SFSH", "Y");
        if ("N".equals(sfsh)) {
            dkbxsqxx.setZt("3");
        }
        dkbxsqxx.setTbje(dto.getTbje());
        dkbxsqxx.setBxbzqx(dto.getBxbzqx());
        dkbxsqxx.setLrruuid(qyuuid);
        dkbxsqxx.setLrrq(new Date());
        dkbxsqxxService.save(dkbxsqxx);
        TxwGxzxShqkbDO shqkbDO = new TxwGxzxShqkbDO();
        shqkbDO.setUuid(IdUtil.fastSimpleUUID());
        shqkbDO.setYwlxDm("bx");
        shqkbDO.setGlid(dkbxsqxx.getSquuid());
        shqkbDO.setLrrq(new Date());
        shqkbDO.setLrrsfid(qyuuid);
        txwGxzxShqkbService.save(shqkbDO);
        return dkbxsqxx.getSquuid();
    }

    @Override
    public DownloadFileInfoVO querySqFileByid(String squuid) {
        GxzxDkbxsqxx dkbxsqxx = dkbxsqxxService.getById(squuid);
        if (GyUtils.isNull(dkbxsqxx)) {
            throw new ServiceException(GlobalErrorCodeConstants.ERROR.getCode(), "未发现符合条件的申请信息！");
        }
        byte[] content = GgzcFileUtils.getContent(dkbxsqxx.getFlzl());
        DownloadFileInfoVO downloadFileInfoVO = new DownloadFileInfoVO();
        downloadFileInfoVO.setFileName(dkbxsqxx.getWjmc());
        downloadFileInfoVO.setFileByte(content);
        return downloadFileInfoVO;
    }

    @SneakyThrows
    @Override
    public String saveOrUpdateCpxx(GxzxLsjrcpxxDto dto) {
        String qyuuid = SessionUtils.getQyuuid();
        String cpuuid = dto.getCpuuid();
        String module = dto.getModule();// 管理员模式可直接修改产品信息，去掉只能修改本企业产品信息限制
        if (GyUtils.isNotNull(cpuuid)) {
            QueryWrapper<GxzxLsjrcpxx> queryWrapper = new QueryWrapper<>();
            queryWrapper.lambda()
                    .eq(GxzxLsjrcpxx::getCpuuid, cpuuid)
//                    .eq(!"manager".equals(module), GxzxLsjrcpxx::getQyuuid, qyuuid)
                    .last("limit 1");
            GxzxLsjrcpxx one = lsjrcpxxService.getOne(queryWrapper);
            if (GyUtils.isNull(one)) {
                throw new RRException("未查询到符合修改条件的产品信息数据");
            }
            dto.setXgrq(new Date());
            dto.setXgruuid(qyuuid);
        } else {
            cpuuid = IdUtil.fastSimpleUUID();
            dto.setCpuuid(cpuuid);
            dto.setLrrq(new Date());
            dto.setLrruuid(qyuuid);
            // 设置logo
            String jgid = dto.getQyuuid();
            String sshyDm = dto.getSshyDm();
            String tableName = "xd".equals(sshyDm) ? "LSJRYH" : "LSJRBXJG";
            Map<String, Object> tableData = CacheUtils.getTableData(tableName, jgid);
            if (GyUtils.isNotNull(tableData)) {
                dto.setYxlogo(GyUtils.isNotNull(tableData.get("bz")) ? tableData.get("bz").toString() : null);
            }
        }
        GxzxLsjrcpxx cpxx = BeanUtil.toBean(dto, GxzxLsjrcpxx.class);
        cpxx.setZt("2");
        cpxx.setSjzt("0");
        cpxx.setFprq(null);
        String sfsh = XtcsUtils.getXtcs("LSJR_SFSH", "Y");
        if ("N".equals(sfsh)) {
            cpxx.setZt("3");
            cpxx.setSjzt("1");
            cpxx.setFprq(new Date());
        } else {
            TxwGxzxShqkbDO shqkbDO = new TxwGxzxShqkbDO();
            shqkbDO.setUuid(IdUtil.fastSimpleUUID());
            shqkbDO.setYwlxDm(cpxx.getSshyDm());
            shqkbDO.setGlid(cpxx.getCpuuid());
            shqkbDO.setLrrq(new Date());
            shqkbDO.setLrrsfid(qyuuid);
            txwGxzxShqkbService.save(shqkbDO);
        }
        lsjrcpxxService.saveOrUpdate(cpxx);
        return cpxx.getCpuuid();
    }

    @Override
    public boolean delete(String cpid) {
        boolean result = lsjrcpxxService.removeById(cpid);
        return result;
    }

    @Override
    public boolean batchDelete(BatchDeleteReqVO reqVO) {
        String cpuuids = reqVO.getCpuuids();
        boolean b = lsjrcpxxService.removeBatchByIds(Arrays.asList(cpuuids.split(",")));
        return b;
    }

    @Override
    public PageResult<ShqkxxVO> queryDspXdbxcpList(DspXdbxcpcxRequestVO requestVO) {
        List<ShqkxxVO> list = new ArrayList<>();
        PageResult<ShqkxxVO> pageResult = new PageResult<>();
        pageResult.setTotal(0L);
        Integer pageNum = requestVO.getPageNum();
        Integer pageSize = requestVO.getPageSize();
        List<String> qyidList = requestVO.getQyidList();
        List<String> typeList = requestVO.getTypeList();
        Page<TxwGxzxShqkbDO> page = new Page<>(pageNum, pageSize);
        QueryWrapper<TxwGxzxShqkbDO> queryWrapper = new QueryWrapper<>();
        queryWrapper.lambda()
                .in(GyUtils.isNotNull(typeList), TxwGxzxShqkbDO::getYwlxDm, typeList)
                .in(GyUtils.isNotNull(qyidList), TxwGxzxShqkbDO::getLrrsfid, qyidList)
                .orderByDesc(TxwGxzxShqkbDO::getLrrq)
                .orderByAsc(TxwGxzxShqkbDO::getShjg1);
        Page<TxwGxzxShqkbDO> cpxxPage = txwGxzxShqkbService.page(page, queryWrapper);
        if (GyUtils.isNotNull(cpxxPage) && GyUtils.isNotNull(cpxxPage.getRecords())) {
            List<TxwGxzxShqkbDO> records = cpxxPage.getRecords();
            list = JsonUtils.toList(JsonUtils.toJson(records), ShqkxxVO.class);
        }
        pageResult.setTotal(cpxxPage.getTotal());
        pageResult.setList(list);
        return pageResult;
    }

    @Override
    public String cpSp(GxspReqVO reqVO) {
        TxwGxzxShqkbDO shqkbDO = txwGxzxShqkbService.getById(reqVO.getUuid());
        shqkbDO.setShrsfid(SessionUtils.getYhUuid());
        shqkbDO.setShjg1(reqVO.getShjg1());
        shqkbDO.setShyj1(reqVO.getShyj1());
        shqkbDO.setShsj(new Date());
        txwGxzxShqkbService.updateById(shqkbDO);
        return "success";
    }

    @Override
    public String cpsxj(CpsxjReqVO reqVO) {
        String cpuuid = reqVO.getCpuuid();
        List<String> cpuuidList = Arrays.asList(cpuuid.split(","));
        UpdateWrapper<GxzxLsjrcpxx> updateWrapper = new UpdateWrapper<>();
        updateWrapper.lambda()
                .set(GxzxLsjrcpxx::getSjzt, reqVO.getSjzt())
                .set(GxzxLsjrcpxx::getXgrq, new Date())
                .set(GxzxLsjrcpxx::getXgruuid, SessionUtils.getYhUuid())
                .in(GxzxLsjrcpxx::getCpuuid, cpuuidList);
        lsjrcpxxService.update(updateWrapper);
        return "success";
    }

    @Override
    public String pldr(MultipartFile file) {
        String originalFilename = file.getOriginalFilename();
        //校验文件
        if (!originalFilename.toLowerCase().contains(".xlsx") && !originalFilename.toLowerCase().contains(".xls")) {
            throw new ServiceException(GlobalErrorCodeConstants.ERROR.getCode(), "导入的文件格式系统不支持，当前仅支持excel格式文件");
        }
        try (InputStream inputStream = new ByteArrayInputStream(file.getBytes())) {
            List<GxzxLsjrcpImportDto> dtoList = this.validImportProductData(inputStream); //主表

            if (CollectionUtils.isEmpty(dtoList)) {
                throw new ServiceException(GlobalErrorCodeConstants.ERROR.getCode(), "导入的文件中未检测到产品信息");
            }
            List<TxwGxzxShqkbDO> spList = new ArrayList<>();
            List<GxzxLsjrcpxx> cpxxList = BeanUtil.copyToList(dtoList, GxzxLsjrcpxx.class);
            for (GxzxLsjrcpxx cpxx : cpxxList) {
                cpxx.setLrrq(new Date());
                cpxx.setLrruuid(SessionUtils.getYhUuid());
                // 设置logo
                String jgid = cpxx.getQyuuid();
                String sshyDm = cpxx.getSshyDm();
                String tableName = "xd".equals(sshyDm) ? "LSJRYH" : "LSJRBXJG";
                Map<String, Object> tableData = CacheUtils.getTableData(tableName, jgid);
                if (GyUtils.isNotNull(tableData)) {
                    cpxx.setYxlogo(GyUtils.isNotNull(tableData.get("bz")) ? tableData.get("bz").toString() : null);
                }
                cpxx.setZt("2");
                cpxx.setSjzt("0");
                cpxx.setFprq(null);
                String sfsh = XtcsUtils.getXtcs("LSJR_SFSH", "Y");
                if ("N".equals(sfsh)) {
                    cpxx.setZt("3");
                    cpxx.setSjzt("1");
                    cpxx.setFprq(new Date());
                } else {
                    TxwGxzxShqkbDO shqkbDO = new TxwGxzxShqkbDO();
                    shqkbDO.setUuid(IdUtil.fastSimpleUUID());
                    shqkbDO.setYwlxDm(cpxx.getSshyDm());
                    shqkbDO.setGlid(cpxx.getCpuuid());
                    shqkbDO.setLrrq(new Date());
                    shqkbDO.setLrrsfid(SessionUtils.getYhUuid());
                    spList.add(shqkbDO);
                }
            }
            if (GyUtils.isNotNull(spList)) {
                txwGxzxShqkbService.saveBatch(spList);
            }
            lsjrcpxxService.saveBatch(cpxxList); //1:处理成功
        } catch (Exception e) {
            throw new ServiceException(GlobalErrorCodeConstants.ERROR.getCode(), "处理导入数据失败");
        }
        return "导入成功";
    }

    private List<GxzxLsjrcpImportDto> validImportProductData(InputStream inputStream) {
        try {
            LsjrcpImportListener listener = new LsjrcpImportListener();
            ExcelReaderBuilder readerBuilder = EasyExcel.read(inputStream, GxzxLsjrcpImportDto.class, listener);
            readerBuilder.sheet().sheetNo(0).headRowNumber(2).doRead();// 设置表头在第3行
            List<GxzxLsjrcpImportDto> successList = listener.getSuccessList();
            List<GxzxLsjrcpImportDto> failList = listener.getFailedList();
            log.info("产品信息导入校验结果：{}/{}", successList.size(), successList.size() + failList.size());
            if (failList.size() > 0) {
                String errorMsg = "产品信息校验结果：";
                errorMsg += failList.stream().map(e -> "<br>第" + (e.getRowNum() + 1) + "行:" + e.getErrorMsg()).collect(Collectors.joining("\n"));
                log.info(errorMsg);
                throw new ServiceException(GlobalErrorCodeConstants.ERROR.getCode(), errorMsg);
            } else {
                return successList;
            }
        } catch (Exception e) {
            log.error("客户批量导入校验异常：", e);
            throw new ServiceException(GlobalErrorCodeConstants.ERROR.getCode(), e.getMessage());
        }
    }

    @Override
    public PageResult<GxzxDkbxsqxxVO> queryXdbxcpsqList(XdbxcpsqRequestVO requestVO) {
        List<GxzxDkbxsqxxVO> list = new ArrayList<>();
        PageResult<GxzxDkbxsqxxVO> pageResult = new PageResult<>();
        pageResult.setTotal(0L);
        Integer pageNum = requestVO.getPageNum();
        Integer pageSize = requestVO.getPageSize();
        Page<GxzxDkbxsqxx> page = new Page<>(pageNum, pageSize);
        QueryWrapper<GxzxDkbxsqxx> queryWrapper = new QueryWrapper<>();
        queryWrapper.lambda()
                .eq(GyUtils.isNotNull(requestVO.getCpuuid()),GxzxDkbxsqxx::getCpuuid, requestVO.getCpuuid())
                .orderByDesc(GxzxDkbxsqxx::getZt)
                .orderByDesc(GxzxDkbxsqxx::getLrrq);
        Page<GxzxDkbxsqxx> sqxxPage = dkbxsqxxService.page(page, queryWrapper);
        if (GyUtils.isNotNull(sqxxPage) && GyUtils.isNotNull(sqxxPage.getRecords())) {
            list = BeanUtil.copyToList(sqxxPage.getRecords(), GxzxDkbxsqxxVO.class);
        }
        pageResult.setTotal(sqxxPage.getTotal());
        pageResult.setList(list);
        return pageResult;
    }

    @Override
    public GxzxDkbxsqxxVO queryXdbxcpsqxq(String squuid) {
        GxzxDkbxsqxx dkbxsqxx = dkbxsqxxService.getById(squuid);
        GxzxDkbxsqxxVO dkbxsqxxVO = BeanUtil.toBean(dkbxsqxx, GxzxDkbxsqxxVO.class);
        return dkbxsqxxVO;
    }

    private String getBase64(MultipartFile file) throws IOException {
        // 获取文件的字节数组
        byte[] fileBytes = file.getBytes();
        // 使用Base64编码
        String base64String = Base64.getEncoder().encodeToString(fileBytes);
        return base64String;
    }

	@Override
	public PageResult<ProductApplyVO> queryXdbxcpsqInfo(XdbxcpsqRequestVO request) {
		return dkbxsqxxService.queryXdbxcpsqInfo(request);
	}
    
}
