021781ae26cf749b8b0b945c0aacc945730ab747.svn-base 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  1. package cn.com.goldenwater.dcproj.controller.importex;
  2. import cn.com.goldenwater.core.web.BaseController;
  3. import cn.com.goldenwater.core.web.BaseResponse;
  4. import cn.com.goldenwater.dcproj.param.TypeExportParam;
  5. import cn.com.goldenwater.dcproj.service.GwComFileService;
  6. import cn.com.goldenwater.dcproj.service.ImpExcelService;
  7. import cn.com.goldenwater.dcproj.socket.WebSocketServer;
  8. import io.swagger.annotations.Api;
  9. import io.swagger.annotations.ApiOperation;
  10. import io.swagger.annotations.ApiParam;
  11. import org.slf4j.Logger;
  12. import org.slf4j.LoggerFactory;
  13. import org.springframework.beans.factory.annotation.Autowired;
  14. import org.springframework.beans.factory.annotation.Value;
  15. import org.springframework.web.bind.annotation.*;
  16. import org.springframework.web.multipart.MultipartFile;
  17. import javax.servlet.http.HttpServletResponse;
  18. import java.io.File;
  19. import java.text.SimpleDateFormat;
  20. import java.util.*;
  21. /**
  22. * <p>
  23. * 导入Excel的接口类
  24. * 策略:先上传,再解析,最后入库
  25. * </p>
  26. *
  27. * @author liyz
  28. * @date 2019/4/9 16:20
  29. **/
  30. @Api(value = "导入Excel", tags = "导入Excel")
  31. @RestController
  32. @RequestMapping("/dc/impExcel")
  33. public class ImpExcelController extends BaseController {
  34. private Logger logger = LoggerFactory.getLogger(getClass());
  35. private final SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss:SSS");
  36. private final String FILE_EXTS = "xls";
  37. @Value("${impExcel.basePath}")
  38. private String path;
  39. @Autowired
  40. private GwComFileService gwComFileService;
  41. @Autowired
  42. private ImpExcelService impExcelService;
  43. @ApiOperation(value = "下载水库Excel上传模板的接口")
  44. @RequestMapping(value = "/downloadTemplate/sk", method = {RequestMethod.GET})
  45. public BaseResponse<String> downloadSkExcelTemplate(HttpServletResponse response, @ApiParam("用户id") @RequestParam(required = false) String userId,
  46. @ApiParam("节点id") @RequestParam() String id) {
  47. TypeExportParam tep = new TypeExportParam();
  48. tep.setType("rsvr");
  49. tep.setPresId(getCurrentPersId());
  50. // 水库
  51. tep.setpType("1");
  52. tep.setTableIds("08");
  53. // 0未督查 1督查中 2已督查
  54. tep.setState("1,0");
  55. tep.setId(id);
  56. int i = impExcelService.downloadSkExcelTemplate(response, tep);
  57. return buildSuccessResponse(Integer.toString(i));
  58. }
  59. @ApiOperation(value = "上传Excel文件并批量入库的接口")
  60. @RequestMapping(value = "/imp/{userId}/{sid}", method = {RequestMethod.POST})
  61. public BaseResponse<String> impExcel(@ApiParam("用户标识") @PathVariable String userId,
  62. @ApiParam("webSocket标识") @PathVariable String sid,
  63. @ApiParam("数据流") @RequestParam(required = false) MultipartFile file,
  64. @ApiParam("表标识 08表示小水库") @RequestParam(required = false) String tid) {
  65. // 上传文件
  66. String id = tid + "-" + UUID.randomUUID().toString();
  67. String ext = "xls";
  68. if (file != null) {
  69. String fileName = file.getOriginalFilename();
  70. ext = fileName.substring(1 + fileName.lastIndexOf("."));
  71. if (!ext.endsWith(FILE_EXTS)) {
  72. WebSocketServer.sendInfo(sdf.format(new Date()) + "-文件类型不对", sid);
  73. return buildSuccessResponse("n");
  74. }
  75. }
  76. //按时间日期存文件
  77. Calendar dat = Calendar.getInstance();
  78. String filePath = this.path + File.separator + dat.get(Calendar.YEAR)
  79. + File.separator + (dat.get(Calendar.MONTH) + 1)
  80. + File.separator + dat.get(Calendar.DAY_OF_MONTH);
  81. boolean boo = gwComFileService.writeFile(id, ext, file, filePath);
  82. WebSocketServer.sendInfo(sdf.format(new Date()) + "-①开始上传", sid);
  83. if (!boo) {
  84. WebSocketServer.sendInfo(sdf.format(new Date()) + "-上传失败×", sid);
  85. } else {
  86. WebSocketServer.sendInfo(sdf.format(new Date()) + "-上传成功√", sid);
  87. boolean sucessState = impExcelService.parseAndInsert(tid, filePath + File.separator + id + "." + ext, sid, userId);
  88. if (!sucessState) {
  89. return buildSuccessResponse("n");
  90. }
  91. }
  92. return buildSuccessResponse("y");
  93. }
  94. @ApiOperation(value = "上传Excel文件接口")
  95. @RequestMapping(value = "/uploadExcel/{userId}/{sid}", method = {RequestMethod.POST, RequestMethod.GET})
  96. public BaseResponse<String> uploadExcel(@ApiParam("用户标识") @PathVariable String userId,
  97. @ApiParam("webSocket标识") @PathVariable String sid,
  98. @ApiParam("数据流") @RequestParam(required = false) MultipartFile file,
  99. @ApiParam("表标识 08表示小水库") @RequestParam(required = false) String tid) {
  100. return buildSuccessResponse(impExcelService.uploadExcel(userId, sid, file, tid));
  101. }
  102. @ApiOperation(value = "解析Excel文件接口")
  103. @RequestMapping(value = "/parseExcel/{userId}/{sid}", method = {RequestMethod.GET, RequestMethod.POST,})
  104. public BaseResponse<List<LinkedHashMap>> parseExcel(@ApiParam("用户标识") @PathVariable String userId,
  105. @ApiParam("webSocket标识") @PathVariable String sid,
  106. @ApiParam("存储路径") @RequestParam(required = false) String filePath,
  107. @ApiParam("表标识") @RequestParam(required = false) String tid,
  108. @ApiParam("节点id") @RequestParam() String pid) {
  109. return buildSuccessResponse(impExcelService.parseExcel(userId, sid, filePath, tid, pid));
  110. }
  111. @ApiOperation(value = "入库Excel文件接口")
  112. @RequestMapping(value = "/insertExcel/{userId}/{sid}", method = {RequestMethod.GET, RequestMethod.POST})
  113. public BaseResponse<Boolean> insertExcel(@ApiParam("用户标识") @PathVariable String userId,
  114. @ApiParam("webSocket标识") @PathVariable String sid,
  115. @ApiParam("表标识") @RequestParam(required = false) String tid) {
  116. return buildSuccessResponse(impExcelService.insertExcel(userId, sid, tid));
  117. }
  118. }