package com.huimv.admin.service.impl;

import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
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.FeedDataVo;
import com.huimv.admin.mapper.FeedingDataMapper;
import com.huimv.admin.mapper.FeedingEnvMapper;
import com.huimv.admin.service.IFeedingEnvService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.text.DecimalFormat;
import java.util.List;
import java.util.Map;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author author
 * @since 2023-09-19
 */
@Service
public class FeedingEnvServiceImpl extends ServiceImpl<FeedingEnvMapper, FeedingEnv> implements IFeedingEnvService {

    @Autowired
    private FeedingEnvMapper feedingEnvMapper;
    @Autowired
    private FeedingDataMapper feedingDataMapper;

    @Override
    public Result countEnv(Map<String, String> paramsMap) {
        String farmId = paramsMap.get("farmId");
        String type = paramsMap.get("type");
        String eartag = paramsMap.get("eartag");
        String pageNum = paramsMap.get("pageNum");
        String pageSize = paramsMap.get("pageSize");
        String deviceCode = paramsMap.get("deviceCode");
        if ("".equals(pageNum) || null == pageNum) {
            pageNum = "1";
        }
        if ("".equals(pageSize) || null == pageSize) {
            pageSize = "10";
        }
        if ("".equals(type) || null == type) {
            type = "1";
        }

        QueryWrapper<FeedingEnv> queryWrapper1 = new QueryWrapper<>();
        queryWrapper1.eq("farm_id", farmId);
        JSONObject jsonObject = new JSONObject();
        if ("1".equals(type)) {
            QueryWrapper<FeedingEnv> queryWrapper = new QueryWrapper<>();
            queryWrapper.eq("farm_id", farmId);
            queryWrapper.in("device_type", 1, 2, 3);
            List<FeedingEnv> feedingEnvs = feedingEnvMapper.selectList(queryWrapper);
            Integer count = 0;//配怀母猪
            Integer count1 = 0;//怀孕母猪
            Integer count2 = 0;//空怀母猪
            Integer count3 = 0;//设备数量
            for (FeedingEnv feedingEnv : feedingEnvs) {
                if (feedingEnv.getDeviceType() == 1) {
                    count = count + 1;
                }
                if (feedingEnv.getDeviceType() == 2) {
                    count1 = count1 + 1;
                }
                if (feedingEnv.getDeviceType() == 3) {
                    count2 = count2 + 1;
                }
            }
            count3 = count + count1 + count2;
            jsonObject.put("count", count);
            jsonObject.put("count1", count1);
            jsonObject.put("count2", count2);
            jsonObject.put("count3", count3);
            queryWrapper1.like(StringUtils.isNotBlank(eartag), "eartag", eartag);
        } else if ("2".equals(type)) {
            QueryWrapper<FeedingEnv> queryWrapper = new QueryWrapper<>();
            queryWrapper.eq("farm_id", farmId);
            queryWrapper.in("device_type", 0, 4);
            List<FeedingEnv> feedingEnvs = feedingEnvMapper.selectList(queryWrapper);
            Integer count = 0;//
            Integer count1 = 0;//
            Integer count2 = 0;//
            Integer count3 = 0;//设备数量
            for (FeedingEnv feedingEnv : feedingEnvs) {
                if (feedingEnv.getDeviceType() == 0) {
                    count = count + 1;
                    count1 = count1 + 1;
                }
                if (feedingEnv.getDeviceType() == 4) {
                    count2 = count2 + 1;
                }
            }
            count3 = count + count1 + count2;
            jsonObject.put("count", count);
            jsonObject.put("count1", count1);
            jsonObject.put("count2", count2);
            jsonObject.put("count3", count3);
            queryWrapper1.like(StringUtils.isNotBlank(eartag), "eartag", eartag);
        } else if ("3".equals(type)) {
            QueryWrapper<FeedingEnv> queryWrapper = new QueryWrapper<>();
            queryWrapper.eq("farm_id", farmId);
            queryWrapper.in("device_type", 0, 1);
            List<FeedingEnv> feedingEnvs = feedingEnvMapper.selectList(queryWrapper);
            Integer count = 0;//
            Integer count1 = 0;//
            for (FeedingEnv feedingEnv : feedingEnvs) {
                if (feedingEnv.getDeviceType() == 0) {
                    count = count + 1;
                }
                if (feedingEnv.getDeviceType() == 1) {
                    count1 = count1 + 1;
                }
            }
            Integer count2 = count + count1;
            if (count2 != 0) {
                queryWrapper.eq("online_status", 0);
                Integer integer = feedingEnvMapper.selectCount(queryWrapper);
                Double rank = ((double) integer / (double) count2) * 100;
                DecimalFormat def = new DecimalFormat("0.00");
                jsonObject.put("count3", def.format(rank));
            } else {
                jsonObject.put("count3", 0);
            }
            jsonObject.put("count", count2);
            jsonObject.put("count1", count);
            jsonObject.put("count2", count1);
            queryWrapper1.like(StringUtils.isNotBlank(deviceCode), "device_code", deviceCode);
        }

        Page<FeedingEnv> page = new Page<>(Integer.parseInt(pageNum), Integer.parseInt(pageSize));
        Page<FeedingEnv> feedingEnvPage = feedingEnvMapper.selectPage(page, queryWrapper1);
        return new Result(ResultCode.SUCCESS, jsonObject, feedingEnvPage);
    }

    @Override
    public Result listFeed(Map<String, String> paramsMap) {
        String farmId = paramsMap.get("farmId");
        String earTag = paramsMap.get("earTag");
        QueryWrapper<FeedingData> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("farm_id", farmId).eq("eartag", earTag);
        List<FeedDataVo> feedDataVos = feedingDataMapper.listFeed(queryWrapper);
        queryWrapper.orderByDesc(" upload_time").last(" limit 1");
        FeedingData feedingData = feedingDataMapper.selectOne(queryWrapper);
        return new Result(ResultCode.SUCCESS, feedDataVos, feedingData);
    }
}
