weaver-hrm-salary/src/com/engine/salary/service/impl/SalaryAcctExcelServiceImpl....

814 lines
43 KiB
Java
Raw Normal View History

2022-04-12 19:25:19 +08:00
package com.engine.salary.service.impl;
import com.cloudstore.eccom.pc.table.WeaTableColumn;
import com.engine.common.util.ServiceUtil;
import com.engine.core.impl.Service;
import com.engine.salary.biz.TaxAgentBiz;
2022-05-05 14:10:19 +08:00
import com.engine.salary.component.WeaTableColumnGroup;
2022-04-19 17:21:27 +08:00
import com.engine.salary.constant.SalaryDefaultTenantConstant;
2022-04-12 19:25:19 +08:00
import com.engine.salary.entity.datacollection.DataCollectionEmployee;
import com.engine.salary.entity.salaryacct.bo.SalaryAcctEmployeeBO;
import com.engine.salary.entity.salaryacct.bo.SalaryAcctResultBO;
import com.engine.salary.entity.salaryacct.dto.SalaryAccEmployeeListDTO;
import com.engine.salary.entity.salaryacct.dto.SalaryAcctImportFieldDTO;
2022-04-13 14:18:00 +08:00
import com.engine.salary.entity.salaryacct.dto.SalaryComparisonResultListDTO;
2022-04-18 17:28:34 +08:00
import com.engine.salary.entity.salaryacct.param.*;
2022-04-19 17:21:27 +08:00
import com.engine.salary.entity.salaryacct.po.ExcelAcctResultPO;
2022-04-12 19:25:19 +08:00
import com.engine.salary.entity.salaryacct.po.SalaryAcctEmployeePO;
import com.engine.salary.entity.salaryacct.po.SalaryAcctRecordPO;
2022-04-19 17:21:27 +08:00
import com.engine.salary.entity.salaryacct.po.SalaryAcctResultPO;
2022-04-12 19:25:19 +08:00
import com.engine.salary.entity.salaryitem.po.SalaryItemPO;
import com.engine.salary.entity.salarysob.dto.SalarySobItemAggregateDTO;
2022-04-18 17:28:34 +08:00
import com.engine.salary.entity.salarysob.dto.SalarySobItemDTO;
import com.engine.salary.entity.salarysob.dto.SalarySobItemGroupDTO;
2022-04-13 14:18:00 +08:00
import com.engine.salary.entity.salarysob.po.SalarySobEmpFieldPO;
2022-04-12 19:25:19 +08:00
import com.engine.salary.entity.salarysob.po.SalarySobItemPO;
import com.engine.salary.entity.taxrate.TaxAgent;
import com.engine.salary.enums.SalaryValueTypeEnum;
import com.engine.salary.exception.SalaryRunTimeException;
import com.engine.salary.service.*;
import com.engine.salary.util.SalaryEntityUtil;
import com.engine.salary.util.SalaryI18nUtil;
2022-04-19 17:21:27 +08:00
import com.engine.salary.util.excel.ExcelParseHelper;
import com.engine.salary.util.excel.ExcelSupport;
2022-04-12 19:25:19 +08:00
import com.engine.salary.util.excel.ExcelUtil;
2022-04-18 17:28:34 +08:00
import com.engine.salary.util.valid.ValidUtil;
2022-04-12 19:25:19 +08:00
import com.google.common.collect.Lists;
2022-04-19 17:21:27 +08:00
import com.google.common.collect.Maps;
import com.google.common.collect.Sets;
2022-04-12 19:25:19 +08:00
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.collections4.CollectionUtils;
import org.apache.commons.lang3.StringUtils;
2022-04-19 17:21:27 +08:00
import org.apache.poi.ss.usermodel.Sheet;
2022-04-19 19:32:09 +08:00
import org.apache.poi.util.IOUtils;
2022-04-12 19:25:19 +08:00
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
2022-04-19 17:21:27 +08:00
import weaver.file.ImageFileManager;
2022-04-12 19:25:19 +08:00
import weaver.hrm.User;
2022-04-19 17:21:27 +08:00
import java.io.InputStream;
2022-04-12 19:25:19 +08:00
import java.util.*;
import java.util.stream.Collectors;
2022-04-19 17:21:27 +08:00
import static com.engine.salary.util.excel.ExcelSupport.EXCEL_TYPE_XLSX;
2022-04-12 19:25:19 +08:00
/**
* 薪资核算导入导出
* <p>Copyright: Copyright (c) 2022</p>
* <p>Company: 泛微软件</p>
*
* @author qiantao
* @version 1.0
**/
@Slf4j
public class SalaryAcctExcelServiceImpl extends Service implements SalaryAcctExcelService {
private SalaryAcctRecordService getSalaryAcctRecordService(User user) {
return (SalaryAcctRecordService) ServiceUtil.getService(SalaryAcctRecordServiceImpl.class, user);
}
private SalaryAcctEmployeeService getSalaryAcctEmployeeService(User user) {
return (SalaryAcctEmployeeService) ServiceUtil.getService(SalaryAcctEmployeeServiceImpl.class, user);
}
private SalaryAcctResultService getSalaryAcctResultService(User user) {
return (SalaryAcctResultService) ServiceUtil.getService(SalaryAcctResultServiceImpl.class, user);
}
private SalarySobEmpFieldService getSalarySobEmpFieldService(User user) {
return (SalarySobEmpFieldService) ServiceUtil.getService(SalarySobEmpFieldServiceImpl.class, user);
}
private SalarySobItemService getSalarySobItemService(User user) {
return (SalarySobItemService) ServiceUtil.getService(SalarySobItemServiceImpl.class, user);
}
private SalaryItemService getSalaryItemService(User user) {
return (SalaryItemService) ServiceUtil.getService(SalaryItemServiceImpl.class, user);
}
// private SalaryFormulaService getSalaryFormulaService(User user) {
// return (SalaryFormulaService) ServiceUtil.getService(SalaryFormulaServiceImpl.class, user);
// }
2022-04-13 14:18:00 +08:00
private SalaryComparisonResultService getSalaryComparisonResultService(User user) {
return (SalaryComparisonResultService) ServiceUtil.getService(SalaryComparisonResultServiceImpl.class, user);
}
2022-04-12 19:25:19 +08:00
private TaxAgentBiz taxAgentService = new TaxAgentBiz();
private SalaryEmployeeService getSalaryEmployeeService(User user) {
return (SalaryEmployeeService) ServiceUtil.getService(SalaryEmployeeServiceImpl.class, user);
}
private SalaryCheckResultService salaryCheckResultService;
private SalaryCheckResultDetailService salaryCheckResultDetailService;
private SalarySobCheckRuleService salarySobCheckRuleService;
@Override
public XSSFWorkbook exportSalaryAcctEmployee(SalaryAcctEmployeeQueryParam queryParam) {
// 1.工作簿名称
String sheetName = SalaryI18nUtil.getI18nLabel(85368, "核算人员范围");
// 2.表头
String[] header = {
SalaryI18nUtil.getI18nLabel(85429, "姓名"),
SalaryI18nUtil.getI18nLabel(86184, "个税扣缴义务人"),
SalaryI18nUtil.getI18nLabel(86185, "部门"),
SalaryI18nUtil.getI18nLabel(86186, "手机号"),
SalaryI18nUtil.getI18nLabel(86186, "工号"),
SalaryI18nUtil.getI18nLabel(91075, "员工状态"),
SalaryI18nUtil.getI18nLabel(91075, "入职日期"),
SalaryI18nUtil.getI18nLabel(91075, "离职日期")
};
List<Object> headerList = new ArrayList<>(Arrays.asList(header));
// 查询薪资核算人员
List<SalaryAcctEmployeePO> salaryAcctEmployees = getSalaryAcctEmployeeService(user).listByParam(queryParam);
// 组装数据
List<List<Object>> rows = new ArrayList<>();
rows.add(headerList);
// 3.表数据
List<List<Object>> lists = convert2ExcelRow(salaryAcctEmployees);
rows.addAll(lists);
return ExcelUtil.genWorkbookV2(rows, sheetName);
}
@Override
public XSSFWorkbook exportReducedEmployee(SalaryAcctEmployeeQueryParam queryParam) {
// 1.工作簿名称
String sheetName = SalaryI18nUtil.getI18nLabel(85368, "环比上月减少人员");
// 2.表头
String[] header = {
SalaryI18nUtil.getI18nLabel(85429, "姓名"),
SalaryI18nUtil.getI18nLabel(86184, "个税扣缴义务人"),
SalaryI18nUtil.getI18nLabel(86185, "部门"),
SalaryI18nUtil.getI18nLabel(86186, "手机号"),
SalaryI18nUtil.getI18nLabel(86186, "工号"),
SalaryI18nUtil.getI18nLabel(91075, "员工状态"),
SalaryI18nUtil.getI18nLabel(91075, "入职日期"),
SalaryI18nUtil.getI18nLabel(91075, "离职日期")
};
List<Object> headerList = new ArrayList<>(Arrays.asList(header));
// 查询薪资核算人员(环比上月减少)
List<SalaryAcctEmployeePO> salaryAcctEmployees = getSalaryAcctEmployeeService(user).listByParam4Reduce(queryParam);
// 组装数据
List<List<Object>> rows = new ArrayList<>();
rows.add(headerList);
// 3.表数据
List<List<Object>> lists = convert2ExcelRow(salaryAcctEmployees);
rows.addAll(lists);
return ExcelUtil.genWorkbookV2(rows, sheetName);
}
private List<List<Object>> convert2ExcelRow(List<SalaryAcctEmployeePO> salaryAcctEmployees) {
if (CollectionUtils.isEmpty(salaryAcctEmployees)) {
return Collections.emptyList();
}
List<SalaryAccEmployeeListDTO> salaryAccEmployeeListDTOS = new ArrayList<>();
List<List<SalaryAcctEmployeePO>> partition = Lists.partition(salaryAcctEmployees, 1000);
for (List<SalaryAcctEmployeePO> tempList : partition) {
// 人员
List<Long> employeeIds = tempList.stream().map(SalaryAcctEmployeePO::getEmployeeId).collect(Collectors.toList());
List<DataCollectionEmployee> simpleEmployees = getSalaryEmployeeService(user).listByIds(employeeIds);
// 个税扣缴义务人
List<Long> taxAgentIds = tempList.stream().map(SalaryAcctEmployeePO::getTaxAgentId).distinct().collect(Collectors.toList());
List<TaxAgent> taxAgents = taxAgentService.listByIds(taxAgentIds);
// 转换成列表数据
salaryAccEmployeeListDTOS.addAll(SalaryAcctEmployeeBO.convert2EmployeeListDTO(tempList, taxAgents, simpleEmployees));
}
List<List<Object>> rows = Lists.newArrayListWithExpectedSize(salaryAccEmployeeListDTOS.size());
for (SalaryAccEmployeeListDTO dto : salaryAccEmployeeListDTOS) {
List<Object> row = Lists.newArrayListWithExpectedSize(8);
row.add(dto.getEmployeeName());
row.add(dto.getTaxAgentName());
row.add(dto.getDepartmentName());
row.add(dto.getMobile());
row.add(dto.getJobNum());
row.add(dto.getStatus());
row.add(dto.getHireDate());
row.add(dto.getDismissDate());
rows.add(row);
}
return rows;
}
@Override
public XSSFWorkbook exportSalaryAcctResult(SalaryAcctResultQueryParam queryParam) {
// 查询薪资核算记录
SalaryAcctRecordPO salaryAcctRecordPO = getSalaryAcctRecordService(user).getById(queryParam.getSalaryAcctRecordId());
if (Objects.isNull(salaryAcctRecordPO)) {
throw new SalaryRunTimeException(SalaryI18nUtil.getI18nLabel(98747, "薪资核算记录不存在或已被删除"));
}
2022-05-05 14:10:19 +08:00
// excel导出的表头
List<WeaTableColumnGroup> headerColumnGroup = Lists.newArrayList();
2022-04-12 19:25:19 +08:00
// 查询列表的表头
2022-05-05 14:10:19 +08:00
List<WeaTableColumnGroup> weaTableColumns = listWeaTableColumn(salaryAcctRecordPO);
parseHeader(headerColumnGroup, weaTableColumns);
2022-04-12 19:25:19 +08:00
// excel导出的表头
2022-05-05 14:10:19 +08:00
String[] headers = headerColumnGroup.stream()
2022-04-12 19:25:19 +08:00
.map(WeaTableColumn::getText)
.toArray(String[]::new);
List<Object> headerList = new ArrayList<>(Arrays.asList(headers));
// 查询薪资核算结果
List<Map<String, Object>> resultMapList = getSalaryAcctResultService(user).listByParam(queryParam);
// excel导出的数据
List<List<Object>> rows = new ArrayList<>();
rows.add(headerList);
for (Map<String, Object> map : resultMapList) {
2022-05-05 14:10:19 +08:00
List<Object> row = Lists.newArrayListWithExpectedSize(headerColumnGroup.size());
for (WeaTableColumnGroup weaTableColumn : headerColumnGroup) {
2022-04-12 19:25:19 +08:00
row.add(map.getOrDefault(weaTableColumn.getColumn(), StringUtils.EMPTY));
}
rows.add(row);
}
String sheetName = "薪资核算结果";
return ExcelUtil.genWorkbookV2(rows, sheetName);
}
/**
* 构建薪资核算结果列表的表头
*
* @param salaryAcctRecordPO 薪资核算记录
* @return
*/
2022-05-05 14:10:19 +08:00
public List<WeaTableColumnGroup> listWeaTableColumn(SalaryAcctRecordPO salaryAcctRecordPO) {
2022-04-12 19:25:19 +08:00
// 查询薪资账套下的薪资项目+员工信息字段
SalarySobItemAggregateDTO salarySobItemAggregateDTO = getSalarySobItemService(user).getAggregateBySalarySobId(salaryAcctRecordPO.getSalarySobId());
// 构建薪资核算结果列表表头
return SalaryAcctResultBO.buildTableColumns(salarySobItemAggregateDTO);
}
2022-05-05 14:10:19 +08:00
/**
* 解析表头
*
* @param headerList
* @param weaTableColumns
*/
private void parseHeader(List<WeaTableColumnGroup> headerList, List<WeaTableColumnGroup> weaTableColumns) {
for (WeaTableColumnGroup weaTableColumnGroup : weaTableColumns) {
if (CollectionUtils.isEmpty(weaTableColumnGroup.getChildren())) {
headerList.add(weaTableColumnGroup);
continue;
}
parseHeader(headerList, weaTableColumnGroup.getChildren());
}
}
2022-04-12 19:25:19 +08:00
@Override
public SalaryAcctImportFieldDTO getImportField(Long salaryAcctRecordId) {
// 查询薪资核算记录
SalaryAcctRecordPO salaryAcctRecordPO = getSalaryAcctRecordService(user).getById(salaryAcctRecordId);
if (Objects.isNull(salaryAcctRecordPO)) {
throw new SalaryRunTimeException(SalaryI18nUtil.getI18nLabel(98747, "薪资核算记录不存在或已被删除"));
}
// 查询薪资核算记录所用的薪资账套的薪资项目副本
List<SalarySobItemPO> salarySobItems = getSalarySobItemService(user).listBySalarySobId(salaryAcctRecordPO.getSalarySobId());
Set<Long> salaryItemIds = SalaryEntityUtil.properties(salarySobItems, SalarySobItemPO::getSalaryItemId);
// 查询薪资项目
List<SalaryItemPO> salaryItems = getSalaryItemService(user).listByIds(salaryItemIds);
// 薪资项目中的公式项
List<SalaryAcctImportFieldDTO.ImportFieldDTO> formulaItems = salaryItems.stream()
.filter(salaryItemPO -> Objects.equals(salaryItemPO.getValueType(), SalaryValueTypeEnum.FORMULA.getValue()))
.map(salaryItemPO -> SalaryAcctImportFieldDTO.ImportFieldDTO.builder()
.salaryItemId(salaryItemPO.getId())
.salaryItemName(salaryItemPO.getName())
.build())
.collect(Collectors.toList());
// 薪资项目中的输入项
List<SalaryAcctImportFieldDTO.ImportFieldDTO> inputItems = salaryItems.stream()
.filter(salaryItemPO -> Objects.equals(salaryItemPO.getValueType(), SalaryValueTypeEnum.INPUT.getValue()))
.map(salaryItemPO -> SalaryAcctImportFieldDTO.ImportFieldDTO.builder()
.salaryItemId(salaryItemPO.getId())
.salaryItemName(salaryItemPO.getName())
.build())
.collect(Collectors.toList());
// 转换成dto
return SalaryAcctImportFieldDTO.builder().formulaItems(formulaItems).inputItems(inputItems).build();
}
@Override
public XSSFWorkbook exportImportTemplate(SalaryAcctImportTemplateParam param) {
// 必须选择导入模板所需的薪资项目
if (CollectionUtils.isEmpty(param.getSalaryItemIds())) {
throw new SalaryRunTimeException(SalaryI18nUtil.getI18nLabel(99019, "参数错误,请选择导入模板所需的薪资项目"));
}
// 查询薪资核算记录
SalaryAcctRecordPO salaryAcctRecordPO = getSalaryAcctRecordService(user).getById(param.getSalaryAcctRecordId());
if (Objects.isNull(salaryAcctRecordPO)) {
throw new SalaryRunTimeException(SalaryI18nUtil.getI18nLabel(98747, "薪资核算记录不存在或已被删除"));
}
// 模板表头(默认必带"个税扣缴义务人"、"姓名")
List<Object> headerList = Lists.newArrayList(SalaryI18nUtil.getI18nLabel(85429, "姓名"), SalaryI18nUtil.getI18nLabel(86184, "个税扣缴义务人"));
List<String> dataIndexList = Lists.newArrayList("username", "taxAgentName");
// 查询薪资项目
List<SalaryItemPO> salaryItemPOS = getSalaryItemService(user).listByIds(param.getSalaryItemIds());
for (SalaryItemPO salaryItemPO : salaryItemPOS) {
headerList.add(salaryItemPO.getName());
dataIndexList.add("" + salaryItemPO.getId());
}
// 查询薪资核算结果
List<Map<String, Object>> resultMapList = getSalaryAcctResultService(user).listByParam(param);
// excel导出的数据
List<List<Object>> rows = Lists.newArrayListWithExpectedSize(resultMapList.size());
rows.add(headerList);
for (Map<String, Object> map : resultMapList) {
List<Object> row = Lists.newArrayListWithExpectedSize(headerList.size());
for (String dataIndex : dataIndexList) {
row.add(map.getOrDefault(dataIndex, StringUtils.EMPTY));
}
rows.add(row);
}
String sheetName = "薪资核算导入模板";
return ExcelUtil.genWorkbookV2(rows, sheetName);
}
2022-04-13 14:18:00 +08:00
@Override
public XSSFWorkbook exportComparisonResult(SalaryComparisonResultQueryParam queryParam) {
// 查询薪资核算记录
SalaryAcctRecordPO salaryAcctRecordPO = getSalaryAcctRecordService(user).getById(queryParam.getSalaryAcctRecordId());
if (Objects.isNull(salaryAcctRecordPO)) {
throw new SalaryRunTimeException(SalaryI18nUtil.getI18nLabel(98747, "薪资核算记录不存在或已被删除"));
}
// 查询线下对比结果
SalaryComparisonResultListDTO salaryComparisonResultListDTO = getSalaryComparisonResultService(user).listByParam(queryParam);
// 薪资核算线下对比结果列表表头
List<Object> headerList = Lists.newArrayList();
// 查询薪资核算所用的薪资账套的员工信息字段
List<SalarySobEmpFieldPO> salarySobEmpFieldPOS = getSalarySobEmpFieldService(user).listBySalarySobId(salaryAcctRecordPO.getSalarySobId());
Set<String> employeeFieldCodeSet = SalaryEntityUtil.properties(salarySobEmpFieldPOS, SalarySobEmpFieldPO::getFieldCode);
// 查询薪资项目
List<SalaryItemPO> salaryItemPOS = getSalarySobItemService(user).listBySalarySobId4SalaryItem(salaryAcctRecordPO.getSalarySobId());
Set<String> salaryItemIds = SalaryEntityUtil.properties(salaryItemPOS, salaryItemPO -> "" + salaryItemPO.getId());
for (WeaTableColumn weaTableColumn : salaryComparisonResultListDTO.getWeaTableColumns()) {
// 员工信息字段
if (employeeFieldCodeSet.contains(weaTableColumn.getColumn())) {
headerList.add(weaTableColumn.getText());
}
// 薪资项目的表头
if (salaryItemIds.contains(weaTableColumn.getColumn())) {
headerList.add(weaTableColumn.getText() + " (线上值)");
headerList.add(weaTableColumn.getText() + " (线下值)");
}
}
List<Map<String, Object>> resultMapList = salaryComparisonResultListDTO.getData().getList();
// excel导出的数据
List<List<Object>> rows = new ArrayList<>();
rows.add(headerList);
for (Map<String, Object> map : resultMapList) {
List<Object> row = Lists.newArrayList();
for (WeaTableColumn weaTableColumn : salaryComparisonResultListDTO.getWeaTableColumns()) {
// 员工信息字段的值
if (employeeFieldCodeSet.contains(weaTableColumn.getColumn())) {
row.add(map.get(weaTableColumn.getColumn()));
}
// 薪资项目的值
if (salaryItemIds.contains(weaTableColumn.getColumn())) {
Map tempMap = (Map) map.getOrDefault(weaTableColumn.getColumn(), Collections.emptyMap());
row.add(tempMap.get("acctResultValue"));
row.add(tempMap.get("excelResultValue"));
}
}
rows.add(row);
}
String sheetName = "线下对比结果";
return ExcelUtil.genWorkbookV2(rows, sheetName);
}
2022-04-18 17:28:34 +08:00
@Override
public XSSFWorkbook exportComparisonResultTemplate(SalaryComparisonResultExportParam exportParam) {
ValidUtil.doValidator(exportParam);
// 查询薪资核算记录
SalaryAcctRecordPO salaryAcctRecordPO = getSalaryAcctRecordService(user).getById(exportParam.getSalaryAcctRecordId());
if (Objects.isNull(salaryAcctRecordPO)) {
throw new SalaryRunTimeException(SalaryI18nUtil.getI18nLabel(98747, "薪资核算记录不存在或已被删除"));
}
// 模板表头(默认必带"个税扣缴义务人"、"姓名")
List<Object> headerList = Lists.newArrayList(SalaryI18nUtil.getI18nLabel(85429, "姓名"), SalaryI18nUtil.getI18nLabel(86184, "个税扣缴义务人"));
// 查询薪资账套下的薪资项目
SalarySobItemAggregateDTO salarySobItemAggregateDTO = getSalarySobItemService(user).getAggregateBySalarySobId(salaryAcctRecordPO.getSalarySobId());
// 薪资项目分类下的新资项目
for (SalarySobItemGroupDTO itemGroup : salarySobItemAggregateDTO.getItemGroups()) {
for (SalarySobItemDTO item : itemGroup.getItems()) {
headerList.add(item.getName());
}
}
// 没有分类的薪资项目
for (SalarySobItemDTO item : salarySobItemAggregateDTO.getItems()) {
headerList.add(item.getName());
}
List<List<Object>> rows = new ArrayList<>();
rows.add(headerList);
String sheetName = "线下对比结果导入模板";
return ExcelUtil.genWorkbookV2(rows, sheetName);
}
2022-04-19 19:32:09 +08:00
public Map<String, Object> importSalaryAcctResult(SalaryAcctImportParam param) {
return batchImport(param, "importSalaryAcctResult");
}
2022-04-18 17:28:34 +08:00
2022-04-19 15:13:08 +08:00
2022-04-19 19:32:09 +08:00
public Map<String, Object> importExcelAcctResult(SalaryAcctImportParam param) {
return batchImport(param, "importExcelAcctResult");
}
2022-04-19 15:13:08 +08:00
2022-04-19 19:50:00 +08:00
@Override
public Map<String, Object> preview(SalaryAcctImportParam param) {
//1、参数校验
ValidUtil.doValidator(param);
Map<String, Object> map = new HashMap<>();
InputStream fileInputStream = null;
try {
fileInputStream = ImageFileManager.getInputStreamById(Integer.parseInt(param.getImageId()));
Sheet sheet = ExcelSupport.parseFile(fileInputStream, 0, EXCEL_TYPE_XLSX);
map.put("headers", ExcelSupport.getSheetHeader(sheet, 0));
map.put("list", ExcelParseHelper.parse2List(sheet, 1));
return map;
} finally {
IOUtils.closeQuietly(fileInputStream);
}
}
2022-04-19 17:21:27 +08:00
private Map<String, Object> batchImport(SalaryAcctImportParam param, String importType) {
2022-04-19 19:32:09 +08:00
Map<String, Object> apidatas = new HashMap<String, Object>();
2022-04-19 17:21:27 +08:00
ValidUtil.doValidator(param);
Date now = new Date();
Long currentEmployeeId = (long) user.getUID();
// 薪资核算记录的id
Long salaryAcctRecordId = param.getSalaryAcctRecordId();
// 薪资核算记录
SalaryAcctRecordPO salaryAcctRecordPO = getSalaryAcctRecordService(user).getById(salaryAcctRecordId);
if (salaryAcctRecordPO == null) {
throw new SalaryRunTimeException(SalaryI18nUtil.getI18nLabel(98747, "薪资核算记录不存在或已被删除"));
}
// 薪资账套下的薪资项目副本
List<SalarySobItemPO> salarySobItemPOS = getSalarySobItemService(user).listBySalarySobId(salaryAcctRecordPO.getSalarySobId());
// 薪资项目
Set<Long> salaryItemIds = SalaryEntityUtil.properties(salarySobItemPOS, SalarySobItemPO::getSalaryItemId);
List<SalaryItemPO> salaryItems = getSalaryItemService(user).listByIds(salaryItemIds);
Map<String, Long> salaryItemMap = SalaryEntityUtil.convert2Map(salaryItems, SalaryItemPO::getName, SalaryItemPO::getId);
// 薪资核算确认的人员
List<SalaryAcctEmployeePO> salaryAcctEmployees = getSalaryAcctEmployeeService(user).listBySalaryAcctRecordId(salaryAcctRecordId);
Map<String, SalaryAcctEmployeePO> salaryAcctEmployeeMap = SalaryEntityUtil.convert2Map(salaryAcctEmployees, e -> e.getEmployeeId() + "-" + e.getTaxAgentId());
// 租户下所有的人员
List<DataCollectionEmployee> salaryEmployees = getSalaryEmployeeService(user).listAll();
Map<String, Long> salaryEmployeeMap = SalaryEntityUtil.convert2Map(salaryEmployees, DataCollectionEmployee::getUsername, DataCollectionEmployee::getEmployeeId);
// 租户下所有的个税扣缴义务人
List<TaxAgent> taxAgents = taxAgentService.listAll();
Map<String, Long> taxAgentNameMap = SalaryEntityUtil.convert2Map(taxAgents, TaxAgent::getName, TaxAgent::getId);
// 索引(用于计算进度)
int index = 0;
// 失败的数量
int failCount = 0;
// 成功的数量
int successCount = 0;
// 总数
2022-04-12 19:25:19 +08:00
// int total = message.getBatchFile().getExcelSheets().stream()
// .filter(e -> CollectionUtils.isNotEmpty(e.getHeader()) && CollectionUtils.isNotEmpty(e.getData()))
// .map(e -> BigDecimal.valueOf(e.getData().size()))
// .reduce(BigDecimal.ZERO, BigDecimal::add)
// .intValue();
2022-04-19 17:21:27 +08:00
// 包含错误提示信息的sheet页
List<Map<String, String>> errorExcelSheets = Lists.newArrayList();
// 解析excel
Set<Long> salaryAcctEmpIds = Sets.newHashSet();
// excel导入了哪些薪资项目
Set<Long> excelSalaryItemIds = Sets.newHashSet();
List<SalaryAcctResultPO> salaryAcctResults = Lists.newArrayList();
List<SalaryAcctEmployeePO> newSalaryAcctEmployees = Lists.newArrayList();
List<ExcelAcctResultPO> excelAcctResults = Lists.newArrayList();
InputStream fileInputStream = null;
2022-04-19 19:32:09 +08:00
try {
fileInputStream = ImageFileManager.getInputStreamById(Integer.parseInt(param.getImageId()));
Sheet sheet = ExcelSupport.parseFile(fileInputStream, 0, EXCEL_TYPE_XLSX);
// 错误提示信息
List<Map> excelComments = Lists.newArrayList();
// 存在错误的那行数据
List<Map<String, Object>> errorDatas = Lists.newArrayList();
// 表头
List<String> headers = ExcelSupport.getSheetHeader(sheet, 0);
// 处理数值
List<Map<String, Object>> data = ExcelParseHelper.parse2Map(sheet, 1);
if (CollectionUtils.isEmpty(headers)) {
throw new RuntimeException("表头为空");
}
if (CollectionUtils.isEmpty(data)) {
throw new RuntimeException("无数据");
}
for (int i = 0; i < data.size(); i++) {
int usernameIndex = 0;
boolean isError = false;
Map<String, Object> map = data.get(i);
Long employeeId = 0L;
Long taxAgentId = 0L;
List<SalaryAcctResultPO> salaryAcctResultsOfOneEmp = Lists.newArrayListWithExpectedSize(headers.size() - 1);
List<ExcelAcctResultPO> excelAcctResultsOfOneEmp = Lists.newArrayListWithExpectedSize(headers.size() - 1);
for (int j = 0; j < headers.size(); j++) {
String header = headers.get(j);
String dataKey = header;
if (dataKey == null) {
continue;
}
String dataValue = (String) map.getOrDefault(dataKey.toString(), "");
if (StringUtils.equals(SalaryI18nUtil.getI18nLabel(85429, "姓名"), dataKey.toString())) {
usernameIndex = j;
if (StringUtils.isEmpty(dataValue)) {
2022-04-19 17:21:27 +08:00
isError = true;
Map<String, String> errorMessageMap = Maps.newHashMap();
2022-04-19 19:32:09 +08:00
errorMessageMap.put("message", SalaryI18nUtil.getI18nLabel(102838, "姓名不能为空"));
2022-04-19 17:21:27 +08:00
excelComments.add(errorMessageMap);
2022-04-19 19:32:09 +08:00
//salaryBatchService.createExcelComment(excelComments, SalaryI18nUtil.getI18nLabel(102838, "姓名不能为空"), i, i, j, j);
} else {
employeeId = salaryEmployeeMap.getOrDefault(dataValue, 0L);
if (employeeId == null || employeeId <= 0) {
isError = true;
Map<String, String> errorMessageMap = Maps.newHashMap();
errorMessageMap.put("message", SalaryI18nUtil.getI18nLabel(100579, "姓名错误,系统内不存在该姓名"));
excelComments.add(errorMessageMap);
2022-04-19 15:13:08 +08:00
// salaryBatchService.createExcelComment(excelComments, SalaryI18nUtil.getI18nLabel(100579, "姓名错误,系统内不存在该姓名"), i, i, j, j);
2022-04-19 19:32:09 +08:00
}
2022-04-19 17:21:27 +08:00
}
2022-04-19 19:32:09 +08:00
} else if (StringUtils.equals(SalaryI18nUtil.getI18nLabel(86184, "个税扣缴义务人"), dataKey.toString())) {
if (StringUtils.isEmpty(dataValue)) {
2022-04-19 17:21:27 +08:00
isError = true;
Map<String, String> errorMessageMap = Maps.newHashMap();
2022-04-19 19:32:09 +08:00
errorMessageMap.put("message", SalaryI18nUtil.getI18nLabel(100579, SalaryI18nUtil.getI18nLabel(102839, "个税扣缴义务人不能为空")));
2022-04-19 17:21:27 +08:00
excelComments.add(errorMessageMap);
2022-04-19 19:32:09 +08:00
// salaryBatchService.createExcelComment(excelComments, SalaryI18nUtil.getI18nLabel(102839, "个税扣缴义务人不能为空"), i, i, j, j);
} else {
taxAgentId = taxAgentNameMap.getOrDefault(dataValue, 0L);
if (taxAgentId == null || taxAgentId <= 0) {
isError = true;
Map<String, String> errorMessageMap = Maps.newHashMap();
errorMessageMap.put("message", SalaryI18nUtil.getI18nLabel(100579, SalaryI18nUtil.getI18nLabel(102840, "个税扣缴义务人错误,系统内不存在该个税扣缴义务人")));
excelComments.add(errorMessageMap);
2022-04-19 15:13:08 +08:00
// salaryBatchService.createExcelComment(excelComments, SalaryI18nUtil.getI18nLabel(102840, "个税扣缴义务人错误,系统内不存在该个税扣缴义务人"), i, i, j, j);
2022-04-19 19:32:09 +08:00
}
2022-04-19 17:21:27 +08:00
}
} else {
2022-04-19 19:32:09 +08:00
Long salaryItemId = salaryItemMap.get(dataKey.toString());
if (salaryItemId == null || salaryItemId <= 0) {
isError = true;
Map<String, String> errorMessageMap = Maps.newHashMap();
errorMessageMap.put("message", SalaryI18nUtil.getI18nLabel(100579, SalaryI18nUtil.getI18nLabel(102840, "本次核算所用账套不包含该薪资项目")));
excelComments.add(errorMessageMap);
// salaryBatchService.createExcelComment(excelComments, SalaryI18nUtil.getI18nLabel(102841, "表头错误,本次核算所用账套不包含该薪资项目"), i, i, j, j);
} else {
excelSalaryItemIds.add(salaryItemId);
if (StringUtils.equals("importExcelAcctResult", importType)) {
ExcelAcctResultPO excelAcctResult = ExcelAcctResultPO.builder()
2022-04-19 15:13:08 +08:00
// .id(IdGenerator.generate())
2022-04-19 19:32:09 +08:00
.salaryAcctRecordId(salaryAcctRecordPO.getId())
.salarySobId(salaryAcctRecordPO.getSalarySobId())
.salaryItemId(salaryItemId)
.resultValue(dataValue)
.creator(currentEmployeeId)
.createTime(now)
.updateTime(now)
.deleteType(0)
.tenantKey(SalaryDefaultTenantConstant.DEFAULT_TENANT_KEY)
.build();
excelAcctResultsOfOneEmp.add(excelAcctResult);
}
if (StringUtils.equals("importSalaryAcctResult", importType)) {
SalaryAcctResultPO salaryAcctResult = SalaryAcctResultPO.builder()
2022-04-19 15:13:08 +08:00
// .id(IdGenerator.generate())
2022-04-19 19:32:09 +08:00
.salaryAcctRecordId(salaryAcctRecordPO.getId())
.salarySobId(salaryAcctRecordPO.getSalarySobId())
.salaryItemId(salaryItemId)
.resultValue(dataValue)
.creator(currentEmployeeId)
.createTime(now)
.updateTime(now)
.deleteType(0)
.tenantKey(SalaryDefaultTenantConstant.DEFAULT_TENANT_KEY)
.build();
salaryAcctResultsOfOneEmp.add(salaryAcctResult);
}
2022-04-19 17:21:27 +08:00
}
}
2022-04-19 19:32:09 +08:00
// (如果是线下对比)如果个税扣缴义务人+人员目前不在核算人员里面,不支持导入
if (StringUtils.equals("importExcelAcctResult", importType)
&& (employeeId != null && employeeId > 0)
&& (taxAgentId != null && taxAgentId > 0)
&& i == data.size() - 1
&& !salaryAcctEmployeeMap.containsKey(employeeId + "-" + taxAgentId)) {
isError = true;
Map<String, String> errorMessageMap = Maps.newHashMap();
errorMessageMap.put("message", SalaryI18nUtil.getI18nLabel(100579, SalaryI18nUtil.getI18nLabel(102840, "本次薪资核算不包含该人员")));
excelComments.add(errorMessageMap);
2022-04-19 15:13:08 +08:00
// salaryBatchService.createExcelComment(excelComments, SalaryI18nUtil.getI18nLabel(102842, "本次薪资核算不包含该人员"), i, i, usernameIndex, usernameIndex);
2022-04-19 19:32:09 +08:00
}
2022-04-19 17:21:27 +08:00
}
2022-04-19 19:32:09 +08:00
// 每处理50个数据更新一次进度
index++;
2022-04-19 15:13:08 +08:00
// if (index % 50 == 0 || index >= total) {
// salaryBatchService.sendImportRate(message.getBizId(), total, index);
// }
2022-04-19 19:32:09 +08:00
if (isError) {
failCount++;
errorDatas.add(map);
continue;
2022-04-19 17:21:27 +08:00
}
2022-04-19 19:32:09 +08:00
SalaryAcctEmployeePO salaryAcctEmployee = salaryAcctEmployeeMap.get(employeeId + "-" + taxAgentId);
Long salaryAcctEmpId = Optional.ofNullable(salaryAcctEmployee).map(SalaryAcctEmployeePO::getId).orElse(0L);
if (StringUtils.equals("importExcelAcctResult", importType)) {
for (ExcelAcctResultPO excelAcctResultPO : excelAcctResultsOfOneEmp) {
excelAcctResultPO.setEmployeeId(employeeId);
excelAcctResultPO.setTaxAgentId(taxAgentId);
excelAcctResultPO.setSalaryAcctEmpId(salaryAcctEmpId);
}
excelAcctResults.addAll(excelAcctResultsOfOneEmp);
2022-04-19 17:21:27 +08:00
}
2022-04-19 19:32:09 +08:00
if (StringUtils.equals("importSalaryAcctResult", importType)) {
if (salaryAcctEmpId <= 0) {
SalaryAcctEmployeePO acctEmployee = SalaryAcctEmployeePO.builder()
// .id(IdGenerator.generate())
.employeeId(employeeId)
.salaryAcctRecordId(salaryAcctRecordId)
.salarySobId(salaryAcctRecordPO.getSalarySobId())
.salaryMonth(salaryAcctRecordPO.getSalaryMonth())
.taxAgentId(taxAgentId)
.creator(currentEmployeeId)
.createTime(now)
.updateTime(now)
.deleteType(0)
.tenantKey(SalaryDefaultTenantConstant.DEFAULT_TENANT_KEY)
.build();
newSalaryAcctEmployees.add(acctEmployee);
salaryAcctEmpId = acctEmployee.getId();
}
for (SalaryAcctResultPO salaryAcctResultPO : salaryAcctResultsOfOneEmp) {
salaryAcctResultPO.setEmployeeId(employeeId);
salaryAcctResultPO.setTaxAgentId(taxAgentId);
salaryAcctResultPO.setSalaryAcctEmpId(salaryAcctEmpId);
}
salaryAcctEmpIds.add(salaryAcctEmpId);
salaryAcctResults.addAll(salaryAcctResultsOfOneEmp);
2022-04-19 17:21:27 +08:00
}
2022-04-19 19:32:09 +08:00
successCount++;
2022-04-19 17:21:27 +08:00
}
2022-04-19 19:32:09 +08:00
// 如果sheet包含错误数据
2022-04-19 15:13:08 +08:00
// if (CollectionUtils.isNotEmpty(errorDatas)) {
// salaryBatchService.createErrorExcelSheet(headers, errorDatas, excelSheet.getName(), excelComments, errorExcelSheets);
2022-04-12 19:25:19 +08:00
// }
2022-04-19 19:32:09 +08:00
if (StringUtils.equals("importExcelAcctResult", importType)) {
getSalaryComparisonResultService(user).deleteBySalaryAcctRecordIds(Collections.singleton(salaryAcctRecordId));
if (CollectionUtils.isNotEmpty(excelAcctResults)) {
getSalaryComparisonResultService(user).batchSave(excelAcctResults);
}
2022-04-19 17:21:27 +08:00
}
2022-04-19 19:32:09 +08:00
if (StringUtils.equals("importSalaryAcctResult", importType)) {
if (CollectionUtils.isNotEmpty(salaryAcctEmpIds)) {
getSalaryAcctResultService(user).deleteByAcctEmployeeIdsAndSalaryItemIds(salaryAcctEmpIds, excelSalaryItemIds);
}
if (CollectionUtils.isNotEmpty(salaryAcctResults)) {
getSalaryAcctResultService(user).batchSave(salaryAcctResults);
}
if (CollectionUtils.isNotEmpty(newSalaryAcctEmployees)) {
getSalaryAcctEmployeeService(user).batchSave(newSalaryAcctEmployees);
}
2022-04-19 17:21:27 +08:00
}
2022-04-19 19:32:09 +08:00
apidatas.put("successCount", successCount);
apidatas.put("errorCount", failCount);
apidatas.put("errorData", excelComments);
} finally {
IOUtils.closeQuietly(fileInputStream);
}
2022-04-19 17:21:27 +08:00
// 发送导入回调信息
2022-04-12 19:25:19 +08:00
// salaryBatchService.sendImportCallBackInfo(message, successCount, failCount, errorExcelSheets);
2022-04-19 19:32:09 +08:00
return apidatas;
2022-04-19 17:21:27 +08:00
}
2022-04-19 15:13:08 +08:00
2022-04-12 19:25:19 +08:00
// @Override
// public Map<String, Object> exportCheckResult(
// SalaryCheckResultExportParam exportParam,
// ) {
// // 查询薪资核算结果校验异常
// List<SalaryCheckResultPO> salaryCheckResults = salaryCheckResultService.listBySalaryAcctRecordId(exportParam.getSalaryAcctRecordId());
// List<ExcelSheetData> excelSheetData = Lists.newArrayListWithExpectedSize(salaryCheckResults.size());
// for (SalaryCheckResultPO salaryCheckResult : salaryCheckResults) {
// excelSheetData.add(buildDetailExcelSheetData(salaryCheckResult, simpleEmployee.getEmployeeId()));
// }
// ExportExcelInfo exportExcelInfo = ExportExcelInfo.builder()
// .fileName("校验异常")
// .dataType("校验异常")
// .function(excelExportParam.getFunction())
// .handlerName("salaryCheckResultHandler")
// .flag(true)
// .bizId(String.valueOf(excelExportParam.getBiz()))
// .module(excelExportParam.getModule())
// .userId(simpleEmployee.getEmployeeId())
// .tenantKey(tenantKey)
// .operator(simpleEmployee.getUsername())
// .eteamsId(eteamsId)
// .build();
// return salaryBatchService.simpleExportExcel(exportExcelInfo, excelSheetData);
// }
//
// @BatchExportHandler("salaryCheckResultHandler")
// private void salaryCheckResultHandler() {
// BatchCallbackMessage message = BatchExportContext.getBatchCallbackMessage();
// log.info("导出结束:{}", JsonUtil.toJsonString(message));
// }
//
// @Override
// public Map<String, Object> exportCheckResultDetail(
// Long checkResultId,
// ) {
// // 查询校验异常
// SalaryCheckResultPO checkResult = salaryCheckResultService.getById(checkResultId);
// // 查询校验异常明细
// ExcelSheetData excelSheetData = buildDetailExcelSheetData(checkResult, simpleEmployee.getEmployeeId());
// ExportExcelInfo exportExcelInfo = ExportExcelInfo.builder()
// .fileName("校验异常明细")
// .dataType("校验异常明细")
// .function(excelExportParam.getFunction())
// .handlerName("salaryCheckResultDetailHandler")
// .flag(true)
// .bizId(String.valueOf(excelExportParam.getBiz()))
// .module(excelExportParam.getModule())
// .userId(simpleEmployee.getEmployeeId())
// .tenantKey(tenantKey)
// .operator(simpleEmployee.getUsername())
// .eteamsId(eteamsId)
// .build();
// return salaryBatchService.simpleExportExcel(exportExcelInfo, Collections.singletonList(excelSheetData));
// }
//
// @BatchExportHandler("salaryCheckResultDetailHandler")
// private void salaryCheckResultDetailHandler() {
// BatchCallbackMessage message = BatchExportContext.getBatchCallbackMessage();
// log.info("导出结束:{}", JsonUtil.toJsonString(message));
// }
//
// private ExcelSheetData buildDetailExcelSheetData(SalaryCheckResultPO checkResult, Long employeeId) {
// // 查询校验异常中的校验规则
// SalarySobCheckRulePO checkRule = salarySobCheckRuleService.getById(checkResult.getSalaryCheckRuleId());
// // 查询校验异常明细
// List<SalaryCheckResultRecordPO> salaryCheckResultRecords = salaryCheckResultDetailService.listByCheckResultId(checkResult.getId());
// String[] headers = new String[]{"规则名称", "规则内容", "姓名"};
// List<List<Object>> rows = Lists.newArrayList();
// ExcelSheetData excelSheetData = new ExcelSheetData();
// excelSheetData.setSheetName("校验异常");
// excelSheetData.setHeaders(Collections.singletonList(headers));
// excelSheetData.setRows(rows);
// if (CollectionUtils.isEmpty(salaryCheckResultRecords)) {
// return excelSheetData;
// }
// List<Long> employeeIds = SalaryEntityUtil.properties(salaryCheckResultRecords, SalaryCheckResultRecordPO::getEmployeeId, Collectors.toList());
// List<SimpleEmployee> simpleEmployees = hrmCommonEmployeeService.getEmployeeByIds(employeeIds);
// Map<Long, String> simpleEmployeeMap = SalaryEntityUtil.convert2Map(simpleEmployees, SimpleEmployee::getEmployeeId, SimpleEmployee::getUsername);
// List<ExpressFormula> expressFormulas = getSalaryFormulaService(user).listExpressFormula(Collections.singleton(checkResult.getFormulaId()));
// Map<Long, String> formulaMap = SalaryEntityUtil.convert2Map(expressFormulas, ExpressFormula::getId, ExpressFormula::getFormula);
// for (SalaryCheckResultRecordPO salaryCheckResultRecord : salaryCheckResultRecords) {
// List<Object> row = Lists.newArrayListWithExpectedSize(3);
// row.add(checkRule.getName());
// row.add(formulaMap.getOrDefault(salaryCheckResultRecord.getFormulaId(), ""));
// row.add(simpleEmployeeMap.getOrDefault(salaryCheckResultRecord.getEmployeeId(), ""));
// rows.add(row);
// }
// return excelSheetData;
// }
}