package com.huimv.video.dhicc.util;

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 java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

public class GetResponse {

    /**
     * //查车辆通行数量的接口**不分进出
     *
     * @param begin 开始时间
     * @param end   结束时间
     * @return
     * @throws ClientException
     */
    public String SendMassageGetPage(String begin, String end) throws ClientException {
        //条件只有开始结束时间  有问题 这个是条数  有统计条数的专门接口
        //时间参数这里之前让前端传了  我这里只做转发  现在自己加上去
        String URL = "/evo-apigw/evo-accesscontrol/1.0.0/card/accessControl/swingCardRecord/bycondition/combinedCount?systime=";  //获取事件URL    post请求
        IClient iClient = new DefaultClient();
        String timestamp = String.valueOf((new Date()).getTime());
        Long aLong = Long.valueOf(timestamp);
        String NewUrl = URL + aLong;
        //这种已经在配置文件里面安排了账号ip以及密码
        GeneralRequest generalRequest = new GeneralRequest(NewUrl, Method.POST);
        generalRequest.header("Content-Type", " application/json");
        //设置时间参数
        Map<String, Object> params = new HashMap<>();
        params.put("startSwingTime", begin);
        params.put("endSwingTime", end);
        params.put("openType", "61");//刷脸开门
        //里面是
        generalRequest.body(JSON.toJSONString(params));
        //发起请求处理应答
        GeneralResponse generalResponse = iClient.doAction(generalRequest, generalRequest.getResponseClass());
        Object data = JSONObject.fromObject(generalResponse.getResult()).get("data");
        return data.toString();
    }

    /**
     * //查车辆通行数量的接口**出去记录
     *
     * @param begin 开始时间
     * @param end   结束时间
     * @return
     * @throws ClientException
     */
    public String SendMassageCarOut(String begin, String end) throws ClientException {
        String param = "queryTimeBegin=" + begin + "&queryTimeEnd=" + end + "&carDirect=9";
        String Url = "/evo-apigw/ipms/carcapture/find/conditions?";
        IClient iClient = new DefaultClient();
        System.out.println("开始执行");
        String newUrl = Url + param;
        GeneralRequest generalRequest = new GeneralRequest(newUrl, Method.GET);
        GeneralResponse generalResponse = iClient.doAction(generalRequest, generalRequest.getResponseClass());
        System.out.println("执行结束");
        JSONObject jsonObject = JSONObject.fromObject(generalResponse.getResult());
        Object data = jsonObject.get("data");
        JSONObject bject = JSONObject.fromObject(data);
        Object data1 = bject.get("totalRows"); //打印 2390
        return data1.toString();
    }


    /**
     * //查车辆通行数量的接口**进入的记录----车辆闸机
     *
     * @param begin 开始时间
     * @param end   结束时间
     * @return
     * @throws ClientException
     */
    public String SendMassageCarIn(String begin, String end) throws ClientException {
        String param = "queryTimeBegin=" + begin + "&queryTimeEnd=" + end + "&carDirect=8";
        String Url = "/evo-apigw/ipms/carcapture/find/conditions?";
        IClient iClient = new DefaultClient();
        System.out.println("开始执行");
        String newUrl = Url + param;
        System.out.println(newUrl);
        GeneralRequest generalRequest = new GeneralRequest(newUrl, Method.GET);
        GeneralResponse generalResponse = iClient.doAction(generalRequest, generalRequest.getResponseClass());
        System.out.println("执行结束");
        JSONObject jsonObject = JSONObject.fromObject(generalResponse.getResult());
        Object data = jsonObject.get("data");
        JSONObject bject = JSONObject.fromObject(data);
        Object data1 = bject.get("totalRows"); //打印 2390
        return data1.toString();

    }


    /**
     * //查人员通过过去几个小时的接口---正常通过次数
     *
     * @param begin 开始时间
     * @param end   结束时间
     * @return
     * @throws ClientException
     */
    public String SendMassageGetPagePerson(String begin, String end) throws ClientException {
        Map<String, Object> params = new HashMap<>();
        params.put("startSwingTime", begin);
        params.put("endSwingTime", end);
        params.put("openType", 61);
        String URL = "/evo-apigw/evo-accesscontrol/1.0.0/card/accessControl/swingCardRecord/bycondition/combinedCount?systime=";  //获取事件URL    post请求
        IClient iClient = new DefaultClient();
        String timestamp = String.valueOf((new Date()).getTime());
        Long aLong = Long.valueOf(timestamp);
        String NewUrl = URL + aLong;
        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");
        return data.toString();
    }


    /**
     * //查人员通过过去几个小时的接口---异常通过次数接口
     *
     * @param begin 开始时间
     * @param end   结束时间
     * @return
     * @throws ClientException
     */
    public String SendMassageGetPagePersonAlarm(String begin, String end) throws ClientException {
        Map<String, Object> params = new HashMap<>();
        params.put("startSwingTime", begin);
        params.put("endSwingTime", end);
        params.put("containDomain", "1");
        params.put("openResult", 0);
        String URL = "/evo-apigw/evo-accesscontrol/1.0.0/card/accessControl/swingCardRecord/bycondition/combinedCount?systime=";  //获取事件URL    post请求
        IClient iClient = new DefaultClient();
        String timestamp = String.valueOf((new Date()).getTime());
        Long aLong = Long.valueOf(timestamp);
        String NewUrl = URL + aLong;
        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");
        return data.toString();
    }


    public static void main(String[] args) throws ClientException {

        Map<String, Object> params1 = new HashMap<>();

        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.set(Calendar.HOUR, +24);
        Date d = c.getTime();
        String formatD = format.format(d);
        System.out.println(formatA + formatD);

        params1.put("pageNum", 1);
        params1.put("pageSize", 20);
        params1.put("startSwingTime", formatA);
        params1.put("endSwingTime", formatD);

        params1.put("containDomain", "1");
        params1.put("openResult", 0); //刷脸失败
        String URL = "http://36.26.62.70:80/evo-apigw/evo-accesscontrol/1.0.0/card/accessControl/swingCardRecord/bycondition/combined?systime=";
        IClient iClient = new DefaultClient();
        GeneralRequest generalRequest = new GeneralRequest(URL + Long.valueOf(String.valueOf((new Date()).getTime())), Method.POST);
        generalRequest.header("Content-Type", " application/json");
        generalRequest.body(JSON.toJSONString(params1));
        GeneralResponse generalResponse = iClient.doAction(generalRequest, generalRequest.getResponseClass());
        JSONObject jsonObject = JSONObject.fromObject(generalResponse.getResult());
        GetResponse getResponse = new GetResponse();

        System.out.println(jsonObject.toString() + getResponse.SendMassageGetPagePersonAlarm((String) params1.get("startSwingTime"), (String) params1.get("endSwingTime")));
    }


    /**
     * //查洗消 ---时间次数的接口-----
     *
     * @param begin 开始时间
     * @param end   结束时间
     * @return
     * @throws ClientException
     */
    public String SendMassageXixiaoPerson(String begin, String end) throws ClientException {
        Map<String, Object> params = new HashMap<>();
        params.put("alarmStartDateString", begin);
        params.put("alarmEndDateString", end);
        params.put("alarmType", 303);
        params.put("dbType", 0);
        params.put("deviceCategory", 1);
        params.put("nodeCodeList", CommonConstant.XixiaoChannle);
        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();
    }


    /**
     * //查大门 ---时间次数的接口-----
     *
     * @param begin 开始时间
     * @param end   结束时间
     * @return
     * @throws ClientException
     */
    public String SendMassageGateCount(String begin, String end) throws ClientException {
        Map<String, Object> params = new HashMap<>();
        params.put("alarmStartDateString", begin);
        params.put("alarmEndDateString", end);
        params.put("alarmType", 303);
        params.put("dbType", 0);
        params.put("deviceCategory", 1);
        params.put("nodeCodeList", CommonConstant.DoorChannle);
        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();
    }


    /**
     * //查死猪 ---时间次数的接口-----
     *
     * @param begin 开始时间
     * @param end   结束时间
     * @return
     * @throws ClientException
     */
    public String SendCountSiZhu(String begin, String end) throws ClientException {
        Map<String, Object> params = new HashMap<>();
        params.put("alarmStartDateString", begin);
        params.put("alarmEndDateString", end);
        params.put("alarmType", 303);
        params.put("dbType", 0);
        params.put("deviceCategory", 1);
        //死猪通道
        String[] nodeCodeList = CommonConstant.SIZhuChannle;
        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();
    }


    /**
     * //查洗消死猪事件的时间-----次数的接口-----
     *
     * @param begin 开始时间
     * @param end   结束时间
     * @return
     * @throws ClientException
     */
    public String SendMassageSiZhuRecord(String begin, String end) throws ClientException {
        Map<String, Object> params = new HashMap<>();
        params.put("alarmStartDateString", begin);
        params.put("alarmEndDateString", end);
        params.put("alarmType", 303);
        params.put("dbType", 0);
        params.put("deviceCategory", 1);
        //死猪的通道
        String[] nodeCodeList = CommonConstant.SIZhuChannle;
        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();
    }


    public Object SendMassagePost(String param, String Url, String chooseMethod) throws ClientException {
        IClient iClient = new DefaultClient();
        System.out.println("开始执行");
        String newUrl = Url + param;
        //这种已经在配置文件里面安排了账号ip以及密码    现在变了
        GeneralRequest generalRequest = new GeneralRequest(newUrl, Method.POST);
        GeneralResponse generalResponse = iClient.doAction(generalRequest, generalRequest.getResponseClass());
        System.out.println("执行结束");
        return R.ok("请求成功").put("token", generalResponse);

    }

}
