package com.ruoyi.bst.vip.service.impl;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import com.github.pagehelper.PageHelper;
import com.ruoyi.bst.areaJoin.domain.enums.AreaJoinPermission;
import com.ruoyi.bst.vip.domain.Vip;
import com.ruoyi.bst.vip.domain.VipQuery;
import com.ruoyi.bst.vip.domain.VipVO;
import com.ruoyi.bst.vip.mapper.VipMapper;
import com.ruoyi.bst.vip.service.VipService;
import com.ruoyi.common.utils.DateUtils;

/**
 * 卡券Service业务层处理
 *
 * @author ruoyi
 * @date 2025-05-23
 */
@Service
public class VipServiceImpl implements VipService
{
    @Autowired
    private VipMapper vipMapper;

    /**
     * 查询卡券
     *
     * @param id 卡券主键
     * @return 卡券
     */
    @Override
    public VipVO selectVipById(Long id, boolean scope)
    {
        if (id == null) {
            return null;
        }
        VipQuery query = new VipQuery();
        query.setId(id);
        query.setScope(scope);
        query.addAreaPermission(AreaJoinPermission.VIP_VIEW.getCode());
        return this.selectOne(query);
    }

    private VipVO selectOne(VipQuery query) {
        PageHelper.startPage(1, 1);
        List<VipVO> list = this.selectVipList(query);
        return CollectionUtils.firstElement(list);
    }

    /**
     * 查询卡券列表
     *
     * @param vip 卡券
     * @return 卡券
     */
    @Override
    public List<VipVO> selectVipList(VipQuery vip)
    {
        return vipMapper.selectVipList(vip);
    }

    /**
     * 新增卡券
     *
     * @param vip 卡券
     * @return 结果
     */
    @Override
    public int insertVip(Vip vip)
    {
        vip.setCreateTime(DateUtils.getNowDate());
        return vipMapper.insertVip(vip);
    }

    /**
     * 修改卡券
     *
     * @param vip 卡券
     * @return 结果
     */
    @Override
    public int updateVip(Vip vip)
    {
        return vipMapper.updateVip(vip);
    }

    /**
     * 逻辑删除卡券
     *
     * @param ids 需要删除的卡券主键集合
     * @return 结果
     */
    @Override
    public int logicDel(List<Long> ids) {
        return vipMapper.logicDel(ids);
    }
}
