package cn.com.goldenwater.dcproj.controller.importex; import cn.com.goldenwater.core.web.BaseController; import cn.com.goldenwater.core.web.BaseResponse; import cn.com.goldenwater.dcproj.param.TypeExportParam; import cn.com.goldenwater.dcproj.service.GwComFileService; import cn.com.goldenwater.dcproj.service.ImpExcelService; import cn.com.goldenwater.dcproj.socket.WebSocketServer; import io.swagger.annotations.Api; import io.swagger.annotations.ApiOperation; import io.swagger.annotations.ApiParam; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Value; import org.springframework.web.bind.annotation.*; import org.springframework.web.multipart.MultipartFile; import javax.servlet.http.HttpServletResponse; import java.io.File; import java.text.SimpleDateFormat; import java.util.*; /** *

* 导入Excel的接口类 * 策略:先上传,再解析,最后入库 *

* * @author liyz * @date 2019/4/9 16:20 **/ @Api(value = "导入Excel", tags = "导入Excel") @RestController @RequestMapping("/dc/impExcel") public class ImpExcelController extends BaseController { private Logger logger = LoggerFactory.getLogger(getClass()); private final SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss:SSS"); private final String FILE_EXTS = "xls"; @Value("${impExcel.basePath}") private String path; @Autowired private GwComFileService gwComFileService; @Autowired private ImpExcelService impExcelService; @ApiOperation(value = "下载水库Excel上传模板的接口") @RequestMapping(value = "/downloadTemplate/sk", method = {RequestMethod.GET}) public BaseResponse downloadSkExcelTemplate(HttpServletResponse response, @ApiParam("用户id") @RequestParam(required = false) String userId, @ApiParam("节点id") @RequestParam() String id) { TypeExportParam tep = new TypeExportParam(); tep.setType("rsvr"); tep.setPresId(getCurrentPersId()); // 水库 tep.setpType("1"); tep.setTableIds("08"); // 0未督查 1督查中 2已督查 tep.setState("1,0"); tep.setId(id); int i = impExcelService.downloadSkExcelTemplate(response, tep); return buildSuccessResponse(Integer.toString(i)); } @ApiOperation(value = "上传Excel文件并批量入库的接口") @RequestMapping(value = "/imp/{userId}/{sid}", method = {RequestMethod.POST}) public BaseResponse impExcel(@ApiParam("用户标识") @PathVariable String userId, @ApiParam("webSocket标识") @PathVariable String sid, @ApiParam("数据流") @RequestParam(required = false) MultipartFile file, @ApiParam("表标识 08表示小水库") @RequestParam(required = false) String tid) { // 上传文件 String id = tid + "-" + UUID.randomUUID().toString(); String ext = "xls"; if (file != null) { String fileName = file.getOriginalFilename(); ext = fileName.substring(1 + fileName.lastIndexOf(".")); if (!ext.endsWith(FILE_EXTS)) { WebSocketServer.sendInfo(sdf.format(new Date()) + "-文件类型不对", sid); return buildSuccessResponse("n"); } } //按时间日期存文件 Calendar dat = Calendar.getInstance(); String filePath = this.path + File.separator + dat.get(Calendar.YEAR) + File.separator + (dat.get(Calendar.MONTH) + 1) + File.separator + dat.get(Calendar.DAY_OF_MONTH); boolean boo = gwComFileService.writeFile(id, ext, file, filePath); WebSocketServer.sendInfo(sdf.format(new Date()) + "-①开始上传", sid); if (!boo) { WebSocketServer.sendInfo(sdf.format(new Date()) + "-上传失败×", sid); } else { WebSocketServer.sendInfo(sdf.format(new Date()) + "-上传成功√", sid); boolean sucessState = impExcelService.parseAndInsert(tid, filePath + File.separator + id + "." + ext, sid, userId); if (!sucessState) { return buildSuccessResponse("n"); } } return buildSuccessResponse("y"); } @ApiOperation(value = "上传Excel文件接口") @RequestMapping(value = "/uploadExcel/{userId}/{sid}", method = {RequestMethod.POST, RequestMethod.GET}) public BaseResponse uploadExcel(@ApiParam("用户标识") @PathVariable String userId, @ApiParam("webSocket标识") @PathVariable String sid, @ApiParam("数据流") @RequestParam(required = false) MultipartFile file, @ApiParam("表标识 08表示小水库") @RequestParam(required = false) String tid) { return buildSuccessResponse(impExcelService.uploadExcel(userId, sid, file, tid)); } @ApiOperation(value = "解析Excel文件接口") @RequestMapping(value = "/parseExcel/{userId}/{sid}", method = {RequestMethod.GET, RequestMethod.POST,}) public BaseResponse> parseExcel(@ApiParam("用户标识") @PathVariable String userId, @ApiParam("webSocket标识") @PathVariable String sid, @ApiParam("存储路径") @RequestParam(required = false) String filePath, @ApiParam("表标识") @RequestParam(required = false) String tid, @ApiParam("节点id") @RequestParam() String pid) { return buildSuccessResponse(impExcelService.parseExcel(userId, sid, filePath, tid, pid)); } @ApiOperation(value = "入库Excel文件接口") @RequestMapping(value = "/insertExcel/{userId}/{sid}", method = {RequestMethod.GET, RequestMethod.POST}) public BaseResponse insertExcel(@ApiParam("用户标识") @PathVariable String userId, @ApiParam("webSocket标识") @PathVariable String sid, @ApiParam("表标识") @RequestParam(required = false) String tid) { return buildSuccessResponse(impExcelService.insertExcel(userId, sid, tid)); } }