weaver-hrm-salary/src/com/engine/salary/service/impl/TaxAgentManageRangeServiceI...

749 lines
39 KiB
Java
Raw Normal View History

2022-05-18 09:19:50 +08:00
package com.engine.salary.service.impl;
import com.api.formmode.mybatis.util.SqlProxyHandle;
2022-11-22 15:37:07 +08:00
import com.cloudstore.dev.api.util.Util_DataCache;
2022-05-24 09:14:26 +08:00
import com.engine.common.util.ServiceUtil;
2022-05-18 09:19:50 +08:00
import com.engine.core.impl.Service;
2022-05-24 09:14:26 +08:00
import com.engine.salary.biz.EmployBiz;
2022-05-18 09:19:50 +08:00
import com.engine.salary.entity.datacollection.DataCollectionEmployee;
2022-05-24 09:14:26 +08:00
import com.engine.salary.entity.hrm.DeptInfo;
import com.engine.salary.entity.hrm.HrmStatus;
import com.engine.salary.entity.hrm.PositionInfo;
import com.engine.salary.entity.hrm.SubCompanyInfo;
2022-05-19 16:36:18 +08:00
import com.engine.salary.entity.taxagent.bo.TaxAgentBO;
import com.engine.salary.entity.taxagent.dto.TaxAgentManageRangeEmployeeListDTO;
2022-05-18 09:19:50 +08:00
import com.engine.salary.entity.taxagent.dto.TaxAgentManageRangeListDTO;
2022-05-19 16:36:18 +08:00
import com.engine.salary.entity.taxagent.param.*;
2022-05-18 09:19:50 +08:00
import com.engine.salary.entity.taxagent.po.TaxAgentManageRangePO;
2022-05-19 16:36:18 +08:00
import com.engine.salary.entity.taxagent.po.TaxAgentPO;
2022-05-24 09:14:26 +08:00
import com.engine.salary.enums.UserStatusEnum;
import com.engine.salary.enums.salarysob.SalaryEmployeeStatusEnum;
2022-05-18 09:19:50 +08:00
import com.engine.salary.enums.salarysob.TargetTypeEnum;
import com.engine.salary.enums.taxagent.TaxAgentRangeTypeEnum;
2022-05-19 16:36:18 +08:00
import com.engine.salary.exception.SalaryRunTimeException;
2022-05-18 09:19:50 +08:00
import com.engine.salary.mapper.datacollection.EmployMapper;
import com.engine.salary.mapper.sys.SalarySysConfMapper;
2022-05-18 09:19:50 +08:00
import com.engine.salary.mapper.taxagent.TaxAgentManageRangeMapper;
import com.engine.salary.service.*;
import com.engine.salary.sys.entity.po.SalarySysConfPO;
2022-05-18 09:19:50 +08:00
import com.engine.salary.util.SalaryEntityUtil;
2022-05-19 16:36:18 +08:00
import com.engine.salary.util.SalaryI18nUtil;
2022-05-24 09:14:26 +08:00
import com.engine.salary.util.db.MapperProxyFactory;
import com.engine.salary.util.excel.ExcelParseHelper;
2022-05-18 09:19:50 +08:00
import com.engine.salary.util.page.PageInfo;
2022-05-25 13:10:03 +08:00
import com.engine.salary.util.page.SalaryPageUtil;
2022-05-18 09:19:50 +08:00
import com.google.common.collect.Lists;
import com.google.common.collect.Maps;
2022-11-22 15:37:07 +08:00
import com.weaver.util.threadPool.ThreadPoolUtil;
import com.weaver.util.threadPool.entity.LocalRunnable;
2022-05-18 09:19:50 +08:00
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.collections4.CollectionUtils;
import org.apache.commons.lang3.StringUtils;
import org.apache.commons.lang3.Validate;
2022-05-18 09:19:50 +08:00
import org.apache.commons.lang3.math.NumberUtils;
import org.apache.poi.util.IOUtils;
import weaver.file.ImageFileManager;
import weaver.general.Util;
2022-05-24 09:14:26 +08:00
import weaver.hrm.User;
2022-05-18 09:19:50 +08:00
import java.io.InputStream;
2022-05-18 09:19:50 +08:00
import java.util.*;
2022-06-09 17:45:28 +08:00
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
2022-05-18 09:19:50 +08:00
import java.util.stream.Collectors;
/**
* 个税扣缴义务人的管理范围
* <p>Copyright: Copyright (c) 2022</p>
* <p>Company: 泛微软件</p>
*
* @author qiantao
* @version 1.0
**/
@Slf4j
public class TaxAgentManageRangeServiceImpl extends Service implements TaxAgentManageRangeService {
2022-06-09 17:45:28 +08:00
private static final ExecutorService taskExecutor = Executors.newFixedThreadPool(5);
2022-05-24 09:14:26 +08:00
private TaxAgentManageRangeMapper getTaxAgentManageRangeMapper() {
return MapperProxyFactory.getProxy(TaxAgentManageRangeMapper.class);
}
2022-05-18 09:19:50 +08:00
2022-07-11 09:39:57 +08:00
private TaxAgentService getTaxAgentService(User user) {
2022-05-31 21:05:59 +08:00
return ServiceUtil.getService(TaxAgentServiceImpl.class, user);
2022-05-24 09:14:26 +08:00
}
2022-05-18 09:19:50 +08:00
2022-05-24 09:14:26 +08:00
private TaxAgentEmpService getTaxAgentEmpService(User user) {
return ServiceUtil.getService(TaxAgentEmpServiceImpl.class, user);
}
2022-05-18 09:19:50 +08:00
2022-12-07 20:15:52 +08:00
private SalaryArchiveService getSalaryArchiveService(User user) {
return ServiceUtil.getService(SalaryArchiveServiceImpl.class, user);
}
2022-05-24 09:14:26 +08:00
private EmployMapper getEmployMapper() {
return MapperProxyFactory.getProxy(EmployMapper.class);
}
2022-05-18 09:19:50 +08:00
private SalarySysConfMapper getSalarySysConfMapper() {
return SqlProxyHandle.getProxy(SalarySysConfMapper.class);
}
private SalaryEmployeeService getSalaryEmployeeService() {
return ServiceUtil.getService(SalaryEmployeeServiceImpl.class, user);
}
2022-05-18 09:19:50 +08:00
2022-05-24 09:14:26 +08:00
private EmployBiz employBiz = new EmployBiz();
2022-05-18 09:19:50 +08:00
private List<TaxAgentManageRangePO> listByTaxAgentIds(List<Long> taxAgentIds) {
if (CollectionUtils.isEmpty(taxAgentIds)) {
return Lists.newArrayList();
}
2022-05-24 09:14:26 +08:00
return getTaxAgentManageRangeMapper().listSome(TaxAgentManageRangePO.builder().rangeType(TaxAgentRangeTypeEnum.TAXAGENT.getValue()).taxAgentIds(taxAgentIds).build());
2022-05-18 09:19:50 +08:00
}
2022-05-19 16:36:18 +08:00
@Override
public List<TaxAgentManageRangePO> listBySubAdminIds(Collection<Long> taxAgentSubAdminIds) {
if (CollectionUtils.isEmpty(taxAgentSubAdminIds)) {
return Lists.newArrayList();
}
2022-05-24 09:14:26 +08:00
return getTaxAgentManageRangeMapper().listSome(TaxAgentManageRangePO.builder().rangeType(TaxAgentRangeTypeEnum.SUBADMIN.getValue()).taxAgentSubAdminIds(taxAgentSubAdminIds).build());
2022-05-19 16:36:18 +08:00
}
2022-05-18 09:19:50 +08:00
/**
* 获取范围下的人员
*
* @param taxAgentId
* @param allTaxAgentManageRanges
2022-11-22 15:37:07 +08:00
* @param salaryEmployees
2022-05-18 09:19:50 +08:00
* @return
*/
2022-11-22 15:37:07 +08:00
private List<DataCollectionEmployee> getManageRangeSalaryEmployees(Long taxAgentId, List<TaxAgentManageRangePO> allTaxAgentManageRanges, List<DataCollectionEmployee> salaryEmployees) {
2022-05-18 09:19:50 +08:00
List<TaxAgentManageRangePO> includeAllTaxAgentManageRanges = allTaxAgentManageRanges.stream().filter(f -> f.getIncludeType().equals(NumberUtils.INTEGER_ONE)).collect(Collectors.toList());
if (CollectionUtils.isEmpty(includeAllTaxAgentManageRanges)) {
return Collections.emptyList();
}
List<TaxAgentManageRangePO> excludeAllTaxAgentManageRanges = allTaxAgentManageRanges.stream().filter(f -> f.getIncludeType().equals(NumberUtils.INTEGER_ZERO)).collect(Collectors.toList());
List<DataCollectionEmployee> includeSalaryEmployees = Lists.newArrayList();
List<TaxAgentManageRangePO> includeTaxAgentManageRanges = includeAllTaxAgentManageRanges.stream().filter(f -> f.getTaxAgentId().equals(taxAgentId)).collect(Collectors.toList());
if (CollectionUtils.isEmpty(includeTaxAgentManageRanges)) {
return includeSalaryEmployees;
}
// 根据上一步的查询参数查询人员
2022-11-22 15:37:07 +08:00
includeSalaryEmployees = listSalaryEmployeeByManageRange(includeTaxAgentManageRanges, salaryEmployees);
2022-05-18 09:19:50 +08:00
if (CollectionUtils.isEmpty(includeSalaryEmployees)) {
return includeSalaryEmployees;
}
// 查询管理范围(从范围中排除)
List<TaxAgentManageRangePO> excludeTaxAgentManageRanges = excludeAllTaxAgentManageRanges.stream().filter(f -> f.getTaxAgentId().equals(taxAgentId)).collect(Collectors.toList());
if (CollectionUtils.isNotEmpty(excludeTaxAgentManageRanges)) {
// 根据上一步的查询参数查询人员
2022-11-22 15:37:07 +08:00
List<DataCollectionEmployee> excludeSalaryEmployees = listSalaryEmployeeByManageRange(excludeTaxAgentManageRanges, salaryEmployees);
2022-05-18 09:19:50 +08:00
// 需要排除的人员范围
Set<Long> excludeEmployeeIds = SalaryEntityUtil.properties(excludeSalaryEmployees, DataCollectionEmployee::getEmployeeId);
// 过滤人员
2022-12-07 20:15:52 +08:00
includeSalaryEmployees = includeSalaryEmployees.stream().filter(salaryEmployee -> !excludeEmployeeIds.contains(salaryEmployee.getEmployeeId())).collect(Collectors.toList());
2022-05-18 09:19:50 +08:00
}
return includeSalaryEmployees;
}
/**
* 根据范围加载人员
*
* @param taxAgentManageRanges
* @return
*/
2022-11-22 15:37:07 +08:00
private List<DataCollectionEmployee> listSalaryEmployeeByManageRange(List<TaxAgentManageRangePO> taxAgentManageRanges, List<DataCollectionEmployee> salaryEmployees) {
if (CollectionUtils.isEmpty(taxAgentManageRanges) || CollectionUtils.isEmpty(salaryEmployees)) {
2022-05-18 09:19:50 +08:00
return Collections.emptyList();
}
List<DataCollectionEmployee> salaryEmployeeList = Lists.newArrayList();
for (TaxAgentManageRangePO manageRange : taxAgentManageRanges) {
2022-12-07 20:15:52 +08:00
salaryEmployeeList.addAll(salaryEmployees.stream().filter(salaryEmployee -> {
if (StringUtils.isEmpty(manageRange.getEmployeeStatus()) || !manageRange.getEmployeeStatus().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()));
2022-05-18 09:19:50 +08:00
}
// 去重
salaryEmployeeList = salaryEmployeeList.stream().collect(Collectors.collectingAndThen(Collectors.toCollection(() -> new TreeSet<>(Comparator.comparing(DataCollectionEmployee::getEmployeeId))), ArrayList::new));
return salaryEmployeeList;
}
2022-05-24 09:14:26 +08:00
/**
* 根据分管理员id获取管理范围列表
*
* @param subAdminId
* @param includeType
* @return
*/
private List<TaxAgentManageRangePO> listBySubAdminIdAndIncludeType(Long subAdminId, Integer includeType) {
return getTaxAgentManageRangeMapper().listSome(TaxAgentManageRangePO.builder().taxAgentSubAdminId(subAdminId).rangeType(TaxAgentRangeTypeEnum.SUBADMIN.getValue()).includeType(includeType).build());
}
2022-05-18 09:19:50 +08:00
/**
* 获取个税口角义务人的管理范围
*
* @param taxAgentId
* @param includeType
* @return
*/
private List<TaxAgentManageRangePO> listByTaxAgentIdAndIncludeType(Long taxAgentId, Integer includeType) {
2022-05-24 09:14:26 +08:00
return getTaxAgentManageRangeMapper().listSome(TaxAgentManageRangePO.builder().taxAgentId(taxAgentId).rangeType(TaxAgentRangeTypeEnum.TAXAGENT.getValue()).includeType(includeType).build());
2022-05-18 09:19:50 +08:00
}
2022-05-24 09:14:26 +08:00
@Override
public PageInfo<TaxAgentManageRangeListDTO> listPageByParamAndIncludeType(TaxAgentSubAdminRangeQueryParam queryParam, Integer includeType) {
// 查询已有的管理范围
List<TaxAgentManageRangePO> taxAgentManageRanges = listBySubAdminIdAndIncludeType(queryParam.getSubAdminId(), includeType);
return listPageByParamAndIncludeType(taxAgentManageRanges, queryParam, includeType);
}
2022-05-18 09:19:50 +08:00
@Override
public PageInfo<TaxAgentManageRangeListDTO> listPageByParamAndIncludeType(TaxAgentRangeQueryParam queryParam, Integer includeType) {
// 查询已有的管理范围
List<TaxAgentManageRangePO> taxAgentManageRanges = listByTaxAgentIdAndIncludeType(queryParam.getTaxAgentId(), includeType);
return listPageByParamAndIncludeType(taxAgentManageRanges, queryParam, includeType);
}
private PageInfo<TaxAgentManageRangeListDTO> listPageByParamAndIncludeType(List<TaxAgentManageRangePO> taxAgentManageRanges, TaxAgentManageRangeQueryParam queryParam, Integer includeType) {
// 查询人员信息
2022-12-07 20:15:52 +08:00
List<Long> employeeIds = taxAgentManageRanges.stream().filter(e -> Objects.equals(e.getTargetType(), TargetTypeEnum.EMPLOYEE.getValue())).map(TaxAgentManageRangePO::getTargetId).collect(Collectors.toList());
2022-05-24 09:14:26 +08:00
// List<DataCollectionEmployee> employeeComInfos = comInfoCache.getCacheList(HrmEmployeeComInfo.class, employeeIds);
List<DataCollectionEmployee> employeeComInfos = employBiz.getEmployeeByIdsAll(employeeIds);
2022-05-18 09:19:50 +08:00
// 查询部门信息
2022-12-07 20:15:52 +08:00
List<Long> departmentIds = taxAgentManageRanges.stream().filter(e -> Objects.equals(e.getTargetType(), TargetTypeEnum.DEPT.getValue())).map(TaxAgentManageRangePO::getTargetId).collect(Collectors.toList());
2022-05-24 09:14:26 +08:00
List<DeptInfo> departmentComInfos = employBiz.getDeptInfoList(departmentIds);
2022-05-18 09:19:50 +08:00
// 查询分部信息
2022-12-07 20:15:52 +08:00
List<Long> subDepartmentIds = taxAgentManageRanges.stream().filter(e -> Objects.equals(e.getTargetType(), TargetTypeEnum.SUBCOMPANY.getValue())).map(TaxAgentManageRangePO::getTargetId).collect(Collectors.toList());
2022-05-24 09:14:26 +08:00
List<SubCompanyInfo> subDepartmentComInfos = employBiz.getSubCompanyInfoList(subDepartmentIds);
2022-05-18 09:19:50 +08:00
// 查询岗位信息
2022-12-07 20:15:52 +08:00
List<Long> positionIds = taxAgentManageRanges.stream().filter(e -> Objects.equals(e.getTargetType(), TargetTypeEnum.POSITION.getValue())).map(TaxAgentManageRangePO::getTargetId).collect(Collectors.toList());
2022-05-24 09:14:26 +08:00
List<PositionInfo> positionComInfos = employBiz.listPositionInfo(positionIds);
2022-05-18 09:19:50 +08:00
// 分页参数
2022-05-25 13:10:03 +08:00
PageInfo<TaxAgentManageRangeListDTO> dtoPage = SalaryPageUtil.buildPage(queryParam.getCurrent(), queryParam.getPageSize(), TaxAgentManageRangeListDTO.class);
2022-05-18 09:19:50 +08:00
// 查询人员状态
2022-05-24 09:14:26 +08:00
// List<HrmStatus> hrmStatusList = hrmCommonHrmStatusService.list();
List<HrmStatus> hrmStatusList = UserStatusEnum.getHrmStatusList();
2022-05-18 09:19:50 +08:00
// 薪资账套的人员范围po转换成列表dto
List<TaxAgentManageRangeListDTO> taxAgentManageRangeList = TaxAgentBO.convert2ListDTO(taxAgentManageRanges, employeeComInfos, departmentComInfos, subDepartmentComInfos, positionComInfos, hrmStatusList);
// 根据对象名称过滤
if (StringUtils.isNotEmpty(queryParam.getTargetName())) {
2022-12-07 20:15:52 +08:00
taxAgentManageRangeList = taxAgentManageRangeList.stream().filter(f -> f.getTargetName().contains(queryParam.getTargetName())).collect(Collectors.toList());
2022-05-18 09:19:50 +08:00
}
// 填充总数和当页数据
dtoPage.setTotal(taxAgentManageRangeList.size());
2022-06-01 15:18:10 +08:00
dtoPage.setList(SalaryPageUtil.subList(dtoPage.getPageNum(), dtoPage.getPageSize(), taxAgentManageRangeList));
2022-05-18 09:19:50 +08:00
return dtoPage;
}
/**
* 根据个税口角义务人id保存管理范围
*
2022-05-24 09:14:26 +08:00
* @param saveParam 保存参数
2022-05-18 09:19:50 +08:00
*/
@Override
2022-05-24 09:14:26 +08:00
public void save(TaxAgentRangeSaveParam saveParam) {
2022-06-10 14:21:21 +08:00
if (saveParam == null) {
throw new SalaryRunTimeException("参数错误");
}
if (Objects.isNull(saveParam.getTaxAgentId())) {
throw new SalaryRunTimeException("个税扣缴义务人的id不允许为空");
}
if (saveParam.getIncludeType() != 0 && saveParam.getIncludeType() != 1) {
throw new SalaryRunTimeException("只能选择 关联人员范围/从范围中排除");
}
if (CollectionUtils.isEmpty(saveParam.getEmployeeStatus())) {
throw new SalaryRunTimeException("员工状态不允许为空");
}
if (CollectionUtils.isNotEmpty(saveParam.getTargetParams())) {
saveParam.getTargetParams().forEach(target -> {
if (target.getTargetType() == null) {
throw new SalaryRunTimeException("对象类型不能为空");
}
if (target.getTargetId() == null) {
throw new SalaryRunTimeException("对象不能为空");
}
});
}
2022-07-11 09:39:57 +08:00
// 查询个税扣缴义务人
TaxAgentPO taxAgent = getTaxAgentService(user).getById(saveParam.getTaxAgentId());
2022-05-18 09:19:50 +08:00
if (Objects.isNull(taxAgent)) {
2022-06-21 22:37:22 +08:00
throw new SalaryRunTimeException(SalaryI18nUtil.getI18nLabel(108605, "参数错误,个税扣缴义务人不存在或不在权限范围内"));
2022-05-18 09:19:50 +08:00
}
// 查询已有的管理范围
2022-05-19 16:36:18 +08:00
List<TaxAgentManageRangePO> taxAgentManageAllRanges = listByTaxAgentId(saveParam.getTaxAgentId());
2022-05-18 09:19:50 +08:00
List<TaxAgentManageRangePO> taxAgentManageRanges = taxAgentManageAllRanges.stream().filter(f -> f.getIncludeType().equals(saveParam.getIncludeType())).collect(Collectors.toList());
// 处理一下本次的保存参数(如果原来添加过对应的人员(/部门/岗位),那么本次不需要新增,只需要更新)
2022-05-24 09:14:26 +08:00
TaxAgentBO.Result result = TaxAgentBO.handleTaxAgentRange(taxAgentManageRanges, saveParam, taxAgent.getId(), (long) user.getUID());
2022-05-18 09:19:50 +08:00
2022-07-11 09:39:57 +08:00
/* 检查当前个税扣缴义务人的所有人员范围与所有分管理员的管理范围===========================start */
2022-05-18 09:19:50 +08:00
List<TaxAgentManageRangePO> allRanges = Lists.newArrayList(taxAgentManageAllRanges);
allRanges.addAll(result.getNeedInsertTaxAgentManageRanges());
allRanges.addAll(result.getNeedUpdateTaxAgentManageRanges());
// 去重
2022-12-07 20:15:52 +08:00
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));
2022-11-22 15:37:07 +08:00
List<DataCollectionEmployee> salaryEmployees = getEmployMapper().listAll();
List<DataCollectionEmployee> allSalaryEmployees = this.getManageRangeSalaryEmployees(saveParam.getTaxAgentId(), allRanges, salaryEmployees);
2022-07-11 09:39:57 +08:00
/* 检查当前个税扣缴义务人的所有人员范围与所有分管理员的管理范围===========================end */
2022-05-18 09:19:50 +08:00
if (CollectionUtils.isNotEmpty(result.getNeedInsertTaxAgentManageRanges())) {
2022-05-24 09:14:26 +08:00
result.getNeedInsertTaxAgentManageRanges().forEach(range -> getTaxAgentManageRangeMapper().insertIgnoreNull(range));
2022-05-18 09:19:50 +08:00
}
if (CollectionUtils.isNotEmpty(result.getNeedUpdateTaxAgentManageRanges())) {
2023-01-11 09:47:57 +08:00
result.getNeedUpdateTaxAgentManageRanges().forEach(range -> getTaxAgentManageRangeMapper().updateIgnoreNull(range));
2022-05-18 09:19:50 +08:00
}
2022-07-11 09:39:57 +08:00
/* 同步本地人员范围的关联人员=========================== */
2022-12-07 20:15:52 +08:00
if (saveParam.isSync()) {
//同步执行
syncLocalEmp(saveParam.getTaxAgentId(), allSalaryEmployees, null);
} else {
taskExecutor.execute(() -> {
syncLocalEmp(saveParam.getTaxAgentId(), allSalaryEmployees, null);
});
}
2022-05-18 09:19:50 +08:00
}
2022-05-31 16:41:11 +08:00
private void syncLocalEmp(Long taxAgentId, List<DataCollectionEmployee> allSalaryEmployees, List<TaxAgentManageRangePO> allSubAdminRanges) {
2022-12-07 20:15:52 +08:00
syncLocalEmp(taxAgentId, allSalaryEmployees, allSubAdminRanges, (long) user.getUID());
//生成档案
2022-12-07 20:16:14 +08:00
getSalaryArchiveService(user).handleChangeData(1L);
2022-06-10 14:21:21 +08:00
2022-05-31 16:41:11 +08:00
}
2022-05-18 09:19:50 +08:00
/**
* 获取个税口角义务人的管理范围
*
* @param taxAgentId
* @return
*/
2022-05-19 16:36:18 +08:00
private List<TaxAgentManageRangePO> listByTaxAgentId(Long taxAgentId) {
2022-05-24 09:14:26 +08:00
return getTaxAgentManageRangeMapper().listSome(TaxAgentManageRangePO.builder().taxAgentId(taxAgentId).rangeType(TaxAgentRangeTypeEnum.TAXAGENT.getValue()).build());
2022-05-18 09:19:50 +08:00
}
2022-05-19 16:36:18 +08:00
private List<TaxAgentManageRangePO> listSunAdminRangeByTaxAgentId(Long taxAgentId) {
2022-05-24 09:14:26 +08:00
return getTaxAgentManageRangeMapper().listSome(TaxAgentManageRangePO.builder().taxAgentId(taxAgentId).rangeType(TaxAgentRangeTypeEnum.SUBADMIN.getValue()).build());
2022-05-18 09:19:50 +08:00
}
2022-05-31 16:41:11 +08:00
2022-05-18 09:19:50 +08:00
/**
* 根据分管理员id获取管理范围列表
*
* @param subAdminId
* @return
*/
2022-05-19 16:36:18 +08:00
private List<TaxAgentManageRangePO> listBySubAdminId(Long subAdminId) {
2022-05-24 09:14:26 +08:00
return getTaxAgentManageRangeMapper().listSome(TaxAgentManageRangePO.builder().taxAgentSubAdminId(subAdminId).rangeType(TaxAgentRangeTypeEnum.SUBADMIN.getValue()).build());
2022-05-18 09:19:50 +08:00
}
2022-05-19 16:36:18 +08:00
private List<TaxAgentManageRangePO> listByIds(Collection<Long> ids) {
2022-05-18 09:19:50 +08:00
if (CollectionUtils.isEmpty(ids)) {
return Collections.emptyList();
}
2022-05-24 09:14:26 +08:00
return getTaxAgentManageRangeMapper().listSome(TaxAgentManageRangePO.builder().ids(ids).build());
2022-05-18 09:19:50 +08:00
}
@Override
2022-05-24 09:14:26 +08:00
public void deleteByIds(Collection<Long> ids) {
2022-05-18 09:19:50 +08:00
// 查询管理范围
2022-05-19 16:36:18 +08:00
List<TaxAgentManageRangePO> taxAgentManageRanges = listByIds(ids);
2022-05-18 09:19:50 +08:00
if (CollectionUtils.isEmpty(taxAgentManageRanges)) {
throw new SalaryRunTimeException(SalaryI18nUtil.getI18nLabel(98604, "数据不存在或已被删除"));
}
List<Long> taxAgentIds = taxAgentManageRanges.stream().map(TaxAgentManageRangePO::getTaxAgentId).distinct().collect(Collectors.toList());
if (taxAgentIds.size() > 1) {
throw new SalaryRunTimeException(SalaryI18nUtil.getI18nLabel(110159, "一次只能删一个个税个税扣缴义务人的范围"));
}
Collection<Long> finalIds = ids;
2022-05-19 16:36:18 +08:00
taxAgentManageRanges = this.listByTaxAgentIds(taxAgentIds);
2022-05-18 09:19:50 +08:00
List<TaxAgentManageRangePO> allManageRanges = taxAgentManageRanges.stream().filter(f -> !finalIds.contains(f.getId())).collect(Collectors.toList());
List<TaxAgentManageRangePO> allRanges = allManageRanges.stream().filter(f -> f.getRangeType().equals(TaxAgentRangeTypeEnum.TAXAGENT.getValue())).collect(Collectors.toList());
List<TaxAgentManageRangePO> allSubAdminRanges = allManageRanges.stream().filter(f -> f.getRangeType().equals(TaxAgentRangeTypeEnum.SUBADMIN.getValue())).collect(Collectors.toList());
Long taxAgentId = taxAgentIds.get(0);
2022-11-22 15:37:07 +08:00
List<DataCollectionEmployee> salaryEmployees = getEmployMapper().listAll();
2022-11-22 15:53:05 +08:00
List<DataCollectionEmployee> allSalaryEmployees = this.getManageRangeSalaryEmployees(taxAgentId, allRanges, salaryEmployees);
2022-11-22 15:37:07 +08:00
2022-11-22 15:53:05 +08:00
List<DataCollectionEmployee> allSubAdminSalaryEmployees = this.getManageRangeSalaryEmployees(taxAgentId, allSubAdminRanges, salaryEmployees);
2022-05-18 09:19:50 +08:00
allSalaryEmployees.forEach(f -> {
allSubAdminSalaryEmployees.removeIf(a -> a.getEmployeeId().equals(f.getEmployeeId()));
});
if (CollectionUtils.isNotEmpty(allSubAdminSalaryEmployees)) {
2022-05-19 16:36:18 +08:00
throw new SalaryRunTimeException(SalaryI18nUtil.getI18nLabel(110160, "分管理员存在超出整体人员范围以外的人员,不可删除"));
2022-05-18 09:19:50 +08:00
}
// 删除管理范围
2022-05-24 09:14:26 +08:00
getTaxAgentManageRangeMapper().deleteByIds(ids);
2022-05-19 16:36:18 +08:00
2022-05-18 09:19:50 +08:00
/** 同步本地人员范围的关联人员=========================== */
2022-05-31 16:41:11 +08:00
syncLocalEmp(taxAgentId, allSalaryEmployees, allSubAdminRanges);
2022-05-18 09:19:50 +08:00
// 记录日志 todo
}
@Override
2022-05-24 09:14:26 +08:00
public void deleteByTaxAgentIds(Collection<Long> taxAgentIds) {
getTaxAgentManageRangeMapper().deleteByTaxAgentIds(taxAgentIds);
2022-05-18 09:19:50 +08:00
// 删除管理范围下的所有人员
2022-05-24 09:14:26 +08:00
getTaxAgentEmpService(user).deleteByTaxAgentIds(taxAgentIds);
2022-05-18 09:19:50 +08:00
}
/**
2022-05-24 09:14:26 +08:00
* 同步处理所有人员范围
2022-05-18 09:19:50 +08:00
*
2022-11-22 15:37:07 +08:00
* @param taxAgentIds 为空代表所有个税扣缴义务人
2022-05-18 09:19:50 +08:00
*/
2022-11-22 15:37:07 +08:00
private void handleSyncTaxAgentEmpData(List<Long> taxAgentIds) {
List<TaxAgentManageRangePO> allManageRanges = getTaxAgentManageRangeMapper().listSome(TaxAgentManageRangePO.builder().taxAgentIds(taxAgentIds).build());
2022-05-18 09:19:50 +08:00
if (CollectionUtils.isEmpty(allManageRanges)) {
return;
}
2022-11-22 15:37:07 +08:00
taxAgentIds = allManageRanges.stream().map(TaxAgentManageRangePO::getTaxAgentId).distinct().collect(Collectors.toList());
// 获取所有人员
List<DataCollectionEmployee> salaryEmployees = getEmployMapper().listAll();
2022-05-18 09:19:50 +08:00
List<TaxAgentEmpSaveParam> taxAgentEmpSaveParamList = Lists.newArrayList();
List<TaxAgentSubAdminEmpSaveParam> subAdminEmpSaveParamList = Lists.newArrayList();
2022-11-22 15:37:07 +08:00
Map<String, List<TaxAgentManageRangePO>> allRangeMap = SalaryEntityUtil.group2Map(allManageRanges, k -> k.getTaxAgentId() + "-" + k.getRangeType());
2022-05-18 09:19:50 +08:00
taxAgentIds.forEach(taxAgentId -> {
// 当前个税扣缴义务人的所有范围
2022-11-22 15:37:07 +08:00
List<TaxAgentManageRangePO> allRanges = Optional.ofNullable(allRangeMap.get(taxAgentId + "-" + TaxAgentRangeTypeEnum.TAXAGENT.getValue())).orElse(Collections.emptyList());
List<DataCollectionEmployee> allSalaryEmployees = this.getManageRangeSalaryEmployees(taxAgentId, allRanges, salaryEmployees);
2022-05-18 09:19:50 +08:00
taxAgentEmpSaveParamList.add(getTaxAgentEmpSyncParam(taxAgentId, allSalaryEmployees));
2022-11-22 15:37:07 +08:00
List<TaxAgentManageRangePO> allSubAdminRanges = Optional.ofNullable(allRangeMap.get(taxAgentId + "-" + TaxAgentRangeTypeEnum.SUBADMIN.getValue())).orElse(Collections.emptyList());
subAdminEmpSaveParamList.addAll(getTaxAgentSubAdminEmpSyncParam(taxAgentId, allSubAdminRanges, salaryEmployees));
2022-05-18 09:19:50 +08:00
});
Long employeeId = 0L;
// 同步管理员的人员
2022-05-24 09:14:26 +08:00
getTaxAgentEmpService(user).syncTaxAgentEmployee(taxAgentEmpSaveParamList, employeeId);
2022-05-18 09:19:50 +08:00
// 同步分管理员的人员
2022-11-22 15:37:07 +08:00
// taxAgentSubAdminEmployeeService.syncTaxAgentSubAdminEmployee(subAdminEmpSaveParamList, employeeId, tenantKey);
2022-12-07 20:15:52 +08:00
//生成档案
getSalaryArchiveService(user).handleChangeData(1L);
2022-05-18 09:19:50 +08:00
}
/**
* 获取个税扣缴义务人的同步参数
*
* @param taxAgentId
* @param allSalaryEmployees
* @return
*/
2022-05-19 16:36:18 +08:00
private TaxAgentEmpSaveParam getTaxAgentEmpSyncParam(Long taxAgentId, List<DataCollectionEmployee> allSalaryEmployees) {
2022-12-07 20:15:52 +08:00
return TaxAgentEmpSaveParam.builder().taxAgentId(taxAgentId).salaryEmployeeList(allSalaryEmployees).build();
2022-05-18 09:19:50 +08:00
}
/**
* 获取分管理员的同步参数
*
* @param taxAgentId
* @param allSubAdminRanges
2022-11-22 15:37:07 +08:00
* @param salaryEmployees
2022-05-18 09:19:50 +08:00
* @return
*/
2022-11-22 15:37:07 +08:00
private List<TaxAgentSubAdminEmpSaveParam> getTaxAgentSubAdminEmpSyncParam(Long taxAgentId, List<TaxAgentManageRangePO> allSubAdminRanges, List<DataCollectionEmployee> salaryEmployees) {
2022-05-18 09:19:50 +08:00
List<Long> allSubAdminIds = allSubAdminRanges.stream().map(TaxAgentManageRangePO::getTaxAgentSubAdminId).distinct().collect(Collectors.toList());
List<TaxAgentSubAdminEmpSaveParam> subAdminEmpSaveParamList = Lists.newArrayList();
allSubAdminIds.forEach(e -> {
List<TaxAgentManageRangePO> singSubAdminRanges = allSubAdminRanges.stream().filter(r -> e.equals(r.getTaxAgentSubAdminId())).collect(Collectors.toList());
2022-11-22 15:37:07 +08:00
List<DataCollectionEmployee> subAdminSalaryEmployees = this.getManageRangeSalaryEmployees(taxAgentId, singSubAdminRanges, salaryEmployees);
2022-05-18 09:19:50 +08:00
2022-12-07 20:15:52 +08:00
subAdminEmpSaveParamList.add(TaxAgentSubAdminEmpSaveParam.builder().taxAgentId(taxAgentId).subAdminId(e).salaryEmployeeList(subAdminSalaryEmployees).build());
2022-05-18 09:19:50 +08:00
});
return subAdminEmpSaveParamList;
}
/**
* 同步本地范围关联人员
*
* @param taxAgentId
* @param allSalaryEmployees 个税扣缴义务人下的所有人员
* @param allSubAdminRanges
* @param employeeId
*/
2022-05-19 16:36:18 +08:00
private void syncLocalEmp(Long taxAgentId, List<DataCollectionEmployee> allSalaryEmployees, List<TaxAgentManageRangePO> allSubAdminRanges, Long employeeId) {
2022-06-09 17:45:28 +08:00
log.info("同步个税扣缴人员范围");
try {
List<TaxAgentEmpSaveParam> taxAgentEmpSaveParamList = Collections.singletonList(getTaxAgentEmpSyncParam(taxAgentId, allSalaryEmployees));
// 同步个税扣缴义务人的人员
getTaxAgentEmpService(user).syncTaxAgentEmployee(taxAgentEmpSaveParamList, employeeId);
2022-05-18 09:19:50 +08:00
2022-05-31 16:41:11 +08:00
// List<TaxAgentSubAdminEmpSaveParam> subAdminEmpSaveParamList = getTaxAgentSubAdminEmpSyncParam(taxAgentId, allSubAdminRanges);
2022-06-09 17:45:28 +08:00
// 同步分管理员的人员
2022-05-31 16:41:11 +08:00
// taxAgentSubAdminEmployeeService.syncTaxAgentSubAdminEmployee(subAdminEmpSaveParamList, employeeId);
2022-06-09 17:45:28 +08:00
} catch (Exception e) {
log.error("同步个税扣缴人员范围异常", e);
}
2022-05-18 09:19:50 +08:00
}
2022-11-22 15:37:07 +08:00
/**
* 同步人员范围
*
* @param taxAgentIds
*/
@Override
2022-12-07 20:15:52 +08:00
public void syncManageRange(List<Long> taxAgentIds, String index) {
2022-11-22 15:37:07 +08:00
// 开始同步
LocalRunnable localRunnable = new LocalRunnable() {
@Override
public void execute() {
try {
Util_DataCache.setObjVal(index, "1");
handleSyncTaxAgentEmpData(taxAgentIds);
} finally {
Util_DataCache.clearVal(index);
}
}
};
ThreadPoolUtil.execute(localRunnable);
}
/**
* 导入数据
* @param taxAgentImportParam
* @return
*/
@Override
public Map<String, Object> importData(TaxAgentImportParam taxAgentImportParam) {
Boolean openDevolution = getTaxAgentService(user).isOpenDevolution();
long currentEmployeeId = user.getUID();
Map<String, Object> apidatas = new HashMap<String, Object>();
//查询对于人员信息导入筛选的全局配置
SalarySysConfPO salarySysConfPO = getSalarySysConfMapper().getOneByCode("matchEmployeeMode");
String confValue = (salarySysConfPO != null && salarySysConfPO.getConfValue() != null && !"".equals(salarySysConfPO.getConfValue())) ? salarySysConfPO.getConfValue() : "0";
//检验参数
checkImportParam(taxAgentImportParam);
//excel文件id
String imageId = Util.null2String(taxAgentImportParam.getImageId());
Validate.notBlank(imageId, "imageId为空");
//个税扣缴义务人
Long taxAgentId = taxAgentImportParam.getTaxAgentId();
TaxAgentPO taxAgent = getTaxAgentService(user).getById(taxAgentId);
InputStream fileInputStream = null;
try {
fileInputStream = ImageFileManager.getInputStreamById(Integer.parseInt(imageId));
List<TaxAgentManageRangeEmployeeListDTO> rangeEmployees = ExcelParseHelper.parse2Map(fileInputStream, TaxAgentManageRangeEmployeeListDTO.class, 0, 1, 4, "TaxAgentEmployee.xlsx");
int total = rangeEmployees.size();
int index = 0;
int successCount = 0;
int errorCount = 0;
//人员信息
List<DataCollectionEmployee> employees = employBiz.listEmployee();
// 错误excel内容
List<Map> errorData = new ArrayList<>();
//合规数据
List<TaxAgentManageRangeSaveParam.TaxAgentSubAdminRangeTargetParam> eligibleData = new ArrayList<>();
// 全部员工状态
Set<String> allEmployeeStatuses = new HashSet<>();
allEmployeeStatuses.addAll(SalaryEmployeeStatusEnum.getAllSalaryEmployeeStatusValues());
TaxAgentRangeSaveParam taxAgentRangeSaveParam = new TaxAgentRangeSaveParam();
taxAgentRangeSaveParam.setTaxAgentId(taxAgentId);
taxAgentRangeSaveParam.setIncludeType(1);
taxAgentRangeSaveParam.setSync(true);
taxAgentRangeSaveParam.setEmployeeStatus(allEmployeeStatuses);
for (int i = 0; i < rangeEmployees.size(); i++) {
TaxAgentManageRangeEmployeeListDTO dto = rangeEmployees.get(i);
//待插入数据库对象
TaxAgentManageRangeSaveParam.TaxAgentSubAdminRangeTargetParam po = new TaxAgentManageRangeSaveParam.TaxAgentSubAdminRangeTargetParam();
po.setTargetType(TargetTypeEnum.EMPLOYEE);
//异常点数量
int errorSum = 0;
//行号
String rowIndex = String.format("第%s行", i + 2);
//相同的姓名
String userName = dto.getUsername();
String deparmentName = dto.getDepartmentName();
String mobile = dto.getMobile();
String workcode = dto.getJobNum();
List<Long> employeeSameIds = new ArrayList<>();
//筛选导入人员信息可以在人力资源池中匹配到的人员信息
List<DataCollectionEmployee> emps = getSalaryEmployeeService()
.matchImportEmployee(employees, userName, deparmentName, mobile, workcode, null);
if (CollectionUtils.isNotEmpty(emps)) {
employeeSameIds = emps.stream()
.map(DataCollectionEmployee::getEmployeeId)
.collect(Collectors.toList());
}
2023-01-09 15:12:44 +08:00
//含在职和离职,选在职数据
if (CollectionUtils.isNotEmpty(emps) && emps.size() > 1) {
employeeSameIds = emps.stream()
.filter(e -> UserStatusEnum.getNormalStatus().contains(e.getStatus()))
.map(DataCollectionEmployee::getEmployeeId)
.collect(Collectors.toList());
}
if (CollectionUtils.isNotEmpty(emps) && emps.size() == 1) {
employeeSameIds = emps.stream()
.map(DataCollectionEmployee::getEmployeeId)
.collect(Collectors.toList());
}
//当人员信息导入筛选的全局配置为"0"时,姓名才是必填项
if (StringUtils.isBlank(userName) && "0".equals(confValue)) {
//姓名 不能为空
//错误消息对象
Map<String, String> errorMessageMap = Maps.newHashMap();
errorMessageMap.put("message", rowIndex + "姓名不能为空");
errorData.add(errorMessageMap);
errorSum += 1;
} else if (CollectionUtils.isEmpty(employeeSameIds) || employeeSameIds.size() > 1) {
Map<String, String> errorMessageMap = Maps.newHashMap();
errorMessageMap.put("message", rowIndex + "员工信息不存在或者存在多个员工");
errorData.add(errorMessageMap);
errorSum += 1;
} else {
Long employeeId = CollectionUtils.isNotEmpty(employeeSameIds) && employeeSameIds.size() == 1 ? employeeSameIds.get(0) : null;
if (employeeId != null && employeeId > 0) {
po.setTargetId(employeeId);
} else {
//姓名错误,系统内不存在该姓名
Map<String, String> errorMessageMap = new HashMap<>();
errorMessageMap.put("message", rowIndex + "姓名错误,系统内不存在该姓名");
errorData.add(errorMessageMap);
errorSum += 1;
}
}
if (Objects.isNull(taxAgent)) {
//个税扣缴义务人不存在
Map<String, String> errorMessageMap = new HashMap<>();
errorMessageMap.put("message","个税扣缴义务人不存在或不在权限范围内");
errorData.add(errorMessageMap);
errorSum += 1;
}
//fixme 分权判断
if (errorSum == 0) {
successCount += 1;
// 合格数据
eligibleData.add(po);
} else {
errorCount += 1;
// 添加错误数据
}
}
//入库-----------------start
2023-01-11 09:47:57 +08:00
eligibleData = handleImportData(eligibleData);
taxAgentRangeSaveParam.setTargetParams(eligibleData);
// 查询已有的管理范围
List<TaxAgentManageRangePO> taxAgentManageAllRanges = listByTaxAgentId(taxAgentId);
List<TaxAgentManageRangePO> taxAgentManageRanges = taxAgentManageAllRanges.stream().filter(f -> f.getIncludeType().equals(taxAgentRangeSaveParam.getIncludeType())).collect(Collectors.toList());
// 处理一下本次的保存参数(如果原来添加过对应的人员(/部门/岗位),那么本次不需要新增,只需要更新)
TaxAgentBO.Result result = TaxAgentBO.handleTaxAgentRange(taxAgentManageRanges, taxAgentRangeSaveParam, taxAgent.getId(), (long) user.getUID());
/* 检查当前个税扣缴义务人的所有人员范围与所有分管理员的管理范围===========================start */
List<TaxAgentManageRangePO> allRanges = Lists.newArrayList(taxAgentManageAllRanges);
allRanges.addAll(result.getNeedInsertTaxAgentManageRanges());
allRanges.addAll(result.getNeedUpdateTaxAgentManageRanges());
// 去重
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<DataCollectionEmployee> salaryEmployees = getEmployMapper().listAll();
List<DataCollectionEmployee> allSalaryEmployees = this.getManageRangeSalaryEmployees(taxAgentId, allRanges, salaryEmployees);
/* 检查当前个税扣缴义务人的所有人员范围与所有分管理员的管理范围===========================end */
if (CollectionUtils.isNotEmpty(result.getNeedInsertTaxAgentManageRanges())) {
result.getNeedInsertTaxAgentManageRanges().forEach(range -> getTaxAgentManageRangeMapper().insertIgnoreNull(range));
}
if (CollectionUtils.isNotEmpty(result.getNeedUpdateTaxAgentManageRanges())) {
result.getNeedInsertTaxAgentManageRanges().forEach(range -> getTaxAgentManageRangeMapper().updateIgnoreNull(range));
}
/* 同步本地人员范围的关联人员=========================== */
if (taxAgentRangeSaveParam.isSync()) {
//同步执行
syncLocalEmp(taxAgentId, allSalaryEmployees, null);
} else {
taskExecutor.execute(() -> {
syncLocalEmp(taxAgentId, allSalaryEmployees, null);
});
}
//-----------------end
apidatas.put("successCount", successCount);
apidatas.put("errorCount", errorCount);
apidatas.put("errorData", errorData);
} finally {
IOUtils.closeQuietly(fileInputStream);
}
return apidatas;
}
2023-01-11 09:47:57 +08:00
// 处理导入数据
private List<TaxAgentManageRangeSaveParam.TaxAgentSubAdminRangeTargetParam> handleImportData(List<TaxAgentManageRangeSaveParam.TaxAgentSubAdminRangeTargetParam> pos) {
if (CollectionUtils.isEmpty(pos)) {
return Collections.emptyList();
}
// 多条相同人的则以第一条为准如果逆序排列用于重复的则以最后一条为准Collections.reverse(pos);
// 去重(通过记录的唯一条件(人员id
List<TaxAgentManageRangeSaveParam.TaxAgentSubAdminRangeTargetParam> finalPos = pos.stream().collect(Collectors.collectingAndThen(Collectors.toCollection(() -> new TreeSet<>(Comparator.comparing(f -> f.getTargetId() ))), ArrayList::new));
return finalPos;
}
private void checkImportParam(TaxAgentImportParam importParam) {
//excel文件id
String imageId = Util.null2String(importParam.getImageId());
if (StringUtils.isBlank(imageId)) {
throw new SalaryRunTimeException("文件不存在");
}
Long taxAgentId = importParam.getTaxAgentId();
if(Objects.isNull(taxAgentId)){
throw new SalaryRunTimeException("个税扣缴义务人为空");
}
}
2022-05-18 09:19:50 +08:00
}