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.DataUill;
import com.huimv.admin.common.utils.Result;
import com.huimv.admin.common.utils.ResultCode;
import com.huimv.admin.entity.EnergyWarningInfo;
import com.huimv.admin.mapper.EnergyWarningInfoMapper;
import com.huimv.admin.service.IEnergyWarningInfoService;
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.Date;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author author
 * @since 2023-02-14
 */
@Service
public class EnergyWarningInfoServiceImpl extends ServiceImpl<EnergyWarningInfoMapper, EnergyWarningInfo> implements IEnergyWarningInfoService {

    @Autowired
    private EnergyWarningInfoMapper energyWarningInfoMapper;

    @Override
    public Result list(HttpServletRequest httpServletRequest, Map<String, String> paramsMap) {

        String farmId = paramsMap.get("farmId");
        String pageSize = paramsMap.get("pageSize");
        String pageNo = paramsMap.get("pageNo");
        String userIds = paramsMap.get("userIds");
        String type = paramsMap.get("type");

        if (type == null || type == "") {
            type = "2";
        }
        if (pageSize == null || pageSize == "") {
            pageSize = "10";
        }
        if (pageNo == null || pageNo == "") {
            pageNo = "1";
        }

        Date timesmorning = DataUill.getTimesmorning();
        QueryWrapper<EnergyWarningInfo> queryWrapper = new QueryWrapper<>();
        if ("1".equals(userIds)) {
            queryWrapper.eq("farm_id", farmId).orderByDesc("date")
                    .eq("warning_type",type).ge("date",timesmorning);
        } else {
            queryWrapper.eq("farm_id", farmId).like("user_ids", userIds).orderByDesc("date")
                    .eq("warning_type",type).ge("date",timesmorning);
        }
        Page<EnergyWarningInfo> page = new Page<>(Integer.parseInt(pageNo), Integer.parseInt(pageSize));
        Page<EnergyWarningInfo> energyWarningInfoPage = energyWarningInfoMapper.selectPage(page, queryWrapper);
        return new Result(ResultCode.SUCCESS, energyWarningInfoPage);
    }
}
