package com.ruoyi.ss.vipLevel.service.impl;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.support.TransactionTemplate;

import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.ss.vipLevel.domain.VipLevel;
import com.ruoyi.ss.vipLevel.domain.VipLevelQuery;
import com.ruoyi.ss.vipLevel.domain.VipLevelVO;
import com.ruoyi.ss.vipLevel.mapper.VipLevelMapper;
import com.ruoyi.ss.vipLevel.service.VipLevelService;
import com.ruoyi.ss.vipLevel.service.VipLevelValidator;

/**
 * VIP等级定价Service业务层处理
 *
 * @author ruoyi
 * @date 2025-01-16
 */
@Service
public class VipLevelServiceImpl implements VipLevelService
{
    @Autowired
    private VipLevelMapper vipLevelMapper;

    @Autowired
    private VipLevelValidator vipLevelValidator;

    @Autowired
    private TransactionTemplate transactionTemplate;

    /**
     * 查询VIP等级定价
     *
     * @param id VIP等级定价主键
     * @return VIP等级定价
     */
    @Override
    public VipLevelVO selectVipLevelById(Long id)
    {
        return vipLevelMapper.selectVipLevelSkuById(id);
    }

    /**
     * 查询VIP等级定价列表
     *
     * @param vipLevel VIP等级定价
     * @return VIP等级定价
     */
    @Override
    public List<VipLevelVO> selectVipLevelList(VipLevelQuery vipLevel)
    {
        return vipLevelMapper.selectVipLevelSkuList(vipLevel);
    }

    /**
     * 新增VIP等级定价
     *
     * @param vipLevel VIP等级定价
     * @return 结果
     */
    @Override
    public int insertVipLevel(VipLevel vipLevel)
    {
        Integer result = transactionTemplate.execute(status -> { 
            vipLevel.setCreateTime(DateUtils.getNowDate());
            int insert = vipLevelMapper.insertVipLevelSku(vipLevel);

            if (insert == 1) {
                // 后校验
                VipLevelVO vo = vipLevelMapper.selectVipLevelSkuById(vipLevel.getId());
                vipLevelValidator.afterValid(vo);
            }

            return insert;
        });
        
        return result == null ? 0 : result;
    }

    /**
     * 修改VIP等级定价
     *
     * @param vipLevel VIP等级定价
     * @return 结果
     */
    @Override
    public int updateVipLevel(VipLevel vipLevel)
    {
        Integer result = transactionTemplate.execute(status -> {
            vipLevel.setUpdateTime(DateUtils.getNowDate());
            int update = vipLevelMapper.updateVipLevelSku(vipLevel);

            if (update == 1) {
                // 后校验
                VipLevelVO vo = vipLevelMapper.selectVipLevelSkuById(vipLevel.getId());
                vipLevelValidator.afterValid(vo);
            }

            return update;
        });

        return result == null ? 0 : result;
    }

    /**
     * 批量删除VIP等级定价
     *
     * @param ids 需要删除的VIP等级定价主键
     * @return 结果
     */
    @Override
    public int logicDel(List<Long> ids)
    {
        return vipLevelMapper.logicDel(ids);
    }

}
