mirror of
https://codeup.aliyun.com/64f7d6b8ce01efaafef1e678/coal/coal.git
synced 2026-01-25 07:46:40 +08:00
通过excel批量导入进行编辑
This commit is contained in:
@@ -95,6 +95,13 @@ public class EmpSalaryController {
|
||||
return true;
|
||||
}
|
||||
|
||||
|
||||
@PostMapping("/batchEditExcel")
|
||||
public Object batchEditExcel(@RequestBody BatchEditDto request) {
|
||||
this.service.batchEditExcel(request);
|
||||
return true;
|
||||
}
|
||||
|
||||
@PostMapping("/archive")
|
||||
public Object archive(@RequestBody IdRequest request) {
|
||||
this.service.archive(request);
|
||||
|
||||
@@ -10,6 +10,8 @@ public class BatchEditDto {
|
||||
|
||||
private String batchId;
|
||||
|
||||
private String fileId;
|
||||
|
||||
private List<String> salaryIds;
|
||||
|
||||
|
||||
|
||||
@@ -4,6 +4,7 @@ import cn.lihongjie.coal.base.dto.CommonQuery;
|
||||
import cn.lihongjie.coal.base.dto.IdRequest;
|
||||
import cn.lihongjie.coal.base.entity.OrgCommonEntity;
|
||||
import cn.lihongjie.coal.base.service.BaseService;
|
||||
import cn.lihongjie.coal.common.ExcelUtils;
|
||||
import cn.lihongjie.coal.common.GroovyScriptUtils;
|
||||
import cn.lihongjie.coal.common.MapUtils;
|
||||
import cn.lihongjie.coal.common.ReflectUtils;
|
||||
@@ -26,6 +27,7 @@ import cn.lihongjie.coal.employee.entity.EmployeeEntity;
|
||||
import cn.lihongjie.coal.employee.mapper.EmployeeMapper;
|
||||
import cn.lihongjie.coal.employee.service.EmployeeService;
|
||||
import cn.lihongjie.coal.exception.BizException;
|
||||
import cn.lihongjie.coal.file.service.FileService;
|
||||
|
||||
import groovy.lang.Binding;
|
||||
import groovy.lang.GroovyClassLoader;
|
||||
@@ -36,6 +38,7 @@ import io.vavr.control.Try;
|
||||
import jakarta.persistence.EntityManager;
|
||||
import jakarta.persistence.PersistenceContext;
|
||||
|
||||
import lombok.Cleanup;
|
||||
import lombok.SneakyThrows;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
|
||||
@@ -56,11 +59,9 @@ import org.springframework.stereotype.Service;
|
||||
import org.springframework.transaction.annotation.Transactional;
|
||||
import org.springframework.util.StopWatch;
|
||||
|
||||
import java.io.InputStream;
|
||||
import java.math.BigDecimal;
|
||||
import java.util.ArrayList;
|
||||
import java.util.HashMap;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.*;
|
||||
import java.util.stream.Collectors;
|
||||
|
||||
@Service
|
||||
@@ -863,4 +864,73 @@ select id from tmp1 where rk = 1
|
||||
|
||||
log.info("删除工资数据 {} 条", cnt);
|
||||
}
|
||||
|
||||
@Autowired FileService fileService;
|
||||
|
||||
public void batchEditExcel(BatchEditDto request) {
|
||||
|
||||
var batch = this.batchService.get(request.getBatchId());
|
||||
|
||||
assertBatchEditable(batch);
|
||||
|
||||
List<EmpSalaryItemEntity> items =
|
||||
this.empSalaryItemService.getItems(batch.getOrganizationId());
|
||||
|
||||
Map<String, EmpSalaryItemEntity> itemMap =
|
||||
items.stream().filter(x -> StringUtils.equalsAny(x.getInputType(), "1")).collect(Collectors.toMap(e -> e.getName(), e -> e));
|
||||
|
||||
@Cleanup InputStream download = fileService.download(request.getFileId());
|
||||
|
||||
List<BatchEditDto> batchEditDtos =
|
||||
ExcelUtils.readFirstSheetToObjectList(
|
||||
download,
|
||||
() -> {
|
||||
BatchEditDto dto = new BatchEditDto();
|
||||
dto.setBatchId(request.getBatchId());
|
||||
dto.setSalaryItems(new HashMap<>());
|
||||
return dto;
|
||||
},
|
||||
new ExcelUtils.CellValueToObjectSetter<BatchEditDto>() {
|
||||
@Override
|
||||
public void set(String header, BatchEditDto object, Object value) {
|
||||
|
||||
if (header.equals("身份证号") || header.equals("身份证")) {
|
||||
object.setSalaryIds(List.of(value + ""));
|
||||
|
||||
} else if (StringUtils.equalsAny("姓名", "部门", "岗位")) {
|
||||
|
||||
// 忽略这些字段
|
||||
|
||||
} else if (itemMap.containsKey(header)) {
|
||||
object.getSalaryItems()
|
||||
.put(
|
||||
itemMap.get(header).getCode(),
|
||||
conversionService.convert(
|
||||
value, BigDecimal.class));
|
||||
} else {
|
||||
log.info("未知列: {}", header);
|
||||
throw new BizException("未知列: {}", header);
|
||||
}
|
||||
}
|
||||
});
|
||||
|
||||
List<EmpSalaryEntity> resultList = em.createQuery(
|
||||
"""
|
||||
select s from EmpSalaryEntity s where s.batch.id =:batchId and s.employee.idCard in :idCards
|
||||
""", EmpSalaryEntity.class)
|
||||
.setParameter("batchId", request.getBatchId())
|
||||
.setParameter(
|
||||
"idCards",
|
||||
batchEditDtos.stream().flatMap(x -> x.getSalaryIds().stream()).toList())
|
||||
.getResultList();
|
||||
|
||||
Map<String, EmpSalaryEntity> map = resultList.stream().collect(Collectors.toMap(e -> e.getIdCard(), e -> e));
|
||||
|
||||
for (BatchEditDto batchEditDto : batchEditDtos) {
|
||||
batchEditDto.setSalaryIds(Collections.singletonList(map.get(batchEditDto.getSalaryIds().get(0)).getId()));
|
||||
batchEdit(batchEditDto);
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user