Commit 08de3d98 authored by licc's avatar licc

新增方案上传接口

parent 73821433
......@@ -7,7 +7,7 @@ import java.util.List;
import java.util.Map;
public interface SchemeMapper extends BaseMapper<SchemeInfo> {
SchemeInfo add(SchemeInfo schemeInfo);
int add(SchemeInfo schemeInfo);
List<SchemeInfo> getList(Map<String,Object> map);
}
package cn.wisenergy.model.app;
import com.alibaba.excel.annotation.ExcelIgnoreUnannotated;
import com.alibaba.excel.annotation.ExcelProperty;
import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Data;
import lombok.NoArgsConstructor;
import java.io.Serializable;
import java.util.Date;
......@@ -13,7 +18,11 @@ import java.util.Date;
* @ Date : 2021/1/7 15:15
*/
@Data
@Builder
@ApiModel(value = "Volunteer")
@NoArgsConstructor
@AllArgsConstructor
@ExcelIgnoreUnannotated
public class Volunteer implements Serializable {
private static final long serialVersionUID = -7880251929353475087L;
......@@ -27,60 +36,70 @@ public class Volunteer implements Serializable {
* 专业名称
*/
@ApiModelProperty(value = "专业名称", name = "majorName")
@ExcelProperty(value = "姓名")
private String majorName;
/**
* 院校
*/
@ApiModelProperty(value = "院校", name = "academy")
@ExcelProperty(value = "姓名")
private String academy;
/**
* 选考科目要求
*/
@ApiModelProperty(value = "选考科目要求", name = "courseDemand")
@ExcelProperty(value = "姓名")
private String courseDemand;
/**
* 学校性质
*/
@ApiModelProperty(value = "学校性质", name = "nature")
@ExcelProperty(value = "姓名")
private String nature;
/**
* 学年制(年)
*/
@ApiModelProperty(value = "学年制(年)", name = "yearLimit")
@ExcelProperty(value = "姓名")
private Integer yearLimit;
/**
* 计划投档
* 计划
*/
@ApiModelProperty(value = "计划投档", name = "planNum")
@ApiModelProperty(value = "计划数", name = "planNum")
@ExcelProperty(value = "姓名")
private Integer planNum;
/**
* 投档计划
* 计划投档
*/
@ApiModelProperty(value = "投档计划数", name = "投档计划数")
@ApiModelProperty(value = "计划投档数", name = "castArchivesNum")
@ExcelProperty(value = "姓名")
private Integer castArchivesNum;
/**
* 投出数
* 投出数
*/
@ApiModelProperty(value = "投出数量", name = "launchNum")
@ApiModelProperty(value = "投出数", name = "launchNum")
@ExcelProperty(value = "姓名")
private Integer launchNum;
/**
* 最低分
*/
@ApiModelProperty(value = "最低分", name = "lowestMark")
@ExcelProperty(value = "姓名")
private String lowestMark;
/**
* 最低位次
*/
@ApiModelProperty(value = "最低位次", name = "lowestRank")
@ExcelProperty(value = "姓名")
private String lowestRank;
/**
......
......@@ -3,6 +3,10 @@ package cn.wisenergy.service.app;
import cn.wisenergy.common.utils.R;
import cn.wisenergy.model.vo.SchemeQueryVo;
import cn.wisenergy.model.vo.VolunteerVo;
import org.springframework.web.multipart.MultipartFile;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
/**
......
......@@ -2,10 +2,17 @@ package cn.wisenergy.service.app;
import cn.wisenergy.common.utils.R;
import cn.wisenergy.model.app.Volunteer;
import com.baomidou.mybatisplus.extension.service.IService;
import org.springframework.web.multipart.MultipartFile;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;
public interface VolunteerService {
/**
* @author 86187
*/
public interface VolunteerService extends IService<Volunteer> {
/**
* 批量添加志愿
*
......@@ -13,4 +20,14 @@ public interface VolunteerService {
* @return true 成功 false 失败
*/
R<Boolean> batchAdd(List<Volunteer> list);
/**
* Excel批量添加方案志愿
* @param file 志愿文件
* @param type 方案类型
* @param schemeName 方案名称
* @param response 相应数据
* @throws IOException 异常
*/
void excelAdd(MultipartFile file, Integer type, String schemeName, HttpServletResponse response) throws IOException;
}
package cn.wisenergy.service.app.impl;
import cn.wisenergy.common.expection.BaseException;
import cn.wisenergy.common.utils.R;
import cn.wisenergy.mapper.SchemeMapper;
import cn.wisenergy.mapper.VolunteerMapper;
import cn.wisenergy.model.app.SchemeInfo;
import cn.wisenergy.model.app.Volunteer;
import cn.wisenergy.service.app.VolunteerService;
import cn.wisenergy.service.common.VolunteerListener;
import com.alibaba.excel.EasyExcel;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.multipart.MultipartFile;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.Date;
import java.util.List;
/**
*@ Description: 志愿接口实现
*@ Author : 86187
*@ Date : 2021/1/8 9:44
*/
* @ Description: 志愿接口实现
* @ Author : 86187
* @ Date : 2021/1/8 9:44
*/
@Service
@Slf4j
public class VolunteerServiceImpl extends ServiceImpl<VolunteerMapper, Volunteer> implements VolunteerService {
@Autowired
private VolunteerMapper volunteerMapper;
@Autowired
private SchemeMapper schemeMapper;
@Override
public R<Boolean> batchAdd(List<Volunteer> list) {
return null;
}
@Override
public void excelAdd(MultipartFile file, Integer type, String schemeName, HttpServletResponse response) throws IOException {
//判断文件类型=
boolean valid = this.validContentType(file.getOriginalFilename());
if (!valid) {
throw new BaseException("请传入Excel文件");
}
//1、保存方案信息
SchemeInfo schemeInfo = new SchemeInfo();
schemeInfo.setSchemeName(schemeName);
schemeInfo.setType(type);
schemeInfo.setUploadTime(new Date());
schemeInfo.setIsDelete(0);
saveSchemeAndVolunteer(schemeInfo,file);
}
/**
* 保存方案和志愿信息
*
* @param schemeInfo 方案信息
* @param file 志愿文件
* @throws IOException 异常
*/
@Transactional(rollbackFor =Exception.class )
public void saveSchemeAndVolunteer(SchemeInfo schemeInfo, MultipartFile file) throws IOException {
//1、保存方案信息
int count = schemeMapper.add(schemeInfo);
if (count == 0) {
throw new RuntimeException("保存方案信息失败!");
}
//2.读取数据进行入库操作
EasyExcel.read(file.getInputStream(), Volunteer.class, new VolunteerListener(this)).sheet().doRead();
}
/**
* 判断文件类型
*
* @param fileName 文件名
* @return true or false
*/
public boolean validContentType(String fileName) {
if (StringUtils.isBlank(fileName)) {
return false;
}
String substring = fileName.substring(fileName.lastIndexOf(".") + 1);
if (substring.equals("xlsx") || substring.equals("xls")) {
return true;
}
return false;
}
}
package cn.wisenergy.service.common;
import cn.wisenergy.model.app.Volunteer;
import cn.wisenergy.service.app.VolunteerService;
import com.alibaba.excel.context.AnalysisContext;
import com.alibaba.excel.event.AnalysisEventListener;
import com.alibaba.excel.metadata.Head;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import lombok.extern.slf4j.Slf4j;
import org.springframework.util.CollectionUtils;
import org.springframework.util.StringUtils;
import java.util.*;
import java.util.stream.Collectors;
/**
* Excel批量添加社区居民 监听器
*/
@Slf4j
public class VolunteerListener extends AnalysisEventListener<Volunteer> {
private List<Volunteer> list;
private VolunteerService volunteerService;
private final int batch = 20000;
/**
* 导入模板头内容
*/
private Map<Integer, String> headMap = new HashMap<>();
/**
* 结果标记 默认错误
*/
private boolean isCorrectFormat = true;
public VolunteerListener(VolunteerService volunteerService) {
this.volunteerService = volunteerService;
list = new ArrayList<>();
}
/**
* 这个每一条数据解析都会来调用 ==
*
* @param volunteer 志愿信息
* @param analysisContext
*/
@Override
public void invoke(Volunteer volunteer, AnalysisContext analysisContext) {
if (!isCorrectFormat) {
//格式是否正确
return;
}
log.info("解析到一条志愿数据>>>>>>>>>>:{}", volunteer);
//此处做校验 本次没有校验
if (volunteer != null) {
if (StringUtils.isEmpty(volunteer.getMajorName())) {
throw new RuntimeException("专业不能为空!");
}
if (StringUtils.isEmpty(volunteer.getAcademy())) {
throw new RuntimeException("院校不能为空!");
}
if (StringUtils.isEmpty(volunteer.getCourseDemand())) {
throw new RuntimeException("选考科目要求不能为空!");
}
if (StringUtils.isEmpty(volunteer.getNature())) {
throw new RuntimeException("学校不能为空!");
}
if (null == volunteer.getYearLimit()) {
throw new RuntimeException("学年制不能为空!");
}
if (null == volunteer.getPlanNum()) {
throw new RuntimeException("计划数数不能为空!");
}
if (null == volunteer.getCastArchivesNum()) {
throw new RuntimeException("计划投档数不能为空!");
}
if (null == volunteer.getLaunchNum()) {
throw new RuntimeException("投出数不能为空!");
}
if (StringUtils.isEmpty(volunteer.getLowestMark())) {
throw new RuntimeException("最低分不能为空!");
}
if (StringUtils.isEmpty(volunteer.getLowestRank())) {
throw new RuntimeException("最低位次不能为空!");
}
list.add(volunteer);
}
//每500条插入一次数据库
if (list.size() >= batch) {
//判断数据库是否已有手机号的数据
log.info("invoke批量插入志愿数据,条数为>>>>>>>>>>:{}", list.size());
if (list.size() > 0) {
volunteerService.saveBatch(list);
}
//保存后清除,防止大量数据导致内存耗尽
list.clear();
}
}
/**
* 所有数据解析完成了 都会来调用
*
* @param analysisContext
*/
@Override
public void doAfterAllAnalysed(AnalysisContext analysisContext) {
log.info("doAfterAllAnalysed解析玩一个志愿sheet工作表>>>>>>>>>>");
//如果集合不为空
if (!CollectionUtils.isEmpty(list)) {
//TODO 判断excel表中的手机号是否重复
log.info("批量插入志愿数据,条数为>>>>>>>>>>:{}", list.size());
if (list.size() > 0) {
volunteerService.saveBatch(list);
}
}
}
/**
* 校验导入模板表头是否正确
*
* @param headMap 表头
* @param context
*/
@Override
public void invokeHeadMap(Map<Integer, String> headMap, AnalysisContext context) {
//是否手动传入表头信息 没有则自动读取实体类头信息
try {
if (this.headMap == null || this.headMap.size() <= 0) {
Map<Integer, Head> map = context.currentReadHolder().excelReadHeadProperty().getHeadMap();
for (int i = 0; i < map.size(); i++) {
//封装成headMap
this.headMap.put(map.get(i).getColumnIndex(), map.get(i).getHeadNameList().get(0));
}
}
} catch (Exception e) {
isCorrectFormat = false;
throw new RuntimeException("导入失败,请联系管理员!");
}
if (this.headMap == null || headMap == null || this.headMap.size() != headMap.size()) {
isCorrectFormat = false;
throw new RuntimeException("导入表的表头与模板表头不符!");
} else {
for (int i = 0; i < this.headMap.size(); i++) {
//循环遍历判断表格头是否一一对应
if (!this.headMap.get(i).equals(headMap.get(i))) {
isCorrectFormat = false;
throw new RuntimeException("导入表的表头与模板表头不符!");
}
}
}
}
}
package cn.wisenergy.web.admin.controller.app;
import cn.wisenergy.common.expection.BaseException;
import cn.wisenergy.common.utils.R;
import cn.wisenergy.model.app.SchemeQueryRecord;
import cn.wisenergy.model.vo.SchemeQueryVo;
import cn.wisenergy.model.vo.SchemeRecordQueryVo;
import cn.wisenergy.model.vo.VolunteerVo;
import cn.wisenergy.service.app.SchemeService;
import cn.wisenergy.service.app.VolunteerService;
import com.github.pagehelper.PageInfo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
/**
* @ Description: PC-方案
......@@ -31,6 +35,9 @@ public class SchemeController {
@Autowired
private SchemeService schemeService;
@Autowired
private VolunteerService volunteerService;
@ApiOperation(value = "获取志愿列表", notes = "获取志愿列表", httpMethod = "GET")
@ApiImplicitParam(name = "queryVo", value = "志愿查询参数", dataType = "SchemeQueryVo")
@GetMapping("/getList")
......@@ -43,4 +50,17 @@ public class SchemeController {
return schemeService.getList(queryVo);
}
@ApiOperation(value = "Excel批量添加方案志愿", notes = "Excel批量添加方案志愿")
@ApiImplicitParam(name = "file", value = "excel文件", dataType = "MultipartFile")
@PostMapping("/excel/add")
public R excelAdd(@RequestParam("uploadfile") MultipartFile file, @RequestParam("type") Integer type,
@RequestParam("schemeName") String schemeName, HttpServletResponse response) throws IOException {
if (file == null || file.isEmpty() || null == type || StringUtils.isBlank(schemeName)) {
throw new BaseException("操作错误");
}
volunteerService.excelAdd(file, type, schemeName, response);
return R.ok("添加成功");
}
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment