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


import com.alibaba.fastjson.JSON;
import com.dahuatech.hutool.http.HttpRequest;
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 com.huimv.video.dhicc.service.IClientAllEventService;
import com.huimv.video.dhicc.service.ISysTelecomEventService;
import net.sf.json.JSONObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
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("/client/event")
public class ClientAllEventController {
    @Autowired
    private ISysTelecomEventService sysTelecomEventService;
    @Autowired
    private IClientAllEventService iClientAllEventService;

    //所有的事件接口
    @RequestMapping("/ListAllEvent")
    public R SiZhuList(@RequestBody Map<String, Object> params1) throws ClientException, ParseException {
        Integer farmId = (int) params1.get("farmId");
        if (farmId != 1) {
            return R.ok("当前牧场无设备").put("data", null);
        }
        return R.ok("请求成功").put("data", getEventList(params1)).put("total", CountTimes(params1));
    }


    //返回所有的事件 的集合  包括死猪和洗消和大门 --分页参数
    public GeneralResponse getEventList(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, 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);
        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.AllChannle;
        params.put("nodeCodeList", nodeCodeList);
        String URL = "/evo-apigw/evo-event/1.2.0/alarm-record/page";  //获取事件URL    post请求
        IClient iClient = new DefaultClient();
        GeneralRequest generalRequest = new GeneralRequest(URL, Method.POST);
        generalRequest.header("Content-Type", " application/json");
        generalRequest.body(JSON.toJSONString(params));
        GeneralResponse generalResponse = iClient.doAction(generalRequest, generalRequest.getResponseClass());
        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, 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);
        Map<String, Object> params = new HashMap<>();
        params.put("alarmStartDateString", formatA);
        params.put("alarmEndDateString", formatD);
        if (StringUtilsWork.isNotEmpty((String) params1.get("alarmStartDateString"))) {
            params.put("alarmStartDateString", params1.get("alarmStartDateString"));
            params.put("alarmEndDateString", params1.get("alarmEndDateString"));
        }
        params.put("alarmType", 303);
        params.put("dbType", 0);
        params.put("deviceCategory", 1);
        String[] nodeCodeList = CommonConstant.AllChannle;
        params.put("nodeCodeList", nodeCodeList);
        String URL = "/evo-apigw/evo-event/1.0.0/alarm-record/count-num";  //获取事件URL    post请求
        IClient iClient = new DefaultClient();
        GeneralRequest generalRequest = new GeneralRequest(URL, Method.POST);
        generalRequest.header("Content-Type", " application/json");
        generalRequest.body(JSON.toJSONString(params));
        JSONObject jsonObject = JSONObject.fromObject(JSONObject.fromObject(iClient.doAction(generalRequest, generalRequest.getResponseClass()).getResult()).get("data"));
        return jsonObject.get("value").toString();
    }

    /**
     * @Method : getEventBySort
     * @Description : 分类查看事件
     * @Params : [farmId, sort, startDateText, endDateText, pageNum, pageSize, request]
     * @Return : com.huimv.video.dhicc.result.R
     * @Author : ZhuoNing
     * @Date : 2021/12/24
     * @Time : 13:34
     */
    @RequestMapping(value = "/listEventBySort", method = RequestMethod.GET)
    public R getEventBySort(@RequestParam(value = "farmId", required = true) Integer farmId, @RequestParam(value = "sort", required = true) Integer sort,
                            @RequestParam(value = "alarmStartDateString", required = true) String startDateText, @RequestParam(value = "alarmEndDateString", required = true) String endDateText,
                            @RequestParam(value = "pageNum", required = true) Integer pageNum, @RequestParam(value = "pageSize", required = true) Integer pageSize, HttpServletRequest request) throws ClientException {
        //牧场id
        if (farmId != 1) {
            return R.ok("当前牧场无数据。").put("data", null);
        }
        //事件分类
        if (sort == null) {
            return R.ok("请选择事件分类。").put("data", null);
        }
        System.out.println("farmId="+farmId);
        System.out.println("sort="+sort);
        System.out.println("startDateText="+startDateText);
        System.out.println("endDateText="+endDateText);
        System.out.println("pageNum="+pageNum);
        System.out.println("pageSize="+pageSize);
        System.out.println("request="+request.toString());
        //
        return iClientAllEventService.getEventBySort(farmId, sort, startDateText, endDateText, pageNum, pageSize, request);
    }


}

