package com.huimv.receiver.eco.controller;

import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.huimv.receiver.cloud.service.IWarningInfo;
import com.huimv.receiver.eco.entity.*;
import com.huimv.receiver.eco.service.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;


//接收温湿度数据
@RestController
@RequestMapping(value = "/receiver/eco")
public class EcoController {
    @Autowired
    private ISysHumidityService sysHumidityService;

    @Autowired
    private ISysTemperatureService sysTemperatureService;

    @Autowired
    private IBaseWarningInfoService baseWarningInfoService;

    @Autowired
    private ISysFodderService fodderService;

    @Autowired
    private ISysMonthWaterService monthWaterService;

    @Autowired
    private ISysDayWaterService dayWaterService;

    @Autowired
    private IBaseRoomService baseRoomService;





    @PostMapping(value = "/save")
    public String save(@RequestBody HumAndTemDto humAndTemDto){
        System.out.println("开始");

        if (ObjectUtil.isEmpty(humAndTemDto)){
            return "数据为空";
        }
        System.out.println(humAndTemDto.toString());
        if (1 ==humAndTemDto.getType()){

            BoxStateChanged boxStateChanged = humAndTemDto.getStateChange();
            if (ObjectUtil.isNotEmpty(boxStateChanged)){
                UpdateWrapper<BaseRoom> wrapper = new UpdateWrapper<>();
                wrapper.eq("box_id",boxStateChanged.id);
                BaseRoom baseRoom = new BaseRoom();
                baseRoom.setStatus(boxStateChanged.getState());
                baseRoomService.update(baseRoom,wrapper);
                System.out.println( boxStateChanged.getState() +"-------------->"+boxStateChanged.getId());
            }
            return "成功";

        }


        List<SysHumidity> humidity = humAndTemDto.getHumidity();
        if (ObjectUtil.isNotEmpty(humidity)){
            try {
                sysHumidityService.saveBatch(humidity);
            }catch (Exception e){
                System.out.println("湿度存储失败");
            }
        }else {
            System.out.println("湿度数据为空");
        }


        List<SysTemperature> temperature = humAndTemDto.getTemperature();
        if (ObjectUtil.isNotEmpty(temperature)){
            try {
                sysTemperatureService.saveBatch(temperature);
            }catch (Exception e){
                System.out.println("温度存储失败");
            }

        }else {
            System.out.println("温度数据为空");
        }


        List<BaseWarningInfo> warningInfos = humAndTemDto.getWarningInfos();
        if (ObjectUtil.isNotEmpty(warningInfos)){
            try {
                baseWarningInfoService.saveBatch(warningInfos);
            }catch (Exception e){
                System.out.println("报警存储失败");
            }

        }else {
            System.out.println("报警数据为空");
        }


        List<SysFodder> fodders = humAndTemDto.getFodders();
        if (ObjectUtil.isNotEmpty(fodders)){
            try {
                for (SysFodder fodder : fodders) {
                    Integer fodderId = fodder.getFodderId();
                    SysFodder byId = fodderService.getById(fodderId);
                    if (ObjectUtil.isEmpty(byId)){
                        fodderService.save(fodder);
                    }else {
                        fodderService.updateById(fodder);
                    }
                }

            }catch (Exception e){
                System.out.println("用料存储失败");
            }

        }else {
            System.out.println("用料数据为空");
        }


        List<SysMonthWater> monthWaters = humAndTemDto.getMonthWaters();
        if (ObjectUtil.isNotEmpty(monthWaters)){
            try {
                monthWaterService.saveBatch(monthWaters);
            }catch (Exception e){
                System.out.println("月用水存储失败");
            }

        }else {
            System.out.println("月用水数据为空");
        }


        List<SysDayWater> dayWaters = humAndTemDto.getDayWaters();
        if (ObjectUtil.isNotEmpty(dayWaters)){
            try {
                dayWaterService.saveBatch(dayWaters);
            }catch (Exception e){
                System.out.println("日用水存储失败");
            }
        }else {
            System.out.println("日用水数据为空");
        }



        System.out.println("结束");
        return "成功";
    }

}
