package com.engine.salary.biz; import com.engine.salary.entity.datacollection.AddUpSituation; import com.engine.salary.entity.datacollection.dto.AddUpSituationDTO; import com.engine.salary.entity.datacollection.dto.AddUpSituationRecordDTO; import com.engine.salary.entity.datacollection.param.AddUpSituationQueryParam; import com.engine.salary.mapper.datacollection.AddUpSituationMapper; import com.engine.salary.util.excel.ExcelUtil; import org.apache.commons.collections4.CollectionUtils; import org.apache.ibatis.session.SqlSession; import org.apache.poi.xssf.usermodel.XSSFWorkbook; import weaver.conn.mybatis.MyBatisFactory; import weaver.general.BaseBean; import weaver.general.Util; import java.text.SimpleDateFormat; import java.util.*; import java.util.stream.Collectors; public class AddUpSituationBiz extends BaseBean { /** * 关联查询查询列表 * * @param param * @return */ public List list(AddUpSituationQueryParam param) { SqlSession sqlSession = MyBatisFactory.sqlSessionFactory.openSession(); try { AddUpSituationMapper mapper = sqlSession.getMapper(AddUpSituationMapper.class); return mapper.list(param); } finally { sqlSession.close(); } } /** * 条件查询 * * @param param * @return */ public List listSome(AddUpSituation param) { SqlSession sqlSession = MyBatisFactory.sqlSessionFactory.openSession(); try { AddUpSituationMapper mapper = sqlSession.getMapper(AddUpSituationMapper.class); return mapper.listSome(param); } finally { sqlSession.close(); } } /** * 根据id获取 * * @param id * @return */ public AddUpSituation getById(Long id) { SqlSession sqlSession = MyBatisFactory.sqlSessionFactory.openSession(); try { AddUpSituationMapper mapper = sqlSession.getMapper(AddUpSituationMapper.class); return mapper.getById(id); } finally { sqlSession.close(); } } /** * 详情列表 * * @param param * @return */ public List recordList(AddUpSituationQueryParam param) { SqlSession sqlSession = MyBatisFactory.sqlSessionFactory.openSession(); try { AddUpSituationMapper mapper = sqlSession.getMapper(AddUpSituationMapper.class); return mapper.recordList(param); } finally { sqlSession.close(); } } /** * 批量插入 * * @param param * @return */ public void batchSave(List param) { SqlSession sqlSession = MyBatisFactory.sqlSessionFactory.openSession(); try { AddUpSituationMapper mapper = sqlSession.getMapper(AddUpSituationMapper.class); mapper.insertData(param); sqlSession.commit(); } finally { sqlSession.close(); } } /** * 批量插入 * * @param param * @return */ public void batchUpdate(List param) { SqlSession sqlSession = MyBatisFactory.sqlSessionFactory.openSession(); try { AddUpSituationMapper mapper = sqlSession.getMapper(AddUpSituationMapper.class); mapper.updateData(param); sqlSession.commit(); } finally { sqlSession.close(); } } /** * 导出 * * @param param * @return */ public XSSFWorkbook export(AddUpSituationQueryParam param) { //获取操作按钮资源 List> rowList = getExcelRowList(param); //获取excel return ExcelUtil.genWorkbook(rowList,"累计情况"); } /** * 获取excel数据行 * * @return 导出数据行集合 */ private List> getExcelRowList(AddUpSituationQueryParam param) { // excel标题 final List title = Arrays.asList("姓名", "个税扣缴义务人", "部门", "手机号", "工号", "证件号码", "入职日期", "累计收入额", "累计减除费用", "累计社保个人合计", "累计公积金个人合计", "累计子女教育", "累计继续教育", "累计住房贷款利息", "累计住房租金", "累计赡养老人", "累计企业(职业)年金及其他福利", "累计其他扣除", "累计免税收入", "累计准予扣除的捐赠额", "累计已预扣预缴税额"); SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd"); List list = list(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.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(dto.getHiredate() == null ? "" : formatter.format(dto.getHiredate())); cellList.add(String.valueOf(dto.getAddUpIncome())); cellList.add(String.valueOf(dto.getAddUpSubtraction())); cellList.add(String.valueOf(dto.getAddUpSocialSecurityTotal())); cellList.add(String.valueOf(dto.getAddUpAccumulationFundTotal())); cellList.add(String.valueOf(dto.getAddUpChildEducation())); cellList.add(String.valueOf(dto.getAddUpContinuingEducation())); cellList.add(String.valueOf(dto.getAddUpHousingLoanInterest())); cellList.add(String.valueOf(dto.getAddUpHousingRent())); cellList.add(String.valueOf(dto.getAddUpSupportElderly())); cellList.add(String.valueOf(dto.getAddUpEnterpriseAndOther())); cellList.add(String.valueOf(dto.getAddUpOtherDeduction())); cellList.add(String.valueOf(dto.getAddUpTaxExemptIncome())); cellList.add(String.valueOf(dto.getAddUpAllowedDonation())); cellList.add(String.valueOf(dto.getAddUpAdvanceTax())); 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(AddUpSituationQueryParam param) { //获取操作按钮资源 List> rowList = getExcelRowDetailList(param); //获取excel return ExcelUtil.genWorkbook(rowList,"累计情况明细"); } /** * 导出详情 * * @param param * @return */ private List> getExcelRowDetailList(AddUpSituationQueryParam param) { //excel标题 List title = Arrays.asList("姓名", "税款所属期", "个税扣缴义务人", "部门", "手机号", "工号", "累计收入额", "累计减除费用", "累计社保个人合计", "累计公积金个人合计", "累计子女教育", "累计继续教育", "累计住房贷款利息", "累计住房租金", "累计赡养老人", "累计企业(职业)年金及其他福利", "累计其他扣除", "累计免税收入", "累计准予扣除的捐赠额", "累计已预扣预缴税额"); SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM"); //查询详细信息 List list = 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.getTaxYearMonth() == null ? "" : formatter.format(dto.getTaxYearMonth()))); cellList.add(Util.null2String(dto.getTaxAgentName())); cellList.add(Util.null2String(dto.getDepartmentName())); cellList.add(Util.null2String(dto.getJobNum())); cellList.add(String.valueOf(dto.getAddUpChildEducation())); cellList.add(String.valueOf(dto.getAddUpContinuingEducation())); cellList.add(String.valueOf(dto.getAddUpHousingLoanInterest())); cellList.add(String.valueOf(dto.getAddUpHousingRent())); cellList.add(String.valueOf(dto.getAddUpSupportElderly())); return cellList; }).collect(Collectors.toList())) .orElse(Collections.emptyList()); List> rowList = new ArrayList<>(); rowList.add(title); rowList.addAll(dataRowList); return rowList; } public void handleImportData(List pos) { if (CollectionUtils.isEmpty(pos)) { return; } AddUpSituation po = pos.get(0); // 多条相同人的则以第一条为准,如果逆序排列(用于重复的则以最后一条为准)Collections.reverse(pos); // 去重(通过记录的唯一条件(申报月份,人员id,个税扣缴义务人id)拼接) List finalPos = pos.stream().collect(Collectors.collectingAndThen(Collectors.toCollection(() -> new TreeSet<>(Comparator.comparing(f -> f.getEmployeeId() + "-" + f.getTaxAgentId()))), ArrayList::new)); // 查询已有数据 List list = listSome(AddUpSituation.builder().taxYearMonth(po.getTaxYearMonth()).build()); // 待修改的 本地已存在则更新【交集】 List updateList = list.stream().map(m -> { Optional optional = finalPos.stream().filter(p -> (p.getEmployeeId() + "-" + p.getTaxAgentId()).equals(m.getEmployeeId() + "-" + m.getTaxAgentId())).findFirst(); AddUpSituation temp = null; if (optional.isPresent()) { temp = optional.get(); // 换成本地库的id temp.setId(m.getId()); } return temp; }).filter(Objects::nonNull).collect(Collectors.toList()); // 待新增的 导入比本地多,则新增【差集(导入 - local)】 List saveList = finalPos.stream().map(m -> { Optional optional = list.stream().filter(p -> (p.getEmployeeId() + "-" + p.getTaxAgentId()).equals(m.getEmployeeId() + "-" + m.getTaxAgentId())).findFirst(); AddUpSituation temp = null; if (!optional.isPresent()) { temp = m; } return temp; }).filter(Objects::nonNull).collect(Collectors.toList()); // 修改 if (CollectionUtils.isNotEmpty(updateList)) { batchUpdate(updateList); } // 保存 if (CollectionUtils.isNotEmpty(saveList)) { batchSave(saveList); } // 记录操作日志 // saveList.addAll(updateList); // // if (CollectionUtils.isNotEmpty(saveList)) { // LoggerContext loggerContext = new LoggerContext(); // loggerContext.setTargetId(String.valueOf(IdGenerator.generate())); // loggerContext.setTargetName(SalaryI18nUtil.getI18nLabel(message.getTenantKey(), message.getUserId(), 100351, "导入累计专项附加扣除")); // loggerContext.setOperateType(OperateTypeEnum.ADD.getValue()); // loggerContext.setOperateTypeName(SalaryI18nUtil.getI18nLabel(message.getTenantKey(), message.getUserId(), 100351, "导入累计专项附加扣除")); // loggerContext.setOperatedesc(SalaryI18nUtil.getI18nLabel(message.getTenantKey(), message.getUserId(), 100351, "导入累计专项附加扣除")); // loggerContext.setNewValueList(saveList); // loggerContext.setTenant_key(message.getTenantKey()); // loggerContext.setOperator(message.getUserId().toString()); // loggerContext.setOperatorName(message.getOpreator()); // loggerContext.setClientIp(message.getClientIp()); // AddUpSituationLoggerTemplate.write(loggerContext); // } } }