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.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.SysFodder;
import com.huimv.environ.eco.entity.dto.FodderDto;
import com.huimv.environ.eco.mapper.BaseFloorMapper;
import com.huimv.environ.eco.mapper.BaseRoomMapper;
import com.huimv.environ.eco.mapper.SysFodderMapper;
import com.huimv.environ.eco.service.ISysFodderService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.*;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author astupidcoder
 * @since 2021-11-15
 */
@Service
public class SysFodderServiceImpl extends ServiceImpl<SysFodderMapper, SysFodder> implements ISysFodderService {

    @Autowired
    private SysFodderMapper fodderMapper;

    @Autowired
    private BaseFloorMapper floorMapper;

    @Autowired
    private BaseRoomMapper baseRoomMapper;




    @Override
    public Result getAllFodder(Map map) {
        String startDate = (String) map.get("startDate");
        String endDate = (String) map.get("endDate");
        Integer type = (Integer) map.get("type");
        Integer farmId = (Integer) map.get("farmId");
        QueryWrapper<SysFodder> wrapper = new QueryWrapper<>();
        wrapper.eq("farm_id",farmId);
        Map endMap = new HashMap();
        baseRoomMapper.setS();
        //自定义
        if ("3".equals(type+"")){
            endDate = endDate +" 23:59:59";
            startDate = startDate+" 00:00:00";
            wrapper.between("create_time",startDate,endDate);

            List<FodderDto> sysFodders = fodderMapper.countAllDayValue(wrapper);
            Integer countAllValue = fodderMapper.countAllValue(wrapper);
            endMap.put("fodderList",sysFodders);
            endMap.put("value",countAllValue);
        }
        //本周
        else if ("1".equals(type+"")){
            wrapper.ge("create_time", DateUtil.beginOfWeek(new Date()));
            List<FodderDto> sysFodders = fodderMapper.countAllDayValue(wrapper);
            Integer countAllValue = fodderMapper.countAllValue(wrapper);
            endMap.put("fodderList",sysFodders);
            endMap.put("value",countAllValue);
        }
        //本月
        else {
            wrapper.ge("create_time", DataUill.getTimesMonthmorning());
            List<FodderDto> sysFodders = fodderMapper.countAllDayValue(wrapper);
            Integer countAllValue = fodderMapper.countAllValue(wrapper);
            endMap.put("fodderList",sysFodders);
            endMap.put("value",countAllValue);
        }
        return new Result(ResultCode.SUCCESS,endMap);
    }

    @Override
    public Result getFodder(Map map) {
        String startDate = (String) map.get("startDate");
        String endDate = (String) map.get("endDate");
        Integer type = (Integer) map.get("type");
        Integer floorId = (Integer)map.get("floorId");
        Integer farmId = (Integer)map.get("farmId");
        baseRoomMapper.setS();
       if (ObjectUtil.isEmpty(floorId)){
           BaseFloor baseFloor = floorMapper.selectOne(new QueryWrapper<BaseFloor>().eq("farm_id", farmId).last("limit 1"));
           if (ObjectUtil.isNotEmpty(baseFloor)){
               floorId = baseFloor.getId();
           }
       }

        QueryWrapper<SysFodder> wrapper = new QueryWrapper<>();
        wrapper.eq("floor_id",floorId);
        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<SysFodder> sysFodders = fodderMapper.countDayValue(wrapper);
            Integer countAllValue = fodderMapper.countAllValue(wrapper);
            endMap.put("fodderList",sysFodders);
            endMap.put("value",countAllValue);
        }
        //本月
        else if ("2".equals(type+"")){
            wrapper.ge("create_time", DateUtil.beginOfMonth(new Date()));
            List<SysFodder> sysFodders = fodderMapper.countDayValue(wrapper);
            Integer countAllValue = fodderMapper.countAllValue(wrapper);
            endMap.put("fodderList",sysFodders);
            endMap.put("value",countAllValue);
        }
        //本周
        else {
            wrapper.ge("create_time", DateUtil.beginOfWeek(new Date()));
            List<SysFodder> sysFodders = fodderMapper.countDayValue(wrapper);
            Integer countAllValue = fodderMapper.countAllValue(wrapper);
            endMap.put("fodderList",sysFodders);
            endMap.put("value",countAllValue);
        }
        return new Result(ResultCode.SUCCESS,endMap);
    }

    @Override
    public Result getTodayAllFodderApp(Map map) {


        Integer  farmId = (Integer)map.get("farmId");

        List<BaseFloor> baseFloors = floorMapper.selectList(new QueryWrapper<BaseFloor>().eq("farm_id", farmId));


        QueryWrapper<SysFodder> wrapper = new QueryWrapper<>();

        List list = new ArrayList();
        for (BaseFloor baseFloor : baseFloors) {
            wrapper.clear();
            wrapper.eq("farm_id",farmId);

            wrapper.ge("create_time", DataUill.getTimesmorning());
            Map endMap = new HashMap();
            wrapper.eq("floor_id",baseFloor.getId());
            Integer value = fodderMapper.countAllValue(wrapper);
            endMap.put("value",value);
            endMap.put("floorName",baseFloor.getFloorName());
            endMap.put("id",baseFloor.getId());
            list.add(endMap);
        }

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


}
