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

import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.dahuatech.icc.exception.ClientException;
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.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.XunZhengRequest.Cn.XunZhengRequestCn;
import com.huimv.video.dhicc.test.request.XunZhengRequest.Hn.XunZhengRequestHn;
import com.huimv.video.dhicc.test.request.XunZhengRequest.Sg.XunZhengRequestSg;
import com.huimv.video.dhicc.util.GetResponse;
import net.sf.json.JSONObject;
import org.springframework.beans.factory.annotation.Autowired;
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 ScreenSiZhuTestController {
    @Autowired
    private OauthConfigBaseInfo oauthConfigBaseInfo;
    @Autowired
    private OauthConfigBaseInfo02 oauthConfigBaseInfo02;
    @Autowired
    private OauthConfigBaseInfo03 oauthConfigBaseInfo03;

    /**
     * xuyao   直接都写在这里了
     *
     * @param request
     * @return
     * @throws ClientException
     * @throws ParseException
     */
    @RequestMapping("/count_sizhu_record")
    public R count_sizhu_record(HttpServletRequest request, @RequestParam(name = "farmId") String farmId) throws ClientException, ParseException {
        System.out.println(farmId);
        Map map1 = new HashMap();
        map1.put("value1", getPastSevenDay(farmId));
        map1.put("value2", getDay(farmId));
        String latestOne = getLatestOne(farmId);
        map1.put("value3",latestOne );
        if (StringUtils.isNotBlank(latestOne)){
            return R.ok("请求成功").put("data", map1);
        }
        return R.ok("当前牧场无设备").put("data", null);
    }
    @RequestMapping("/SiZhuList")
    public R SiZhuList(HttpServletRequest request, @RequestParam(name = "farmId") String farmId) throws ClientException, ParseException {
        System.out.println(farmId);
        GeneralResponse siZhuList = getSiZhuList(farmId);
        if (ObjectUtil.isNotEmpty(siZhuList)){
            return R.ok("请求成功").put("data", siZhuList);
        }
        return R.ok("当前牧场无设备").put("data", null);
    }

    //过去七天的值
    public String getPastSevenDay(String farmId) 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);
        if ("1".equals(farmId)){
            return  getResponse.SendMassageXixiaoPerson(formatB, formatA,oauthConfigBaseInfo,CommonConstant.SIZhuChannle);
        }
        if ("22".equals(farmId)){
            return  getResponse.SendMassageXixiaoPerson02(formatB, formatA,oauthConfigBaseInfo02,CommonConstant.SIZhuChannle);
        }
        if ("21".equals(farmId)){
            return  getResponse.SendMassageXixiaoPerson03(formatB, formatA,oauthConfigBaseInfo03,CommonConstant.SIZhuChannle);
        }
        return "0";

    }


    //今天的数据
    public String getDay(String farmId) 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);
        if ("1".equals(farmId)){
            return  getResponse.SendMassageXixiaoPerson(formatB, formatA,oauthConfigBaseInfo,CommonConstant.SIZhuChannle);
        }
        if ("22".equals(farmId)){
            return  getResponse.SendMassageXixiaoPerson02(formatB, formatA,oauthConfigBaseInfo02,CommonConstant.SIZhuChannle);
        }
        if ("21".equals(farmId)){
            return  getResponse.SendMassageXixiaoPerson03(formatB, formatA,oauthConfigBaseInfo03,CommonConstant.SIZhuChannle);
        }
        return "0";
    }


    //最新一条的数据
    public String getLatestOne(String farmId)   {
        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);
        String result ="";
        if ("1".equals(farmId)){
            System.out.println("曙光");
            XunZhengRequestSg requestSg = new XunZhengRequestSg(oauthConfigBaseInfo);
            requestSg.setPageNum(1);
            requestSg.setPageSize(1);
            requestSg.setAlarmStartDateString(formatA);
            requestSg.setAlarmEndDateString(formatD);
            requestSg.setEventType(1);
            result = requestSg.doAction();
        }
        if ("22".equals(farmId)){
            System.out.println("海宁");
            XunZhengRequestHn requestSg = new XunZhengRequestHn(oauthConfigBaseInfo02);
            requestSg.setPageNum(1);
            requestSg.setPageSize(1);
            requestSg.setAlarmStartDateString(formatA);
            requestSg.setAlarmEndDateString(formatD);
            requestSg.setEventType(1);
            result = requestSg.doAction();
        }
        if ("21".equals(farmId)){
            System.out.println("苍南");
            XunZhengRequestCn requestSg = new XunZhengRequestCn(oauthConfigBaseInfo03);
            requestSg.setPageNum(1);
            requestSg.setPageSize(1);
            requestSg.setAlarmStartDateString(formatA);
            requestSg.setAlarmEndDateString(formatD);
            requestSg.setEventType(1);
            result = requestSg.doAction();
        }
        if (!"".equals(result)){
            JSONObject jsonObject = JSONObject.fromObject(result);
            System.out.println(jsonObject);
            JSONObject data = (JSONObject)jsonObject.get("data");
            String substring = data.get("pageData").toString().substring(1, data.get("pageData").toString().length() - 1);
            if (ObjectUtil.isNotEmpty(substring)){
                JSONObject jsonObject1 = JSONObject.fromObject(substring);
                Object alarmDate = jsonObject1.get("alarmDate");
                return alarmDate.toString();
            }
        }

        return result;

    }


    //返回所有的死猪事件
    public GeneralResponse getSiZhuList(String farmId) 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<>();
        if ("1".equals(farmId)){
            XunZhengRequestSg requestSg = new XunZhengRequestSg(oauthConfigBaseInfo);
            requestSg.setPageNum(1);
            requestSg.setPageSize(10);
            requestSg.setAlarmStartDateString(formatA);
            requestSg.setAlarmEndDateString(formatD);
            requestSg.setEventType(1);
            String result = requestSg.doAction();
            System.out.println(result);
        }
        if ("22".equals(farmId)){
            XunZhengRequestHn requestSg = new XunZhengRequestHn(oauthConfigBaseInfo02);
            requestSg.setPageNum(1);
            requestSg.setPageSize(10);
            requestSg.setAlarmStartDateString(formatA);
            requestSg.setAlarmEndDateString(formatD);
            requestSg.setEventType(1);
            String result = requestSg.doAction();
            System.out.println(result);
        }
        if ("21".equals(farmId)){
            XunZhengRequestCn requestSg = new XunZhengRequestCn(oauthConfigBaseInfo03);
            requestSg.setPageNum(1);
            requestSg.setPageSize(10);
            requestSg.setAlarmStartDateString(formatA);
            requestSg.setAlarmEndDateString(formatD);
            requestSg.setEventType(1);
            String result = requestSg.doAction();
            System.out.println(result);
        }
        return null;
    }
}



