package com.huimv.face.service.impl;

import com.huimv.face.domain.FaceReco;
import com.huimv.face.repo.FaceRecoRepository;
import com.huimv.face.service.FaceRecoService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.stereotype.Service;

import javax.persistence.criteria.CriteriaBuilder;
import javax.persistence.criteria.CriteriaQuery;
import javax.persistence.criteria.Predicate;
import javax.persistence.criteria.Root;
import java.text.SimpleDateFormat;
import java.util.*;

/**
 * @Project : huimv.shiwan
 * @Package : com.huimv.face.service.impl
 * @Description : TODO
 * @Author : yuxuexuan
 * @Create : 2020/12/29 0029 10:17
 **/
@Service
public class FaceRecoServiceImpl implements FaceRecoService {

    @Autowired
    private  FaceRecoRepository recoRepository;

    @Override
    public List findOneDay() {
        //设置时间
        //固定时间
        //String day = "2020-10-14 23:59:59";
        //及时数据
        String now = timeUtils();
        String day = now.substring(0,11);
        int hour = Integer.parseInt(now.substring(11,13));

        List list = new ArrayList();
        if (hour >=0){
            for (int i = 1; i < hour ; i++){
                if (i == 10){
                    break;
                }
                Map map = new HashMap();
                map.put("key","0"+i+":00");
                map.put("value",findTimeList(day+"0"+(i-1)+":00:00",day+"0"+i+":59:59"));
                list.add(map);
            }
            if( hour >=10 ){
                for (int i = 10; i < hour ; i++){
                    if (i == 20){
                        break;
                    }
                    Map map = new HashMap();
                    map.put("key",i+":00");
                    map.put("value",findTimeList(day+(i-1)+":00:00",day+i+":59:59"));
                    list.add(map);
                }
                if (hour>=20){
                    for (int i = 20; i < hour ; i++){
                        Map map = new HashMap();
                        map.put("key",i+":00");
                        map.put("value",findTimeList(day+(i-1)+":00:00",day+i+":59:59"));
                        list.add(map);
                    }

                }
            }

        }
        if (hour>=20){

        }

        return list;
    }

    @Override
    public List findOneWeek() {
        //固定时间,把这个转成时间戳
        //String now = "2020-10-14 23:59:59";
        //设置时间
        long now = System.currentTimeMillis();
        SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        Long oneDay = 1000*60*60*24L;
        List list = new ArrayList();
        for (int i = 1; i <= 7;i++){
            Long lastDay = now-oneDay*i;
            String date = df.format(lastDay);
            Map map = new HashMap();
            map.put("key",date.substring(5,10));
            map.put("value",findTimeList(date.substring(0,11)+"00:00:00",date.substring(0,11)+"23:59:59"));
            list.add(map);
        }

        return list;
    }

    @Override
    public List findOneYear() {
        //设置时间

        //固定时间
        //String now = "2020-10-14 23:59:59";

        //及时数据
        String now = timeUtils();
        String year = now.substring(0,4);
        List list = new ArrayList();

        int mouth = Integer.parseInt(now.substring(5,7));

        //如果是一月，查询上一年的数据
        if (mouth == 1){
            year  = (Integer.parseInt(year)-1)+"";
            Map map1 = new HashMap();
            map1.put("key","一月");
            map1.put("value",findTimeList(year+"-01-01 00:00:00",year+"-01-31 23:59:59"));
            list.add(map1);

            Map map2 = new HashMap();
            map2.put("key","二月");
            map2.put("value",findTimeList(year+"-02-01 00:00:00",year+"-02-28 23:59:59"));
            list.add(map2);

            Map map3 = new HashMap();
            map3.put("key","三月");
            map3.put("value",findTimeList(year+"-03-01 00:00:00",year+"-03-31 23:59:59"));
            list.add(map3);

            Map map4 = new HashMap();
            map4.put("key","四月");
            map4.put("value",findTimeList(year+"-04-01 00:00:00",year+"-04-30 23:59:59"));
            list.add(map4);

            Map map5 = new HashMap();
            map5.put("key","五月");
            map5.put("value",findTimeList(year+"-05-01 00:00:00",year+"-05-31 23:59:59"));
            list.add(map5);

            Map map6 = new HashMap();
            map6.put("key","六月");
            map6.put("value",findTimeList(year+"-06-01 00:00:00",year+"-06-30 23:59:59"));
            list.add(map6);

            Map map7 = new HashMap();
            map7.put("key","七月");
            map7.put("value",findTimeList(year+"-07-01 00:00:00",year+"-07-31 23:59:59"));
            list.add(map7);

            Map map8 = new HashMap();
            map8.put("key","八月");
            map8.put("value",findTimeList(year+"-08-01 00:00:00",year+"-08-31 23:59:59"));
            list.add(map8);

            Map map9 = new HashMap();
            map9.put("key","九月");
            map9.put("value",findTimeList(year+"-09-01 00:00:00",year+"-09-30 23:59:59"));
            list.add(map9);

            Map map10 = new HashMap();
            map10.put("key","十月");
            map10.put("value",findTimeList(year+"-10-01 00:00:00",year+"-10-31 23:59:59"));
            list.add(map10);

            Map map11 = new HashMap();
            map11.put("key","十一月");
            map11.put("value",findTimeList(year+"-11-01 00:00:00",year+"-11-30 23:59:59"));
            list.add(map11);

            Map map12 = new HashMap();
            map12.put("key","十二月");
            map12.put("value",findTimeList(year+"-12-01 00:00:00",year+"-12-31 23:59:59"));
            list.add(map12);
        }
        if (mouth > 1){
            Map map1 = new HashMap();
            map1.put("key","一月");
            map1.put("value",findTimeList(year+"-01-01 00:00:00",year+"-01-31 23:59:59"));
            list.add(map1);
            if (mouth > 2 ){
                Map map2 = new HashMap();
                map2.put("key","二月");
                map2.put("value",findTimeList(year+"-02-01 00:00:00",year+"-02-28 23:59:59"));
                list.add(map2);
                if (mouth > 3 ){
                    Map map3 = new HashMap();
                    map3.put("key","三月");
                    map3.put("value",findTimeList(year+"-03-01 00:00:00",year+"-03-31 23:59:59"));
                    list.add(map3);
                    if (mouth > 4 ){
                        Map map4 = new HashMap();
                        map4.put("key","四月");
                        map4.put("value",findTimeList(year+"-04-01 00:00:00",year+"-04-30 23:59:59"));
                        list.add(map4);
                        if (mouth > 5 ){
                            Map map5 = new HashMap();
                            map5.put("key","五月");
                            map5.put("value",findTimeList(year+"-05-01 00:00:00",year+"-05-31 23:59:59"));
                            list.add(map5);
                            if (mouth > 6 ){
                                Map map6 = new HashMap();
                                map6.put("key","六月");
                                map6.put("value",findTimeList(year+"-06-01 00:00:00",year+"-06-30 23:59:59"));
                                list.add(map6);
                                if (mouth > 7 ){
                                    Map map7 = new HashMap();
                                    map7.put("key","七月");
                                    map7.put("value",findTimeList(year+"-07-01 00:00:00",year+"-07-31 23:59:59"));
                                    list.add(map7);
                                    if (mouth > 8 ){
                                        Map map8 = new HashMap();
                                        map8.put("key","八月");
                                        map8.put("value",findTimeList(year+"-08-01 00:00:00",year+"-08-31 23:59:59"));
                                        list.add(map8);
                                        if (mouth > 9 ){
                                            Map map9 = new HashMap();
                                            map9.put("key","九月");
                                            map9.put("value",findTimeList(year+"-09-01 00:00:00",year+"-09-30 23:59:59"));
                                            list.add(map9);
                                            if (mouth > 10 ){
                                                Map map10 = new HashMap();
                                                map10.put("key","十月");
                                                map10.put("value",findTimeList(year+"-10-01 00:00:00",year+"-10-31 23:59:59"));
                                                list.add(map10);
                                                if (mouth > 11 ){
                                                    Map map11 = new HashMap();
                                                    map11.put("key","十一月");
                                                    map11.put("value",findTimeList(year+"-11-01 00:00:00",year+"-11-30 23:59:59"));
                                                    list.add(map11);
                                                }
                                            }
                                        }
                                    }
                                }
                            }
                        }
                    }
                }
            }
        }

        return list;
    }


    @Override
    public List<FaceReco> findTop10ByOrderByIdDesc() {
        return recoRepository.findTop10ByOrderByIdDesc();
    }

    /*
    * @Method      : findTimeList
    * @Description : 查询某个时间段的信息
    * @Params      : [startTime, endTime]
    * @Return      : java.lang.Integer
    *
    * @Author      : yuxuexuan
    * @Date        : 2020/12/30 0030
    * @Time        : 8:44
    */
    private Integer findTimeList(String startTime, String endTime) {
        Specification specification = new Specification() {
            @Override
            public Predicate toPredicate(Root root, CriteriaQuery query, CriteriaBuilder cb) {
                Predicate predicate = cb.conjunction();
                if (startTime != null && !startTime.trim().equals("")) {
                    predicate.getExpressions().add(cb.greaterThanOrEqualTo(root.get("XGSJ").as(String.class), startTime));
                }
                if (endTime != null && !endTime.trim().equals("")) {
                    predicate.getExpressions().add(cb.lessThanOrEqualTo(root.get("XGSJ").as(String.class), endTime));
                }
                return predicate;
            }
        };
        List<FaceReco> list = recoRepository.findAll(specification);
        //设置设备
        int count = 0;
        for (FaceReco faceReco : list) {
            if ("110201190808KN4872".equals(faceReco.getSBID())||
                    "110201190808KN2340".equals(faceReco.getSBID())||
                    "110201190808KN1206".equals(faceReco.getSBID())||
                    "110201190808KN4475".equals(faceReco.getSBID())||
                    "110201190808KN1937".equals(faceReco.getSBID())||
                    "110201190808KN4191".equals(faceReco.getSBID())||
                    "110201190808KN4746".equals(faceReco.getSBID())||
                    "110201190808KN0582".equals(faceReco.getSBID())||
                    "110201190808KN1791".equals(faceReco.getSBID())||
                    "110201190808KN2096".equals(faceReco.getSBID())||
                    "110201190808KN1555".equals(faceReco.getSBID())||
                    "110201190808KN2053".equals(faceReco.getSBID())){
                count++;
            }
        }
        return count;
    }

    private String timeUtils(){
        return new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date());
    }
}
