// Decompiled by Jad v1.5.8g. Copyright 2001 Pavel Kouznetsov.
// Jad home page: http://www.kpdus.com/jad.html
// Decompiler options: packimports(3) 
// Source File Name:   ShipRecognitionController.java

package com.example.ghjgpython.controller;

import com.example.ghjgpython.dao.People;
import com.example.ghjgpython.service.ShipRecognitionService;
import com.shanghaigeography.Util.DateUtils;
import org.apache.poi.ss.usermodel.Workbook;
import org.jeecgframework.poi.excel.ExcelExportUtil;
import org.jeecgframework.poi.excel.entity.TemplateExportParams;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.text.ParseException;
import java.util.*;

@CrossOrigin
@RestController
@RequestMapping("ghjg/shipRecognition")
public class ShipRecognitionController {
    @Autowired
    private ShipRecognitionService shipRecognitionService;


    //根据日期获取py图片，对比图
    @GetMapping("coverByDate/{date}")
    public String getRecognitionByDate(@PathVariable String date) {
        Date toDate = null;
        try {
            toDate = DateUtils.stringToDate(date, "yyyy-MM-dd");
        } catch (ParseException e) {
            return "err";
        }
        return shipRecognitionService.getRecognitionByDate(toDate);
    }

    //根据日期范围获取py图片,对比图
    @PostMapping("coverByDates")
    public String getRecognitionByDate(@RequestBody Map map) {
        if (!map.isEmpty())
            try {
                Date startDate = DateUtils.stringToDate((String) map.get("startDate"), "yyyy-MM-dd");
                Date endDate = DateUtils.stringToDate((String) map.get("endDate"), "yyyy-MM-dd");
                return shipRecognitionService.getRecognitionByDates(startDate, endDate);
            } catch (ParseException e) {
                return "err";
            }
        else
            return null;
    }

    @PostMapping("getPhoto")
    public String getPhoto() {
        List<String> list = Collections.singletonList("hs772540");
     return shipRecognitionService.getPhoto(list);
    }

    @PostMapping("newCoverByDates")
    public String getnewCoverByDates(@RequestBody Map map) {
        if (!map.isEmpty())
            try {
                Date startDate = DateUtils.stringToDate((String) map.get("startDate"), "yyyy-MM-dd HH:mm:ss");
                Date endDate = DateUtils.stringToDate((String) map.get("endDate"), "yyyy-MM-dd HH:mm:ss");
                return shipRecognitionService.getnewCoverByDates(startDate, endDate);
            } catch (ParseException e) {
                return "err";
            } catch (Exception e) {
                throw new RuntimeException(e);
            }
        else
            return null;
    }

    @PostMapping("coverPictureByDatesOnDay")
    public String getCoverPictureByDatesOnDay(@RequestBody Map map) {
        if (!map.isEmpty())
            try {
                Date startDate = DateUtils.stringToDate((String) map.get("startDate"), "yyyy-MM-dd");
                Date endDate = DateUtils.stringToDate((String) map.get("endDate"), "yyyy-MM-dd");
                return shipRecognitionService.getCoverPictureByDatesOnDay(startDate, endDate);
            } catch (ParseException e) {
                return "err";
            }
        else
            return null;
    }

    /**
     * 根据传入的时间段以及类型定向分析
     *
     * @param map
     * @return
     */
    @PostMapping("coverByDatesAndType")
    public String getCoverDatesAndType(@RequestBody Map map) {
        if (!map.isEmpty())
            try {
                Date startDate = DateUtils.stringToDate((String) map.get("startDate"), "yyyy-MM-dd HH:mm:ss");
                Date endDate = DateUtils.stringToDate((String) map.get("endDate"), "yyyy-MM-dd HH:mm:ss");
                return shipRecognitionService.getCoverDatesAndType(startDate, endDate);
            } catch (ParseException e) {
                return "err";
            }
        else
            return null;
    }

    //根据货类下载图片
    @PostMapping("downLoadFlag")
    public String downloadFlag(@RequestBody Map map) {
        if (!map.isEmpty()) {
            try {
                Date startDate = DateUtils.stringToDate((String) map.get("startDate"), "yyyy-MM-dd HH:mm:SS");
                Date endDate = DateUtils.stringToDate((String) map.get("endDate"), "yyyy-MM-dd HH:mm:SS");
                Integer type = null;
                if (map.get("type") != null) {
                    type = Integer.parseInt(map.get("type").toString());
                }
                return shipRecognitionService.downloadPicture(startDate, endDate, type);
            } catch (Exception e) {
                return "err";
            }
        }
        return null;
    }

    //根据目录以及想要的文件名称获取图片
    @PostMapping("getAllPicture")
    public String downLoadPic(@RequestBody Map map) throws IOException {
        if (!map.isEmpty()) {
            String dir = map.get("dir").toString();
            String fileLike = map.get("want").toString();
            return shipRecognitionService.downLoadPic(dir, fileLike);
        }
        return null;
    }

    //特定接口, 根据日期进行获取文件
    @PostMapping("getPicByDate")
    public String getPictureByDate(@RequestBody Map map) {
        if (!map.isEmpty()) {
            String dir = map.get("dir").toString();
            String fileLike = map.get("want").toString();
            String startDate = map.get("startDate").toString();
            String endDate = map.get("endDate").toString();
            String startHour = map.get("startHour").toString();
            String endHour = map.get("endHour").toString();
            Date start = null;
            Date end = null;
            try {
                start = DateUtils.stringToDate(startDate, "yyy-MM-dd");
                end = DateUtils.stringToDate(endDate, "yyyy-MM-dd");
                if (start.getTime() > end.getTime()) {
                    return "开始时间大于结束时间";
                }
            } catch (ParseException e) {
                return "时间格式不正确";
            }

            int sh = Integer.parseInt(startHour);
            int eh = Integer.parseInt(endHour);
            if (sh > eh) {
                return "开始小时大于结束小时";
            }
            return shipRecognitionService.getPicByDate(dir, fileLike, start, end, sh, eh);
        }
        return "err";
    }

    @GetMapping("test")
    public String test() throws IOException {
        TemplateExportParams templateExportParams = new TemplateExportParams("templateDir/test_template.xlsx");
        HashMap<String, Object> stringObjectHashMap = new HashMap<>();
        stringObjectHashMap.put("date", new Date());
        stringObjectHashMap.put("title", "测试");
        ArrayList<People> objects = new ArrayList<>();
        objects.add(new People(1, "张三", "20", "男"));
        objects.add(new People(2, "李四", "21", "女"));
        stringObjectHashMap.put("list", objects);
        stringObjectHashMap.put("sum", "1234567890");
        Workbook sheets = ExcelExportUtil.exportExcel(templateExportParams, stringObjectHashMap);
        File file = new File("E:\\test");
        if (!file.exists()) {
            file.mkdirs();
        }
        FileOutputStream fileOutputStream = new FileOutputStream("E:\\test\\test.xlsx");
        sheets.write(fileOutputStream);
        fileOutputStream.close();
        return shipRecognitionService.getTest();
    }


    /**
     * 救生衣查询
     */

    @PostMapping("lifeJackets")
    public String lifeJacketPy(@RequestBody Map<String, String> map) throws IOException {
        String dir = map.get("dir");
        String path = map.get("path");
        return shipRecognitionService.getLifeJacket(path, dir);
    }

    @GetMapping("lifeJacket/{path}")
    public String lifeJacketGetPy(@PathVariable String path) throws IOException {
        return shipRecognitionService.getLifeJacket(path, null);
    }
}
