package com.huimv.eartag.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.huimv.eartag.entity.*;
import com.huimv.eartag.service.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.web.bind.annotation.RestController;

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

/**
 * <p>
 * 栋舍统计数据 前端控制器
 * </p>
 *
 * @author author
 * @since 2022-06-01
 */
@RestController
@RequestMapping("/feedBuildCount")
public class FeedBuildCountController {


    @Autowired
    private IFeedBuildService feedBuildService;

    @Autowired
    private IFeedBuildCountService feedBuildCountService;

    @Autowired
    private IFeedStageService feedStageService;

    @Autowired
    private IEartagDataService eartagDataService;


    @Autowired
    private IFeedFowlRecordService feedFowlRecordService;

//    @Scheduled(cron = "0 * * * * ? ")
    @GetMapping("/test1")
    public void updateBulidCount(){
        System.out.println("111111111111111");
        List<FeedBuild> list = feedBuildService.list();
        for (FeedBuild feedBuild : list) {
            FeedBuildCount feedBuildCount = new FeedBuildCount();

            FeedStage feedStage = feedStageService.getOne(new QueryWrapper<FeedStage>().eq("stage_code", feedBuild.getStageCode()));
            FeedFowlRecord buildCode = feedFowlRecordService.getOne(new QueryWrapper<FeedFowlRecord>().eq("build_code", feedBuild.getBuildCode()).last("limit 1"));


            feedBuildCount.setAbnormity(0);
            feedBuildCount.setAveTemp(buildCode.getAveTemp());
            feedBuildCount.setBuildCode(feedBuild.getBuildCode());
            feedBuildCount.setEnvTemp(buildCode.getEnvTemp());
            feedBuildCount.setFowlGender(1);
            feedBuildCount.setFowlType(feedStage.getStageName());
            feedBuildCount.setUpdateDate(new Date());
            feedBuildCountService.save(feedBuildCount);
        }
    }

    //    @Scheduled(cron = "30 * * * * ? ")
    @GetMapping("/test")
    public void updateFowlRecordService() {


        System.out.println("ssss=========");
        List<FeedFowlRecord> list = feedFowlRecordService.list();
        for (FeedFowlRecord feedFowlRecord : list) {
            String earmark = feedFowlRecord.getEarmark();
            EartagData one = eartagDataService.getOne(new QueryWrapper<EartagData>().eq("earmark", earmark).orderByDesc("id").last("limit 1"));
            feedFowlRecord.setAveTemp(Float.parseFloat(one.getOther()));
            feedFowlRecord.setEarTemp(one.getEarTemp1());
            feedFowlRecord.setEnvTemp(one.getEnvTemp1());
            feedFowlRecordService.updateById(feedFowlRecord);
        }

    }

}
