package com.huimv.environ.eco.timer;


import cn.hutool.core.date.DateTime;
import cn.hutool.core.date.DateUtil;
import cn.hutool.json.JSONObject;
import com.huimv.common.utils.Result;
import com.huimv.common.utils.ResultCode;
import com.huimv.environ.eco.entity.EggData;
import com.huimv.environ.eco.entity.EggHouse;
import com.huimv.environ.eco.entity.eggvo.HistoryEntity;
import com.huimv.environ.eco.entity.eggvo.HistroyOne;
import com.huimv.environ.eco.entity.eggvo.HistroyTwo;
import com.huimv.environ.eco.service.IEggDataService;
import com.huimv.environ.eco.service.IEggHouseService;
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.client.RestTemplate;

import java.util.Date;
import java.util.List;

@Component
public class EggTimer {
    private static  final  String BASE_URL= "https://iot1.bigherdsman.com/api/v2/";
    private static  final  String TOKEN= "72438b180e9168fe90db126e3310cd0b20a7196d";

    private static  final  String HISTORY_LIST= "history/list/?code=";
    private static final String FARM_CODE = "2022011710";

    @Autowired
    private IEggDataService eggDataService;
    @Autowired
    private IEggHouseService eggHouseService;


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

        List<EggHouse> list = eggHouseService.list();
        Date date= new Date();
        long end = date.getTime();
        DateTime dateTime = DateUtil.offsetMinute(date, -10);
        long start = dateTime.getTime();

        for (EggHouse eggHouse : list) {
            Integer i = eggHouse.getI();
            ResponseEntity<HistoryEntity> forEntity = restTemplate.exchange(BASE_URL + HISTORY_LIST+FARM_CODE+"&i="+i+"&start="+start+"&end="+end, HttpMethod.GET, httpEntity,HistoryEntity.class );

            List<HistroyOne> data = forEntity.getBody().getData();
            for (HistroyOne datum : data) {
                Long uploadTime = datum.getUpload_time();
                HistroyTwo data1 = datum.getData();

                EggData eggData = new EggData();
                eggData.setCo2(data1.getCo2());
                eggData.setFtDoTalTdy(data1.getFt_do_tal_tdy());
                eggData.setHR(data1.getH_r());
                eggData.setI(i);
                eggData.setTCur(data1.getT_cur());
                eggData.setWtrTdy(data1.getWtr_tdy());
                eggData.setUploadTime( new Date(uploadTime));


                eggData.setFN( data1.getF_n());
                eggData.setFLv( data1.getF_lv());
                eggData.setFV(data1.getF_v() );
                eggData.setTS(data1.getT_s());
                eggData.setTT( data1.getT_t());
                eggData.setHT( data1.getH_t());
                eggData.setHtL( data1.getHt_l());


                eggDataService.save(eggData);
            }

        }

    }


}
