package com.engine.salary.service.impl; import com.engine.common.util.ServiceUtil; import com.engine.core.impl.Service; import com.engine.salary.biz.AddUpDeductionBiz; import com.engine.salary.biz.EmployBiz; import com.engine.salary.cmd.datacollection.AddUpDeductionGetSearchConditionCmd; import com.engine.salary.cmd.datacollection.AddUpDeductionImportCmd; import com.engine.salary.cmd.datacollection.AddUpDeductionPreviewCmd; import com.engine.salary.common.LocalDateRange; import com.engine.salary.entity.datacollection.AddUpDeduction; import com.engine.salary.entity.datacollection.DataCollectionEmployee; import com.engine.salary.entity.datacollection.dto.AddUpDeductionDTO; import com.engine.salary.entity.datacollection.dto.AddUpDeductionRecordDTO; import com.engine.salary.entity.datacollection.param.AddUpDeductionQueryParam; import com.engine.salary.entity.salaryacct.po.SalaryAcctEmployeePO; import com.engine.salary.entity.salaryacct.po.SalaryAcctRecordPO; import com.engine.salary.entity.taxagent.bo.TaxAgentBO; import com.engine.salary.entity.taxagent.dto.TaxAgentEmployeeDTO; import com.engine.salary.entity.taxagent.po.TaxAgentPO; import com.engine.salary.enums.salaryaccounting.SalaryAcctRecordStatusEnum; import com.engine.salary.exception.SalaryRunTimeException; import com.engine.salary.mapper.datacollection.AddUpDeductionMapper; import com.engine.salary.service.*; 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.ExcelUtil; import com.engine.salary.util.page.PageInfo; import com.engine.salary.util.page.SalaryPageUtil; import com.google.common.collect.Lists; import org.apache.commons.collections4.CollectionUtils; import org.apache.poi.xssf.usermodel.XSSFWorkbook; import weaver.general.Util; import weaver.hrm.User; import java.text.SimpleDateFormat; import java.time.LocalDate; import java.time.YearMonth; import java.util.*; import java.util.stream.Collectors; public class AddUpDeductionServiceImpl extends Service implements AddUpDeductionService { private AddUpDeductionMapper getAddUpDeductionMapper() { return MapperProxyFactory.getProxy(AddUpDeductionMapper.class); } private TaxAgentV2Service getTaxAgentV2Service(User user) { return ServiceUtil.getService(TaxAgentV2ServiceImpl.class, user); } private SalaryAcctRecordService getSalaryAcctRecordService(User user) { return ServiceUtil.getService(SalaryAcctRecordServiceImpl.class, user); } private SalaryAcctEmployeeService getSalaryAcctEmployeeService(User user) { return ServiceUtil.getService(SalaryAcctEmployeeServiceImpl.class, user); } private SalaryEmployeeService getSalaryEmployeeService(User user) { return ServiceUtil.getService(SalaryEmployeeServiceImpl.class, user); } @Override public Map getSearchCondition(Map params) { return commandExecutor.execute(new AddUpDeductionGetSearchConditionCmd(params, user)); } @Override public Map importAddUpDeduction(Map params) { return commandExecutor.execute(new AddUpDeductionImportCmd(params, user)); } @Override public Map preview(Map params) { return commandExecutor.execute(new AddUpDeductionPreviewCmd(params, user)); } @Override public AddUpDeduction getById(Long id) { return getAddUpDeductionMapper().getById(id); } // /** // * 获取作为修改者的最新记录 // * 说明:以人员id和个税口角义务人id去重 // * // * @param currentEmployeeId // * @return // */ // private List getLastListByModifier(Long currentEmployeeId) { // List list = new LambdaQueryChainWrapper<>(getAddUpDeductionMapper()) // .eq(AddUpDeductionPO::getDeleteType, 0) // .eq(AddUpDeductionPO::getTenantKey) // .eq(AddUpDeductionPO::getModifier, currentEmployeeId) // .orderByDesc(AddUpDeductionPO::getDeclareMonth) // .list(); // return list.stream() // .collect(Collectors.collectingAndThen(Collectors.toCollection(() -> new TreeSet<>(Comparator.comparing(f -> f.getEmployeeId() + "-" + f.getTaxAgentId()))), ArrayList::new)); // } @Override public PageInfo listPage(AddUpDeductionQueryParam queryParam) { long employeeId = (long) user.getUID(); // 未开启分权或是薪酬模块总管理员 if (!getTaxAgentV2Service(user).isOpenDevolution() || getTaxAgentV2Service(user).isChief(employeeId)) { SalaryPageUtil.start(queryParam.getCurrent(), queryParam.getPageSize()); List list = getAddUpDeductionMapper().list(queryParam); return new PageInfo<>(list, AddUpDeductionDTO.class); } else { List taxAgentEmployees = getTaxAgentV2Service(user).listTaxAgentAndEmployee(employeeId); List taxAgentIdsAsAdmin = getTaxAgentV2Service(user).listAllTaxAgentsAsAdmin(employeeId).stream().map(TaxAgentPO::getId).collect(Collectors.toList()); // List lastList = getLastListByModifier(employeeId); List list = getAddUpDeductionMapper().list(queryParam); list = list.stream().filter(f -> // 作为管理员 taxAgentIdsAsAdmin.contains(f.getTaxAgentId()) // 作为分管理员 || TaxAgentBO.checkTaxAgentAndEmployee(taxAgentEmployees, f.getTaxAgentId(), f.getEmployeeId()) // 自己最后修改过的,则可以看到最新和其历史 // || lastList.stream().anyMatch(l -> l.getEmployeeId().equals(f.getEmployeeId()) && l.getTaxAgentId().equals(f.getTaxAgentId()) && !l.getDeclareMonth().isBefore(f.getDeclareMonth())) ).collect(Collectors.toList()); // 填充总数和当页数据 // 分页参数 PageInfo dtoPage = SalaryPageUtil.buildPage(queryParam.getCurrent(), queryParam.getPageSize(), AddUpDeductionDTO.class); dtoPage.setTotal(list.size()); dtoPage.setList(SalaryPageUtil.subList(dtoPage.getPageNum(), dtoPage.getSize(), list)); return dtoPage; } } @Override public PageInfo recordListPage(AddUpDeductionQueryParam queryParam) { long employeeId = (long) user.getUID(); // 未开启分权或是薪酬模块总管理员 if (!getTaxAgentV2Service(user).isOpenDevolution() || getTaxAgentV2Service(user).isChief(employeeId)) { SalaryPageUtil.start(queryParam.getCurrent(), queryParam.getPageSize()); List list = getAddUpDeductionMapper().recordList(queryParam); return new PageInfo<>(list, AddUpDeductionRecordDTO.class); } else { List taxAgentEmployees = getTaxAgentV2Service(user).listTaxAgentAndEmployee(employeeId); List taxAgentIdsAsAdmin = getTaxAgentV2Service(user).listAllTaxAgentsAsAdmin(employeeId).stream().map(TaxAgentPO::getId).collect(Collectors.toList()); // List lastList = getLastListByModifier(employeeId, tenantKey); List list = getAddUpDeductionMapper().recordList(queryParam); list = list.stream().filter(f -> // 作为管理员 taxAgentIdsAsAdmin.contains(f.getTaxAgentId()) // 作为分管理员 || TaxAgentBO.checkTaxAgentAndEmployee(taxAgentEmployees, f.getTaxAgentId(), f.getEmployeeId()) // 自己最后修改过的,则可以看到最新和其历史 // || lastList.stream().anyMatch(l -> l.getEmployeeId().equals(f.getEmployeeId()) && l.getTaxAgentId().equals(f.getTaxAgentId()) && !l.getDeclareMonth().isBefore(f.getDeclareMonth())) ).collect(Collectors.toList()); // 分页参数 // 填充总数和当页数据 PageInfo dtoPage = SalaryPageUtil.buildPage(queryParam.getCurrent(), queryParam.getPageSize(), AddUpDeductionRecordDTO.class); dtoPage.setTotal(list.size()); dtoPage.setList(SalaryPageUtil.subList(dtoPage.getPageNum(), dtoPage.getSize(), list)); return dtoPage; } } /** * 导出详情列表 * * @return */ public XSSFWorkbook exportDetail(Long beLongEmployeeId, boolean isChief, AddUpDeductionQueryParam queryParam) { queryParam.setEmployeeId(beLongEmployeeId); EmployBiz employBiz = new EmployBiz(); AddUpDeductionBiz biz = new AddUpDeductionBiz(); Long id = queryParam.getAccumulatedSpecialAdditionalDeductionId(); if (id == null) { throw new SalaryRunTimeException("id不能为空"); } AddUpDeduction 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("员工信息不存在"); } //查询参数 queryParam.setEmployeeId(po.getEmployeeId()); //申报月份 List declareMonth = queryParam.getDeclareMonth(); if (CollectionUtils.isNotEmpty(declareMonth)) { queryParam.setDeclareMonth(declareMonth.stream().map(e -> e + "-01 00:00:00").collect(Collectors.toList())); } //获取操作按钮资源 List> rowList = getExcelRowDetailList(isChief, queryParam); //获取excel return ExcelUtil.genWorkbook(rowList, "累计专项附加扣除明细"); } /** * 导出详情 * * @param param * @return */ private List> getExcelRowDetailList(boolean isChief, AddUpDeductionQueryParam param) { long employeeId = user.getUID(); //excel标题 List title = Arrays.asList("姓名", "申报月份", "个税扣缴义务人", "部门", "工号", "累计子女教育", "累计继续教育", "累计住房贷款利息", "累计住房租金", "累计赡养老人", "累计大病医疗", "累计婴幼儿照护"); SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM"); //查询详细信息 List list = new AddUpDeductionBiz().recordList(param); 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.getJobNum())); cellList.add(Util.null2String(dto.getAddUpChildEducation())); cellList.add(Util.null2String(dto.getAddUpContinuingEducation())); cellList.add(Util.null2String(dto.getAddUpHousingLoanInterest())); cellList.add(Util.null2String(dto.getAddUpHousingRent())); cellList.add(Util.null2String(dto.getAddUpSupportElderly())); cellList.add(Util.null2String(dto.getAddUpIllnessMedical())); cellList.add(Util.null2String(dto.getAddUpInfantCare())); return cellList; }).collect(Collectors.toList())) .orElse(Collections.emptyList()); // 开启分权并且不是薪酬模块总管理员 if (getTaxAgentV2Service(user).isOpenDevolution() && !isChief) { List taxAgentEmployees = getTaxAgentV2Service(user).listTaxAgentAndEmployee(employeeId); List taxAgentIdsAsAdmin = getTaxAgentV2Service(user).listAllTaxAgentsAsAdmin(employeeId).stream().map(TaxAgentPO::getId).collect(Collectors.toList()); // List lastList = getLastListByModifier(employeeId, tenantKey); list = list.stream().filter(f -> // 作为管理员 taxAgentIdsAsAdmin.contains(f.getTaxAgentId()) // 作为分管理员 || TaxAgentBO.checkTaxAgentAndEmployee(taxAgentEmployees, f.getTaxAgentId(), f.getEmployeeId()) // 自己最后修改过的,则可以看到最新和其历史 // || lastList.stream().anyMatch(l -> l.getEmployeeId().equals(f.getEmployeeId()) && l.getTaxAgentId().equals(f.getTaxAgentId()) && !l.getDeclareMonth().isBefore(f.getDeclareMonth())) ).collect(Collectors.toList()); } List> rowList = new ArrayList<>(); rowList.add(title); rowList.addAll(dataRowList); return rowList; } @Override public List getAddUpDeductionList(YearMonth declareMonth, List employeeIds) { AddUpDeductionBiz addUpDeductionBiz = new AddUpDeductionBiz(); if (declareMonth == null) { throw new SalaryRunTimeException(SalaryI18nUtil.getI18nLabel(100342, "参数有误:申报月份、租户key必传")); } return addUpDeductionBiz.listSome(AddUpDeduction.builder().declareMonth(SalaryDateUtil.toDateStartOfMonth(declareMonth)).employeeIds(employeeIds).build()); } @Override public XSSFWorkbook downloadTemplate(boolean isChief, AddUpDeductionQueryParam queryParam) { String sheetName = SalaryI18nUtil.getI18nLabel(101603, "累计专项附加扣除导入模板"); 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(86321, "累计子女教育"), SalaryI18nUtil.getI18nLabel(86323, "累计继续教育"), SalaryI18nUtil.getI18nLabel(86324, "累计住房贷款利息"), SalaryI18nUtil.getI18nLabel(86325, "累计住房租金"), SalaryI18nUtil.getI18nLabel(86326, "累计赡养老人"), SalaryI18nUtil.getI18nLabel(105142, "累计大病医疗"), SalaryI18nUtil.getI18nLabel(105142, "累计婴幼儿照护") }; List headerList = Arrays.asList(header); List> rows = new ArrayList<>(); rows.add(headerList); // // 2.表头 // if (queryParam.getDeclareMonth() != null) { // queryParam.setDeclareMonth(queryParam.getDeclareMonth().stream().map(e -> e + "-01 00:00:00").collect(Collectors.toList())); // } // // 获取累计专项附加扣除 // List list = getAddUpDeductionMapper().list(queryParam); // // 人员信息赋值 // list.forEach(m -> { // // todo 身份证号 // m.setIdNo(""); // }); // // for (AddUpDeductionDTO dto : list) { // List row = new ArrayList<>(); // row.add(Util.null2String(dto.getUsername())); // row.add(Util.null2String(dto.getTaxAgentName())); // row.add(Util.null2String(dto.getDepartmentName())); // row.add(Util.null2String(dto.getMobile())); // row.add(Util.null2String(dto.getJobNum())); // row.add(Util.null2String(dto.getIdNo())); // row.add(Util.null2String(dto.getHiredate())); // row.add(Util.null2String(dto.getAddUpChildEducation())); // row.add(Util.null2String(dto.getAddUpContinuingEducation())); // row.add(Util.null2String(dto.getAddUpHousingLoanInterest())); // row.add(Util.null2String(dto.getAddUpHousingRent())); // row.add(Util.null2String(dto.getAddUpSupportElderly())); // row.add(Util.null2String(dto.getAddUpIllnessMedical())); // row.add(Util.null2String(dto.getAddUpInfantCare())); // rows.add(row); // } // 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, "输入数字"))); excelComments.add(new ExcelComment(11, 0, 14, 2, SalaryI18nUtil.getI18nLabel(100344, "输入数字"))); excelComments.add(new ExcelComment(12, 0, 15, 2, SalaryI18nUtil.getI18nLabel(100344, "输入数字"))); excelComments.add(new ExcelComment(13, 0, 16, 2, SalaryI18nUtil.getI18nLabel(100344, "输入数字"))); XSSFWorkbook book = ExcelUtil.genWorkbookV2(rows, sheetName, excelComments); return book; } @Override public XSSFWorkbook export(boolean isChief, AddUpDeductionQueryParam queryParam) { //获取操作按钮资源 List> rowList = getExcelRowList(isChief, queryParam); //获取excel return ExcelUtil.genWorkbook(rowList, "累计专项附加扣除"); } /** * 获取excel数据行 * * @return 导出数据行集合 */ private List> getExcelRowList(boolean isChief, AddUpDeductionQueryParam param) { Long employeeId = (long) user.getUID(); //excel标题 List title = Arrays.asList("姓名", "个税扣缴义务人", "部门", "手机号", "工号", "证件号码", "入职日期", "累计子女教育", "累计继续教育", "累计住房贷款利息", "累计住房租金", "累计赡养老人", "累计大病医疗", "累计婴幼儿照护"); List list = new AddUpDeductionBiz().list(param); // 开启分权并且不是薪酬模块总管理员 if (getTaxAgentV2Service(user).isOpenDevolution() && !isChief) { List taxAgentEmployees = getTaxAgentV2Service(user).listTaxAgentAndEmployee(employeeId); List taxAgentIdsAsAdmin = getTaxAgentV2Service(user).listAllTaxAgentsAsAdmin(employeeId).stream().map(TaxAgentPO::getId).collect(Collectors.toList()); // List lastList = getLastListByModifier(employeeId); list = list.stream().filter(f -> // 作为管理员 taxAgentIdsAsAdmin.contains(f.getTaxAgentId()) // 作为分管理员 || TaxAgentBO.checkTaxAgentAndEmployee(taxAgentEmployees, f.getTaxAgentId(), f.getEmployeeId()) // 自己最后修改过的,则可以看到最新和其历史 // || lastList.stream().anyMatch(l->l.getEmployeeId().equals(f.getEmployeeId()) && l.getTaxAgentId().equals(f.getTaxAgentId()) && !l.getDeclareMonth().isBefore(f.getDeclareMonth())) ).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.getAddUpChildEducation())); cellList.add(Util.null2String(dto.getAddUpContinuingEducation())); cellList.add(Util.null2String(dto.getAddUpHousingLoanInterest())); cellList.add(Util.null2String(dto.getAddUpHousingRent())); cellList.add(Util.null2String(dto.getAddUpSupportElderly())); cellList.add(Util.null2String(dto.getAddUpIllnessMedical())); cellList.add(Util.null2String(dto.getAddUpInfantCare())); return cellList; }).collect(Collectors.toList())) .orElse(Collections.emptyList()); List> rowList = new ArrayList<>(); rowList.add(title); rowList.addAll(dataRowList); return rowList; } /** * 根据年月获取已核算数据 * * @param yearMonth * @return */ @Override public List getAccountedEmployeeData(String yearMonth) { List list = Lists.newArrayList(); LocalDate salaryMonthDate = LocalDate.parse(yearMonth + "-01", SalaryDateUtil.DATE_FORMATTER); List salaryAcctRecords = getSalaryAcctRecordService(user).listBySalaryMonth(LocalDateRange.builder().fromDate(SalaryDateUtil.localDateToDate(salaryMonthDate)).endDate(SalaryDateUtil.localDateToDate(salaryMonthDate)).build()); salaryAcctRecords.forEach(e -> { boolean isAccounted = e.getStatus() > SalaryAcctRecordStatusEnum.NOT_ARCHIVED.getValue(); if (isAccounted) { list.addAll(getSalaryAcctEmployeeService(user).listBySalaryAcctRecordIds(Collections.singleton(salaryAcctRecords.get(0).getId()))); } }); return list; } }