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

import cn.hutool.core.util.ObjectUtil;
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.video.dhicc.icc.CommonConstant;
import com.huimv.video.dhicc.result.R;
import com.huimv.video.dhicc.util.GetResponse;
import net.sf.json.JSONObject;
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/sizhu")
public class ScreenSiZhuController {
    /**
     * xuyao   直接都写在这里了
     *
     * @param request
     * @return
     * @throws ClientException
     * @throws ParseException
     */
    @RequestMapping("/count_sizhu_record")
    public R count_sizhu_record(HttpServletRequest request, @RequestParam(name = "farmId") Integer farmId) throws ClientException, ParseException {
        if (farmId != 1) {
            return R.ok("当前牧场无设备").put("data", null);
        }


        List result = new ArrayList();
        Map map1 = new HashMap();
        map1.put("value1", getPastSevenDay());
        map1.put("value2", getDay());
        map1.put("value3", getLatestOne());
        return R.ok("请求成功").put("data", map1);
    }


    /**
     * xuyao   直接都写在这里了
     *
     * @param request
     * @return 查询事件
     * @throws ClientException
     * @throws ParseException  写一个查左右的  事件都拿过来 存起来 建新的表来处理
     */
    @RequestMapping("/SiZhuList")
    public R SiZhuList(HttpServletRequest request, @RequestParam(name = "farmId") Integer farmId) throws ClientException, ParseException {

        if (farmId != 1) {
            return R.ok("当前牧场无设备").put("data", null);
        }

        return R.ok("请求成功").put("data", getSiZhuList());
    }


    //过去七天的值
    public String getPastSevenDay() throws ClientException {
        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.DATE, 0);
        Date A = c.getTime();
        String formatA = format.format(A);
        c.add(Calendar.DATE, -7);
        Date B = c.getTime();
        String formatB = format.format(B);
        return getResponse.SendCountSiZhu(formatB, formatA);
    }


    //今天的数据
    public String getDay() throws ClientException {
        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.DATE, 0);
        Date A = c.getTime();
        String formatA = format.format(A);
        c.add(Calendar.DATE, -1);
        Date B = c.getTime();
        String formatB = format.format(B);
        return getResponse.SendCountSiZhu(formatB, formatA);
    }


    //最新一条的数据
    public String getLatestOne() 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);
        params.put("pageNum", 1);
        params.put("pageSize", 1);
        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.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());


        JSONObject jsondata = (JSONObject) JSONObject.fromObject(generalResponse.getResult()).get("data");
        String substring = jsondata.get("pageData").toString().substring(1, jsondata.get("pageData").toString().length() - 1);
        if (ObjectUtil.isNotEmpty(substring)){
            JSONObject jsonObject1 = JSONObject.fromObject(substring);
            Object alarmDate = jsonObject1.get("alarmDate");

            return alarmDate.toString();
        }
        return null;

    }


    //返回所有的死猪事件
    public GeneralResponse getSiZhuList() 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, +24);
        Date d = c.getTime();
        String formatD = format.format(d);
        Map<String, Object> params = new HashMap<>();
        params.put("alarmStartDateString", formatA);
        params.put("alarmEndDateString", formatD);
        params.put("pageNum", 1);
        params.put("pageSize", 10);
        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.2.0/alarm-record/page";  //获取事件URL    post请求
        IClient iClient = new DefaultClient();
        //这种已经在配置文件里面安排了账号ip以及密码
        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;
    }
}



