package com.huimv.admin.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.huimv.admin.common.utils.Result;
import com.huimv.admin.common.utils.ResultCode;
import com.huimv.admin.entity.EnvDeviceOnline;
import com.huimv.admin.mapper.EnvDeviceOnlineMapper;
import com.huimv.admin.service.IEnvDeviceOnlineService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.checkerframework.checker.units.qual.A;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.servlet.http.HttpServletRequest;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.List;
import java.util.Map;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author author
 * @since 2023-03-02
 */
@Service
public class EnvDeviceOnlineServiceImpl extends ServiceImpl<EnvDeviceOnlineMapper, EnvDeviceOnline> implements IEnvDeviceOnlineService {

    @Autowired
    private EnvDeviceOnlineMapper onlineMapper;

    @Override
    public Result listOnline(HttpServletRequest httpServletRequest, Map<String, String> paramsMap) {
        String farmId = paramsMap.get("farmId");
        Calendar calendar = Calendar.getInstance();
        calendar.set(Calendar.DATE, calendar.get(Calendar.DATE) - 7);
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        QueryWrapper<EnvDeviceOnline> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("farm_id", farmId).ge("creat_time", sdf.format(calendar.getTime()));
        List<EnvDeviceOnline> list = onlineMapper.selectList(queryWrapper);
        return new Result(ResultCode.SUCCESS,list);
    }
}
