累计专项附加扣除防抖动

This commit is contained in:
Harryxzy 2023-06-29 13:53:07 +08:00
parent 6a5304a605
commit e3e0bcd66b
1 changed files with 104 additions and 94 deletions

View File

@ -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<TaxAgentPO> 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<AddUpDeduction> updateList = new ArrayList<>();
List<AddUpDeduction> insertList = new ArrayList<>();
List<Long> errorMessages = new ArrayList<>();
List<SalaryAcctEmployeePO> accountedEmployeeData =
getAccountedEmployeeData(DateUtil.format(yearMonth, "yyyy-MM"));
for (TaxAgentPO taxAgent : taxAgents) {
Collection<Long> employeeIds = getTaxAgentService(user)
.listEmployeeIdsInTaxAgent(taxAgent.getId());
List<SpecialAddDeductionPO> employeePOs = getSpecialAddDeductionService(user)
.getSpecialAddDeductionPOByEmployee(null, taxAgent.getId());
//获取上月员工数据用于累加
LocalDateTime lastMonthDateTime = yearMonthTime.minusMonths(1);
Map<Long, List<AddUpDeduction>> 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<TaxAgentPO> 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<AddUpDeduction> updateList = new ArrayList<>();
List<AddUpDeduction> insertList = new ArrayList<>();
List<Long> errorMessages = new ArrayList<>();
List<SalaryAcctEmployeePO> accountedEmployeeData =
getAccountedEmployeeData(DateUtil.format(yearMonth, "yyyy-MM"));
for (TaxAgentPO taxAgent : taxAgents) {
Collection<Long> employeeIds = getTaxAgentService(user)
.listEmployeeIdsInTaxAgent(taxAgent.getId());
List<SpecialAddDeductionPO> employeePOs = getSpecialAddDeductionService(user)
.getSpecialAddDeductionPOByEmployee(null, taxAgent.getId());
//获取当月员工数据用于更新
YearMonth currentMonth = YearMonth.of(yearMonthTime.getYear(), yearMonthTime.getMonth());
Map<Long, List<AddUpDeduction>> 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<Long, List<AddUpDeduction>> 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<Long, List<AddUpDeduction>> 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<AddUpDeduction>) l));
Lists.partition(updateList, 100)
.forEach(l -> getAddUpDeductionMapper().updateDataAndDeclareMonth((List<AddUpDeduction>) 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<AddUpDeduction>) l));
Lists.partition(updateList, 100)
.forEach(l -> getAddUpDeductionMapper().updateDataAndDeclareMonth((List<AddUpDeduction>) l));
if (!errorMessages.isEmpty()) {
String userNames = getSalaryEmployeeService(user)
.getEmployeeByIdsAll(errorMessages)
.stream()
.map(DataCollectionEmployee::getUsername)
.collect(Collectors.joining(","));
return "一键累计完成!员工" + userNames + "在该年月已核算归档,跳过本次累计";
}
return "一键累计完成!";
}
/**