完善excel导入

This commit is contained in:
2024-04-05 13:32:13 +08:00
parent a36a54f4e1
commit 9178825cdb
9 changed files with 96 additions and 31 deletions

View File

@@ -90,6 +90,6 @@ cd /data/backup && sudo -u postgres pg_dumpall | gzip > /data/backup/$(date +"%
``` ```
rabbitmqctl add_user datacollector datacollector rabbitmqctl add_user datacollector datacollector
rabbitmqctl set_permissions --vhost /coal/test datacollector '' 'sysExchange' '' rabbitmqctl set_permissions --vhost /coal/test datacollector '' 'sysExchange' 'pms\.client.*'
``` ```

View File

@@ -23,6 +23,7 @@ import java.net.URLEncoder;
import java.nio.charset.StandardCharsets; import java.nio.charset.StandardCharsets;
import java.time.LocalDate; import java.time.LocalDate;
import java.time.LocalDateTime; import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.*; import java.util.*;
import java.util.concurrent.ConcurrentHashMap; import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.atomic.AtomicReference; import java.util.concurrent.atomic.AtomicReference;
@@ -34,16 +35,24 @@ public class ExcelUtils {
public static Map<String, Map<String, Object>> wbCtxManager = new ConcurrentHashMap<>(); public static Map<String, Map<String, Object>> wbCtxManager = new ConcurrentHashMap<>();
public static String getCode(Object value) { public static String getCode(Object value, Function<String, String> codeGetter) {
if (value == null) { if (value == null) {
return null; return null;
} }
if (value instanceof String) { if (value instanceof String) {
if (((String) value).contains("-")){
return Splitter.on("-") return Splitter.on("-")
.trimResults() .trimResults()
.omitEmptyStrings() .omitEmptyStrings()
.splitToList((String) value) .splitToList((String) value)
.get(0); .get(0);
}else {
return codeGetter.apply((String) value);
}
} }
return value.toString(); return value.toString();
} }
@@ -82,7 +91,9 @@ public class ExcelUtils {
readFirstSheet( readFirstSheet(
inputStream, inputStream,
row -> { row -> {
return objectCreator.get(); T t = objectCreator.get();
ans.add(t);
return t;
}, },
(headerCell, dataCell, object, value) -> { (headerCell, dataCell, object, value) -> {
valueSetter.set(headerCell.getStringCellValue(), (T) object, value); valueSetter.set(headerCell.getStringCellValue(), (T) object, value);
@@ -449,6 +460,30 @@ public class ExcelUtils {
} }
} }
public static String getString(Object value) {
if (value == null) {
return null;
}
if (value instanceof String) {
return (String) value;
}
if (value instanceof LocalDateTime){
return ((LocalDateTime) value).format(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss"));
}
if (value instanceof Double d){
if (d % 1 == 0){
return String.valueOf(d.intValue());
}
}
return value.toString();
}
public interface ValidationInfo { public interface ValidationInfo {
record ListValidationInfo(List<String> list) implements ValidationInfo {} record ListValidationInfo(List<String> list) implements ValidationInfo {}

View File

@@ -83,6 +83,9 @@ public class DataCollectorService
public void delete(IdRequest request) { public void delete(IdRequest request) {
this.repository.deleteAllById(request.getIds()); this.repository.deleteAllById(request.getIds());
} }
public void createQueue(IdRequest request) {
this.repository.deleteAllById(request.getIds());
}
public DataCollectorDto getById(String id) { public DataCollectorDto getById(String id) {
DataCollectorEntity entity = repository.get(id); DataCollectorEntity entity = repository.get(id);

View File

@@ -3,12 +3,21 @@ package cn.lihongjie.coal.dictionary.repository;
import cn.lihongjie.coal.base.dao.BaseRepository; import cn.lihongjie.coal.base.dao.BaseRepository;
import cn.lihongjie.coal.dictionary.entity.DictionaryEntity; import cn.lihongjie.coal.dictionary.entity.DictionaryEntity;
import cn.lihongjie.coal.script.entity.ScriptEntity; import cn.lihongjie.coal.script.entity.ScriptEntity;
import java.util.Optional;
import org.springframework.data.jpa.repository.Query;
import org.springframework.stereotype.Repository; import org.springframework.stereotype.Repository;
import java.util.Optional;
@Repository @Repository
public interface DictionaryRepository extends BaseRepository<DictionaryEntity> { public interface DictionaryRepository extends BaseRepository<DictionaryEntity> {
DictionaryEntity findByCode(String code); DictionaryEntity findByCode(String code);
Optional<DictionaryEntity> findByScript(ScriptEntity script); Optional<DictionaryEntity> findByScript(ScriptEntity script);
@Query(
value =
"select i.code from t_dictionary_item i inner join t_dictionary d on i.dictionary_id = d.id where d.code = :code and i.name = :name",
nativeQuery = true)
String translate(String code, String name);
} }

View File

@@ -20,6 +20,8 @@ import com.fasterxml.jackson.core.type.TypeReference;
import com.fasterxml.jackson.databind.DeserializationFeature; import com.fasterxml.jackson.databind.DeserializationFeature;
import com.fasterxml.jackson.databind.ObjectMapper; import com.fasterxml.jackson.databind.ObjectMapper;
import io.vavr.Function2;
import jakarta.annotation.PostConstruct; import jakarta.annotation.PostConstruct;
import jakarta.persistence.EntityManager; import jakarta.persistence.EntityManager;
import jakarta.persistence.PersistenceContext; import jakarta.persistence.PersistenceContext;
@@ -272,8 +274,20 @@ class DictionaryService extends BaseService<DictionaryEntity, DictionaryReposito
ArrayList<String> list = new ArrayList<>(); ArrayList<String> list = new ArrayList<>();
TreeUtils.dfsList(tree.getTree(), (TreeDto x )->x.getChildren(), object -> list.add(object.getCurrent().getCode() + "-" + object.getCurrent().getName()), (TreeDto x )->x.getId()); TreeUtils.dfsList(tree.getTree(), (TreeDto x )->x.getChildren(), object -> list.add(object.getCurrent().getName()), (TreeDto x )->x.getId());
return list; return list;
} }
public Function2<String, String, String> getTranslator() {
return Function2.of(this::translate).memoized();
}
public String translate(String code, String s) {
return this.repository.translate(code, s);
}
} }

View File

@@ -6,7 +6,7 @@ import cn.lihongjie.coal.employee.entity.EmpCarVO;
import cn.lihongjie.coal.employee.entity.EmpCertVO; import cn.lihongjie.coal.employee.entity.EmpCertVO;
import cn.lihongjie.coal.employee.entity.EmpFamilyMemberVO; import cn.lihongjie.coal.employee.entity.EmpFamilyMemberVO;
import cn.lihongjie.coal.file.entity.FileEntity; import cn.lihongjie.coal.file.entity.FileEntity;
import cn.lihongjie.coal.jobPost.entity.JobPostEntity; import cn.lihongjie.coal.jobPost.dto.JobPostDto;
import jakarta.persistence.ElementCollection; import jakarta.persistence.ElementCollection;
import jakarta.persistence.ManyToOne; import jakarta.persistence.ManyToOne;
@@ -81,7 +81,7 @@ public class EmployeeDto extends OrgCommonDto {
@Comment("岗位") @Comment("岗位")
@ManyToOne @ManyToOne
private JobPostEntity jobPost; private JobPostDto jobPost;
@Comment("银行编码") @Comment("银行编码")
private String bank; private String bank;

View File

@@ -5,10 +5,7 @@ import cn.lihongjie.coal.department.entity.DepartmentEntity;
import cn.lihongjie.coal.file.entity.FileEntity; import cn.lihongjie.coal.file.entity.FileEntity;
import cn.lihongjie.coal.jobPost.entity.JobPostEntity; import cn.lihongjie.coal.jobPost.entity.JobPostEntity;
import jakarta.persistence.ElementCollection; import jakarta.persistence.*;
import jakarta.persistence.Entity;
import jakarta.persistence.ManyToOne;
import jakarta.persistence.OneToMany;
import lombok.Data; import lombok.Data;
@@ -96,7 +93,7 @@ public class EmployeeEntity extends OrgCommonEntity {
@Comment("手机号") @Comment("手机号")
private String phone; private String phone;
@ManyToOne @ManyToOne(fetch = FetchType.LAZY)
private FileEntity headImage; private FileEntity headImage;
@Comment("身份证照片") @Comment("身份证照片")
@@ -108,11 +105,11 @@ public class EmployeeEntity extends OrgCommonEntity {
private List<FileEntity> bankCardImages; private List<FileEntity> bankCardImages;
@Comment("部门") @Comment("部门")
@ManyToOne @ManyToOne(fetch = FetchType.LAZY)
private DepartmentEntity department; private DepartmentEntity department;
@Comment("岗位") @Comment("岗位")
@ManyToOne @ManyToOne(fetch = FetchType.LAZY)
private JobPostEntity jobPost; private JobPostEntity jobPost;
@Comment("银行编码") @Comment("银行编码")

View File

@@ -23,6 +23,7 @@ import cn.lihongjie.coal.jobPost.entity.JobPostEntity;
import cn.lihongjie.coal.jobPost.service.JobPostService; import cn.lihongjie.coal.jobPost.service.JobPostService;
import io.vavr.Function1; import io.vavr.Function1;
import io.vavr.Function2;
import jakarta.persistence.EntityManager; import jakarta.persistence.EntityManager;
import jakarta.persistence.PersistenceContext; import jakarta.persistence.PersistenceContext;
@@ -187,35 +188,41 @@ public class EmployeeService extends BaseService<EmployeeEntity, EmployeeReposit
}) })
.memoized(); .memoized();
Function2<String, String, String> translator = dictionaryService.getTranslator();
var sexTranslator = translator.apply("sex");
var nationTranslator = translator.apply("nation");
var marriageTranslator = translator.apply("marriage");
var educationTranslator = translator.apply("education");
List<EmployeeEntity> list = List<EmployeeEntity> list =
ExcelUtils.readFirstSheetToObjectList( ExcelUtils.readFirstSheetToObjectList(
download, download,
EmployeeEntity::new, EmployeeEntity::new,
(header, emp, value) -> { (header, emp, value) -> {
switch (header) { switch (header) {
case "姓名" -> emp.setName((String) value); case "姓名" -> emp.setName(ExcelUtils.getString(value));
case "性别" -> emp.setSex(ExcelUtils.getCode(value)); case "性别" -> emp.setSex(ExcelUtils.getCode(value, sexTranslator));
case "民族" -> emp.setNation(ExcelUtils.getCode(value)); case "民族" -> emp.setNation(ExcelUtils.getCode(value, nationTranslator));
case "婚姻状况" -> emp.setMarriage(ExcelUtils.getCode(value)); case "婚姻状况" -> emp.setMarriage(ExcelUtils.getCode(value, marriageTranslator));
case "入职时间" -> emp.setEntryDate(ExcelUtils.getLocalDate(value)); case "入职时间" -> emp.setEntryDate(ExcelUtils.getLocalDate(value));
case "身份证号" -> emp.setIdCard((String) value); case "身份证号" -> emp.setIdCard(ExcelUtils.getString(value));
case "学历" -> emp.setEducation(ExcelUtils.getCode(value)); case "学历" -> emp.setEducation(ExcelUtils.getCode(value, educationTranslator));
case "毕业学校" -> emp.setSchool((String) value); case "毕业学校" -> emp.setSchool(ExcelUtils.getString(value));
case "籍贯" -> emp.setNativePlace((String) value); case "籍贯" -> emp.setNativePlace(ExcelUtils.getString(value));
case "住址" -> emp.setAddress((String) value); case "住址" -> emp.setAddress(ExcelUtils.getString(value));
case "手机号" -> emp.setPhone((String) value); case "手机号" -> emp.setPhone(ExcelUtils.getString(value));
case "部门" -> case "部门" ->
emp.setDepartment( emp.setDepartment(
entityManager.getReference( entityManager.getReference(
DepartmentEntity.class, DepartmentEntity.class,
getDepartmentId.apply((String) value))); getDepartmentId.apply(ExcelUtils.getString(value))));
case "岗位" -> case "岗位" ->
emp.setJobPost( emp.setJobPost(
entityManager.getReference( entityManager.getReference(
JobPostEntity.class, JobPostEntity.class,
getJobPostId.apply((String) value))); getJobPostId.apply(ExcelUtils.getString(value))));
case "银行卡号" -> emp.setBank(ExcelUtils.getCode(value)); case "银行卡号" -> emp.setBank(ExcelUtils.getString(value));
case "收款人姓名" -> emp.setBankName((String) value); case "收款人姓名" -> emp.setBankName(ExcelUtils.getString(value));
} }
}); });

View File

@@ -15,9 +15,9 @@ import cn.lihongjie.coal.spring.config.AliyunProperty;
import cn.lihongjie.coal.spring.config.HwCloudProperty; import cn.lihongjie.coal.spring.config.HwCloudProperty;
import com.aliyun.oss.OSSClient; import com.aliyun.oss.OSSClient;
import com.aliyun.oss.model.OSSObject;
import com.fasterxml.jackson.databind.ObjectMapper; import com.fasterxml.jackson.databind.ObjectMapper;
import com.obs.services.ObsClient; import com.obs.services.ObsClient;
import com.obs.services.model.ObsObject;
import lombok.SneakyThrows; import lombok.SneakyThrows;
import lombok.extern.slf4j.Slf4j; import lombok.extern.slf4j.Slf4j;
@@ -226,8 +226,8 @@ public class FileService extends BaseService<FileEntity, FileRepository> {
FileEntity fileEntity = repository.get(fileId); FileEntity fileEntity = repository.get(fileId);
String objectKey = getObjectKey(fileEntity); String objectKey = getObjectKey(fileEntity);
try { try {
ObsObject object = OSSObject object =
obsClient.getObject(aliyunProperty.getOSS().getBucketName(), objectKey); ossClient.getObject(aliyunProperty.getOSS().getBucketName(), objectKey);
return return
object.getObjectContent(); object.getObjectContent();
} catch (Exception e) { } catch (Exception e) {