批量编辑接口

This commit is contained in:
2024-08-16 09:44:50 +08:00
parent e54f067b99
commit 75a68894d0
5 changed files with 115 additions and 31 deletions

View File

@@ -15,18 +15,18 @@ import io.vavr.Tuple2;
import lombok.SneakyThrows;
import lombok.experimental.UtilityClass;
import org.apache.commons.collections4.CollectionUtils;
import org.apache.commons.collections4.MapUtils;
import org.apache.commons.lang3.ObjectUtils;
import org.apache.commons.lang3.StringUtils;
import org.apache.commons.lang3.reflect.FieldUtils;
import org.apache.commons.lang3.reflect.MethodUtils;
import org.jetbrains.annotations.NotNull;
import org.springframework.core.convert.ConversionService;
import org.springframework.util.ReflectionUtils;
import java.lang.reflect.Field;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Optional;
import java.util.*;
import java.util.concurrent.ExecutionException;
@UtilityClass
@@ -44,8 +44,7 @@ public class ReflectUtils {
});
private static final Cache<Tuple2<Class, String>, Optional<Field>> getFieldCache =
CacheBuilder.newBuilder().maximumSize(10000).build();
private static final ObjectMapper objectMapper;
static {
@@ -209,17 +208,14 @@ public class ReflectUtils {
return new HashMap<>();
}
return objectMapper.convertValue(entity, new TypeReference<Map<String, Object>>() {});
}
public static <T> T fromMap(Map<String, Object> map, Class<T> cls) {
if (map == null){
if (map == null) {
return null;
}
return objectMapper.convertValue(map, cls);
}
@@ -227,4 +223,29 @@ public class ReflectUtils {
fieldCache.invalidateAll();
getFieldCache.invalidateAll();
}
public static void updateFromMap(
Object target,
Map<String, Object> map,
Set<String> keys,
ConversionService conversionService) {
if (target == null || MapUtils.isEmpty(map) || CollectionUtils.isEmpty(keys)) {
return;
}
keys.forEach(
x -> {
Object val = map.get(x);
if (val != null) {
Optional<Field> field = getField(target.getClass(), x);
if (field.isPresent()) {
Field f = field.get();
Object converted = conversionService.convert(val, f.getType());
writeField(target, x, converted);
}
}
});
}
}

View File

@@ -4,10 +4,7 @@ import cn.lihongjie.coal.annotation.OrgScope;
import cn.lihongjie.coal.annotation.SysLog;
import cn.lihongjie.coal.base.dto.CommonQuery;
import cn.lihongjie.coal.base.dto.IdRequest;
import cn.lihongjie.coal.empSalary.dto.CreateEmpSalaryDto;
import cn.lihongjie.coal.empSalary.dto.EmpSalaryDto;
import cn.lihongjie.coal.empSalary.dto.InitSalaryDto;
import cn.lihongjie.coal.empSalary.dto.UpdateEmpSalaryDto;
import cn.lihongjie.coal.empSalary.dto.*;
import cn.lihongjie.coal.empSalary.service.EmpSalaryService;
import lombok.extern.slf4j.Slf4j;
@@ -77,9 +74,10 @@ public class EmpSalaryController {
@PostMapping("/initSalary")
public Object initSalary(@RequestBody InitSalaryDto request) {
this.service.initSalary(request);
this.service.initSalary(request);
return true;
}
@PostMapping("/recalculate")
public Object recalculate(@RequestBody IdRequest request) {
this.service.recalculate(request);
@@ -91,6 +89,12 @@ public class EmpSalaryController {
return this.service.recalculatePreview(request);
}
@PostMapping("/batchEdit")
public Object batchEdit(@RequestBody BatchEditDto request) {
this.service.batchEdit(request);
return true;
}
@PostMapping("/archive")
public Object archive(@RequestBody IdRequest request) {
this.service.archive(request);

View File

@@ -0,0 +1,21 @@
package cn.lihongjie.coal.empSalary.dto;
import lombok.Data;
import java.math.BigDecimal;
import java.util.*;
@Data
public class BatchEditDto {
private String batchId;
private List<String> salaryIds;
private Map<String, BigDecimal> salaryItems = new HashMap<>();
}

View File

@@ -315,7 +315,7 @@ public class EmpSalaryEntity extends OrgCommonEntity {
this.batchYearMonth = salaryEntity.batchYearMonth;
}
private void updateAttendance(EmpSalaryEntity salaryEntity) {
public void updateAttendance(EmpSalaryEntity salaryEntity) {
this.fullAttendance = salaryEntity.fullAttendance;
this.fullWork = salaryEntity.fullWork;
this.shouldAttendanceDays = salaryEntity.shouldAttendanceDays;
@@ -336,7 +336,7 @@ public class EmpSalaryEntity extends OrgCommonEntity {
this.absenteeismTimes = salaryEntity.absenteeismTimes;
}
private void updateEmpInfo(EmpSalaryEntity salaryEntity) {
public void updateEmpInfo(EmpSalaryEntity salaryEntity) {
this.empName = salaryEntity.empName;
this.empCode = salaryEntity.empCode;
@@ -385,7 +385,7 @@ public class EmpSalaryEntity extends OrgCommonEntity {
this.birthday = salaryEntity.birthday;
}
private void updateItems(EmpSalaryEntity salaryEntity) {
public void updateItems(EmpSalaryEntity salaryEntity) {
this.item0 = salaryEntity.item0;
this.item1 = salaryEntity.item1;

View File

@@ -96,6 +96,44 @@ public class EmpSalaryService extends BaseService<EmpSalaryEntity, EmpSalaryRepo
return page.map(this.mapper::toDto);
}
public void batchEdit(BatchEditDto dto) {
EmpSalaryBatchEntity batch = batchService.get(dto.getBatchId());
assertBatchEditable(batch);
RLock lock = redissonClient.getLock("batchModify." + batch.getId());
boolean tryLock = lock.tryLock();
if (!tryLock) {
batchModifing(batch);
}
if (org.apache.commons.collections4.MapUtils.isEmpty(dto.getSalaryItems())) {
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);
}
public void archive(IdRequest dto) {
this.repository.archive(dto);
}
@@ -257,20 +295,22 @@ public class EmpSalaryService extends BaseService<EmpSalaryEntity, EmpSalaryRepo
List<EmpSalaryEntity> salaries =
em.createQuery(
"""
select s from EmpSalaryEntity s
select s from EmpSalaryEntity s
left join fetch s.employee e
left join fetch e.department
left join fetch e.jobPost
left join fetch s.batch
left join fetch s.empMonthAttendance
left join fetch s.batch
left join fetch s.empMonthAttendance
where s.id in :ids""",
EmpSalaryEntity.class)
.setParameter("ids", ids.getIds())
.getResultList();
salaries.stream().map(x -> x.getBatch().getId()).distinct().forEach(this::assertBatchEditable);
salaries.stream()
.map(x -> x.getBatch().getId())
.distinct()
.forEach(this::assertBatchEditable);
recalculate(salaries, true);
}
@@ -329,17 +369,17 @@ public class EmpSalaryService extends BaseService<EmpSalaryEntity, EmpSalaryRepo
checkBatchStatus(batch);
stopWatch.stop();
stopWatch.start("parseScript");
Script scriptObj = empSalaryItemService.newScriptInstance(batch.getOrganizationId());
stopWatch.stop();
var dtoByIds = salaries.stream().map(x -> x.getEmployee()).map(employeeMapper::toCalculateDto).toList();
var dtoByIds =
salaries.stream()
.map(x -> x.getEmployee())
.map(employeeMapper::toCalculateDto)
.toList();
Map<String, EmployeeCalculateDto> dtoMap =
dtoByIds.stream().collect(Collectors.toMap(e -> e.getId(), e -> e));
@@ -698,9 +738,7 @@ public class EmpSalaryService extends BaseService<EmpSalaryEntity, EmpSalaryRepo
private void checkBatchStatus(EmpSalaryBatchEntity batch) {
assertBatchEditable(batch);
}
@Autowired EmpSalaryMapper empSalaryMapper;