package com.huimv.environ.eco.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.huimv.common.utils.Result;
import com.huimv.common.utils.ResultCode;
import com.huimv.environ.eco.entity.BaseBox;
import com.huimv.environ.eco.entity.BaseRoom;
import com.huimv.environ.eco.entity.SysHumidity;
import com.huimv.environ.eco.entity.SysTemperature;
import com.huimv.environ.eco.service.IBaseRoomService;
import com.huimv.environ.eco.service.ISysHumidityService;
import com.huimv.environ.eco.service.ISysTemperatureService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

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

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author astupidcoder
 * @since 2021-11-11
 */
@RestController
@RequestMapping("/room")
public class BaseRoomController {

    @Autowired
    private IBaseRoomService baseRoomService;

    @GetMapping("/getAllRoomEnvironment")
    private Result getAllRoomEnvironment(@RequestParam(name = "farmId") Integer farmId){
        return  baseRoomService.getAllRoomEnvironment(farmId);

    }

    @GetMapping("list")
    public Result list(@RequestParam(name = "farmId") Integer farmId){
        return new Result(ResultCode.SUCCESS,baseRoomService.list(new QueryWrapper<BaseRoom>().eq("farm_id",farmId)));
    }

    @PostMapping("/listByRoom")
    public Result listByRoom(@RequestBody Map<String, String> map){
        return  baseRoomService.listByRoom(map);
    }

    @GetMapping("/listBy")
    public Result listByRoom(@RequestParam(name = "uid") String uid){
        return new Result(ResultCode.SUCCESS,baseRoomService.list(new QueryWrapper<BaseRoom>().eq("box_id",uid)));
    }


    @GetMapping("/listLargeScreen")
    public Result listLargeScreen(@RequestParam(name = "farmId") String farmId){
        return baseRoomService.listLargeScreen(farmId);
    }

    @PostMapping("/listAllWaterEnvPowerApp")
    public Result listAllWaterEnvPowerApp(@RequestBody Map<String, String> map){
        return baseRoomService.listAllWaterEnvPowerApp(map);
    }



    @Autowired
    private ISysTemperatureService sysTemperatureService;

    @Autowired
    private ISysHumidityService sysHumidityService;

    @GetMapping("tettt")
    public void tettt(){
        List<BaseRoom> list = baseRoomService.list(null);
        Date now = new Date();

        for (BaseRoom baseRoom : list) {
            Integer roomId = baseRoom.getId();
            SysTemperature one = sysTemperatureService.getOne(new QueryWrapper<SysTemperature>().last("limit 1 ").eq("room_id",roomId));

            SysHumidity one2 = sysHumidityService.getOne(new QueryWrapper<SysHumidity>().last("limit 1 ").eq("room_id",roomId));
            Double aDouble = one.getValue();
            Double aDouble2 = one2.getValue();
            SysTemperature sysTemperature = new SysTemperature();
            SysHumidity sysHumidity = new SysHumidity();



            sysTemperature.setRoomId(roomId);
            sysTemperature.setCreateTime(now);
            sysTemperature.setFarmId(1);
            sysTemperature.setId("111111");
            sysTemperature.setOriginal(aDouble *10 );
            sysTemperature.setValue(aDouble+0.1);


            sysHumidity.setRoomId(roomId);
            sysHumidity.setCreateTime(now);
            sysHumidity.setFarmId(1);
            sysHumidity.setId("111111");
            sysHumidity.setValue(aDouble2+0.1);



            sysTemperatureService.save(sysTemperature);
            sysHumidityService.save(sysHumidity);
        }

    }

}
