From 29e5c27ffb15d6072c37cea9eff14f5fed8b5255 Mon Sep 17 00:00:00 2001 From: sy Date: Wed, 17 Jan 2024 16:07:27 +0800 Subject: [PATCH] =?UTF-8?q?=E8=96=AA=E9=85=AC=E7=B3=BB=E7=BB=9F-=E7=A6=8F?= =?UTF-8?q?=E5=88=A9=E5=8F=B0=E8=B4=A6=EF=BC=8C=E8=87=AA=E5=8A=A8=E6=A0=B8?= =?UTF-8?q?=E7=AE=97=E5=B9=B6=E5=BD=92=E6=A1=A3=E4=BB=BB=E5=8A=A1=E7=B1=BB?= =?UTF-8?q?=E5=BC=80=E5=8F=91?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../entity/siaccount/param/AccountParam.java | 3 + .../salary/service/SIAccountService.java | 7 + .../service/impl/SIAccountServiceImpl.java | 1139 ++++++++++++++++- .../salary/timer/AutoSiAccountAndFileJob.java | 107 ++ 4 files changed, 1223 insertions(+), 33 deletions(-) create mode 100644 src/com/engine/salary/timer/AutoSiAccountAndFileJob.java diff --git a/src/com/engine/salary/entity/siaccount/param/AccountParam.java b/src/com/engine/salary/entity/siaccount/param/AccountParam.java index ceff087e9..34e11c6e5 100644 --- a/src/com/engine/salary/entity/siaccount/param/AccountParam.java +++ b/src/com/engine/salary/entity/siaccount/param/AccountParam.java @@ -42,4 +42,7 @@ public class AccountParam { @DataCheck(require = true,message = "个税扣缴义务人不能为空") private Long paymentOrganization; + // 是否核算后归档 + private boolean fileFlag = false; + } diff --git a/src/com/engine/salary/service/SIAccountService.java b/src/com/engine/salary/service/SIAccountService.java index 99c157855..60d06ed86 100644 --- a/src/com/engine/salary/service/SIAccountService.java +++ b/src/com/engine/salary/service/SIAccountService.java @@ -84,6 +84,13 @@ public interface SIAccountService { */ String save(AccountParam param); + /** + * 新建核算并归档 + * @param param + * @return + */ + String saveAndFile(AccountParam param); + /** * 正常缴纳页核算 * @param saveCommonAccountParam diff --git a/src/com/engine/salary/service/impl/SIAccountServiceImpl.java b/src/com/engine/salary/service/impl/SIAccountServiceImpl.java index cbed96243..6bb0fd1f2 100644 --- a/src/com/engine/salary/service/impl/SIAccountServiceImpl.java +++ b/src/com/engine/salary/service/impl/SIAccountServiceImpl.java @@ -1,6 +1,7 @@ package com.engine.salary.service.impl; import com.alibaba.fastjson.JSON; +import com.alibaba.fastjson.TypeReference; import com.api.browser.bean.SearchConditionGroup; import com.api.browser.bean.SearchConditionItem; import com.api.browser.bean.SearchConditionOption; @@ -10,24 +11,20 @@ import com.cloudstore.eccom.result.WeaResultMsg; import com.engine.common.util.ServiceUtil; import com.engine.core.impl.Service; import com.engine.salary.biz.*; +import com.engine.salary.cache.SalaryCacheKey; import com.engine.salary.component.SalaryWeaTable; import com.engine.salary.constant.SalaryDefaultTenantConstant; import com.engine.salary.encrypt.EncryptUtil; import com.engine.salary.entity.datacollection.DataCollectionEmployee; +import com.engine.salary.entity.progress.ProgressDTO; import com.engine.salary.entity.siaccount.bo.InsuranceAccountBO; import com.engine.salary.entity.siaccount.dto.InsuranceAccountBatchListDTO; import com.engine.salary.entity.siaccount.dto.InsuranceAccountTabDTO; import com.engine.salary.entity.siaccount.dto.InsuranceAccountViewListDTO; import com.engine.salary.entity.siaccount.dto.InsuranceAcctDetailImportFieldDTO; import com.engine.salary.entity.siaccount.param.*; -import com.engine.salary.entity.siaccount.po.ExcelInsuranceDetailPO; -import com.engine.salary.entity.siaccount.po.InsuranceAccountBatchPO; -import com.engine.salary.entity.siaccount.po.InsuranceAccountDetailPO; -import com.engine.salary.entity.siaccount.po.InsuranceAccountInspectPO; -import com.engine.salary.entity.siarchives.po.InsuranceArchivesBaseInfoPO; -import com.engine.salary.entity.siarchives.po.InsuranceArchivesEmployeePO; -import com.engine.salary.entity.siarchives.po.InsuranceArchivesFundSchemePO; -import com.engine.salary.entity.siarchives.po.InsuranceArchivesSocialSchemePO; +import com.engine.salary.entity.siaccount.po.*; +import com.engine.salary.entity.siarchives.po.*; import com.engine.salary.entity.sicategory.po.ICategoryPO; import com.engine.salary.entity.siexport.param.InsuranceExportParam; import com.engine.salary.entity.siexport.po.AccountExportPO; @@ -40,12 +37,10 @@ import com.engine.salary.enums.siaccount.*; import com.engine.salary.enums.sicategory.*; import com.engine.salary.exception.SalaryRunTimeException; import com.engine.salary.mapper.InsuranceExportMapper; -import com.engine.salary.mapper.siaccount.ExcelInsuranceDetailMapper; -import com.engine.salary.mapper.siaccount.InsuranceAccountBatchMapper; -import com.engine.salary.mapper.siaccount.InsuranceAccountDetailMapper; -import com.engine.salary.mapper.siaccount.InsuranceAccountInspectMapper; +import com.engine.salary.mapper.siaccount.*; import com.engine.salary.mapper.siarchives.FundSchemeMapper; import com.engine.salary.mapper.siarchives.InsuranceBaseInfoMapper; +import com.engine.salary.mapper.siarchives.OtherSchemeMapper; import com.engine.salary.mapper.siarchives.SocialSchemeMapper; import com.engine.salary.mapper.sicategory.ICategoryMapper; import com.engine.salary.mapper.sischeme.InsuranceSchemeDetailMapper; @@ -53,14 +48,13 @@ import com.engine.salary.mapper.sischeme.InsuranceSchemeMapper; import com.engine.salary.mapper.sys.SalarySysConfMapper; import com.engine.salary.mapper.taxagent.TaxAgentMapper; import com.engine.salary.service.*; +import com.engine.salary.sys.constant.SalarySysConstant; import com.engine.salary.sys.entity.po.SalarySysConfPO; import com.engine.salary.sys.entity.vo.OrderRuleVO; +import com.engine.salary.sys.enums.SalaryAcctEmployeeRuleEnum; import com.engine.salary.sys.service.SalarySysConfService; import com.engine.salary.sys.service.impl.SalarySysConfServiceImpl; -import com.engine.salary.util.SalaryDateUtil; -import com.engine.salary.util.SalaryEntityUtil; -import com.engine.salary.util.SalaryFormItemUtil; -import com.engine.salary.util.SalaryI18nUtil; +import com.engine.salary.util.*; import com.engine.salary.util.db.MapperProxyFactory; import com.engine.salary.util.excel.ExcelParseHelper; import com.engine.salary.util.excel.ExcelSupport; @@ -73,13 +67,16 @@ import com.engine.salary.util.valid.ValidUtil; import com.engine.salary.wrapper.SalaryFormulaWrapper; import com.google.common.collect.Lists; import com.google.common.collect.Maps; -import com.mzlion.core.utils.BeanUtils; import dm.jdbc.util.IdGenerator; +import lombok.extern.slf4j.Slf4j; import org.apache.commons.collections4.CollectionUtils; +import org.apache.commons.lang.math.NumberUtils; +import org.apache.commons.lang3.ObjectUtils; import org.apache.commons.lang3.StringUtils; import org.apache.poi.ss.usermodel.Sheet; import org.apache.poi.util.IOUtils; import org.apache.poi.xssf.usermodel.XSSFWorkbook; +import org.springframework.beans.BeanUtils; import weaver.file.ImageFileManager; import weaver.general.Util; import weaver.hrm.User; @@ -89,6 +86,7 @@ import java.math.BigDecimal; import java.util.*; import java.util.concurrent.ExecutorService; import java.util.concurrent.Executors; +import java.util.function.Function; import java.util.regex.Matcher; import java.util.regex.Pattern; import java.util.stream.Collectors; @@ -102,6 +100,7 @@ import static com.engine.salary.util.excel.ExcelSupport.EXCEL_TYPE_XLSX; * @Date 2022/4/11 * @Version V1.0 **/ +@Slf4j public class SIAccountServiceImpl extends Service implements SIAccountService { // private SIAccountBiz siAccountBiz = new SIAccountBiz(); @@ -180,6 +179,10 @@ public class SIAccountServiceImpl extends Service implements SIAccountService { return MapperProxyFactory.getProxy(FundSchemeMapper.class); } + private OtherSchemeMapper getOtherSchemeMapper() { + return MapperProxyFactory.getProxy(OtherSchemeMapper.class); + } + private TaxAgentBiz taxAgentBiz = new TaxAgentBiz(); private ICategoryMapper getICategoryMapper() { @@ -210,6 +213,18 @@ public class SIAccountServiceImpl extends Service implements SIAccountService { return (SIBalanceService) ServiceUtil.getService(SIBalanceServiceImpl.class, user); } + private ProgressService getSalaryAcctProgressService(User user) { + return ServiceUtil.getService(ProgressServiceImpl.class, user); + } + + private SIAccountDetailTempMapper getSIAccountDetailTempMapper() { + return MapperProxyFactory.getProxy(SIAccountDetailTempMapper.class); + } + + private InsuranceCompensationMapper getInsuranceCompensationMapper() { + return MapperProxyFactory.getProxy(InsuranceCompensationMapper.class); + } + @Override public Map listPage(InsuranceAccountBatchParam queryParam) { Long employeeId = (long) user.getUID(); @@ -563,20 +578,22 @@ public class SIAccountServiceImpl extends Service implements SIAccountService { @Override public String save(AccountParam param) { - ValidUtil.doValidator(param); - - Long employeeId = (long) user.getUID(); - String lastName = user.getLastname(); - return getSiAccountBiz(user).save(param.isFlag(), param, employeeId, lastName); +// ValidUtil.doValidator(param); +// +// Long employeeId = (long) user.getUID(); +// String lastName = user.getLastname(); +// return getSiAccountBiz(user).save(param.isFlag(), param, employeeId, lastName); + return saveAndFile(param); } @Override public void commonAccount(SaveCommonAccountParam param) { - ValidUtil.doValidator(param); - - Long employeeId = (long) user.getUID(); - String currentUserName = user.getLastname(); - getSiAccountBiz(user).saveCommonAccount(param, employeeId, currentUserName); +// ValidUtil.doValidator(param); +// +// Long employeeId = (long) user.getUID(); +// String currentUserName = user.getLastname(); +// getSiAccountBiz(user).saveCommonAccount(param, employeeId, currentUserName); + siSaveCommonAccount(param); } @Override @@ -587,9 +604,31 @@ public class SIAccountServiceImpl extends Service implements SIAccountService { @Override public void saveCommonAccount(SaveCommonAccountParam accountParam) { - Long employeeId = (long) user.getUID(); - String currentUserName = user.getLastname(); - getSiAccountBiz(user).saveCommonAccount(accountParam, employeeId, currentUserName); +// Long employeeId = (long) user.getUID(); +// String currentUserName = user.getLastname(); +// getSiAccountBiz(user).saveCommonAccount(accountParam, employeeId, currentUserName); + siSaveCommonAccount(accountParam); + } + + /** + * 福利台账正常缴纳页-核算 + */ + public void siSaveCommonAccount(SaveCommonAccountParam param) { + ValidUtil.doValidator(param); + + List collect; + SalaryAssert.notEmpty(param.getIncludes(), SalaryI18nUtil.getI18nLabel(100466, "参数为空")); + SalaryAssert.notNull(param.getBillMonth(), SalaryI18nUtil.getI18nLabel(100467, "账单月为空")); + if (CollectionUtils.isNotEmpty(param.getExcludes())) { + collect = param.getIncludes().stream().filter(item -> !param.getExcludes().contains(item)).collect(Collectors.toList()); + } else { + collect = param.getIncludes(); + } + SalaryAssert.notEmpty(collect, SalaryI18nUtil.getI18nLabel(99920, "无核算人员")); + AccountParam accountParam = new AccountParam(); + accountParam.setBillMonth(param.getBillMonth()); + accountParam.setIds(collect); + siAccounting(accountParam); } @Override @@ -618,9 +657,10 @@ public class SIAccountServiceImpl extends Service implements SIAccountService { @Override public void file(AccountParam accountParam) { - Long employeeId = (long) user.getUID(); - String billMonth = accountParam.getBillMonth(); - getSiAccountBiz(user).file(billMonth, employeeId, accountParam.getPaymentOrganization()); +// Long employeeId = (long) user.getUID(); +// String billMonth = accountParam.getBillMonth(); +// getSiAccountBiz(user).file(billMonth, employeeId, accountParam.getPaymentOrganization()); + siFile(accountParam.getBillMonth(), accountParam.getPaymentOrganization()); } @Override @@ -4279,4 +4319,1037 @@ public class SIAccountServiceImpl extends Service implements SIAccountService { insuranceAccountDetailPO.setOtherComSum(otherComSum.toPlainString()); return insuranceAccountDetailPO; } + + /** + * 新建核算并归档 + * @param param + * @return + */ + @Override + public String saveAndFile(AccountParam param) { + ValidUtil.doValidator(param); + if (param.isFlag()) { + InsuranceAccountBatchPO insuranceAccountBatchPO = getInsuranceAccountBatchMapper().getByBillMonth(param.getBillMonth(), param.getPaymentOrganization()); + insuranceAccountBatchPO = encryptUtil.decrypt(insuranceAccountBatchPO, InsuranceAccountBatchPO.class); + SalaryAssert.isNull(insuranceAccountBatchPO, SalaryI18nUtil.getI18nLabel(0, "所属月份存在核算数据")); + + InsuranceAccountBatchPO build = InsuranceAccountBatchPO.builder() + .paymentOrganization(param.getPaymentOrganization()) + .accountant(user.getLastname()) + .billMonth(param.getBillMonth()) + .billStatus(BillStatusEnum.NOT_ARCHIVED.getValue()) + .remarks(param.getRemarks()) + .creator((long) user.getUID()) + .tenantKey(SalaryDefaultTenantConstant.DEFAULT_TENANT_KEY) + .createTime(new Date()) + .updateTime(new Date()) + .deleteType(DeleteTypeEnum.NOT_DELETED.getValue()) + .paymentOrganization(param.getPaymentOrganization()) + .socialPay("0") + .fundPay("0") + .otherPay("0") + .build(); + encryptUtil.encrypt(build, InsuranceAccountBatchPO.class); + getInsuranceAccountBatchMapper().insert(build); +// LoggerContext insuranceSchemeContext = new LoggerContext(); +// insuranceSchemeContext.setTargetId(String.valueOf(build.getId())); +// insuranceSchemeContext.setTargetName(build.getBillMonth()); +// insuranceSchemeContext.setOperateType(OperateTypeEnum.ADD.getValue()); +// insuranceSchemeContext.setOperateTypeName(SalaryI18nUtil.getI18nLabel(tenantKey, employeeId, 100462, "新增台账")); +// insuranceSchemeContext.setOperatedesc(SalaryI18nUtil.getI18nLabel(tenantKey, employeeId, 100462, "新增台账")); +// insuranceSchemeContext.setNewValues(build); +// siAccountLoggerTemplate.write(insuranceSchemeContext); + } + try { + List employeeIds = getInsuranceAccountDetailMapper().selectEmpByPaymentOrg(param.getPaymentOrganization()); + if (CollectionUtils.isEmpty(employeeIds)) { + List list = Lists.newArrayList(getInsuranceAccountDetailMapper().queryNormalListByBillMonth(param.getBillMonth(), param.getPaymentOrganization())); + encryptUtil.decryptList(list, InsuranceAccountDetailPO.class); + if (CollectionUtils.isNotEmpty(list)) { + list.stream().forEach(f -> { + getInsuranceAccountDetailMapper().deleteById(f.getId()); + }); + } + } + SalaryAssert.notEmpty(employeeIds, SalaryI18nUtil.getI18nLabel(0, "没有需要核算的人员")); +// AccountParam selectParam = new AccountParam(); +// selectParam.setBillMonth(param.getBillMonth()); +// selectParam.setPaymentOrganization(param.getPaymentOrganization()); + ExecutorService taskExecutor = Executors.newCachedThreadPool(); + taskExecutor.execute(() -> { + siAccounting(param); + if (param.isFileFlag()) { + siFile(param.getBillMonth(), param.getPaymentOrganization()); + } + }); + } catch (Exception e) { + // 回滚 + List list = Lists.newArrayList(getInsuranceAccountBatchMapper().getByBillMonth(param.getBillMonth(), param.getPaymentOrganization())); + list = encryptUtil.decryptList(list, InsuranceAccountBatchPO.class); + if (CollectionUtils.isNotEmpty(list)) { + list.stream().forEach(f -> { + getInsuranceAccountBatchMapper().deleteById(f.getId()); + }); + } + throw new SalaryRunTimeException(e.getMessage()); + } + return String.valueOf(user.getUID()); + } + + + public void siAccounting(AccountParam param) { + //福利核算进度 + ProgressDTO salaryAcctProgressDTO = getSalaryAcctProgressService(user).getProgress(SalaryCacheKey.ACCT_PROGRESS + param.getBillMonth()); + if (salaryAcctProgressDTO != null && salaryAcctProgressDTO.isStatus() && salaryAcctProgressDTO.getProgress().compareTo(BigDecimal.ONE) < 0) { + return; + } + + log.info("开始核算,当前操作人为:{}", user.getLastname()); + log.info("核算时间:{}, 核算月份:{}, 个税扣缴义务人:{}, 是否首次核算:{}", new Date(), param.getBillMonth(), param.getPaymentOrganization(), param.isFlag()); + try { + List ids; + List validIds = new ArrayList<>(); + if (CollectionUtils.isEmpty(param.getIds())) { + List empIds = getInsuranceAccountDetailMapper().selectEmpByPaymentOrg(param.getPaymentOrganization()); + // 获取薪资核算人员规则 + SalarySysConfPO salaryAcctEmployeeRule = getSalarySysConfService(user).getOneByCode(SalarySysConstant.SALARY_ACCT_EMPLOYEE_RULE); + List status = new ArrayList<>(); + if(Objects.isNull(salaryAcctEmployeeRule) || StringUtils.equals(salaryAcctEmployeeRule.getConfValue(), SalaryAcctEmployeeRuleEnum.BYPAYENDTIME.getValue()) ){ + // 包含停缴 + status = Arrays.asList(EmployeeStatusEnum.PAYING.getValue(),EmployeeStatusEnum.STAY_DEL.getValue(), + EmployeeStatusEnum.STOP_PAYMENT_FROM_DEL.getValue()); + }else{ + status = Arrays.asList(EmployeeStatusEnum.PAYING.getValue(),EmployeeStatusEnum.STAY_DEL.getValue()); + } + List finalStatus = status; + //过滤出需要核算的人员,即福利档案基础信息表中runStatus为正在缴纳和待减员的人员 + List baseInfoPOList = getInsuranceBaseInfoMapper().listAll(); + List canAccountIds = baseInfoPOList.stream() + .filter(f->f.getPaymentOrganization().equals(param.getPaymentOrganization()) + && (finalStatus.contains(f.getRunStatus()) )) + .map(InsuranceArchivesBaseInfoPO::getEmployeeId) + .collect(Collectors.toList()); + + //20231122逻辑优化,过滤出不在起始缴纳月和最后缴纳月区间的人员 + List empIdsInPayMonthRange = listCanPayEmpIds(param.getPaymentOrganization(), param.getBillMonth()); + empIds = empIds.stream().filter(f->canAccountIds.contains(f) && empIdsInPayMonthRange.contains(f)).collect(Collectors.toList()); + + List socials = siArchivesBiz.getSocialByEmployeeIds(empIds); + //过滤出目标个税扣缴义务人相关信息 + socials = socials.stream().filter(f -> f.getPaymentOrganization().equals(param.getPaymentOrganization())).collect(Collectors.toList()); + List emp1 = socials.stream() + .filter(s -> !(StringUtils.isBlank(s.getSocialEndTime()) && StringUtils.isBlank(s.getSocialStartTime())) && + (StringUtils.isBlank(s.getSocialEndTime()) || (SalaryDateUtil.stringToDate(s.getSocialEndTime() + "-01") != null && !SalaryDateUtil.stringToDate(param.getBillMonth() + "-01").after(SalaryDateUtil.stringToDate(s.getSocialEndTime() + "-01")))) ) + .map(InsuranceArchivesSocialSchemePO::getEmployeeId) + .collect(Collectors.toList()); + + List funds = siArchivesBiz.getFundByEmployeeIds(empIds); + //过滤出目标个税扣缴义务人相关信息 + funds = funds.stream().filter(f -> f.getPaymentOrganization().equals(param.getPaymentOrganization())).collect(Collectors.toList()); + List emp2 = funds.stream() + .filter(s -> !(StringUtils.isBlank(s.getFundStartTime()) && StringUtils.isBlank(s.getFundEndTime())) && + (StringUtils.isBlank(s.getFundEndTime()) || (SalaryDateUtil.stringToDate(s.getFundEndTime() + "-01") != null && !SalaryDateUtil.stringToDate(param.getBillMonth() + "-01").after(SalaryDateUtil.stringToDate(s.getFundEndTime() + "-01"))))) + .map(InsuranceArchivesFundSchemePO::getEmployeeId) + .collect(Collectors.toList()); + + List others = siArchivesBiz.getOtherByEmployeeIds(empIds); + //过滤出目标个税扣缴义务人相关信息 + others = others.stream().filter(f -> f.getPaymentOrganization().equals(param.getPaymentOrganization())).collect(Collectors.toList()); + List emp3 = others.stream() + .filter(s -> !(StringUtils.isBlank(s.getOtherStartTime()) && StringUtils.isBlank(s.getOtherEndTime())) && + (StringUtils.isBlank(s.getOtherEndTime()) || (SalaryDateUtil.stringToDate(s.getOtherEndTime() + "-01") != null && !SalaryDateUtil.stringToDate(param.getBillMonth() + "-01").after(SalaryDateUtil.stringToDate(s.getOtherEndTime() + "-01"))))) + .map(InsuranceArchivesOtherSchemePO::getEmployeeId) + .collect(Collectors.toList()); + validIds.addAll(emp1); + validIds.addAll(emp2); + validIds.addAll(emp3); + + List finalValidIds = validIds.stream().distinct().collect(Collectors.toList()); + ids = empIds.stream().filter(finalValidIds::contains).collect(Collectors.toList()); + } else { + ids = param.getIds(); + } + + // 初始化进度 + ProgressDTO initProgress = new ProgressDTO() + .setTitle(SalaryI18nUtil.getI18nLabel(0, "核算中")) + .setTitleLabelId(97515L) + .setTotalQuantity(ids.size()) + .setCalculatedQuantity(NumberUtils.INTEGER_ZERO) + .setProgress(BigDecimal.ZERO) + .setStatus(true) + .setMessage(StringUtils.EMPTY); + getSalaryAcctProgressService(user).initProgress(SalaryCacheKey.ACCT_PROGRESS + param.getBillMonth(), initProgress); + + if (CollectionUtils.isEmpty(ids)) { + getSalaryAcctProgressService(user).finish(SalaryCacheKey.ACCT_PROGRESS + param.getBillMonth(), true); + return; + } + + log.info("开始生成福利核算数据,待处理人员数量:{}", ids.size()); + siCommonAccount(param.getBillMonth(), ids, param.getPaymentOrganization()); + log.info("福利核算数据生成完毕,开始数据处理"); + + handleData(ids, param); + log.info("福利核算数据处理完毕!"); + + List> partition = Lists.partition((List) ids, 100); + partition.forEach(part -> { + getSIAccountDetailTempMapper().batchDelByEmpIdsAndMonthAndPayOrg(part, param.getBillMonth(), param.getPaymentOrganization()); + }); + log.info("更新福利核算进度······"); + getSalaryAcctProgressService(user).finish(SalaryCacheKey.ACCT_PROGRESS + param.getBillMonth(), true); + log.info("福利核算进度完成!"); + } catch (Exception e) { + log.error("account run fail", e); + getSalaryAcctProgressService(user).fail(SalaryCacheKey.ACCT_PROGRESS + param.getBillMonth(), SalaryI18nUtil.getI18nLabel(0, "福利核算出错") + ": " + e.getMessage()); + + List list = Lists.newArrayList(getInsuranceAccountBatchMapper().getByBillMonth(param.getBillMonth(), param.getPaymentOrganization())); + if (CollectionUtils.isNotEmpty(list)) { + list.stream().forEach(f -> { + getInsuranceAccountBatchMapper().deleteById(f.getId()); + }); + } + + } + + } + + public void siCommonAccount(String billMonth, List ids, Long paymentOrganization) { + SIArchivesBiz siArchivesBiz = new SIArchivesBiz(); + Map insuranceArchivesAccountPOS = siArchivesBiz.buildBatchAccount(ids, paymentOrganization); + List list = new ArrayList<>(); + int count = 0; + for (Map.Entry entry : insuranceArchivesAccountPOS.entrySet()) { + Long k = entry.getKey(); + InsuranceArchivesAccountPO v = entry.getValue(); + InsuranceAccountDetailPO insuranceAccountDetailPO = new InsuranceAccountDetailPO(); + insuranceAccountDetailPO.setBillMonth(billMonth); + insuranceAccountDetailPO.setBillStatus(BillStatusEnum.NOT_ARCHIVED.getValue()); + insuranceAccountDetailPO.setCreator((long) user.getUID()); + insuranceAccountDetailPO.setCreateTime(new Date()); + insuranceAccountDetailPO.setDeleteType(DeleteTypeEnum.NOT_DELETED.getValue()); + insuranceAccountDetailPO.setEmployeeId(k); + insuranceAccountDetailPO.setUpdateTime(new Date()); + insuranceAccountDetailPO.setPaymentStatus(PaymentStatusEnum.COMMON.getValue()); + insuranceAccountDetailPO.setResourceFrom(ResourceFromEnum.SYSTEM.getValue()); + insuranceAccountDetailPO.setTenantKey(SalaryDefaultTenantConstant.DEFAULT_TENANT_KEY); + insuranceAccountDetailPO.setPaymentOrganization(paymentOrganization); + + //核算社保 + accountSocial(insuranceAccountDetailPO, v, billMonth); + //核算公积金 + accountFund(insuranceAccountDetailPO, v, billMonth); + //核算其他福利 + accountOther(insuranceAccountDetailPO, v, billMonth); + //计算合计 + account(insuranceAccountDetailPO); + //临时表PO + InsuranceAccountDetailTempPO insuranceAccountDetailTempPO = new InsuranceAccountDetailTempPO(); + BeanUtils.copyProperties(insuranceAccountDetailPO, insuranceAccountDetailTempPO); + list.add(insuranceAccountDetailTempPO); + count++; + + if (count % 50 == 0 || count >= ids.size()) { + if (count >= ids.size()) { + getSalaryAcctProgressService(user).updateProgress(SalaryCacheKey.ACCT_PROGRESS + billMonth, BigDecimal.valueOf(0.99), false); + log.info("更新福利核算进度,当前进度为:{}", getSalaryAcctProgressService(user).getProgress(SalaryCacheKey.ACCT_PROGRESS + billMonth)); + } else { + getSalaryAcctProgressService(user).getAndAddCalculatedQty(SalaryCacheKey.ACCT_PROGRESS + billMonth, count >= ids.size() ? count % 50 : 50); + log.info("更新福利核算进度,当前进度为:{}", getSalaryAcctProgressService(user).getProgress(SalaryCacheKey.ACCT_PROGRESS + billMonth)); + } + + } + + } + //临时表入库前先对(可能存在的)历史数据进行删除 + List> partition = Lists.partition((List) ids, 100); + partition.forEach(part -> { + getSIAccountDetailTempMapper().batchDelByEmpIdsAndMonthAndPayOrg(part, billMonth, paymentOrganization); + }); + //临时表数据入库 + if (CollectionUtils.isNotEmpty(list)) { + encryptUtil.encryptList(list, InsuranceAccountDetailTempPO.class); + List> lists = splitList(list, 40); + lists.forEach(subList -> { + getSIAccountDetailTempMapper().batchSaveAccountTempDetails(subList); + }); + } + + } + + public InsuranceAccountDetailPO accountOther(InsuranceAccountDetailPO insuranceAccountDetailPO, InsuranceArchivesAccountPO accountPO, String billMonth) { + boolean welBaseDiffSign = siArchivesBiz.isDiffWelBase(); + if (accountPO.getOther() != null) { + InsuranceArchivesOtherSchemePO otherPO = accountPO.getOther(); + insuranceAccountDetailPO.setOtherPayOrg(otherPO.getPaymentOrganization()); + insuranceAccountDetailPO.setOtherSchemeId(otherPO.getOtherSchemeId()); + insuranceAccountDetailPO.setOtherPaymentBaseString(otherPO.getOtherPaymentBaseString()); + insuranceAccountDetailPO.setOtherPaymentComBaseString(otherPO.getOtherPaymentComBaseString()); + //判断是否在起始缴纳月和最后缴纳月之间 + Boolean inDataRange = SalaryDateUtil.monthInRange(insuranceAccountDetailPO.getBillMonth(), otherPO.getOtherStartTime(), otherPO.getOtherEndTime()); + if ((Objects.equals(NonPaymentEnum.YES.getValue(), otherPO.getNonPayment()) || otherPO.getNonPayment() == null) && otherPO.getOtherSchemeId() != null && inDataRange) { + List detailPOS = getInsuranceSchemeDetailMapper().queryListBySchemeId(otherPO.getOtherSchemeId()); + encryptUtil.decryptList(detailPOS, InsuranceSchemeDetailPO.class); + //方案中包含的需要缴纳其他的个人福利 + int monthIndex = Integer.parseInt(billMonth.split("-")[1]) - 1; + Map otherPerson = detailPOS.stream() + .filter(item -> Objects.equals(IsPaymentEnum.YES.getValue(), item.getIsPayment()) && Objects.equals(item.getPaymentScope(), PaymentScopeEnum.SCOPE_PERSON.getValue()) + && (item.getPaymentCycle() == null || item.getPaymentCycle() == 0 || (item.getPaymentCycle() == 1 && String.valueOf(item.getCycleSetting().charAt(monthIndex)).equals("1")))) + .collect( + Collectors.toMap(InsuranceSchemeDetailPO::getInsuranceId, Function.identity())); + //档案中包含的基数信息 + HashMap archivesPerson = JSON.parseObject(otherPO.getOtherPaymentBaseString(), new HashMap().getClass()); + //需要核算其他的福利id 个人 + List needArchivesPerson = new ArrayList<>(); + if (archivesPerson != null) { + archivesPerson.forEach((id, value) -> { + if (otherPerson.containsKey(Long.valueOf(id))) { + needArchivesPerson.add(Long.valueOf(id)); + } + }); + } + //避免福利档案的方案未设置基数,导致核算时遗漏这些福利项的核算,遍历下方案相关福利项,并将遗漏的福利项id添加 + if(otherPerson.size() > 0) { + otherPerson.forEach((id, object) -> { + if (!needArchivesPerson.contains(id)) { + needArchivesPerson.add(id); + } + }); + } + //判断核算周期、核算月、福利起始缴纳月的关系 + checkCycleSettingWithStartMonth(otherPerson, billMonth, otherPO.getOtherStartTime()); + + List otherPer = new ArrayList<>(); + Map otherPerJsonMap = new HashMap<>(); + needArchivesPerson.stream().forEach(e -> { + InsuranceSchemeDetailPO po = otherPerson.get(e); + BigDecimal paymentProportion = new BigDecimal(StringUtils.isBlank(po.getPaymentProportion()) ? "0" : po.getPaymentProportion()).divide(new BigDecimal("100")); + BigDecimal paymentNum = new BigDecimal((ObjectUtils.isEmpty(archivesPerson) || StringUtils.isBlank(archivesPerson.get(String.valueOf(e)))) ? "0" : archivesPerson.get(String.valueOf(e))); + BigDecimal fixedCost = StringUtils.isBlank(po.getFixedCost()) ? new BigDecimal("0") : new BigDecimal(po.getFixedCost()); + Integer newScale = po.getValidNum() == null ? 0 : po.getValidNum(); + BigDecimal result = new BigDecimal("0"); + if (po.getPaymentCycle() != null && po.getPaymentCycle() == 1) { + int monthValue = 1; + for (int i = monthIndex - 1; i >= 0; i--) { + String cycleValue = po.getCycleSetting().charAt(i) + ""; + if (Integer.parseInt(cycleValue) == 1) { + break; + } + monthValue++; + } + if (po.getAccountType() == 1) { + paymentNum = paymentNum.multiply(new BigDecimal(monthValue)); + fixedCost = fixedCost.multiply(new BigDecimal(monthValue)); + result = SalaryEntityUtil.carryRule(newScale, po.getRententionRule(), paymentNum.multiply(paymentProportion).add(fixedCost)); + } else { + for (int i = 0; i < monthValue; i++) { + result = result.add(SalaryEntityUtil.carryRule(newScale, po.getRententionRule(), paymentNum.multiply(paymentProportion).add(fixedCost))); + } + } + } else { + result = SalaryEntityUtil.carryRule(newScale, po.getRententionRule(), paymentNum.multiply(paymentProportion).add(fixedCost)); + } + otherPerJsonMap.put(String.valueOf(e), result.toPlainString()); + otherPer.add(result); + }); + insuranceAccountDetailPO.setOtherPerJson(JSON.toJSONString(otherPerJsonMap)); + BigDecimal otherPerSum = new BigDecimal("0"); + for (BigDecimal bigDecimal : otherPer) { + otherPerSum = otherPerSum.add(bigDecimal); + } + insuranceAccountDetailPO.setOtherPerSum(otherPerSum.toPlainString()); + + //方案中包含的需要缴纳公积金的单位福利 + Map otherCom = detailPOS.stream() + .filter( + item -> Objects.equals(IsPaymentEnum.YES.getValue(), item.getIsPayment()) && Objects.equals(item.getPaymentScope(), PaymentScopeEnum.SCOPE_COMPANY.getValue()) + && (item.getPaymentCycle() == null || item.getPaymentCycle() == 0 || (item.getPaymentCycle() == 1 && String.valueOf(item.getCycleSetting().charAt(monthIndex)).equals("1")))) + .collect( + Collectors.toMap(InsuranceSchemeDetailPO::getInsuranceId, Function.identity())); + //档案中包含的基数信息 + HashMap archivesCom = new HashMap<>(); + if (welBaseDiffSign) { + archivesCom = JSON.parseObject(otherPO.getOtherPaymentComBaseString(), new HashMap().getClass()); + } else { + archivesCom = JSON.parseObject(otherPO.getOtherPaymentBaseString(), new HashMap().getClass()); + } + //需要核算其他的福利id 单位 + List needArchivesCom = new ArrayList<>(); + if (archivesCom != null) { + archivesCom.forEach((id, value) -> { + if (otherCom.containsKey(Long.valueOf(id))) { + needArchivesCom.add(Long.valueOf(id)); + } + }); + } + //避免福利档案的方案未设置基数,导致核算时遗漏这些福利项的核算,遍历下方案相关福利项,并将遗漏的福利项id添加 + if(otherCom.size() > 0) { + otherCom.forEach((id, object) -> { + if (!needArchivesCom.contains(id)) { + needArchivesCom.add(id); + } + }); + } + //判断核算周期、核算月、福利起始缴纳月的关系 + checkCycleSettingWithStartMonth(otherCom, billMonth, otherPO.getOtherStartTime()); + + List otherComList = new ArrayList<>(); + Map otherComJsonMap = new HashMap<>(); + HashMap finalArchivesCom = archivesCom; + needArchivesCom.stream().forEach(e -> { + InsuranceSchemeDetailPO po = otherCom.get(e); + BigDecimal paymentProportion = new BigDecimal(StringUtils.isBlank(po.getPaymentProportion()) ? "0" : po.getPaymentProportion()).divide(new BigDecimal("100")); + BigDecimal paymentNum = new BigDecimal((ObjectUtils.isEmpty(finalArchivesCom) || StringUtils.isBlank(finalArchivesCom.get(String.valueOf(e)))) ? "0" : finalArchivesCom.get(String.valueOf(e))); + BigDecimal fixedCost = StringUtils.isBlank(po.getFixedCost()) ? new BigDecimal("0") : new BigDecimal(po.getFixedCost()); + Integer newScale = po.getValidNum() == null ? 0 : po.getValidNum(); + BigDecimal result = new BigDecimal("0"); + if (Objects.equals(po.getPaymentCycle(), 1)) { + int monthValue = 1; + for (int i = monthIndex - 1; i >= 0; i--) { + String cycleValue = po.getCycleSetting().charAt(i) + ""; + if (Integer.parseInt(cycleValue) == 1) { + break; + } + monthValue++; + } + if (po.getAccountType() == 1) { + paymentNum = paymentNum.multiply(new BigDecimal(monthValue)); + fixedCost = fixedCost.multiply(new BigDecimal(monthValue)); + result = SalaryEntityUtil.carryRule(newScale, po.getRententionRule(), paymentNum.multiply(paymentProportion).add(fixedCost)); + } else { + for (int i = 0; i < monthValue; i++) { + result = result.add(SalaryEntityUtil.carryRule(newScale, po.getRententionRule(), paymentNum.multiply(paymentProportion).add(fixedCost))); + } + } + } else { + result = SalaryEntityUtil.carryRule(newScale, po.getRententionRule(), paymentNum.multiply(paymentProportion).add(fixedCost)); + } + otherComJsonMap.put(String.valueOf(e), result.toPlainString()); + otherComList.add(result); + }); + insuranceAccountDetailPO.setOtherComJson(JSON.toJSONString(otherComJsonMap)); + BigDecimal otherComSum = new BigDecimal("0"); + for (BigDecimal bigDecimal : otherComList) { + otherComSum = otherComSum.add(bigDecimal); + } + insuranceAccountDetailPO.setOtherComSum(otherComSum.toPlainString()); + } + } + return insuranceAccountDetailPO; + } + + + public InsuranceAccountDetailPO accountFund(InsuranceAccountDetailPO insuranceAccountDetailPO, InsuranceArchivesAccountPO accountPO, String billMonth) { + boolean welBaseDiffSign = siArchivesBiz.isDiffWelBase(); + if (accountPO.getFund() != null) { + InsuranceArchivesFundSchemePO fundPO = accountPO.getFund(); + insuranceAccountDetailPO.setFundPayOrg(fundPO.getPaymentOrganization()); + insuranceAccountDetailPO.setFundAccount(fundPO.getFundAccount()); + insuranceAccountDetailPO.setSupplementFundAccount(fundPO.getSupplementFundAccount()); + insuranceAccountDetailPO.setFundSchemeId(fundPO.getFundSchemeId()); + insuranceAccountDetailPO.setFundPaymentBaseString(fundPO.getFundPaymentBaseString()); + insuranceAccountDetailPO.setFundPaymentComBaseString(fundPO.getFundPaymentComBaseString()); + //判断是否在起始缴纳月和最后缴纳月之间 + Boolean inDataRange = SalaryDateUtil.monthInRange(insuranceAccountDetailPO.getBillMonth(), fundPO.getFundStartTime(), fundPO.getFundEndTime()); + if ((NonPaymentEnum.YES.getValue().equals(fundPO.getNonPayment()) || fundPO.getNonPayment() == null) && fundPO.getFundSchemeId() != null && inDataRange) { + List detailPOS = getInsuranceSchemeDetailMapper().queryListBySchemeId(fundPO.getFundSchemeId()); + encryptUtil.decryptList(detailPOS, InsuranceSchemeDetailPO.class); + //方案中包含的需要缴纳社保的个人福利 + int monthIndex = Integer.parseInt(billMonth.split("-")[1]) - 1; + Map fundperson = detailPOS.stream() + .filter(item -> Objects.equals(IsPaymentEnum.YES.getValue(), item.getIsPayment()) && Objects.equals(item.getPaymentScope(), PaymentScopeEnum.SCOPE_PERSON.getValue()) + && (item.getPaymentCycle() == null || item.getPaymentCycle() == 0 || (item.getPaymentCycle() == 1 && String.valueOf(item.getCycleSetting().charAt(monthIndex)).equals("1")))) + .collect( + Collectors.toMap(InsuranceSchemeDetailPO::getInsuranceId, Function.identity())); + //档案中包含的基数信息 + HashMap archivesPerson = JSON.parseObject(fundPO.getFundPaymentBaseString(), new HashMap().getClass()); + //需要核算公积金的福利id 个人 + List needArchivesPerson = new ArrayList<>(); + if (archivesPerson != null) { + archivesPerson.forEach((id, value) -> { + if (fundperson.containsKey(Long.valueOf(id))) { + needArchivesPerson.add(Long.valueOf(id)); + } + }); + } + //避免福利档案的方案未设置基数,导致核算时遗漏这些福利项的核算,遍历下方案相关福利项,并将遗漏的福利项id添加 + if(fundperson.size() > 0) { + fundperson.forEach((id, object) -> { + if (!needArchivesPerson.contains(id)) { + needArchivesPerson.add(id); + } + }); + } + //判断核算周期、核算月、福利起始缴纳月的关系 + checkCycleSettingWithStartMonth(fundperson, billMonth, fundPO.getFundStartTime()); + + List fundPer = new ArrayList<>(); + Map fundPerJsonMap = new HashMap<>(); + needArchivesPerson.stream().forEach(e -> { + InsuranceSchemeDetailPO po = fundperson.get(e); + BigDecimal paymentProportion = new BigDecimal(StringUtils.isBlank(po.getPaymentProportion()) ? "0" : po.getPaymentProportion()).divide(new BigDecimal("100")); + BigDecimal paymentNum = new BigDecimal((ObjectUtils.isEmpty(archivesPerson) || StringUtils.isBlank(archivesPerson.get(String.valueOf(e)))) ? "0" : archivesPerson.get(String.valueOf(e))); + BigDecimal fixedCost = StringUtils.isBlank(po.getFixedCost()) ? new BigDecimal("0") : new BigDecimal(po.getFixedCost()); + Integer newScale = po.getValidNum() == null ? 0 : po.getValidNum(); + BigDecimal result = new BigDecimal("0"); + if (Objects.equals(po.getPaymentCycle(), 1)) { + int monthValue = 1; + for (int i = monthIndex - 1; i >= 0; i--) { + String cycleValue = po.getCycleSetting().charAt(i) + ""; + if (Integer.parseInt(cycleValue) == 1) { + break; + } + monthValue++; + } + if (po.getAccountType() == 1) { + paymentNum = paymentNum.multiply(new BigDecimal(monthValue)); + fixedCost = fixedCost.multiply(new BigDecimal(monthValue)); + result = SalaryEntityUtil.carryRule(newScale, po.getRententionRule(), paymentNum.multiply(paymentProportion).add(fixedCost)); + } else { + for (int i = 0; i < monthValue; i++) { + result = result.add(SalaryEntityUtil.carryRule(newScale, po.getRententionRule(), paymentNum.multiply(paymentProportion).add(fixedCost))); + } + } + } else { + result = SalaryEntityUtil.carryRule(newScale, po.getRententionRule(), paymentNum.multiply(paymentProportion).add(fixedCost)); + } + fundPerJsonMap.put(String.valueOf(e), result.toPlainString()); + fundPer.add(result); + }); + insuranceAccountDetailPO.setFundPerJson(JSON.toJSONString(fundPerJsonMap)); + BigDecimal funPerSum = new BigDecimal("0"); + for (BigDecimal bigDecimal : fundPer) { + funPerSum = funPerSum.add(bigDecimal); + } + insuranceAccountDetailPO.setFundPerSum(funPerSum.toPlainString()); + + //方案中包含的需要缴纳公积金的单位福利 + Map fundCom = detailPOS.stream() + .filter(item -> Objects.equals(IsPaymentEnum.YES.getValue(), item.getIsPayment()) && Objects.equals(item.getPaymentScope(), PaymentScopeEnum.SCOPE_COMPANY.getValue()) + && (item.getPaymentCycle() == null || item.getPaymentCycle() == 0 || (item.getPaymentCycle() == 1 && String.valueOf(item.getCycleSetting().charAt(monthIndex)).equals("1")))) + .collect( + Collectors.toMap(InsuranceSchemeDetailPO::getInsuranceId, Function.identity())); + //档案中包含的基数信息 + HashMap archivesCom = new HashMap<>(); + if (welBaseDiffSign) { + archivesCom = JSON.parseObject(fundPO.getFundPaymentComBaseString(), new HashMap().getClass()); + } else { + archivesCom = JSON.parseObject(fundPO.getFundPaymentBaseString(), new HashMap().getClass()); + } + //需要核算公积金的福利id 单位 + List needArchivesCom = new ArrayList<>(); + if (archivesCom != null) { + archivesCom.forEach((id, value) -> { + if (fundCom.containsKey(Long.valueOf(id))) { + needArchivesCom.add(Long.valueOf(id)); + } + }); + } + //避免福利档案的方案未设置基数,导致核算时遗漏这些福利项的核算,遍历下方案相关福利项,并将遗漏的福利项id添加 + if(fundCom.size() > 0) { + fundCom.forEach((id, object) -> { + if (!needArchivesCom.contains(id)) { + needArchivesCom.add(id); + } + }); + } + //判断核算周期、核算月、福利起始缴纳月的关系 + checkCycleSettingWithStartMonth(fundCom, billMonth, fundPO.getFundStartTime()); + + List fundComList = new ArrayList<>(); + Map fundComJsonMap = new HashMap<>(); + HashMap finalArchivesCom = archivesCom; + needArchivesCom.stream().forEach(e -> { + InsuranceSchemeDetailPO po = fundCom.get(e); + BigDecimal paymentProportion = new BigDecimal(StringUtils.isBlank(po.getPaymentProportion()) ? "0" : po.getPaymentProportion()).divide(new BigDecimal("100")); + BigDecimal paymentNum = new BigDecimal((ObjectUtils.isEmpty(finalArchivesCom) || StringUtils.isBlank(finalArchivesCom.get(String.valueOf(e)))) ? "0" : finalArchivesCom.get(String.valueOf(e))); + BigDecimal fixedCost = StringUtils.isBlank(po.getFixedCost()) ? new BigDecimal("0") : new BigDecimal(po.getFixedCost()); + Integer newScale = po.getValidNum() == null ? 0 : po.getValidNum(); + BigDecimal result = new BigDecimal("0"); + if (Objects.equals(po.getPaymentCycle(), 1)) { + int monthValue = 1; + for (int i = monthIndex - 1; i >= 0; i--) { + String cycleValue = po.getCycleSetting().charAt(i) + ""; + if (Integer.parseInt(cycleValue) == 1) { + break; + } + monthValue++; + } + if (po.getAccountType() == 1) { + paymentNum = paymentNum.multiply(new BigDecimal(monthValue)); + fixedCost = fixedCost.multiply(new BigDecimal(monthValue)); + result = SalaryEntityUtil.carryRule(newScale, po.getRententionRule(), paymentNum.multiply(paymentProportion).add(fixedCost)); + } else { + for (int i = 0; i < monthValue; i++) { + result = result.add(SalaryEntityUtil.carryRule(newScale, po.getRententionRule(), paymentNum.multiply(paymentProportion).add(fixedCost))); + } + } + } else { + result = SalaryEntityUtil.carryRule(newScale, po.getRententionRule(), paymentNum.multiply(paymentProportion).add(fixedCost)); + } + fundComJsonMap.put(String.valueOf(e), result.toPlainString()); + fundComList.add(result); + }); + insuranceAccountDetailPO.setFundComJson(JSON.toJSONString(fundComJsonMap)); + BigDecimal fundComSum = new BigDecimal("0"); + for (BigDecimal bigDecimal : fundComList) { + fundComSum = fundComSum.add(bigDecimal); + } + insuranceAccountDetailPO.setFundComSum(fundComSum.toPlainString()); + } + } + return insuranceAccountDetailPO; + } + + + public InsuranceAccountDetailPO accountSocial(InsuranceAccountDetailPO insuranceAccountDetailPO, InsuranceArchivesAccountPO accountPO, String billMonth) { + boolean welBaseDiffSign = siArchivesBiz.isDiffWelBase(); + if (accountPO.getSocial() != null) { + InsuranceArchivesSocialSchemePO socialPO = accountPO.getSocial(); + insuranceAccountDetailPO.setSocialPayOrg(socialPO.getPaymentOrganization()); + insuranceAccountDetailPO.setSocialAccount(socialPO.getSocialAccount()); + insuranceAccountDetailPO.setSocialSchemeId(socialPO.getSocialSchemeId()); + insuranceAccountDetailPO.setSocialPaymentBaseString(socialPO.getSocialPaymentBaseString()); + insuranceAccountDetailPO.setSocialPaymentComBaseString(socialPO.getSocialPaymentComBaseString()); + //判断是否在起始缴纳月和最后缴纳月之间 + Boolean inDataRange = SalaryDateUtil.monthInRange(insuranceAccountDetailPO.getBillMonth(), socialPO.getSocialStartTime(), socialPO.getSocialEndTime()); + if ((NonPaymentEnum.YES.getValue().equals(socialPO.getNonPayment()) || socialPO.getNonPayment() == null) && socialPO.getSocialSchemeId() != null && inDataRange) { + List detailPOS = getInsuranceSchemeDetailMapper().queryListBySchemeId(socialPO.getSocialSchemeId()); + encryptUtil.decryptList(detailPOS, InsuranceSchemeDetailPO.class); + //方案中包含的需要缴纳社保的个人福利 + int monthIndex = Integer.parseInt(billMonth.split("-")[1]) - 1; + Map schemeperson = detailPOS.stream() + .filter(item -> + Objects.equals(IsPaymentEnum.YES.getValue(), item.getIsPayment()) && + Objects.equals(item.getPaymentScope(), PaymentScopeEnum.SCOPE_PERSON.getValue()) && + (item.getPaymentCycle() == null || item.getPaymentCycle() == 0 || (item.getPaymentCycle() == 1 && String.valueOf(item.getCycleSetting().charAt(monthIndex)).equals("1"))) + ) + .collect( + Collectors.toMap(InsuranceSchemeDetailPO::getInsuranceId, Function.identity())); + //档案中包含的基数信息 + HashMap archivesPerson = JSON.parseObject(socialPO.getSocialPaymentBaseString(), new HashMap().getClass()); + //需要核算社保的福利id 个人 + List needArchivesPerson = new ArrayList<>(); + if (archivesPerson != null) { + archivesPerson.forEach((id, value) -> { + if (schemeperson.containsKey(Long.valueOf(id))) { + needArchivesPerson.add(Long.valueOf(id)); + } + }); + } + //避免福利档案的方案未设置基数,导致核算时遗漏这些福利项的核算,遍历下方案相关福利项,并将遗漏的福利项id添加 + if(schemeperson.size() > 0) { + schemeperson.forEach((id, object) -> { + if (!needArchivesPerson.contains(id)) { + needArchivesPerson.add(id); + } + }); + } + //判断核算周期、核算月、福利起始缴纳月的关系 + checkCycleSettingWithStartMonth(schemeperson, billMonth, socialPO.getSocialStartTime()); + + List socialPer = new ArrayList<>(); + Map socialPerJsonMap = new HashMap<>(); + needArchivesPerson.forEach(e -> { + InsuranceSchemeDetailPO po = schemeperson.get(e); + BigDecimal paymentProportion = new BigDecimal(StringUtils.isBlank(po.getPaymentProportion()) ? "0" : po.getPaymentProportion()).divide(new BigDecimal("100")); + BigDecimal paymentNum = new BigDecimal((ObjectUtils.isEmpty(archivesPerson) || StringUtils.isBlank(archivesPerson.get(String.valueOf(e)))) ? "0" : archivesPerson.get(String.valueOf(e))); + BigDecimal fixedCost = StringUtils.isBlank(po.getFixedCost()) ? new BigDecimal("0") : new BigDecimal(po.getFixedCost()); + Integer newScale = po.getValidNum() == null ? 0 : po.getValidNum(); + BigDecimal result = new BigDecimal("0"); + if (Objects.equals(po.getPaymentCycle(), 1)) { + int monthValue = 1; + for (int i = monthIndex - 1; i >= 0; i--) { + String cycleValue = po.getCycleSetting().charAt(i) + ""; + if (Integer.parseInt(cycleValue) == 1) { + break; + } + monthValue++; + } + if (po.getAccountType() == 1) { + paymentNum = paymentNum.multiply(new BigDecimal(monthValue)); + fixedCost = fixedCost.multiply(new BigDecimal(monthValue)); + result = SalaryEntityUtil.carryRule(newScale, po.getRententionRule(), paymentNum.multiply(paymentProportion).add(fixedCost)); + } else { + for (int i = 0; i < monthValue; i++) { + result = result.add(SalaryEntityUtil.carryRule(newScale, po.getRententionRule(), paymentNum.multiply(paymentProportion).add(fixedCost))); + } + } + } else { + result = SalaryEntityUtil.carryRule(newScale, po.getRententionRule(), paymentNum.multiply(paymentProportion).add(fixedCost)); + } + socialPerJsonMap.put(String.valueOf(e), result.toPlainString()); + socialPer.add(result); + }); + insuranceAccountDetailPO.setSocialPerJson(JSON.toJSONString(socialPerJsonMap)); + BigDecimal socialPerSum = new BigDecimal("0"); + for (BigDecimal bigDecimal : socialPer) { + socialPerSum = socialPerSum.add(bigDecimal); + } + insuranceAccountDetailPO.setSocialPerSum(socialPerSum.toPlainString()); + //方案中包含的需要缴纳社保的单位福利 + Map schemeCom = detailPOS.stream() + .filter(item -> Objects.equals(IsPaymentEnum.YES.getValue(), item.getIsPayment()) && Objects.equals(item.getPaymentScope(), PaymentScopeEnum.SCOPE_COMPANY.getValue()) && + (item.getPaymentCycle() == null || item.getPaymentCycle() == 0 || (item.getPaymentCycle() == 1 && String.valueOf(item.getCycleSetting().charAt(monthIndex)).equals("1")))) + .collect( + Collectors.toMap(InsuranceSchemeDetailPO::getInsuranceId, Function.identity())); + //档案中包含的基数信息 + HashMap archivesCom = new HashMap<>(); + if (welBaseDiffSign) { + archivesCom = JSON.parseObject(socialPO.getSocialPaymentComBaseString(), new HashMap().getClass()); + } else { + archivesCom = JSON.parseObject(socialPO.getSocialPaymentBaseString(), new HashMap().getClass()); + } + //需要核算社保的福利id 单位 + List needArchivesCom = new ArrayList<>(); + if (archivesCom != null) { + archivesCom.forEach((id, value) -> { + if (schemeCom.containsKey(Long.valueOf(id))) { + needArchivesCom.add(Long.valueOf(id)); + } + }); + } + //避免福利档案的方案未设置基数,导致核算时遗漏这些福利项的核算,遍历下方案相关福利项,并将遗漏的福利项id添加 + if(schemeCom.size() > 0) { + schemeCom.forEach((id, object) -> { + if (!needArchivesCom.contains(id)) { + needArchivesCom.add(id); + } + }); + } + //判断核算周期、核算月、福利起始缴纳月的关系 + checkCycleSettingWithStartMonth(schemeCom, billMonth, socialPO.getSocialStartTime()); + + List socialCom = new ArrayList<>(); + Map sociaComJsonMap = new HashMap<>(); + HashMap finalArchivesCom = archivesCom; + needArchivesCom.stream().forEach(e -> { + InsuranceSchemeDetailPO po = schemeCom.get(e); + BigDecimal paymentProportion = new BigDecimal(StringUtils.isBlank(po.getPaymentProportion()) ? "0" : po.getPaymentProportion()).divide(new BigDecimal("100")); + BigDecimal paymentNum = new BigDecimal((ObjectUtils.isEmpty(finalArchivesCom) || StringUtils.isBlank(finalArchivesCom.get(String.valueOf(e)))) ? "0" : finalArchivesCom.get(String.valueOf(e))); + BigDecimal fixedCost = StringUtils.isBlank(po.getFixedCost()) ? new BigDecimal("0") : new BigDecimal(po.getFixedCost()); + Integer newScale = po.getValidNum() == null ? 0 : po.getValidNum(); + BigDecimal result = new BigDecimal("0"); + if (Objects.equals(po.getPaymentCycle(), 1)) { + int monthValue = 1; + for (int i = monthIndex - 1; i >= 0; i--) { + String cycleValue = po.getCycleSetting().charAt(i) + ""; + if (Integer.parseInt(cycleValue) == 1) { + break; + } + monthValue++; + } + if (po.getAccountType() == 1) { + paymentNum = paymentNum.multiply(new BigDecimal(monthValue)); + fixedCost = fixedCost.multiply(new BigDecimal(monthValue)); + result = SalaryEntityUtil.carryRule(newScale, po.getRententionRule(), paymentNum.multiply(paymentProportion).add(fixedCost)); + } else { + for (int i = 0; i < monthValue; i++) { + result = result.add(SalaryEntityUtil.carryRule(newScale, po.getRententionRule(), paymentNum.multiply(paymentProportion).add(fixedCost))); + } + } + } else { + result = SalaryEntityUtil.carryRule(newScale, po.getRententionRule(), paymentNum.multiply(paymentProportion).add(fixedCost)); + } + sociaComJsonMap.put(String.valueOf(e), result.toPlainString()); + socialCom.add(result); + + }); + insuranceAccountDetailPO.setSocialComJson(JSON.toJSONString(sociaComJsonMap)); + BigDecimal socialComSum = new BigDecimal("0"); + for (BigDecimal decimal : socialCom) { + socialComSum = socialComSum.add(decimal); + } + insuranceAccountDetailPO.setSocialComSum(socialComSum.toPlainString()); + } + + } + return insuranceAccountDetailPO; + } + + /** + * 对于核算月和福利起始缴纳月处于同年时,要避免根据周期缴纳福利费用时,可能出现的多余费用缴纳情况 + * @param schemeDetail 福利方案明细 + * @param billMonth 核算月 + * @param startMonth 福利起始缴纳月 + * @return + */ + public Map checkCycleSettingWithStartMonth(Map schemeDetail, String billMonth, String startMonth) { + String billYear = billMonth.substring(0,4); + if (StringUtils.isNotBlank(startMonth) && billYear.equals(startMonth.substring(0,4))) { + int startMonthIndex = Integer.parseInt(startMonth.split("-")[1]) - 1; + schemeDetail.forEach((k, v) -> { + if (v.getPaymentCycle() != null && v.getPaymentCycle().equals(1) && startMonthIndex != 0) { + StringBuilder newCycleSetting = new StringBuilder(v.getCycleSetting()); + newCycleSetting.setCharAt(startMonthIndex - 1, '1'); + v.setCycleSetting(newCycleSetting.toString()); + } + }); + + } + return schemeDetail; + } + + private List> splitList(List list, int groupSize) { + int length = list.size(); + // 计算可以分成多少组 + int num = (length + groupSize - 1) / groupSize; + List> newList = new ArrayList<>(num); + for (int i = 0; i < num; i++) { + // 开始位置 + int fromIndex = i * groupSize; + // 结束位置 + int toIndex = (i + 1) * groupSize < length ? (i + 1) * groupSize : length; + newList.add(list.subList(fromIndex, toIndex)); + } + return newList; + } + + /** + * 根据个税扣缴义务人和账单月,获取三类福利档案中符合缴纳开始结束月区间的人员id + * @param paymentOrganization + * @param billMonth + * @return + */ + public List listCanPayEmpIds(Long paymentOrganization, String billMonth) { + List listCanPayEmpIds = new ArrayList<>(); + //社保档案中可进行缴纳的人员 + List socialCanPayEmpIds = getSocialSchemeMapper().listCanPayEmpIds(paymentOrganization, billMonth); + //公积金档案中可进行缴纳的人员 + List fundCanPayEmpIds = getFundSchemeMapper().listCanPayEmpIds(paymentOrganization, billMonth); + //其他福利档案中可进行缴纳的人员 + List otherCanPayEmpIds = getOtherSchemeMapper().listCanPayEmpIds(paymentOrganization, billMonth); + if (socialCanPayEmpIds != null && socialCanPayEmpIds.size() > 0) { + listCanPayEmpIds.addAll(socialCanPayEmpIds); + } + if (fundCanPayEmpIds != null && fundCanPayEmpIds.size() > 0) { + listCanPayEmpIds.addAll(fundCanPayEmpIds); + } + if (otherCanPayEmpIds != null && otherCanPayEmpIds.size() > 0) { + listCanPayEmpIds.addAll(otherCanPayEmpIds); + } + //去重 + listCanPayEmpIds = listCanPayEmpIds.stream().distinct().collect(Collectors.toList()); + return listCanPayEmpIds; + } + + private void handleData(List ids, AccountParam param) { + String billMonth = param.getBillMonth(); + try { + List list = new ArrayList<>(); + List> partitionDetailTempInfo = Lists.partition((List) ids, 100); + partitionDetailTempInfo.forEach(part -> list.addAll( + getSIAccountDetailTempMapper().getListByEmployeeIdsAndBillMonth(part, billMonth, param.getPaymentOrganization()))); + + encryptUtil.decryptList(list, InsuranceAccountDetailTempPO.class); + Integer paymentStatus = 0; + log.info("核算明细临时表 hrsa_bill_detail_temp待处理数量:{}", list.size()); + List> partitionIds = Lists.partition((List) ids, 100); + log.info("bill_detail入库前删除数据数量:{}", ids.size()); + for (List part : partitionIds) { + getInsuranceAccountDetailMapper().batchDelAccountDetails(part, billMonth, param.getPaymentOrganization(), paymentStatus); + + //删除账单月份+个税扣缴义务人+人员id下的调差数据 + getInsuranceCompensationMapper().deleteByBillMonthPayOrgEmpIds(InsuranceCompensationPO.builder() + .billMonth(billMonth) + .paymentOrganization(param.getPaymentOrganization()) + .employeeIds(part) + .build()); + } + + //生成bill_detail入库数据 + List collect = list.stream().map(item -> { + InsuranceAccountDetailPO insuranceAccountDetailPO = new InsuranceAccountDetailPO(); + BeanUtils.copyProperties(item, insuranceAccountDetailPO); + return insuranceAccountDetailPO; + }).collect(Collectors.toList()); + if (CollectionUtils.isNotEmpty(collect)) { + log.info("bill_detail入库数据数量:{}", collect.size()); + batchSaveAccountInspectDetail(collect); + encryptUtil.encryptList(collect, InsuranceAccountDetailPO.class); + List> lists = splitDetailList(collect, 20); + lists.forEach(subList -> { + getInsuranceAccountDetailMapper().batchSaveAccountDetails(subList); + }); + updateBatchAccount(param); + } + } catch (Exception e) { + log.error("福利核算数据处理失败", e); + getSalaryAcctProgressService(user).fail(SalaryCacheKey.ACCT_PROGRESS + param.getBillMonth(), SalaryI18nUtil.getI18nLabel(0, "福利核算出错") + ": " + e.getMessage()); + throw e; + } + } + + public void batchSaveAccountInspectDetail(List list) { + List insuranceAccountInspectAllPOS = accountInspect(list, true); + batchDelInspectDetail(insuranceAccountInspectAllPOS); + List insuranceAccountInspectPOS = accountInspect(list, false); + if (CollectionUtils.isNotEmpty(insuranceAccountInspectPOS)) { + batchSaveInspectDetail(insuranceAccountInspectPOS); + } + } + public void batchDelInspectDetail(List list) { + List> lists = Lists.partition(list, 100); + lists.forEach(getInsuranceAccountInspectMapper()::batchDelInspectDetails); + + } + + public void batchSaveInspectDetail(List list) { + List> lists = Lists.partition(list, 100); + lists.forEach(getInsuranceAccountInspectMapper()::batchSaveInspectDetails); + } + + public List accountInspect(List list, boolean isAll) { + List insuranceAccountInspectPOS = new ArrayList<>(); + for (InsuranceAccountDetailPO e : list) { + if (isAll || detailCheck(e.getSocialPerJson()) || detailCheck(e.getSocialComJson()) + || detailCheck(e.getFundPerJson()) || detailCheck(e.getFundComJson()) + || detailCheck(e.getOtherPerJson()) || detailCheck(e.getOtherComJson())) { + InsuranceAccountInspectPO insuranceAccountInspectPO = InsuranceAccountInspectPO.builder().build(); + insuranceAccountInspectPO.setSupplementaryProjects(e.getSupplementaryProjects()); + insuranceAccountInspectPO.setInspectStatus(InspectStatusEnum.IGNORE.getValue()); + insuranceAccountInspectPO.setBillMonth(e.getBillMonth()); + insuranceAccountInspectPO.setCreator(e.getCreator()); + insuranceAccountInspectPO.setCreateTime(new Date()); + insuranceAccountInspectPO.setDeleteType(DeleteTypeEnum.NOT_DELETED.getValue()); + insuranceAccountInspectPO.setEmployeeId(e.getEmployeeId()); + insuranceAccountInspectPO.setPaymentStatus(e.getPaymentStatus()); + insuranceAccountInspectPO.setSupplementaryMonth(e.getSupplementaryMonth()); + insuranceAccountInspectPO.setTenantKey(e.getTenantKey()); + insuranceAccountInspectPO.setUpdateTime(new Date()); + insuranceAccountInspectPOS.add(insuranceAccountInspectPO); + } + } + return insuranceAccountInspectPOS; + } + + public boolean detailCheck(String baseString) { + if (StringUtils.isEmpty(baseString)) { + return false; + } + Map baseStringMap = JSON.parseObject(baseString, new TypeReference>() { + }); + if (baseStringMap == null || baseStringMap.size() == 0) { + return false; + } + return baseStringMap.values().stream().anyMatch(item -> StringUtils.isEmpty(item) || Pattern.matches("^([0].?[0]*)$", item)); + + } + + private List> splitDetailList(List list, int groupSize) { + int length = list.size(); + // 计算可以分成多少组 + int num = (length + groupSize - 1) / groupSize; + List> newList = new ArrayList<>(num); + for (int i = 0; i < num; i++) { + // 开始位置 + int fromIndex = i * groupSize; + // 结束位置 + int toIndex = (i + 1) * groupSize < length ? (i + 1) * groupSize : length; + newList.add(list.subList(fromIndex, toIndex)); + } + return newList; + } + + /** + * 更新台账接口 + */ + public void updateBatchAccount(AccountParam param) { + String billMonth = param.getBillMonth(); + + List insuranceAccountDetailPOS = getInsuranceAccountDetailMapper().selectList(billMonth, param.getPaymentOrganization()); + Map> map = insuranceAccountDetailPOS.stream().filter(item -> item.getEmployeeId() != null) + .collect(Collectors.groupingBy(InsuranceAccountDetailPO::getEmployeeId)); + int socialAccountPerson = 0; + int funcAccountPerson = 0; + int otherAccountPerson = 0; + BigDecimal socialSum = new BigDecimal("0"); + BigDecimal fundSum = new BigDecimal("0"); + BigDecimal otherSum = new BigDecimal("0"); + for (Map.Entry> entry : map.entrySet()) { + List v = entry.getValue(); + BigDecimal socialTemp = new BigDecimal("0"); + BigDecimal fundTemp = new BigDecimal("0"); + BigDecimal otherTemp = new BigDecimal("0"); + encryptUtil.decryptList(v, InsuranceAccountDetailPO.class); + boolean socialPersonFlag = false; + boolean fundPersonFlag = false; + boolean otherPersonFlag = false; + for (InsuranceAccountDetailPO item : v) { + // 判断社保是否为0 + socialPersonFlag = (StringUtils.isBlank(item.getSocialSum()) || SalaryEntityUtil.StringEqZERO(item.getSocialSum())); + BigDecimal socialPerson = socialPersonFlag ? new BigDecimal("0") : new BigDecimal(item.getSocialSum()); + fundPersonFlag = (StringUtils.isBlank(item.getFundSum()) || SalaryEntityUtil.StringEqZERO(item.getFundSum())); + BigDecimal fundPerson = fundPersonFlag ? new BigDecimal("0") : new BigDecimal(item.getFundSum()); + otherPersonFlag = (StringUtils.isBlank(item.getOtherSum()) || SalaryEntityUtil.StringEqZERO(item.getOtherSum())); + BigDecimal otherPerson = otherPersonFlag ? new BigDecimal("0") : new BigDecimal(item.getOtherSum()); + socialTemp = socialTemp.add(socialPerson); + fundTemp = fundTemp.add(fundPerson); + otherTemp = otherTemp.add(otherPerson); + if (!socialPersonFlag && !item.getPaymentStatus().equals(PaymentStatusEnum.BALANCE.getValue())) { + socialAccountPerson += 1; + } + if (!fundPersonFlag && !item.getPaymentStatus().equals(PaymentStatusEnum.BALANCE.getValue())) { + funcAccountPerson += 1; + } + if (!otherPersonFlag && !item.getPaymentStatus().equals(PaymentStatusEnum.BALANCE.getValue())) { + otherAccountPerson += 1; + } + } + + if (!"0".equals(socialTemp.toPlainString())) { + socialSum = socialSum.add(socialTemp); + } + if (!"0".equals(fundTemp.toPlainString())) { + fundSum = fundSum.add(fundTemp); + } + if (!"0".equals(otherTemp.toPlainString())) { + otherSum = otherSum.add(otherTemp); + } + } + InsuranceAccountBatchPO insuranceAccountBatchPO = getInsuranceAccountBatchMapper().getByBillMonth(billMonth, param.getPaymentOrganization()); + encryptUtil.decrypt(insuranceAccountBatchPO, InsuranceAccountBatchPO.class); + + insuranceAccountBatchPO.setAccountant(user.getLastname()); + insuranceAccountBatchPO.setUpdateTime(new Date()); + insuranceAccountBatchPO.setSocialPay(socialSum.toPlainString()); + insuranceAccountBatchPO.setSocialNum(socialAccountPerson); + insuranceAccountBatchPO.setFundNum(funcAccountPerson); + insuranceAccountBatchPO.setFundPay(fundSum.toPlainString()); + insuranceAccountBatchPO.setOtherNum(otherAccountPerson); + insuranceAccountBatchPO.setOtherPay(otherSum.toPlainString()); + encryptUtil.encrypt(insuranceAccountBatchPO, InsuranceAccountBatchPO.class); + getInsuranceAccountBatchMapper().updateById(insuranceAccountBatchPO); +// LoggerContext insuranceSchemeContext = new LoggerContext(); +// insuranceSchemeContext.setTargetId(String.valueOf(insuranceAccountBatchPO.getId())); +// insuranceSchemeContext.setTargetName(insuranceAccountBatchPO.getBillMonth()); +// insuranceSchemeContext.setOperateType(OperateTypeEnum.UPDATE.getValue()); +// insuranceSchemeContext.setOperateTypeName(SalaryI18nUtil.getI18nLabel(tenantKey, employeeId, 100491, "更新台账")); +// insuranceSchemeContext.setOperatedesc(SalaryI18nUtil.getI18nLabel(tenantKey, employeeId, 100491, "更新台账")); +// insuranceSchemeContext.setNewValues(insuranceAccountBatchPO); +// siAccountLoggerTemplate.write(insuranceSchemeContext); + + } + + public void siFile(String billMonth, Long paymentOrganization) { + + //开始归档数据 + InsuranceAccountBatchPO insuranceAccountBatchPO = getInsuranceAccountBatchMapper().getByBillStatus(billMonth, BillStatusEnum.NOT_ARCHIVED.getValue(), paymentOrganization); + encryptUtil.decrypt(insuranceAccountBatchPO, InsuranceAccountBatchPO.class); + SalaryAssert.notNull(insuranceAccountBatchPO, SalaryI18nUtil.getI18nLabel(0, "月份账单不存在")); + SalaryAssert.isFalse(Objects.equals(insuranceAccountBatchPO.getBillStatus(), BillStatusEnum.ARCHIVED.getValue()), SalaryI18nUtil.getI18nLabel(0, "月份账单已归档")); + insuranceAccountBatchPO.setBillStatus(BillStatusEnum.ARCHIVED.getValue()); + encryptUtil.encrypt(insuranceAccountBatchPO, InsuranceAccountBatchPO.class); + getInsuranceAccountBatchMapper().updateById(insuranceAccountBatchPO); + //日志记录 +// LoggerContext insuranceSchemeContext = new LoggerContext(); +// insuranceSchemeContext.setTargetId(String.valueOf(insuranceAccountBatchPO.getId())); +// insuranceSchemeContext.setTargetName(insuranceAccountBatchPO.getBillMonth()); +// insuranceSchemeContext.setOperateType(OperateTypeEnum.UPDATE.getValue()); +// insuranceSchemeContext.setOperateTypeName(SalaryI18nUtil.getI18nLabel(tenantKey, employeeId, 100506, "台账归档")); +// insuranceSchemeContext.setOperatedesc(SalaryI18nUtil.getI18nLabel(tenantKey, employeeId, 100506, "台账归档")); +// insuranceSchemeContext.setNewValues(insuranceAccountBatchPO); +// siAccountLoggerTemplate.write(insuranceSchemeContext); + } } diff --git a/src/com/engine/salary/timer/AutoSiAccountAndFileJob.java b/src/com/engine/salary/timer/AutoSiAccountAndFileJob.java new file mode 100644 index 000000000..7d1b8f757 --- /dev/null +++ b/src/com/engine/salary/timer/AutoSiAccountAndFileJob.java @@ -0,0 +1,107 @@ +package com.engine.salary.timer; + +import cn.hutool.core.util.StrUtil; +import com.engine.common.util.ServiceUtil; +import com.engine.salary.common.SalaryContext; +import com.engine.salary.entity.siaccount.param.AccountParam; +import com.engine.salary.entity.taxagent.po.TaxAgentPO; +import com.engine.salary.formlua.util.RegularUtil; +import com.engine.salary.service.SIAccountService; +import com.engine.salary.service.TaxAgentService; +import com.engine.salary.service.impl.SIAccountServiceImpl; +import com.engine.salary.service.impl.TaxAgentServiceImpl; +import lombok.extern.slf4j.Slf4j; +import weaver.hrm.User; +import weaver.interfaces.schedule.BaseCronJob; + +import java.text.SimpleDateFormat; +import java.util.Arrays; +import java.util.Calendar; +import java.util.List; +import java.util.stream.Collectors; + +/** + * @Author: sy + * @Description: 福利台账核算并归档任务 + * @Date: 2024/1/15 + **/ +@Slf4j +public class AutoSiAccountAndFileJob extends BaseCronJob { + + private String diffToCurrentMonth; + + public String getDiffToCurrentMonth() { + return diffToCurrentMonth; + } + + public void setDiffToCurrentMonth(String diffToCurrentMonth) { + this.diffToCurrentMonth = diffToCurrentMonth; + } + + private String taxAgentNames; + + public String getTaxAgentNames() { + return taxAgentNames; + } + + public void setTaxAgentNames(String taxAgentNames) { + this.taxAgentNames = taxAgentNames; + } + + private String fileFlag; + + public String getFileFlag() { + return fileFlag; + } + + public void setFileFlag(String fileFlag) { + this.fileFlag = fileFlag; + } + + private TaxAgentService getTaxAgentService(User user) { + SalaryContext.get().setValue("user",user); + return ServiceUtil.getService(TaxAgentServiceImpl.class, user); + } + + public SIAccountService getSIAccountService(User user) { + SalaryContext.get().setValue("user",user); + return ServiceUtil.getService(SIAccountServiceImpl.class, user); + } + + @Override + public void execute() { + if (StrUtil.isNotBlank(diffToCurrentMonth) && RegularUtil.isInteger(diffToCurrentMonth)) { + User user = new User(); + user.setUid(1); + user.setLoginid("sysadmin"); + user.setLastname("sysadmin"); + + Calendar accountTime= Calendar.getInstance(); +// prevMonth.setTimeInMillis(System.currentTimeMillis()); + accountTime.set(Calendar.MONTH, accountTime.get(Calendar.MONTH) + Integer.parseInt(diffToCurrentMonth)); + SimpleDateFormat s=new SimpleDateFormat("yyyy-MM"); + + String accountMonth = s.format(accountTime.getTime()); + boolean isFile = false; + if (StrUtil.isNotBlank(fileFlag) && "true".equals(fileFlag)) { + isFile = true; + } + //核算并归档 + List taxAgentList = getTaxAgentService(user).listAll(); + //判断是否过滤个税扣缴义务人 + if (StrUtil.isNotBlank(taxAgentNames)) { + List taxAgentNameList = Arrays.stream(taxAgentNames.split(",")).map(String::new).collect(Collectors.toList()); + taxAgentList = taxAgentList.stream().filter(f -> taxAgentNameList.contains(f.getName())).collect(Collectors.toList()); + } + for (TaxAgentPO po : taxAgentList) { + try { + getSIAccountService(user).saveAndFile(AccountParam.builder().paymentOrganization(po.getId()).billMonth(accountMonth).flag(true).fileFlag(isFile).build()); + } catch (Exception e) { + log.info("个税扣缴义务人-" + po.getName() + ",新建账单月份" + accountMonth + "的福利核算(并归档)过程失败,原因:" + e.getMessage()); + } + } + + } + + } +}