package com.huimv.admin.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
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.EnvWarningInfo;
import com.huimv.admin.entity.dto.EnvWarningDto;
import com.huimv.admin.mapper.EnvWarningInfoMapper;
import com.huimv.admin.service.IEnvWarningInfoService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.servlet.http.HttpServletRequest;
import java.util.List;
import java.util.Map;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author author
 * @since 2023-02-13
 */
@Service
public class EnvWarningInfoServiceImpl extends ServiceImpl<EnvWarningInfoMapper, EnvWarningInfo> implements IEnvWarningInfoService {

    @Autowired
    private EnvWarningInfoMapper envWarningInfoMapper;
    @Override
    public Result list(HttpServletRequest httpServletRequest, Map<String, String> paramsMap) {
        String pageSize = paramsMap.get("pageSize");
        String pageNo = paramsMap.get("pageNo");
        String farmId =paramsMap.get("farmId");
        String userIds = paramsMap.get("userIds");
        if (pageSize==null||pageSize=="") {
            pageSize = "10";
        }
        if (pageNo==null||pageNo=="") {
            pageNo = "1";
        }
        QueryWrapper<EnvWarningInfo> queryWrapper = new QueryWrapper<>();
        if ("1".equals(userIds)) {
            queryWrapper.eq("farm_id", farmId).orderByDesc("date");
        } else {
            queryWrapper.eq("farm_id", farmId).like("user_ids",userIds).orderByDesc("date");
        }
        Page<EnvWarningInfo> page = new Page<>(Integer.parseInt(pageNo),Integer.parseInt(pageSize));
        Page<EnvWarningInfo> warningInfoPage = envWarningInfoMapper.selectPage(page, queryWrapper);
        return new Result(ResultCode.SUCCESS,warningInfoPage);
    }

    @Override
    public Result listTop(HttpServletRequest httpServletRequest, Map<String, String> paramsMap) {
        String farmId = paramsMap.get("farmId");
        List<EnvWarningDto> envWarningDtoList = envWarningInfoMapper.listTop(farmId);
        return new Result(ResultCode.SUCCESS,envWarningDtoList);
    }

    @Override
    public Result listScreen(HttpServletRequest httpServletRequest, Map<String, String> paramsMap) {
        String farmId = paramsMap.get("farmId");
        QueryWrapper<EnvWarningInfo> wrapper = new QueryWrapper<>();
        wrapper.eq("farm_id",farmId);
        wrapper.orderByDesc("id");
        wrapper.isNotNull("unit_id");
        wrapper.last("limit 5");
        return new Result(ResultCode.SUCCESS, this.list(wrapper));
    }

    @Override
    public Result listScreenAll(HttpServletRequest httpServletRequest, Map<String, String> paramsMap) {
        String farmId = paramsMap.get("farmId");
        String pageNum = paramsMap.get("pageNum");
        String pageSize = paramsMap.get("pageSize");
        if ("".equals(pageNum )|| null == pageNum) {
            pageNum = "1";
        }
        if ("".equals(pageSize )|| null == pageSize) {
            pageSize = "10";
        }
        QueryWrapper<EnvWarningInfo> wrapper = new QueryWrapper<>();
        wrapper.eq("farm_id",farmId);
        wrapper.orderByDesc("id");
        wrapper.isNotNull("unit_id");
        Page<EnvWarningInfo> page = new Page<>(Integer.parseInt(pageNum), Integer.parseInt(pageSize));
        return new Result(ResultCode.SUCCESS, envWarningInfoMapper.selectPage(page, wrapper));
    }

}
