package com.engine.salary.service.impl;
import cn.hutool.core.map.MapUtil;
import com.engine.common.util.ServiceUtil;
import com.engine.core.impl.Service;
import com.engine.hrmelog.entity.dto.LoggerContext;
import com.engine.salary.common.LocalDateRange;
import com.engine.salary.common.taxdeclare.AsyncRequestIdDTO;
import com.engine.salary.config.SalaryElogConfig;
import com.engine.salary.constant.SalaryDefaultTenantConstant;
import com.engine.salary.constant.SzyhApiConstant;
import com.engine.salary.entity.datacollection.DataCollectionEmployee;
import com.engine.salary.entity.employeedeclare.bo.EmployeeDeclareList;
import com.engine.salary.entity.employeedeclare.bo.EmployeeDeclareRefresh;
import com.engine.salary.entity.employeedeclare.bo.EmployeeDeclareRequest;
import com.engine.salary.entity.employeedeclare.dto.*;
import com.engine.salary.entity.employeedeclare.param.*;
import com.engine.salary.entity.employeedeclare.po.EmployeeDeclarePO;
import com.engine.salary.entity.employeedeclare.po.EmployeeDeclareRecordPO;
import com.engine.salary.entity.employeedeclare.response.DeclareEmployeeFeedbackResponse;
import com.engine.salary.entity.employeedeclare.response.DeclareEmployeeInfoResponse;
import com.engine.salary.entity.salaryacct.po.SalaryAcctTaxAgentPO;
import com.engine.salary.entity.salaryarchive.dto.SalaryArchiveDataDTO;
import com.engine.salary.entity.salaryarchive.po.SalaryArchivePO;
import com.engine.salary.entity.taxagent.bo.TaxAgentTaxReturnBO;
import com.engine.salary.entity.taxagent.po.TaxAgentPO;
import com.engine.salary.entity.taxagent.po.TaxAgentTaxReturnPO;
import com.engine.salary.entity.taxapiflow.bo.TaxApiFlowBO;
import com.engine.salary.entity.taxapiflow.po.TaxDeclarationApiFlowRecordPO;
import com.engine.salary.entity.taxdeclaration.po.TaxDeclarationApiConfigPO;
import com.engine.salary.enums.OperateTypeEnum;
import com.engine.salary.enums.SalaryCycleTypeEnum;
import com.engine.salary.enums.SalaryOnOffEnum;
import com.engine.salary.enums.datacollection.UseEmployeeTypeEnum;
import com.engine.salary.enums.employeedeclare.CardTypeEnum;
import com.engine.salary.enums.employeedeclare.DeclareStatusEnum;
import com.engine.salary.enums.employeedeclare.EmploymentStatusEnum;
import com.engine.salary.enums.employeedeclare.EmploymentTypeEnum;
import com.engine.salary.enums.salaryaccounting.EmployeeTypeEnum;
import com.engine.salary.enums.sicategory.DeleteTypeEnum;
import com.engine.salary.enums.taxagent.TaxAgentTaxReturnStatusEnum;
import com.engine.salary.enums.taxdeclaration.EnumDeclareApiBusinessType;
import com.engine.salary.exception.SalaryRunTimeException;
import com.engine.salary.mapper.employeedeclare.EmployeeDeclareMapper;
import com.engine.salary.remote.tax.client.EmployeeClient;
import com.engine.salary.remote.tax.request.employee.BatchInvalidSubmissionRequest;
import com.engine.salary.remote.tax.request.employee.InformationChangeRequest;
import com.engine.salary.service.*;
import com.engine.salary.util.*;
import com.engine.salary.util.db.IdGenerator;
import com.engine.salary.util.db.MapperProxyFactory;
import com.engine.salary.util.page.PageInfo;
import com.engine.salary.util.page.SalaryPageUtil;
import com.engine.salary.util.valid.ValidUtil;
import com.google.common.collect.ImmutableMap;
import com.google.common.collect.Lists;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.collections4.CollectionUtils;
import org.apache.commons.lang3.ObjectUtils;
import org.apache.commons.lang3.StringUtils;
import weaver.general.Util;
import weaver.hrm.User;
import java.time.LocalDate;
import java.util.*;
import java.util.stream.Collectors;
/**
* 人员报送(人员)
*
Copyright: Copyright (c) 2023
* Company: 泛微软件
*
* @author qiantao
* @version 1.0
**/
@Slf4j
public class EmployeeDeclareServiceImpl extends Service implements EmployeeDeclareService {
private EmployeeDeclareMapper getEmployeeDeclareMapper() {
return MapperProxyFactory.getProxy(EmployeeDeclareMapper.class);
}
private TaxAgentService getTaxAgentService(User user) {
return ServiceUtil.getService(TaxAgentServiceImpl.class, user);
}
private SalaryEmployeeService getSalaryEmployeeService(User user) {
return ServiceUtil.getService(SalaryEmployeeServiceImpl.class, user);
}
private SalaryArchiveService getSalaryArchiveService(User user) {
return ServiceUtil.getService(SalaryArchiveServiceImpl.class, user);
}
private TaxDeclarationApiConfigService getTaxDeclarationApiConfigService(User user) {
return ServiceUtil.getService(TaxDeclarationApiConfigServiceImpl.class, user);
}
private TaxAgentTaxReturnService getTaxAgentTaxReturnService(User user) {
return ServiceUtil.getService(TaxAgentTaxReturnServiceImpl.class, user);
}
private EmployeeDeclareRecordService getEmployeeDeclareRecordService(User user) {
return ServiceUtil.getService(EmployeeDeclareRecordServiceImpl.class, user);
}
private TaxDeclarationApiBillingService getTaxDeclarationApiBillingService(User user) {
return ServiceUtil.getService(TaxDeclarationApiBillingServiceImpl.class, user);
}
@Override
public EmployeeDeclarePO getById(Long id) {
return getEmployeeDeclareMapper().getById(id);
}
@Override
public List listByIds(Collection ids) {
if (CollectionUtils.isEmpty(ids)) {
return Collections.emptyList();
}
return getEmployeeDeclareMapper().listSome(EmployeeDeclarePO.builder().ids(ids).build());
}
@Override
public List listByTaxCycleAndTaxAgentId(Date taxCycle, Long taxAgentId) {
return getEmployeeDeclareMapper().listSome(EmployeeDeclarePO.builder()
.taxCycle(taxCycle)
.taxAgentId(taxAgentId)
.build());
}
@Override
public List listByTaxCycleAndTaxAgentIdAndEmployeeIds(Date taxCycle, Long taxAgentId, Collection employeeIds) {
if (CollectionUtils.isEmpty(employeeIds)) {
return Collections.emptyList();
}
return getEmployeeDeclareMapper().listSome(EmployeeDeclarePO.builder()
.taxCycle(taxCycle)
.taxAgentId(taxAgentId)
.employeeIds(employeeIds)
.build());
}
@Override
public List listByParam(EmployeeDeclareListQueryParam queryParam) {
return getEmployeeDeclareMapper().listByParam(queryParam);
}
@Override
public List list4AddByParam(EmployeeDeclareAddListQueryParam queryParam) {
queryParam.setPreTaxCycle(SalaryDateUtil.plusMonths(queryParam.getTaxCycle(), -1));
return getEmployeeDeclareMapper().list4AddByParam(queryParam);
}
@Override
public List list4UpdateByParam(EmployeeDeclareListQueryParam queryParam) {
return getEmployeeDeclareMapper().list4UpdateByParam(queryParam);
}
@Override
public PageInfo listPage4UpdateByParam(EmployeeDeclareListQueryParam queryParam) {
List list = getEmployeeDeclareMapper().list4UpdateByParam(queryParam);
return SalaryPageUtil.buildPage(queryParam.getCurrent(), queryParam.getPageSize(), list, EmployeeDeclarePO.class);
}
// @Override
// public List list4FailByParam(EmployeeDeclareFailListQueryParam queryParam) {
// return new LambdaQueryChainWrapper<>(employeeDeclareMapper)
// .eq(EmployeeDeclarePO::getTenantKey)
// .eq(EmployeeDeclarePO::getDeleteType, DeleteTypeEnum.NOT_DELETED.getValue())
// .eq(EmployeeDeclarePO::getTaxAgentId, queryParam.getTaxAgentId())
// .eq(EmployeeDeclarePO::getTaxCycle, queryParam.getTaxCycle().toString())
// .eq(EmployeeDeclarePO::getDeclareStatus, DeclareStatusEnum.DECLARE_FAIL.getValue())
// .list();
// }
@Override
public PageInfo listPage4FailByParam(EmployeeDeclareFailListQueryParam queryParam) {
ValidUtil.doValidator(queryParam);
List list = getEmployeeDeclareMapper().listSome(EmployeeDeclarePO.builder()
.taxAgentId(queryParam.getTaxAgentId())
.taxCycle(queryParam.getTaxCycle())
.declareStatus(DeclareStatusEnum.DECLARE_FAIL.getValue())
.build());
PageInfo page = SalaryPageUtil.buildPage(queryParam.getCurrent(), queryParam.getPageSize(),
list, EmployeeDeclarePO.class);
return page;
}
@Override
public List listBySalaryAcctTaxAgent(List salaryAcctTaxAgents) {
if (CollectionUtils.isEmpty(salaryAcctTaxAgents)) {
return Collections.emptyList();
}
Map employeeDeclareParamMap = new HashMap<>();
for (SalaryAcctTaxAgentPO po : salaryAcctTaxAgents) {
employeeDeclareParamMap.putIfAbsent(po.getTaxAgentId() + "-" + po.getTaxCycle(), po);
}
List employeeDeclares = new ArrayList<>();
for (Map.Entry entry : employeeDeclareParamMap.entrySet()) {
SalaryAcctTaxAgentPO salaryAcctTaxAgent = entry.getValue();
List temp = listByTaxCycleAndTaxAgentId(salaryAcctTaxAgent.getTaxCycle(), salaryAcctTaxAgent.getTaxAgentId());
employeeDeclares.addAll(temp);
}
return employeeDeclares;
}
@Override
public void save(EmployeeDeclareSaveParam saveParam) {
Date now = new Date();
EmployeeDeclarePO employeeDeclare = new EmployeeDeclarePO();
employeeDeclare.setTaxAgentId(saveParam.getTaxAgentId());
employeeDeclare.setTaxCycle(saveParam.getTaxCycle());
employeeDeclare.setEmployeeName(saveParam.getEmployeeName());
employeeDeclare.setJobNum(saveParam.getJobNum());
employeeDeclare.setNationality(saveParam.getNationality());
employeeDeclare.setCardType(saveParam.getCardType().getValue());
employeeDeclare.setCardNum(saveParam.getCardNum().toUpperCase());
employeeDeclare.setGender(saveParam.getGender().getValue());
employeeDeclare.setBirthday(SalaryDateUtil.localDateToDate(saveParam.getBirthday()));
employeeDeclare.setBirthplace(saveParam.getBirthplace());
employeeDeclare.setTaxReasons(saveParam.getTaxReasons());
employeeDeclare.setEmploymentStatus(saveParam.getEmploymentStatus().getValue());
employeeDeclare.setMobile(saveParam.getMobile());
employeeDeclare.setEmploymentType(saveParam.getEmploymentType().getValue());
employeeDeclare.setEmploymentFirstYear("");
employeeDeclare.setEmploymentDate(SalaryDateUtil.localDateToDate(saveParam.getEmploymentDate()));
employeeDeclare.setDismissDate(SalaryDateUtil.localDateToDate(saveParam.getDismissDate()));
employeeDeclare.setEntryDate(SalaryDateUtil.localDateToDate(saveParam.getEntryDate()));
employeeDeclare.setDepartureDate(SalaryDateUtil.localDateToDate(saveParam.getDepartureDate()));
employeeDeclare.setDisability(saveParam.getDisability().getValue());
employeeDeclare.setDisabilityCardNo(saveParam.getDisabilityCardNo());
employeeDeclare.setLonelyOld(saveParam.getLonelyOld().getValue());
employeeDeclare.setMartyrDependents(saveParam.getMartyrDependents().getValue());
employeeDeclare.setMartyrDependentsCardNo(saveParam.getMartyrDependentsCardNo());
employeeDeclare.setDeductExpenses(saveParam.getDeductExpenses().getValue());
employeeDeclare.setSuccessfullyDeclared(0);
employeeDeclare.setNewEmployeeInfo(0);
employeeDeclare.setDeclareStatus(DeclareStatusEnum.NOT_DECLARE.getValue());
employeeDeclare.setDeclareErrorMsg("");
TaxAgentPO taxAgent = getTaxAgentService(user).getById(saveParam.getTaxAgentId());
if (Objects.isNull(saveParam.getId())) {
employeeDeclare.setId(IdGenerator.generate());
employeeDeclare.setEmployeeId(saveParam.getEmployeeId());
employeeDeclare.setEmployeeType(saveParam.getEmployeeType().getValue());
employeeDeclare.setTenantKey(SalaryDefaultTenantConstant.DEFAULT_TENANT_KEY);
employeeDeclare.setCreator((long) user.getUID());
employeeDeclare.setDeleteType(DeleteTypeEnum.NOT_DELETED.getValue());
employeeDeclare.setCreateTime(now);
employeeDeclare.setUpdateTime(now);
getEmployeeDeclareMapper().insertIgnoreNull(employeeDeclare);
// 记录日志
LoggerContext loggerContext = new LoggerContext<>();
loggerContext.setUser(user);
loggerContext.setTargetId(Util.null2String(saveParam.getTaxAgentId()));
loggerContext.setTargetName((taxAgent == null ? "" : taxAgent.getName()) + "-" + employeeDeclare.getEmployeeName());
loggerContext.setOperateType(OperateTypeEnum.ADD.getValue());
loggerContext.setOperateTypeName(SalaryI18nUtil.getI18nLabel(0, "新增报送人员"));
loggerContext.setOperatedesc(SalaryI18nUtil.getI18nLabel(0, "新增报送人员:") + employeeDeclare.getEmployeeName());
loggerContext.setNewValues(employeeDeclare);
SalaryElogConfig.employeeDeclareLoggerTemplate.write(loggerContext);
} else {
EmployeeDeclarePO originEmployeeDeclare = getById(saveParam.getId());
if (Objects.isNull(originEmployeeDeclare)) {
throw new SalaryRunTimeException(SalaryI18nUtil.getI18nLabel(156442, "人员不存在或已被删除"));
}
if (Objects.equals(originEmployeeDeclare.getSuccessfullyDeclared(), 1)
&& !Objects.equals(originEmployeeDeclare.getEmployeeName(), employeeDeclare.getEmployeeName())
&& !Objects.equals(originEmployeeDeclare.getCardNum(), employeeDeclare.getCardNum())) {
throw new SalaryRunTimeException(SalaryI18nUtil.getI18nLabel(156443, "成功报送过的人员,姓名和证件号码只能修改其一,同时需要修改的请前往办税服务厅"));
}
employeeDeclare.setId(originEmployeeDeclare.getId());
employeeDeclare.setEmployeeId(originEmployeeDeclare.getEmployeeId());
employeeDeclare.setEmployeeType(originEmployeeDeclare.getEmployeeType());
employeeDeclare.setTenantKey(originEmployeeDeclare.getTenantKey());
employeeDeclare.setCreator(originEmployeeDeclare.getCreator());
employeeDeclare.setDeleteType(DeleteTypeEnum.NOT_DELETED.getValue());
employeeDeclare.setCreateTime(originEmployeeDeclare.getCreateTime());
employeeDeclare.setUpdateTime(now);
employeeDeclare.setSuccessfullyDeclared(originEmployeeDeclare.getSuccessfullyDeclared());
// 判断本次编辑是否有修改人员信息
if (!StringUtils.equals(employeeDeclare.toCompareString(), originEmployeeDeclare.toCompareString())) {
employeeDeclare.setNewEmployeeInfo(1);
}
getEmployeeDeclareMapper().update(employeeDeclare);
// 记录日志
LoggerContext loggerContext = new LoggerContext<>();
loggerContext.setUser(user);
loggerContext.setTargetId(Util.null2String(saveParam.getTaxAgentId()));
loggerContext.setTargetName((taxAgent == null ? "" : taxAgent.getName()) + "-" + employeeDeclare.getEmployeeName());
loggerContext.setOperateType(OperateTypeEnum.UPDATE.getValue());
loggerContext.setOperateTypeName(SalaryI18nUtil.getI18nLabel(0, "编辑报送人员"));
loggerContext.setOperatedesc(SalaryI18nUtil.getI18nLabel(0, "编辑报送人员:") + employeeDeclare.getEmployeeName());
loggerContext.setOldValues(originEmployeeDeclare);
loggerContext.setNewValues(employeeDeclare);
SalaryElogConfig.employeeDeclareLoggerTemplate.write(loggerContext);
}
}
@Override
public void batchUpdate(EmployeeDeclareBatchUpdateParam batchUpdateParam) {
// 查询报送人员
List employeeDeclares;
if (CollectionUtils.isNotEmpty(batchUpdateParam.getIds())) {
employeeDeclares = listByIds(batchUpdateParam.getIds());
} else {
employeeDeclares = listByParam(batchUpdateParam);
}
// 人员为空时,不允许批量编辑
if (CollectionUtils.isEmpty(employeeDeclares)) {
throw new SalaryRunTimeException(SalaryI18nUtil.getI18nLabel(182693, "当前人员列表为空,请选择要批量编辑的人员"));
}
Date now = new Date();
// 校验参数
if (Objects.equals(batchUpdateParam.getBatchUpdateItem(), "employmentStatus")) {
EmploymentStatusEnum employmentStatusEnum = null;
for (EmploymentStatusEnum value : EmploymentStatusEnum.values()) {
if (Objects.equals(value.name(), batchUpdateParam.getItemValue())) {
employmentStatusEnum = value;
}
}
if (employmentStatusEnum == null) {
throw new SalaryRunTimeException(SalaryI18nUtil.getI18nLabel(84026, "参数错误"));
}
for (EmployeeDeclarePO employeeDeclare : employeeDeclares) {
if (!Objects.equals(employeeDeclare.getEmploymentStatus(), employmentStatusEnum.getValue())) {
employeeDeclare.setEmploymentStatus(employmentStatusEnum.getValue());
employeeDeclare.setNewEmployeeInfo(1);
employeeDeclare.setDeclareStatus(DeclareStatusEnum.NOT_DECLARE.getValue());
employeeDeclare.setDeclareErrorMsg("");
employeeDeclare.setUpdateTime(now);
}
}
} else if (Objects.equals(batchUpdateParam.getBatchUpdateItem(), "employmentType")) {
EmploymentTypeEnum employmentTypeEnum = null;
for (EmploymentTypeEnum value : EmploymentTypeEnum.values()) {
if (Objects.equals(value.name(), batchUpdateParam.getItemValue())) {
employmentTypeEnum = value;
}
}
if (employmentTypeEnum == null) {
throw new SalaryRunTimeException(SalaryI18nUtil.getI18nLabel(84026, "参数错误"));
}
for (EmployeeDeclarePO employeeDeclare : employeeDeclares) {
if (!Objects.equals(employeeDeclare.getEmploymentType(), employmentTypeEnum.getValue())) {
employeeDeclare.setEmploymentType(employmentTypeEnum.getValue());
employeeDeclare.setNewEmployeeInfo(1);
employeeDeclare.setDeclareStatus(DeclareStatusEnum.NOT_DECLARE.getValue());
employeeDeclare.setDeclareErrorMsg("");
employeeDeclare.setUpdateTime(now);
}
}
} else if (Objects.equals(batchUpdateParam.getBatchUpdateItem(), "employmentDate")) {
if (!SalaryDateUtil.checkDay(batchUpdateParam.getItemValue())) {
throw new SalaryRunTimeException(SalaryI18nUtil.getI18nLabel(84026, "参数错误"));
}
Date employmentDate = SalaryDateUtil.localDateToDate(LocalDate.parse(batchUpdateParam.getItemValue()));
for (EmployeeDeclarePO employeeDeclare : employeeDeclares) {
if (!Objects.equals(employeeDeclare.getEmploymentDate(), employmentDate)) {
employeeDeclare.setEmploymentDate(employmentDate);
employeeDeclare.setNewEmployeeInfo(1);
employeeDeclare.setDeclareStatus(DeclareStatusEnum.NOT_DECLARE.getValue());
employeeDeclare.setDeclareErrorMsg("");
employeeDeclare.setUpdateTime(now);
}
}
} else if (Objects.equals(batchUpdateParam.getBatchUpdateItem(), "dismissDate")) {
if (!SalaryDateUtil.checkDay(batchUpdateParam.getItemValue())) {
throw new SalaryRunTimeException(SalaryI18nUtil.getI18nLabel(84026, "参数错误"));
}
Date dismissDate = SalaryDateUtil.localDateToDate(LocalDate.parse(batchUpdateParam.getItemValue()));
for (EmployeeDeclarePO employeeDeclare : employeeDeclares) {
if (!Objects.equals(employeeDeclare.getDismissDate(), dismissDate)) {
employeeDeclare.setDismissDate(dismissDate);
employeeDeclare.setNewEmployeeInfo(1);
employeeDeclare.setDeclareStatus(DeclareStatusEnum.NOT_DECLARE.getValue());
employeeDeclare.setDeclareErrorMsg("");
employeeDeclare.setUpdateTime(now);
}
}
} else if (Objects.equals(batchUpdateParam.getBatchUpdateItem(), "deductExpenses")) {
SalaryOnOffEnum salaryOnOffEnum = null;
for (SalaryOnOffEnum value : SalaryOnOffEnum.values()) {
if (Objects.equals(value.name(), batchUpdateParam.getItemValue())) {
salaryOnOffEnum = value;
}
}
if (salaryOnOffEnum == null) {
throw new SalaryRunTimeException(SalaryI18nUtil.getI18nLabel(84026, "参数错误"));
}
for (EmployeeDeclarePO employeeDeclare : employeeDeclares) {
if (!Objects.equals(employeeDeclare.getDeductExpenses(), salaryOnOffEnum.getValue())) {
employeeDeclare.setDeductExpenses(salaryOnOffEnum.getValue());
employeeDeclare.setNewEmployeeInfo(1);
employeeDeclare.setDeclareStatus(DeclareStatusEnum.NOT_DECLARE.getValue());
employeeDeclare.setDeclareErrorMsg("");
employeeDeclare.setUpdateTime(now);
}
}
}
employeeDeclares.forEach(getEmployeeDeclareMapper()::updateIgnoreNull);
// updateBatchById(employeeDeclares);
}
@Override
public void deleteByIds(Collection ids) {
List employeeDeclares = listByIds(ids);
if (CollectionUtils.isEmpty(employeeDeclares)) {
throw new SalaryRunTimeException(SalaryI18nUtil.getI18nLabel(156466, "参数错误,人员不存在或已被删除"));
}
boolean declareSuccess = employeeDeclares.stream().anyMatch(e -> Objects.equals(e.getSuccessfullyDeclared(), 1));
if (declareSuccess) {
// throw new SalaryRunTimeException(SalaryI18nUtil.getI18nLabel(156467, "报送成功过的人员不允许删除"));
}
getEmployeeDeclareMapper().deleteByIds(ids, new Date());
// 记录日志
List taxAgentPOS = getTaxAgentService(user).listAll();
Map taxAgentMap = SalaryEntityUtil.convert2Map(taxAgentPOS, TaxAgentPO::getId);
for (EmployeeDeclarePO employeeDeclare : employeeDeclares) {
TaxAgentPO taxAgent = taxAgentMap.get(employeeDeclare.getTaxAgentId());
LoggerContext loggerContext = new LoggerContext<>();
loggerContext.setUser(user);
loggerContext.setTargetId(Util.null2String(employeeDeclare.getTaxAgentId()));
loggerContext.setTargetName((taxAgent == null ? "" : taxAgent.getName()) + "-" + employeeDeclare.getEmployeeName());
loggerContext.setOperateType(OperateTypeEnum.DELETE.getValue());
loggerContext.setOperateTypeName(SalaryI18nUtil.getI18nLabel(0, "删除报送人员"));
loggerContext.setOperatedesc(SalaryI18nUtil.getI18nLabel(0, "删除报送人员:") + employeeDeclare.getEmployeeName());
loggerContext.setNewValues(employeeDeclare);
SalaryElogConfig.employeeDeclareLoggerTemplate.write(loggerContext);
}
}
@Override
public List convert(List employeeDeclares) {
if (CollectionUtils.isEmpty(employeeDeclares)) {
return Collections.emptyList();
}
// 查询个税扣缴义务人
Set taxAgentIds = SalaryEntityUtil.properties(employeeDeclares, EmployeeDeclarePO::getTaxAgentId);
List taxAgents = getTaxAgentService(user).listByIds(taxAgentIds);
List dtoList = EmployeeDeclareList.convert(employeeDeclares, taxAgents);
// 人员id集合
List employeeIds = employeeDeclares.stream().map(EmployeeDeclarePO::getEmployeeId).distinct().collect(Collectors.toList());
// 获取人员部门、分部详细信息
Map employeeInfoMap = SalaryEntityUtil.convert2Map(getSalaryEmployeeService(user).getEmployeeByIdsAll(employeeIds), emp -> emp.getEmployeeId() + "-" + emp.isExtEmp());
// 补充分部,部门
dtoList.forEach(po -> {
boolean isExtEmp = EmployeeTypeEnum.EXT_EMPLOYEE.getValue().compareTo(po.getEmployeeType().getValue()) == 0;
DataCollectionEmployee employee = employeeInfoMap.get(po.getEmployeeId() + "-" + isExtEmp);
if (ObjectUtils.isNotEmpty(employee)) {
po.setSubCompanyName(Optional.ofNullable(employee.getSubcompanyName()).orElse(""));
po.setDepartmentName(Optional.ofNullable(employee.getDepartmentName()).orElse(""));
}
});
return dtoList;
}
@Override
public List convert2FailListDTO(List employeeDeclares) {
if (CollectionUtils.isEmpty(employeeDeclares)) {
return Collections.emptyList();
}
// 查询人员信息
List employeeIds = SalaryEntityUtil.properties(employeeDeclares, EmployeeDeclarePO::getEmployeeId, Collectors.toList());
Map employeeInfoMap = SalaryEntityUtil.convert2Map(getSalaryEmployeeService(user).getEmployeeByIdsAll(employeeIds), emp -> emp.getEmployeeId() + "-" + emp.isExtEmp());
return EmployeeDeclareList.convert2FailListDTO(employeeDeclares, employeeInfoMap);
}
@Override
public void refresh(EmployeeDeclareRefreshParam refreshParam, Long employeeId) {
// 查询本月已有的需要报送的人员
List employeeDeclares = listByTaxCycleAndTaxAgentId(refreshParam.getTaxCycle(), refreshParam.getTaxAgentId());
// 查询上月已有报送的人员
List preTaxCycleEmployeeDeclares = listByTaxCycleAndTaxAgentId(SalaryDateUtil.plusMonths(refreshParam.getTaxCycle(), -1), refreshParam.getTaxAgentId())
//只需要正常的
.stream()
.filter(e -> Objects.equals(e.getEmploymentStatus(), EmploymentStatusEnum.NORMAL.getValue())
&& Objects.equals(e.getDeclareStatus(), DeclareStatusEnum.DECLARE_SUCCESS.getValue()))
.collect(Collectors.toList());
// 查询个税扣缴义务人
TaxAgentPO taxAgent = getTaxAgentService(user).getById(refreshParam.getTaxAgentId());
// 根据税款所属期计算出薪资周期
Date salaryCycle;
Date taxCycle = refreshParam.getTaxCycle();
// TODO:先临时写死本月!!!!需先完成个税扣缴义务人的改造
// SalaryCycleTypeEnum salaryCycleTypeEnum = SalaryEnumUtil.enumMatchByValue(taxAgent.getTaxCycleType(), SalaryCycleTypeEnum.class);
SalaryCycleTypeEnum salaryCycleTypeEnum = SalaryCycleTypeEnum.THIS_MONTH;
if (salaryCycleTypeEnum == SalaryCycleTypeEnum.BEFORE_LAST_MONTH) {
salaryCycle = SalaryDateUtil.plusMonths(taxCycle, 2);
} else if (salaryCycleTypeEnum == SalaryCycleTypeEnum.LAST_MONTH) {
salaryCycle = SalaryDateUtil.plusMonths(taxCycle, 1);
} else if (salaryCycleTypeEnum == SalaryCycleTypeEnum.NEXT_MONTH) {
salaryCycle = SalaryDateUtil.plusMonths(taxCycle, -1);
} else {
salaryCycle = taxCycle;
}
// 查询薪资档案的发薪人员
LocalDateRange salaryCycleRange = LocalDateRange.builder().fromDate(salaryCycle).endDate(SalaryDateUtil.localDateToDate(SalaryDateUtil.localDate2YearMonth(salaryCycle).atEndOfMonth())).build();
List salaryArchiveDataDTOS = getSalaryArchiveService(user).getSalaryArchiveTaxAgentData(salaryCycleRange, Collections.emptyList(), refreshParam.getTaxAgentId());
List employeeIds = salaryArchiveDataDTOS.stream().map(SalaryArchiveDataDTO::getEmployeeId).collect(Collectors.toList());
List salaryArchivePOS = getSalaryArchiveService(user).listSome(SalaryArchivePO.builder().taxAgentId(refreshParam.getTaxAgentId()).employeeIds(employeeIds).build());
// 本月增加了哪些人员
Set orgEmployeeIds = ((List) CollectionUtils.union(preTaxCycleEmployeeDeclares, employeeDeclares))
.stream()
.filter(e -> Objects.equals(e.getEmployeeType(), EmployeeTypeEnum.ORGANIZATION.getValue()))
.map(EmployeeDeclarePO::getEmployeeId)
.collect(Collectors.toSet());
Set newOrgEmployeeIds = salaryArchiveDataDTOS.stream()
.map(SalaryArchiveDataDTO::getEmployeeId)
.filter(e -> !orgEmployeeIds.contains(e))
.collect(Collectors.toSet());
// 查询新增加的人员信息
List employeeInfos = getSalaryEmployeeService(user).getEmployeeByIdsAll(new ArrayList<>(newOrgEmployeeIds));
// List newSimpleUserInfos = salaryEmployeeService.listByEmployeeIds(newOrgEmployeeIds);
// List newHrmEmployeeComInfos = comInfoCache.getCacheList(HrmEmployeeComInfo.class, Lists.newArrayList(newOrgEmployeeIds));
// 查询本月增加了哪些非系统人员
// List extEmployees = extEmployeeService.listBySalaryCycleAndTaxAgentId(salaryCycleRange, refreshParam.getTaxAgentId());
// Set extEmployeeIds = employeeDeclares.stream()
// .filter(e -> Objects.equals(e.getEmployeeType(), EmployeeTypeEnum.EXT_EMPLOYEE.getValue()))
// .map(EmployeeDeclarePO::getEmployeeId)
// .collect(Collectors.toSet());
// List newExtEmployees = extEmployees.stream()
// .filter(e -> !extEmployeeIds.contains(e.getId()))
// .collect(Collectors.toList());
// 本次新增的需要报送的人员
EmployeeDeclareRefreshDTO dto = new EmployeeDeclareRefreshDTO()
.setTaxAgentId(taxAgent.getId())
.setTaxCycle(taxCycle)
.setEmployeeDeclares(employeeDeclares)
.setPreTaxCycleEmployeeDeclare(preTaxCycleEmployeeDeclares)
.setSalaryArchives(salaryArchivePOS)
.setHrmEmployeeComInfos(employeeInfos);
EmployeeDeclareRefresh.Result result = EmployeeDeclareRefresh.refresh(dto, employeeId);
// 保存新增的人员
if (CollectionUtils.isNotEmpty(result.getNewEmployeeDeclares())) {
result.getNewEmployeeDeclares().forEach(getEmployeeDeclareMapper()::insertIgnoreNull);
}
// 更新已有人员
if (CollectionUtils.isNotEmpty(result.getEmployeeDeclares())) {
result.getEmployeeDeclares().forEach(getEmployeeDeclareMapper()::updateIgnoreNull);
}
// 记录日志
LoggerContext loggerContext = new LoggerContext<>();
loggerContext.setUser(user);
loggerContext.setTargetId(refreshParam.getTaxAgentId().toString());
loggerContext.setTargetName(SalaryI18nUtil.getI18nLabel(0, "个税扣缴义务人:「{0}」税款所属期:「{1}」").replace("{0}", taxAgent.getName()).replace("{1}", SalaryDateUtil.getFormatYearMonth(refreshParam.getTaxCycle())));
loggerContext.setOperateType(OperateTypeEnum.UPDATE.getValue());
loggerContext.setOperateTypeName(SalaryI18nUtil.getI18nLabel(0, "刷新数据"));
loggerContext.setOperatedesc(SalaryI18nUtil.getI18nLabel(0, "刷新数据"));
SalaryElogConfig.employeeDeclareLoggerTemplate.write(loggerContext);
}
@Override
public EmployeeDeclareInfoDTO getDeclareInfo(EmployeeDeclareParam employeeDeclareParam) {
// 查询本月已有的需要报送的人员
List employeeDeclares = listByTaxCycleAndTaxAgentId(employeeDeclareParam.getTaxCycle(), employeeDeclareParam.getTaxAgentId());
// 本月是否有修改过人员信息
boolean updateEmployeeInfo = employeeDeclares.stream().anyMatch(e -> Objects.equals(e.getNewEmployeeInfo(), 1) && Objects.equals(e.getDeclareStatus(), DeclareStatusEnum.NOT_DECLARE.getValue()));
// 本月报送失败的人数
Integer declareFailSize = (int) employeeDeclares.stream().filter(e -> Objects.equals(e.getDeclareStatus(), DeclareStatusEnum.DECLARE_FAIL.getValue())).count();
// 本月未报送的人数
Integer notDeclareSize = (int) employeeDeclares.stream().filter(e -> Objects.equals(e.getDeclareStatus(), DeclareStatusEnum.NOT_DECLARE.getValue())).count();
// 本月报送成功的人数
Integer declareSuccessSize = (int) employeeDeclares.stream().filter(e -> Objects.equals(e.getDeclareStatus(), DeclareStatusEnum.DECLARE_SUCCESS.getValue())).count();
return new EmployeeDeclareInfoDTO()
.setShowUpdate(updateEmployeeInfo)
.setDeclareFailSize(declareFailSize)
.setNotDeclareSize(notDeclareSize)
.setDeclareSuccessSize(declareSuccessSize);
}
@Override
public void declare(EmployeeDeclareParam param) {
// 获取api配置信息
TaxDeclarationApiConfigPO apiConfig = getTaxDeclarationApiConfigService(user).getConfig(true);
// 查询个税扣缴义务人
TaxAgentPO taxAgent = getTaxAgentService(user).getById(param.getTaxAgentId());
// 查询个税扣缴义务人关联的企业信息
TaxAgentTaxReturnPO taxAgentTaxReturn = getTaxAgentTaxReturnService(user).getByTaxAgentId(param.getTaxAgentId());
if (Objects.isNull(taxAgentTaxReturn) || !Objects.equals(taxAgentTaxReturn.getCheckStatus(), TaxAgentTaxReturnStatusEnum.SUCCESS.getValue())) {
throw new SalaryRunTimeException(SalaryI18nUtil.getI18nLabel(156469, "个税扣缴义务人的报税信息验证未通过,请先维护好个税扣缴义务人的报税信息"));
}
// 查询需要报送的人员
List employeeDeclares = listByTaxCycleAndTaxAgentId(param.getTaxCycle(), param.getTaxAgentId());
if (CollectionUtils.isEmpty(employeeDeclares)) {
throw new SalaryRunTimeException(SalaryI18nUtil.getI18nLabel(156470, "当前报送人员列表暂无数据"));
}
EmployeeDeclareRecordPO employeeDeclareRecord = getEmployeeDeclareRecordService(user).getByTaxCycleAndTaxAgentId(param.getTaxCycle(), param.getTaxAgentId());
if (employeeDeclareRecord != null && StringUtils.isNotEmpty(employeeDeclareRecord.getRequestId())) {
throw new SalaryRunTimeException(SalaryI18nUtil.getI18nLabel(156471, "报送中,稍后请点击【获取报送结果】更新报送状态"));
}
// 人员列表
List