package com.engine.salary.service.impl;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.extension.conditions.query.LambdaQueryChainWrapper;
import com.baomidou.mybatisplus.extension.conditions.update.LambdaUpdateChainWrapper;
import com.engine.core.impl.Service;
import com.engine.salary.entity.datacollection.DataCollectionEmployee;
import com.engine.salary.entity.taxagent.bo.TaxAgentBO;
import com.engine.salary.entity.taxagent.dto.TaxAgentManageRangeEmployeeDTO;
import com.engine.salary.entity.taxagent.dto.TaxAgentManageRangeListDTO;
import com.engine.salary.entity.taxagent.param.*;
import com.engine.salary.entity.taxagent.po.TaxAgentManageRangePO;
import com.engine.salary.entity.taxagent.po.TaxAgentPO;
import com.engine.salary.entity.taxagent.po.TaxAgentSubAdminPO;
import com.engine.salary.enums.SalaryJobFlagEnum;
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.exception.SalaryRunTimeException;
import com.engine.salary.mapper.datacollection.EmployMapper;
import com.engine.salary.mapper.taxagent.TaxAgentManageRangeMapper;
import com.engine.salary.service.*;
import com.engine.salary.util.JsonUtil;
import com.engine.salary.util.SalaryEntityUtil;
import com.engine.salary.util.SalaryI18nUtil;
import com.engine.salary.util.page.PageInfo;
import com.engine.salary.util.page.PageUtil;
import com.google.common.collect.Lists;
import com.weaver.common.async.bean.AsyncBean;
import com.weaver.common.async.consumer.anno.method.AsyncListener;
import com.weaver.common.cache.tablecache.impl.ComInfoCache;
import com.weaver.common.hrm.cache.HrmDepartmentComInfo;
import com.weaver.common.hrm.cache.HrmEmployeeComInfo;
import com.weaver.common.hrm.cache.HrmPositionComInfo;
import com.weaver.common.hrm.domain.employee.HrmStatus;
import com.weaver.common.hrm.domain.queue.HrmCommonQueue;
import com.weaver.common.hrm.service.employee.HrmCommonHrmStatusService;
import com.weaver.datasecurity.interceptor.DSTenantKeyThreadVar;
import com.weaver.hrm.salary.common.SalaryEmployee;
import com.weaver.hrm.salary.entity.taxagent.param.*;
import com.weaver.hrm.salary.service.*;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.collections4.CollectionUtils;
import org.apache.commons.lang3.StringUtils;
import org.apache.commons.lang3.math.NumberUtils;
import org.springframework.transaction.annotation.Transactional;
import java.util.*;
import java.util.concurrent.ExecutorService;
import java.util.stream.Collectors;
/**
* 个税扣缴义务人的管理范围
*
Copyright: Copyright (c) 2022
* Company: 泛微软件
*
* @author qiantao
* @version 1.0
**/
@Slf4j
public class TaxAgentManageRangeServiceImpl extends Service implements TaxAgentManageRangeService {
private TaxAgentManageRangeMapper taxAgentManageRangeMapper;
private TaxAgentV2Service taxAgentService;
private TaxAgentSubAdminService taxAgentSubAdminService;
private TaxAgentEmpService taxAgentEmployeeService;
private TaxAgentSubAdminEmpService taxAgentSubAdminEmployeeService;
private ComInfoCache comInfoCache;
private EmployMapper employeeMapper;
private ExecutorService taskExecutor;
private HrmCommonHrmStatusService hrmCommonHrmStatusService;
private List listByTaxAgentIds(List taxAgentIds) {
if (CollectionUtils.isEmpty(taxAgentIds)) {
return Lists.newArrayList();
}
return taxAgentManageRangeMapper.listSome(TaxAgentManageRangePO.builder().rangeType(TaxAgentRangeTypeEnum.TAXAGENT.getValue()).taxAgentIds(taxAgentIds).build());
}
@Override
public List listSalaryEmployeeByTaxAgentIds(List taxAgentIds) {
return listSalaryEmployeeByTaxAgentIds(null, taxAgentIds);
}
@Override
public List listSalaryEmployeeByTaxAgentIds(SalaryEmployeeStatusEnum employeeStatus, List taxAgentIds) {
List allTaxAgentManageRanges = this.listByTaxAgentIds(taxAgentIds);
return convertTaxAgentEmployee(employeeStatus, taxAgentIds, allTaxAgentManageRanges);
}
@Override
public List listBySubAdminIds(Collection taxAgentSubAdminIds) {
if (CollectionUtils.isEmpty(taxAgentSubAdminIds)) {
return Lists.newArrayList();
}
taxAgentManageRangeMapper.listSome(TaxAgentManageRangePO.builder().rangeType(TaxAgentRangeTypeEnum.SUBADMIN.getValue()).build())
return new LambdaQueryChainWrapper<>(taxAgentManageRangeMapper)
.eq(TaxAgentManageRangePO::getTenantKey)
.eq(TaxAgentManageRangePO::getDeleteType, 0)
.eq(TaxAgentManageRangePO::getRangeType, TaxAgentRangeTypeEnum.SUBADMIN.getValue())
.in(TaxAgentManageRangePO::getTaxAgentSubAdminId, taxAgentSubAdminIds)
.orderByDesc(TaxAgentManageRangePO::getId)
.list();
}
@Override
public List listSalaryEmployeeBySubAdminIds(List taxAgentIds) {
return listSalaryEmployeeBySubAdminIds(null, taxAgentIds);
}
@Override
public List listSalaryEmployeeBySubAdminIds(SalaryEmployeeStatusEnum employeeStatus, List taxAgentSubAdminIds) {
List allTaxAgentManageRanges = this.listBySubAdminIds(taxAgentSubAdminIds);
List taxAgentIds = allTaxAgentManageRanges.stream().map(TaxAgentManageRangePO::getTaxAgentId).distinct().collect(Collectors.toList());
return convertTaxAgentEmployee(employeeStatus, taxAgentIds, allTaxAgentManageRanges);
}
private List convertTaxAgentEmployee(SalaryEmployeeStatusEnum employeeStatus, List taxAgentIds, List allTaxAgentManageRanges) {
return taxAgentIds.stream().distinct().map(e -> {
// 获取范围下的人员
List salaryEmployees = getManageRangeSalaryEmployees(employeeStatus, e, allTaxAgentManageRanges);
if (CollectionUtils.isEmpty(salaryEmployees)) {
return null;
}
List taxAgentEmployees = salaryEmployees.stream().map(m -> {
TaxAgentManageRangeEmployeeDTO.TaxAgentEmployee taxAgentEmployee = new TaxAgentManageRangeEmployeeDTO.TaxAgentEmployee();
taxAgentEmployee.setEmployeeId(m.getEmployeeId());
taxAgentEmployee.setUsername(m.getUsername());
return taxAgentEmployee;
}).collect(Collectors.toList());
return TaxAgentManageRangeEmployeeDTO.builder()
.taxAgentId(e)
.taxAgentName("")
.employeeList(taxAgentEmployees)
.build();
}).filter(Objects::nonNull).collect(Collectors.toList());
}
/**
* 获取范围下的人员
*
* @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) {
// 查询人员状态
List hrmStatusList = hrmCommonHrmStatusService.list();
if (employeeStatus.equals(SalaryEmployeeStatusEnum.NORMAL)) {
personnelStatuss = hrmStatusList.stream().filter(f -> f.getJobflag().equals(SalaryJobFlagEnum.normal.getValue())).map(m -> m.getCodeId() + "").collect(Collectors.toList());
} else if (employeeStatus.equals(SalaryEmployeeStatusEnum.UNAVAILABLE)) {
personnelStatuss = hrmStatusList.stream().filter(f -> f.getJobflag().equals(SalaryJobFlagEnum.unavailable.getValue())).map(m -> m.getCodeId() + "").collect(Collectors.toList());
}
}
// 根据上一步的查询参数查询人员
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 = employeeMapper.listAll();
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.getPersonnelStatus())) {
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.getDepartmentId())) {
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;
}
// /**
// * 根据分管理员id获取管理范围列表
// *
// * @param subAdminId
// * @param includeType
// * @return
// */
// private List listBySubAdminIdAndIncludeType(Long subAdminId, Integer includeType) {
//
// return new LambdaQueryChainWrapper<>(taxAgentManageRangeMapper)
// .eq(TaxAgentManageRangePO::getTenantKey)
// .eq(TaxAgentManageRangePO::getDeleteType, 0)
// .eq(TaxAgentManageRangePO::getTaxAgentSubAdminId, subAdminId)
// .eq(TaxAgentManageRangePO::getRangeType, TaxAgentRangeTypeEnum.SUBADMIN.getValue())
// .eq(TaxAgentManageRangePO::getIncludeType, includeType)
// .orderByDesc(TaxAgentManageRangePO::getId)
// .list();
// }
/**
* 获取个税口角义务人的管理范围
*
* @param taxAgentId
* @param includeType
* @return
*/
private List listByTaxAgentIdAndIncludeType(Long taxAgentId, Integer includeType) {
return taxAgentManageRangeMapper.listSome(TaxAgentManageRangePO.builder().taxAgentId(taxAgentId).rangeType(TaxAgentRangeTypeEnum.TAXAGENT.getValue()).includeType(includeType).build());
}
// @Override
// public Page listPageByParamAndIncludeType(TaxAgentSubAdminRangeQueryParam queryParam, Integer includeType) {
// // 查询已有的管理范围
// List taxAgentManageRanges = listBySubAdminIdAndIncludeType(queryParam.getSubAdminId(), includeType);
// return listPageByParamAndIncludeType(taxAgentManageRanges, queryParam, includeType);
// }
@Override
public PageInfo listPageByParamAndIncludeType(TaxAgentRangeQueryParam queryParam, Integer includeType) {
// 查询已有的管理范围
List taxAgentManageRanges = listByTaxAgentIdAndIncludeType(queryParam.getTaxAgentId(), includeType);
return listPageByParamAndIncludeType(taxAgentManageRanges, queryParam, includeType);
}
private PageInfo listPageByParamAndIncludeType(List taxAgentManageRanges, TaxAgentManageRangeQueryParam queryParam, Integer includeType) {
// 查询人员信息
List