FileController.java 5.63 KB
package vion.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import lombok.extern.slf4j.Slf4j;
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 vion.Global;
import vion.enums.ResultEnum;
import vion.model.File;
import vion.model.Inspect;
import vion.model.Store;
import vion.service.IFileSerrvice;
import vion.utils.ResultUtil;
import vion.vo.StoreVO;

import javax.servlet.http.HttpServletResponse;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.OutputStream;
import java.util.Comparator;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping(Global.BASE_URL)
@Slf4j
public class FileController {

    @Autowired
    private IFileSerrvice fileSerrvice;


    @Value("${fileurl:}")
    private String fileurl;

    @GetMapping("/files")
    @ResponseBody
    public Object getFiles(@RequestParam(name = "storeId",required=false)Integer store_id,
                                 @RequestParam(name = "sourceId",required=false)Integer source_id,
                                 @RequestParam(name = "pageNum")Integer pageNum,
                                 @RequestParam(name = "pageSize")Integer pageSize){
        //pageNum当前页,pageSize每页条数
        Page page= PageHelper.startPage(pageNum,pageSize);
        QueryWrapper<File> wrapper=new QueryWrapper<>();
        if(store_id!=null)
            wrapper.eq("store_id",store_id);
        if(source_id!=null)
            wrapper.eq("source_id",source_id);
        List<File> fileList=fileSerrvice.list(wrapper);

        fileList.sort(Comparator.comparing(File::getId));
        PageInfo pageInfo=new PageInfo<>(page);
        pageInfo.setList(fileList);

        if (pageInfo != null) {
            return ResultUtil.success(pageInfo);
        } else {
            return ResultUtil.error(ResultEnum.SELECT_ERROR);
        }
    }

    @PostMapping("/files")
    @ResponseBody
    public Map<String, Object> getFile(@RequestBody File file){
        Map<String, Object> resultMap = new HashMap<String, Object>();
        try {
            //添加文件附件
            File tempFile=new File();
            tempFile.setStoreId(file.getStoreId());
            tempFile.setSourceId(file.getSourceId());
            tempFile.setSourceType(file.getSourceType());//1项目、2工单预处理,3工单操作,4巡检
            tempFile.setName(file.getName());
            tempFile.setUrl(file.getUrl());
            tempFile.setType(file.getType());
            fileSerrvice.save(tempFile);

            resultMap.put("msg_code", 200);
        } catch (Exception e) {
            e.printStackTrace();
            resultMap.put("msg_code", 500);
        }
        return resultMap;
    }

    @GetMapping("/deleteFile")
    @ResponseBody
    public Map<String, Object> deleteFile(@RequestParam(name = "id")Integer id){
        Map<String, Object> resultMap = new HashMap<String, Object>();
        try {

            //解除文件关系
            fileSerrvice.lambdaUpdate()
                    .setSql("id="+id)
                    .eq(File::getSourceId,-1)
                    .update();

            resultMap.put("msg_code", 200);
        } catch (Exception e) {
            e.printStackTrace();
            resultMap.put("msg_code", 500);
        }
        return resultMap;
    }


    @PostMapping("/upLoadFile")
    public Map<String, Object> uploadFile(@RequestParam(name="file") MultipartFile file){
        Map<String, Object> resultMap = new HashMap<String, Object>();
        try {
            //上传url地址
            String path=fileurl+"/"+file.getOriginalFilename();
            java.io.File fileinfo=new java.io.File(path);

            file.transferTo(fileinfo);

            resultMap.put("msg_code", 200);
            resultMap.put("fileUrl",path);
        } catch (IOException e) {
            e.printStackTrace();
            resultMap.put("msg_code", 500);
            resultMap.put("message",e.getMessage());
        }
        return resultMap;
    }

    @GetMapping("/downLoadFile")
    public void downLoadFile(@RequestParam(name="fileurl")String fileurl,
                         @RequestParam(name="filename")String filename,
                         HttpServletResponse response){
        response.setContentType("application/octet-stream");
        response.setHeader("Content-Disposition","attachment;filename="+filename);
        java.io.File file=new java.io.File(fileurl);
        try {
            FileInputStream fis=new FileInputStream(file);
            OutputStream os=response.getOutputStream();
            byte[] buffer=new byte[1024];
            int length;
            while((length=fis.read(buffer))>0){
                os.write(buffer,0,length);
            }

            fis.close();
            os.close();
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    public List<File> getFileList(Integer store_id,Integer source_id){

        QueryWrapper<File> wrapper=new QueryWrapper<>();
        if(store_id!=null)
            wrapper.eq("store_id",store_id);
        if(source_id!=null)
            wrapper.eq("source_id",source_id);
        List<File> fileList=fileSerrvice.list(wrapper);

        return fileList;
    }
}