package com.muyu.platform.service.impl;

import java.util.List;

import com.muyu.platform.mapper.WarnRuleMapper;
import org.springframework.stereotype.Service;
import com.muyu.platform.domain.WarnRule;
import com.muyu.platform.service.IWarnRuleService;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.muyu.common.core.utils.StringUtils;
import org.springframework.util.Assert;

/**
 * 预警规则Service业务层处理
 *
 * @author muyu
 * @date 2024-09-20
 */
@Service
public class WarnRuleServiceImpl
        extends ServiceImpl<WarnRuleMapper, WarnRule>
        implements IWarnRuleService {

    /**
     * 精确查询预警规则
     *
     * @param id 预警规则主键
     * @return 预警规则
     */
    @Override
    public WarnRule selectWarnRuleById(Long id)
    {
        LambdaQueryWrapper<WarnRule> queryWrapper = new LambdaQueryWrapper<>();
        Assert.notNull(id, "id不可为空");
        queryWrapper.eq(WarnRule::getId, id);
        return this.getOne(queryWrapper);
    }


    /**
     * 查询预警规则列表
     *
     * @param warnRule 预警规则
     * @return 预警规则
     */
    @Override
    public List<WarnRule> selectWarnRuleList(WarnRule warnRule)
    {
        LambdaQueryWrapper<WarnRule> queryWrapper = new LambdaQueryWrapper<>();
        if (StringUtils.isNotEmpty(warnRule.getRuleName())){
            queryWrapper.like(WarnRule::getRuleName, warnRule.getRuleName());
        }
        if (StringUtils.isNotNull(warnRule.getStrategyId())){
            queryWrapper.eq(WarnRule::getStrategyId, warnRule.getStrategyId());
        }
        if (StringUtils.isNotNull(warnRule.getMsgTypeId())){
            queryWrapper.eq(WarnRule::getMsgTypeId, warnRule.getMsgTypeId());
        }
        if (StringUtils.isNotNull(warnRule.getSlideTime())){
            queryWrapper.eq(WarnRule::getSlideTime, warnRule.getSlideTime());
        }
        if (StringUtils.isNotNull(warnRule.getSlideFrequency())){
            queryWrapper.eq(WarnRule::getSlideFrequency, warnRule.getSlideFrequency());
        }
        if (StringUtils.isNotNull(warnRule.getMaxValue())){
            queryWrapper.eq(WarnRule::getMaxValue, warnRule.getMaxValue());
        }
        if (StringUtils.isNotNull(warnRule.getMinValue())){
            queryWrapper.eq(WarnRule::getMinValue, warnRule.getMinValue());
        }
        return this.list(queryWrapper);
    }

    /**
    * 唯一 判断
    * @param warnRule 预警规则
     * @return 预警规则
    */
    @Override
    public Boolean checkIdUnique(WarnRule warnRule) {
        LambdaQueryWrapper<WarnRule> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(WarnRule::getId, warnRule.getId());
        return this.count(queryWrapper) > 0;
    }

}
