package cn.iocoder.yudao.module.member.service.impl;

import cn.iocoder.yudao.framework.common.util.object.BeanUtils;
import cn.iocoder.yudao.module.member.entity.MemberLevel;
import cn.iocoder.yudao.module.member.mapper.MemberLevelMapper;
import cn.iocoder.yudao.module.member.service.MemberLevelService;
import cn.iocoder.yudao.module.member.vo.MemberLevelPageReqVO;
import cn.iocoder.yudao.module.member.vo.MemberLevelRespVO;
import cn.iocoder.yudao.module.member.vo.MemberLevelSaveReqVO;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;
import org.springframework.validation.annotation.Validated;

import javax.annotation.Resource;
import java.util.Comparator;
import java.util.List;
import java.util.stream.Collectors;

/**
 * 会员等级 Service 实现类
 *
 * @author pqz
 */
@Service
@Validated
public class MemberLevelServiceImpl extends ServiceImpl<MemberLevelMapper, MemberLevel> implements MemberLevelService {

    @Resource
    private MemberLevelMapper levelMapper;


    /**
     * 保存会员等级
     *
     * @param saveReqVO MemberLevelSaveReqVO
     * @author PQZ
     * @date 13:39 2024/9/19
     **/
    @Override
    public void saveLevel(MemberLevelSaveReqVO saveReqVO) {
        MemberLevel memberLevel = BeanUtils.toBean(saveReqVO, MemberLevel.class);
        saveOrUpdate(memberLevel);
    }

    /**
     * 删除会员等级
     *
     * @param id 会员等级id
     * @author PQZ
     * @date 13:41 2024/9/19
     **/
    @Override
    public void deleteLevel(String id) {
        // 删除
        levelMapper.deleteById(id);
    }


    /**
     * 根据会员等级id查询会员等级信息
     *
     * @param id 会员等级id
     * @return cn.iocoder.yudao.module.member.entity.MemberLevel
     * @author PQZ
     * @date 13:41 2024/9/19
     **/
    @Override
    public MemberLevel getLevel(String id) {
        return levelMapper.selectById(id);
    }

    /**
     * 分页查询会员等级
     *
     * @param pageReqVO MemberLevelPageReqVO实体
     * @param page
     * @return cn.iocoder.yudao.framework.common.pojo.PageResult<cn.iocoder.yudao.module.member.entity.MemberLevel>
     * @author PQZ
     * @date 13:42 2024/9/19
     **/
    @Override
    public IPage<MemberLevelRespVO> queryListPage(MemberLevelPageReqVO pageReqVO, Page<MemberLevelRespVO> page) {
        return levelMapper.selectListPage(pageReqVO,page);
    }

    @Override
    public List<MemberLevel> queryListAll(MemberLevelPageReqVO pageReqVO) {

        QueryWrapper<MemberLevel> q = new QueryWrapper<>();
        List<MemberLevel> memberLevels = this.levelMapper.selectList(q);
        return memberLevels.stream().sorted(Comparator.comparing(MemberLevel::getSort)).collect(Collectors.toList());
    }


}