package com.muyu.enterpise.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.muyu.common.core.utils.StringUtils;
import com.muyu.domain.WarnStrategy;
import com.muyu.enterprise.cache.WarnStrategyCacjeService;
import com.muyu.enterpise.mapper.WarnStrategyMapper;
import com.muyu.enterpise.service.IWarnStrategyService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * 预警策略Service业务层处理
 * @author muyu
 * @date 2024-09-20
 */
@Service
public class WarnStrategyServiceImpl
        extends ServiceImpl<WarnStrategyMapper, WarnStrategy>
        implements IWarnStrategyService {

    @Autowired
    private WarnStrategyCacjeService warnStrategyCacjeService;

    /**
     * 精确查询预警策略
     *
     * @param id 预警策略主键
     * @return 预警策略
     */
    @Override
    public WarnStrategy selectWarnStrategyById(Long id)
    {
        LambdaQueryWrapper<WarnStrategy> queryWrapper = new LambdaQueryWrapper<>();
        if(null != id){
            queryWrapper.eq(WarnStrategy::getId, id);
        }
        return this.getOne(queryWrapper);
    }


    /**
     * 查询预警策略列表
     *
     * @param warnStrategy 预警策略
     * @return 预警策略
     */
    @Override
    public List<WarnStrategy> selectWarnStrategyList(WarnStrategy warnStrategy)
    {
        LambdaQueryWrapper<WarnStrategy> queryWrapper = new LambdaQueryWrapper<>();
        if (null!=warnStrategy.getId()){
            queryWrapper.eq(WarnStrategy::getId, warnStrategy.getId());
        }
        if (StringUtils.isNotBlank(warnStrategy.getStrategyName())){
            queryWrapper.like(WarnStrategy::getStrategyName, warnStrategy.getStrategyName());
        }
        if (null!=warnStrategy.getMsgId()){
            queryWrapper.eq(WarnStrategy::getMsgId, warnStrategy.getMsgId());
        }

        List<WarnStrategy> list = this.list(queryWrapper);

        for (WarnStrategy strategy : list) {

            warnStrategyCacjeService.put(String.valueOf(strategy.getId()),strategy);
        }

        return list;
    }

    /**
    * 唯一 判断
    * @param warnStrategy 预警策略
     * @return 预警策略
    */
    @Override
    public Boolean checkIdUnique(WarnStrategy warnStrategy) {
        LambdaQueryWrapper<WarnStrategy> queryWrapper = new LambdaQueryWrapper<>();
        if(null != warnStrategy.getStrategyName()){
            queryWrapper.eq(WarnStrategy::getId, warnStrategy.getId());
        }
        return this.count(queryWrapper) > 0;
    }

}
