|
@@ -0,0 +1,64 @@
|
|
|
+package com.ym.mec.web.controller;
|
|
|
+
|
|
|
+import com.ym.mec.biz.dal.entity.Goods;
|
|
|
+import com.ym.mec.biz.service.GoodsService;
|
|
|
+import com.ym.mec.common.controller.BaseController;
|
|
|
+import com.ym.mec.common.entity.HttpResponseResult;
|
|
|
+import com.ym.mec.util.date.DateUtil;
|
|
|
+import io.swagger.annotations.Api;
|
|
|
+import io.swagger.annotations.ApiOperation;
|
|
|
+import org.apache.tomcat.util.http.fileupload.IOUtils;
|
|
|
+import org.springframework.beans.factory.annotation.Autowired;
|
|
|
+import org.springframework.core.io.ClassPathResource;
|
|
|
+import org.springframework.security.access.prepost.PreAuthorize;
|
|
|
+import org.springframework.web.bind.annotation.*;
|
|
|
+import org.springframework.web.multipart.MultipartFile;
|
|
|
+
|
|
|
+import javax.servlet.http.HttpServletResponse;
|
|
|
+import java.io.IOException;
|
|
|
+import java.io.InputStream;
|
|
|
+import java.io.OutputStream;
|
|
|
+import java.util.Date;
|
|
|
+import java.util.List;
|
|
|
+
|
|
|
+@RequestMapping("import")
|
|
|
+@Api(tags = "数据导入服务")
|
|
|
+@RestController
|
|
|
+public class ImportController extends BaseController {
|
|
|
+
|
|
|
+ @Autowired
|
|
|
+ private GoodsService goodsService;
|
|
|
+
|
|
|
+ @ApiOperation(value = "导入商品")
|
|
|
+ @PostMapping(value = "goods")
|
|
|
+ @PreAuthorize("@pcs.hasPermissions('import/goods')")
|
|
|
+ public HttpResponseResult<List<Goods>> importExamSong(@RequestParam("file") MultipartFile file) throws Exception {
|
|
|
+ return succeed(goodsService.importGoods(file));
|
|
|
+ }
|
|
|
+
|
|
|
+ @ApiOperation(value = "获取商品导入模板")
|
|
|
+ @GetMapping(value = "getGoodsTemplate")
|
|
|
+ @PreAuthorize("@pcs.hasPermissions('import/getGoodsTemplate')")
|
|
|
+ public void getGoodsTemplate(HttpServletResponse response) throws IOException {
|
|
|
+ InputStream inputStream = new ClassPathResource("excelTemplate/goods.xls").getInputStream();
|
|
|
+ OutputStream outputStream = response.getOutputStream();
|
|
|
+ try {
|
|
|
+ response.setContentType("application/octet-stream");
|
|
|
+ response.setHeader("Content-Disposition", "attachment;filename=goods-" + DateUtil.getDate(new Date()) + ".xls");
|
|
|
+ response.flushBuffer();
|
|
|
+ outputStream = response.getOutputStream();
|
|
|
+ IOUtils.copy(inputStream, outputStream);
|
|
|
+ outputStream.flush();
|
|
|
+ } catch (Exception e) {
|
|
|
+ e.printStackTrace();
|
|
|
+ } finally {
|
|
|
+ if (outputStream != null) {
|
|
|
+ try {
|
|
|
+ outputStream.close();
|
|
|
+ } catch (IOException e) {
|
|
|
+ e.printStackTrace();
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+}
|