Commit 9c633736 by hzc

提交常规文件管理功能

parent 55255eb6
package com.keymobile.indicators.api.uploadfile;
import com.keymobile.indicators.model.entity.uploadfile.CommonFile;
import com.keymobile.indicators.service.uploadfile.CommonFileService;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.List;
import java.util.Map;
/**
* @author: hzc
* @create: 2021/12/20 16:13
*/
@RestController
@RequestMapping("/commonFile")
public class CommonFileCtrl {
@Autowired
private CommonFileService commonFileService;
@ApiOperation("上传文件")
@PostMapping("/uploadFile")
public String uploadFile(@RequestParam MultipartFile file ,
@RequestParam String code,
@RequestParam String fileName,
@RequestParam String fileDesc,
@RequestParam String user) throws Exception {
CommonFile commonFile = new CommonFile();
commonFile.setCode(code);
commonFile.setFileName(fileName);
commonFile.setUploadUser(user);
commonFile.setFileDesc(fileDesc);
return commonFileService.uploadFile(file,commonFile);
}
@ApiOperation(value = "下载文件", notes = "下载文件")
@GetMapping(value = "/downloadFile")
public void downloadFile(@RequestParam Integer id, HttpServletRequest request,
HttpServletResponse response)throws Exception{
commonFileService.downloadFile(id, request, response);
}
@ApiOperation(value = "根据条件加载下载文件信息", notes = "根据条件加载下载文件信息")
@PostMapping(value = "/getByPageAndKeywordAndCode")
public Map<String,Object> getByPageAndKeywordAndCode(@RequestParam String code,
@RequestParam(required = false)String keyword, @RequestParam(defaultValue = "0")int page,
@RequestParam(defaultValue = "10")int rows)throws Exception{
return commonFileService.getByPageAndKeywordAndCodeIn(code, keyword, page, rows);
}
@ApiOperation(value = "批量删除文件信息", notes = "批量删除文件信息")
@PostMapping(value = "/deleteUploadFile")
public void deleteUploadFile(@RequestParam List<Integer> ids) {
commonFileService.deleteUploadFile(ids);
}
}
\ No newline at end of file
package com.keymobile.indicators.model.entity.uploadfile;
import lombok.Data;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.Table;
@Data
@Table(name="p_common_file")
public class CommonFile {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;
private String fileName;//文件名称
private String fileType;//文件类型
private String filePath;//文件路径
private String fileDesc;//文件说明
private Integer fileStatus;//文件状态
private String uploadUser;//上传的用户
private String uploadDate;//上传时间
private String code;//机构编码
private String orgName;//机构名称
}
package com.keymobile.indicators.model.mapper.uploadfile;
import com.keymobile.indicators.model.entity.uploadfile.CommonFile;
import com.keymobile.indicators.model.entity.uploadfile.UploadFile;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;
import tk.mybatis.mapper.common.BaseMapper;
import java.util.List;
import java.util.Map;
@Mapper
public interface CommonFileMapper extends BaseMapper<CommonFile> {
int getByKeywordCount(Map<String, Object> paramMap);
List<UploadFile> getPageByKeywordAndCode(Map<String, Object> paramMap);
void deleteByIdIn(@Param("ids") List<Integer> ids);
List<CommonFile> selectByIdIn(@Param("ids") List<Integer> ids);
}
...@@ -187,10 +187,10 @@ public class DriveIndCalResultService { ...@@ -187,10 +187,10 @@ public class DriveIndCalResultService {
reportOneMapper.deleteByCompareIdInAndDateIn(param1); reportOneMapper.deleteByCompareIdInAndDateIn(param1);
reportTwoMapper.deleteByCompareIdInAndDateIn(param1); reportTwoMapper.deleteByCompareIdInAndDateIn(param1);
driveIndCalResultDefMapper.deleteByCompareIdInAndDateIn(param2); driveIndCalResultDefMapper.deleteByCompareIdInAndDateIn(param2);
objScoreCalResultMapper.deleteByCompareIdInAndDateIn(param2); objScoreCalResultMapper.deleteByCompareIdInAndDateIn(param1);
}else if("0".equals(isTest)){ }else if("0".equals(isTest)){
testDriveIndCalResultDefMapper.deleteByCompareIdInAndDateIn(param2); testDriveIndCalResultDefMapper.deleteByCompareIdInAndDateIn(param2);
testObjScoreCalResultMapper.deleteByCompareIdInAndDateIn(param2); testObjScoreCalResultMapper.deleteByCompareIdInAndDateIn(param1);
} }
} }
} }
......
package com.keymobile.indicators.service.uploadfile;
import com.alibaba.fastjson.JSONObject;
import com.keymobile.indicators.model.entity.uploadfile.CommonFile;
import com.keymobile.indicators.model.entity.uploadfile.UploadFile;
import com.keymobile.indicators.model.mapper.uploadfile.CommonFileMapper;
import com.keymobile.indicators.service.SystemAuthService;
import com.keymobile.indicators.utils.DateUtils;
import com.keymobile.indicators.utils.FileUtils;
import org.apache.commons.lang.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.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.*;
/**
* @author: hzc
* @create: 2021/12/20 16:20
*/
@Service
public class CommonFileService {
private Logger logger = LoggerFactory.getLogger(UploadFileService.class);
@Autowired
private SystemAuthService systemAuthService;
@Autowired
private CommonFileMapper commonFileMapper;
@Value("${uploadfile.path}")
private String uploadFilePath;
public String uploadFile(MultipartFile file, CommonFile commonFile) throws Exception {
if(file!=null){
String originalFilename = file.getOriginalFilename();
// 获取后缀
String fileType = null;
if(originalFilename.lastIndexOf(".")>=0) {
fileType = originalFilename.substring(originalFilename.lastIndexOf(".")+1);
// originalFilename = originalFilename.substring(0,originalFilename.lastIndexOf("."));
}
commonFile.setFileType(fileType);
commonFile.setFileStatus(1);
List<JSONObject> byNos = systemAuthService.getByNos(Arrays.asList(commonFile.getCode()));
if(!byNos.isEmpty()){
String orgName = byNos.get(0).getString("name");
commonFile.setOrgName(orgName);
}else{
logger.info("编码:"+commonFile.getCode()+"->获取不到中文名称");
}
commonFileMapper.insert(commonFile);
//设置上传文件路径
StringBuilder filePath = new StringBuilder("");
if(StringUtils.isNotBlank(fileType)) {
filePath.append(uploadFilePath).append("/").append(commonFile.getFileName()).append("_")
.append(commonFile.getId()).append(".").append(fileType);
}else {
filePath.append(uploadFilePath).append("/").append(commonFile.getFileName()).append("_")
.append(commonFile.getId());
}
commonFile.setFilePath(filePath.toString());
commonFile.setUploadDate(DateUtils.formatDate(new Date(), "yyyy-MM-dd HH:mm:ss"));
//上传文件到指定路径
FileUtils.uploadFile(file.getInputStream(), filePath.toString());
//修改上传文件路径和状态
commonFileMapper.updateByPrimaryKey(commonFile);
}else{
return "upload file is null";
}
return "success upload file";
}
public void downloadFile(Integer id, HttpServletRequest request, HttpServletResponse response) throws Exception {
CommonFile commonFile = commonFileMapper.selectByPrimaryKey(id);
FileUtils.downloadFile(request,response,commonFile.getFilePath());
}
public Map<String, Object> getByPageAndKeywordAndCodeIn(String code, String keyword, int page, int rows) {
Map<String,Object> resultMap = new HashMap<>();
Map<String,Object> paramMap = new HashMap<>();
paramMap.put("code", code+"%");
if(StringUtils.isBlank(keyword)) {
paramMap.put("keyword", null);
}else {
paramMap.put("keyword", "%"+keyword+"%");
}
//计算总数
int count = commonFileMapper.getByKeywordCount(paramMap);
//计算start
int start = page*rows;
paramMap.put("start", start);
paramMap.put("end", rows);
List<UploadFile> resultList = commonFileMapper.getPageByKeywordAndCode(paramMap);
resultMap.put("resultList", resultList);
resultMap.put("total", count);
return resultMap;
}
public void deleteUploadFile(List<Integer> ids) {
//删除文件
deleteFile(ids);
//删除数据库记录
commonFileMapper.deleteByIdIn(ids);
}
//删除文件
private void deleteFile(List<Integer> ids){
List<CommonFile> commonFiles = commonFileMapper.selectByIdIn(ids);
for (CommonFile commonFile : commonFiles) {
FileUtils.deleteFileByPath(commonFile.getFilePath());
}
}
}
\ No newline at end of file
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.keymobile.indicators.model.mapper.uploadfile.CommonFileMapper">
<select id="getPageByKeywordAndCode" parameterType="map" resultType="com.keymobile.indicators.model.entity.uploadfile.CommonFile" >
select *
from p_common_file
where
file_status=1 and
code like #{code}
<if test="keyword!=null">
and file_name like #{keyword}
</if>
order by upload_date desc
limit #{start},#{end}
</select>
<select id="getByKeywordCount" parameterType="map" resultType="java.lang.Integer">
select count(1)
from p_common_file
where
file_status=1 and
code like #{code}
<if test="keyword!=null">
and file_name like #{keyword}
</if>
</select>
<select id="selectByIdIn" resultType="com.keymobile.indicators.model.entity.uploadfile.CommonFile">
select *
from p_common_file
where id in
<foreach item="id" collection="ids" open="(" close=")" separator=",">
#{id}
</foreach>
</select>
<delete id="deleteByIdIn" parameterType="java.util.List">
delete
from p_common_file
where id in
<foreach item="id" collection="ids" open="(" close=")" separator=",">
#{id}
</foreach>
</delete>
</mapper>
\ No newline at end of file
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment