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


import com.alibaba.fastjson.JSON;
import com.dahuatech.hutool.http.Method;
import com.dahuatech.icc.exception.ClientException;
import com.dahuatech.icc.oauth.http.DefaultClient;
import com.dahuatech.icc.oauth.http.IClient;
import com.dahuatech.icc.oauth.model.v202010.GeneralRequest;
import com.dahuatech.icc.oauth.model.v202010.GeneralResponse;
import com.huimv.common.utils.StringUtilsWork;
import com.huimv.video.dhicc.icc.CommonConstant;
import com.huimv.video.dhicc.result.R;
import net.sf.json.JSONObject;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

/**
 * 开门事件  以及看回放
 */
@RestController
@RequestMapping("door")
public class ClientDoorController {

    public static void main(String[] args) {
        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);
        Date A = c.getTime();
        String formatA = format.format(A);
        c.set(Calendar.HOUR, +24);
        Date d = c.getTime();
        String formatD = format.format(d);
        System.out.println(formatA + formatD);
    }


    //大门事件
    @RequestMapping("/ListDoorEvent")
    public R ListDoorEvent(@RequestBody Map<String, Object> params1) throws ClientException, ParseException {
        Integer farmId = (int) params1.get("farmId");
        if (farmId != 1) {
            //返回为空数据
            return R.ok("当前牧场无设备").put("data", null);
        }

        System.out.println("yuyuyuyuyuyu");
        return R.ok("请求成功").put("data", getDoorEvent(params1)).put("total", CountTimes(params1));
    }




    //返回所有的事件 的集合  包括死猪和洗消和大门 --分页参数
    public GeneralResponse getDoorEvent(Map<String, Object> params1) throws ClientException {
        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);
        Date A = c.getTime();
        String formatA = format.format(A);
        c.set(Calendar.HOUR_OF_DAY, +24);
        Date d = c.getTime();
        String formatD = format.format(d);
        Map<String, Object> params = new HashMap<>();
        params.put("alarmStartDateString", formatA);
        params.put("alarmEndDateString", formatD);
        if (StringUtilsWork.isNotEmpty((String) params1.get("alarmStartDateString"))) {
            params.put("alarmStartDateString", (String) params1.get("alarmStartDateString"));
            params.put("alarmEndDateString", (String) params1.get("alarmEndDateString"));
        }
        params.put("pageNum", (Integer) params1.get("pageNum"));
        params.put("pageSize", (Integer) params1.get("pageSize"));
        params.put("alarmType", 303);
        params.put("dbType", 0);
        params.put("deviceCategory", 1);
        String[] nodeCodeList = CommonConstant.DoorChannle;
        System.out.println("liebiaodamememememmee"+nodeCodeList);
        params.put("nodeCodeList", nodeCodeList);
        // String URL = "/evo-apigw/evo-event/1.2.0/alarm-record/page"; //获取事件URL    post请求
        String URL = "/evo-apigw/evo-event/1.2.0/alarm-record/page";  //获取事件URL    post请求
        IClient iClient = new DefaultClient();
        String NewUrl = URL;
        //这种已经在配置文件里面安排了账号ip以及密码
        GeneralRequest generalRequest = new GeneralRequest(NewUrl, Method.POST);
        System.out.println(NewUrl);
        generalRequest.header("Content-Type", " application/json");
        generalRequest.body(JSON.toJSONString(params));
        GeneralResponse generalResponse = iClient.doAction(generalRequest, generalRequest.getResponseClass());
        System.out.println("damen");
        return generalResponse;
    }


    public String CountTimes(Map<String, Object> params1) throws ClientException {
        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);
        Date A = c.getTime();
        String formatA = format.format(A);
        c.set(Calendar.HOUR_OF_DAY, +24);
        Date d = c.getTime();
        String formatD = format.format(d);
        Map<String, Object> params = new HashMap<>();
        params.put("alarmStartDateString", formatA);
        params.put("alarmEndDateString", formatD);
        if (StringUtilsWork.isNotEmpty((String) params1.get("alarmStartDateString"))) {
            params.put("alarmStartDateString", (String) params1.get("alarmStartDateString"));
            params.put("alarmEndDateString", (String) params1.get("alarmEndDateString"));
        }
        params.put("alarmType", 303);
        params.put("dbType", 0);
        params.put("deviceCategory", 1);
        String[] nodeCodeList = CommonConstant.DoorChannle;
        System.out.println(nodeCodeList+"大门                     列表");
        params.put("nodeCodeList", nodeCodeList);
        String URL = "/evo-apigw/evo-event/1.0.0/alarm-record/count-num";  //获取事件URL    post请求
        IClient iClient = new DefaultClient();
        String NewUrl = URL;
        System.out.println("开始执行");
        //这种已经在配置文件里面安排了账号ip以及密码
        GeneralRequest generalRequest = new GeneralRequest(NewUrl, Method.POST);
        System.out.println(NewUrl);
        generalRequest.header("Content-Type", " application/json");
        System.out.println(JSON.toJSONString(params));
        //里面是
        generalRequest.body(JSON.toJSONString(params));
        //发起请求处理应答
        GeneralResponse generalResponse = iClient.doAction(generalRequest, generalRequest.getResponseClass());
        System.out.println("执行结束");
        JSONObject jsonObject = JSONObject.fromObject(generalResponse.getResult());
        Object data = jsonObject.get("data");
        JSONObject jsonObject1 = JSONObject.fromObject(data);
        System.out.println(jsonObject1);
        return jsonObject1.get("value").toString();
    }

}
