ShowCtrl.java 4.55 KB
package com.example.demo;

import org.apache.commons.io.FileUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;

import java.io.File;
import java.io.IOException;
import java.util.ArrayList;

/**
 * Created by jack on 2018/4/18.
 */
@RestController
@RequestMapping(path = "/show")
public class ShowCtrl {

    @Autowired
    ShowSer showSer;


    @RequestMapping("/index")
    public ModelAndView index() {
        return new ModelAndView("index");
    }

    @GetMapping(path = "/load")
    public ModelAndView loadPhotoDir(@RequestParam String curDir) {
        ModelAndView mv = new ModelAndView("showImage");
        showSer.loadDir(curDir);
        mv.addObject("curDir",curDir);
        mv.addObject("photoViewObj", showSer.fetchCurentPhotos(curDir, 0));
        return mv;
    }

    //跳转至样本展示页面
    @GetMapping(path = "/toSampleShow")
    public ModelAndView toSampleShow() {
       return new ModelAndView("showSamples");
    }

    /**
     * 获取样本table 分页数据
     * @param req
     * @param sampleDir
     * @return
     */
    @RequestMapping ("/getSamplesByPage")
    @ResponseBody
    public PageListResp<SampleViewObj> getSamplesByPage(PageTableReq req,String sampleDir ) {
        if(! showSer.samplesMap.containsKey(sampleDir)){
            showSer.loadSampleInfo(sampleDir);//统计样本个数
        }
        Integer count = showSer.samplesMap.get(sampleDir);
        if(count == null || count == 0){//加载文件失败
            return PageListResp.newBuilder().iTotalDisplayRecords(0).iTotalRecords(0).aaData(new ArrayList()).build();
        }
        PageListResp.Builder pageListResp = PageListResp.newBuilder().iTotalDisplayRecords(count.intValue()).iTotalRecords(count.intValue());
        pageListResp.aaData(showSer.fetchSamplesByPage(sampleDir, req.getIDisplayStart(), req.getIDisplayLength()));
        return pageListResp.build();
    }

    @GetMapping(path = "/next")
    public ModelAndView nextPhoto(@RequestParam String curDir) {
        ModelAndView mv = new ModelAndView("showImage");
        mv.addObject("curDir",curDir);
        mv.addObject("photoViewObj", showSer.fetchCurentPhotos(curDir, 1));
        return mv;
    }

    @GetMapping(path = "/prev")
    public ModelAndView prevPhoto(@RequestParam String curDir) {
        ModelAndView mv = new ModelAndView("showImage");
        mv.addObject("curDir",curDir);
        mv.addObject("photoViewObj", showSer.fetchPrevPhotos(curDir));
        return mv;

    }

    @GetMapping(path = "/jump")
    public ModelAndView jumpSknPhoto(@RequestParam("curDir") String curDir, @RequestParam String skn) {
        ModelAndView mv = new ModelAndView("showImage");
        mv.addObject("curDir",curDir);
        mv.addObject("photoViewObj", showSer.fetchSknPhotos(curDir, skn));
        return mv;

    }

    @GetMapping(path = "/read")
    public Object readPhotoBytes(@RequestParam("curDir") String curDir, @RequestParam("skn") String skn, @RequestParam("order") String order) {
        try {
            return showSer.readPhotoBytes(curDir, skn, order);
        } catch (IOException e) {
            e.printStackTrace();
        }
        return null;
    }

    @GetMapping(path = "/readAli")
    public Object readPhotoBytes(@RequestParam("file") String filePath) {
        try {
            return showSer.readPhotoBytes(filePath);
        } catch (IOException e) {
            e.printStackTrace();
        }
        return null;
    }

    /**
     * 更新update文件
     * @param flag
     * @param currentIndex
     * @return
     */
    @GetMapping("/updateSampleInfo")
    public String updateSampleInfo(boolean flag, int currentIndex, String sampleDir){
        File file = new File(sampleDir + "/update.info");
        if(!showSer.checkFile(file)){
            return "创建文件失败!";
        }

        if( !showSer.modifyFile(file, currentIndex, flag)){
            return "更新失败!";
        }
        return "";
    }


    @GetMapping("/updateSamplesByBatch")
    public String updateSamplesByBatch(boolean flag, String indexListStr, String sampleDir){
        File file = new File(sampleDir + "/update.info");
        if(!showSer.checkFile(file)){
            return "创建文件失败!";
        }
        if( !showSer.modifyFile(file, indexListStr, flag)){
            return "更新失败!";
        }
        return "";
    }

    @RequestMapping("/loadSingleImage")
    public ModelAndView loadSingleImage(){
        return  new ModelAndView("loadSingleImage");
    }


}