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

import java.math.BigDecimal;
import java.util.Collections;
import java.util.List;

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

import com.github.pagehelper.PageHelper;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.collection.CollectionUtils;
import com.ruoyi.ss.vip.domain.Vip;
import com.ruoyi.ss.vip.domain.VipQuery;
import com.ruoyi.ss.vip.domain.VipVO;
import com.ruoyi.ss.vip.mapper.VipMapper;
import com.ruoyi.ss.vip.service.VipService;
import com.ruoyi.ss.vip.service.VipValidator;

/**
 * 会员Service业务层处理
 *
 * @author ruoyi
 * @date 2024-12-09
 */
@Service
public class VipServiceImpl implements VipService
{
    @Autowired
    private VipMapper vipMapper;

    @Autowired
    private VipValidator vipValidator;
    @Autowired
    private TransactionTemplate transactionTemplate;

    /**
     * 查询会员
     *
     * @param id 会员主键
     * @return 会员
     */
    @Override
    public VipVO selectVipById(Long id)
    {
        return vipMapper.selectVipById(id);
    }

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

    /**
     * 新增会员
     *
     * @param vip 会员
     * @return 结果
     */
    @Override
    public int insertVip(Vip vip) {
        vipValidator.beforeCheck(vip);

        vip.setCreateTime(DateUtils.getNowDate());

        Integer result = transactionTemplate.execute(status -> {
            int insert = vipMapper.insertVip(vip);

            if (insert == 1) {
                VipVO vo = this.selectVipById(vip.getId());
                vipValidator.afterCheck(vo);
            }

            return insert;
        });

        return result == null ? 0 : result;
    }

    /**
     * 修改会员
     *
     * @param vip 会员
     * @return 结果
     */
    @Override
    public int updateVip(Vip vip) {
        vipValidator.beforeCheck(vip);

        Integer result = transactionTemplate.execute(status -> {
            int update = vipMapper.updateVip(vip);

            if (update == 1) {
                VipVO vo = this.selectVipById(vip.getId());
                vipValidator.afterCheck(vo);
            }

            return update;
        });

        return result == null ? 0 : result;
    }

    /**
     * 批量删除会员
     *
     * @param ids 需要删除的会员主键
     * @return 结果
     */
    @Override
    public int deleteVipByIds(Long[] ids) {
        return vipMapper.deleteVipByIds(ids);
    }

    /**
     * 删除会员信息
     *
     * @param id 会员主键
     * @return 结果
     */
    @Override
    public int deleteVipById(Long id)
    {
        return vipMapper.deleteVipById(id);
    }

    @Override
    public int selectCount(VipQuery query) {
        return vipMapper.selectCount(query);
    }

    @Override
    public VipVO selectUserSameVip(Long userId, Long levelId, BigDecimal discount, Long storeId, String limitType, Integer limitCount) {
        VipQuery query = new VipQuery();
        query.setUserId(userId);
        query.setLevelId(levelId);
        query.setDiscount(discount);
        query.setStoreId(storeId);
        query.setLimitType(limitType);
        query.setLimitCount(limitCount);
        query.setInValid(true);
        return selectOne(query);
    }

    @Override
    public int renewal(Long id, Integer days, Integer count) {
        if (id == null || days == null || days < 0) {
            return 0;
        }
        return vipMapper.renewal(id, days, count);
    }

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

    @Override
    public int logicDel(List<Long> ids) {
        if (CollectionUtils.isEmptyElement(ids)) {
            return 0;
        }
        return vipMapper.logicDel(ids);
    }

    @Override
    public List<VipVO> selectVipByIds(List<Long> ids) {
        if (CollectionUtils.isEmptyElement(ids)) {
            return Collections.emptyList();
        }
        VipQuery query = new VipQuery();
        query.setIds(ids);
        return vipMapper.selectVipList(query);
    }

    @Override
    public List<VipVO> selectAvailableList(VipQuery query) {
        if (query == null) {
            return Collections.emptyList();
        }
        query.setInValid(true); // 在有效期内
        query.setSurplusCountStart(BigDecimal.ONE);  // 剩余次数>=1
        query.setInLimit(false);    // 不在限制中
        return vipMapper.selectVipList(query);
    }

    @Override
    public List<VipVO> selectUserAvaliableStoreVip(Long userId, Long storeId) {
        if (userId == null || storeId == null) {
            return Collections.emptyList();
        }
        VipQuery query = new VipQuery();
        query.setUserId(userId);
        query.setStoreId(storeId);
        return selectAvailableList(query);
    }
}

