浏览代码

增加工况分析图相关接口,上传通用接口

chenwen 1 年之前
父节点
当前提交
ea36be9277

+ 6 - 0
pom.xml

@@ -83,6 +83,12 @@
 		    <groupId>com.squareup.okhttp3</groupId>
 		    <artifactId>okhttp</artifactId>
 	   </dependency>
+	   
+	   <dependency>
+		  <groupId>commons-fileupload</groupId>
+		  <artifactId>commons-fileupload</artifactId>
+		  <version>1.3</version>
+	   </dependency>
 
   </dependencies>
 	

+ 82 - 0
src/main/java/com/hb/proj/analysis/controller/AnalysisDiagramController.java

@@ -0,0 +1,82 @@
+package com.hb.proj.analysis.controller;
+
+import org.springframework.beans.factory.annotation.Autowired;
+import org.springframework.validation.annotation.Validated;
+import org.springframework.web.bind.annotation.RequestMapping;
+import org.springframework.web.bind.annotation.RequestParam;
+import org.springframework.web.bind.annotation.RestController;
+
+import com.hb.proj.allconfig.AccessToken;
+import com.hb.proj.analysis.service.AnalysisDiagramService;
+import com.hb.proj.model.AnalysisDiagramPO;
+import com.hb.proj.model.AnalysisDiagramVO;
+import com.hb.proj.utils.RequestParams;
+import com.hb.proj.utils.RespVO;
+import com.hb.proj.utils.RespVOBuilder;
+import com.hb.xframework.dao.util.PageModel;
+
+import jakarta.validation.constraints.NotBlank;
+
+@RestController
+@RequestMapping("/analysis/diagram")
+@Validated
+public class AnalysisDiagramController {
+
+	@Autowired
+	private AnalysisDiagramService  service;
+	
+	
+	/**
+	 * 分页查询工况分析图上传记录
+	 * @param params
+	 * @param pageNum
+	 * @param pageSize
+	 * @return
+	 */
+	@RequestMapping("/query")
+	public RespVO<PageModel<AnalysisDiagramVO>> query(RequestParams params,
+			@RequestParam(value = "currentPage", defaultValue = "1") Integer pageNum,
+			@RequestParam(value = "pageSize", defaultValue = "20") Integer pageSize) {
+		return RespVOBuilder.ok(service.query(params.getObjectMap(), pageNum, pageSize));
+	}
+	
+	
+	/**
+	 * 根据记录号删除记录(逻辑删除)
+	 * @param id
+	 * @return
+	 */
+	@RequestMapping("/delete")
+	public RespVO<Object> delete(Integer id){
+		service.delete(id);
+		return RespVOBuilder.ok();
+	}
+	
+	
+	/**
+	 * 增加工况分析图上传记录
+	 * @param analysis
+	 * @param token
+	 * @return
+	 */
+	@RequestMapping("/add")
+	public RespVO<String> add(@Validated  AnalysisDiagramPO analysis,AccessToken token){
+		 if(service.existSame(analysis)) {
+			 return RespVOBuilder.error("该工况分析图已存在,请勿重复添加");
+		 }
+		 analysis.setCreateBy(token.getUsName());
+		 service.add(analysis);
+		 return RespVOBuilder.ok();
+		
+	}
+	
+	/**
+	 * 查看工况分析图
+	 * @param id
+	 * @return
+	 */
+	@RequestMapping("/view")
+	public AnalysisDiagramVO view(@NotBlank(message="缺少记录号") Integer id){
+		return service.view(id);
+	}
+}

+ 81 - 0
src/main/java/com/hb/proj/analysis/service/AnalysisDiagramService.java

@@ -0,0 +1,81 @@
+package com.hb.proj.analysis.service;
+
+import java.util.ArrayList;
+import java.util.Date;
+import java.util.List;
+import java.util.Map;
+
+import org.apache.commons.lang3.StringUtils;
+import org.springframework.beans.factory.annotation.Autowired;
+import org.springframework.stereotype.Service;
+
+import com.hb.proj.model.AnalysisDiagramPO;
+import com.hb.proj.model.AnalysisDiagramVO;
+import com.hb.xframework.dao.core.SpringJdbcDAO;
+import com.hb.xframework.dao.util.PageModel;
+import com.hb.xframework.util.DateUtil;
+
+@Service
+public class AnalysisDiagramService {
+
+	@Autowired
+	private SpringJdbcDAO  dao;
+	
+	public PageModel<AnalysisDiagramVO> query(Map<String,Object> args,int pageNo,int pageSize){
+		List<Object> sqlParams=new ArrayList<Object>();
+		StringBuilder sql=new StringBuilder(100);
+		sql.append("select w.well_name,f.access_path diagram_path,d.* ");
+		sql.append(" from tzl_analysis_diagram d ");
+		sql.append(" left join tzl_well w on d.well_id=w.well_id and w.del_if=false");
+		sql.append(" left join tsys_file_info f on d.file_id=f.file_id");
+		sql.append(" where d.del_if=false ");
+		
+		if(args!=null&&StringUtils.isNotBlank((String)args.get("wellId"))) {
+			sql.append(" and d.well_id=?");
+			sqlParams.add(args.get("wellId"));
+		}
+	
+		
+		if(args!=null && StringUtils.isNotBlank((String)args.get("startMonth"))) {
+			sql.append(" and date_format(diagram_month,'%Y-%m')>=?");
+			sqlParams.add(args.get("startMonth"));
+		}
+		
+		if(args!=null && StringUtils.isNotBlank((String)args.get("endMonth"))) {
+			sql.append(" and date_format(diagram_month,'%Y-%m')<=?");
+			sqlParams.add(args.get("endMonth"));
+		}
+		
+		sql.append(" order by diagram_month desc");
+		
+		Object[] sqlArgs=sqlParams.size()>0?sqlParams.toArray():null;
+		return dao.queryForPagedData(sql.toString(),pageNo, pageSize,AnalysisDiagramVO.class ,sqlArgs);
+	}
+	
+	public void add(AnalysisDiagramPO analysis) {
+		analysis.setDelIf(false);
+		analysis.setCreateTime(new Date());
+		dao.insert(analysis, "tzl_analysis_diagram", "id");
+	}
+	
+	public void delete(int id) {
+		dao.exeUpdate("update tzl_analysis_diagram set del_if=true where id=?", id);
+	}
+	
+	public AnalysisDiagramVO  view(int id) {
+		String sql="""
+				select access_path
+				from tsys_file_info where file_id=(select file_id from tzl_analysis_diagram where del_if=false and id=? limit 1)
+				""";
+		return dao.queryForPojo(sql, AnalysisDiagramVO.class, id);
+	}
+	
+	public boolean existSame(AnalysisDiagramPO analysis) {
+		String sql="""
+				select count(1) from tzl_analysis_diagram 
+				where del_if=false and well_id=? and diagram_type=? and date_format(diagram_month,'%Y-%m')=?
+				""";
+		int num=dao.queryForObject(sql, Integer.class, analysis.getWellId(),analysis.getDiagramType(),DateUtil.format(analysis.getDiagramMonth(), "YYYY-MM"));
+		return num>0;
+	}
+}

+ 32 - 0
src/main/java/com/hb/proj/model/AnalysisDiagramPO.java

@@ -0,0 +1,32 @@
+package com.hb.proj.model;
+
+import java.util.Date;
+
+import jakarta.validation.constraints.NotBlank;
+import lombok.Data;
+
+@Data
+public class AnalysisDiagramPO {
+	
+	public static final String TYPE_MONTH="month";  //分析图类型:月度分析
+	
+	public static final String TYPE_EXCEPTION="exception"; //分析图类型:异常分析
+	
+	private Integer id;
+
+	@NotBlank(message="缺少井号")
+	private String wellId;
+	
+	private String diagramType;
+	
+	private Date diagramMonth;
+	
+	private String fileId;
+	
+	private Date createTime;
+	
+	private String createBy;
+	
+	private Boolean delIf;
+	
+}

+ 39 - 0
src/main/java/com/hb/proj/model/AnalysisDiagramVO.java

@@ -0,0 +1,39 @@
+package com.hb.proj.model;
+
+import java.util.Date;
+
+import com.hb.xframework.util.DateUtil;
+
+import lombok.Data;
+import lombok.EqualsAndHashCode;
+
+@Data
+@EqualsAndHashCode(callSuper=false)
+public class AnalysisDiagramVO extends AnalysisDiagramPO {
+
+	private String wellName;
+	
+	private String accessPath;
+	
+	private String diagramTypeName;
+	
+	private String diagramMonthFmt;
+	
+	private String diagramPath;
+	
+	public void setDiagramType(String diagramType) {
+		super.setDiagramType(diagramType);
+		if(AnalysisDiagramPO.TYPE_MONTH.equals(diagramType)) {
+			diagramTypeName="月度分析";
+		}
+		else if(AnalysisDiagramPO.TYPE_EXCEPTION.equals(diagramType)) {
+			diagramTypeName="异常分析";
+		}
+	}
+	
+	public void setDiagramMonth(Date diagramMonth) {
+		super.setDiagramMonth(diagramMonth);
+		diagramMonthFmt=DateUtil.format(diagramMonth, "yyyy-MM");
+	}
+	
+}

+ 78 - 0
src/main/java/com/hb/proj/model/FileInfoPO.java

@@ -0,0 +1,78 @@
+package com.hb.proj.model;
+
+import java.util.Date;
+
+public class FileInfoPO {
+
+	private String fileId;
+	
+	private String fileName;
+	
+	private String filePath;
+	
+	private String accessPath;
+	
+	private String useFor;
+	
+	private Date createTime;
+	
+	private String createBy;
+
+	public String getFileId() {
+		return fileId;
+	}
+
+	public void setFileId(String fileId) {
+		this.fileId = fileId;
+	}
+
+	public String getFileName() {
+		return fileName;
+	}
+
+	public void setFileName(String fileName) {
+		this.fileName = fileName;
+	}
+
+	public String getFilePath() {
+		return filePath;
+	}
+
+	public void setFilePath(String filePath) {
+		this.filePath = filePath;
+	}
+
+	public String getAccessPath() {
+		return accessPath;
+	}
+
+	public void setAccessPath(String accessPath) {
+		this.accessPath = accessPath;
+	}
+
+	public String getUseFor() {
+		return useFor;
+	}
+
+	public void setUseFor(String useFor) {
+		this.useFor = useFor;
+	}
+
+	public Date getCreateTime() {
+		return createTime;
+	}
+
+	public void setCreateTime(Date createTime) {
+		this.createTime = createTime;
+	}
+
+	public String getCreateBy() {
+		return createBy;
+	}
+
+	public void setCreateBy(String createBy) {
+		this.createBy = createBy;
+	}
+
+	
+}

+ 72 - 0
src/main/java/com/hb/proj/model/UploadResult.java

@@ -0,0 +1,72 @@
+package com.hb.proj.model;
+
+public class UploadResult {
+
+	private String fileName;
+	
+	private String filePath;
+	
+	private String accessPath;
+	
+	private boolean success;
+	
+	private String exception;
+	
+	public UploadResult() {
+		this.success=true;
+	}
+	
+	public UploadResult(String fileName,String filePath,String accessPath) {
+		this.success=true;
+		this.fileName=fileName;
+		this.filePath=filePath;
+		this.accessPath=accessPath;
+	}
+	
+	public UploadResult(boolean success,String exception) {
+		this.success=success;
+		this.exception=exception;
+	}
+	
+
+	public String getFileName() {
+		return fileName;
+	}
+
+	public void setFileName(String fileName) {
+		this.fileName = fileName;
+	}
+
+	public String getFilePath() {
+		return filePath;
+	}
+
+	public void setFilePath(String filePath) {
+		this.filePath = filePath;
+	}
+
+	public String getAccessPath() {
+		return accessPath;
+	}
+
+	public void setAccessPath(String accessPath) {
+		this.accessPath = accessPath;
+	}
+
+	public boolean isSuccess() {
+		return success;
+	}
+
+	public void setSuccess(boolean success) {
+		this.success = success;
+	}
+
+	public String getException() {
+		return exception;
+	}
+
+	public void setException(String exception) {
+		this.exception = exception;
+	}
+	
+}

+ 122 - 0
src/main/java/com/hb/proj/sys/controller/UploadController.java

@@ -0,0 +1,122 @@
+package com.hb.proj.sys.controller;
+
+import java.util.Date;
+import java.util.HashMap;
+import java.util.Map;
+
+import org.apache.commons.lang3.StringUtils;
+import org.springframework.beans.factory.annotation.Autowired;
+import org.springframework.validation.annotation.Validated;
+import org.springframework.web.bind.annotation.RequestMapping;
+import org.springframework.web.bind.annotation.RequestParam;
+import org.springframework.web.bind.annotation.RestController;
+import org.springframework.web.multipart.MultipartFile;
+
+import com.hb.proj.allconfig.AccessToken;
+import com.hb.proj.model.FileInfoPO;
+import com.hb.proj.model.UploadResult;
+import com.hb.proj.sys.service.UploadService;
+import com.hb.proj.utils.RespVO;
+import com.hb.proj.utils.RespVOBuilder;
+import com.hb.proj.utils.UploadUtils;
+import com.hb.xframework.util.DateUtil;
+import com.hb.xframework.util.MapUtils;
+
+import jakarta.servlet.http.HttpServletRequest;
+import jakarta.validation.constraints.NotBlank;
+
+@RestController
+@RequestMapping("/upload")
+@Validated
+public class UploadController {
+	
+	
+	private String uploadAccessPath="";
+	
+	@Autowired
+	private UploadService  service;
+	
+	
+	
+	/**
+	 * 只进行文件的磁盘存储
+	 * @param request
+	 * @param file
+	 * @return
+	 * @throws Exception
+	 */
+	@RequestMapping("/save")
+	public RespVO<Object> save(HttpServletRequest request,@RequestParam("file") MultipartFile file) throws Exception{
+		String subDirectory=request.getParameter("sort");
+		if(StringUtils.isBlank(subDirectory)) {
+			subDirectory="other";
+		}
+		String needDate=request.getParameter("needDate");
+		if(StringUtils.isBlank(needDate)||"1".equals(needDate)) {
+			subDirectory+="/"+DateUtil.format(new Date(), "yyyyMM");
+		}
+		UploadResult rst=UploadUtils.saveFile(request, file,subDirectory, uploadAccessPath);
+		if(!rst.isSuccess()) {
+			return RespVOBuilder.error(rst.getException());
+		}
+		
+		return RespVOBuilder.ok(rst);
+	}
+	
+	/**
+	   * 富文本编辑器上传文件
+	 * @param request
+	 * @param file
+	 * @return
+	 * @throws Exception
+	 */
+	@RequestMapping("/forEditor")
+	public Object forEditor(HttpServletRequest request,@RequestParam("file") MultipartFile file) throws Exception{
+		String subDirectory="subject/"+DateUtil.format(new Date(), "yyyyMM");
+		UploadResult rst=UploadUtils.saveFile(request, file,subDirectory, uploadAccessPath);
+		if(!rst.isSuccess()) {
+			return null;
+		}
+		return MapUtils.build("location",rst.getAccessPath());
+	}
+
+	@RequestMapping("/add")
+	public RespVO<Object> addFile(HttpServletRequest request,AccessToken token,@RequestParam("file") MultipartFile file) throws Exception{
+		String subDirectory=request.getParameter("sort");
+		if(StringUtils.isBlank(subDirectory)) {
+			subDirectory="other";
+		}
+		String needDate=request.getParameter("needDate");
+		if(StringUtils.isBlank(needDate)||"1".equals(needDate)) {
+			subDirectory+="/"+DateUtil.format(new Date(), "yyyyMM");
+		}
+		UploadResult rst=UploadUtils.saveFile(request, file,subDirectory, uploadAccessPath);
+		if(!rst.isSuccess()) {
+			return RespVOBuilder.error(rst.getException());
+		}
+		
+		Map<String,Object>  rstMap=new HashMap<String,Object>();
+		rstMap.put("useFor", request.getParameter("useFor"));
+		rstMap.put("createBy", token!=null?token.getUsName():"unknow");
+		rstMap.put("fileName",rst.getFileName());
+		rstMap.put("filePath",rst.getFilePath());
+		rstMap.put("accessPath",rst.getAccessPath());
+		service.addFile(rstMap);
+		
+		return RespVOBuilder.ok(rstMap);
+	}
+	
+	@RequestMapping("/del")
+	public RespVO<Object>  delFile(@NotBlank(message = "文件编号不能为空") String fileId,HttpServletRequest request) {
+		FileInfoPO  fileInfo=service.getFile(fileId);
+		if(fileInfo==null) {
+			return RespVOBuilder.ok();
+		}
+		UploadUtils.removeFile(request, fileInfo.getFilePath());
+		
+		service.delFile(fileId);
+		return RespVOBuilder.ok();
+	}
+	
+	
+}

+ 82 - 0
src/main/java/com/hb/proj/sys/service/UploadService.java

@@ -0,0 +1,82 @@
+package com.hb.proj.sys.service;
+
+import java.util.ArrayList;
+import java.util.List;
+import java.util.Map;
+
+import org.apache.commons.lang3.StringUtils;
+import org.springframework.beans.factory.annotation.Autowired;
+import org.springframework.stereotype.Service;
+
+import com.hb.proj.model.FileInfoPO;
+import com.hb.proj.utils.RequestParams;
+import com.hb.xframework.dao.core.SpringJdbcDAO;
+import com.hb.xframework.dao.util.PageModel;
+import com.hb.xframework.dao.util.UUIDHexGenerator;
+
+@Service
+public class UploadService {
+
+	@Autowired
+	private SpringJdbcDAO  dao;
+	
+	public PageModel<Map<String,Object>> query(RequestParams  params,int pageNum,int pageSize){
+		List<Object>  sqlParams=new ArrayList<Object>();
+		StringBuilder sql=new StringBuilder();
+		sql.append("select f.*");
+		sql.append(" from tsys_file_info f ");
+		sql.append(" where 1=1");
+		
+		
+		if(StringUtils.isNotBlank(params.get("title"))){
+			sql.append(" and use_for like ? ");
+			sqlParams.add("%"+params.get("title")+"%");
+		}
+		
+		if(StringUtils.isNotBlank(params.get("startDate"))){
+			sql.append(" and date_format(create_time,'%Y-%m-%d') >= ? ");
+			sqlParams.add(params.get("startDate"));
+		}
+		
+		if(StringUtils.isNotBlank(params.get("endDate"))){
+			sql.append(" and date_format(create_time,'%Y-%m-%d') <= ? ");
+			sqlParams.add(params.get("endDate"));
+		}
+		sql.append(" order by create_time desc");
+		
+		return dao.queryForPagedData(sql.toString(), pageNum, pageSize,sqlParams.size()>0?sqlParams.toArray():null);
+	}
+	
+	public String addFile(Map<String,Object> newFile) {
+		UUIDHexGenerator  uuid=UUIDHexGenerator.getInstance();
+		newFile.put("fileId",uuid.generate());
+		StringBuilder sql=new StringBuilder();
+		sql.append("insert into tsys_file_info(file_id,file_name,file_path,create_time,create_by,access_path,use_for)");
+		sql.append(" values(:fileId,:fileName,:filePath,sysdate(),:createBy,:accessPath,:useFor)");
+		dao.updateNamedSQL(sql.toString(), newFile);
+		return (String)newFile.get("fileId");
+	}
+	
+	public FileInfoPO getFile(String fileId) {
+		return dao.queryForPojo("select * from tsys_file_info where file_id=?", FileInfoPO.class, fileId);
+	}
+	
+	
+	public boolean delFile(String fileId) {
+		int num=dao.exeUpdate("delete from tsys_file_info where file_id=?", fileId);
+		return num>0;
+	}
+	
+	public boolean updateHolder(String fileIds,String holderId) {
+		if(StringUtils.isEmpty(fileIds)||StringUtils.isEmpty(holderId)) {
+			return false;
+		}
+		fileIds="'"+fileIds.replaceAll(",", "','")+"'";
+		int num=dao.exeUpdate("update  tsys_file_info set holder_id=? where file_id in ("+fileIds+")", holderId);
+		return num>0;
+	}
+	
+	public List<Map<String,Object>>  getFileByHolder(String holdeId){
+		return dao.queryForList("select access_path from tsys_file_info where holder_id=?", holdeId);
+	}
+}

+ 55 - 0
src/main/java/com/hb/proj/utils/UploadUtils.java

@@ -0,0 +1,55 @@
+package com.hb.proj.utils;
+
+import java.io.File;
+import java.io.IOException;
+
+import org.apache.commons.io.FileUtils;
+import org.apache.commons.lang3.StringUtils;
+import org.springframework.web.multipart.MultipartFile;
+
+import com.hb.proj.model.UploadResult;
+import com.hb.xframework.dao.util.UUIDHexGenerator;
+
+import jakarta.servlet.http.HttpServletRequest;
+
+public class UploadUtils {
+
+	public static UploadResult saveFile(HttpServletRequest request,MultipartFile file,String subFolder,String accessPathPrefix) throws IllegalStateException, IOException {
+		String OriginalFilename = file.getOriginalFilename();//获取原文件名
+        String suffixName = OriginalFilename.substring(OriginalFilename.lastIndexOf("."));
+
+		String[] subPaths=subFolder.split("\\.|/");
+		String basePath=request.getSession().getServletContext().getRealPath("");
+		String relPath="upload"+File.separator+StringUtils.join(subPaths,File.separator);
+		basePath=basePath+File.separator+relPath;
+		
+		FileUtils.forceMkdir(new File(basePath));
+		
+		UUIDHexGenerator  uuid=UUIDHexGenerator.getInstance();
+		String newFileName=uuid.generate()+suffixName;
+		File localFile=new File(basePath+File.separator+newFileName);
+		file.transferTo(localFile);
+		
+		String accessPath=accessPathPrefix+"/upload/"+StringUtils.join(subPaths,"/")+"/"+newFileName;
+		
+		return new UploadResult(newFileName,(relPath+File.separator+newFileName),accessPath);
+		
+	}
+	
+	public static void removeFile(HttpServletRequest request,String filePath){
+		
+		
+			try {
+				String basePath=request.getSession().getServletContext().getRealPath("");
+				filePath=basePath+File.separator+filePath;
+				File filedev=new File(filePath);
+				if(filedev.exists()){
+					FileUtils.forceDelete(filedev);
+				}
+				
+			} catch (IOException e) {
+				e.printStackTrace();
+			}
+		
+	}
+}

+ 1 - 1
src/main/resources/application-dev.properties

@@ -53,7 +53,7 @@ cache.token.expire=30
 token.header.name=token
 
 #api调用权限控制filter配置
-api.filter.exclude=/login
+api.filter.exclude=/login,/upload
 
 #系统管理员
 sys.admin.account=admin

+ 1 - 1
src/main/resources/application-pro.properties

@@ -53,7 +53,7 @@ cache.token.expire=30
 token.header.name=token
 
 #api调用权限控制filter配置
-api.filter.exclude=/login
+api.filter.exclude=/login,/upload
 
 #系统管理员
 sys.admin.account=admin