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


import com.dahuatech.icc.exception.ClientException;
import com.dahuatech.icc.oauth.model.v202010.GeneralResponse;
import com.huimv.video.dhicc.result.R;
import com.huimv.video.dhicc.util.GetResponse;
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.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.*;

@RestController
@RequestMapping("/screen/person")
public class ScreenPersonController {

    //成功通行记录
    @RequestMapping("/count_person_record")
    public R ddgetCarRecord(HttpServletRequest request, @RequestParam(name = "farmId") Integer farmId) throws ClientException, ParseException {

        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_OF_DAY, +2);
        Date A = c.getTime();
        String formatA = format.format(A);

        c.add(Calendar.HOUR_OF_DAY, -2);
        Date B = c.getTime();
        String formatB = format.format(B);

        c.add(Calendar.HOUR_OF_DAY, -2);
        Date C = c.getTime();
        String formatC = format.format(C);

        c.add(Calendar.HOUR_OF_DAY, -2);
        Date D = c.getTime();
        String formatD = format.format(D);

        c.add(Calendar.HOUR_OF_DAY, -2);
        Date E = c.getTime();
        String formatE = format.format(E);

        c.add(Calendar.HOUR_OF_DAY, -2);
        Date F = c.getTime();
        String formatF = format.format(F);

        c.add(Calendar.HOUR_OF_DAY, -2);
        Date G = c.getTime();
        String formatG = format.format(G);

        c.add(Calendar.HOUR_OF_DAY, -2);
        Date H = c.getTime();
        String formatH = format.format(H);

        List result = new ArrayList();

        Map map1 = new HashMap();
        map1.put("time", splitTimeByHoure(B));
        map1.put("value", getResponse.SendMassageGetPage(formatB, formatA));

        Map map2 = new HashMap();
        map2.put("time", splitTimeByHoure(C));
        map2.put("value", getResponse.SendMassageGetPage(formatC, formatB));

        Map map3 = new HashMap();
        map3.put("time", splitTimeByHoure(D));
        map3.put("value", getResponse.SendMassageGetPage(formatD, formatC));

        Map map4 = new HashMap();
        map4.put("time", splitTimeByHoure(E));
        map4.put("value", getResponse.SendMassageGetPage(formatE, formatD));

        Map map5 = new HashMap();
        map5.put("time", splitTimeByHoure(F));
        map5.put("value", getResponse.SendMassageGetPage(formatF, formatE));


        Map map6 = new HashMap();
        map6.put("time", splitTimeByHoure(G));
        map6.put("value", getResponse.SendMassageGetPage(formatG, formatF));

        Map map7 = new HashMap();
        map7.put("time", splitTimeByHoure(H));
        map7.put("value", getResponse.SendMassageGetPage(formatH, formatG));

        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);
    }


    //人员正常通行与异常通行的数据
    @RequestMapping("/count_person_record_fail_success")
    public R count_person_record_fail_success(HttpServletRequest request, @RequestParam(name = "farmId") Integer farmId) throws ClientException, ParseException {

        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.HOUR_OF_DAY, 0);
        c.set(Calendar.MINUTE, 0);
        c.set(Calendar.SECOND, 0);

        c.add(Calendar.DATE, +1);
        Date A = c.getTime();
        String formatA = format.format(A);

        c.add(Calendar.DATE, -1);
        Date B = c.getTime();
        String formatB = format.format(B);

        Map map1 = new HashMap();
        map1.put("value1", getResponse.SendMassageGetPagePerson(formatB, formatA));
        map1.put("value2", getResponse.SendMassageGetPagePersonAlarm(formatB, formatA));

        System.out.println();
        return R.ok("请求成功").put("data", map1);

    }

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

}
