package com.huimv.video.dhicc.controller.FaceAIController;


//金铭的人脸识别数据的接口  放这个里面


import com.alibaba.fastjson.JSONObject;
import com.huimv.video.dhicc.result.R;
import com.huimv.video.dhicc.util.GetResponse;
import com.huimv.video.dhicc.util.HttpClient;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.io.IOException;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.*;

@RestController
@RequestMapping("/AiFaceCount")
public class AiFaceController {

    //这个接口里面的条件还是后端来写死吧  后端写比前端写简单一点
    @RequestMapping("/groups")
    public R addgroupspeople(HttpServletRequest request,
                             @RequestParam(name = "farmId") Integer farmId
    ) throws Exception {

        if (farmId != 1) {
            //返回为空数据
            return R.ok("当前牧场无设备").put("data", null);
        }

        GetResponse getResponse = new GetResponse();
        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        Calendar c = Calendar.getInstance();
        Date date = new Date();
        c.setTime(date);
        c.set(Calendar.MINUTE, 0);
        c.set(Calendar.SECOND, 0);

        c.add(Calendar.HOUR, +2);
        Date A = c.getTime();

        c.add(Calendar.HOUR, -2);
        Date B = c.getTime();

        c.add(Calendar.HOUR, -2);
        Date C = c.getTime();

        c.add(Calendar.HOUR, -2);
        Date D = c.getTime();

        c.add(Calendar.HOUR, -2);
        Date E = c.getTime();

        c.add(Calendar.HOUR, -2);
        Date F = c.getTime();

        c.add(Calendar.HOUR, -2);
        Date G = c.getTime();

        c.add(Calendar.HOUR, -2);
        Date H = c.getTime();


        List result = new ArrayList();

        Map map1 = new HashMap();
        map1.put("time", splitTimeByHoure(B));
        map1.put("value", getCountByHoure(B, A));

        Map map2 = new HashMap();
        map2.put("time", splitTimeByHoure(C));
        map2.put("value", getCountByHoure(C, B));

        Map map3 = new HashMap();
        map3.put("time", splitTimeByHoure(D));
        map3.put("value", getCountByHoure(D, C));

        Map map4 = new HashMap();
        map4.put("time", splitTimeByHoure(E));
        map4.put("value", getCountByHoure(E, D));


        Map map5 = new HashMap();
        map5.put("time", splitTimeByHoure(F));
        map5.put("value", getCountByHoure(F, E));


        Map map6 = new HashMap();
        map6.put("time", splitTimeByHoure(G));
        map6.put("value", getCountByHoure(G, F));

        Map map7 = new HashMap();
        map7.put("time", splitTimeByHoure(H));
        map7.put("value", getCountByHoure(H, G));

        result.add(map1);
        result.add(map2);
        result.add(map3);
        result.add(map4);
        result.add(map5);
        result.add(map6);
        result.add(map7);
        //翻转
        Collections.reverse(result);
        return R.ok("请求成功").put("data", result).put("namalCount", 12).put("alarmCount", 45);
    }

    public String getCountByHoure(Date startTime, Date EndTime) throws IOException {
        long startTimes = startTime.getTime();
        long endTimes = EndTime.getTime();
        String URL = "http://36.26.62.70:10080/api/pass_records?_t=1640069918983&start_page=1&page_count=3000&begin_time=" + startTimes + "&end_time=" + endTimes + "&camera_id=5";
        HttpClient httpClient = new HttpClient(URL);
        httpClient.setHttps(false);
        httpClient.get();
        String content = httpClient.getContent();
        System.out.println(content);
        JSONObject jsonObject = JSONObject.parseObject(content);
        JSONObject data1 = JSONObject.parseObject(jsonObject.get("data").toString());
        return data1.get("total").toString();

    }

    public String splitTimeByHoure(Date date) throws ParseException {
        SimpleDateFormat df = new SimpleDateFormat("HH:mm");//设置日期格式,这里只取出小时和分钟
        String format = df.format(date);
        return format;
    }


}
