文档库--导入

This commit is contained in:
zhn
2022-03-07 23:03:57 +08:00
parent 435e4565f4
commit 1936890016
8 changed files with 771 additions and 260 deletions
@@ -0,0 +1,37 @@
package com.jero.common.constant.enums;
/**
* 中英文切换标识
* @description
* @date 2022/1/21 15:22
* @auth zhn
*/
public enum IsMustEnum {
YES("是","0"),
NO("否","1");
String name;
String value;
private IsMustEnum(String name, String value) {
this.name = name;
this.value = value;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getValue() {
return value;
}
public void setValue(String value) {
this.value = value;
}
}
@@ -646,5 +646,26 @@ public class DateUtils extends PropertyEditorSupport {
calendar.setTime(getDate()); calendar.setTime(getDate());
return calendar.get(Calendar.YEAR); return calendar.get(Calendar.YEAR);
} }
/***
* @Description: 判断多个日期是否都符合日期格式
* @Author: yangxuenan
* @Date: 2020/12/31 11:31
* @Param: [str]
* @Return: boolean
*/
public static boolean isValidDate(String str){
SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");
String[] strArr = str.split(",");
for (String dateStr : strArr) {
String replace = dateStr.replace("/", "-");
try {
simpleDateFormat.setLenient(false);
simpleDateFormat.parse(replace);
} catch (Exception e){
return false;
}
}
return true;
}
} }
@@ -413,16 +413,8 @@ public class BussDocumentLibraryEOController extends JeroController<BussDocument
@ApiOperation(value = "导入.zip") @ApiOperation(value = "导入.zip")
@PostMapping(value = "/importZip") @PostMapping(value = "/importZip")
public Result<?> importZip(@RequestParam(value = "file", required = false) MultipartFile file) throws Exception { public Result<?> importZip(@RequestParam(value = "file", required = false) MultipartFile file) throws Exception {
//验证文件名是否合格 String result = bussDocumentLibraryEOService.importZip(file);
//截取后缀名 return Result.OK(result);
int pos = file.getOriginalFilename().lastIndexOf(".");
String str = file.getOriginalFilename().substring(pos+1).toLowerCase();
String filenameorg = file.getOriginalFilename().substring(0,pos);
//判断上传文件必须是zip
if (!str.equals("zip")) {
return Result.error("请上传zip文件");
}
return Result.OK("导入成功");
} }
@ApiOperation(value = "推送") @ApiOperation(value = "推送")
@@ -78,4 +78,7 @@ public interface BussDocumentLibraryEOMapper extends BaseMapper<BussDocumentLibr
List<Map<String, Object>> getInfoListByReplaceStandard(@Param("replaceStandard") String replaceStandard); List<Map<String, Object>> getInfoListByReplaceStandard(@Param("replaceStandard") String replaceStandard);
List<Map<String,Object>> getListBySerialNumber(@Param("serialNumbers") String serialNumbers);
} }
@@ -70,5 +70,16 @@
</foreach> </foreach>
</if> </if>
</select> </select>
<!--根据编码列表查询-->
<select id="getListBySerialNumber" resultType="java.util.LinkedHashMap">
select * from buss_document_library
where 1=1
<if test="serialNumbers != null" >
and serial_number in
<foreach collection="serialNumbers.split(',')" index="index" item="item" open="(" separator="," close=")">
#{item}
</foreach>
</if>
</select>
</mapper> </mapper>
@@ -3,7 +3,7 @@ package com.jero.modules.document.service;
import com.baomidou.mybatisplus.core.metadata.IPage; import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.IService; import com.baomidou.mybatisplus.extension.service.IService;
import com.jero.modules.document.entity.BussDocumentLibraryEO; import com.jero.modules.document.entity.BussDocumentLibraryEO;
import org.apache.poi.ss.usermodel.Workbook; import org.springframework.web.multipart.MultipartFile;
import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse; import javax.servlet.http.HttpServletResponse;
@@ -72,6 +72,7 @@ public interface IBussDocumentLibraryEOService extends IService<BussDocumentLibr
/** /**
* 编辑数据查询 * 编辑数据查询
*
* @param id * @param id
* @return * @return
*/ */
@@ -79,6 +80,7 @@ public interface IBussDocumentLibraryEOService extends IService<BussDocumentLibr
/** /**
* 详情数据查询 * 详情数据查询
*
* @param id * @param id
* @return * @return
*/ */
@@ -86,18 +88,21 @@ public interface IBussDocumentLibraryEOService extends IService<BussDocumentLibr
/** /**
* 新增数据 * 新增数据
*
* @param map * @param map
*/ */
void addInfo(Map<String, Object> map); void addInfo(Map<String, Object> map);
/** /**
* 编辑数据 * 编辑数据
*
* @param map * @param map
*/ */
void updateInfo(Map<String, Object> map); void updateInfo(Map<String, Object> map);
/** /**
* 分页 * 分页
*
* @param parameter * @param parameter
* @return * @return
*/ */
@@ -105,6 +110,7 @@ public interface IBussDocumentLibraryEOService extends IService<BussDocumentLibr
/** /**
* 代替标准分页 * 代替标准分页
*
* @param parameter * @param parameter
* @return * @return
*/ */
@@ -112,6 +118,7 @@ public interface IBussDocumentLibraryEOService extends IService<BussDocumentLibr
/** /**
* ocr识别调取已入库文件分页 * ocr识别调取已入库文件分页
*
* @param parameter * @param parameter
* @return * @return
*/ */
@@ -119,24 +126,28 @@ public interface IBussDocumentLibraryEOService extends IService<BussDocumentLibr
/** /**
* 添加收藏 * 添加收藏
*
* @param id * @param id
*/ */
void addCollect(String id); void addCollect(String id);
/** /**
* 取消收藏 * 取消收藏
*
* @param id * @param id
*/ */
void cancelCollect(String id); void cancelCollect(String id);
/** /**
* 添加订阅 * 添加订阅
*
* @param id * @param id
*/ */
void addSubscribe(String id); void addSubscribe(String id);
/** /**
* 取消订阅 * 取消订阅
*
* @param id * @param id
*/ */
void cancelSubscribe(String id); void cancelSubscribe(String id);
@@ -144,6 +155,7 @@ public interface IBussDocumentLibraryEOService extends IService<BussDocumentLibr
/** /**
* 导出excel * 导出excel
*
* @param map * @param map
* @param response * @param response
* @param request * @param request
@@ -158,6 +170,7 @@ public interface IBussDocumentLibraryEOService extends IService<BussDocumentLibr
/** /**
* 模板下载 * 模板下载
*
* @param response * @param response
* @param request * @param request
*/ */
@@ -165,6 +178,7 @@ public interface IBussDocumentLibraryEOService extends IService<BussDocumentLibr
/** /**
* ocr识别调取已入库文件-中英文切换 * ocr识别调取已入库文件-中英文切换
*
* @param flag * @param flag
* @param cut * @param cut
* @return * @return
@@ -173,4 +187,6 @@ public interface IBussDocumentLibraryEOService extends IService<BussDocumentLibr
String pullMessage(String departIds, String ids, String documentIds); String pullMessage(String departIds, String ids, String documentIds);
String importZip(MultipartFile file);
} }
@@ -10,12 +10,14 @@ import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.jero.common.constant.CommonConstant; import com.jero.common.constant.CommonConstant;
import com.jero.common.constant.WebsocketConst; import com.jero.common.constant.WebsocketConst;
import com.jero.common.constant.enums.CutEnum; import com.jero.common.constant.enums.CutEnum;
import com.jero.common.constant.enums.IsMustEnum;
import com.jero.common.constant.enums.MessageTypeEnum; import com.jero.common.constant.enums.MessageTypeEnum;
import com.jero.common.constant.enums.ModuleEnum; import com.jero.common.constant.enums.ModuleEnum;
import com.jero.common.constant.enums.YesOrNoEnum; import com.jero.common.constant.enums.YesOrNoEnum;
import com.jero.common.exception.JeroBootException; import com.jero.common.exception.JeroBootException;
import com.jero.common.system.api.ISysBaseAPI; import com.jero.common.system.api.ISysBaseAPI;
import com.jero.common.system.vo.LoginUser; import com.jero.common.system.vo.LoginUser;
import com.jero.common.util.DateUtils;
import com.jero.generater.modules.online.cgform.entity.OnlCgformField; import com.jero.generater.modules.online.cgform.entity.OnlCgformField;
import com.jero.generater.modules.online.cgform.service.impl.OnlCgformFieldServiceImpl; import com.jero.generater.modules.online.cgform.service.impl.OnlCgformFieldServiceImpl;
import com.jero.modules.collection.entity.OnlCgformCollection; import com.jero.modules.collection.entity.OnlCgformCollection;
@@ -30,6 +32,7 @@ import com.jero.modules.log.service.IBussLogEOService;
import com.jero.modules.message.websocket.WebSocket; import com.jero.modules.message.websocket.WebSocket;
import com.jero.modules.oss.entity.OSSFile; import com.jero.modules.oss.entity.OSSFile;
import com.jero.modules.oss.service.IOSSFileService; import com.jero.modules.oss.service.IOSSFileService;
import com.jero.modules.split.common.FileUnZip;
import com.jero.modules.subscribe.entity.OnlCgformSubscribe; import com.jero.modules.subscribe.entity.OnlCgformSubscribe;
import com.jero.modules.subscribe.service.IOnlCgformSubscribeService; import com.jero.modules.subscribe.service.IOnlCgformSubscribeService;
import com.jero.modules.system.entity.SysAnnouncement; import com.jero.modules.system.entity.SysAnnouncement;
@@ -44,9 +47,13 @@ import com.jero.modules.system.service.impl.SysCategoryServiceImpl;
import com.jero.modules.system.service.impl.SysDictItemServiceImpl; import com.jero.modules.system.service.impl.SysDictItemServiceImpl;
import com.jero.modules.tag.entity.OnlCgformArea; import com.jero.modules.tag.entity.OnlCgformArea;
import com.jero.modules.tag.service.impl.OnlCgformAreaServiceImpl; import com.jero.modules.tag.service.impl.OnlCgformAreaServiceImpl;
import lombok.SneakyThrows;
import org.apache.commons.io.FileUtils;
import org.apache.commons.lang3.ObjectUtils; import org.apache.commons.lang3.ObjectUtils;
import org.apache.commons.lang3.StringUtils; import org.apache.commons.lang3.StringUtils;
import org.apache.poi.hssf.usermodel.HSSFCell;
import org.apache.poi.hssf.usermodel.HSSFCellStyle; import org.apache.poi.hssf.usermodel.HSSFCellStyle;
import org.apache.poi.hssf.usermodel.HSSFDateUtil;
import org.apache.poi.hssf.usermodel.HSSFRichTextString; import org.apache.poi.hssf.usermodel.HSSFRichTextString;
import org.apache.poi.hssf.usermodel.HSSFSheet; import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook; import org.apache.poi.hssf.usermodel.HSSFWorkbook;
@@ -57,6 +64,7 @@ import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet; import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.VerticalAlignment; import org.apache.poi.ss.usermodel.VerticalAlignment;
import org.apache.poi.ss.usermodel.Workbook; import org.apache.poi.ss.usermodel.Workbook;
import org.apache.poi.ss.usermodel.WorkbookFactory;
import org.apache.poi.ss.util.CellRangeAddress; import org.apache.poi.ss.util.CellRangeAddress;
import org.apache.poi.xssf.usermodel.XSSFWorkbook; import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.apache.shiro.SecurityUtils; import org.apache.shiro.SecurityUtils;
@@ -64,7 +72,9 @@ import org.aspectj.util.FileUtil;
import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.NotNull;
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value; import org.springframework.beans.factory.annotation.Value;
import org.springframework.mock.web.MockMultipartFile;
import org.springframework.stereotype.Service; import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;
import javax.annotation.Resource; import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletRequest;
@@ -74,6 +84,7 @@ import java.io.FileInputStream;
import java.io.FileOutputStream; import java.io.FileOutputStream;
import java.io.IOException; import java.io.IOException;
import java.io.OutputStream; import java.io.OutputStream;
import java.text.DateFormat;
import java.text.ParseException; import java.text.ParseException;
import java.text.SimpleDateFormat; import java.text.SimpleDateFormat;
import java.util.ArrayList; import java.util.ArrayList;
@@ -81,12 +92,14 @@ import java.util.Arrays;
import java.util.Date; import java.util.Date;
import java.util.HashMap; import java.util.HashMap;
import java.util.Iterator; import java.util.Iterator;
import java.util.LinkedHashMap;
import java.util.LinkedList; import java.util.LinkedList;
import java.util.List; import java.util.List;
import java.util.Map; import java.util.Map;
import java.util.UUID; import java.util.UUID;
import java.util.stream.Collectors; import java.util.stream.Collectors;
/** /**
* @Description: 文档库信息表 * @Description: 文档库信息表
* @Author: jero-boot * @Author: jero-boot
@@ -317,7 +330,8 @@ public class BussDocumentLibraryEOServiceImpl extends ServiceImpl<BussDocumentLi
} }
Map<String, Object> map = new HashMap<>(); Map<String, Object> map = new HashMap<>();
mapPut(cut, map, "field_show_type", onlCgformField.getFieldShowType(), "dict_field", onlCgformField.getDictField(), "db_field_name", onlCgformField.getDbFieldName(), "db_field_txt", onlCgformField.getDbFieldTxt(), "db_field_en_name", onlCgformField.getDbFieldEnName(), "area", areaName, sysCategoryTreeVOList); mapPut(cut, map, "field_show_type", onlCgformField.getFieldShowType(), "dict_field", onlCgformField.getDictField(), "db_field_name", onlCgformField.getDbFieldName(), "db_field_txt", onlCgformField.getDbFieldTxt(), "db_field_en_name", onlCgformField.getDbFieldEnName(), "area", areaName, sysCategoryTreeVOList);
map.put("field_must_input", onlCgformField.getFieldMustInput());//区域 map.put("field_must_input", onlCgformField.getFieldMustInput());//是否必填
map.put("db_length", onlCgformField.getDbLength());//字段长度
result.add(map); result.add(map);
} }
} }
@@ -616,7 +630,7 @@ public class BussDocumentLibraryEOServiceImpl extends ServiceImpl<BussDocumentLi
if (ObjectUtils.isNotEmpty(map.get("replace_standard_id"))) { if (ObjectUtils.isNotEmpty(map.get("replace_standard_id"))) {
map.put("replace_standard", map.get("replace_standard_id")); map.put("replace_standard", map.get("replace_standard_id"));
} }
if (ObjectUtils.isNotEmpty(map.get("corresponding_standard"))) { if (ObjectUtils.isNotEmpty(map.get("corresponding_standard_id"))) {
map.put("corresponding_standard", map.get("corresponding_standard_id")); map.put("corresponding_standard", map.get("corresponding_standard_id"));
} }
@@ -1717,6 +1731,7 @@ public class BussDocumentLibraryEOServiceImpl extends ServiceImpl<BussDocumentLi
/** /**
* 处理对应标准,代替标准,被代替标准(由id转中文) * 处理对应标准,代替标准,被代替标准(由id转中文)
*
* @param datas * @param datas
*/ */
private void getStandard(List<Map<String, Object>> datas) { private void getStandard(List<Map<String, Object>> datas) {
@@ -1809,7 +1824,6 @@ public class BussDocumentLibraryEOServiceImpl extends ServiceImpl<BussDocumentLi
} }
Row row = sheet.createRow(i + 1); Row row = sheet.createRow(i + 1);
i++; i++;
int sheetNum = 0; int sheetNum = 0;
@@ -2182,6 +2196,7 @@ public class BussDocumentLibraryEOServiceImpl extends ServiceImpl<BussDocumentLi
/** /**
* 推送 * 推送
*
* @param userIds * @param userIds
* @param documentIds * @param documentIds
* @return * @return
@@ -2219,8 +2234,10 @@ public class BussDocumentLibraryEOServiceImpl extends ServiceImpl<BussDocumentLi
return "推送成功"; return "推送成功";
} }
/** /**
* 属性字典处理中文 * 属性字典处理中文
*
* @param categoryList * @param categoryList
* @param entry * @param entry
* @param treeFieldList * @param treeFieldList
@@ -2244,4 +2261,401 @@ public class BussDocumentLibraryEOServiceImpl extends ServiceImpl<BussDocumentLi
} }
} }
} }
@SneakyThrows
@Override
public String importZip(MultipartFile file) {
List<String> list = getWorkbookTitle("cn");
String result = "";
int pos = file.getOriginalFilename().lastIndexOf(".");
String str = file.getOriginalFilename().substring(pos + 1).toLowerCase();
String filenameorg = file.getOriginalFilename().substring(0, pos);
//判断上传文件必须是zip
if (!str.equals("zip") && !str.equals("rar")) {
return "请上传zip格式的文件或rar格式的文件";
}
String path = uploadpath + "/modal/" + filenameorg;
File saveDirectory = new File(path);
if (!saveDirectory.isDirectory()) {
saveDirectory.mkdir();
}
FileUtils.copyInputStreamToFile(file.getInputStream(), new File(path + "/" + file.getOriginalFilename()));
//解压缩
String zipEntryName = FileUnZip.unZipFiles(path + "/" + file.getOriginalFilename(), path);
// 数据相关处理,
// 1.获取其中的Excel,
List<File> excelfilelist = FileUnZip.readImpExcelFile(zipEntryName);
System.gc();
if (excelfilelist.size() < 1) {
//删除原上传文件
FileUnZip.deleteDir(saveDirectory);
return "压缩包内没有上传Excel数据";
} else if (excelfilelist.size() > 1) {
//删除原上传文件
FileUnZip.deleteDir(saveDirectory);
return "压缩包内有多个Excel数据源";
}
File excelfile = excelfilelist.get(0);
Workbook workbook = WorkbookFactory.create(excelfile);
List<Map<String, String>> dataList = new ArrayList<>();
if (workbook != null) {
DateFormat df = new SimpleDateFormat("yyyy-MM-dd");
Sheet sheet = workbook.getSheetAt(0);
if (sheet != null) {
StringBuilder headerSb = new StringBuilder();
int rowNos = sheet.getLastRowNum();// 得到excel的总记录条数
for (int i = 0; i <= rowNos; i++) {// 遍历行
if (i == 1) {
continue;
}
Row row = sheet.getRow(i);
Row headerRow = sheet.getRow(0);
boolean isBlank = isRowEmpty(row);
if (row != null && !isBlank) {
int columNos = headerRow.getLastCellNum();// 表头总共的列数
Map<String, String> rowList = new LinkedHashMap<>();
for (int j = 0; j < columNos; j++) {
Cell cell = row.getCell(j);
Cell headerCell = headerRow.getCell(j);
if (cell != null) {
if (i == 0) {
cell.setCellType(HSSFCell.CELL_TYPE_STRING);
headerSb.append(cell.getStringCellValue() + ",");
} else {
if (HSSFCell.CELL_TYPE_NUMERIC == cell.getCellType() && HSSFDateUtil.isCellDateFormatted(cell)) {
Date d = cell.getDateCellValue();
rowList.put(headerCell.getStringCellValue(), df.format(d));
} else {
cell.setCellType(HSSFCell.CELL_TYPE_STRING);
String stringCellValue = cell.getStringCellValue();
if (StringUtils.isNotBlank(stringCellValue)) {
String replace = stringCellValue.replace(",", ",");
rowList.put(headerCell.getStringCellValue(), replace);
}
}
}
} else {
if (i != 0) {
rowList.put(headerCell.getStringCellValue(), "");
}
}
}
if (i != 0) {
dataList.add(rowList);
}
}
}
// 校验头部是否符合模板
String fields = list.get(0);
String substring = headerSb.substring(0, headerSb.length() - 1);
String excelHeader = substring.toString();
if (!fields.equals(excelHeader)) {
workbook.close();
//删除原上传文件
FileUnZip.deleteDir(saveDirectory);
return "读取失败,请严格按照模板文件导入数据";
}
}
}
//树形数据字典
List<SysCategory> categoryList = sysCategoryService.list();
//普通数据字典
List<SysDictItem> dictItemList = sysDictItemServiceImpl.selectItemsAll();
//表头
List<Map<String, Object>> addForm = getAddForm(ModuleEnum.DOCUMENT_LIBRARY.getValue(), "cn", "add");
result = exportDatas(dataList, addForm, categoryList, dictItemList, zipEntryName);
return result;
}
String exportDatas(List<Map<String, String>> dataList,
List<Map<String, Object>> fieldList,
List<SysCategory> categoryList,
List<SysDictItem> dictItemList,
String zipEntryName) {
//树形数据字典
List<String> treeNameList = categoryList.stream().map(SysCategory::getName).collect(Collectors.toList());
//普通数据字典
List<String> itemNameList = dictItemList.stream().map(SysDictItem::getItemText).collect(Collectors.toList());
int i = 2;
List<String> msgList = new ArrayList<>();
Map<String, Object> mapResult = new HashMap<>();
for (Map<String, String> stringStringMap : dataList) {
i++;
for (Map.Entry<String, String> entry : stringStringMap.entrySet()) {
int countError = 0; //记录失败数据数量
String errorMsg = "第" + i + "行:";
String key = entry.getKey();
if (key.contains("*")) {
key = key.replace("*", "");
}
String value = entry.getValue();
String field = "";
for (Map<String, Object> map : fieldList) {
String fieldNameCn = (String) map.get("db_field_txt");//中文名称
String fieldName = (String) map.get("db_field_name");//字段名
String mustInput = (String) map.get("field_must_input");//是否必填
String dbLength = map.get("db_length").toString();//字段长度
String fieldShowType = (String) map.get("field_show_type");//字段类型
if (key.equals(fieldNameCn)) {
field = fieldName;
//验证字段是否必填,长度,下拉框和树形的值是否匹配
if (FieldTypeEnum.TREE.getValue().equals(fieldShowType)) {
//树形
//判断是否必填
if (IsMustEnum.YES.getValue().equals(mustInput) && StringUtils.isBlank(value)) {
errorMsg += key + "为必填项,不能为空";
countError++;
}
//判断数据是否匹配
if (StringUtils.isNotBlank(value)) {
for (String valueTemp : value.split(",")) {
if (!treeNameList.contains(valueTemp)) {
errorMsg += key + "中的" + valueTemp + "与数据字典不匹配";
countError++;
}
}
}
String valueId = "";
//文字转ID
if (StringUtils.isNotBlank(value)) {
for (String valueTemp : value.split(",")) {
if (treeNameList.contains(valueTemp)) {
List<SysCategory> collect = categoryList.stream().filter(e -> e.getName().equals(valueTemp)).collect(Collectors.toList());
valueId += collect.get(0).getId();
}
}
value = valueId;
}
} else if (FieldTypeEnum.TEXT_STRING.getValue().equals(fieldShowType)) {
//输入框
//判断是否必填
if (IsMustEnum.YES.getValue().equals(mustInput) && StringUtils.isBlank(value)) {
errorMsg += key + "为必填项,不能为空";
countError++;
}
//判断长度
if (StringUtils.isNotBlank(value) && value.length() > Long.parseLong(dbLength)) {
errorMsg += key + "不能超过" + dbLength + "个字符";
countError++;
}
} else if (FieldTypeEnum.TEXT_NUMBER.getValue().equals(fieldShowType)) {
//正则判断仅能为 负号(-),小数点(.)和数字
} else if (FieldTypeEnum.PULL_SINGLE.getValue().equals(fieldShowType)) {
//下拉单选或下拉多选
//判断是否必填
if (IsMustEnum.YES.getValue().equals(mustInput) && StringUtils.isBlank(value)) {
errorMsg += key + "为必填项,不能为空";
countError++;
}
//判断是否是单选
if (StringUtils.isNotBlank(value) && value.contains(",")) {
errorMsg += key + "为单选项";
countError++;
}
//判断数据是否匹配
if (StringUtils.isNotBlank(value)) {
if (!itemNameList.contains(value)) {
errorMsg += key + "中的" + value + "与数据字典不匹配";
countError++;
}
}
//文字转数据字典编码
if (itemNameList.contains(value)) {
String finalValue = value;
List<SysDictItem> collect = dictItemList.stream().filter(e -> e.getItemText().equals(finalValue)).collect(Collectors.toList());
value = collect.get(0).getItemValue();
}
} else if (FieldTypeEnum.PULL_MORE.getValue().equals(fieldShowType)) {
//判断是否必填
if (IsMustEnum.YES.getValue().equals(mustInput) && StringUtils.isBlank(value)) {
errorMsg += key + "为必填项,不能为空";
countError++;
}
//下拉多选
for (String valueTemp : value.split(",")) {
if (!itemNameList.contains(valueTemp)) {
errorMsg += key + "中的" + valueTemp + "与数据字典不匹配";
countError++;
}
}
String valueId = "";
//文字转数据字典id
if (StringUtils.isNotBlank(value)) {
for (String valueTemp : value.split(",")) {
if (itemNameList.contains(valueTemp)) {
List<SysDictItem> collect = dictItemList.stream().filter(e -> e.getItemText().equals(valueTemp)).collect(Collectors.toList());
valueId += collect.get(0).getItemValue();
}
}
value = valueId;
}
} else if (FieldTypeEnum.DATE_SINGLE.getValue().equals(fieldShowType)) {
//单选日期
//判断是否必填
if (IsMustEnum.YES.getValue().equals(mustInput) && StringUtils.isBlank(value)) {
errorMsg += key + "为必填项,不能为空";
countError++;
}
//a判断是否是单日期选择
if (StringUtils.isNotBlank(value) && value.contains(",")) {
errorMsg += key + "为单日期选项";
countError++;
}
//判断格式是否正确
if (!DateUtils.isValidDate((String) value)) {
errorMsg += key + "格式不正确,正确格式如:yyyy/m/d、yyyy-MM-dd、yyyy年MM月dd日";
countError++;
}
} else if (FieldTypeEnum.DATE_SINGLE.getValue().equals(fieldShowType)) {
//多选日期
//判断是否必填
if (IsMustEnum.YES.getValue().equals(mustInput) && StringUtils.isBlank(value)) {
errorMsg += key + "为必填项,不能为空";
countError++;
}
//判断格式是否正确
if (!DateUtils.isValidDate((String) value)) {
errorMsg += key + "格式不正确,正确格式如:yyyy/m/d、yyyy-MM-dd、yyyy年MM月dd日";
countError++;
}
} else if (FieldTypeEnum.FILE.getValue().equals(fieldShowType)) {
//文件
//判断是否必填
if (IsMustEnum.YES.getValue().equals(mustInput) && StringUtils.isBlank(value)) {
errorMsg += key + "为必填项,不能为空";
countError++;
}
if (StringUtils.isNotBlank(value)) {
for (String fileName : value.split(",")) {
List<File> nowFileList = FileUnZip.readFileByFilename(zipEntryName, fileName);
if (nowFileList.size() == 0) {
errorMsg += key + "压缩包中没有" + fileName + "文件; ";
countError++;
} else {
try {
FileInputStream input = new FileInputStream(nowFileList.get(0));
MultipartFile multipartFile =
new MockMultipartFile(nowFileList.get(0).getName(), nowFileList.get(0).getName(), "text/plain", input);
//文件存入文件表
OSSFile ossFile = iOSSFileService.uploadLocal(multipartFile, "");
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
} else if (FieldTypeEnum.STANDARD.getValue().equals(fieldShowType)) {
//标准选择
//判断是否必填
if (IsMustEnum.YES.getValue().equals(mustInput) && StringUtils.isBlank(value)) {
errorMsg += key + "为必填项,不能为空";
countError++;
}
//判断库中是否存在,如果存在则存id,如果不存在则存输入的值
List<Map<String, Object>> listBySerialNumber = getListBySerialNumber(value);
String serialNumberStr = "";
StringBuilder sb = new StringBuilder();
List<String> list = new ArrayList<>();
for (Map<String, Object> stringObjectMap : listBySerialNumber) {
sb.append((String) stringObjectMap.get("id") + ",");
list.add((String) stringObjectMap.get("serial_number"));
}
for (String s : value.split(",")) {
if (!list.contains(s)) {
sb.append(s);
}
}
if (ObjectUtils.isNotEmpty(sb)) {
String substring = sb.substring(0, sb.length() - 1);
value = substring;
}
}
}
}
if (countError > 0) {
msgList.add(errorMsg);
}
mapResult.put(field, value);
}
}
if (msgList.size() > 0) {
//返回报错信息
String html = "";
for (String s : msgList) {
html += s + "</br>";
}
return html;
} else {
this.addInfo(mapResult);
}
return "数据导入成功";
}
//判断row是否为空 空返回true
public boolean isRowEmpty(Row row) {
if (null == row) {
return true;
}
int firstCellNum = row.getFirstCellNum(); //第一个列位置
int lastCellNum = row.getLastCellNum(); //最后一列位置
int nullCellNum = 0; //空列数量
for (int c = firstCellNum; c < lastCellNum; c++) {
Cell cell = row.getCell(c);
if (null == cell) {
nullCellNum++;
continue;
}
String value = "";
switch (cell.getCellType()) {
case HSSFCell.CELL_TYPE_NUMERIC: // 数字
//如果为时间格式的内容
value = String.valueOf(cell.getNumericCellValue());
break;
case HSSFCell.CELL_TYPE_STRING: // 字符串
value = cell.getStringCellValue();
break;
case HSSFCell.CELL_TYPE_BOOLEAN: // Boolean
value = cell.getBooleanCellValue() + "";
break;
case HSSFCell.CELL_TYPE_FORMULA: // 公式
value = cell.getCellFormula() + "";
break;
default:
break;
}
if (org.apache.commons.lang.StringUtils.isEmpty(value)) {
nullCellNum++;
}
}
//所有列都为空
if (nullCellNum == (lastCellNum - firstCellNum)) {
return true;
}
return false;
}
public List<Map<String, Object>> getListBySerialNumber(String serialNumbers) {
return bussDocumentLibraryEOMapper.getListBySerialNumber(serialNumbers);
}
} }
@@ -152,4 +152,21 @@ public class FileUnZip {
} }
} }
public static List<File> readImpExcelFile(String path) {
File file = new File(path);
List<File> resultlist = new ArrayList<>();
if (file.isDirectory()) {
File[] files = file.listFiles();
for (File fi : files) {
// 对文件进行过滤,只读取Excel文件
String name = fi.getName();
// "导入模板".equals(fi.getName()) || "导入模板.xlsx".equals(fi.getName())
if (name != null &&( fi.getName().contains(".xls") || fi.getName().contains(".xlsx") )){
resultlist.add(fi);
}
}
}
return resultlist;
}
} }