package com.yd.csf.service.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.yd.csf.service.model.MemberGradeConfig;
import com.yd.csf.service.dao.MemberGradeConfigMapper;
import com.yd.csf.service.service.IMemberGradeConfigService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;

/**
 * <p>
 * 会员等级配置表 服务实现类
 * </p>
 *
 * @author zxm
 * @since 2025-10-15
 */
@Service
public class MemberGradeConfigServiceImpl extends ServiceImpl<MemberGradeConfigMapper, MemberGradeConfig> implements IMemberGradeConfigService {

    /**
     * 根据积分查询范围内的等级
     * @param fycValue
     * @return
     */
    @Override
    public MemberGradeConfig queryOne(BigDecimal fycValue) {
        LambdaQueryWrapper<MemberGradeConfig> wrapper = new LambdaQueryWrapper<>();
        wrapper.le(MemberGradeConfig::getMinValue, fycValue)   // min_value <= fycValue
                .and(w -> w.isNull(MemberGradeConfig::getMaxValue)        // max_value IS NULL
                        .or()
                        .gt(MemberGradeConfig::getMaxValue, fycValue) // 或 fycValue < max_value
                )
                .eq(MemberGradeConfig::getStatus, 1)
                .last(" limit 1");
        return getOne(wrapper);
    }
}
