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

import cn.hutool.core.date.DateUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.huimv.common.utils.Result;
import com.huimv.common.utils.ResultCode;
import com.huimv.environ.eco.entity.EggData;
import com.huimv.environ.eco.entity.eggvo.EggVo;
import com.huimv.environ.eco.mapper.EggDataMapper;
import com.huimv.environ.eco.service.IEggDataService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import jdk.nashorn.internal.ir.LiteralNode;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

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

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author author
 * @since 2022-06-22
 */
@Service
public class EggDataServiceImpl extends ServiceImpl<EggDataMapper, EggData> implements IEggDataService {

    @Autowired
    private EggDataMapper eggDataMapper;

    @Override
    public Result getAll(Map<String,String> map) {
        String startDate = map.get("startDate");
        String endDate = map.get("endDate");
        String type = map.get("type");
        Date now = new Date();
        DecimalFormat df = new DecimalFormat("0.00");



        QueryWrapper<EggData> wrapper = new QueryWrapper<>();
        //周
        if (type.equals("1")){
            wrapper.ge("upload_time", DateUtil.beginOfWeek(now));

        }
        //月
        else if (type.equals("2")){
            wrapper.ge("upload_time", DateUtil.beginOfWeek(now));
        }
        //自定义
        else if (type.equals("3")){

            wrapper.between("upload_time",startDate,endDate);
        }
        //今日
        else {
            wrapper.ge("upload_time", DateUtil.beginOfWeek(now));
           List<EggVo> list =  eggDataMapper.getAllDay(wrapper);
           double maxFood = 0.0;
           double maxWater = 0.0;

            for (EggVo eggVo : list) {
                maxFood = Math.max(eggVo.getFtDoTalTdy(), maxFood);

                maxWater = Math.max(eggVo.getWtrTdy(), maxWater);
            }


            Map endMap = new HashMap();
            endMap.put("list" ,list);
            endMap.put("maxFood" ,maxFood);
            endMap.put("maxWater" ,maxWater);
            return new Result(ResultCode.SUCCESS,endMap);
        }

        List<EggVo> list =  eggDataMapper.getAllWeek(wrapper);
        double maxFood = 0.0;
        double maxWater = 0.0;

        for (EggVo eggVo : list) {
            maxFood += eggVo.getFtDoTalTdy();
            maxWater += eggVo.getWtrTdy();
        }

        Map endMap = new HashMap();
        endMap.put("list" ,list);
        endMap.put("maxFood" ,maxFood);
        endMap.put("maxWater" ,maxWater);


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

    @Override
    public Result getOneData(Map<String, String> map) {
        String startDate = map.get("startDate");
        String endDate = map.get("endDate");
        String i = map.get("i");
        String type = map.get("type");
        Date now = new Date();
        DecimalFormat df = new DecimalFormat("0.00");
        if (StringUtils.isBlank(i)){
            i ="1";
        }
        QueryWrapper<EggData> wrapper = new QueryWrapper<>();
        wrapper.eq("i",i);
        //周
        if (type.equals("1")){
            wrapper.ge("upload_time", DateUtil.beginOfWeek(now));

        }
        //月
        else if (type.equals("2")){
            wrapper.ge("upload_time", DateUtil.beginOfWeek(now));
        }
        //自定义
        else if (type.equals("3")){

            wrapper.between("upload_time",startDate,endDate);
        }
        //今日
        else {
            wrapper.ge("upload_time", DateUtil.beginOfWeek(now));
            List<EggVo> list =  eggDataMapper.getAllDay(wrapper);
            double maxFood = 0.0;
            double maxWater = 0.0;

            for (EggVo eggVo : list) {
                maxFood = Math.max(eggVo.getFtDoTalTdy(), maxFood);

                maxWater = Math.max(eggVo.getWtrTdy(), maxWater);
            }


            Map endMap = new HashMap();
            endMap.put("list" ,list);
            endMap.put("maxFood" ,df.format(maxFood));
            endMap.put("maxWater" ,df.format(maxWater));
            return new Result(ResultCode.SUCCESS,endMap);
        }

        List<EggVo> list =  eggDataMapper.getAllWeek(wrapper);
        double maxFood = 0.0;
        double maxWater = 0.0;

        for (EggVo eggVo : list) {
            maxFood += eggVo.getFtDoTalTdy();
            maxWater += eggVo.getWtrTdy();
        }

        Map endMap = new HashMap();
        endMap.put("list" ,list);
        endMap.put("maxFood" ,df.format(maxFood));
        endMap.put("maxWater" ,df.format(maxWater));


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

    @Override
    public Result getAllFood(Map<String, String> map) {
        String startDate = map.get("startDate");
        String endDate = map.get("endDate");
        String type = map.get("type");
        Date now = new Date();
        DecimalFormat df = new DecimalFormat("0.00");



        QueryWrapper<EggData> wrapper = new QueryWrapper<>();
        //周
        if (type.equals("1")){
            wrapper.ge("upload_time", DateUtil.beginOfWeek(now));

        }
        //月
        else if (type.equals("2")){
            wrapper.ge("upload_time", DateUtil.beginOfWeek(now));
        }
        //自定义
        else if (type.equals("3")){

            wrapper.between("upload_time",startDate,endDate);
        }
        //今日
        else {
            wrapper.ge("upload_time", DateUtil.beginOfWeek(now));
            List<EggVo> list =  eggDataMapper.getAllDayFood(wrapper);
            double maxFood = 0.0;
            double maxWater = 0.0;

            for (EggVo eggVo : list) {
                maxFood = Math.max(eggVo.getFtDoTalTdy(), maxFood);

                maxWater = Math.max(eggVo.getWtrTdy(), maxWater);
            }


            Map endMap = new HashMap();
            endMap.put("list" ,list);
            endMap.put("maxFood" ,df.format(maxFood));
            endMap.put("maxWater" ,df.format(maxWater));
            return new Result(ResultCode.SUCCESS,endMap);
        }

        List<EggVo> list =  eggDataMapper.getAllWeekFood(wrapper);
        double maxFood = 0.0;
        double maxWater = 0.0;

        for (EggVo eggVo : list) {
            maxFood += eggVo.getFtDoTalTdy();
            maxWater += eggVo.getWtrTdy();
        }
        Set<String> set = new HashSet();
        for (EggVo eggVo : list) {
            set.add(eggVo.getUploadTime());
        }
        List endList = new ArrayList();
        for (String o : set) {
            Map map1 = new HashMap();

            for (EggVo eggVo : list) {
                if (eggVo.getUploadTime().equals(o)){
                    map1.put("value"+eggVo.getI(),eggVo.getFtDoTalTdy());
                }
            }
            map1.put("updateTime",o);
            endList.add(map1);
        }

        Map endMap = new HashMap();
        endMap.put("list" ,endList);

        endMap.put("maxFood" ,df.format(maxFood));
        endMap.put("maxWater" ,df.format(maxWater));


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

    @Override
    public Result getOneFood(Map<String, String> map) {
        String startDate = map.get("startDate");
        String endDate = map.get("endDate");
        String i = map.get("i");
        String type = map.get("type");
        Date now = new Date();
        DecimalFormat df = new DecimalFormat("0.00");
        if (StringUtils.isBlank(i)){
            i ="1";
        }
        QueryWrapper<EggData> wrapper = new QueryWrapper<>();
        wrapper.eq("i",i);
        //周
        if (type.equals("1")){
            wrapper.ge("upload_time", DateUtil.beginOfWeek(now));

        }
        //月
        else if (type.equals("2")){
            wrapper.ge("upload_time", DateUtil.beginOfWeek(now));
        }
        //自定义
        else if (type.equals("3")){

            wrapper.between("upload_time",startDate,endDate);
        }
        //今日
        else {
            wrapper.ge("upload_time", DateUtil.beginOfWeek(now));
            List<EggVo> list =  eggDataMapper.getAllDayFood(wrapper);
            double maxFood = 0.0;
            double maxWater = 0.0;

            for (EggVo eggVo : list) {
                maxFood = Math.max(eggVo.getFtDoTalTdy(), maxFood);

                maxWater = Math.max(eggVo.getWtrTdy(), maxWater);
            }


            Map endMap = new HashMap();
            endMap.put("list" ,list);
            endMap.put("maxFood" ,df.format(maxFood));
            endMap.put("maxWater" ,df.format(maxWater));
            return new Result(ResultCode.SUCCESS,endMap);
        }

        List<EggVo> list =  eggDataMapper.getAllWeekFood(wrapper);
        double maxFood = 0.0;
        double maxWater = 0.0;

        for (EggVo eggVo : list) {
            maxFood += eggVo.getFtDoTalTdy();
            maxWater += eggVo.getWtrTdy();
        }

        Map endMap = new HashMap();
        endMap.put("list" ,list);
        endMap.put("maxFood" ,df.format(maxFood));
        endMap.put("maxWater" ,df.format(maxWater));


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

}
