package com.engine.salary.timer; import com.engine.common.util.ServiceUtil; import com.engine.salary.entity.datacollection.DataCollectionEmployee; import com.engine.salary.entity.taxagent.param.TaxAgentEmpSaveParam; import com.engine.salary.entity.taxagent.po.TaxAgentManageRangePO; import com.engine.salary.entity.taxagent.po.TaxAgentPO; import com.engine.salary.enums.salarysob.SalaryEmployeeStatusEnum; import com.engine.salary.enums.salarysob.TargetTypeEnum; import com.engine.salary.enums.taxagent.TaxAgentRangeTypeEnum; import com.engine.salary.mapper.datacollection.EmployMapper; import com.engine.salary.mapper.taxagent.TaxAgentManageRangeMapper; import com.engine.salary.mapper.taxagent.TaxAgentMapper; import com.engine.salary.service.TaxAgentEmpService; import com.engine.salary.service.impl.TaxAgentEmpServiceImpl; import com.engine.salary.util.JsonUtil; import com.engine.salary.util.SalaryEntityUtil; import com.engine.salary.util.db.MapperProxyFactory; import com.google.common.collect.Lists; import lombok.extern.slf4j.Slf4j; import org.apache.commons.collections4.CollectionUtils; import org.apache.commons.lang3.math.NumberUtils; import weaver.hrm.User; import weaver.interfaces.schedule.BaseCronJob; import java.util.*; import java.util.stream.Collectors; @Slf4j public class SyncTaxAgentEmpJob extends BaseCronJob { private TaxAgentMapper getTaxAgentMapper() { return MapperProxyFactory.getProxy(TaxAgentMapper.class); } private TaxAgentManageRangeMapper getTaxAgentManageRangeMapper() { return MapperProxyFactory.getProxy(TaxAgentManageRangeMapper.class); } private EmployMapper getEmployMapper() { return MapperProxyFactory.getProxy(EmployMapper.class); } private TaxAgentEmpService getTaxAgentEmpService(User user) { return ServiceUtil.getService(TaxAgentEmpServiceImpl.class, user); } @Override public void execute() { long time = System.currentTimeMillis(); log.info("计划任务【SyncTaxAgentEmpJob】开始执行"); start(); log.info("计划任务【SyncTaxAgentEmpJob】执行结束,用时:" + (System.currentTimeMillis() - time)); } /** * 代码执行逻辑方法 */ private void start() { try { //获取所以个税扣缴义务人 List taxAgentPOS = getTaxAgentMapper().listAll(); //执行同步 taxAgentPOS.forEach(taxAgent -> { Long taxAgentId = taxAgent.getId(); // 查询已有的管理范围 List taxAgentManageAllRanges = getTaxAgentManageRangeMapper().listSome(TaxAgentManageRangePO.builder() .taxAgentId(taxAgentId).rangeType(TaxAgentRangeTypeEnum.TAXAGENT.getValue()).build()); /* 检查当前个税扣缴义务人的所有人员范围与所有分管理员的管理范围===========================start */ List allRanges = Lists.newArrayList(taxAgentManageAllRanges); // 去重 allRanges = allRanges.stream().collect(Collectors.collectingAndThen(Collectors.toCollection(() -> new TreeSet<>( Comparator.comparing(f -> f.getTaxAgentId() + "." + f.getRangeType() + "." + f.getTargetType() + "." + f.getTargetId() + "." + f.getEmployeeStatus() + "." + f.getIncludeType())) ), ArrayList::new)); List allSalaryEmployees = this.getManageRangeSalaryEmployees(null, taxAgentId, allRanges); /* 同步本地人员范围的关联人员=========================== */ syncLocalEmp(taxAgentId, allSalaryEmployees, 0L); }); } catch (Exception e) { log.error("计划任务【SyncTaxAgentEmpJob】执行异常:" + e); } } /** * 同步本地范围关联人员 * * @param taxAgentId * @param allSalaryEmployees 个税扣缴义务人下的所有人员 * @param employeeId */ private void syncLocalEmp(Long taxAgentId, List allSalaryEmployees, Long employeeId) { try { List taxAgentEmpSaveParamList = Collections.singletonList(getTaxAgentEmpSyncParam(taxAgentId, allSalaryEmployees)); // 同步个税扣缴义务人的人员 getTaxAgentEmpService(null).syncTaxAgentEmployee(taxAgentEmpSaveParamList, employeeId); } catch (Exception e) { log.error("同步个税扣缴人员范围异常", e); } } /** * 获取个税扣缴义务人的同步参数 * * @param taxAgentId * @param allSalaryEmployees * @return */ private TaxAgentEmpSaveParam getTaxAgentEmpSyncParam(Long taxAgentId, List allSalaryEmployees) { return TaxAgentEmpSaveParam.builder() .taxAgentId(taxAgentId) .salaryEmployeeList(allSalaryEmployees) .build(); } /** * 获取范围下的人员 * * @param employeeStatus * @param taxAgentId * @param allTaxAgentManageRanges * @return */ private List getManageRangeSalaryEmployees(SalaryEmployeeStatusEnum employeeStatus, Long taxAgentId, List allTaxAgentManageRanges) { List includeAllTaxAgentManageRanges = allTaxAgentManageRanges.stream().filter(f -> f.getIncludeType().equals(NumberUtils.INTEGER_ONE)).collect(Collectors.toList()); if (CollectionUtils.isEmpty(includeAllTaxAgentManageRanges)) { return Collections.emptyList(); } List excludeAllTaxAgentManageRanges = allTaxAgentManageRanges.stream().filter(f -> f.getIncludeType().equals(NumberUtils.INTEGER_ZERO)).collect(Collectors.toList()); List includeSalaryEmployees = Lists.newArrayList(); List includeTaxAgentManageRanges = includeAllTaxAgentManageRanges.stream().filter(f -> f.getTaxAgentId().equals(taxAgentId)).collect(Collectors.toList()); if (CollectionUtils.isEmpty(includeTaxAgentManageRanges)) { return includeSalaryEmployees; } // 如果需要状态过滤 List personnelStatuss = Lists.newArrayList(); if (employeeStatus != null) { // 查询人员状态 // if (employeeStatus.equals(SalaryEmployeeStatusEnum.NORMAL)) { // personnelStatuss = UserStatusEnum.getNormalStatus(); // } else if (employeeStatus.equals(SalaryEmployeeStatusEnum.UNAVAILABLE)) { // personnelStatuss = UserStatusEnum.getUnavailableStatus(); // } personnelStatuss.add(employeeStatus.getValue().toString()); } // 根据上一步的查询参数查询人员 includeSalaryEmployees = listSalaryEmployeeByManageRange(includeTaxAgentManageRanges, personnelStatuss); if (CollectionUtils.isEmpty(includeSalaryEmployees)) { return includeSalaryEmployees; } // 查询管理范围(从范围中排除) List excludeTaxAgentManageRanges = excludeAllTaxAgentManageRanges.stream().filter(f -> f.getTaxAgentId().equals(taxAgentId)).collect(Collectors.toList()); if (CollectionUtils.isNotEmpty(excludeTaxAgentManageRanges)) { // 根据上一步的查询参数查询人员 List excludeSalaryEmployees = listSalaryEmployeeByManageRange(excludeTaxAgentManageRanges, personnelStatuss); // 需要排除的人员范围 Set excludeEmployeeIds = SalaryEntityUtil.properties(excludeSalaryEmployees, DataCollectionEmployee::getEmployeeId); // 过滤人员 includeSalaryEmployees = includeSalaryEmployees.stream() .filter(salaryEmployee -> !excludeEmployeeIds.contains(salaryEmployee.getEmployeeId())) .collect(Collectors.toList()); } return includeSalaryEmployees; } /** * 根据范围加载人员 * * @param taxAgentManageRanges * @return */ private List listSalaryEmployeeByManageRange(List taxAgentManageRanges, List personnelStatuss) { if (CollectionUtils.isEmpty(taxAgentManageRanges)) { return Collections.emptyList(); } List salaryEmployees = getEmployMapper().listAllFields(); List salaryEmployeeList = Lists.newArrayList(); for (TaxAgentManageRangePO manageRange : taxAgentManageRanges) { salaryEmployeeList.addAll(salaryEmployees.stream() .filter(salaryEmployee -> { // 判断人员状态 List hrmStatusList = JsonUtil.parseList(manageRange.getEmployeeStatus(), String.class); // 有状态过滤则取交集 if (CollectionUtils.isNotEmpty(personnelStatuss)) { hrmStatusList = hrmStatusList.stream().filter(personnelStatuss::contains).collect(Collectors.toList()); } if (CollectionUtils.isNotEmpty(hrmStatusList) && !hrmStatusList.contains(salaryEmployee.getStatus())) { return false; } if (Objects.equals(manageRange.getTargetType(), TargetTypeEnum.ALL.getValue())) { return true; } if (Objects.equals(manageRange.getTargetType(), TargetTypeEnum.EMPLOYEE.getValue()) && Objects.equals(manageRange.getTargetId(), salaryEmployee.getEmployeeId())) { return true; } if (Objects.equals(manageRange.getTargetType(), TargetTypeEnum.DEPT.getValue()) && Objects.equals(manageRange.getTargetId(), salaryEmployee.getDepartmentId())) { return true; } if (Objects.equals(manageRange.getTargetType(), TargetTypeEnum.SUBCOMPANY.getValue()) && Objects.equals(manageRange.getTargetId(), salaryEmployee.getSubcompanyid())) { return true; } if (Objects.equals(manageRange.getTargetType(), TargetTypeEnum.POSITION.getValue()) && Objects.equals(manageRange.getTargetId(), salaryEmployee.getJobtitleId())) { return true; } return false; }).collect(Collectors.toList())); } // 去重 salaryEmployeeList = salaryEmployeeList.stream().collect(Collectors.collectingAndThen(Collectors.toCollection(() -> new TreeSet<>(Comparator.comparing(DataCollectionEmployee::getEmployeeId))), ArrayList::new)); return salaryEmployeeList; } }