package com.muyu.server.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.github.yulichang.query.MPJLambdaQueryWrapper;
import com.muyu.common.core.utils.StringUtils;
import com.muyu.domain.CarMessage;
import com.muyu.domain.WarnRule;
import com.muyu.domain.resp.CarMessageResp;
import com.muyu.server.mapper.CarMessageMapper;
import com.muyu.server.mapper.WarnRuleMapper;
import com.muyu.server.service.CarMessageService;
import com.muyu.server.service.WarnRuleService;
import lombok.AllArgsConstructor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.Assert;

import java.util.List;

/**
 * 预警规则业务实现层
 * @Author：蓬叁
 * @Package：com.muyu.warn.controller
 * @Project：cloud-server-8
 * @name：WarnLogsController
 * @Date：2024/9/20 下午7:29
 */
@Service
@AllArgsConstructor
public class WarnRuleServiceImpl
        extends ServiceImpl<WarnRuleMapper, WarnRule>
        implements WarnRuleService {

    private final CarMessageService carMessageService;

    /**
     * 精确查询预警规则
     *
     * @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;
    }

    @Override
    public List<CarMessageResp> findByMsg(Long id) {
        return carMessageService.selectJoinList(id);
    }

}
