package com.engine.salary.service.impl; import com.engine.core.impl.Service; import com.engine.salary.biz.TaxAgentBiz; import com.engine.salary.cmd.TaxAgent.*; import com.engine.salary.constant.TaxAgentPermissionConstant; import com.engine.salary.entity.datacollection.AddUpDeduction; import com.engine.salary.entity.datacollection.AddUpSituation; import com.engine.salary.entity.datacollection.po.OtherDeductionPO; import com.engine.salary.entity.salaryarchive.po.SalaryArchiveTaxAgentPO; import com.engine.salary.entity.taxagent.po.TaxAgentAdminPO; import com.engine.salary.entity.taxagent.po.TaxAgentPO; import com.engine.salary.entity.taxrate.TaxAgent; import com.engine.salary.entity.taxrate.param.TaxAgentQueryParam; import com.engine.salary.enums.taxagent.TaxAgentRoleTypeEnum; import com.engine.salary.exception.SalaryRunTimeException; import com.engine.salary.mapper.TaxAgentMapper; import com.engine.salary.mapper.archive.SalaryArchiveMapper; import com.engine.salary.mapper.archive.SalaryArchiveTaxAgentMapper; import com.engine.salary.mapper.datacollection.AddUpDeductionMapper; import com.engine.salary.mapper.datacollection.AddUpSituationMapper; import com.engine.salary.mapper.datacollection.OtherDeductionMapper; import com.engine.salary.service.*; import com.engine.salary.util.SalaryI18nUtil; import com.engine.salary.util.db.MapperProxyFactory; import org.apache.commons.collections4.CollectionUtils; import org.apache.commons.lang3.StringUtils; import org.springframework.beans.BeanUtils; import org.springframework.transaction.annotation.Transactional; import java.time.LocalDateTime; import java.util.*; import java.util.stream.Collectors; public class TaxAgentServiceImpl extends Service implements TaxAgentService { private SalaryArchiveTaxAgentMapper getSalaryArchiveTaxAgentMapper() { return MapperProxyFactory.getProxy(SalaryArchiveTaxAgentMapper.class); } private AddUpDeductionMapper getAddUpDeductionMapper() { return MapperProxyFactory.getProxy(AddUpDeductionMapper.class); } private OtherDeductionMapper getOtherDeductionMapper() { return MapperProxyFactory.getProxy(OtherDeductionMapper.class); } private AddUpSituationMapper getAddUpSituationMapper() { return MapperProxyFactory.getProxy(AddUpSituationMapper.class); } private TaxAgentMapper getTaxAgentMapper() { return MapperProxyFactory.getProxy(TaxAgentMapper.class); } @Override public Map list(Map params) { return commandExecutor.execute(new TaxAgentListCmd(params, user)); } @Override public Map getForm(Map params) { return commandExecutor.execute(new TaxAgentGetFromCmd(params, user)); } @Override public Map save(Map params) { return commandExecutor.execute(new TaxAgentSaveCmd(params, user)); } @Override public Map update(Map params) { return commandExecutor.execute(new TaxAgentUpdateCmd(params, user)); } @Override public String delete(Collection ids) { List taxAgents = getTaxAgentMapper().listBySome(TaxAgentQueryParam.builder().ids(ids).build()); if (CollectionUtils.isEmpty(taxAgents)) { throw new SalaryRunTimeException(SalaryI18nUtil.getI18nLabel(85382, "要删除的个税扣缴义务人在不存在或已删除")); } // 正在使用的记录不允许删除 boolean checkUsed = checkUsed(ids); if (checkUsed) { throw new SalaryRunTimeException(SalaryI18nUtil.getI18nLabel(100570, "正在使用的记录不允许删除")); } getTaxAgentMapper().deleteByIds(ids); // // 记录日志 // taxAgents.forEach(e -> SalaryLoggerUtil.recordDeleteSingleLog(taxAgentLoggerTemplate, // e.getId(), // e.getName(), // SalaryI18nUtil.getI18nLabel(tenantKey, employeeId, 100546, "删除个税扣缴义务人"), // SalaryI18nUtil.getI18nLabel(tenantKey, employeeId, 100546, "删除个税扣缴义务人") + ":" + e.getName(), // e)); return StringUtils.EMPTY; } /** * 检查正在被使用的记录 * * @param ids * @return */ private boolean checkUsed(Collection ids) { // 被薪资档案引用 List salaryArchiveTaxAgentList = getSalaryArchiveTaxAgentMapper().listSome(SalaryArchiveTaxAgentPO.builder().taxAgentIds(ids).build()); if (CollectionUtils.isNotEmpty(salaryArchiveTaxAgentList)) { return Boolean.TRUE; } // 被累计专项附加扣除引用 List addUpDeductionList = getAddUpDeductionMapper().listSome(AddUpDeduction.builder().taxAgentIds(ids).build()); if (CollectionUtils.isNotEmpty(addUpDeductionList)) { return Boolean.TRUE; } // 被其他免税扣除引用 List otherDeductionList = getOtherDeductionMapper().listSome(OtherDeductionPO.builder().taxAgentIds(ids).build()); if (CollectionUtils.isNotEmpty(otherDeductionList)) { return Boolean.TRUE; } // 被往期累计情况引用 List addUpSituationList = getAddUpSituationMapper().listSome(AddUpSituation.builder().taxAgentIds(ids).build()); if (CollectionUtils.isNotEmpty(addUpSituationList)) { return Boolean.TRUE; } return Boolean.FALSE; } @Override public Map selectList(Map params) { return commandExecutor.execute(new TaxAgentSelectListCmd(params, user)); } @Override public Collection findAll() { return new TaxAgentBiz().listAll(); } @Override public TaxAgent getById(Long id) { return getTaxAgentMapper().getById(id); } private TaxAgentMapper taxAgentMapper; // // private ExtEmployeeService extEmployeeService; private TaxAgentBaseService taxAgentBaseService; private TaxAgentAdminService taxAgentAdminService; // private TaxAgentSubAdminService taxAgentSubAdminService; // private TaxAgentManageRangeService taxAgentManageRangeService; private TaxAgentEmpService taxAgentEmpService; // private TaxAgentSubAdminEmpService taxAgentSubAdminEmpService; private SalaryArchiveMapper salaryArchiveMapper; private AddUpDeductionMapper addUpDeductionMapper; private OtherDeductionMapper otherDeductionMapper; private AddUpSituationMapper addUpSituationMapper; // private PaymentAgencyMapper paymentAgencyMapper; // private SIArchivesSocialMapper siArchivesSocialMapper; // private SIArchivesFundMapper siArchivesFundMapper; // private SIArchivesOtherMapper siArchivesOtherMapper; // private AuthorityService authorityService; private SalaryAcctRecordService salaryAcctRecordService; private SIAccountService siAccountService; private HrmCommonHrmStatusService hrmCommonHrmStatusService; @Override public TaxAgentRoleTypeEnum getRoleType(Long currentEmployeeId) { TaxAgentRoleTypeEnum roleType = TaxAgentRoleTypeEnum.NONE; if (isChief(currentEmployeeId)) { return TaxAgentRoleTypeEnum.CHIEF; } // 是否开启分权 boolean isOpenDevolution = taxAgentBaseService.isOpenDevolution(); if (!isOpenDevolution) { List taxAgentAdmins = taxAgentAdminService.listByEmployeeId(currentEmployeeId, currentTenantKey); if (CollectionUtils.isNotEmpty(taxAgentAdmins)) { return TaxAgentRoleTypeEnum.ADMIN; } List taxAgentSubAdmins = taxAgentSubAdminService.listByAndEmployeeId(currentEmployeeId, currentTenantKey); if (CollectionUtils.isNotEmpty(taxAgentSubAdmins)) { return TaxAgentRoleTypeEnum.SUBADMIN; } } return roleType; } @Override public Boolean isChief(Long currentEmployeeId) { return currentEmployeeId == 1; //return authorityService.checkPermissionSet(TaxAgentPermissionConstant.MENU_CODE, TaxAgentPermissionConstant.ITEM_CODE_DEVOLUTION, currentEmployeeId); } @Override public Boolean isDefaultOpen(Long currentEmployeeId, String currentTenantKey) { return authorityService.checkPermissionSet(TaxAgentPermissionConstant.MENU_CODE, TaxAgentPermissionConstant.ITEM_CODE_DEFAULT, currentEmployeeId); } @Override public Boolean isAdminEnable(Long currentEmployeeId, String currentTenantKey) { // 如果不是超管,是管理员 if (!isChief(currentEmployeeId)) { // 没有开启分权 if (!isOpenDevolution(currentTenantKey)) { return Boolean.FALSE; } else { // 自己有没有被分配为管理员 List taxAgentAdminList = taxAgentAdminService.listByEmployeeId(currentEmployeeId); return CollectionUtils.isNotEmpty(taxAgentAdminList); } } return Boolean.TRUE; } @Override public Page listPage(Page page, TaxAgentQueryParam queryParam, String currentTenantKey) { taxAgentMapper.list(page, queryParam, currentTenantKey); return page; } @Override public List list(TaxAgentQueryParam queryParam, String currentTenantKey) { return taxAgentMapper.list(queryParam, currentTenantKey); } @Override public List listByIds(Collection ids, String tenantKey) { return new LambdaQueryChainWrapper<>(taxAgentMapper) .eq(TaxAgentPO::getTenantKey, tenantKey) .eq(TaxAgentPO::getDeleteType, 0) .in(TaxAgentPO::getId, ids) .list(); } @Override public List listAll(String tenantKey) { return new LambdaQueryChainWrapper<>(taxAgentMapper) .eq(TaxAgentPO::getTenantKey, tenantKey) .eq(TaxAgentPO::getDeleteType, 0) .list(); } @Override public TaxAgentPO getById(Long id, String tenantKey) { return new LambdaQueryChainWrapper<>(taxAgentMapper) .eq(TaxAgentPO::getTenantKey, tenantKey) .eq(TaxAgentPO::getDeleteType, 0) .eq(TaxAgentPO::getId, id) .one(); } @Override public Collection findAll(String tenantKey) { List taxAgents = taxAgentMapper.list(TaxAgentQueryParam.builder().build(), tenantKey); return TaxAgentBO.convertToListDTO(taxAgents, tenantKey); } @Override public Collection listAllTaxAgents(Long employeeId, String tenantKey) { List taxAgents = taxAgentMapper.list(TaxAgentQueryParam.builder().build(), tenantKey); return handleForDevolution(taxAgents, employeeId, tenantKey); } @Override public Collection listAllTaxAgentsAsAdmin(Long employeeId, String tenantKey) { List taxAgents = taxAgentMapper.list(TaxAgentQueryParam.builder().build(), tenantKey); // 是否开启分权 boolean isOpenDevolution = taxAgentBaseService.isOpenDevolution(tenantKey); if (!isOpenDevolution) { return taxAgents; } List taxAgentIds = taxAgents.stream().map(TaxAgentPO::getId).collect(Collectors.toList()); List enableTaxAgentIds = Lists.newArrayList(); // 1.判断自己是否是管理员, 如果是管理员,就是能够查看所属个税扣缴义务人 List taxAgentAdminList = taxAgentAdminService.listByTaxAgentIds(taxAgentIds, tenantKey); // 是管理员的列表 List adminTaxAgentIds = taxAgentAdminList.stream().filter(f -> f.getEmployeeId().equals(employeeId)).map(TaxAgentAdminPO::getTaxAgentId).collect(Collectors.toList()); enableTaxAgentIds.addAll(adminTaxAgentIds); return taxAgents.stream().filter(f -> enableTaxAgentIds.contains(f.getId())).collect(Collectors.toList()); } @Override public Collection listAllTaxAgentsAsRange(List employeeIds, String tenantKey) { List taxAgents = taxAgentMapper.list(TaxAgentQueryParam.builder().build(), tenantKey); List taxAgentIds = taxAgents.stream().map(TaxAgentPO::getId).collect(Collectors.toList()); List taxAgentEmployees = taxAgentEmpService.listByTaxAgentIds(taxAgentIds, tenantKey); if (CollectionUtils.isEmpty(taxAgentEmployees)) { return Lists.newArrayList(); } // 转换数据结构 List allemployeeIds = taxAgentEmployees.stream().map(TaxAgentEmpPO::getEmployeeId).distinct().collect(Collectors.toList()); return allemployeeIds.stream().map(m -> { List enableTaxAgentIds = taxAgentEmployees.stream().filter(f -> f.getEmployeeId().equals(m)).map(TaxAgentEmpPO::getTaxAgentId).distinct() .collect(Collectors.toList()); return TaxAgentEmployeeTaxAgentDTO.builder() .employeeId(m) .taxAgentIds(enableTaxAgentIds) .build(); }).collect(Collectors.toList()); } @Override @Transactional(rollbackFor = Exception.class) public String save(TaxAgentSaveParam saveParam, Long employeeId, String tenantKey) { // 是否开启分权 boolean isOpenDevolution = taxAgentBaseService.isOpenDevolution(tenantKey); if (isOpenDevolution && saveParam.getAdminUserId() == null) { throw new SalaryRunTimeException(SalaryI18nUtil.getI18nLabel(tenantKey, employeeId, 106271, "请选择管理员")); } QueryWrapper queryWrapper = new QueryWrapper<>(); queryWrapper.eq("tenant_key", tenantKey) .eq("delete_type", 0) .eq("name", saveParam.getName()); List taxAgents = taxAgentMapper.selectList(queryWrapper); if (CollectionUtils.isNotEmpty(taxAgents)) { throw new SalaryRunTimeException(SalaryI18nUtil.getI18nLabel(tenantKey, employeeId, 98080, "名称不允许重复")); } PaymentAgencyPO paymentAgencyPO = new LambdaQueryChainWrapper<>(paymentAgencyMapper) .eq(PaymentAgencyPO::getTenantKey, tenantKey) .eq(PaymentAgencyPO::getDeleteType, DeleteTypeEnum.NOT_DELETED.getValue()) .eq(PaymentAgencyPO::getAgencyName, saveParam.getName()) .one(); if (paymentAgencyPO != null) { throw new SalaryRunTimeException(SalaryI18nUtil.getI18nLabel(tenantKey, employeeId, 115130, "名称不允许与社保福利代缴机构名称重复")); } // 保存 saveParam.setId(IdGenerator.generate()); TaxAgentPO taxAgent = TaxAgentBO.convertToPO(saveParam, employeeId, tenantKey); taxAgentMapper.insert(taxAgent); if (isOpenDevolution) { taxAgentAdminService.batchInsert(saveParam.getId(), Collections.singletonList(saveParam.getAdminUserId()), employeeId, tenantKey); } // 记录日志 SalaryLoggerUtil.recordAddSingleLog(taxAgentLoggerTemplate, taxAgent.getId(), taxAgent.getName(), SalaryI18nUtil.getI18nLabel(tenantKey, employeeId, 93766, "新增个税扣缴义务人"), SalaryI18nUtil.getI18nLabel(tenantKey, employeeId, 93766, "新增个税扣缴义务人"), taxAgent); return StringUtils.EMPTY + taxAgent.getId(); } @Override public String paymentAgencyUpdate(TaxAgentPO taxAgentPO, Long employeeId, String tenantKey) { taxAgentMapper.updateById(taxAgentPO); return ""; } @Override @Transactional(rollbackFor = Exception.class) public String update(TaxAgentSaveParam saveParam, Long employeeId, String tenantKey) { TaxAgentPO taxAgent = getById(saveParam.getId(), tenantKey); if (taxAgent == null) { throw new SalaryRunTimeException(SalaryI18nUtil.getI18nLabel(tenantKey, employeeId, 100545, "个税扣缴义务人不存在")); } // 是否开启分权 boolean isOpenDevolution = taxAgentBaseService.isOpenDevolution(tenantKey); if (isOpenDevolution && saveParam.getAdminUserId() == null) { throw new SalaryRunTimeException(SalaryI18nUtil.getI18nLabel(tenantKey, employeeId, 106271, "请选择管理员")); } QueryWrapper queryWrapper = new QueryWrapper<>(); queryWrapper.eq("tenant_key", tenantKey) .eq("delete_type", 0) .eq("name", saveParam.getName()); List individualTaxWithholdingAgents = taxAgentMapper.selectList(queryWrapper); boolean nameExist = CollectionUtils.emptyIfNull(individualTaxWithholdingAgents).stream().anyMatch(e -> !Objects.equals(e.getId(), saveParam.getId())); if (nameExist) { throw new SalaryRunTimeException(SalaryI18nUtil.getI18nLabel(tenantKey, employeeId, 98080, "名称不允许重复")); } TaxAgentPO taxAgentNew = new TaxAgentPO(); BeanUtils.copyProperties(taxAgent, taxAgentNew); BeanUtils.copyProperties(saveParam, taxAgentNew); taxAgentNew.setUpdateTime(LocalDateTime.now()); taxAgentMapper.updateById(taxAgentNew); if (isOpenDevolution) { Boolean isUnEnableChange = adminChangeCheck(TaxAgentAdminChangeCheckParam.builder() .taxAgentId(saveParam.getId()) .adminUserId(saveParam.getAdminUserId()) .build(), employeeId, tenantKey); if (isUnEnableChange) { throw new SalaryRunTimeException(SalaryI18nUtil.getI18nLabel(tenantKey, employeeId, 115937, "该管理员有未归档核算数据,不可更换管理员")); } // 删除管理员 taxAgentAdminService.deleteByTaxAgentIds(Collections.singletonList(taxAgent.getId()), tenantKey); // 新增管理员 taxAgentAdminService.batchInsert(saveParam.getId(), Collections.singletonList(saveParam.getAdminUserId()), employeeId, tenantKey); } // 记录日志 SalaryLoggerUtil.recordUpdateSingleLog(taxAgentLoggerTemplate, taxAgent.getId(), taxAgentNew.getName(), SalaryI18nUtil.getI18nLabel(tenantKey, employeeId, 93767, "编辑个税扣缴义务人"), SalaryI18nUtil.getI18nLabel(tenantKey, employeeId, 93767, "编辑个税扣缴义务人"), taxAgent, taxAgentNew); return StringUtils.EMPTY; } @Override public Boolean adminChangeCheck(TaxAgentAdminChangeCheckParam checkParam, Long currentEmployeeId, String currentTenantKey) { List taxAgentAdminList = taxAgentAdminService.listByTaxAgentIds(Collections.singletonList(checkParam.getTaxAgentId()), currentTenantKey); Long adminUserId = CollectionUtils.isNotEmpty(taxAgentAdminList) ? taxAgentAdminList.get(0).getEmployeeId() : 0L; boolean isChanged = checkParam.getAdminUserId() != null && !adminUserId.equals(checkParam.getAdminUserId()); // 更换了管理员 if (isChanged) { // 检查是否核算 // 1.社保福利档案是否有核算未归档 Boolean checkedValue = siAccountService.changeAdminUnfiledCheck(adminUserId, currentTenantKey); // 2.薪资核算是否有核算未归档 List salaryAcctRecords = salaryAcctRecordService.listByStatusAndEmployeeId(SalaryAcctRecordStatusEnum.NOT_ARCHIVED, adminUserId, currentTenantKey); if (CollectionUtils.isNotEmpty(salaryAcctRecords) || checkedValue) { return Boolean.TRUE; } } return Boolean.FALSE; } @Override @Transactional(rollbackFor = Exception.class) public String delete(Collection ids, Long employeeId, String tenantKey) { List taxAgents = taxAgentMapper.list(TaxAgentQueryParam.builder().ids(ids).build(), tenantKey); if (CollectionUtils.isEmpty(taxAgents)) { throw new SalaryRunTimeException(SalaryI18nUtil.getI18nLabel(tenantKey, employeeId, 85382, "要删除的个税扣缴义务人在不存在或已删除")); } // 正在使用的记录不允许删除 boolean checkUsed = checkUsed(ids, tenantKey); if (checkUsed) { throw new SalaryRunTimeException(SalaryI18nUtil.getI18nLabel(tenantKey, UserContext.getCurrentEmployeeId(), 100570, "正在使用的记录不允许删除")); } // 删除管理员 taxAgentAdminService.deleteByTaxAgentIds(ids, tenantKey); // 删除分管理员和对应管理范围 taxAgentSubAdminService.deleteByTaxAgentIds(ids, tenantKey); // 删除人员范围 taxAgentManageRangeService.deleteByTaxAgentIds(ids, tenantKey); new LambdaUpdateChainWrapper<>(taxAgentMapper) .eq(TaxAgentPO::getDeleteType, 0) .eq(TaxAgentPO::getTenantKey, tenantKey) .in(TaxAgentPO::getId, ids) .set(TaxAgentPO::getDeleteType, 1) .update(); // 记录日志 taxAgents.forEach(e -> SalaryLoggerUtil.recordDeleteSingleLog(taxAgentLoggerTemplate, e.getId(), e.getName(), SalaryI18nUtil.getI18nLabel(tenantKey, employeeId, 100546, "删除个税扣缴义务人"), SalaryI18nUtil.getI18nLabel(tenantKey, employeeId, 100546, "删除个税扣缴义务人") + ":" + e.getName(), e)); return StringUtils.EMPTY; } /** * 检查正在被使用的记录 * * @param ids * @param tenantKey * @return */ private boolean checkUsed(Collection ids, String tenantKey) { // 被薪资档案引用 List salaryArchiveList = new LambdaQueryChainWrapper<>(salaryArchiveMapper) .eq(SalaryArchivePO::getDeleteType, 0) .eq(SalaryArchivePO::getTenantKey, tenantKey) .in(SalaryArchivePO::getTaxAgentId, ids) .list(); if (CollectionUtils.isNotEmpty(salaryArchiveList)) { return Boolean.TRUE; } // 被社保福利档案引用 List socialSchemePOList = new LambdaQueryChainWrapper<>(siArchivesSocialMapper) .eq(InsuranceArchivesSocialSchemePO::getTenantKey, tenantKey) .eq(InsuranceArchivesSocialSchemePO::getDeleteType, DeleteTypeEnum.NOT_DELETED.getValue()) .in(InsuranceArchivesSocialSchemePO::getPaymentOrganization, ids) .list(); if (CollectionUtils.isNotEmpty(socialSchemePOList)) { return Boolean.TRUE; } List fundSchemePOList = new LambdaQueryChainWrapper<>(siArchivesFundMapper) .eq(InsuranceArchivesFundSchemePO::getTenantKey, tenantKey) .eq(InsuranceArchivesFundSchemePO::getDeleteType, DeleteTypeEnum.NOT_DELETED.getValue()) .in(InsuranceArchivesFundSchemePO::getPaymentOrganization, ids) .list(); if (CollectionUtils.isNotEmpty(fundSchemePOList)) { return Boolean.TRUE; } List otherSchemePOList = new LambdaQueryChainWrapper<>(siArchivesOtherMapper) .eq(InsuranceArchivesOtherSchemePO::getTenantKey, tenantKey) .eq(InsuranceArchivesOtherSchemePO::getDeleteType, DeleteTypeEnum.NOT_DELETED.getValue()) .in(InsuranceArchivesOtherSchemePO::getPaymentOrganization, ids) .list(); if (CollectionUtils.isNotEmpty(otherSchemePOList)) { return Boolean.TRUE; } // 被外部人员引用 List extEmployeeList = extEmployeeService.listAll(tenantKey); List extTaxAgentIds = extEmployeeList.stream().map(ExtEmployeePO::getTaxAgentId).distinct().collect(Collectors.toList()); if (CollectionUtils.isNotEmpty(extTaxAgentIds)) { return ids.stream().anyMatch(e -> extTaxAgentIds.stream().anyMatch(f -> f.equals(e))); } // 被累计专项附加扣除引用 List addUpDeductionList = new LambdaQueryChainWrapper<>(addUpDeductionMapper) .eq(AddUpDeductionPO::getDeleteType, 0) .eq(AddUpDeductionPO::getTenantKey, tenantKey) .in(AddUpDeductionPO::getTaxAgentId, ids) .list(); if (CollectionUtils.isNotEmpty(addUpDeductionList)) { return Boolean.TRUE; } // 被其他免税扣除引用 List otherDeductionList = new LambdaQueryChainWrapper<>(otherDeductionMapper) .eq(OtherDeductionPO::getDeleteType, 0) .eq(OtherDeductionPO::getTenantKey, tenantKey) .in(OtherDeductionPO::getTaxAgentId, ids) .list(); if (CollectionUtils.isNotEmpty(otherDeductionList)) { return Boolean.TRUE; } // 被往期累计情况引用 List addUpSituationList = new LambdaQueryChainWrapper<>(addUpSituationMapper) .eq(AddUpSituationPO::getDeleteType, 0) .eq(AddUpSituationPO::getTenantKey, tenantKey) .in(AddUpSituationPO::getTaxAgentId, ids) .list(); if (CollectionUtils.isNotEmpty(addUpSituationList)) { return Boolean.TRUE; } return Boolean.FALSE; } @Override public List> selectList(Long currentEmployeeId, String currentTenantKey) { List taxAgents = taxAgentMapper.list(TaxAgentQueryParam.builder().build(), currentTenantKey); taxAgents = handleForDevolution(taxAgents, currentEmployeeId, currentTenantKey); return CollectionUtils.emptyIfNull(taxAgents).stream().map(m -> { Map map = new HashMap<>(2); map.put("id", String.valueOf(m.getId())); map.put("content", m.getName()); return map; }).collect(Collectors.toList()); } /** * 分权处理 * * @param taxAgents * @param currentEmployeeId * @param currentTenantKey * @return */ private List handleForDevolution(List taxAgents, Long currentEmployeeId, String currentTenantKey) { // 是否开启分权 boolean isOpenDevolution = taxAgentBaseService.isOpenDevolution(currentTenantKey); if (!isOpenDevolution || isChief(currentEmployeeId, currentTenantKey)) { return taxAgents; } List taxAgentIds = taxAgents.stream().map(TaxAgentPO::getId).collect(Collectors.toList()); List enableTaxAgentIds = Lists.newArrayList(); // 1.判断自己是否是管理员, 如果是管理员,就是能够查看所属个税扣缴义务人 List taxAgentAdminList = taxAgentAdminService.listByTaxAgentIdsAndEmployeeId(taxAgentIds, currentEmployeeId, currentTenantKey); // 是管理员的列表 List adminTaxAgentIds = taxAgentAdminList.stream().map(TaxAgentAdminPO::getTaxAgentId).collect(Collectors.toList()); enableTaxAgentIds.addAll(adminTaxAgentIds); // 2.根据作为非管理员查找自己作为分管理员的个税扣缴义务人id List noAdminTaxAgentIds = taxAgents.stream() .map(TaxAgentPO::getId) .filter(id -> !adminTaxAgentIds.contains(id)).collect(Collectors.toList()); if (CollectionUtils.isNotEmpty(noAdminTaxAgentIds)) { List taxAgentSubAdmins = taxAgentSubAdminService.listByTaxAgentIdsAndEmployeeId(noAdminTaxAgentIds, currentEmployeeId, currentTenantKey); List sunAdminTaxAgentIds = taxAgentSubAdmins.stream().map(TaxAgentSubAdminPO::getTaxAgentId).collect(Collectors.toList()); enableTaxAgentIds.addAll(sunAdminTaxAgentIds); } return taxAgents.stream().filter(f -> enableTaxAgentIds.contains(f.getId())).collect(Collectors.toList()); } @Override public Boolean isOpenDevolution(String tenantKey) { return taxAgentBaseService.isOpenDevolution(tenantKey); } @Override public List listTaxAgentAndEmployeeTree(Long employeeId, String tenantKey) { return listTaxAgentAndEmployeeTree(null, employeeId, tenantKey); } @Override public List listTaxAgentAndEmployee(Long employeeId, String tenantKey) { return listTaxAgentAndEmployee(null, employeeId, tenantKey); } @Override public List listTaxAgentAndEmployeeTree(SalaryEmployeeStatusEnum employeeStatus, Long employeeId, String tenantKey) { List taxAgentManageRangeEmployeeList = Lists.newArrayList(); // 所有个税扣缴义务人 List allTaxAgents = listAll(tenantKey); if (CollectionUtils.isEmpty(allTaxAgents)) { return taxAgentManageRangeEmployeeList; } List taxAgentIds = allTaxAgents.stream().map(TaxAgentPO::getId).collect(Collectors.toList()); List allEmployees = taxAgentMapper.listEmployee(tenantKey); if (employeeStatus != null) { List personnelStatusList; // 查询人员状态 List hrmStatusList = hrmCommonHrmStatusService.list(tenantKey); if (employeeStatus.equals(SalaryEmployeeStatusEnum.NORMAL)) { personnelStatusList = hrmStatusList.stream().filter(f -> f.getJobflag().equals(SalaryJobFlagEnum.normal.getValue())).map(m -> m.getCodeId() + "").collect(Collectors.toList()); List finalPersonnelStatus1 = personnelStatusList; allEmployees = allEmployees.stream().filter(f -> finalPersonnelStatus1.contains(f.getPersonnelStatus())).collect(Collectors.toList()); } else if (employeeStatus.equals(SalaryEmployeeStatusEnum.UNAVAILABLE)) { personnelStatusList = hrmStatusList.stream().filter(f -> f.getJobflag().equals(SalaryJobFlagEnum.unavailable.getValue())).map(m -> m.getCodeId() + "").collect(Collectors.toList()); List finalPersonnelStatuss2 = personnelStatusList; allEmployees = allEmployees.stream().filter(f -> finalPersonnelStatuss2.contains(f.getPersonnelStatus())).collect(Collectors.toList()); } } // 是否开启分权 if (!isOpenDevolution(tenantKey) || isChief(employeeId, tenantKey)) { return getTaxAgentEmp(allTaxAgents, taxAgentIds, allEmployees, tenantKey); } // 1.判断自己是否是管理员, 如果是管理员,就是能够操作所属个税扣缴义务人下的所有人的数据 List taxAgentAdminList = taxAgentAdminService.listByTaxAgentIdsAndEmployeeId(taxAgentIds, employeeId, tenantKey); // 是管理员的列表 List adminTaxAgentIds = taxAgentAdminList.stream().map(TaxAgentAdminPO::getTaxAgentId).collect(Collectors.toList()); if (CollectionUtils.isNotEmpty(adminTaxAgentIds)) { taxAgentManageRangeEmployeeList.addAll(getTaxAgentEmp(allTaxAgents, adminTaxAgentIds, allEmployees, tenantKey)); } // 2.根据作为非管理员查找自己作为分管理员, 对应的管理范围人员 List noAdminTaxAgentIds = allTaxAgents.stream() .map(TaxAgentPO::getId) .filter(id -> !adminTaxAgentIds.contains(id)).collect(Collectors.toList()); if (CollectionUtils.isNotEmpty(noAdminTaxAgentIds)) { taxAgentManageRangeEmployeeList.addAll(getTaxAgentSubAdminEmp(allTaxAgents, noAdminTaxAgentIds, allEmployees, tenantKey)); } return taxAgentManageRangeEmployeeList; } /** * 获取分管理员的关联人员 * * @param allTaxAgents * @param noAdminTaxAgentIds * @param allEmployees * @param tenantKey * @return */ private List getTaxAgentSubAdminEmp(List allTaxAgents, List noAdminTaxAgentIds, List allEmployees, String tenantKey) { List taxAgentManageRangeEmployeeList = Lists.newArrayList(); List taxAgentEmps = taxAgentSubAdminEmpService.listByTaxAgentIds(noAdminTaxAgentIds, tenantKey); taxAgentEmps = taxAgentEmps.stream().filter(f -> allEmployees.stream().anyMatch(e -> e.getEmployeeId().equals(f.getEmployeeId()))).collect(Collectors.toList()); List finalTaxAgentEmps = taxAgentEmps; noAdminTaxAgentIds.forEach(e -> { Optional optionalTaxAgent = allTaxAgents.stream().filter(t -> t.getId().equals(e)).findFirst(); String taxAgentName = (optionalTaxAgent.isPresent() ? optionalTaxAgent.get().getName() : ""); List taxAgentEmployees = finalTaxAgentEmps.stream().filter(f -> f.getTaxAgentId().equals(e)).map(m -> { TaxAgentManageRangeEmployeeDTO.TaxAgentEmployee taxAgentEmployee = new TaxAgentManageRangeEmployeeDTO.TaxAgentEmployee(); taxAgentEmployee.setEmployeeId(m.getEmployeeId()); taxAgentEmployee.setUsername(m.getEmployeeName()); return taxAgentEmployee; }).collect(Collectors.toList()); taxAgentManageRangeEmployeeList.add( TaxAgentManageRangeEmployeeDTO.builder() .taxAgentId(e) .taxAgentName(taxAgentName) .employeeList(taxAgentEmployees) .build() ); }); return taxAgentManageRangeEmployeeList; } /** * 获取个税口角义务人的关联人员 * * @param allTaxAgents * @param taxAgentIds * @param tenantKey * @return */ private List getTaxAgentEmp(List allTaxAgents, List taxAgentIds, List allEmployees, String tenantKey) { List taxAgentManageRangeEmployeeList = Lists.newArrayList(); List taxAgentEmps = taxAgentEmpService.listByTaxAgentIds(taxAgentIds, tenantKey); taxAgentEmps = taxAgentEmps.stream().filter(f -> allEmployees.stream().anyMatch(e -> e.getEmployeeId().equals(f.getEmployeeId()))).collect(Collectors.toList()); List finalTaxAgentEmps = taxAgentEmps; taxAgentIds.forEach(e -> { Optional optionalTaxAgent = allTaxAgents.stream().filter(t -> t.getId().equals(e)).findFirst(); String taxAgentName = (optionalTaxAgent.isPresent() ? optionalTaxAgent.get().getName() : ""); List taxAgentEmployees = finalTaxAgentEmps.stream().filter(f -> f.getTaxAgentId().equals(e)).map(m -> { TaxAgentManageRangeEmployeeDTO.TaxAgentEmployee taxAgentEmployee = new TaxAgentManageRangeEmployeeDTO.TaxAgentEmployee(); taxAgentEmployee.setEmployeeId(m.getEmployeeId()); taxAgentEmployee.setUsername(m.getEmployeeName()); return taxAgentEmployee; }).collect(Collectors.toList()); taxAgentManageRangeEmployeeList.add( TaxAgentManageRangeEmployeeDTO.builder() .taxAgentId(e) .taxAgentName(taxAgentName) .employeeList(taxAgentEmployees) .build() ); }); return taxAgentManageRangeEmployeeList; } @Override public List listTaxAgentAndEmployee(SalaryEmployeeStatusEnum employeeStatus, Long employeeId, String tenantKey) { List taxAgentEmployeeList = Lists.newArrayList(); List taxAgentManageRangeEmployeeList = listTaxAgentAndEmployeeTree(employeeStatus, employeeId, tenantKey); taxAgentManageRangeEmployeeList.forEach(m -> { List employeeList = m.getEmployeeList(); if (CollectionUtils.isEmpty(employeeList)) { taxAgentEmployeeList.add(TaxAgentEmployeeDTO.builder() .taxAgentId(m.getTaxAgentId()) .taxAgentName(m.getTaxAgentName()) .employeeId(null) .username(null) .build()); } else { employeeList.forEach(f -> taxAgentEmployeeList.add(TaxAgentEmployeeDTO.builder() .taxAgentId(m.getTaxAgentId()) .taxAgentName(m.getTaxAgentName()) .employeeId(f.getEmployeeId()) .username(f.getUsername()) .build())); } }); return taxAgentEmployeeList; } }