ludc
2023-07-20 7bacb4be65407d9f8a6bdcd44b4192aa32de91a1
Source/UBCS/ubcs-service/ubcs-code/src/main/java/com/vci/ubcs/code/service/impl/MdmIOServiceImpl.java
@@ -3,9 +3,11 @@
import com.alibaba.fastjson.JSONObject;
import com.alibaba.nacos.common.utils.StringUtils;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.vci.file.util.VciZipUtil;
import com.vci.ubcs.code.bo.AttributeValue;
import com.vci.ubcs.code.bo.CodeClassifyFullInfoBO;
import com.vci.ubcs.code.bo.CodeTemplateAttrSqlBO;
import com.vci.ubcs.code.dto.CodeExportAttrDTO;
import com.vci.ubcs.code.dto.CodeOrderDTO;
import com.vci.ubcs.code.entity.CodeAllCode;
import com.vci.ubcs.code.enumpack.CodeDefaultLC;
@@ -151,11 +153,18 @@
    */
   @Autowired
   private IBtmTypeClient btmTypeClient;
   /***
    * 密级服务
    */
   @Resource
   private IWebSecretClient secretService;
   /**
    * 导出的十万条
    */
   public static final int EXPORT_LIMIT = 100000;
   /**
    * 批量申请:选取选中分类下的所有模板关键属性,相似属性,必填属性,写入execl中
    *
@@ -1450,6 +1459,210 @@
      return dataGrid;
   }
   /**
    * 导出主题库的数据
    *
    * @param exportAttrDTO 导出相关的配置,必须要有主题库分类的主键
    * @return 导出的excel的文件
    */
   @Override
   public String exportCode(CodeExportAttrDTO exportAttrDTO) {
      VciBaseUtil.alertNotNull(exportAttrDTO,"导出的配置",exportAttrDTO.getCodeClassifyOid(),"主题库分类的主键");
      CodeClassifyFullInfoBO classifyFullInfo = classifyService.getClassifyFullInfo(exportAttrDTO.getCodeClassifyOid());
      //获取最新的模板
      CodeClassifyTemplateVO templateVO = engineService.getUsedTemplateByClassifyOid(exportAttrDTO.getCodeClassifyOid());
      //先查询数据
      String btmTypeId = classifyFullInfo.getTopClassifyVO().getBtmTypeId();
      Map<String, String> conditionMap = exportAttrDTO.getConditionMap();
      if(conditionMap == null){
         conditionMap = new HashMap<>();
      }
      if(conditionMap.containsKey(VciQueryWrapperForDO.OID_FIELD)){
         conditionMap.put(VciQueryWrapperForDO.OID_FIELD,QueryOptionConstant.IN + "(" + VciBaseUtil.toInSql(conditionMap.get(VciQueryWrapperForDO.OID_FIELD)) + ")");
      }
      PageHelper pageHelper = new PageHelper(exportAttrDTO.getLimit()==null?-1:exportAttrDTO.getLimit());
      pageHelper.setPage(exportAttrDTO.getPage()==null?1:exportAttrDTO.getPage());
      pageHelper.setSort(exportAttrDTO.getSort());
      pageHelper.setOrder(exportAttrDTO.getOrder());
      pageHelper.addDefaultDesc("createTime");
      conditionMap.put("codeclsfpath","*" + exportAttrDTO.getCodeClassifyOid() + "*");
      conditionMap.put("lastr", "1");
      conditionMap.put("lastv", "1");
      //先查询总数
      int total = 0;
      if(exportAttrDTO.getEndPage()!=null && exportAttrDTO.getEndPage()>0
         &&exportAttrDTO.getPage() !=null && exportAttrDTO.getPage() >0
         &&exportAttrDTO.getEndPage()>exportAttrDTO.getPage()){
         //从多少页到多少页的查询方式,
         for(int i = exportAttrDTO.getPage() ;i <= exportAttrDTO.getEndPage();i++){
            PageHelper thisPage = new PageHelper(exportAttrDTO.getLimit()==null?-1:exportAttrDTO.getLimit());
            thisPage.setPage(exportAttrDTO.getPage()==null?1:exportAttrDTO.getPage());
            thisPage.setSort(exportAttrDTO.getSort());
            thisPage.setOrder(exportAttrDTO.getOrder());
            thisPage.addDefaultDesc("createTime");
            total += boService.queryCount(btmTypeId, conditionMap);
         }
      }else{
         total=boService.queryCount(btmTypeId, conditionMap);
      }
      List<String> selectFieldList = new ArrayList<>();
      if(!CollectionUtils.isEmpty(exportAttrDTO.getAttrIdIndexMap())){
         selectFieldList = exportAttrDTO.getAttrIdIndexMap().values().stream().map(s->s.toLowerCase(Locale.ROOT)).collect(Collectors.toList());
      }else{
         selectFieldList = templateVO.getAttributes().stream().filter(s->VciBaseUtil.getBoolean(s.getFormDisplayFlag())
            ||VciBaseUtil.getBoolean(s.getTableDisplayFlag())).map(s->s.getId().toLowerCase(Locale.ROOT)).collect(Collectors.toList());
      }
      //参照让平台直接查询就行
      List<String> finalSelectFieldList = selectFieldList;
      List<CodeClassifyTemplateAttrVO> referAttrVOs = templateVO.getAttributes().stream().filter(
         s -> StringUtils.isNotBlank(s.getReferBtmId())
            &&
            (finalSelectFieldList.size() ==0 || finalSelectFieldList.contains(s.getId().toLowerCase(Locale.ROOT)))
      ).collect(Collectors.toList());
      if(!CollectionUtils.isEmpty(referAttrVOs)){
         for (int i = 0; i < referAttrVOs.size(); i++) {
            selectFieldList.add(referAttrVOs.get(i).getId() + ".name");
         }
      }
      List<String> excelNameList = new CopyOnWriteArrayList<>();
      String tempFolder = LocalFileUtil.getDefaultTempFolder();
      if(total>EXPORT_LIMIT){
         //分组来执行
         int queryCount = (total-total%EXPORT_LIMIT)/EXPORT_LIMIT;
         if(total%EXPORT_LIMIT>0){
            queryCount = queryCount + 1;
         }
         List<Integer> indexList = new ArrayList<>();
         for (int i = 0; i <queryCount ; i++) {
            indexList.add(i);
         }
         Map<String, String> finalConditionMap = conditionMap;
         //并行查询看看
         SessionInfo sessionInfo = VciBaseUtil.getCurrentUserSessionInfo();
         indexList.stream().forEach(index->{
            //线程的方式,所以需要设置当前用户
            VciBaseUtil.setCurrentUserSessionInfo(sessionInfo);
            PageHelper thisPage = new PageHelper(EXPORT_LIMIT);
            thisPage.setPage(index+1);
            thisPage.setSort(exportAttrDTO.getSort());
            thisPage.setOrder(exportAttrDTO.getOrder());
            thisPage.addDefaultDesc("createTime");
            selectDataAndExportExcelName(btmTypeId, finalConditionMap,thisPage,finalSelectFieldList,
               classifyFullInfo,templateVO,exportAttrDTO,
               excelNameList,tempFolder,index);
         });
      }else{
         pageHelper.setLimit(total);
         pageHelper.setPage(1);
         selectDataAndExportExcelName(btmTypeId,conditionMap,pageHelper,finalSelectFieldList,
            classifyFullInfo,templateVO,exportAttrDTO,
            excelNameList,tempFolder,1);
      }
      if(excelNameList.size() ==0){
         throw new VciBaseException("没有数据可以被导出");
      }
      if(excelNameList.size() == 1){
         return excelNameList.get(0);
      }
      //是多个,我们需要打成压缩包
      String zipFileName = LocalFileUtil.getDefaultTempFolder() + File.separator + classifyFullInfo.getCurrentClassifyVO().getId() + "_" + classifyFullInfo.getCurrentClassifyVO().getName() + "_导出_" + excelNameList.size()+".zip";
      VciZipUtil zipUtil = new VciZipUtil();
      File file = new File(tempFolder);
      zipUtil.addFileToZip(file,zipFileName);
      File[] files = file.listFiles();
      for (int i = 0; i < files.length; i++) {
         LocalFileUtil.deleteTempFile(files[i],false);
      }
      LocalFileUtil.deleteTempFile(file,true);
      return zipFileName;
   }
   /**
    * 查询数据并导出到excel
    * @param btmTypeId 业务类型
    * @param conditionMap 查询条件
    * @param pageHelper 分页
    * @param selectFieldList 查询的字段
    * @param classifyFullInfo 分类的全部信息
    * @param templateVO 模板的信息
    * @param exportAttrDTO 导出的属性
    * @param excelNameList excel的文件名称
    * @param tempFolder 临时文件夹
    * @param excelIndex excel的顺序
    */
   private void selectDataAndExportExcelName(String btmTypeId, Map<String, String> conditionMap, PageHelper pageHelper, List<String> selectFieldList,
                                   CodeClassifyFullInfoBO classifyFullInfo, CodeClassifyTemplateVO templateVO, CodeExportAttrDTO exportAttrDTO,
                                   List<String> excelNameList, String tempFolder,
                                   Integer excelIndex) {
      DataGrid<Map<String, String>> dataGrid = engineService.queryGrid(btmTypeId, templateVO, conditionMap, pageHelper);
      if(dataGrid == null || CollectionUtils.isEmpty(dataGrid.getData())){
         return;
      }
      //转换数据
      List<Map<String, String>> dataMap = dataGrid.getData();
      //封装查询出来的数据
      engineService.wrapperData(dataMap, templateVO, selectFieldList,false);
      //modify by weidy@2022-09-27
      //因为在列表和表单的显示的时候,我们的开关类型页面会处理,但是在导出的时候,我们需要将true和false都替换成中文
      engineService.wrapperBoolean(dataMap,templateVO);
      Map<String, CodeClassifyTemplateAttrVO> attrVOMap = templateVO.getAttributes().stream().filter(s->selectFieldList.contains(s.getId().toLowerCase(Locale.ROOT))).collect(Collectors.toMap(s -> s.getId().toLowerCase(Locale.ROOT), t -> t));
      Map<Integer, String> attrIdIndexMap = exportAttrDTO.getAttrIdIndexMap();
      if (CollectionUtils.isEmpty(attrIdIndexMap)) {
         attrIdIndexMap = templateVO.getAttributes().stream().filter(s->selectFieldList.contains(s.getId().toLowerCase(Locale.ROOT))).collect(Collectors.toMap(s -> s.getOrderNum(), t -> t.getId()));
      }
      List<Integer> indexList = attrIdIndexMap.keySet().stream().sorted().collect(Collectors.toList());
      String excelName = tempFolder + File.separator +
         classifyFullInfo.getCurrentClassifyVO().getId() + "_" + classifyFullInfo.getCurrentClassifyVO().getName() + "_导出_" + excelIndex + ".xls";
      try {
         new File(excelName).createNewFile();
      } catch (Throwable e) {
         throw new VciBaseException(LangBaseUtil.getErrorMsg(e), new String[]{excelName}, e);
      }
      excelNameList.add(excelName);
      List<WriteExcelData> excelDataList = new ArrayList<>();
      Workbook workbook = new HSSFWorkbook();
      for (int i = 0; i < indexList.size(); i++) {
         String attrId = attrIdIndexMap.get(indexList.get(i)).toLowerCase(Locale.ROOT);
         if (attrVOMap.containsKey(attrId)) {
            CodeClassifyTemplateAttrVO attrVO = attrVOMap.get(attrId);
            Object text = attrVO.getName();
            text = exportKeyAndRequired(workbook,attrVO,text);
            WriteExcelData excelData = new WriteExcelData(0, i, text);
            if(text instanceof RichTextString){
               excelData.setFontColor(String.valueOf(HSSFColor.HSSFColorPredefined.RED.getIndex()));
            }
            excelDataList.add(excelData);
         }
      }
      final Integer[] rowIndex = {0};
      Map<Integer, String> finalAttrIdIndexMap = attrIdIndexMap;
      dataMap.stream().forEach(data -> {
         rowIndex[0]++;
         for (int i = 0; i < indexList.size(); i++) {
            Integer index = indexList.get(i);
            String attrId = finalAttrIdIndexMap.get(index).toLowerCase(Locale.ROOT);
            if (attrVOMap.containsKey(attrId)) {
               CodeClassifyTemplateAttrVO attrVO = attrVOMap.get(attrId);
               if (StringUtils.isNotBlank(attrVO.getEnumId()) || StringUtils.isNotBlank(attrVO.getEnumString())) {
                  attrId = attrId + "Text";
               }
               if (StringUtils.isNotBlank(attrVO.getReferBtmId()) || StringUtils.isNotBlank(attrVO.getReferConfig())) {
                  attrId = attrId + "name";
               }
               if(VciQueryWrapperForDO.LC_STATUS_FIELD.equalsIgnoreCase(attrId)){
                  attrId = VciQueryWrapperForDO.LC_STATUS_FIELD_TEXT.toLowerCase(Locale.ROOT);
               }
               excelDataList.add(new WriteExcelData(rowIndex[0], i, data.getOrDefault(attrId, "")));
            }
         }
      });
      WriteExcelOption excelOption = new WriteExcelOption(excelDataList);
      ExcelUtil.writeDataToFile(excelName, excelOption);
   }
   @Override
   public R batchImportData(List<CodeImprotSaveDatVO> codeImprotSaveDatVOList, String classifyAttr, boolean isImprot) {
      WriteExcelOption eo = new WriteExcelOption();