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

import cn.hutool.core.date.DateUtil;
import cn.hutool.core.util.ObjectUtil;
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.PercentUtil;
import com.huimv.common.utils.Result;
import com.huimv.common.utils.ResultCode;
import com.huimv.environ.eco.entity.BaseRoom;
import com.huimv.environ.eco.entity.SysDayWater;
import com.huimv.environ.eco.entity.SysMonthWater;
import com.huimv.environ.eco.entity.dto.DayWaterDto;
import com.huimv.environ.eco.entity.dto.WaterAndPower;
import com.huimv.environ.eco.mapper.BaseRoomMapper;
import com.huimv.environ.eco.mapper.SysDayWaterMapper;
import com.huimv.environ.eco.mapper.SysMonthWaterMapper;
import com.huimv.environ.eco.service.ISysDayWaterService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.text.DecimalFormat;
import java.util.*;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author astupidcoder
 * @since 2021-11-12
 */
@Service
public class SysDayWaterServiceImpl extends ServiceImpl<SysDayWaterMapper, SysDayWater> implements ISysDayWaterService {

    @Autowired
    private SysDayWaterMapper dayWaterMapper;

    @Autowired
    private SysMonthWaterMapper monthWaterMapper;

    @Autowired
    private BaseRoomMapper baseRoomMapper;



    @Override
    public Result listWater(Map<String, String> map) {
        String type = map.get("type");
        String startDate = map.get("startDate");
        String endDate = map.get("endDate");
        String roomId = map.get("roomId");
        String farmId = map.get("farmId");
        DecimalFormat df = new DecimalFormat("0.00");
        baseRoomMapper.setS();
        BaseRoom baseRoom ;
        if (roomId == null){
            baseRoom =  baseRoomMapper.selectOne(new QueryWrapper<BaseRoom>().eq("farm_id",farmId).last("limit 1"));
            if (ObjectUtil.isNotEmpty(baseRoom)){
                roomId = baseRoom.getId()+"";
            }
        }
        baseRoom =baseRoomMapper.selectById(roomId);

        if (ObjectUtil.isEmpty(baseRoom)){
            return new Result(ResultCode.SUCCESS,null);
        }

        QueryWrapper<SysDayWater> wrapper = new QueryWrapper<>();
        wrapper.eq(StringUtils.isNotBlank(roomId),"room_id",roomId);


        Map endMap = new HashMap();
        //自定义
        if ("4".equals(type)){
            endDate= endDate+" 23:59:59";
            startDate= startDate+" 00:00:00";
            wrapper.between("create_time", startDate, endDate);
            List<SysDayWater> dayWater = dayWaterMapper.listDay(wrapper);
            endMap.put("data",dayWater);
            Double countWater = dayWaterMapper.countWater(wrapper);
            endMap.put("countWater",countWater+"");
        }
        //本月
        else if ("3".equals(type)) {
            wrapper.ge("create_time", DataUill.getTimesMonthmorning());
            List<SysDayWater> dayWater = dayWaterMapper.listDay(wrapper);
            endMap.put("data",dayWater);


            Double countWater = dayWaterMapper.countWater(wrapper);
            endMap.put("countWater",countWater+"");
        }

        //本周
        else if ("2".equals(type)) {
            wrapper.ge("create_time", DateUtil.beginOfWeek(new Date()));
            List<SysDayWater> dayWater = dayWaterMapper.listDay(wrapper);
            endMap.put("data",dayWater);
            Double countWater = dayWaterMapper.countWater(wrapper);
            endMap.put("countWater",countWater+"");
        }
        //今日
        else  {

            QueryWrapper<SysMonthWater> monthWaterWrapper = new QueryWrapper<>();
            monthWaterWrapper.ge("create_time", DataUill.getTimesmorning());
            monthWaterWrapper.eq("room_id",roomId);
            List<SysMonthWater> monthWaters = monthWaterMapper.selecAllMonthWater(monthWaterWrapper);
            List<SysMonthWater> list= new ArrayList<>();
            endMap.put("countWater",null);
            if (ObjectUtil.isNotEmpty(monthWaters)){
                Double num  =0.0;
                for (SysMonthWater monthWater : monthWaters) {

                    num =  Double.valueOf( monthWater.getValue()) + num;
                }
                String format = df.format(num);
                endMap.put("countWater",format);

            }
            endMap.put("data",monthWaters);

        }
        endMap.put("room",baseRoom.getAlias()+baseRoom.getRoomName());

        wrapper.clear();
        wrapper.eq("farm_id",farmId);
        DayWaterDto dayWaterDto = dayWaterMapper.countMonthAndWeek(wrapper);

        endMap.put("monthWater",dayWaterDto.getLastMonth());
        endMap.put("monthWeekPercent", PercentUtil.getGrowth(dayWaterDto.getLastMonth(),dayWaterDto.getLastTwoMonth()));

        endMap.put("weekWater",dayWaterDto.getLastWeek());
        endMap.put("weekWeekPercent", PercentUtil.getGrowth(dayWaterDto.getLastWeek(),dayWaterDto.getLastTwoWeek()));


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

    @Override
    public Result listAllWater(Map<String, String> map) {
        baseRoomMapper.setS();
        String type = map.get("type");
        String startDate = map.get("startDate");
        String endDate = map.get("endDate");
        String farmId = map.get("farmId");
        DecimalFormat df = new DecimalFormat("0.00");


        QueryWrapper<SysDayWater> wrapper = new QueryWrapper<>();
        wrapper.eq("farm_id",farmId);

        Map endMap = new HashMap();
        //自定义
        if ("4".equals(type)){
            endDate= endDate+" 23:59:59";
            startDate= startDate+" 00:00:00";
            wrapper.between("create_time", startDate, endDate);
            List<SysDayWater> dayWater = dayWaterMapper.listAllWater(wrapper);
            endMap.put("data",dayWater);

            Double countWater = dayWaterMapper.countWater(wrapper);
            if (ObjectUtil.isNotEmpty(countWater)){
                endMap.put("countWater",countWater+"");
            }else {
                endMap.put("countWater",null);
            }

        }
        //本月
        else if ("3".equals(type)) {
            wrapper.ge("create_time", DataUill.getTimesMonthmorning());
            List<SysDayWater> dayWater = dayWaterMapper.listAllWater(wrapper);
            endMap.put("data",dayWater);


            Double countWater = dayWaterMapper.countWater(wrapper);
            endMap.put("countWater",countWater+"");
        }

        //本周
        else if ("2".equals(type)) {
            wrapper.ge("create_time", DateUtil.beginOfWeek(new Date()));
            List<SysDayWater> dayWater = dayWaterMapper.listAllWater(wrapper);
            endMap.put("data",dayWater);
            Double countWater = dayWaterMapper.countWater(wrapper);
            endMap.put("countWater",countWater+"");
        }
        //今日
        else  {

            QueryWrapper<SysMonthWater> monthWaterWrapper = new QueryWrapper<>();
            monthWaterWrapper.ge("create_time", DataUill.getTimesmorning());
            monthWaterWrapper.eq("farm_id",farmId);
            List<SysMonthWater> monthWaters = monthWaterMapper.selecAllMonthWater(monthWaterWrapper);
//            List<SysMonthWater> list= new ArrayList<>();
            endMap.put("countWater",null);
            if (ObjectUtil.isNotEmpty(monthWaters)){
                Double num  =0.0;
                for (SysMonthWater monthWater : monthWaters) {
                   num =  Double.valueOf( monthWater.getValue()) + num;
                }
                String format = df.format(num);
                endMap.put("countWater",format);
            }
            endMap.put("data",monthWaters);

        }



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

    @Override
    public Result listAllWaterAndPower(Integer farmId) {
        WaterAndPower waterAndPower = monthWaterMapper.listAllWaterAndPower(farmId);
        return new Result(ResultCode.SUCCESS,waterAndPower);
    }
}
