weaver-hrm-salary/src/com/engine/salary/report/service/impl/SalaryStatisticsEmployeeSer...

369 lines
21 KiB
Java
Raw Normal View History

2023-05-09 10:43:51 +08:00
package com.engine.salary.report.service.impl;
import com.engine.common.util.ServiceUtil;
import com.engine.core.impl.Service;
import com.engine.salary.entity.datacollection.DataCollectionEmployee;
import com.engine.salary.entity.salaryacct.po.SalaryAcctEmployeePO;
import com.engine.salary.entity.salaryacct.po.SalaryAcctRecordPO;
2023-05-09 10:43:51 +08:00
import com.engine.salary.entity.salaryacct.po.SalaryAcctResultPO;
import com.engine.salary.entity.salaryitem.po.SalaryItemPO;
import com.engine.salary.entity.salarysob.po.SalarySobPO;
2023-05-09 10:43:51 +08:00
import com.engine.salary.entity.taxagent.po.TaxAgentPO;
import com.engine.salary.enums.UserStatusEnum;
2023-07-12 09:45:00 +08:00
import com.engine.salary.enums.datacollection.UseEmployeeTypeEnum;
2023-05-09 10:43:51 +08:00
import com.engine.salary.mapper.salaryacct.SalaryAcctEmployeeMapper;
import com.engine.salary.report.common.constant.SalaryConstant;
import com.engine.salary.report.entity.bo.SalaryStatisticsEmployeeBO;
import com.engine.salary.report.entity.dto.SalaryStatisticsEmployeeDetailResultDTO;
import com.engine.salary.report.entity.dto.SalaryStatisticsEmployeeListDTO;
import com.engine.salary.report.entity.param.SalaryStatisticsEmployeeDetailQueryParam;
import com.engine.salary.report.entity.param.SalaryStatisticsEmployeeQueryParam;
import com.engine.salary.report.service.SalaryStatisticsEmployeeService;
import com.engine.salary.service.*;
import com.engine.salary.service.impl.*;
import com.engine.salary.util.SalaryAssert;
import com.engine.salary.util.SalaryDateUtil;
import com.engine.salary.util.SalaryEntityUtil;
import com.engine.salary.util.SalaryI18nUtil;
import com.engine.salary.util.db.MapperProxyFactory;
import com.engine.salary.util.page.PageInfo;
import com.engine.salary.util.page.SalaryPageUtil;
import com.google.common.collect.Lists;
import com.google.common.collect.Maps;
import com.google.common.collect.Sets;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.collections4.CollectionUtils;
import org.apache.commons.lang3.StringUtils;
import weaver.hrm.User;
import java.util.*;
import java.util.stream.Collectors;
/**
* 薪酬统计员工明细
* <p>Copyright: Copyright (c) 2022</p>
* <p>Company: 泛微软件</p>
*
* @author qiantao
* @version 1.0
**/
@Slf4j
public class SalaryStatisticsEmployeeServiceImpl extends Service implements SalaryStatisticsEmployeeService {
private SalaryAcctEmployeeMapper getSalaryAcctEmployeeMapper() {
return MapperProxyFactory.getProxy(SalaryAcctEmployeeMapper.class);
}
private SalaryAcctEmployeeService getSalaryAcctEmployeeService(User user) {
return ServiceUtil.getService(SalaryAcctEmployeeServiceImpl.class, user);
}
private SalaryAcctResultService getSalaryAcctResultService(User user) {
return ServiceUtil.getService(SalaryAcctResultServiceImpl.class, user);
}
private SalaryItemService getSalaryItemService(User user) {
return ServiceUtil.getService(SalaryItemServiceImpl.class, user);
}
private SalarySobService getSalarySobService(User user) {
return ServiceUtil.getService(SalarySobServiceImpl.class, user);
}
private SalaryAcctRecordService getSalaryAcctRecordService(User user) {
return ServiceUtil.getService(SalaryAcctRecordServiceImpl.class, user);
}
2023-05-09 10:43:51 +08:00
// private ExtEmployeeMapper extEmployeeMapper;
private TaxAgentService getTaxAgentService(User user) {
return ServiceUtil.getService(TaxAgentServiceImpl.class, user);
}
private SalaryEmployeeService getSalaryEmployeeService(User user) {
return ServiceUtil.getService(SalaryEmployeeServiceImpl.class, user);
}
@Override
public PageInfo<SalaryStatisticsEmployeeListDTO> listPage(SalaryStatisticsEmployeeQueryParam queryParam) {
List<SalaryStatisticsEmployeeListDTO> list = Collections.emptyList();
PageInfo<SalaryStatisticsEmployeeListDTO> page = SalaryPageUtil.buildPage(queryParam.getCurrent(), queryParam.getPageSize(), list, SalaryStatisticsEmployeeListDTO.class);
2023-05-09 10:43:51 +08:00
// 1.分权处理, 首先获取个税扣缴义务人参数
Collection<TaxAgentPO> taxAgentViews = getTaxAgentService(user).listAllTaxAgents((long) user.getUID());
List<Long> taxAgentIds = Objects.isNull(taxAgentViews) ? Lists.newArrayList() : taxAgentViews.stream().map(TaxAgentPO::getId).collect(Collectors.toList());
if (CollectionUtils.isEmpty(taxAgentIds)) {
return page;
}
// 2.年月参数处理注意薪资所属月居然是用字符串存储的无法通过sql between处理
Set<Date> salaryMonths = SalaryStatisticsEmployeeBO.getSalaryMonths(queryParam.getYear(), queryParam.getSalaryMonth())
.stream()
.map(SalaryDateUtil::dateStrToLocalYearMonth)
.collect(Collectors.toSet());
// 查询薪资核算人员
List<SalaryAcctEmployeePO> salaryAcctEmployeeList = getSalaryAcctEmployeeService(user).listByTaxAgentAndSalaryMonth(taxAgentIds, salaryMonths);
if (CollectionUtils.isEmpty(salaryAcctEmployeeList)) {
return page;
}
// if (queryParam.getEmployeeType() != null) {
// salaryAcctEmployeeList = salaryAcctEmployeeList.stream().filter(s -> queryParam.getEmployeeType().getValue().equals(s.getEmployeeType())).collect(Collectors.toList());
// }
// // 外部人员id
// Set<Long> extEmployeeIds = Sets.newHashSet();
// 内部人员id
Set<Long> innerEmployeeIds = Sets.newHashSet();
for (SalaryAcctEmployeePO sae : salaryAcctEmployeeList) {
// if (EmployeeTypeEnum.EXT_EMPLOYEE.getValue().equals(sae.getEmployeeType())) {
// extEmployeeIds.add(sae.getEmployeeId());
// } else if (EmployeeTypeEnum.ORGANIZATION.getValue().equals(sae.getEmployeeType())) {
// innerEmployeeIds.add(sae.getEmployeeId());
// }
innerEmployeeIds.add(sae.getEmployeeId());
}
// 3.关键字搜索参数
if (StringUtils.isNotEmpty(queryParam.getKeyword())) {
// if (CollectionUtils.isNotEmpty(extEmployeeIds)) {
// // 查询外部人员
// List<ExtEmployeePO> extEmployeeList = new LambdaQueryChainWrapper<>(extEmployeeMapper)
// .eq(ExtEmployeePO::getTenantKey, tenantKey)
// .eq(ExtEmployeePO::getDeleteType, 0)
// .list();
// Set<Long> finalExtEmployeeIds = extEmployeeIds;
// extEmployeeIds = extEmployeeList.stream()
// .filter(e -> finalExtEmployeeIds.contains(e.getId()) && (e.getUsername().contains(queryParam.getKeyword()) || (StringUtils.isNotEmpty(e.getCardNum()) && e.getCardNum().contains(queryParam.getKeyword()))))
// .map(ExtEmployeePO::getId)
// .collect(Collectors.toSet());
// }
if (CollectionUtils.isNotEmpty(innerEmployeeIds)) {
2023-07-12 09:45:00 +08:00
List<DataCollectionEmployee> salaryEmployees = getSalaryEmployeeService(user).listAll(UseEmployeeTypeEnum.ALL);
2023-05-09 10:43:51 +08:00
Set<Long> finalInnerEmployeeIds = innerEmployeeIds;
innerEmployeeIds = salaryEmployees.stream()
.filter(e -> finalInnerEmployeeIds.contains(e.getEmployeeId()) && (e.getUsername().contains(queryParam.getKeyword()) || (StringUtils.isNotEmpty(e.getWorkcode()) && e.getWorkcode().contains(queryParam.getKeyword()))))
.map(DataCollectionEmployee::getEmployeeId)
.collect(Collectors.toSet());
// Map<Long, String> idNoMap = idNoMapByEmployeeIds(finalInnerEmployeeIds);
// for (Long k : idNoMap.keySet()) {
// if (idNoMap.get(k) != null && idNoMap.get(k).contains(queryParam.getKeyword())) {
// innerEmployeeIds.addAll(idNoMap.keySet());
// }
// }
}
}
List<Long> innerEmployeeIdList = innerEmployeeIds.stream().sorted(Comparator.comparing(e -> e)).collect(Collectors.toList());
// List<Long> extEmployeeIdList = extEmployeeIds.stream().sorted(Comparator.comparing(e -> e)).collect(Collectors.toList());
// 排序,内部员工优先
list = innerEmployeeIdList.stream().map(e -> SalaryStatisticsEmployeeListDTO.builder()
.id(e)
// .employeeType(EmployeeTypeEnum.ORGANIZATION.getValue())
.build()).collect(Collectors.toList());
// list.addAll(extEmployeeIdList.stream().map(e -> SalaryStatisticsEmployeeListDTO.builder()
// .id(e)
// .employeeType(EmployeeTypeEnum.EXT_EMPLOYEE.getValue())
// .build()).collect(Collectors.toList()));
// 第一页数据显示处理
page = SalaryPageUtil.buildPage(queryParam.getCurrent(), queryParam.getPageSize(), list, SalaryStatisticsEmployeeListDTO.class);
List<SalaryStatisticsEmployeeListDTO> salaryStatisticsEmployeeListDTOs = page.getList();
List<Long> employeeIds = salaryStatisticsEmployeeListDTOs.stream().map(SalaryStatisticsEmployeeListDTO::getId).collect(Collectors.toList());
// 查询外部人员
// List<ExtEmployeePO> extEmployeeList = CollectionUtils.isEmpty(extEmployeeIdList) ? Lists.newArrayList() : new LambdaQueryChainWrapper<>(extEmployeeMapper)
// .eq(ExtEmployeePO::getTenantKey, tenantKey)
// .eq(ExtEmployeePO::getDeleteType, 0)
// .in(ExtEmployeePO::getId, extEmployeeIdList)
// .list();
// Map<Long, ExtEmployeePO> extEmployeeMap = extEmployeeList.stream().collect(Collectors.toMap(ExtEmployeePO::getId, v -> v));
List<DataCollectionEmployee> simpleEmployeeList = getSalaryEmployeeService(user).getEmployeeByIdsAll(employeeIds);
2023-05-09 10:43:51 +08:00
Map<Long, DataCollectionEmployee> innerEmployeeMap = simpleEmployeeList.stream().collect(Collectors.toMap(DataCollectionEmployee::getEmployeeId, v -> v));
salaryStatisticsEmployeeListDTOs.forEach(e -> {
DataCollectionEmployee simpleEmployee = innerEmployeeMap.get(e.getId());
e.setName(Objects.isNull(simpleEmployee) ? "" : simpleEmployee.getUsername());
e.setSubCompany(simpleEmployee.getSubcompanyName());
e.setDepartment(simpleEmployee.getDepartmentName());
e.setPosition(simpleEmployee.getJobtitleName());
e.setStatus(UserStatusEnum.getDefaultLabelByValue(Integer.parseInt(simpleEmployee.getStatus())));
2023-05-09 10:43:51 +08:00
e.setJobNum(simpleEmployee.getWorkcode());
e.setIdNo(simpleEmployee.getIdNo());
});
page.setList(salaryStatisticsEmployeeListDTOs);
return page;
}
@Override
public SalaryStatisticsEmployeeDetailResultDTO getDetailSalaryAcctResult(SalaryStatisticsEmployeeDetailQueryParam queryParam) {
SalaryAssert.notNull(queryParam.getEmployeeId(), SalaryI18nUtil.getI18nLabel(163974, "人员id不能为空"));
// 薪资所属月参数,如果已经有年的就取交集
Set<Date> salaryMonths = SalaryStatisticsEmployeeBO.getSalaryMonths(queryParam.getYear(), queryParam.getSalaryMonth()).stream().map(SalaryDateUtil::dateStrToLocalYearMonth).collect(Collectors.toSet());
// 1.获取该员工所有核算人员数据
SalaryAcctEmployeePO build = SalaryAcctEmployeePO.builder().employeeId(queryParam.getEmployeeId()).build();
if (CollectionUtils.isNotEmpty(queryParam.getIds())) {
build.setIds(queryParam.getIds());
}
if (CollectionUtils.isNotEmpty(salaryMonths)) {
build.setSalaryMonths(salaryMonths);
}
if (Objects.nonNull(queryParam.getTaxAgentId())) {
build.setTaxAgentId(queryParam.getTaxAgentId());
}
List<SalaryAcctEmployeePO> salaryAcctEmployees = getSalaryAcctEmployeeMapper().listSome(build);
salaryAcctEmployees = salaryAcctEmployees.stream().sorted(Comparator.comparing(SalaryAcctEmployeePO::getSalaryMonth)).collect(Collectors.toList());
Collections.reverse(salaryAcctEmployees);
// 2.获取核算结果数据
2023-05-25 13:51:27 +08:00
List<Long> salaryAcctEmployeeIds = salaryAcctEmployees.stream().map(SalaryAcctEmployeePO::getId).collect(Collectors.toList());
2023-05-09 10:43:51 +08:00
List<SalaryAcctResultPO> salaryAcctResultValues = getSalaryAcctResultService(user).listBySalaryAcctEmployeeIds(salaryAcctEmployeeIds);
// 3.获取薪资项目
Map<String, String> resultMap = Maps.newHashMap();
salaryAcctResultValues.forEach(sv -> {
resultMap.put(sv.getSalaryItemId() + "", sv.getResultValue());
});
List<Long> salaryItemIds = resultMap.keySet().stream().map(Long::valueOf).collect(Collectors.toList());
List<SalaryItemPO> salaryItemList = CollectionUtils.isEmpty(salaryItemIds) ? Lists.newArrayList() : getSalaryItemService(user).listByIds(salaryItemIds);
return SalaryStatisticsEmployeeDetailResultDTO.builder()
.salaryAcctEmployeeList(salaryAcctEmployees)
.salaryAcctResultValueList(salaryAcctResultValues)
.salaryItemList(salaryItemList)
.build();
}
@Override
public List<Map<String, Object>> listDetailPage(SalaryStatisticsEmployeeDetailResultDTO salaryStatisticsEmployeeDetailResult, SalaryStatisticsEmployeeDetailQueryParam queryParam) {
List<TaxAgentPO> taxAgentList = getTaxAgentService(user).listAll();
Map<Long, String> taxAgentMap = SalaryEntityUtil.convert2Map(taxAgentList, TaxAgentPO::getId, TaxAgentPO::getName);
List<SalarySobPO> salarySobPOS = getSalarySobService(user).listAll();
Map<Long, String> SalarySobMap = SalaryEntityUtil.convert2Map(salarySobPOS, SalarySobPO::getId, SalarySobPO::getName);
List<SalaryAcctRecordPO> salaryAcctRecordPOS = getSalaryAcctRecordService(user).listAll();
Map<Long, Integer> salaryAcctRecordMap = SalaryEntityUtil.convert2Map(salaryAcctRecordPOS, SalaryAcctRecordPO::getId, SalaryAcctRecordPO::getAcctTimes);
2023-05-09 10:43:51 +08:00
Map<Long, List<SalaryAcctResultPO>> acctResultValueList = SalaryEntityUtil.group2Map(salaryStatisticsEmployeeDetailResult.getSalaryAcctResultValueList(), SalaryAcctResultPO::getSalaryAcctEmpId);
Map<Long, Map<String, String>> acctResultValueMap = new HashMap<>();
acctResultValueList.forEach((k, v) -> {
Map<String, String> map = new HashMap();
v.forEach(l -> {
map.put(l.getSalaryItemId() + "", l.getResultValue());
});
acctResultValueMap.put(k, map);
});
List<Map<String, Object>> list = Lists.newArrayList();
Map<String, Object> map;
for (SalaryAcctEmployeePO se : salaryStatisticsEmployeeDetailResult.getSalaryAcctEmployeeList()) {
map = Maps.newHashMap();
Map<String, String> resultValueMap = Optional.ofNullable(acctResultValueMap.get(se.getId())).orElse(Maps.newHashMap());
Map<String, Object> finalMap = map;
resultValueMap.forEach((k, v) -> {
finalMap.put(k + SalaryConstant.DYNAMIC_SUFFIX, v);
});
map.put("id", se.getId().toString());
map.put("salaryMonth", SalaryDateUtil.getFormatYearMonth(se.getSalaryMonth()));
2023-05-09 10:43:51 +08:00
map.put("taxAgent", taxAgentMap.get(se.getTaxAgentId()));
map.put("salarySob",SalarySobMap.get(se.getSalarySobId()));
map.put("acctTimes", salaryAcctRecordMap.get(se.getSalaryAcctRecordId()));
2023-05-09 10:43:51 +08:00
// IncomeCategoryEnum incomeCategoryEnum = IncomeCategoryEnum.parseByValue(Integer.parseInt(se.getIncomeCategory()));
// map.put("incomeCategory", Objects.isNull(incomeCategoryEnum) ? "" : SalaryI18nUtil.getI18nLabel(incomeCategoryEnum.getLabelId(), incomeCategoryEnum.getDefaultLabel()));
list.add(map);
}
return list;
}
// @Override
// public void exportDetailList(Map<String, Object> map, SalaryStatisticsEmployeeDetailQueryParam queryParam) {
// DataCollectionEmployee employee = getSalaryEmployeeService(user).getEmployeeById(queryParam.getEmployeeId());
// // 获取核算数据
// SalaryStatisticsEmployeeDetailResultDTO salaryStatisticsEmployeeDetailResult;
// if (Objects.isNull(employee)) {
// salaryStatisticsEmployeeDetailResult = SalaryStatisticsEmployeeDetailResultDTO.builder()
// .salaryAcctEmployeeList(Lists.newArrayList())
// .salaryAcctResultValueList(Lists.newArrayList())
// .salaryItemList(Lists.newArrayList())
// .build();
// } else {
// salaryStatisticsEmployeeDetailResult = this.getDetailSalaryAcctResult(queryParam);
// }
//
// String nameI18n = (StringUtils.isEmpty(employee.getUsername()) ? "" : "[" + employee.getUsername() + "]") + SalaryI18nUtil.getI18nLabel(tenantKey, employeeId, 177855, "薪资明细表");
//
// List<ExcelSheetData> sheetList = new ArrayList<>();
// ExcelSheetData excelSheetData = new ExcelSheetData();
// // 1.工作簿名称
// excelSheetData.setSheetName(nameI18n);
// List<String> headerList = Lists.newArrayList();
// headerList.add(SalaryI18nUtil.getI18nLabel(tenantKey, employeeId, 87614, "薪资所属月"));
// headerList.add(SalaryI18nUtil.getI18nLabel(tenantKey, employeeId, 86184, "个税扣缴义务人"));
// headerList.add(SalaryI18nUtil.getI18nLabel(tenantKey, employeeId, 121908, "收入所得项目"));
// salaryStatisticsEmployeeDetailResult.getSalaryItemList().forEach(item -> headerList.add(item.getName()));
// // 2.表头
// excelSheetData.setHeaders(Collections.singletonList(headerList.toArray(new String[]{})));
//
// List<TaxAgentPO> taxAgentList = getTaxAgentService(user).listAll(tenantKey);
// Map<Long, String> taxAgentMap = SalaryEntityUtil.convert2Map(taxAgentList, TaxAgentPO::getId, TaxAgentPO::getName);
//
// Map<Long, Map<String, String>> acctResultValueMap = SalaryEntityUtil.convert2Map(salaryStatisticsEmployeeDetailResult.getSalaryAcctResultValueList(), SalaryAcctResultPO::getSalaryAcctEmployeeId, SalaryAcctResultPO::getResultValue);
//
// // 组装数据
// List<List<Object>> rows = new ArrayList<>();
// for (SalaryAcctEmployeePO se : salaryStatisticsEmployeeDetailResult.getSalaryAcctEmployeeList()) {
// if (CollectionUtils.isNotEmpty(queryParam.getIds()) && !queryParam.getIds().contains(se.getId())) {
// continue;
// }
// List<Object> row = new ArrayList<>();
// row.add(se.getSalaryMonth());
// row.add(taxAgentMap.get(se.getTaxAgentId()));
// IncomeCategoryEnum incomeCategoryEnum = IncomeCategoryEnum.parseByValue(Integer.parseInt(se.getIncomeCategory()));
// row.add(Objects.isNull(incomeCategoryEnum) ? "" : SalaryI18nUtil.getI18nLabel(incomeCategoryEnum.getLabelId(), incomeCategoryEnum.getDefaultLabel()));
// // 薪资项目
// Map<String, String> resultValueMap = Optional.ofNullable(acctResultValueMap.get(se.getId())).orElse(Maps.newHashMap());
// salaryStatisticsEmployeeDetailResult.getSalaryItemList().forEach(item -> row.add(Optional.ofNullable(resultValueMap.get(item.getId().toString())).orElse("")));
//
// rows.add(row);
// }
// if (CollectionUtils.isNotEmpty(rows)) {
// List<Object> countRow = new ArrayList<>();
// countRow.add(SalaryI18nUtil.getI18nLabel(tenantKey, employeeId, 93278, "合计"));
// countRow.add("-");
// countRow.add("-");
//
// for (int i = 3; i < headerList.size(); i++) {
// BigDecimal sumBigDecimal = new BigDecimal(SalaryStatisticsReportBO.D_ZERO);
// for (List<Object> row : rows) {
// sumBigDecimal = sumBigDecimal.add(new BigDecimal(StringUtils.isEmpty(row.get(i).toString()) ? SalaryStatisticsReportBO.ZERO : row.get(i).toString()));
// }
// countRow.add(sumBigDecimal.toString());
// }
// rows.add(countRow);
// }
//
// // 3.表数据
// excelSheetData.setRows(rows);
//
// sheetList.add(excelSheetData);
//
// salaryBatchService.simpleExportExcel(ExportExcelInfo.builder()
//// .sharePassword(queryParam.getSharePassword())
// .bizId(map.get("biz").toString())
// .flag(true)
// .userId(employeeId)
// .eteamsId(map.get("eteamsId").toString())
// .tenantKey(tenantKey)
// .operator(map.get("username").toString())
// .module(map.get("module").toString())
// .fileName(nameI18n + ReportTimeUtil.getFormatLocalDateTime(LocalDateTime.now()))
// .handlerName("exportSalaryStatisticsEmployeeDetailList")
// .dataType(nameI18n)
// .function("exportSalaryStatisticsEmployeeDetailList").build(), sheetList);
// }
}