This commit is contained in:
zhuyy 2023-07-20 14:13:04 +08:00
parent c2b1dff5cf
commit 4fe9de70ae
8 changed files with 247 additions and 10 deletions

28
pom.xml
View File

@ -233,6 +233,34 @@
<version>1.1.0</version>
</dependency>
<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>ooxml-schemas</artifactId>
<version>1.1</version>
</dependency>
<dependency>
<groupId>cn.afterturn</groupId>
<artifactId>easypoi-base</artifactId>
<version>4.1.2</version>
</dependency>
<dependency>
<groupId>cn.afterturn</groupId>
<artifactId>easypoi-web</artifactId>
<version>4.1.2</version>
</dependency>
<dependency>
<groupId>cn.afterturn</groupId>
<artifactId>easypoi-annotation</artifactId>
<version>4.1.2</version>
</dependency>
<!--pdf-->
<dependency>
<groupId>org.docx4j</groupId>
<artifactId>docx4j-export-fo</artifactId>
<version>6.1.0</version>
</dependency>
</dependencies>
<build>

View File

@ -1,6 +1,5 @@
package com.ydool.staff.controller;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import com.github.xiaoymin.knife4j.annotations.ApiSupport;
import com.github.xiaoymin.knife4j.annotations.DynamicParameter;
@ -9,7 +8,6 @@ import com.ydool.common.base.BaseController;
import com.ydool.common.constant.UrlConstant;
import com.ydool.common.data.dto.AjaxResult;
import com.ydool.staff.dto.RecruitmentDto;
import com.ydool.staff.entity.Audit;
import com.ydool.staff.request.RecruitmentRequest;
import com.ydool.staff.service.IAuditService;
import com.ydool.staff.service.IRecruitmentService;
@ -20,6 +18,8 @@ import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
@RestController
@RequestMapping(UrlConstant.RECRUITMENT)
@ -65,4 +65,10 @@ public class RecruitmentController extends BaseController {
public AjaxResult waiting() {
return auditService.waitingAudit(getPage());
}
@GetMapping
@ApiOperation("招聘导出")
public void export(String id, HttpServletRequest request, HttpServletResponse response) {
recruitmentService.export(id, request, response);
}
}

View File

@ -0,0 +1,26 @@
package com.ydool.staff.dto;
import io.swagger.annotations.ApiModelProperty;
import lombok.Data;
import java.util.List;
@Data
public class ExportRecruitmentDto {
private String id;
@ApiModelProperty(value = "企业名称")
private String companyName;
@ApiModelProperty(value = "原核定用工指标人数")
private Long checkNumber;
@ApiModelProperty(value = "在职人数")
private Long activeStaff;
@ApiModelProperty(value = "剩余指标人数")
private Long residualNumber;
private List<RecruitmentListDto> recruitmentListDtoList;
}

View File

@ -0,0 +1,26 @@
package com.ydool.staff.dto;
import io.swagger.annotations.ApiModelProperty;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
@Data
@AllArgsConstructor
@NoArgsConstructor
public class RecruitmentListDto {
private String id;
@ApiModelProperty(value = "部门")
private String deptId;
@ApiModelProperty(value = "岗位职务")
private String position;
@ApiModelProperty(value = "名额")
private Long places;
@ApiModelProperty(value = "条件要求")
private String requirements;
}

View File

@ -0,0 +1,33 @@
package com.ydool.staff.excel;
import com.alibaba.excel.annotation.ExcelProperty;
import com.alibaba.excel.annotation.write.style.ColumnWidth;
import com.alibaba.excel.annotation.write.style.ContentStyle;
import com.alibaba.excel.annotation.write.style.HeadRowHeight;
import com.alibaba.excel.annotation.write.style.HeadStyle;
import lombok.Data;
import org.apache.poi.ss.usermodel.HorizontalAlignment;
import org.apache.poi.ss.usermodel.VerticalAlignment;
@Data
@HeadRowHeight(20)
@ColumnWidth(20)
@HeadStyle(horizontalAlignment = HorizontalAlignment.CENTER)
@ContentStyle(verticalAlignment = VerticalAlignment.CENTER)
public class ExcelRecruitment {
@ExcelProperty("企业名称")
private String companyId;
@ExcelProperty("部门")
private String deptId;
@ExcelProperty("岗位")
private String position;
@ExcelProperty("名额")
private Long places;
@ExcelProperty("要求")
private String requirements;
}

View File

@ -1,13 +1,11 @@
package com.ydool.staff.service;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.ydool.common.data.dto.AjaxResult;
import com.ydool.staff.entity.Recruitment;
import com.ydool.staff.request.RecruitmentRequest;
import com.ydool.staff.request.RecruitmentSearch;
import com.ydool.system.request.IdsRequest;
import java.util.List;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
public interface IRecruitmentService {
@ -32,4 +30,6 @@ public interface IRecruitmentService {
*/
AjaxResult selectRecruitmentById(String id);
void export(String id, HttpServletRequest request, HttpServletResponse response);
}

View File

@ -1,17 +1,21 @@
package com.ydool.staff.service.impl;
import cn.afterturn.easypoi.word.WordExportUtil;
import cn.dev33.satoken.stp.StpUtil;
import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.core.date.DateUtil;
import cn.hutool.core.util.NumberUtil;
import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.spire.doc.Document;
import com.spire.doc.FileFormat;
import com.ydool.common.cache.ConfigCache;
import com.ydool.common.constant.ArgsConst;
import com.ydool.common.data.dto.AjaxResult;
import com.ydool.common.exception.ResultException;
import com.ydool.staff.dto.RecruitmentDto;
import com.ydool.staff.entity.*;
import com.ydool.common.base.BaseService;
@ -25,14 +29,20 @@ import com.ydool.system.entity.User;
import com.ydool.system.mapper.ConfigMapper;
import com.ydool.system.mapper.UserMapper;
import com.ydool.system.request.IdsRequest;
import com.ydool.system.service.impl.DeptServiceImpl;
import org.apache.poi.xwpf.usermodel.XWPFDocument;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.Assert;
import java.util.Arrays;
import java.util.List;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.*;
import java.net.URLDecoder;
import java.net.URLEncoder;
import java.util.*;
import java.util.concurrent.atomic.AtomicInteger;
import java.util.stream.Collectors;
/**
* <p>
@ -47,6 +57,7 @@ public class RecruitmentServiceImpl extends BaseService<RecruitmentMapper, Recru
private static final AtomicInteger counter = new AtomicInteger(0);
private final String filepath = System.getProperty("user.dir");
@Autowired
RecruitmentMapper recruitmentMapper;
@Autowired
@ -61,6 +72,8 @@ public class RecruitmentServiceImpl extends BaseService<RecruitmentMapper, Recru
private ConfigMapper configMapper;
@Autowired
UserMapper userMapper;
@Autowired
private DeptServiceImpl deptService;
public static String getNextId() {
int value = counter.incrementAndGet();
@ -125,5 +138,110 @@ public class RecruitmentServiceImpl extends BaseService<RecruitmentMapper, Recru
return AjaxResult.ok().data(recruitmentRequest);
}
@Override
public void export(String id, HttpServletRequest request, HttpServletResponse response) {
if (StrUtil.isBlank(id)) {
throw new ResultException("id不能为空");
}
File uploadFile = new File(filepath);
String wordDir = "";
if (!uploadFile.exists()) {
uploadFile.mkdirs();
}
if (!filepath.endsWith("/")) {
wordDir = filepath + "/upload/word";
}
File tf = null;
try {
tf = new File(URLDecoder.decode(Objects.requireNonNull(Thread.currentThread().getContextClassLoader().getResource("doc/象山县国有企业人员招聘报备表.docx")).getPath(), "UTF-8"));
} catch (UnsupportedEncodingException e) {
throw new RuntimeException(e);
}
System.out.println("tf.getPath() = " + tf.getPath());
Organization organization = organizationMapper.selectById(id);
CompanyName companyName = companyNameMapper.selectById(organization.getCompanyId());
List<Recruitment> recruitments = recruitmentMapper.selectList(Wrappers.lambdaQuery(Recruitment.class).eq(Recruitment::getCompanyId, organization.getId()));
long sum = recruitments.stream().mapToLong(Recruitment::getPlaces).sum();
Map<String, Object> params = new HashMap<>();
params.put("name", companyName.getCompanyName());
params.put("check", organization.getCheckNumber());
params.put("staff", organization.getActiveStaff());
params.put("number", organization.getResidualNumber());
params.put("sum", sum);
params.put("list", recruitments);
String fileName = "象山县国有企业人员招聘报备表.docx";
String word = exportWord(tf.getPath(), wordDir, fileName, params);
String pdfFileName = "象山县国有企业人员招聘报备表.pdf";
Document document = new Document();
document.loadFromFile(word);
document.saveToFile(wordDir + "/" + pdfFileName, FileFormat.PDF);
try {
response.setContentType("application/force-download");// 设置强制下载不打开
response.setHeader("Content-Disposition", "attachment;filename=" + URLEncoder.encode(pdfFileName, "UTF-8")); // 中文名称下载
} catch (UnsupportedEncodingException e) {
throw new RuntimeException(e);
}
byte[] buffer = new byte[1024];
FileInputStream fis = null;
BufferedInputStream bis = null;
OutputStream outputStream = null;
try {
fis = new FileInputStream(wordDir + File.separator + pdfFileName);
bis = new BufferedInputStream(fis);
outputStream = response.getOutputStream();
int i = bis.read(buffer);
while (i != -1) {
outputStream.write(buffer, 0, i);
i = bis.read(buffer);
}
outputStream.flush();
} catch (Exception e) {
e.printStackTrace();
} finally {
try {
if (outputStream != null) {
outputStream.close();
}
if (bis != null) {
bis.close();
}
if (fis != null) {
fis.close();
}
} catch (IOException e) {
e.printStackTrace();
}
}
}
public static String exportWord(String templatePath, String saveDir, String fileName, Map<String, Object> params) {
org.springframework.util.Assert.notNull(templatePath, "模板路径不能为空");
org.springframework.util.Assert.notNull(saveDir, "临时文件路径不能为空");
org.springframework.util.Assert.notNull(fileName, "导出文件名不能为空");
Assert.isTrue(fileName.endsWith(".docx"), "word导出请使用docx格式");
if (!saveDir.endsWith("/")) {
saveDir = saveDir + File.separator;
}
File dir = new File(saveDir);
if (!dir.exists()) {
dir.mkdirs();
}
String savePath = saveDir + fileName;
try {
XWPFDocument doc = WordExportUtil.exportWord07(templatePath, params);
FileOutputStream fos = new FileOutputStream(savePath);
doc.write(fos);
fos.flush();
fos.close();
} catch (Exception e) {
e.printStackTrace();
}
return savePath;
}
}