|
@@ -1,5 +1,8 @@
|
|
|
package edu.travel.web;
|
|
|
|
|
|
+import com.alibaba.excel.EasyExcel;
|
|
|
+import com.alibaba.excel.context.AnalysisContext;
|
|
|
+import com.alibaba.excel.read.listener.ReadListener;
|
|
|
import com.baomidou.mybatisplus.extension.service.IService;
|
|
|
import edu.travel.interfaces.InsertGroups;
|
|
|
import edu.travel.interfaces.UpdateGroups;
|
|
@@ -7,6 +10,8 @@ import edu.travel.resp.BaseResponse;
|
|
|
import edu.travel.resp.PageResponse;
|
|
|
import edu.travel.rpc.RPCBaseResponse;
|
|
|
import edu.travel.rpc.RPCPageResponse;
|
|
|
+import org.slf4j.Logger;
|
|
|
+import org.slf4j.LoggerFactory;
|
|
|
import org.springframework.beans.factory.annotation.Autowired;
|
|
|
import org.springframework.context.annotation.Lazy;
|
|
|
import org.springframework.http.HttpStatus;
|
|
@@ -14,16 +19,49 @@ import org.springframework.transaction.annotation.Transactional;
|
|
|
import org.springframework.validation.Errors;
|
|
|
import org.springframework.validation.ObjectError;
|
|
|
import org.springframework.validation.annotation.Validated;
|
|
|
-import org.springframework.web.bind.annotation.GetMapping;
|
|
|
-import org.springframework.web.bind.annotation.PostMapping;
|
|
|
-import org.springframework.web.bind.annotation.RequestBody;
|
|
|
-import org.springframework.web.bind.annotation.RequestParam;
|
|
|
+import org.springframework.web.bind.annotation.*;
|
|
|
+import org.springframework.web.multipart.MultipartFile;
|
|
|
|
|
|
+import javax.servlet.http.HttpServletResponse;
|
|
|
+import java.io.IOException;
|
|
|
+import java.io.UnsupportedEncodingException;
|
|
|
+import java.net.URLEncoder;
|
|
|
import java.util.List;
|
|
|
|
|
|
public class BaseController<T> {
|
|
|
+ Logger logger = LoggerFactory.getLogger(BaseController.class);
|
|
|
@Autowired
|
|
|
private IService<T> service;
|
|
|
+ @GetMapping("/download")
|
|
|
+ public void download(T clazz,HttpServletResponse response) throws IOException {
|
|
|
+ response.setContentType("application/vnd.openxmlformats-officedocument.spreadsheetml.sheet");
|
|
|
+ response.setCharacterEncoding("utf-8");
|
|
|
+ String fileName = URLEncoder.encode("导出数据.xlsx", "UTF-8").replaceAll("\\+", "%20");
|
|
|
+ response.setHeader("Content-Disposition", "attachment;filename*=utf-8''" + fileName + ".xlsx");
|
|
|
+ List<T> list = service.list();
|
|
|
+ EasyExcel.write(response.getOutputStream(), clazz.getClass())
|
|
|
+ .sheet("数据")
|
|
|
+ .doWrite(list);
|
|
|
+ }
|
|
|
+ @PostMapping("/upload/excel")
|
|
|
+ public RPCBaseResponse uploadExcel(@RequestBody T clazz ,@RequestPart("file") MultipartFile file) throws IOException {
|
|
|
+ EasyExcel.read(file.getInputStream(),clazz.getClass(),new ReadListener<T>(){
|
|
|
+
|
|
|
+ @Override
|
|
|
+ public void invoke(T t, AnalysisContext analysisContext) {
|
|
|
+ saveTarget(t);
|
|
|
+ }
|
|
|
+
|
|
|
+ @Override
|
|
|
+ public void doAfterAllAnalysed(AnalysisContext analysisContext) {
|
|
|
+ logger.debug("读取完成");
|
|
|
+ }
|
|
|
+ })
|
|
|
+ .sheet()
|
|
|
+ .doRead();
|
|
|
+ return RPCBaseResponse.success();
|
|
|
+ }
|
|
|
+
|
|
|
@GetMapping("/getById")
|
|
|
public RPCBaseResponse<T> getId(String id) {
|
|
|
T byId = service.getById(id);
|