From 75a68894d0ae260880f754c4131223b17d8f337b Mon Sep 17 00:00:00 2001 From: lihongjie0209 Date: Fri, 16 Aug 2024 09:44:50 +0800 Subject: [PATCH] =?UTF-8?q?=E6=89=B9=E9=87=8F=E7=BC=96=E8=BE=91=E6=8E=A5?= =?UTF-8?q?=E5=8F=A3?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../lihongjie/coal/common/ReflectUtils.java | 41 +++++++++--- .../controller/EmpSalaryController.java | 14 ++-- .../coal/empSalary/dto/BatchEditDto.java | 21 ++++++ .../empSalary/entity/EmpSalaryEntity.java | 6 +- .../empSalary/service/EmpSalaryService.java | 64 +++++++++++++++---- 5 files changed, 115 insertions(+), 31 deletions(-) create mode 100644 src/main/java/cn/lihongjie/coal/empSalary/dto/BatchEditDto.java diff --git a/src/main/java/cn/lihongjie/coal/common/ReflectUtils.java b/src/main/java/cn/lihongjie/coal/common/ReflectUtils.java index 07f7687c..52f43539 100644 --- a/src/main/java/cn/lihongjie/coal/common/ReflectUtils.java +++ b/src/main/java/cn/lihongjie/coal/common/ReflectUtils.java @@ -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, Optional> 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>() {}); } - public static T fromMap(Map map, Class 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 map, + Set 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 = getField(target.getClass(), x); + + if (field.isPresent()) { + Field f = field.get(); + Object converted = conversionService.convert(val, f.getType()); + writeField(target, x, converted); + } + } + }); + } } diff --git a/src/main/java/cn/lihongjie/coal/empSalary/controller/EmpSalaryController.java b/src/main/java/cn/lihongjie/coal/empSalary/controller/EmpSalaryController.java index effe3162..113d0e4a 100644 --- a/src/main/java/cn/lihongjie/coal/empSalary/controller/EmpSalaryController.java +++ b/src/main/java/cn/lihongjie/coal/empSalary/controller/EmpSalaryController.java @@ -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); diff --git a/src/main/java/cn/lihongjie/coal/empSalary/dto/BatchEditDto.java b/src/main/java/cn/lihongjie/coal/empSalary/dto/BatchEditDto.java new file mode 100644 index 00000000..92631066 --- /dev/null +++ b/src/main/java/cn/lihongjie/coal/empSalary/dto/BatchEditDto.java @@ -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 salaryIds; + + + private Map salaryItems = new HashMap<>(); + + + + +} diff --git a/src/main/java/cn/lihongjie/coal/empSalary/entity/EmpSalaryEntity.java b/src/main/java/cn/lihongjie/coal/empSalary/entity/EmpSalaryEntity.java index f50f8813..555f0781 100644 --- a/src/main/java/cn/lihongjie/coal/empSalary/entity/EmpSalaryEntity.java +++ b/src/main/java/cn/lihongjie/coal/empSalary/entity/EmpSalaryEntity.java @@ -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; diff --git a/src/main/java/cn/lihongjie/coal/empSalary/service/EmpSalaryService.java b/src/main/java/cn/lihongjie/coal/empSalary/service/EmpSalaryService.java index d942546c..5570f2fa 100644 --- a/src/main/java/cn/lihongjie/coal/empSalary/service/EmpSalaryService.java +++ b/src/main/java/cn/lihongjie/coal/empSalary/service/EmpSalaryService.java @@ -96,6 +96,44 @@ public class EmpSalaryService extends BaseService all = this.findAllByIds(dto.getSalaryIds()); + + HashMap src = new HashMap<>(dto.getSalaryItems()); + + Script scriptObj = empSalaryItemService.newScriptInstance(batch.getOrganizationId()); + + for (EmpSalaryEntity salary : all) { + + Map 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 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 x.getEmployee()).map(employeeMapper::toCalculateDto).toList(); - + var dtoByIds = + salaries.stream() + .map(x -> x.getEmployee()) + .map(employeeMapper::toCalculateDto) + .toList(); Map dtoMap = dtoByIds.stream().collect(Collectors.toMap(e -> e.getId(), e -> e)); @@ -698,9 +738,7 @@ public class EmpSalaryService extends BaseService