package com.ruoyi.base.service.impl;

import java.util.ArrayList;
import java.util.List;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.ruoyi.base.vo.CardRightsVO;
import com.ruoyi.common.core.domain.DlBaseEntity;
import com.ruoyi.common.utils.DateUtils;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.ruoyi.constant.DictConstants;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ruoyi.base.mapper.BaseCardRightsMapper;
import com.ruoyi.base.domain.BaseCardRights;
import com.ruoyi.base.service.IBaseCardRightsService;

/**
 * 会员卡权益Service业务层处理
 * 
 * @author vinjor-m
 * @date 2025-03-17
 */
@Service
public class BaseCardRightsServiceImpl extends ServiceImpl<BaseCardRightsMapper,BaseCardRights>  implements IBaseCardRightsService
{
    @Autowired
    private BaseCardRightsMapper baseCardRightsMapper;

    @Override
    public IPage<BaseCardRights> queryListPage(BaseCardRights pageReqVO, Page<BaseCardRights> page) {
        return baseCardRightsMapper.queryListPage(pageReqVO, page);
    }

    /**
     * 查询会员卡权益
     *
     * @param id       会员卡id
     * @param userType 用户类型（）
     * @return java.util.List<com.ruoyi.base.domain.BaseCardRights>
     * @author vinjor-M
     * @date 15:23 2025/3/18
     **/
    @Override
    public List<CardRightsVO> getByIdAndType(String id, String userType) {
        List<CardRightsVO> list = baseCardRightsMapper.getByIdAndType(id, userType);
        list.forEach(item->{
            item.setCardId(id);
            if(DictConstants.RIGHTS_TYPE_NUMBER.equals(item.getRightsType()) && null==item.getRightsValue()){
                //数量限制
                item.setRightsValue(0);
            }
        });
        return list;
    }

    /**
     * 保存会员卡权益
     *
     * @param cardRightsVOS
     * @author vinjor-M
     * @date 16:09 2025/3/18
     **/
    @Override
    public void saveAndUpdate(List<CardRightsVO> cardRightsVOS) {
        //先删除原会员权益
        baseCardRightsMapper.removeByCardId(cardRightsVOS.get(0).getCardId());
        //保存新权益
        List<BaseCardRights> insertList = new ArrayList<>();
        cardRightsVOS.forEach(item->{
            BaseCardRights cardRights = new BaseCardRights();
            cardRights.setCardId(item.getCardId());
            cardRights.setRightsId(item.getId());
            cardRights.setRightsValue(item.getRightsValue());
            insertList.add(cardRights);
        });
        this.saveBatch(insertList);
    }

    /**
     * 通过会员卡id查询权益
     *
     * @param cardId 会员卡id
     * @return java.util.List<com.ruoyi.base.domain.BaseCardRights>
     * @author PQZ
     * @date 16:31 2025/3/26
     **/
    @Override
    public List<CardRightsVO> queryByCardId(String cardId) {
        return baseCardRightsMapper.queryByCardId(cardId);
    }
}
