package com.engine.salary.service.impl;
import com.engine.common.util.ServiceUtil;
import com.engine.core.impl.Service;
import com.engine.salary.constant.SalaryDefaultTenantConstant;
import com.engine.salary.encrypt.EncryptUtil;
import com.engine.salary.entity.datacollection.DataCollectionEmployee;
import com.engine.salary.entity.taxagent.dto.TaxAgentEmployeeDTO;
import com.engine.salary.entity.taxdeclaration.bo.TaxDeclarationBO;
import com.engine.salary.entity.taxdeclaration.bo.TaxDeclarationDetailBO;
import com.engine.salary.entity.taxdeclaration.dto.*;
import com.engine.salary.entity.taxdeclaration.param.TaxDeclarationDetailFormEditParam;
import com.engine.salary.entity.taxdeclaration.param.TaxDeclarationDetailFormQueryParam;
import com.engine.salary.entity.taxdeclaration.param.TaxDeclarationDetailListQueryParam;
import com.engine.salary.entity.taxdeclaration.po.TaxDeclarationDetailPO;
import com.engine.salary.entity.taxdeclaration.po.TaxDeclarationPO;
import com.engine.salary.enums.salaryaccounting.EmployeeTypeEnum;
import com.engine.salary.enums.salarysob.IncomeCategoryEnum;
import com.engine.salary.exception.SalaryRunTimeException;
import com.engine.salary.mapper.taxdeclaration.TaxDeclarationDetailMapper;
import com.engine.salary.service.SalaryEmployeeService;
import com.engine.salary.service.TaxAgentService;
import com.engine.salary.service.TaxDeclarationDetailService;
import com.engine.salary.service.TaxDeclarationService;
import com.engine.salary.sys.entity.vo.OrderRuleVO;
import com.engine.salary.sys.service.SalarySysConfService;
import com.engine.salary.sys.service.impl.SalarySysConfServiceImpl;
import com.engine.salary.util.SalaryEntityUtil;
import com.engine.salary.util.db.IdGenerator;
import com.engine.salary.util.db.MapperProxyFactory;
import com.engine.salary.util.page.Column;
import com.engine.salary.util.page.PageInfo;
import com.engine.salary.util.page.SalaryPageUtil;
import com.google.common.collect.Lists;
import org.apache.commons.collections4.CollectionUtils;
import org.apache.commons.lang3.BooleanUtils;
import org.apache.commons.lang3.math.NumberUtils;
import weaver.hrm.User;
import weaver.wechat.util.Utils;
import java.util.*;
import java.util.stream.Collectors;
/**
* 个税申报表明细
*
Copyright: Copyright (c) 2022
* Company: 泛微软件
*
* @author qiantao
* @version 1.0
**/
public class TaxDeclarationDetailServiceImpl extends Service implements TaxDeclarationDetailService {
private EncryptUtil encryptUtil = new EncryptUtil();
private TaxDeclarationDetailMapper getTaxDeclarationDetailMapper() {
return MapperProxyFactory.getProxy(TaxDeclarationDetailMapper.class);
}
private SalaryEmployeeService getSalaryEmployeeService() {
return ServiceUtil.getService(SalaryEmployeeServiceImpl.class, user);
}
private TaxDeclarationService getTaxDeclarationService(User user) {
return ServiceUtil.getService(TaxDeclarationServiceImpl.class, user);
}
private TaxAgentService getTaxAgentService(User user) {
return ServiceUtil.getService(TaxAgentServiceImpl.class, user);
}
private SalaryEmployeeService getSalaryEmployeeService(User user) {
return (SalaryEmployeeService) ServiceUtil.getService(SalaryEmployeeServiceImpl.class, user);
}
private SalarySysConfService getSalarySysConfService(User user) {
return ServiceUtil.getService(SalarySysConfServiceImpl.class, user);
}
@Override
public List listByTaxDeclarationIdAndEmployeeIds(Long taxDeclarationId, Collection employeeIds) {
if (CollectionUtils.isEmpty(employeeIds)) {
return Collections.emptyList();
}
return getTaxDeclarationDetailMapper().listByTaxDeclarationIdAndEmployeeIds(taxDeclarationId, employeeIds);
}
@Override
public PageInfo listPage4EmployeeIdByParam(TaxDeclarationDetailListQueryParam queryParam) {
//排序配置
OrderRuleVO orderRule = getSalarySysConfService(user).orderRule();
queryParam.setOrderRule(orderRule);
// SalaryPageUtil.start(queryParam.getCurrent(), queryParam.getPageSize());
List taxDeclarationEmployeeDTOS = getTaxDeclarationDetailMapper().listPage4EmployeeId(queryParam);
taxDeclarationEmployeeDTOS = taxDeclarationEmployeeDTOS.stream().filter(SalaryEntityUtil.distinctByKey(TaxDeclarationEmployeeDTO::getEmployeeId)).collect(Collectors.toList());
List list = SalaryPageUtil.subList(queryParam.getCurrent(), queryParam.getPageSize(), taxDeclarationEmployeeDTOS);
PageInfo pageInfo = new PageInfo<>(list, TaxDeclarationEmployeeDTO.class);
pageInfo.setPageNum(queryParam.getCurrent());
pageInfo.setPageSize(queryParam.getPageSize());
pageInfo.setTotal(taxDeclarationEmployeeDTOS.size());
return pageInfo;
}
@Override
public PageInfo listDtoPageByParam(TaxDeclarationDetailListQueryParam queryParam) {
// 查询个税申报表明细的人员
List employeeIdPage = getTaxDeclarationDetailMapper().listEmployeeId(queryParam.getTaxDeclarationId());
PageInfo dtoPage = new PageInfo(TaxDeclarationDetailListDTO.class);
dtoPage.setPageNum(queryParam.getCurrent());
dtoPage.setPageSize(queryParam.getPageSize());
if (null == employeeIdPage) {
return dtoPage;
}
if (CollectionUtils.isNotEmpty(employeeIdPage)) {
// 查询个税申报表明细
List taxDeclarationDetailPOS = listByTaxDeclarationIdAndEmployeeIds(queryParam.getTaxDeclarationId(), employeeIdPage);
// 查询人员
List simpleEmployees = getSalaryEmployeeService().getEmployeeByIdsAll(employeeIdPage);
// List simpleEmployees = hrmCommonEmployeeService.getEmployeeByIds(employeeIdPage.getRecords(), tenantKey);
// 转换成列表dto
TaxDeclarationBO.buildDetailListDTO(queryParam.getTaxDeclarationId(), dtoPage, taxDeclarationDetailPOS, simpleEmployees);
}
return dtoPage;
}
@Override
public PageInfo listDtoPageByParam4Labor(TaxDeclarationDetailListQueryParam queryParam) {
// 查询个税申报主表
TaxDeclarationPO taxDeclarationPO = getTaxDeclarationService(user).getById(queryParam.getTaxDeclarationId());
// 判断是否有权限查看
boolean canSee = getTaxDeclarationService(user).checkByAuthority(taxDeclarationPO, (long) user.getUID());
if (!canSee) {
throw new SalaryRunTimeException("对不起,您暂时没有权限查看");
}
// 查询个税申报表明细的人员
PageInfo employeeIdPage = listPage4EmployeeIdByParam(queryParam);
PageInfo dtoPage = new PageInfo<>(TaxDeclarationLaborListDTO.class);
dtoPage.setPageSize(employeeIdPage.getPageSize());
dtoPage.setPageNum(employeeIdPage.getPageNum());
dtoPage.setTotal(employeeIdPage.getTotal());
List list = employeeIdPage.getList();
if (CollectionUtils.isNotEmpty(list)) {
// 转换成列表dto
List taxDeclarationLaborListDTOS = listDto4Labor(queryParam.getTaxDeclarationId(), list);
dtoPage.setList(taxDeclarationLaborListDTOS);
}
return dtoPage;
}
@Override
public PageInfo listDtoPageByParam4Annual(TaxDeclarationDetailListQueryParam queryParam) {
// 查询个税申报主表
TaxDeclarationPO taxDeclarationPO = getTaxDeclarationService(user).getById(queryParam.getTaxDeclarationId());
// 判断是否有权限查看
boolean canSee = getTaxDeclarationService(user).checkByAuthority(taxDeclarationPO, (long) user.getUID());
if (!canSee) {
throw new SalaryRunTimeException("对不起,您暂时没有权限查看");
}
// 查询个税申报表明细的人员
PageInfo employeeIdPage = listPage4EmployeeIdByParam(queryParam);
PageInfo dtoPage = new PageInfo<>(TaxDeclarationAnnualListDTO.class);
dtoPage.setPageSize(employeeIdPage.getPageSize());
dtoPage.setPageNum(employeeIdPage.getPageNum());
dtoPage.setTotal(employeeIdPage.getTotal());
List list = employeeIdPage.getList();
if (CollectionUtils.isNotEmpty(list)) {
// 转换成列表dto
List taxDeclarationLaborListDTOS = listDto4Annual(queryParam.getTaxDeclarationId(), list);
dtoPage.setList(taxDeclarationLaborListDTOS);
}
return dtoPage;
}
@Override
public List listDto4Labor(Long taxDeclarationId, List taxDeclarationEmployees) {
long employeeId = (long) user.getUID();
// 查询个税申报主表
TaxDeclarationPO taxDeclarationPO = getTaxDeclarationService(user).getById(taxDeclarationId);
// 判断是否有权限查看
boolean canSee = getTaxDeclarationService(user).checkByAuthority(taxDeclarationPO, employeeId);
if (!canSee) {
throw new SalaryRunTimeException("对不起,您暂时没有权限查看");
}
// 查询个税申报表明细
List employeeIds = taxDeclarationEmployees.stream().map(TaxDeclarationEmployeeDTO::getEmployeeId).collect(Collectors.toList());
List taxDeclarationDetailPOS = listByTaxDeclarationIdAndEmployeeIds(taxDeclarationId, employeeIds);
// 查询人员信息
List simpleEmployees = getSalaryEmployeeService().getEmployeeByIdsAll(employeeIds);
// 转换成列表dto
return TaxDeclarationDetailBO.convert2ListDTO4Labor(taxDeclarationDetailPOS, taxDeclarationEmployees, simpleEmployees);
}
public List listDto4Annual(Long taxDeclarationId, List taxDeclarationEmployees) {
long employeeId = (long) user.getUID();
// 查询个税申报主表
TaxDeclarationPO taxDeclarationPO = getTaxDeclarationService(user).getById(taxDeclarationId);
// 判断是否有权限查看
boolean canSee = getTaxDeclarationService(user).checkByAuthority(taxDeclarationPO, employeeId);
if (!canSee) {
throw new SalaryRunTimeException("对不起,您暂时没有权限查看");
}
// 查询个税申报表明细
List employeeIds = taxDeclarationEmployees.stream().map(TaxDeclarationEmployeeDTO::getEmployeeId).collect(Collectors.toList());
List taxDeclarationDetailPOS = listByTaxDeclarationIdAndEmployeeIds(taxDeclarationId, employeeIds);
// 查询人员信息
List simpleEmployees = getSalaryEmployeeService().getEmployeeByIdsAll(employeeIds);
// 转换成列表dto
return TaxDeclarationDetailBO.convert2ListDTO4Annual(taxDeclarationDetailPOS, taxDeclarationEmployees, simpleEmployees);
}
/**
* 根据权限范围过滤
*
* @param taxDeclarationPO
* @param taxDeclarationEmployeeDTOS
* @return
*/
private List filterByAuthority(TaxDeclarationPO taxDeclarationPO, List taxDeclarationEmployeeDTOS) {
long employeeId = user.getUID();
if (CollectionUtils.isEmpty(taxDeclarationEmployeeDTOS)) {
return Collections.emptyList();
}
// 判断是否开启了分权
Boolean openDevolution = getTaxAgentService(user).isOpenDevolution();
// 是否是总管理员
Boolean isChief = getTaxAgentService(user).isChief(employeeId);
if (BooleanUtils.isFalse(openDevolution) || isChief) {
return taxDeclarationEmployeeDTOS;
}
// 查询权限范围内的人员
List taxAgentEmployeeDTOS = getTaxAgentService(user).listTaxAgentAndEmployee(employeeId);
Set simpleEmployeeKeySet = SalaryEntityUtil.properties(taxAgentEmployeeDTOS, taxAgentEmployeeDTO -> taxAgentEmployeeDTO.getEmployeeId() + "-" + taxAgentEmployeeDTO.getTaxAgentId());
// 查询权限范围内的外部人员
// List extEmployeePOS = extEmployeeService.listCanUseByEmployeeId(employeeId, tenantKey);
// Set extEmployeeKeySet = SalaryEntityUtil.properties(extEmployeePOS, extEmployeePO -> extEmployeePO.getId() + "-" + extEmployeePO.getTaxAgentId());
// 根据权限范围过滤
return taxDeclarationEmployeeDTOS.stream().filter(taxDeclarationEmployeeDTO -> {
if (taxDeclarationEmployeeDTO.getEmployeeType() == null || Objects.equals(taxDeclarationEmployeeDTO.getEmployeeType(), EmployeeTypeEnum.ORGANIZATION.getValue())
&& simpleEmployeeKeySet.contains(taxDeclarationEmployeeDTO.getEmployeeId() + "-" + taxDeclarationPO.getTaxAgentId())) {
return true;
}
// if (Objects.equals(taxDeclarationEmployeeDTO.getEmployeeType(), EmployeeTypeEnum.EXT_EMPLOYEE.getValue())
// && extEmployeeKeySet.contains(taxDeclarationEmployeeDTO.getEmployeeId() + "-" + taxDeclarationPO.getTaxAgentId())) {
// return true;
// }
return false;
}).collect(Collectors.toList());
}
@Override
public PageInfo listDtoPageByParam4Wage(TaxDeclarationDetailListQueryParam queryParam) {
// 查询个税申报主表
TaxDeclarationPO taxDeclarationPO = getTaxDeclarationService(user).getById(queryParam.getTaxDeclarationId());
// 查询个税申报表明细的人员
PageInfo employeeIdPage = SalaryPageUtil.buildPage(queryParam.getCurrent(), queryParam.getPageSize());
// 判断是否开启了分权
Boolean openDevolution = getTaxAgentService(user).isOpenDevolution();
// 是否是总管理员
Boolean isChief = getTaxAgentService(user).isChief((long) user.getUID());
// if (BooleanUtils.isTrue(openDevolution) && !isChief) {
// List taxDeclarationEmployeeDTOS = getTaxDeclarationDetailMapper().list4EmployeeId(queryParam.getTaxDeclarationId());
// // 根据权限过滤
// taxDeclarationEmployeeDTOS = filterByAuthority(taxDeclarationPO, taxDeclarationEmployeeDTOS);
// employeeIdPage.setTotal(taxDeclarationEmployeeDTOS.size());
// employeeIdPage.setList(SalaryPageUtil.subList((int) employeeIdPage.getPageNum(), (int) employeeIdPage.getPageSize(), taxDeclarationEmployeeDTOS));
// } else {
employeeIdPage = listPage4EmployeeIdByParam(queryParam);
// }
PageInfo dtoPage = SalaryPageUtil.buildPage(queryParam.getCurrent(), queryParam.getPageSize(), TaxDeclarationWageListDTO.class);
dtoPage.setTotal(employeeIdPage.getTotal());
if (CollectionUtils.isNotEmpty(employeeIdPage.getList())) {
List list = employeeIdPage.getList();
// 查询个税申报表明细
Set employeeIds = SalaryEntityUtil.properties(list, TaxDeclarationEmployeeDTO::getEmployeeId);
List taxDeclarationDetailPOS = listByTaxDeclarationIdAndEmployeeIds(queryParam.getTaxDeclarationId(),
employeeIds);
encryptUtil.decryptList(taxDeclarationDetailPOS, TaxDeclarationDetailPO.class);
// 查询人员信息
List simpleEmployeeIds = list.stream()
.filter(taxDeclarationEmployeeDTO -> Objects.equals(taxDeclarationEmployeeDTO.getEmployeeType(), EmployeeTypeEnum.ORGANIZATION.getValue()))
.map(TaxDeclarationEmployeeDTO::getEmployeeId)
.collect(Collectors.toList());
List simpleEmployees = getSalaryEmployeeService(user).getEmployeeByIds(simpleEmployeeIds);
// 查询人员薪资(身份证号码等)
// List simpleUserInfos = salaryEmployeeService.listByEmployeeIds(simpleEmployeeIds, tenantKey);
// 查询外部人员
// Set extEmployeeIds = employeeIdPage.getRecords().stream()
// .filter(taxDeclarationEmployeeDTO -> Objects.equals(taxDeclarationEmployeeDTO.getEmployeeType(), EmployeeTypeEnum.EXT_EMPLOYEE.getValue()))
// .map(TaxDeclarationEmployeeDTO::getEmployeeId)
// .collect(Collectors.toSet());
// List extEmployees = extEmployeeService.listByIds(extEmployeeIds, tenantKey);
// 转换成列表dto
List taxDeclarationWageListDTOS = TaxDeclarationDetailBO.convert2ListDTO4Wage(taxDeclarationDetailPOS,
employeeIdPage.getList(), simpleEmployees);
dtoPage.setList(taxDeclarationWageListDTOS);
}
return dtoPage;
}
@Override
public void batchSave(Collection taxDeclarationDetailPOS) {
if (CollectionUtils.isNotEmpty(taxDeclarationDetailPOS)) {
taxDeclarationDetailPOS = encryptUtil.encryptList(new ArrayList<>(taxDeclarationDetailPOS), TaxDeclarationDetailPO.class);
List> partition = Lists.partition((List) taxDeclarationDetailPOS, 100);
partition.forEach(getTaxDeclarationDetailMapper()::batchInsert);
}
}
public List listByTaxDeclarationIdAndEmployeeIds(Long taxDeclarationId, List employeeIds) {
if (CollectionUtils.isEmpty(employeeIds)) {
return Collections.emptyList();
}
List taxDeclarationDetailPOS = getTaxDeclarationDetailMapper().listByTaxDeclarationIdAndEmployeeIds(taxDeclarationId, employeeIds);
return encryptUtil.decryptList(taxDeclarationDetailPOS, TaxDeclarationDetailPO.class);
}
@Override
public void deleteByTaxDeclarationIds(Collection taxDeclarationIds) {
getTaxDeclarationDetailMapper().deleteByTaxDeclarationIds(taxDeclarationIds);
}
@Override
public Map getTaxDeclarationDetailForm(TaxDeclarationDetailFormQueryParam param) {
Map resultMap = new HashMap<>();
if (param.getTaxDeclarationId() == null || param.getEmployeeId() == null) {
return resultMap;
}
// 查询个税申报主表
TaxDeclarationPO taxDeclarationPO = getTaxDeclarationService(user).getById(param.getTaxDeclarationId());
if (taxDeclarationPO == null) {
return resultMap;
}
List taxDeclarationDetailPOS = listByTaxDeclarationIdAndEmployeeIds(param.getTaxDeclarationId(), Collections.singletonList(param.getEmployeeId()));
if (CollectionUtils.isEmpty(taxDeclarationDetailPOS)) {
return resultMap;
}
Integer employeeType = taxDeclarationDetailPOS.stream().map(TaxDeclarationDetailPO::getEmployeeType).findFirst().orElse(0);
List taxDeclarationEmployeeDTOS = Collections.singletonList(TaxDeclarationEmployeeDTO.builder().employeeType(employeeType).employeeId(param.getEmployeeId()).build());
List simpleEmployees = getSalaryEmployeeService(user).getEmployeeByIds(Collections.singletonList(param.getEmployeeId()));
// 正常工资薪金所得
if (Objects.equals(taxDeclarationPO.getIncomeCategory(), IncomeCategoryEnum.WAGES_AND_SALARIES.getValue())) {
List taxDeclarationWageListDTOS = TaxDeclarationDetailBO.convert2ListDTO4Wage(taxDeclarationDetailPOS, taxDeclarationEmployeeDTOS, simpleEmployees);
PageInfo pageInfo = new PageInfo<>(TaxDeclarationWageListDTO.class);
List columns = pageInfo.getColumns();
resultMap.put("column", columns);
if (CollectionUtils.isNotEmpty(taxDeclarationWageListDTOS)) {
resultMap.put("data", taxDeclarationWageListDTOS.get(0));
}
}
// 劳务报酬所得
if (Objects.equals(taxDeclarationPO.getIncomeCategory(), IncomeCategoryEnum.REMUNERATION_FOR_LABOR.getValue())) {
List taxDeclarationLaborListDTOS = listDto4Labor(param.getTaxDeclarationId(), taxDeclarationEmployeeDTOS);
PageInfo pageInfo = new PageInfo<>(TaxDeclarationLaborListDTO.class);
List columns = pageInfo.getColumns();
resultMap.put("column", columns);
if (CollectionUtils.isNotEmpty(taxDeclarationLaborListDTOS)) {
resultMap.put("data", taxDeclarationLaborListDTOS.get(0));
}
}
// 全年一次性奖金收入
if (Objects.equals(taxDeclarationPO.getIncomeCategory(), IncomeCategoryEnum.ONETIME_ANNUAL_BONUS.getValue())) {
List taxDeclarationLaborListDTOS = listDto4Annual(param.getTaxDeclarationId(), taxDeclarationEmployeeDTOS);
PageInfo pageInfo = new PageInfo<>(TaxDeclarationAnnualListDTO.class);
List columns = pageInfo.getColumns();
resultMap.put("column", columns);
if (CollectionUtils.isNotEmpty(taxDeclarationLaborListDTOS)) {
resultMap.put("data", taxDeclarationLaborListDTOS.get(0));
}
}
return resultMap;
}
@Override
public void editTaxDeclarationDetailData(TaxDeclarationDetailFormEditParam param) {
if (param.getTaxDeclarationId() == null || param.getEmployeeId() == null || param.getValueMap() == null || param.getValueMap().isEmpty()) {
return;
}
// 查询个税申报主表
TaxDeclarationPO taxDeclarationPO = getTaxDeclarationService(user).getById(param.getTaxDeclarationId());
if (taxDeclarationPO == null) {
throw new SalaryRunTimeException("个税申报表不存在,请刷新页面后重试");
}
List taxDeclarationDetailPOS = getTaxDeclarationDetailMapper().listByTaxDeclarationIdAndEmployeeIds(param.getTaxDeclarationId(), Collections.singletonList(param.getEmployeeId()));
Integer employeeType = taxDeclarationDetailPOS.stream().map(TaxDeclarationDetailPO::getEmployeeType).distinct().collect(Collectors.toList()).get(0);
// 插入数据
List needInsertList = Lists.newArrayListWithExpectedSize(param.getValueMap().size());
param.getValueMap().forEach((fieldCode, fieldValue) -> {
TaxDeclarationDetailPO taxDeclarationDetailPO = TaxDeclarationDetailPO.builder()
.id(IdGenerator.generate())
.taxDeclarationId(param.getTaxDeclarationId())
.employeeType(employeeType)
.employeeId(param.getEmployeeId())
.fieldCode(fieldCode)
.fieldValue(Utils.null2String(fieldValue))
.creator(Long.valueOf(user.getUID()))
.createTime(new Date())
.updateTime(new Date())
.deleteType(NumberUtils.INTEGER_ZERO)
.tenantKey(SalaryDefaultTenantConstant.DEFAULT_TENANT_KEY)
.build();
needInsertList.add(taxDeclarationDetailPO);
});
if (CollectionUtils.isNotEmpty(needInsertList)) {
// 先删除原有数据
deleteByTaxDeclarationIdAndEmployeeId(param.getTaxDeclarationId(), param.getEmployeeId());
batchSave(needInsertList);
}
}
private void deleteByTaxDeclarationIdAndEmployeeId(Long taxDeclarationId, Long employeeId) {
if (taxDeclarationId == null || employeeId == null) {
return;
}
getTaxDeclarationDetailMapper().deleteByTaxDeclarationIdAndEmployeeId(taxDeclarationId, employeeId);
}
}