From e3e0bcd66bbbb2f9907d0e559f0cb68c0cb4535a Mon Sep 17 00:00:00 2001 From: Harryxzy Date: Thu, 29 Jun 2023 13:53:07 +0800 Subject: [PATCH] =?UTF-8?q?=E7=B4=AF=E8=AE=A1=E4=B8=93=E9=A1=B9=E9=99=84?= =?UTF-8?q?=E5=8A=A0=E6=89=A3=E9=99=A4=E9=98=B2=E6=8A=96=E5=8A=A8?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../impl/AddUpDeductionServiceImpl.java | 198 +++++++++--------- 1 file changed, 104 insertions(+), 94 deletions(-) diff --git a/src/com/engine/salary/service/impl/AddUpDeductionServiceImpl.java b/src/com/engine/salary/service/impl/AddUpDeductionServiceImpl.java index eee2d6930..0f9ce49b4 100644 --- a/src/com/engine/salary/service/impl/AddUpDeductionServiceImpl.java +++ b/src/com/engine/salary/service/impl/AddUpDeductionServiceImpl.java @@ -6,10 +6,10 @@ import com.api.browser.bean.SearchConditionItem; import com.api.browser.util.ConditionFactory; import com.api.browser.util.ConditionType; import com.api.formmode.mybatis.util.SqlProxyHandle; +import com.cloudstore.dev.api.util.Util_DataCache; import com.engine.common.util.ServiceUtil; import com.engine.core.impl.Service; import com.engine.salary.biz.AddUpDeductionBiz; -import com.engine.salary.biz.EmployBiz; import com.engine.salary.common.LocalDateRange; import com.engine.salary.encrypt.EncryptUtil; import com.engine.salary.entity.datacollection.AddUpDeduction; @@ -636,105 +636,115 @@ public class AddUpDeductionServiceImpl extends Service implements AddUpDeduction @Override public String autoAddAll(Date yearMonth, Boolean isAdmin) { - //如果是定时任务直接查询所有,isAdmin传true - boolean isChief = Boolean.TRUE.equals(isAdmin) - || getTaxAgentService(user).isChief((long) user.getUID()); - Collection taxAgents; - if (isChief) { - taxAgents = getTaxAgentService(user).listAll(); - } else { - taxAgents = getTaxAgentService(user).listAllTaxAgentsAsAdmin((long) user.getUID()); - } - LocalDateTime yearMonthTime = DateUtil.toLocalDateTime(yearMonth); - //设置时间到下一年1月1号 - Instant instant = yearMonthTime.plusYears(1L) - .withMonth(1).withDayOfMonth(1) - .atZone(ZoneOffset.systemDefault()).toInstant(); - Date nextYearStart = Date.from(instant); - int countByDeclareAfter = getAddUpDeductionMapper() - .countByDeclareAfter(yearMonth, nextYearStart, - taxAgents.stream().map(TaxAgentPO::getId).collect(Collectors.toList()) - ); - if (countByDeclareAfter > 0) { - throw new SalaryRunTimeException("无法累计,请检查当前累计年度内该月后是否有累计专项附加扣除记录!"); - } - List updateList = new ArrayList<>(); - List insertList = new ArrayList<>(); - List errorMessages = new ArrayList<>(); - List accountedEmployeeData = - getAccountedEmployeeData(DateUtil.format(yearMonth, "yyyy-MM")); - for (TaxAgentPO taxAgent : taxAgents) { - Collection employeeIds = getTaxAgentService(user) - .listEmployeeIdsInTaxAgent(taxAgent.getId()); - List employeePOs = getSpecialAddDeductionService(user) - .getSpecialAddDeductionPOByEmployee(null, taxAgent.getId()); - - - //获取上月员工数据,用于累加 - LocalDateTime lastMonthDateTime = yearMonthTime.minusMonths(1); - Map> lastEmpInfo; - if (lastMonthDateTime.getYear() == yearMonthTime.getYear()) { - YearMonth lastMonth = YearMonth.of(lastMonthDateTime.getYear(), lastMonthDateTime.getMonth()); - lastEmpInfo = getEmpInfoByYearMonth(taxAgent, employeePOs, lastMonth); - } else { - lastEmpInfo = new HashMap<>(0); + String cacheKey = "addUpDeduction_autoAddAll_processing"; + try { + Object objVal = Util_DataCache.getObjVal( cacheKey); + if(objVal != null){ + throw new SalaryRunTimeException(SalaryI18nUtil.getI18nLabel(135788, "一键累计过于频繁,请稍后再试")); } + Util_DataCache.setObjVal(cacheKey,true ); + //如果是定时任务直接查询所有,isAdmin传true + boolean isChief = Boolean.TRUE.equals(isAdmin) + || getTaxAgentService(user).isChief((long) user.getUID()); + Collection taxAgents; + if (isChief) { + taxAgents = getTaxAgentService(user).listAll(); + } else { + taxAgents = getTaxAgentService(user).listAllTaxAgentsAsAdmin((long) user.getUID()); + } + LocalDateTime yearMonthTime = DateUtil.toLocalDateTime(yearMonth); + //设置时间到下一年1月1号 + Instant instant = yearMonthTime.plusYears(1L) + .withMonth(1).withDayOfMonth(1) + .atZone(ZoneOffset.systemDefault()).toInstant(); + Date nextYearStart = Date.from(instant); + int countByDeclareAfter = getAddUpDeductionMapper() + .countByDeclareAfter(yearMonth, nextYearStart, + taxAgents.stream().map(TaxAgentPO::getId).collect(Collectors.toList()) + ); + if (countByDeclareAfter > 0) { + throw new SalaryRunTimeException("无法累计,请检查当前累计年度内该月后是否有累计专项附加扣除记录!"); + } + List updateList = new ArrayList<>(); + List insertList = new ArrayList<>(); + List errorMessages = new ArrayList<>(); + List accountedEmployeeData = + getAccountedEmployeeData(DateUtil.format(yearMonth, "yyyy-MM")); + for (TaxAgentPO taxAgent : taxAgents) { + Collection employeeIds = getTaxAgentService(user) + .listEmployeeIdsInTaxAgent(taxAgent.getId()); + List employeePOs = getSpecialAddDeductionService(user) + .getSpecialAddDeductionPOByEmployee(null, taxAgent.getId()); - //获取当月员工数据,用于更新 - YearMonth currentMonth = YearMonth.of(yearMonthTime.getYear(), yearMonthTime.getMonth()); - Map> currentEmpInfo = getEmpInfoByYearMonth(taxAgent, employeePOs, currentMonth); - employeePOs.forEach(employeePO -> { - Long employeeId = employeePO.getEmployeeId(); - // 如果该员工当前月份已经核算,不做累计 - SalaryAcctEmployeePO anyAccountedEmployee = accountedEmployeeData.stream() - .filter(e -> e.getEmployeeId().equals(employeeId)) - .filter(e -> e.getTaxAgentId().equals(taxAgent.getId())) - .findAny().orElse(null); - if (anyAccountedEmployee != null) { - errorMessages.add(employeeId); - return; - } - AddUpDeduction addUpDeduction = Optional.ofNullable(lastEmpInfo.get(employeeId)) - .flatMap(list -> list.stream().findFirst()) - .orElseGet(AddUpDeduction::new); - this.combine(addUpDeduction, employeePO); - - addUpDeduction.setEmployeeId(employeeId); - addUpDeduction.setTaxAgentId(taxAgent.getId()); - addUpDeduction.setDeclareMonth(DateUtil.beginOfMonth(yearMonth)); - addUpDeduction.setCreator(user == null ? 0 : (long) user.getUID()); - addUpDeduction.setTenantKey(DEFAULT_TENANT_KEY); - - //确认当期是否有已经累计的记录 - AddUpDeduction oldInfo = Optional.ofNullable(currentEmpInfo.get(employeeId)) - .flatMap(c -> c.stream().findFirst()) - .orElse(null); - if (oldInfo == null) { - addUpDeduction.setCreateTime(yearMonth); - addUpDeduction.setUpdateTime(yearMonth); - insertList.add(encryptUtil.encrypt(addUpDeduction, AddUpDeduction.class)); + //获取上月员工数据,用于累加 + LocalDateTime lastMonthDateTime = yearMonthTime.minusMonths(1); + Map> lastEmpInfo; + if (lastMonthDateTime.getYear() == yearMonthTime.getYear()) { + YearMonth lastMonth = YearMonth.of(lastMonthDateTime.getYear(), lastMonthDateTime.getMonth()); + lastEmpInfo = getEmpInfoByYearMonth(taxAgent, employeePOs, lastMonth); } else { - addUpDeduction.setId(oldInfo.getId()); - addUpDeduction.setCreateTime(oldInfo.getCreateTime()); - addUpDeduction.setUpdateTime(yearMonth); - updateList.add(encryptUtil.encrypt(addUpDeduction, AddUpDeduction.class)); + lastEmpInfo = new HashMap<>(0); } - }); + + //获取当月员工数据,用于更新 + YearMonth currentMonth = YearMonth.of(yearMonthTime.getYear(), yearMonthTime.getMonth()); + Map> currentEmpInfo = getEmpInfoByYearMonth(taxAgent, employeePOs, currentMonth); + + employeePOs.forEach(employeePO -> { + Long employeeId = employeePO.getEmployeeId(); + // 如果该员工当前月份已经核算,不做累计 + SalaryAcctEmployeePO anyAccountedEmployee = accountedEmployeeData.stream() + .filter(e -> e.getEmployeeId().equals(employeeId)) + .filter(e -> e.getTaxAgentId().equals(taxAgent.getId())) + .findAny().orElse(null); + if (anyAccountedEmployee != null) { + errorMessages.add(employeeId); + return; + } + AddUpDeduction addUpDeduction = Optional.ofNullable(lastEmpInfo.get(employeeId)) + .flatMap(list -> list.stream().findFirst()) + .orElseGet(AddUpDeduction::new); + this.combine(addUpDeduction, employeePO); + + addUpDeduction.setEmployeeId(employeeId); + addUpDeduction.setTaxAgentId(taxAgent.getId()); + addUpDeduction.setDeclareMonth(DateUtil.beginOfMonth(yearMonth)); + addUpDeduction.setCreator(user == null ? 0 : (long) user.getUID()); + addUpDeduction.setTenantKey(DEFAULT_TENANT_KEY); + + //确认当期是否有已经累计的记录 + AddUpDeduction oldInfo = Optional.ofNullable(currentEmpInfo.get(employeeId)) + .flatMap(c -> c.stream().findFirst()) + .orElse(null); + if (oldInfo == null) { + addUpDeduction.setCreateTime(yearMonth); + addUpDeduction.setUpdateTime(yearMonth); + insertList.add(encryptUtil.encrypt(addUpDeduction, AddUpDeduction.class)); + } else { + addUpDeduction.setId(oldInfo.getId()); + addUpDeduction.setCreateTime(oldInfo.getCreateTime()); + addUpDeduction.setUpdateTime(yearMonth); + updateList.add(encryptUtil.encrypt(addUpDeduction, AddUpDeduction.class)); + } + }); + } + Lists.partition(insertList, 100) + .forEach(l -> getAddUpDeductionMapper().insertData((List) l)); + Lists.partition(updateList, 100) + .forEach(l -> getAddUpDeductionMapper().updateDataAndDeclareMonth((List) l)); + if (!errorMessages.isEmpty()) { + String userNames = getSalaryEmployeeService(user) + .getEmployeeByIdsAll(errorMessages) + .stream() + .map(DataCollectionEmployee::getUsername) + .collect(Collectors.joining(",")); + return "一键累计完成!员工" + userNames + "在该年月已核算归档,跳过本次累计"; + } + return "一键累计完成!"; + } finally { + Util_DataCache.clearVal(cacheKey); } - Lists.partition(insertList, 100) - .forEach(l -> getAddUpDeductionMapper().insertData((List) l)); - Lists.partition(updateList, 100) - .forEach(l -> getAddUpDeductionMapper().updateDataAndDeclareMonth((List) l)); - if (!errorMessages.isEmpty()) { - String userNames = getSalaryEmployeeService(user) - .getEmployeeByIdsAll(errorMessages) - .stream() - .map(DataCollectionEmployee::getUsername) - .collect(Collectors.joining(",")); - return "一键累计完成!员工" + userNames + "在该年月已核算归档,跳过本次累计"; - } - return "一键累计完成!"; } /**