package cn.com.goldenwater.dcproj.controller.general; import cn.com.goldenwater.core.web.BaseController; import cn.com.goldenwater.core.web.BaseResponse; import cn.com.goldenwater.dcproj.model.*; import cn.com.goldenwater.dcproj.param.GwComFileParam; import cn.com.goldenwater.dcproj.service.*; import cn.com.goldenwater.dcproj.utils.AdLevelUtil; import cn.com.goldenwater.id.util.UuidUtil; import com.github.pagehelper.PageInfo; import io.swagger.annotations.Api; import io.swagger.annotations.ApiOperation; import io.swagger.annotations.ApiParam; import org.apache.commons.lang3.StringUtils; 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.util.Assert; import org.springframework.web.bind.annotation.*; import org.springframework.web.multipart.MultipartFile; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import java.io.File; import java.io.IOException; import java.util.*; import java.util.regex.Pattern; /** * @author zhaohg * @date 2019-2-18 */ @Api(value = "文件管理", tags = "01文件管理") @RestController @RequestMapping("/file") public class GwComFileController extends BaseController { private Logger logger = LoggerFactory.getLogger(getClass()); private Pattern pattern = Pattern.compile("\\d{12}$"); @Autowired private GwComFileService gwComFileService; @Autowired private AttAdBaseService attAdBaseService; @Autowired private NewsService newsService; @Autowired private BisInspOprdCstrProjRgstrService bisInspOprdCstrProjRgstrService; @Value("${getFile.prefix}") public String prefix; @Autowired private OlBisInspOrgService olBisInspOrgService; @ApiOperation(value = "添加文件", notes = "参数字段说明:{\n\r" + " \"file\":\"上传文件\",\n\r" + " \"fileTitle\":\"标题\",\n\r" + " \"keyWord\":\"版号\",\n\r" + " };\n\r" + "返回结构说明:{\n\r" + " \"success\":\"是否成功(true为成功,false为失败)\",\n\r" + " \"code\":\"错误代码\",\n\r" + " \"data(数据信息)\":\n\r" + " {\n\r" + " \"guid \":\"主键id\",\n\r" + " \"filePath \":\"文件路径\",\n\r" + " \"fileType \":\"文件类型\",\n\r" + " \"fileTitle\":\"标题\",\n\r" + " \"createDate\":\"时间\",\n\r" + " \"keyWord\":\"版号\",\n\r" + " \"fileName\":\"文件名称\",\n\r" + " \"fileExt\":\"文件扩展名\",\n\r" + " }\n\r" + " }") @RequestMapping(value = "/insert", method = RequestMethod.POST) public BaseResponse insert(@RequestParam("file") MultipartFile file, GwComFile gwComFile, HttpServletRequest request) { String uuid = UuidUtil.uuid(); // 生成uuid gwComFile.setId(uuid); //按时间日期存文件 System.out.println("submit file in"); Calendar dat = Calendar.getInstance(); if (null == gwComFile.getCreateDate()) { gwComFile.setCreateDate(Calendar.getInstance().getTime()); } if (file != null) { String originalFilename = file.getOriginalFilename(); gwComFile.setFileName(originalFilename); gwComFile.setFileTitle(StringUtils.isNotBlank(gwComFile.getFileTitle()) ? gwComFile.getFileTitle() : gwComFile.getFileName()); gwComFile.setFileSize((double) file.getSize()); gwComFile.setFileExt(originalFilename.substring(1 + originalFilename.lastIndexOf("."))); String filePath = prefix + dat.get(Calendar.YEAR) + File.separator + (dat.get(Calendar.MONTH) + 1) + File.separator + dat.get(Calendar.DAY_OF_MONTH) + File.separator + uuid + "." + gwComFile.getFileExt(); gwComFile.setFilePath(filePath); if (gwComFile.getFileExt().toLowerCase().equals("html") || gwComFile.getFileExt().toLowerCase().equals("htm") || gwComFile.getFileExt().toLowerCase().equals("js") || gwComFile.getFileExt().toLowerCase().equals("jsp") || gwComFile.getFileExt().toLowerCase().equals("aspx") || gwComFile.getFileExt().toLowerCase().equals("php") || gwComFile.getFileExt().toLowerCase().equals("python") || gwComFile.getFileExt().toLowerCase().equals("exe")) { return buildFailResponse(1001, "文件上传失败"); } } //写入文件 String filePath = dat.get(Calendar.YEAR) + File.separator + (dat.get(Calendar.MONTH) + 1) + File.separator + dat.get(Calendar.DAY_OF_MONTH); boolean success = gwComFileService.writeFile(gwComFile, file, filePath); System.out.println("submit file " + success); if (success) { System.out.println("submit file success"); gwComFileService.insert(gwComFile); if ("1".equals(gwComFile.getIsNotice())) { // String currentAdcode = AdLevelUtil.getAddvcd(olBisInspOrgService.getProvince(getCurrentOrgId())); BisInspOrg bisInspOrg = olBisInspOrgService.getDefaultOrg(getCurrentOrgId()); AttAdBase attAdBase = attAdBaseService.get(bisInspOrg.getAdCode()); News news = new News(); news.setOrgId(bisInspOrg.getOrgId()); news.setAdCode(bisInspOrg.getAdCode()); news.setAdName(attAdBase.getAdName()); news.setAdFullName(attAdBase.getAdFullName()); news.setAdGrad(attAdBase.getAdGrad()); news.setTitle(file.getOriginalFilename().substring(0, file.getOriginalFilename().lastIndexOf("."))); news.setDes(file.getOriginalFilename().substring(0, file.getOriginalFilename().lastIndexOf("."))); news.setClcUrl(gwComFile.getFilePath()); news.setIntm(new Date()); news.setDataStat("0"); newsService.insert(news); } if (StringUtils.isNotBlank(gwComFile.getBizType()) && gwComFile.getBizType().contains("signature")) { String orgId = request.getHeader("orgId"); if (gwComFile.getBizId().endsWith(orgId)) { String registrId = gwComFile.getBizId().replace(orgId, ""); BisInspOprdCstrProjRgstr oprdCstrProjRgstr = bisInspOprdCstrProjRgstrService.get(registrId); if (oprdCstrProjRgstr != null) { oprdCstrProjRgstr.setState("1"); bisInspOprdCstrProjRgstrService.update(oprdCstrProjRgstr); } } } } else { return buildFailResponse(1001, "文件上传失败"); } return buildSuccessResponse(gwComFile); } @ApiOperation(value = "添加文件", notes = "参数字段说明:{\n\r" + " \"file\":\"上传文件\",\n\r" + " \"fileTitle\":\"标题\",\n\r" + " \"keyWord\":\"版号\",\n\r" + " };\n\r" + "返回结构说明:{\n\r" + " \"success\":\"是否成功(true为成功,false为失败)\",\n\r" + " \"code\":\"错误代码\",\n\r" + " \"data(数据信息)\":\n\r" + " {\n\r" + " \"guid \":\"主键id\",\n\r" + " \"filePath \":\"文件路径\",\n\r" + " \"fileType \":\"文件类型\",\n\r" + " \"fileTitle\":\"标题\",\n\r" + " \"createDate\":\"时间\",\n\r" + " \"keyWord\":\"版号\",\n\r" + " \"fileName\":\"文件名称\",\n\r" + " \"fileExt\":\"文件扩展名\",\n\r" + " }\n\r" + " }") @RequestMapping(value = "/insertMulId", method = RequestMethod.POST) public BaseResponse insertMulId(@RequestParam("file") MultipartFile file, GwComFile gwComFile, HttpServletRequest request) { try { if (gwComFile != null) { if (StringUtils.isNotBlank(gwComFile.getBizId())) { String[] arr = gwComFile.getBizId().split(","); for (String id : arr) { GwComFile gwComFile1 = new GwComFile(); gwComFile1.setBizId(id); gwComFile1.setBizType(gwComFile.getBizType()); this.insert(file, gwComFile1, request); } } } } catch (Exception e) { return buildFailResponse(); } return buildSuccessResponse(gwComFile); } @RequestMapping(value = "/insertList", method = RequestMethod.POST) public BaseResponse> insertList(@RequestParam("file") List files, GwComFile gwComFile, HttpServletRequest request) { List gwComFiles = new ArrayList<>(); for (int i = 0; i < files.size(); i++) { String uuid = UuidUtil.uuid(); // 生成uuid GwComFile bizParam = new GwComFile(); bizParam.setBizId(gwComFile.getBizId()); bizParam.setBizType(gwComFile.getBizType()); bizParam.setId(uuid); //按时间日期存文件 System.out.println("submit file in"); Calendar dat = Calendar.getInstance(); if (null == bizParam.getCreateDate()) { bizParam.setCreateDate(Calendar.getInstance().getTime()); } if (files.get(i) != null) { String originalFilename = files.get(i).getOriginalFilename(); bizParam.setFileName(originalFilename); bizParam.setFileTitle(StringUtils.isNotBlank(bizParam.getFileTitle()) ? bizParam.getFileTitle() : bizParam.getFileName()); bizParam.setFileSize((double) files.get(i).getSize()); bizParam.setFileExt(originalFilename.substring(1 + originalFilename.lastIndexOf("."))); String filePath = prefix + dat.get(Calendar.YEAR) + File.separator + (dat.get(Calendar.MONTH) + 1) + File.separator + dat.get(Calendar.DAY_OF_MONTH) + File.separator + uuid + "." + bizParam.getFileExt(); bizParam.setFilePath(filePath); } //写入文件 String filePath = dat.get(Calendar.YEAR) + File.separator + (dat.get(Calendar.MONTH) + 1) + File.separator + dat.get(Calendar.DAY_OF_MONTH); boolean success = gwComFileService.writeFile(bizParam, files.get(i), filePath); System.out.println("submit file " + success); if (success) { System.out.println("submit file success"); gwComFileService.insert(bizParam); if ("1".equals(bizParam.getIsNotice())) { // String currentAdcode = AdLevelUtil.getAddvcd(olBisInspOrgService.getProvince(getCurrentOrgId())); BisInspOrg bisInspOrg = olBisInspOrgService.getDefaultOrg(getCurrentOrgId()); AttAdBase attAdBase = attAdBaseService.get(bisInspOrg.getAdCode()); News news = new News(); news.setOrgId(bisInspOrg.getOrgId()); news.setAdCode(bisInspOrg.getAdCode()); news.setAdName(attAdBase.getAdName()); news.setAdFullName(attAdBase.getAdFullName()); news.setAdGrad(attAdBase.getAdGrad()); news.setTitle(files.get(i).getOriginalFilename().substring(0, files.get(i).getOriginalFilename().lastIndexOf("."))); news.setDes(files.get(i).getOriginalFilename().substring(0, files.get(i).getOriginalFilename().lastIndexOf("."))); news.setClcUrl(bizParam.getFilePath()); news.setIntm(new Date()); news.setDataStat("0"); newsService.insert(news); } if (StringUtils.isNotBlank(bizParam.getBizType()) && bizParam.getBizType().contains("signature")) { String orgId = request.getHeader("orgId"); if (bizParam.getBizId().endsWith(orgId)) { String registrId = bizParam.getBizId().replace(orgId, ""); BisInspOprdCstrProjRgstr oprdCstrProjRgstr = bisInspOprdCstrProjRgstrService.get(registrId); if (oprdCstrProjRgstr != null) { oprdCstrProjRgstr.setState("1"); bisInspOprdCstrProjRgstrService.update(oprdCstrProjRgstr); } } } gwComFiles.add(bizParam); } else { return buildFailResponse(1001, "文件上传失败"); } } return buildSuccessResponse(gwComFiles); } @ApiOperation(value = "根据ID删除xxx") @RequestMapping(value = "/{id}", method = RequestMethod.POST) public BaseResponse delete(@ApiParam(name = "id", value = "id", required = true) @PathVariable String id) { GwComFile gwComFile = gwComFileService.get(id); int ret = gwComFileService.delete(id); gwComFileService.updatePblmVedio(gwComFile.getBizId()); return buildSuccessResponse(); } @ApiOperation(value = "文件列表") @RequestMapping(value = "/findImgPage", method = RequestMethod.POST) public BaseResponse findPage(@ApiParam(name = "gwComFileParam", value = "GwComFileParam", required = true) @RequestBody GwComFileParam gwComFileParam) { return buildSuccessResponse(gwComFileService.findImgPage(gwComFileParam)); } @ApiOperation(value = "更新xxx信息") @RequestMapping(value = "/update", method = RequestMethod.POST) public BaseResponse update(@ApiParam(name = "gwComFile", value = "GwComFile", required = true) @RequestBody GwComFile gwComFile) { Assert.notNull(gwComFile.getId(), "主键id为必填参数"); int ret = gwComFileService.update(gwComFile); return buildSuccessResponse(); } @ApiOperation(value = "更新文件信息") @RequestMapping(value = "/updateFile", method = RequestMethod.POST) public BaseResponse updateFile(@RequestParam("file") MultipartFile file, GwComFile newGwComFile) { Assert.notNull(newGwComFile.getId(), "主键id为必填参数"); GwComFile gwComFile = gwComFileService.get(newGwComFile.getId()); File gwFile = new File(gwComFile.getFilePath()); if (gwFile.exists()) { gwFile.delete(); } Calendar dat = Calendar.getInstance(); if (null == gwComFile.getCreateDate()) { gwComFile.setCreateDate(Calendar.getInstance().getTime()); } if (file != null) { String originalFilename = file.getOriginalFilename(); gwComFile.setFileName(originalFilename); gwComFile.setFileTitle(StringUtils.isNotBlank(gwComFile.getFileTitle()) ? gwComFile.getFileTitle() : gwComFile.getFileName()); gwComFile.setFileSize((double) file.getSize()); gwComFile.setFileExt(originalFilename.substring(1 + originalFilename.lastIndexOf("."))); String filePath = prefix + dat.get(Calendar.YEAR) + File.separator + (dat.get(Calendar.MONTH) + 1) + File.separator + dat.get(Calendar.DAY_OF_MONTH) + File.separator + gwComFile.getId() + "." + gwComFile.getFileExt(); gwComFile.setFilePath(filePath); } //写入文件 String filePath = dat.get(Calendar.YEAR) + File.separator + (dat.get(Calendar.MONTH) + 1) + File.separator + dat.get(Calendar.DAY_OF_MONTH); boolean success = gwComFileService.writeFile(gwComFile, file, filePath); if (success) { int ret = gwComFileService.update(gwComFile); } else { return buildFailResponse(1001, "文件上传失败"); } return buildSuccessResponse(gwComFile); } @ApiOperation(value = "根据ID获取xxx(单表)") @RequestMapping(value = "/{id}", method = RequestMethod.GET) public BaseResponse get(@ApiParam(name = "id", value = "id", required = true) @PathVariable String id) { GwComFile gwComFile = gwComFileService.get(id); return buildSuccessResponse(gwComFile); } @ApiOperation(value = "根据业务ID获取文件") @RequestMapping(value = "/findFileByBiz", method = RequestMethod.POST) public BaseResponse> findFileByBiz(@ApiParam(name = "bizId", value = "bizId", required = true) @RequestParam String bizId) { List gwComFileList = gwComFileService.findFileByBiz(bizId); return buildSuccessResponse(gwComFileList); } @ApiOperation(value = "把所有图片制作一份缩略图(按月份的文件夹修改)") @RequestMapping(value = "/changeSmallImg", method = RequestMethod.GET) public BaseResponse changeSmallImg(@ApiParam(name = "year", value = "year", required = true) @RequestParam String year, @ApiParam(name = "mnth", value = "mnth", required = true) @RequestParam String mnth) { gwComFileService.changeSmallImg(year, mnth); return buildSuccessResponse("执行完成"); } @ApiOperation(value = "根据查询条件获取文件列表") @RequestMapping(value = "/findPageInfo", method = RequestMethod.POST) public BaseResponse> findPageInfo(@RequestBody GwComFileParam gwComFileParam) { PageInfo gwComFileList = gwComFileService.findFilePageByBiz(gwComFileParam); return buildSuccessResponse(gwComFileList); } @ApiOperation(value = "根据查询条件获取所有文件列表") @RequestMapping(value = "/findAllFile", method = RequestMethod.POST) public BaseResponse> findAllFile(@RequestBody GwComFileParam gwComFileParam) { List gwComFileList = gwComFileService.findAllFile(gwComFileParam); return buildSuccessResponse(gwComFileList); } @ApiOperation(value = "根据id下载文件") @RequestMapping(value = "/downFile/{id}", method = RequestMethod.GET) public BaseResponse downFile(@ApiParam(name = "id", value = "id", required = true) @PathVariable String id, HttpServletResponse response) { GwComFile gwComFile = gwComFileService.get(id); if (gwComFile == null) { return buildFailResponse("文件不存在"); } gwComFile.setFilePath(gwComFile.getFilePath().substring(gwComFile.getFilePath().indexOf(prefix) + 7)); int result = gwComFileService.downFile(gwComFile, response); if (result == 0) { return buildFailResponse("文件不存在"); } return buildSuccessResponse(); } @ApiOperation(value = "根据文件ID置顶") @GetMapping(value = "/top/{id}") public BaseResponse getTop(@PathVariable String id) { gwComFileService.updateTopById(id); return buildSuccessResponse(id); } @ApiOperation(value = "根据文件ID取消置顶") @GetMapping(value = "/cancelTop/{id}") @Deprecated public BaseResponse cancelTop(@PathVariable String id) { gwComFileService.updateTopById(id); return buildSuccessResponse(id); } @ApiOperation(value = "获取前N条近期面貌督查") @RequestMapping(value = "/getTopNFace", method = RequestMethod.GET) public BaseResponse> getTopNFace( @ApiParam(name = "n", value = "前n调记录", required = false) @RequestParam(defaultValue = "5", required = false) String n, @ApiParam(name = "adCode", value = "行政区代码", required = false) @RequestParam(required = false) String adCode, @ApiParam(name = "ptype", value = "督查类别", required = false) @RequestParam(required = false) String pType) { String currentAdcode = AdLevelUtil.getAddvcd(olBisInspOrgService.getProvince(getCurrentOrgId())); if (StringUtils.isNotBlank(pType)) { pType = String.valueOf(Integer.parseInt(pType)); } if (StringUtils.isNotBlank(adCode)) { if (validateAdCode(adCode)) { adCode = AdLevelUtil.getAddvcd(adCode); } else { adCode = currentAdcode; } } else { adCode = currentAdcode; } List gwComFiles = gwComFileService.getTopNFace(n, adCode, pType, currentAdcode); return buildSuccessResponse(gwComFiles); } @ApiOperation(value = "获取前N条问题") @RequestMapping(value = "/getTopNPblm", method = RequestMethod.GET) public BaseResponse> getTopNPblm( @ApiParam(name = "n", value = "前n调记录", required = false) @RequestParam(defaultValue = "8", required = false) String n, @ApiParam(name = "adCode", value = "行政区代码", required = false) @RequestParam(required = false) String adCode, @ApiParam(name = "ptype", value = "督查类别", required = false) @RequestParam(required = false) String pType) { String currentAdcode = AdLevelUtil.getAddvcd(olBisInspOrgService.getProvince(getCurrentOrgId())); if (StringUtils.isNotBlank(pType)) { pType = String.valueOf(Integer.parseInt(pType)); } if (StringUtils.isNotBlank(adCode)) { adCode = AdLevelUtil.getAddvcd(adCode); } else { adCode = currentAdcode; } List gwComFiles = gwComFileService.getTopNPblm(n, adCode, pType, currentAdcode); return buildSuccessResponse(gwComFiles); } @ApiOperation(value = "获取前N条问题视频") @RequestMapping(value = "/getTopNPblmVido", method = RequestMethod.GET) public BaseResponse> getTopNPblmVido( @ApiParam(name = "n", value = "前n调记录", required = false) @RequestParam(defaultValue = "5", required = false) String n, @ApiParam(name = "adCode", value = "行政区代码", required = false) @RequestParam(required = false) String adCode, @ApiParam(name = "ptype", value = "督查类别", required = false) @RequestParam(required = false) String pType) { String currentAdcode = AdLevelUtil.getAddvcd(olBisInspOrgService.getProvince(getCurrentOrgId())); if (StringUtils.isNotBlank(pType)) { pType = String.valueOf(Integer.parseInt(pType)); } if (StringUtils.isNotBlank(adCode)) { if (validateAdCode(adCode)) { adCode = AdLevelUtil.getAddvcd(adCode); } else { adCode = currentAdcode; } } else { adCode = currentAdcode; } List gwComFiles = gwComFileService.getTopNPblmVido(n, adCode, pType, currentAdcode); return buildSuccessResponse(gwComFiles); } /** * 正则:行政区划编码 12位数字即可 * * @param in * @return */ private boolean validateAdCode(String in) { return this.pattern.matcher(in).matches(); } @ApiOperation(value = "获取前N条稽察问题资源") @RequestMapping(value = "/getTopTacPblm", method = RequestMethod.GET) public BaseResponse> getTopTacPlam( @ApiParam(name = "n", value = "前n调记录", required = false) @RequestParam(defaultValue = "5", required = false) String n, @ApiParam(name = "adCode", value = "行政区代码", required = false) @RequestParam(required = false) String adCode) { String province = ""; if (StringUtils.isBlank(adCode)) { province = olBisInspOrgService.getProvince(getCurrentOrgId()); } else { province = adCode; } List gwComFiles = gwComFileService.getTopTacPblm(n, province); return buildSuccessResponse(gwComFiles); } @ApiOperation(value = "获取前N条稽察问题视频资源") @RequestMapping(value = "/getTopTacPblmvideo", method = RequestMethod.GET) public BaseResponse> getTopTacPblmvideo( @ApiParam(name = "n", value = "前n调记录", required = false) @RequestParam(defaultValue = "5", required = false) String n, @ApiParam(name = "adCode", value = "行政区代码", required = false) @RequestParam(required = false) String adCode) { String province = ""; if (StringUtils.isBlank(adCode)) { province = olBisInspOrgService.getProvince(getCurrentOrgId()); } else { province = adCode; } List gwComFiles = gwComFileService.getTopTacPblmVideo(n, province); return buildSuccessResponse(gwComFiles); } //按照项目导出工地标准化的图片数据 @ApiOperation(value = "按照项目导出工地标准化的图片数据") @RequestMapping(value = "/getpicInfo", method = RequestMethod.GET) public BaseResponse getpicInfo() throws IOException { gwComFileService.getpicInfo(); return buildSuccessResponse(); } @ApiOperation(value = "调用wsdl接口推送数据") @RequestMapping(value = "/wsdl/push/data", method = RequestMethod.POST) public BaseResponse sendToWsdlData(@ApiParam(name = "gwComFileParam", value = "GwComFileParam", required = true) @RequestBody GwComFileParam gwComFileParam) { BaseResponse response = buildSuccessResponse(); String result = gwComFileService.toWsdlPushData(gwComFileParam); response.setData(result); return response; } }