package com.muyu.warning.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.github.yulichang.base.MPJBaseServiceImpl;
import com.muyu.common.core.utils.StringUtils;
import com.muyu.warning.domain.WarnRuleDTO;
import com.muyu.warning.mapper.WarnRuleMapper;
import com.muyu.warning.service.IWarnRuleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.Assert;

import java.util.List;

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


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


    /**
     * 查询预警规则列表
     *
     * @param warnRuleDTO 预警规则
     * @return 预警规则
     */
    @Override
    public List<WarnRuleDTO> selectWarnRuleList(WarnRuleDTO warnRuleDTO) {

//        MPJLambdaWrapper<WarnRule> queryWrapper = new MPJLambdaWrapper<WarnRule>()
//                .selectAll(WarnRule.class)
//                .select(WarnStrategy::getStrategyName)
//                .selectAs(WarnStrategy::getStrategyName, WarnRule::getStrategyName);
//        List<WarnRule> list = warnRuleMapper.selectJoinList(WarnRule.class, queryWrapper);
//        return list;
//        MPJLambdaWrapper<WarnRule> wrapper = JoinWrappers.lambda(WarnRule.class)
//                .selectAll(WarnRule.class)
//                .select(WarnStrategy::getStrategyName)
//                .leftJoin(WarnStrategy.class, WarnStrategy::getId, WarnRule::getId);
//        return warnRuleMapper.selectJoinList(WarnRule.class, wrapper);
//        MPJQueryWrapper<WarnRule> queryWrapper = new MPJQueryWrapper<>();
//        if (StringUtils.isNotEmpty(warnRule.getRuleName())) {
////            queryWrapper.like(WarnRule::getRuleName, warnRule.getRuleName());
//            queryWrapper.like("rule_name", warnRule.getRuleName());
//        }
//

        LambdaQueryWrapper<WarnRuleDTO> queryWrapper = new LambdaQueryWrapper<>();
        if (StringUtils.isNotNull(warnRuleDTO.getStrategyId())) {
            queryWrapper.eq(WarnRuleDTO::getStrategyId, warnRuleDTO.getStrategyId());
//            queryWrapper.eq("strategy_id", warnRule.getStrategyId());
        }
        if (StringUtils.isNotNull(warnRuleDTO.getMsgTypeId())) {
            queryWrapper.eq(WarnRuleDTO::getMsgTypeId, warnRuleDTO.getMsgTypeId());
        }
        if (StringUtils.isNotNull(warnRuleDTO.getSlideTime())) {
            queryWrapper.eq(WarnRuleDTO::getSlideTime, warnRuleDTO.getSlideTime());
        }
        if (StringUtils.isNotNull(warnRuleDTO.getSlideFrequency())) {
            queryWrapper.eq(WarnRuleDTO::getSlideFrequency, warnRuleDTO.getSlideFrequency());
        }
        if (StringUtils.isNotNull(warnRuleDTO.getMaxValue())) {
            queryWrapper.eq(WarnRuleDTO::getMaxValue, warnRuleDTO.getMaxValue());
        }
        if (StringUtils.isNotNull(warnRuleDTO.getMinValue())) {
            queryWrapper.eq(WarnRuleDTO::getMinValue, warnRuleDTO.getMinValue());
        }
        return this.list(queryWrapper);


    }

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

}
