package com.huimv.environ.eco.service.impl;

import cn.hutool.core.date.DateUtil;
import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.huimv.common.utils.DataUill;
import com.huimv.common.utils.Result;
import com.huimv.common.utils.ResultCode;
import com.huimv.environ.eco.entity.BaseFloor;
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.entity.dto.AllRoomEnvironment;
import com.huimv.environ.eco.entity.dto.AllWaterEnvPowerApp;
import com.huimv.environ.eco.entity.dto.ScreenDto;
import com.huimv.environ.eco.mapper.*;
import com.huimv.environ.eco.service.IBaseRoomService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

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

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author astupidcoder
 * @since 2021-11-11
 */
@Service
public class BaseRoomServiceImpl extends ServiceImpl<BaseRoomMapper, BaseRoom> implements IBaseRoomService {

    @Autowired
    private SysHumidityMapper humidityMapper;

    @Autowired
    private SysTemperatureMapper temperatureMapper;

    @Autowired
    private SysFodderMapper fodderMapper;

    @Autowired
    private BaseRoomMapper baseRoomMapper;

    @Autowired
    private BaseFloorMapper floorMapper;


    @Override
    public Result getAllRoomEnvironment(Integer farmId) {
        baseRoomMapper.setS();
        List<AllRoomEnvironment> list =  baseRoomMapper.getAllRoomEnvironment(farmId);
        for (AllRoomEnvironment allRoomEnvironment : list) {
            if (allRoomEnvironment.getStatus() != 1){
                allRoomEnvironment.setHumidity(null);
                allRoomEnvironment.setTemperature(null);
            }else {
                if (null == allRoomEnvironment.getHumidity()){
                    allRoomEnvironment.setHumidity(0.0);
                }
                if (null == allRoomEnvironment.getTemperature()){
                    allRoomEnvironment.setTemperature(0.0);
                }
            }
        }

        return new Result(ResultCode.SUCCESS,list);
    }

    @Override
    public Result listByRoom(Map<String,String> map) {
        String roomId = map.get("roomId");
        String startDate = map.get("startDate");
        String endDate = map.get("endDate");
        String type = map.get("type");
        String farmId = map.get("farmId");

        baseRoomMapper.setS();
        BaseRoom baseRoom = new BaseRoom();
        if (StrUtil.isBlank(roomId)){
            baseRoom =  this.getOne(new QueryWrapper<BaseRoom>().eq("farm_id",farmId).last("limit 1"));
            if (ObjectUtil.isNotEmpty(baseRoom)){
                roomId = baseRoom.getId()+"";
            }

        }
        baseRoom =this.getById(roomId);

        if (ObjectUtil.isEmpty(baseRoom)){
            return new Result(ResultCode.SUCCESS,null);
        }
        QueryWrapper<SysHumidity> humidityWrapper = new QueryWrapper<>();
        QueryWrapper<SysTemperature> temperatureWrapper = new QueryWrapper<>();
        humidityWrapper.eq("room_id",roomId);
        temperatureWrapper.eq("room_id",roomId);
        humidityWrapper.between("value",0,100);
        temperatureWrapper.between("value", -50,50);

        List<SysHumidity> humidities;
        List<SysTemperature> semperatures;
        //根据自定义
        Map resultMap = new HashMap<>();
        if ("4".equals(type) ){
            startDate =startDate + " 00:00:00";
            endDate =endDate + " 23:59:59";
            humidityWrapper.between("create_time", startDate, endDate);
            temperatureWrapper.between("create_time", startDate, endDate);
            humidities =   humidityMapper.listDay(humidityWrapper);
            semperatures =   temperatureMapper.listDay(temperatureWrapper);
            resultMap.put("humidities",humidities);
            resultMap.put("semperatures",semperatures);
        }

        //本月
        else if ("3".equals(type)){
            Date timesMonthmorning = DataUill.getTimesMonthmorning();
            humidityWrapper.ge("create_time",timesMonthmorning );

            temperatureWrapper.ge("create_time", timesMonthmorning);

            humidities =   humidityMapper.listDay(humidityWrapper);
            semperatures =   temperatureMapper.listDay(temperatureWrapper);

            resultMap.put("humidities",humidities);
            resultMap.put("semperatures",semperatures);
        }
        //本周
        else if ("2".equals(type)){
            Date timesWeekmorning = DateUtil.beginOfWeek(new Date());
            humidityWrapper.ge("create_time",timesWeekmorning );
            temperatureWrapper.ge("create_time", timesWeekmorning);
            humidities =   humidityMapper.listDay(humidityWrapper);
            semperatures =   temperatureMapper.listDay(temperatureWrapper);

            resultMap.put("humidities",humidities);
            resultMap.put("semperatures",semperatures);
        }
        //今日
        else {
            Date timesmorning = DataUill.getTimesmorning();
            humidities = humidityMapper.selectList(humidityWrapper.ge("create_time",timesmorning ));
            semperatures = temperatureMapper.selectList(temperatureWrapper.ge("create_time", timesmorning));
            resultMap.put("humidities",humidities);
            resultMap.put("semperatures",semperatures);
        }

        resultMap.put("roomName",baseRoom.getAlias()+baseRoom.getRoomName());
        return new Result(ResultCode.SUCCESS,resultMap);
    }

    @Override
    public Result listLargeScreen(String farmId) {
        ScreenDto screenDto =  fodderMapper.fodderMapper(farmId);
        return new Result(ResultCode.SUCCESS,screenDto);

    }

    @Override
    public Result listAllWaterEnvPowerApp(Map<String,String> map) {
        String farmId = map.get("farmId");
        String boxId = map.get("boxId");
        QueryWrapper<BaseRoom> wrapper = new QueryWrapper<>();
        if (StringUtils.isNotBlank(farmId)){
            wrapper.last("where br.farm_id = " +farmId);
        }
        if (StringUtils.isNotBlank(boxId)){
            wrapper.last("where br.box_id = '" +boxId+"'");
        }
        baseRoomMapper.setS();
        List<AllWaterEnvPowerApp> waterEnvPowerApps = baseRoomMapper.listAllWaterEnvPowerApp(wrapper);

        return new Result(ResultCode.SUCCESS,waterEnvPowerApps);
    }



}
