package com.huimv.admin.service.impl;

import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.huimv.admin.common.utils.DataUill;
import com.huimv.admin.common.utils.DateUtil;
import com.huimv.admin.common.utils.Result;
import com.huimv.admin.common.utils.ResultCode;
import com.huimv.admin.entity.FeedingData;
import com.huimv.admin.entity.FeedingEnv;
import com.huimv.admin.entity.vo.FeedDataVo2;
import com.huimv.admin.mapper.FeedingDataMapper;
import com.huimv.admin.mapper.FeedingEnvMapper;
import com.huimv.admin.service.IFeedingDataService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

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

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author author
 * @since 2023-09-19
 */
@Service
public class FeedingDataServiceImpl extends ServiceImpl<FeedingDataMapper, FeedingData> implements IFeedingDataService {

    @Autowired
    private FeedingDataMapper dataMapper;
    @Autowired
    private FeedingEnvMapper envMapper;

    @Override
    public Result listFeed(Map<String, String> paramsMap) {
        String farmId = paramsMap.get("farmId");
        QueryWrapper<FeedingData> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("farm_id", farmId);
        List<FeedDataVo2> dataVo2s = dataMapper.listDayFeed(queryWrapper);
        for (FeedDataVo2 dataVo2 : dataVo2s) {
            Date dateTime = dataVo2.getDateTime();
            SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
            String format = sdf.format(dateTime);
            String start = format + " 00:00:00";
            String end = format + " 23:59:59";
            QueryWrapper<FeedingData> queryWrapper1 = new QueryWrapper<>();
            queryWrapper1.eq("farm_id", farmId).between("upload_time", start, end);
            queryWrapper1.groupBy("device_code");
            List<FeedingData> feedingData = dataMapper.selectList(queryWrapper1);
            dataVo2.setCount(feedingData.size());
        }
        return new Result(ResultCode.SUCCESS, dataVo2s);
    }

    @Override
    public Result listPig(Map<String, String> paramsMap) {
        String farmId = paramsMap.get("farmId");
        QueryWrapper<FeedingEnv> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("farm_id", farmId).eq("device_type", 0);
        Integer integer = envMapper.selectCount(queryWrapper);//哺乳
        QueryWrapper<FeedingEnv> queryWrapper1 = new QueryWrapper<>();
        queryWrapper1.eq("farm_id", farmId).eq("device_type", 1);
        Integer integer1 = envMapper.selectCount(queryWrapper);//配怀

        Date timesmorning = DataUill.getTimesmorning();
        QueryWrapper<FeedingData> queryWrapper2 = new QueryWrapper<>();
        queryWrapper2.eq("farm_id", farmId).ge("upload_time", timesmorning).groupBy("device_code");
        List<FeedingData> feedingData = dataMapper.selectList(queryWrapper2);
        Integer integer2 = feedingData.size();//头数
        JSONObject jsonObject = new JSONObject();
        jsonObject.put("peihuai", 0);
        jsonObject.put("buru", 25);
        jsonObject.put("oneHour", 0);
        jsonObject.put("threeHour", 0);
        jsonObject.put("allHour", 0);
        return new Result(ResultCode.SUCCESS, jsonObject);
    }

    @Override
    public Result listEnv(Map<String, String> paramsMap) {
        String farmId = paramsMap.get("farmId");
        QueryWrapper<FeedingEnv> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("farm_id", farmId).orderByDesc("last_time").groupBy("device_code");
        List<FeedingEnv> feedingEnvs = envMapper.selectList(queryWrapper);
        return new Result(ResultCode.SUCCESS, feedingEnvs);
    }

    @Override
    public Result listMeals(Map<String, String> paramsMap) {
        String farmId = paramsMap.get("farmId");
        QueryWrapper<FeedingData> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("farm_id", farmId).groupBy("device_code").eq("meals", 0);
        List<FeedingData> feedingData = dataMapper.selectList(queryWrapper);
        QueryWrapper<FeedingData> queryWrapper2 = new QueryWrapper<>();
        queryWrapper2.eq("farm_id", farmId).groupBy("device_code").eq("meals", 1);
        List<FeedingData> feedingData2 = dataMapper.selectList(queryWrapper2);
        QueryWrapper<FeedingData> queryWrapper3 = new QueryWrapper<>();
        queryWrapper3.eq("farm_id", farmId).groupBy("device_code").eq("meals", 2);
        List<FeedingData> feedingData3 = dataMapper.selectList(queryWrapper3);
        JSONObject jsonObject = new JSONObject();
        jsonObject.put("first", feedingData.size());
        jsonObject.put("second", feedingData2.size());
        jsonObject.put("third", feedingData3.size());
        jsonObject.put("huai", feedingData.size());
        jsonObject.put("yun", feedingData2.size());
        return new Result(ResultCode.SUCCESS, jsonObject);
    }
}
