package com.foreverwin.mesnac.common.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.foreverwin.mesnac.common.constant.SystemRuleConstant;
import com.foreverwin.mesnac.common.mapper.SystemRuleMapper;
import com.foreverwin.mesnac.common.model.SystemRule;
import com.foreverwin.mesnac.common.service.SystemRuleService;
import com.foreverwin.modular.core.util.CommonMethods;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Service
@Transactional(rollbackFor = Exception.class)
public class SystemRuleServiceImpl extends ServiceImpl<SystemRuleMapper, SystemRule> implements SystemRuleService {

    @Override
    public SystemRule selectOneBySiteNameGroup(String site, String name, String ruleGroup) {
        LambdaQueryWrapper<SystemRule> queryWrapper = new LambdaQueryWrapper();
        queryWrapper.eq(SystemRule::getSite, site);
        queryWrapper.eq(SystemRule::getRuleName, name);
        queryWrapper.eq(SystemRule::getRuleGroup, ruleGroup);
        //----------------------------------------------------------------------------------------------------------------------
        List<SystemRule> systemRules = super.list(queryWrapper);
        return systemRules != null && systemRules.size() > 0 ? systemRules.get(0) : null;
    }

    @Override
    public List<SystemRule> selectListBySiteGroup(String site, String ruleGroup) {
        LambdaQueryWrapper<SystemRule> queryWrapper = new LambdaQueryWrapper();
        queryWrapper.eq(SystemRule::getSite, site);
        queryWrapper.eq(SystemRule::getRuleGroup, ruleGroup);
        //----------------------------------------------------------------------------------------------------------------------
        return super.list(queryWrapper);
    }

    @Override
    public List<SystemRule> selectListBySiteName(String site, String name) {
        LambdaQueryWrapper<SystemRule> queryWrapper = new LambdaQueryWrapper();
        queryWrapper.eq(SystemRule::getSite, site);
        if (name != null && !name.equals("")) queryWrapper.like(SystemRule::getRuleName, name);
        //----------------------------------------------------------------------------------------------------------------------
        return super.list(queryWrapper);
    }
}