package com.huimv.produce.warning.timer;


import cn.hutool.core.date.DateTime;
import cn.hutool.core.date.DateUtil;
import cn.hutool.json.JSONObject;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.huimv.produce.warning.entity.BaseWarningInfo;
import com.huimv.produce.warning.service.IBaseWarningInfoService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpEntity;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpMethod;
import org.springframework.http.ResponseEntity;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;

//@RestController
//@RequestMapping(value = "/test")
@Component
public class SavWarning {
    private static  final  String BASE_URL= "https://iot1.bigherdsman.com/api/v2/";
    private static  final  String TOKEN= "72438b180e9168fe90db126e3310cd0b20a7196d";

    private static  final  String HISTORY_LIST= "record/alarm?code=";
    private static final String FARM_CODE = "2022011710";


    @Autowired
    private IBaseWarningInfoService baseWarningInfoService;

//        @GetMapping("/test")
    @Scheduled(cron = "0 0/10 * * * ? ")
    public void saveEggData () throws ParseException {
            System.out.println("test");
        RestTemplate restTemplate = new RestTemplate();
        HttpHeaders headers = new HttpHeaders();
        headers.set("Authorization","Token "+TOKEN);
        HttpEntity<JSONObject> httpEntity = new HttpEntity<>(headers);

        Date date= new Date();
        long end = date.getTime();
        DateTime dateTime = DateUtil.offsetMinute(date, -10);
        long start = dateTime.getTime();
//        long start =  DateUtil.offsetMonth(date, -12).getTime();

        ResponseEntity<OneWarnig> exchange = restTemplate.exchange(BASE_URL + HISTORY_LIST + FARM_CODE + "&start=" + start + "&end=" + end, HttpMethod.GET, httpEntity, OneWarnig.class);

        OneWarnig body = exchange.getBody();
        SimpleDateFormat sf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        System.out.println(body);
        for (TwoWarning datum : body.getData()) {

            String i = datum.getI();
            String startTime = datum.getStart_time();
            String endTime = datum.getEnd_time();
            if(StringUtils.isNotBlank(startTime)){

                BaseWarningInfo baseWarningInfo = new BaseWarningInfo();
                baseWarningInfo.setFarmId(1);
                baseWarningInfo.setLevel(1);
                baseWarningInfo.setLevelName("一级报警");
                baseWarningInfo.setUploadTime(new Date());

                baseWarningInfo.setWarningTime(new Date(Long.parseLong(datum.getStart_time()+"000")));
            //高温报警
            if ("a_t_h".equals(datum.getName())){
                if (StringUtils.isNotBlank(startTime) && startTime.length() > 5){
                    baseWarningInfo.setMsg(i+"号舍高温预警,报警时段："+sf.format(new Date(Long.parseLong(startTime+"000")))+"至"+sf.format(new Date(Long.parseLong(endTime+"000"))));
                }

                baseWarningInfo.setAlarmType(7);
                baseWarningInfo.setWarningName("高温报警");
                baseWarningInfoService.save(baseWarningInfo);
            }

            //低温报警
            if ("a_t_l".equals(datum.getName())){
                if (StringUtils.isNotBlank(startTime) && startTime.length() > 5){
                    baseWarningInfo.setMsg(i+"号舍低温预警,报警时段："+sf.format(new Date(Long.parseLong(startTime+"000")))+"至"+sf.format(new Date(Long.parseLong(endTime+"000"))));
                }
                baseWarningInfo.setAlarmType(7);
                baseWarningInfo.setWarningName("低温报警");
                baseWarningInfoService.save(baseWarningInfo);
            }

            //高湿度
            if ("a_h_h".equals(datum.getName())){
                if (StringUtils.isNotBlank(startTime) && startTime.length() > 5){
                    baseWarningInfo.setMsg(i+"号舍高湿度预警,报警时段："+sf.format(new Date(Long.parseLong(startTime+"000")))+"至"+sf.format(new Date(Long.parseLong(endTime+"000"))));
                }
                baseWarningInfo.setAlarmType(7);
                baseWarningInfo.setWarningName("高湿度报警");
                baseWarningInfoService.save(baseWarningInfo);
            }

            //低湿度
            if ("a_h_l".equals(datum.getName())){
                if (StringUtils.isNotBlank(startTime) && startTime.length() > 5){
                    baseWarningInfo.setMsg(i+"号舍低湿度预警,报警时段："+sf.format(new Date(Long.parseLong(startTime+"000")))+"至"+sf.format(new Date(Long.parseLong(endTime+"000"))));
                }
                baseWarningInfo.setAlarmType(7);
                baseWarningInfo.setWarningName("低湿度报警");
                baseWarningInfoService.save(baseWarningInfo);
            }

            }

        }


    }

}
