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



import com.dahuatech.icc.exception.ClientException;
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 com.huimv.video.dhicc.test.config.OauthConfigBaseInfo;
import com.huimv.video.dhicc.test.config.OauthConfigBaseInfo02;
import com.huimv.video.dhicc.test.config.OauthConfigBaseInfo03;
import com.huimv.video.dhicc.test.request.event.Cn.ClientAllEventRecordRequestCn;
import com.huimv.video.dhicc.test.request.event.Hn.ClientAllEventRecordRequestHn;
import com.huimv.video.dhicc.test.request.event.Sg.ClientAllEventRecordRequestSg;
import com.huimv.video.dhicc.test.request.event.Cn.ClientAllEventRequestCn;
import com.huimv.video.dhicc.test.request.event.Hn.ClientAllEventRequestHn;
import com.huimv.video.dhicc.test.request.event.Sg.ClientAllEventRequestSg;
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.Map;


@RestController
@RequestMapping("/client/event/Test")
public class ClientAllEventControllerTest {
    @Autowired
    private ISysTelecomEventService sysTelecomEventService;
    @Autowired
    private IClientAllEventService iClientAllEventService;
    @Autowired
    private OauthConfigBaseInfo oauthConfigBaseInfo;
    @Autowired
    private OauthConfigBaseInfo02 oauthConfigBaseInfo02;
    @Autowired
    private OauthConfigBaseInfo03 oauthConfigBaseInfo03;

    //所有的事件接口
    @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 Object 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);

        Integer farmId =(int) params1.get("farmId");
        Object action=null;
        if (farmId == 1) {
            System.out.println("shuguang");
            ClientAllEventRecordRequestSg requestSg = new ClientAllEventRecordRequestSg(oauthConfigBaseInfo);
            if (StringUtilsWork.isNotEmpty((String) params1.get("alarmStartDateString"))) {
                requestSg.setAlarmStartDateString((String) params1.get("alarmStartDateString"));
                requestSg.setAlarmEndDateString((String) params1.get("alarmEndDateString"));
            } else {
                requestSg.setAlarmStartDateString(formatA);
                requestSg.setAlarmEndDateString(formatD);
            }
            requestSg.setPageNum((Integer) params1.get("pageNum"));
            requestSg.setPageSize((Integer) params1.get("pageSize"));
            requestSg.setAlarmType(303);
            requestSg.setDbType(0);
            requestSg.setDeviceCategory(1);
            String[] nodeCodeList = CommonConstant.AllChannle;
            requestSg.setNodeCodeList(nodeCodeList);
            Object action1 = requestSg.doAction();
            action = action1;
        }
        if (farmId == 22) {
            System.out.println("haining");
            ClientAllEventRecordRequestHn requestHn = new ClientAllEventRecordRequestHn(oauthConfigBaseInfo02);
            if (StringUtilsWork.isNotEmpty((String) params1.get("alarmStartDateString"))) {
                requestHn.setAlarmStartDateString((String) params1.get("alarmStartDateString"));
                requestHn.setAlarmEndDateString((String) params1.get("alarmEndDateString"));
            } else {
                requestHn.setAlarmStartDateString(formatA);
                requestHn.setAlarmEndDateString(formatD);
            }
            requestHn.setPageNum((Integer) params1.get("pageNum"));
            requestHn.setPageSize((Integer) params1.get("pageSize"));
            requestHn.setAlarmType(303);
            requestHn.setDbType(0);
            requestHn.setDeviceCategory(1);
            String[] nodeCodeList = CommonConstant.AllChannle;
            requestHn.setNodeCodeList(nodeCodeList);
            Object action1 = requestHn.doAction();
            action = action1;
        }
        if (farmId == 21) {
            System.out.println("cangnan");
            ClientAllEventRecordRequestCn requestCn = new ClientAllEventRecordRequestCn(oauthConfigBaseInfo03);
            if (StringUtilsWork.isNotEmpty((String) params1.get("alarmStartDateString"))) {
                requestCn.setAlarmStartDateString((String) params1.get("alarmStartDateString"));
                requestCn.setAlarmEndDateString((String) params1.get("alarmEndDateString"));
            } else {
                requestCn.setAlarmStartDateString(formatA);
                requestCn.setAlarmEndDateString(formatD);
            }
            requestCn.setPageNum((Integer) params1.get("pageNum"));
            requestCn.setPageSize((Integer) params1.get("pageSize"));
            requestCn.setAlarmType(303);
            requestCn.setDbType(0);
            requestCn.setDeviceCategory(1);
            String[] nodeCodeList = CommonConstant.AllChannle;
            requestCn.setNodeCodeList(nodeCodeList);
            Object action1= requestCn.doAction();
            action = action1;
        }


        return action;
    }


    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);
        String[] nodeCodeList = CommonConstant.AllChannle;

        JSONObject jsonObject = null;
        if ((int) params1.get("farmId") == 1) {
            System.out.println("shuguang");
            ClientAllEventRequestSg requestSg = new ClientAllEventRequestSg(oauthConfigBaseInfo);
            requestSg.setDbType(0);
            requestSg.setAlarmType(303);
            requestSg.setDeviceCategory(1);
            requestSg.setNodeCodeList(nodeCodeList);
            if (StringUtilsWork.isNotEmpty((String) params1.get("alarmStartDateString"))) {
                requestSg.setAlarmStartDateString((String) params1.get("alarmStartDateString"));
                requestSg.setAlarmEndDateString((String) params1.get("alarmEndDateString"));
            } else {
                requestSg.setAlarmStartDateString(formatA);
                requestSg.setAlarmEndDateString(formatD);
            }
            JSONObject action = JSONObject.fromObject(requestSg.doAction());
            jsonObject = action;

        }
        if ((int) params1.get("farmId") == 22) {
            System.out.println("haining");
            ClientAllEventRequestHn requestHn = new ClientAllEventRequestHn(oauthConfigBaseInfo02);
            requestHn.setDbType(0);
            requestHn.setAlarmType(303);
            requestHn.setDeviceCategory(1);
            requestHn.setNodeCodeList(nodeCodeList);
            if (StringUtilsWork.isNotEmpty((String) params1.get("alarmStartDateString"))) {
                requestHn.setAlarmStartDateString((String) params1.get("alarmStartDateString"));
                requestHn.setAlarmEndDateString((String) params1.get("alarmEndDateString"));
            } else {
                requestHn.setAlarmStartDateString(formatA);
                requestHn.setAlarmEndDateString(formatD);
            }
            JSONObject action = JSONObject.fromObject(requestHn.doAction());
            jsonObject = action;
        }
        if ((int) params1.get("farmId") == 21) {
            System.out.println("cangnan");
            ClientAllEventRequestCn requestCn = new ClientAllEventRequestCn(oauthConfigBaseInfo03);
            requestCn.setDbType(0);
            requestCn.setAlarmType(303);
            requestCn.setDeviceCategory(1);
            requestCn.setNodeCodeList(nodeCodeList);
            if (StringUtilsWork.isNotEmpty((String) params1.get("alarmStartDateString"))) {
                requestCn.setAlarmStartDateString((String) params1.get("alarmStartDateString"));
                requestCn.setAlarmEndDateString((String) params1.get("alarmEndDateString"));
            } else {
                requestCn.setAlarmStartDateString(formatA);
                requestCn.setAlarmEndDateString(formatD);
            }
            JSONObject action = JSONObject.fromObject(requestCn.doAction());
            jsonObject = action;
        }

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


}

