package com.huimv.face.controller;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.huimv.face.domain.*;
import com.huimv.face.repo.FaceUserImgRepository;
import com.huimv.face.result.Result;
import com.huimv.face.service.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.text.SimpleDateFormat;
import java.util.*;

/**
 * @Project : huimv.shiwan
 * @Package : com.huimv.face.controller
 * @Description : TODO
 * @Author : yuxuexuan
 * @Create : 2020/12/29 0029 10:21
 **/
@CrossOrigin
@RestController
@RequestMapping("/face")
public class FaceUserController {

    @Autowired
    private FaceUserService faceUserService;

    @Autowired
    private SaveDbService saveDbService;

    @Autowired
    private FaceRecoService faceRecoService;

    @Autowired
    private FaceSbdaService faceSbdaService;

    @Autowired
    private FaceUserImgsService faceUserImgsService;


    /*
    * @Method      : findOneDay
    * @Description : 查询一天的数据，存起来
    * @Params      : []
    * @Return      : void
    *
    * @Author      : yuxuexuan
    * @Date        : 2020/12/29 0029
    * @Time        : 17:33
    */
    @Scheduled(cron = "0 0 * * * ?")
    @RequestMapping("findOneDay")
    public void findOneDay(){
        String s = JSON.toJSONString(faceRecoService.findOneDay());
        System.out.println(s);
        SaveDb saveDb = new SaveDb();
        saveDb.setId(3);
        saveDb.setKey1("oneDay");
        saveDb.setValue1(s);
        saveDbService.save(saveDb);
    }
    /*
    * @Method      : findOneWeek
    * @Description : 查询一周的数据，存起来
    * @Params      : []
    * @Return      : void
    *
    * @Author      : yuxuexuan
    * @Date        : 2020/12/29 0029
    * @Time        : 17:34
    */
    @Scheduled(cron = "0 0 23 * * ?")
    @RequestMapping("/findOneWeek")
    public void findOneWeek(){
        String s = JSON.toJSONString(faceRecoService.findOneWeek());
        System.out.println(s);
        SaveDb saveDb = new SaveDb();
        saveDb.setId(2);
        saveDb.setKey1("oneWeek");
        saveDb.setValue1(s);
        saveDbService.save(saveDb);
    }
    /*
    * @Method      : findOneMouth
    * @Description : 查询一年的数据，存起来
    * @Params      : []
    * @Return      : void
    *
    * @Author      : yuxuexuan
    * @Date        : 2020/12/29 0029
    * @Time        : 17:34
    */
    @Scheduled(cron = "0 0 1 1 * ?")
    @RequestMapping("/findOneYear")
    public void findOneYear(){
        String s = JSON.toJSONString(faceRecoService.findOneYear());
        System.out.println(s);
        SaveDb saveDb = new SaveDb();
        saveDb.setId(1);
        saveDb.setKey1("oneYear");
        saveDb.setValue1(s);
        saveDbService.save(saveDb);
    }
    /*
    * @Method      : findDb
    * @Description : 查询折线图数据,传入值:oneDay,oneWeek,oneYear
    * @Params      : [timeHorizon]
    * @Return      : com.huimv.face.result.Result
    * 
    * @Author      : yuxuexuan
    * @Date        : 2020/12/30 0030
    * @Time        : 10:22 
    */
    @RequestMapping("/findDb")
    public Result findDb(String timeHorizon){
        return saveDbService.findDb(timeHorizon);
    }

    /*
    * @Method      : findEnd
    * @Description : 查询最后几条数据
    * @Params      : []
    * @Return      : com.huimv.face.result.Result
    *
    * @Author      : yuxuexuan
    * @Date        : 2020/12/30 0030
    * @Time        : 13:44
    */
    @RequestMapping("/findEnd")
    public Result findEnd(){
        try {
            List<FaceReco> faceRecoList = faceRecoService.findTop10ByOrderByIdDesc();
            List  list = new ArrayList();
            for (FaceReco faceReco : faceRecoList) {
                FaceUser faceUser = faceUserService.findAllByUserId(faceReco.getUSERID());
                FaceSbda faceSbda = faceSbdaService.findAllBySBID(faceReco.getSBID());
                FaceUserImgs faceUserImg = faceUserImgsService.findAllByUserId(faceReco.getUSERID());
                if (faceUser != null&&faceSbda!=null) {
                    Map map = new HashMap();
                    map.put("userName", faceUser.getXM());
                    map.put("sbName", faceSbda.getSBMC());
                    map.put("time", new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(faceReco.getXGSJ()));
                    if (faceUserImg != null){
                        byte[] img = faceUserImg.getImg();
                        String imgBase64 = "data:image/jpg;base64,"+Base64.getEncoder().encodeToString(img);
                        map.put("image",imgBase64);
                    }
                    list.add(map);
                }
            }
            return new Result(10000,"查询成功",list);
        }catch (Exception e){
            return new Result(10001,"查询失败",null);
        }

    }


    @PostMapping("/findFaceUserByName")
    public  Result findFaceUserByName(String userName){
        return faceUserService.findFaceUserByName(userName);
    }

    @PostMapping("/upload")
    public  Result upload(Integer ID ,String XM , String USERID, @RequestParam(required = true)MultipartFile file){
        return faceUserImgsService.upload(ID,XM,USERID,file);
    }

    @Autowired
    FaceUserImgRepository faceUserImgRepository;

    @RequestMapping(value = "/alarm", method = RequestMethod.GET)
    public JSONObject listUser() {

        return   faceUserImgsService.findAlam();
    }
}
