From 88692b8e3a10d728f4ec9948c8693b1403f9f6da Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E6=A2=81=E7=90=A6=E6=B6=9B?= Date: Sun, 7 Jul 2024 14:49:27 +0800 Subject: [PATCH] =?UTF-8?q?=E5=AE=8C=E5=96=84=E5=B8=82=E5=9C=BA=E6=B3=95?= =?UTF-8?q?=E8=A7=84=E5=BA=93=E6=B8=85=E5=8D=95=E6=B7=BB=E5=8A=A0=E6=8E=A5?= =?UTF-8?q?=E5=8F=A3?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../common/service/ILawsCommonService.java | 7 ++ .../service/impl/LawsCommonServiceImpl.java | 104 ++++++++++++++++++ .../entity/LawsMarketStandardDetailExcel.java | 24 ++-- .../xml/LawsMarketStandardDetailMapper.xml | 4 +- .../LawsMarketStandardDetailServiceImpl.java | 75 +++++++++---- .../jero/modules/laws/utils/ValidUtil.java | 19 +++- 6 files changed, 196 insertions(+), 37 deletions(-) diff --git a/laws-modules/src/main/java/com/jero/modules/laws/common/service/ILawsCommonService.java b/laws-modules/src/main/java/com/jero/modules/laws/common/service/ILawsCommonService.java index 3b199da2..d631e2ee 100644 --- a/laws-modules/src/main/java/com/jero/modules/laws/common/service/ILawsCommonService.java +++ b/laws-modules/src/main/java/com/jero/modules/laws/common/service/ILawsCommonService.java @@ -223,4 +223,11 @@ public interface ILawsCommonService { * @return */ Map getInfoByStandardNo(String standardNumber); + + /** + * 验证excel注解翻译是否匹配 + * @param obj + * @return + */ + StringBuilder validateFieldByDict(Object obj); } diff --git a/laws-modules/src/main/java/com/jero/modules/laws/common/service/impl/LawsCommonServiceImpl.java b/laws-modules/src/main/java/com/jero/modules/laws/common/service/impl/LawsCommonServiceImpl.java index e6c8b029..f9c2437f 100644 --- a/laws-modules/src/main/java/com/jero/modules/laws/common/service/impl/LawsCommonServiceImpl.java +++ b/laws-modules/src/main/java/com/jero/modules/laws/common/service/impl/LawsCommonServiceImpl.java @@ -4,20 +4,29 @@ import cn.hutool.core.io.FileUtil; import cn.hutool.core.text.CharSequenceUtil; import cn.hutool.core.util.StrUtil; import com.alibaba.fastjson.JSON; +import com.alibaba.fastjson.JSONObject; import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper; import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper; import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper; import com.baomidou.mybatisplus.core.metadata.IPage; import com.baomidou.mybatisplus.core.toolkit.IdWorker; import com.baomidou.mybatisplus.extension.plugins.pagination.Page; +import com.fasterxml.jackson.annotation.JsonFormat; +import com.fasterxml.jackson.core.JsonProcessingException; +import com.fasterxml.jackson.databind.ObjectMapper; import com.jero.common.api.vo.ResultCommon; +import com.jero.common.aspect.annotation.Dict; +import com.jero.common.constant.CommonConstant; import com.jero.common.constant.enums.LanguageEnum; import com.jero.common.constant.enums.YesOrNoEnum; import com.jero.common.exception.JeroBootException; +import com.jero.common.system.api.ISysBaseAPI; import com.jero.common.system.vo.LoginUser; +import com.jero.common.system.vo.SysDictItemCore; import com.jero.common.util.IntekeyUtils; import com.jero.common.util.MessageUtils; import com.jero.common.util.MinioUtil; +import com.jero.common.util.oConvertUtils; import com.jero.modules.activiti.process.common.enums.StandardStateEnum; import com.jero.modules.laws.common.constant.FieldCommon; import com.jero.modules.laws.common.mapper.LawsCommonMapper; @@ -69,6 +78,7 @@ import org.apache.poi.ss.util.CellRangeAddress; import org.apache.poi.xssf.usermodel.XSSFSheet; import org.apache.poi.xssf.usermodel.XSSFWorkbook; import org.apache.shiro.SecurityUtils; +import org.jeecgframework.poi.excel.annotation.Excel; import org.jetbrains.annotations.NotNull; import org.springframework.beans.factory.annotation.Value; import org.springframework.mock.web.MockMultipartFile; @@ -79,6 +89,7 @@ import org.springframework.web.multipart.MultipartFile; import javax.annotation.Resource; import javax.servlet.http.HttpServletResponse; import java.io.*; +import java.lang.reflect.Field; import java.net.URLEncoder; import java.nio.charset.StandardCharsets; import java.nio.file.Files; @@ -153,6 +164,8 @@ public class LawsCommonServiceImpl implements ILawsCommonService { private ISysUserService sysUserRoleService; @Resource private ILawsPartNameStandardService partNameStandardService; + @Resource + private ISysBaseAPI sysBaseAPI; public static boolean ENCRYPT_ENABLE; public static String DECRYPT_URL; @@ -2619,6 +2632,7 @@ public class LawsCommonServiceImpl implements ILawsCommonService { return null; } + /** * 创建企标导入模板的Workbook * @@ -2779,4 +2793,94 @@ public class LawsCommonServiceImpl implements ILawsCommonService { this.setRow3CellValue(row3, fieldList, style); return workbook; } + + + @Override + public StringBuilder validateFieldByDict(Object record) { + StringBuilder msg = new StringBuilder(); + List listDict = sysBaseAPI.getDictItemAll(); + if(CollectionUtils.isEmpty(listDict)){ + return msg; + } + ObjectMapper mapper = new ObjectMapper(); + String json; + String jsonError = "json解析失败"; + try { + json = mapper.writeValueAsString(record); + } catch (JsonProcessingException e) { + log.error(jsonError + e.getMessage(), e); + throw new JeroBootException(jsonError); + } + JSONObject item; + try { + item = JSON.parseObject(json); + } catch (Exception e) { + log.error(jsonError + e.getMessage(), e); + throw new JeroBootException(jsonError); + } + + for (Field field : oConvertUtils.getAllFields(record)) { + field.setAccessible(true); + if (field.getAnnotation(Excel.class) != null) { + String code = field.getAnnotation(Excel.class).dicCode(); + if(StringUtils.isBlank(code)){ + continue; + } + String name = field.getAnnotation(Excel.class).name(); + String text = field.getAnnotation(Excel.class).dicText(); + String table = field.getAnnotation(Excel.class).dictTable(); + String key = String.valueOf(item.get(field.getName())); + if(StringUtils.isBlank(key) || Objects.equals("null", key)){ + item.put(field.getName(), null); + continue; + } + String textValue = translateDictValue(code, text, table, key, listDict); + if(StringUtils.isBlank(textValue)){ + msg.append(name).append("数据不存在;"); + }else{ + item.put(field.getName(), textValue); + } + } + } + return msg; + } + + /** + * 翻译字典文本 + * + * @param code 编码 + * @param text 值 + * @param table 表名 + * @param key 键 + * @param listDict 字典集合 + */ + private String translateDictValue(String code, String text, String table, String key, List listDict) { + if (oConvertUtils.isEmpty(key)) { + return null; + } + StringBuilder textValue = new StringBuilder(); + String[] keys = key.split(","); + for (String k : keys) { + String tmpValue = null; + if (k.trim().length() == 0) { + continue; //跳过循环 + } + if (!org.springframework.util.StringUtils.isEmpty(table)) { + tmpValue = sysBaseAPI.queryTableDictTextByKey(table, code, text, k.trim()); + } else { + List listNew = listDict.stream().filter(o -> Objects.equals(o.getDictCode(), code) && Objects.equals(o.getItemText(), k.trim())).collect(Collectors.toList()); + if (!org.springframework.util.CollectionUtils.isEmpty(listNew)) { + tmpValue = listNew.get(0).getItemText(); + } + } + + if (tmpValue != null) { + if (!"".equals(textValue.toString())) { + textValue.append(","); + } + textValue.append(tmpValue); + } + } + return textValue.toString(); + } } diff --git a/laws-modules/src/main/java/com/jero/modules/laws/marketStandard/entity/LawsMarketStandardDetailExcel.java b/laws-modules/src/main/java/com/jero/modules/laws/marketStandard/entity/LawsMarketStandardDetailExcel.java index 5450840d..d1ab1d40 100644 --- a/laws-modules/src/main/java/com/jero/modules/laws/marketStandard/entity/LawsMarketStandardDetailExcel.java +++ b/laws-modules/src/main/java/com/jero/modules/laws/marketStandard/entity/LawsMarketStandardDetailExcel.java @@ -123,7 +123,7 @@ public class LawsMarketStandardDetailExcel implements Serializable { /** * 国家 */ - @Excel(name = "*国家/地区", width = 15,dicCode = "market_state") + @Excel(name = "*国家/地区", width = 15) @NotBlank(message = "国家/地区不能为空") @ApiModelProperty(value = "国家") @Dict(dicCode = "market_state") @@ -132,7 +132,7 @@ public class LawsMarketStandardDetailExcel implements Serializable { /** * 适用车型 */ - @Excel(name = "适用车型", width = 15, dicCode = "applicable_vehicle_type") + @Excel(name = "适用车型", width = 15) @Dict(dicCode = "applicable_vehicle_type") @ApiModelProperty(value = "适用车型") private String applicableVehicle; @@ -148,7 +148,7 @@ public class LawsMarketStandardDetailExcel implements Serializable { */ @ApiModelProperty(value = "认证对象") @Dict(dicCode = "authentication_object") - @Excel(name = "认证对象", width = 15, dicCode = "authentication_object") + @Excel(name = "认证对象", width = 15) private String authenticationObject; /** @@ -162,7 +162,7 @@ public class LawsMarketStandardDetailExcel implements Serializable { * 类型 */ @Dict(dicCode = "type") - @Excel(name = "类型", width = 15, dicCode = "type") + @Excel(name = "类型", width = 15) @ApiModelProperty(value = "类型") private String type; @@ -171,7 +171,7 @@ public class LawsMarketStandardDetailExcel implements Serializable { */ @Dict(dicCode = "dynamic_type") @ApiModelProperty(value = "动力类型") - @Excel(name = "动力类型", width = 15, dicCode = "dynamic_type") + @Excel(name = "动力类型", width = 15) private String dynamicType; /** @@ -180,7 +180,7 @@ public class LawsMarketStandardDetailExcel implements Serializable { @Dict(dictTable = "sys_depart", dicCode = "id", dicText = "depart_name") @ApiModelProperty(value = "主控部门") @NotBlank(message = "主控部门不能为空") - @Excel(name = "*主控部门", width = 15,dictTable = "sys_depart", dicCode = "id", dicText = "depart_name") + @Excel(name = "*主控部门", width = 15) private String mainDept; /** @@ -189,7 +189,7 @@ public class LawsMarketStandardDetailExcel implements Serializable { @ApiModelProperty(value = "主控部门专业模块") @NotBlank(message = "主控部门专业模块不能为空") @Dict(dicCode = "professional_module") - @Excel(name = "*主控部门专业模块", width = 15, dicCode = "professional_module") + @Excel(name = "*主控部门专业模块", width = 15) private String mainDeptProfMode; /** @@ -197,14 +197,14 @@ public class LawsMarketStandardDetailExcel implements Serializable { */ @Dict(dictTable = "sys_depart", dicCode = "id", dicText = "depart_name") @ApiModelProperty(value = "关联部门") - @Excel(name = "关联部门", width = 15,dictTable = "sys_depart", dicCode = "id", dicText = "depart_name") + @Excel(name = "关联部门", width = 15) private String associateDept; /** * 关联部门专业模块 */ @ApiModelProperty(value = "关联部门专业模块") - @Excel(name = "关联部门专业模块", width = 15, dicCode = "professional_module") + @Excel(name = "关联部门专业模块", width = 15) @Dict(dicCode = "professional_module") private String associateDeptProfMode; @@ -212,7 +212,7 @@ public class LawsMarketStandardDetailExcel implements Serializable { * 批量限制 */ @ApiModelProperty(value = "批量限制") - @Excel(name = "批量限制", width = 15, dicCode = "batch_restriction") + @Excel(name = "批量限制", width = 15) @Dict(dicCode = "batch_restriction") private String batchLimit; @@ -221,7 +221,7 @@ public class LawsMarketStandardDetailExcel implements Serializable { */ @Dict(dicCode = "automatic_driving_level") @ApiModelProperty(value = "自动驾驶等级") - @Excel(name = "自动驾驶等级", width = 15,dicCode = "automatic_driving_level") + @Excel(name = "自动驾驶等级", width = 15) private String autopilotLevel; /** @@ -229,7 +229,7 @@ public class LawsMarketStandardDetailExcel implements Serializable { */ @Dict(dicCode = "requirement_type") @ApiModelProperty(value = "要求类型") - @Excel(name = "要求类型", width = 15,dicCode = "requirement_type") + @Excel(name = "要求类型", width = 15) private String requireType; diff --git a/laws-modules/src/main/java/com/jero/modules/laws/marketStandard/mapper/xml/LawsMarketStandardDetailMapper.xml b/laws-modules/src/main/java/com/jero/modules/laws/marketStandard/mapper/xml/LawsMarketStandardDetailMapper.xml index 25fe9a1c..d52eb449 100644 --- a/laws-modules/src/main/java/com/jero/modules/laws/marketStandard/mapper/xml/LawsMarketStandardDetailMapper.xml +++ b/laws-modules/src/main/java/com/jero/modules/laws/marketStandard/mapper/xml/LawsMarketStandardDetailMapper.xml @@ -10,10 +10,10 @@ from laws_domestic_standard where del_flag = 0 union all - select id, standard_number, + select id, standard_number from laws_enterprise_standard where del_flag = 0) a where standard_number in - + #{item} diff --git a/laws-modules/src/main/java/com/jero/modules/laws/marketStandard/service/impl/LawsMarketStandardDetailServiceImpl.java b/laws-modules/src/main/java/com/jero/modules/laws/marketStandard/service/impl/LawsMarketStandardDetailServiceImpl.java index 4c36cca5..165d0d60 100644 --- a/laws-modules/src/main/java/com/jero/modules/laws/marketStandard/service/impl/LawsMarketStandardDetailServiceImpl.java +++ b/laws-modules/src/main/java/com/jero/modules/laws/marketStandard/service/impl/LawsMarketStandardDetailServiceImpl.java @@ -10,10 +10,10 @@ import com.jero.common.exception.JeroBootException; import com.jero.common.system.query.QueryGenerator; import com.jero.common.system.vo.LoginUser; import com.jero.common.util.ImportExcelUtil; +import com.jero.modules.laws.common.service.ILawsCommonService; import com.jero.modules.laws.marketStandard.entity.LawsMarketStandard; import com.jero.modules.laws.marketStandard.entity.LawsMarketStandardDetail; import com.jero.modules.laws.marketStandard.entity.LawsMarketStandardDetailExcel; -import com.jero.modules.laws.marketStandard.entity.MarketStandardDetailVO; import com.jero.modules.laws.marketStandard.mapper.LawsMarketStandardDetailMapper; import com.jero.modules.laws.marketStandard.service.ILawsMarketStandardDetailService; import com.jero.modules.laws.marketStandard.service.ILawsMarketStandardService; @@ -23,7 +23,6 @@ import org.apache.commons.lang3.StringUtils; import org.apache.shiro.SecurityUtils; import org.jeecgframework.poi.excel.ExcelImportUtil; import org.jeecgframework.poi.excel.entity.ImportParams; -import org.springframework.beans.factory.annotation.Value; import org.springframework.core.io.ClassPathResource; import org.springframework.stereotype.Service; import org.springframework.transaction.annotation.Transactional; @@ -32,7 +31,9 @@ import org.springframework.web.multipart.MultipartFile; import javax.annotation.Resource; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; -import java.io.*; +import java.io.IOException; +import java.io.InputStream; +import java.io.OutputStream; import java.util.ArrayList; import java.util.Arrays; import java.util.List; @@ -45,13 +46,15 @@ import java.util.stream.Collectors; * @createDate 2024-06-29 11:34:34 */ @Service -public class LawsMarketStandardDetailServiceImpl extends ServiceImpl - implements ILawsMarketStandardDetailService { +public class LawsMarketStandardDetailServiceImpl extends ServiceImpl implements ILawsMarketStandardDetailService { @Resource private ILawsMarketStandardService marketStandardService; @Resource private LawsMarketStandardDetailMapper lawsMarketStandardDetailMapper; + @Resource + private ILawsCommonService lawsCommonService; @Override @@ -115,7 +118,6 @@ public class LawsMarketStandardDetailServiceImpl extends ServiceImpl importExcel(MultipartFile file,String manifestId) { - List list = new ArrayList<>(); // 校验文件是否为excel if (!ImportExcelUtil.checkExcelFile(file)) { return Result.error("请导入excel文件"); @@ -123,50 +125,86 @@ public class LawsMarketStandardDetailServiceImpl extends ServiceImpl listLawsMarketStandardDetailExcel = ExcelImportUtil.importExcel(file.getInputStream(), LawsMarketStandardDetailExcel.class, params); - if(!CollectionUtils.isEmpty(listLawsMarketStandardDetailExcel) && listLawsMarketStandardDetailExcel.size() >= 2){ - listLawsMarketStandardDetailExcel = listLawsMarketStandardDetailExcel.subList(2,listLawsMarketStandardDetailExcel.size()); + List listLawsMarketStandardDetailExcel = ExcelImportUtil.importExcel( + file.getInputStream(), LawsMarketStandardDetailExcel.class, params); + if(!CollectionUtils.isEmpty(listLawsMarketStandardDetailExcel) + && listLawsMarketStandardDetailExcel.size() >= 2){ + listLawsMarketStandardDetailExcel = listLawsMarketStandardDetailExcel. + subList(2,listLawsMarketStandardDetailExcel.size()); } if(CollectionUtils.isEmpty(listLawsMarketStandardDetailExcel)){ return Result.error("导入数据为空"); } List listLawsMarketStandardDetail = JSON.parseArray( JSON.toJSONString(listLawsMarketStandardDetailExcel),LawsMarketStandardDetail.class); - List listStandardNumberNotNull = listLawsMarketStandardDetail.stream().filter(o->!StringUtils.isBlank(o.getStandardNumber())).collect(Collectors.toList()); + List listStandardNumberNotNull = listLawsMarketStandardDetail.stream().filter(o-> + !StringUtils.isBlank(o.getStandardNumber())).collect(Collectors.toList()); if(CollectionUtils.isEmpty(listStandardNumberNotNull)){ return Result.error("标准编号不能为空"); } - List listStandardNumber = listStandardNumberNotNull.stream().map(LawsMarketStandardDetail::getStandardNumber).collect(Collectors.toList()); + List listStandardNumber = listStandardNumberNotNull.stream() + .map(LawsMarketStandardDetail::getStandardNumber).collect(Collectors.toList()); // 根据标准编号获取标准id List listIds = lawsMarketStandardDetailMapper.getStandardData(listStandardNumber); if(CollectionUtils.isEmpty(listIds)){ return Result.error("标准编号不存在"); } StringBuilder m = new StringBuilder(); + // 获取当前清单的标准 + LambdaQueryWrapper queryLawsMarketStandardDetail = new LambdaQueryWrapper<>(); + queryLawsMarketStandardDetail.select(LawsMarketStandardDetail::getStandardId); + queryLawsMarketStandardDetail.eq(LawsMarketStandardDetail::getManifestId,manifestId); + queryLawsMarketStandardDetail.isNotNull(LawsMarketStandardDetail::getStandardId); + List listStandardId = list(queryLawsMarketStandardDetail); + List listStandardIds = new ArrayList<>(); + if(!CollectionUtils.isEmpty(listStandardId)){ + listStandardIds = listStandardId.stream().map(LawsMarketStandardDetail::getStandardId) + .collect(Collectors.toList()); + } // 验证标准编号是否重复 - List listStandardNum = new ArrayList<>(); + List listStandardIdRepeat = new ArrayList<>(); for (int i = 0; i < listLawsMarketStandardDetail.size(); i++) { LawsMarketStandardDetail p = listLawsMarketStandardDetail.get(i); p.setManifestId(manifestId); // 校验数据 - StringBuilder g = ValidUtil.validateReturnBuilder(p); + StringBuilder s = new StringBuilder(); + StringBuilder g = ValidUtil.validateReturnBuilder(p); if(!StringUtils.isEmpty(g.toString())){ - m.append(i + 4).append("行,").append(g.toString()); + s.append(g.toString()); } + // 验证字典值是否满足条件 + StringBuilder dict = lawsCommonService.validateFieldByDict(p); + if(!StringUtils.isBlank(dict)){ + s.append(dict); + } + // 验证标准编号 if(!StringUtils.isBlank(p.getStandardNumber())){ List listStandardNumberData = listIds.stream().filter(o-> Objects.equals(o.getStandardNumber(),p.getStandardNumber())).collect(Collectors.toList()); - if(CollectionUtils.isEmpty(listStandardNumberData)){ - m.append(i + 4).append("行,标准编号不存在"); + s.append("标准编号不存在;"); + }else{ + String id = listStandardNumberData.get(0).getStandardId(); + p.setStandardId(id); + if(!CollectionUtils.isEmpty(listStandardIds) && listStandardIds.contains(id)){ + s.append("标准编号已存在;"); + }else if(!CollectionUtils.isEmpty(listStandardIdRepeat) && listStandardIdRepeat.contains(id)){ + s.append("标准编号重复;"); + } + listStandardIdRepeat.add(listStandardNumberData.get(0).getId()); } } + if(!StringUtils.isBlank(s.toString())){ + m.append("第").append(i + 4).append("行,").append(s.toString()); + } } + if(!StringUtils.isBlank(m.toString())){ return Result.error(m.toString()); } @@ -176,6 +214,7 @@ public class LawsMarketStandardDetailServiceImpl extends ServiceImpl