package com.engine.salary.service.impl; import com.engine.common.util.ServiceUtil; import com.engine.core.impl.Service; import com.engine.salary.biz.EmployBiz; import com.engine.salary.biz.OtherDeductionBiz; import com.engine.salary.encrypt.datacollection.OtherDeductionListDTOEncrypt; import com.engine.salary.encrypt.datacollection.OtherDeductionRecordDTOEncrypt; import com.engine.salary.entity.datacollection.DataCollectionEmployee; import com.engine.salary.entity.datacollection.dto.OtherDeductionListDTO; import com.engine.salary.entity.datacollection.dto.OtherDeductionRecordDTO; import com.engine.salary.entity.datacollection.param.OtherDeductionImportParam; import com.engine.salary.entity.datacollection.param.OtherDeductionQueryParam; import com.engine.salary.entity.datacollection.po.OtherDeductionPO; import com.engine.salary.entity.salaryacct.po.SalaryAcctEmployeePO; import com.engine.salary.entity.taxagent.dto.TaxAgentManageRangeEmployeeDTO; import com.engine.salary.entity.taxagent.po.TaxAgentPO; import com.engine.salary.enums.UserStatusEnum; import com.engine.salary.exception.SalaryRunTimeException; import com.engine.salary.mapper.datacollection.OtherDeductionMapper; import com.engine.salary.service.AddUpDeductionService; import com.engine.salary.service.OtherDeductionService; import com.engine.salary.service.SalaryEmployeeService; import com.engine.salary.service.TaxAgentService; import com.engine.salary.util.SalaryDateUtil; import com.engine.salary.util.SalaryI18nUtil; import com.engine.salary.util.db.MapperProxyFactory; import com.engine.salary.util.excel.ExcelComment; import com.engine.salary.util.excel.ExcelParseHelper; import com.engine.salary.util.excel.ExcelUtil; 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 org.apache.commons.collections4.CollectionUtils; import org.apache.commons.lang3.StringUtils; import org.apache.commons.lang3.Validate; import org.apache.poi.util.IOUtils; import org.apache.poi.xssf.usermodel.XSSFWorkbook; import weaver.file.ImageFileManager; import weaver.general.Util; import weaver.hrm.User; import java.io.InputStream; import java.text.SimpleDateFormat; import java.time.YearMonth; import java.util.*; import java.util.stream.Collectors; import static com.engine.salary.constant.SalaryDefaultTenantConstant.DEFAULT_TENANT_KEY; public class OtherDeductionServiceImpl extends Service implements OtherDeductionService { private OtherDeductionMapper getOtherDeductionMapper() { return MapperProxyFactory.getProxy(OtherDeductionMapper.class); } private TaxAgentService getTaxAgentService(User user) { return ServiceUtil.getService(TaxAgentServiceImpl.class, user); } private AddUpDeductionService getAddUpDeductionService(User user) { return ServiceUtil.getService(AddUpDeductionServiceImpl.class, user); } private SalaryEmployeeService getSalaryEmployeeService(User user) { return ServiceUtil.getService(SalaryEmployeeServiceImpl.class, user); } @Override public OtherDeductionPO getById(Long id) { return getOtherDeductionMapper().getById(id); } @Override public PageInfo listPage(OtherDeductionQueryParam queryParam) { //申报月份 List declareMonth = queryParam.getDeclareMonth(); if (CollectionUtils.isNotEmpty(declareMonth)) { queryParam.setDeclareMonth(declareMonth.stream().map(e -> e + "-01 00:00:00").collect(Collectors.toList())); queryParam.setDeclareMonthDate(declareMonth.stream().map(e -> e + "-01 00:00:00").map(SalaryDateUtil::dateStrToLocalTime).collect(Collectors.toList())); } long employeeId = user.getUID(); Boolean needAuth = getTaxAgentService(user).isNeedAuth(employeeId); if (needAuth) { List taxAgentIdsAsAdmin = getTaxAgentService(user).listAllTaxAgentsAsAdmin(employeeId).stream().map(TaxAgentPO::getId).collect(Collectors.toList()); if (CollectionUtils.isEmpty(taxAgentIdsAsAdmin)) { return new PageInfo<>(OtherDeductionListDTO.class); } queryParam.setTaxAgentIds(taxAgentIdsAsAdmin); } SalaryPageUtil.start(queryParam.getCurrent(), queryParam.getPageSize()); List list = getOtherDeductionMapper().list(queryParam); OtherDeductionListDTOEncrypt.decryptOtherDeductionListDTOList(list); return new PageInfo<>(list, OtherDeductionListDTO.class); } @Override public PageInfo recordListPage(OtherDeductionQueryParam queryParam) { long employeeId = user.getUID(); //申报月份 List declareMonth = queryParam.getDeclareMonth(); if (CollectionUtils.isNotEmpty(declareMonth)) { queryParam.setDeclareMonth(declareMonth.stream().map(e -> e + "-01 00:00:00").collect(Collectors.toList())); queryParam.setDeclareMonthDate(declareMonth.stream().map(e -> e + "-01 00:00:00").map(SalaryDateUtil::dateStrToLocalTime).collect(Collectors.toList())); } Boolean needAuth = getTaxAgentService(user).isNeedAuth(employeeId); if (needAuth) { List taxAgentIdsAsAdmin = getTaxAgentService(user).listAllTaxAgentsAsAdmin(employeeId).stream().map(TaxAgentPO::getId).collect(Collectors.toList()); if (CollectionUtils.isEmpty(taxAgentIdsAsAdmin)) { return new PageInfo<>(OtherDeductionRecordDTO.class); } queryParam.setTaxAgentIds(taxAgentIdsAsAdmin); } SalaryPageUtil.start(queryParam.getCurrent(), queryParam.getPageSize()); List list = getOtherDeductionMapper().recordList(queryParam); OtherDeductionRecordDTOEncrypt.decryptOtherDeductionRecordDTOList(list); return new PageInfo<>(list, OtherDeductionRecordDTO.class); } @Override public Map preview(OtherDeductionImportParam importParam) { Map apidatas = new HashMap(); //excel文件id String imageId = Util.null2String(importParam.getImageId()); Validate.notBlank(imageId, "imageId为空"); InputStream fileInputStream = null; try { fileInputStream = ImageFileManager.getInputStreamById(Integer.parseInt(imageId)); List OtherDeductions = ExcelParseHelper.parse2Map(fileInputStream, OtherDeductionListDTO.class, 0, 1, 11, "OtherDeductionTemplate.xlsx"); apidatas.put("preview", OtherDeductions); } finally { IOUtils.closeQuietly(fileInputStream); } return apidatas; } public Map importData(OtherDeductionImportParam importParam) { Boolean openDevolution = getTaxAgentService(user).isOpenDevolution(); long currentEmployeeId = user.getUID(); Map apidatas = new HashMap(); EmployBiz employBiz = new EmployBiz(); OtherDeductionBiz OtherDeductionBiz = new OtherDeductionBiz(); //检验参数 checkImportParam(importParam); //excel文件id String imageId = Util.null2String(importParam.getImageId()); Validate.notBlank(imageId, "imageId为空"); //税款所属期 String declareMonthStr = Util.null2String(importParam.getDeclareMonth()); //个税扣缴义务人 String taxAgentId = Util.null2String(importParam.getTaxAgentId()); InputStream fileInputStream = null; try { fileInputStream = ImageFileManager.getInputStreamById(Integer.parseInt(imageId)); List OtherDeductions = ExcelParseHelper.parse2Map(fileInputStream, OtherDeductionListDTO.class, 0, 1, 11, "OtherDeductionTemplate.xlsx"); int total = OtherDeductions.size(); int index = 0; int successCount = 0; int errorCount = 0; //人员信息 List employees = employBiz.listEmployee(); // 获取所有个税扣缴义务人 Collection taxAgentList = getTaxAgentService(user).listTaxAgentAndEmployeeTree(currentEmployeeId); //税款所属期 Date declareMonth = SalaryDateUtil.stringToDate(declareMonthStr + "-01"); // 获取已经核算的数据 List salaryAcctEmployees = getAddUpDeductionService(user).getAccountedEmployeeData(declareMonthStr); // 查询已有数据 List list = getOtherDeductionMapper().listSome(OtherDeductionPO.builder().declareMonth(declareMonth).build()); // 错误excel内容 List errorData = new ArrayList<>(); //合规数据 List eligibleData = new ArrayList<>(); List taxAgentEmployees = Lists.newArrayList(); for (int i = 0; i < OtherDeductions.size(); i++) { OtherDeductionListDTO dto = OtherDeductions.get(i); Date now = new Date(); //待插入数据库对象 OtherDeductionPO po = OtherDeductionPO.builder() .tenantKey(DEFAULT_TENANT_KEY) .createTime(now) .updateTime(now) .creator((long) user.getUID()) .declareMonth(declareMonth).build(); //异常点数量 int errorSum = 0; //行号 String rowIndex = String.format("第%s行", i + 2); //相同的姓名 String userName = dto.getUsername(); String deparmentName = dto.getDepartmentName(); List employeeSameIds = new ArrayList<>(); List emps = employees.stream().filter(e -> (StringUtils.isBlank(userName) || Objects.equals(e.getUsername(), userName)) && (StringUtils.isBlank(deparmentName) || Objects.equals(e.getDepartmentName(), deparmentName))) .collect(Collectors.toList()); //含在职和离职,选在职数据 if (CollectionUtils.isNotEmpty(emps) && emps.size() > 1) { employeeSameIds = emps.stream() .filter(e -> UserStatusEnum.getNormalStatus().contains(e.getStatus())) .map(DataCollectionEmployee::getEmployeeId) .collect(Collectors.toList()); } if (CollectionUtils.isNotEmpty(emps) && emps.size() == 1) { employeeSameIds = emps.stream() .map(DataCollectionEmployee::getEmployeeId) .collect(Collectors.toList()); } if (StringUtils.isBlank(userName)) { //姓名 不能为空 //错误消息对象 Map errorMessageMap = Maps.newHashMap(); errorMessageMap.put("message", rowIndex + "姓名不能为空"); errorData.add(errorMessageMap); errorSum += 1; } else if (CollectionUtils.isEmpty(employeeSameIds) || employeeSameIds.size() > 1) { Map errorMessageMap = Maps.newHashMap(); errorMessageMap.put("message", rowIndex + "员工信息不能为空且不可重复(姓名与部门同时确认唯一)"); errorData.add(errorMessageMap); errorSum += 1; } else { Long employeeId = CollectionUtils.isNotEmpty(employeeSameIds) && employeeSameIds.size() == 1 ? employeeSameIds.get(0) : null; if (employeeId != null && employeeId > 0) { po.setEmployeeId(employeeId); } else { //姓名错误,系统内不存在该姓名 Map errorMessageMap = Maps.newHashMap(); errorMessageMap.put("message", rowIndex + "姓名错误,系统内不存在该姓名"); errorData.add(errorMessageMap); errorSum += 1; } } String taxAgentName = dto.getTaxAgentName(); if (StringUtils.isBlank(taxAgentName)) { //个税扣缴义务人不能为空 Map errorMessageMap = Maps.newHashMap(); errorMessageMap.put("message", rowIndex + "个税扣缴义务人不能为空"); errorData.add(errorMessageMap); errorSum += 1; } else { Optional optionalTemp = taxAgentList.stream().filter(m -> m.getTaxAgentName().equals(taxAgentName)).findFirst(); if (optionalTemp.isPresent()) { if (StringUtils.isNotEmpty(taxAgentId) && !optionalTemp.get().getTaxAgentId().equals(Long.valueOf(taxAgentId))) { //个税扣缴义务人与导入时选择的不一致 Map errorMessageMap = Maps.newHashMap(); errorMessageMap.put("message", rowIndex + "个税扣缴义务人与导入时选择的不一致"); errorData.add(errorMessageMap); errorSum += 1; } else { po.setTaxAgentId(optionalTemp.get().getTaxAgentId()); taxAgentEmployees = optionalTemp.get().getEmployeeList(); } } else { //个税扣缴义务人不存在 Map errorMessageMap = Maps.newHashMap(); errorMessageMap.put("message", rowIndex + "个税扣缴义务人不存在或不在权限范围内"); errorData.add(errorMessageMap); errorSum += 1; } } //商业健康保险 String businessHealthyInsurance = dto.getBusinessHealthyInsurance(); po.setBusinessHealthyInsurance(businessHealthyInsurance); //税延养老保险 String taxDelayEndowmentInsurance = dto.getTaxDelayEndowmentInsurance(); po.setTaxDelayEndowmentInsurance(taxDelayEndowmentInsurance); //其他 String otherDeduction = dto.getOtherDeduction(); po.setOtherDeduction(otherDeduction); //准予扣除的捐赠额 String deductionAllowedDonation = dto.getDeductionAllowedDonation(); po.setDeductionAllowedDonation(deductionAllowedDonation); // 分权判断 if (openDevolution) { OtherDeductionPO finalPoE = po; Optional optionalTaxAgentEmp = taxAgentEmployees.stream().filter(f -> f.getEmployeeId().equals(finalPoE.getEmployeeId())).findFirst(); if (!optionalTaxAgentEmp.isPresent()) { Map errorMessageMap = Maps.newHashMap(); errorMessageMap.put("message", rowIndex + "该条数据不在数据权限范围内,不可导入"); errorData.add(errorMessageMap); errorSum += 1; } } // 判断是否有核算过 if (CollectionUtils.isNotEmpty(salaryAcctEmployees)) { OtherDeductionPO finalPo = po; Optional optionalAcctEmp = salaryAcctEmployees.stream().filter(f -> f.getEmployeeId().equals(finalPo.getEmployeeId()) && f.getTaxAgentId().equals(finalPo.getTaxAgentId())).findFirst(); boolean isExist = list.stream().anyMatch(f -> f.getEmployeeId().equals(finalPo.getEmployeeId()) && f.getTaxAgentId().equals(finalPo.getTaxAgentId())); if (optionalAcctEmp.isPresent() && isExist) { Map errorMessageMap = Maps.newHashMap(); errorMessageMap.put("message", rowIndex + "该年月这条数据已经核算过,不可导入"); errorData.add(errorMessageMap); errorSum += 1; } } if (errorSum == 0) { successCount += 1; // 合格数据 eligibleData.add(po); } else { errorCount += 1; // 添加错误数据 } } //入库 OtherDeductionBiz.handleImportData(eligibleData); apidatas.put("successCount", successCount); apidatas.put("errorCount", errorCount); apidatas.put("errorData", errorData); } finally { IOUtils.closeQuietly(fileInputStream); } return apidatas; } private void checkImportParam(OtherDeductionImportParam importParam) { //excel文件id String imageId = Util.null2String(importParam.getImageId()); //税款所属期 String declareMonthStr = Util.null2String(importParam.getDeclareMonth()); //个税扣缴义务人 String taxAgentId = Util.null2String(importParam.getTaxAgentId()); if (StringUtils.isBlank(imageId)) { throw new SalaryRunTimeException("文件不存在"); } if (StringUtils.isBlank(declareMonthStr)) { throw new SalaryRunTimeException("税款所属期为空"); } } /** * 导出 * * @param param * @return */ public XSSFWorkbook export(OtherDeductionQueryParam param) { //获取操作按钮资源 List> rowList = getExcelRowList(param); //获取excel return ExcelUtil.genWorkbook(rowList, "其他免税扣除"); } /** * 获取excel数据行 * * @return 导出数据行集合 */ private List> getExcelRowList(OtherDeductionQueryParam param) { long employeeId = user.getUID(); //excel标题 List title = Arrays.asList("姓名", "个税扣缴义务人", "部门", "手机号", "工号", "证件号码", "入职日期", "商业健康保险", "税延养老保险", "其他", "准予扣除的捐赠额"); List list = getOtherDeductionMapper().list(param); OtherDeductionListDTOEncrypt.decryptOtherDeductionListDTOList(list); // 开启分权并且不是薪酬模块总管理员 if (getTaxAgentService(user).isOpenDevolution() && !getTaxAgentService(user).isChief(employeeId)) { List taxAgentIdsAsAdmin = getTaxAgentService(user).listAllTaxAgentsAsAdmin(employeeId).stream().map(TaxAgentPO::getId).collect(Collectors.toList()); list = list.stream().filter(f -> // 作为管理员 taxAgentIdsAsAdmin.contains(f.getTaxAgentId()) ).collect(Collectors.toList()); } final List> dataRowList = Optional.ofNullable(list) .map(List::stream) .map(operatorStream -> operatorStream.map(dto -> { List cellList = new ArrayList<>(); cellList.add(Util.null2String(dto.getUsername())); cellList.add(Util.null2String(dto.getTaxAgentName())); cellList.add(Util.null2String(dto.getDepartmentName())); cellList.add(Util.null2String(dto.getMobile())); cellList.add(Util.null2String(dto.getJobNum())); cellList.add(Util.null2String(dto.getIdNo())); cellList.add(Util.null2String(dto.getHiredate())); cellList.add(Util.null2String(dto.getBusinessHealthyInsurance())); cellList.add(Util.null2String(dto.getTaxDelayEndowmentInsurance())); cellList.add(Util.null2String(dto.getOtherDeduction())); cellList.add(Util.null2String(dto.getDeductionAllowedDonation())); return cellList; }).collect(Collectors.toList())) .orElse(Collections.emptyList()); List> rowList = new ArrayList<>(); rowList.add(title); rowList.addAll(dataRowList); return rowList; } /** * 导出详情列表 * * @param param * @return */ public XSSFWorkbook exportDetail(OtherDeductionQueryParam param) { OtherDeductionBiz biz = new OtherDeductionBiz(); EmployBiz employBiz = new EmployBiz(); Long id = param.getOtherTaxExemptDeductionId(); if (id == null) { throw new SalaryRunTimeException("id不能为空"); } OtherDeductionPO po = biz.getById(id); if (po == null) { throw new SalaryRunTimeException(String.format("其他免税扣除不存在" + "[id:%s]", id)); } List employeeList = employBiz.getEmployeeByIds(Collections.singletonList(po.getEmployeeId())); if (CollectionUtils.isEmpty(employeeList)) { throw new SalaryRunTimeException("员工信息不存在"); } //构建参数 param.setEmployeeId(po.getEmployeeId()); //申报月份 List declareMonth = param.getDeclareMonth(); if (CollectionUtils.isNotEmpty(declareMonth)) { param.setDeclareMonth(declareMonth.stream().map(e -> e + "-01 00:00:00").collect(Collectors.toList())); param.setDeclareMonthDate(declareMonth.stream().map(e -> e + "-01 00:00:00").map(SalaryDateUtil::dateStrToLocalTime).collect(Collectors.toList())); } //获取操作按钮资源 List> rowList = getExcelRowDetailList(param); //获取excel return ExcelUtil.genWorkbook(rowList, "其他免税扣除明细"); } /** * 导出详情 * * @param param * @return */ private List> getExcelRowDetailList(OtherDeductionQueryParam param) { //excel标题 List title = Arrays.asList("姓名", "申报月份", "个税扣缴义务人", "部门", "手机号", "工号", "商业健康保险", "税延养老保险", "其他", "准予扣除的捐赠额"); SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM"); //查询详细信息 List list = getOtherDeductionMapper().recordList(param); OtherDeductionRecordDTOEncrypt.decryptOtherDeductionRecordDTOList(list); final List> dataRowList = Optional.ofNullable(list) .map(List::stream) .map(operatorStream -> operatorStream.map(dto -> { List cellList = new ArrayList<>(); cellList.add(Util.null2String(dto.getUsername())); cellList.add(Util.null2String(dto.getDeclareMonth() == null ? "" : formatter.format(dto.getDeclareMonth()))); cellList.add(Util.null2String(dto.getTaxAgentName())); cellList.add(Util.null2String(dto.getDepartmentName())); cellList.add(Util.null2String(dto.getMobile())); cellList.add(Util.null2String(dto.getJobNum())); cellList.add(Util.null2String(dto.getBusinessHealthyInsurance())); cellList.add(Util.null2String(dto.getTaxDelayEndowmentInsurance())); cellList.add(Util.null2String(dto.getOtherDeduction())); cellList.add(Util.null2String(dto.getDeductionAllowedDonation())); return cellList; }).collect(Collectors.toList())) .orElse(Collections.emptyList()); List> rowList = new ArrayList<>(); rowList.add(title); rowList.addAll(dataRowList); return rowList; } @Override public List getOtherDeductionList(YearMonth declareMonth, List employeeIds) { if (declareMonth == null) { throw new SalaryRunTimeException(SalaryI18nUtil.getI18nLabel(100342, "参数有误:申报月份必传")); } OtherDeductionBiz OtherDeductionBiz = new OtherDeductionBiz(); return OtherDeductionBiz.listSome(OtherDeductionPO.builder().declareMonth(SalaryDateUtil.toDateStartOfMonth(declareMonth)).employeeIds(employeeIds).build()); } @Override public XSSFWorkbook downloadTemplate(OtherDeductionQueryParam param) { // 1.工作簿名称 String sheetName = SalaryI18nUtil.getI18nLabel(101604, "其他免税扣除导入模板"); String[] header = { SalaryI18nUtil.getI18nLabel(85429, "姓名"), SalaryI18nUtil.getI18nLabel(86184, "个税扣缴义务人"), SalaryI18nUtil.getI18nLabel(86185, "部门"), SalaryI18nUtil.getI18nLabel(86186, "手机号"), SalaryI18nUtil.getI18nLabel(86317, "工号"), SalaryI18nUtil.getI18nLabel(86318, "证件号码"), SalaryI18nUtil.getI18nLabel(86319, "入职日期"), SalaryI18nUtil.getI18nLabel(91238, "商业健康保险"), SalaryI18nUtil.getI18nLabel(91239, "税延养老保险"), SalaryI18nUtil.getI18nLabel(84500, "其他"), SalaryI18nUtil.getI18nLabel(91240, "准予扣除的捐赠额") }; // 2.表头 List> rows = new ArrayList<>(); List headerList = Arrays.asList(header); rows.add(headerList); // 4.注释 List excelComments = Lists.newArrayList(); excelComments.add(new ExcelComment(0, 0, 3, 2, SalaryI18nUtil.getI18nLabel(100344, "必填"))); excelComments.add(new ExcelComment(1, 0, 4, 2, SalaryI18nUtil.getI18nLabel(100344, "必填"))); excelComments.add(new ExcelComment(7, 0, 10, 2, SalaryI18nUtil.getI18nLabel(100344, "输入数字"))); excelComments.add(new ExcelComment(8, 0, 11, 2, SalaryI18nUtil.getI18nLabel(100344, "输入数字"))); excelComments.add(new ExcelComment(9, 0, 12, 2, SalaryI18nUtil.getI18nLabel(100344, "输入数字"))); excelComments.add(new ExcelComment(10, 0, 13, 2, SalaryI18nUtil.getI18nLabel(100344, "输入数字"))); XSSFWorkbook book = ExcelUtil.genWorkbookV2(rows, sheetName, excelComments); return book; } }