优化批量编辑

This commit is contained in:
2024-08-16 11:19:09 +08:00
parent ac7f7859d1
commit fa12c182bd
2 changed files with 91 additions and 45 deletions

View File

@@ -20,4 +20,8 @@ public class BatchEditDto {
}

View File

@@ -3,6 +3,7 @@ package cn.lihongjie.coal.empSalary.service;
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.mapper.CommonMapper;
import cn.lihongjie.coal.base.service.BaseService;
import cn.lihongjie.coal.common.ExcelUtils;
import cn.lihongjie.coal.common.MapUtils;
@@ -63,6 +64,7 @@ import java.util.stream.Collectors;
@Slf4j
@Transactional
public class EmpSalaryService extends BaseService<EmpSalaryEntity, EmpSalaryRepository> {
@Autowired private CommonMapper commonMapper;
@Autowired private EmpSalaryRepository repository;
@Autowired private EmpSalaryMapper mapper;
@@ -92,6 +94,31 @@ public class EmpSalaryService extends BaseService<EmpSalaryEntity, EmpSalaryRepo
return page.map(this.mapper::toDto);
}
public void batchEdit(String organizationId, List<Map<String, Object>> list) {
List<String> ids = list.stream().map(x -> x.get("id") + "").toList();
Map<String, Map<String, Object>> map =
list.stream().collect(Collectors.toMap(e -> e.get("id") + "", e -> e));
List<EmpSalaryEntity> all = this.findAllByIds(ids);
Script scriptObj = empSalaryItemService.newScriptInstance(organizationId);
for (EmpSalaryEntity salary : all) {
Map<String, Object> ctx = buildRecalculateCtx(salary);
MapUtils.merge(map.get(salary.getId()), ctx, EmpSalaryEntity.ITEM_KEYS);
scriptObj.setBinding(new Binding(Map.of("salary", ctx)));
scriptObj.run();
ReflectUtils.updateFromMap(salary, ctx, EmpSalaryEntity.ITEM_KEYS, conversionService);
}
this.saveAll(all);
}
public void batchEdit(BatchEditDto dto) {
EmpSalaryBatchEntity batch = batchService.get(dto.getBatchId());
@@ -109,25 +136,17 @@ public class EmpSalaryService extends BaseService<EmpSalaryEntity, EmpSalaryRepo
return;
}
List<EmpSalaryEntity> all = this.findAllByIds(dto.getSalaryIds());
HashMap<String, Object> src = new HashMap<>(dto.getSalaryItems());
Script scriptObj = empSalaryItemService.newScriptInstance(batch.getOrganizationId());
for (EmpSalaryEntity salary : all) {
Map<String, Object> ctx = buildRecalculateCtx(salary);
MapUtils.merge(src, ctx, EmpSalaryEntity.ITEM_KEYS);
scriptObj.setBinding(new Binding(Map.of("salary", ctx)));
scriptObj.run();
ReflectUtils.updateFromMap(salary, ctx, EmpSalaryEntity.ITEM_KEYS, conversionService);
}
this.saveAll(all);
batchEdit(
batch.getOrganizationId(),
dto.getSalaryIds().stream()
.map(
x -> {
HashMap<String, Object> map = new HashMap<>();
map.put("id", x);
map.putAll(dto.getSalaryItems());
return map;
})
.collect(Collectors.toList()));
}
public void archive(IdRequest dto) {
@@ -802,40 +821,47 @@ select id from tmp1 where rk = 1
assertBatchEditable(batch);
RLock lock = redissonClient.getLock("batchModify." + batch.getId());
boolean tryLock = lock.tryLock();
if (!tryLock) {
batchModifing(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));
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 =
List<Map<String, Object>> batchEditDtos =
ExcelUtils.readFirstSheetToObjectList(
download,
() -> {
BatchEditDto dto = new BatchEditDto();
dto.setBatchId(request.getBatchId());
dto.setSalaryItems(new HashMap<>());
return dto;
return new HashMap<String, Object>();
},
new ExcelUtils.CellValueToObjectSetter<BatchEditDto>() {
new ExcelUtils.CellValueToObjectSetter<Map<String, Object>>() {
@Override
public void set(String header, BatchEditDto object, Object value) {
public void set(
String header, Map<String, Object> object, Object value) {
if (header.equals("身份证号") || header.equals("身份证")) {
object.setSalaryIds(List.of(value + ""));
object.put("idCard", value);
} else if (StringUtils.equalsAny("姓名", "部门", "岗位")) {
// 忽略这些字段
} else if (itemMap.containsKey(header)) {
object.getSalaryItems()
.put(
itemMap.get(header).getCode(),
conversionService.convert(
value, BigDecimal.class));
object.put(
itemMap.get(header).getCode(),
conversionService.convert(value, BigDecimal.class));
} else {
log.info("未知列: {}", header);
throw new BizException("未知列: {}", header);
@@ -843,23 +869,39 @@ select id from tmp1 where rk = 1
}
});
List<EmpSalaryEntity> resultList = em.createQuery(
"""
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();
""",
EmpSalaryEntity.class)
.setParameter("batchId", request.getBatchId())
.setParameter(
"idCards",
batchEditDtos.stream()
.map(x -> x.get("idCard").toString())
.toList())
.getResultList();
Map<String, EmpSalaryEntity> map = resultList.stream().collect(Collectors.toMap(e -> e.getIdCard(), e -> e));
for (EmpSalaryEntity salary : resultList) {
for (BatchEditDto batchEditDto : batchEditDtos) {
batchEditDto.setSalaryIds(Collections.singletonList(map.get(batchEditDto.getSalaryIds().get(0)).getId()));
batchEdit(batchEditDto);
for (Map<String, Object> dto : batchEditDtos) {
if (StringUtils.equals(
salary.getEmployee().getIdCard(), dto.get("idCard").toString())) {
dto.put("id", salary.getId());
}
}
}
for (Map<String, Object> dto : batchEditDtos) {
if (dto.get("id") == null) {
log.warn("未找到员工 {}", dto.get("idCard"));
}
}
batchEdit(batch.getOrganizationId(), batchEditDtos);
}
}