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

import java.util.List;

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

import com.github.pagehelper.PageHelper;
import com.ruoyi.bst.areaJoin.domain.enums.AreaJoinPermission;
import com.ruoyi.bst.vipOrder.domain.VipOrder;
import com.ruoyi.bst.vipUser.domain.VipUser;
import com.ruoyi.bst.vipUser.domain.VipUserQuery;
import com.ruoyi.bst.vipUser.domain.VipUserVO;
import com.ruoyi.bst.vipUser.mapper.VipUserMapper;
import com.ruoyi.bst.vipUser.service.VipUserService;
import com.ruoyi.bst.vipUser.utils.VipUserUtil;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.collection.CollectionUtils;

/**
 * 用户卡券Service业务层处理
 *
 * @author ruoyi
 * @date 2025-05-26
 */
@Service
public class VipUserServiceImpl implements VipUserService
{
    @Autowired
    private VipUserMapper vipUserMapper;

    /**
     * 查询用户卡券
     *
     * @param id 用户卡券主键
     * @return 用户卡券
     */
    @Override
    public VipUserVO selectVipUserById(Long id, boolean scope)
    {
        if (id == null) {
            return null;
        }
        VipUserQuery query = new VipUserQuery();
        query.setId(id);
        query.setScope(scope);
        query.addAreaPermission(AreaJoinPermission.VIP_USER_VIEW.getCode());
        return this.selectOne(query);
    }

    /**
     * 查询用户卡券列表
     *
     * @param vipUser 用户卡券
     * @return 用户卡券
     */
    @Override
    public List<VipUserVO> selectVipUserList(VipUserQuery vipUser)
    {
        return vipUserMapper.selectVipUserList(vipUser);
    }

    /**
     * 新增用户卡券
     *
     * @param vipUser 用户卡券
     * @return 结果
     */
    @Override
    public int insertVipUser(VipUser vipUser)
    {
        vipUser.setCreateTime(DateUtils.getNowDate());
        return vipUserMapper.insertVipUser(vipUser);
    }

    /**
     * 修改用户卡券
     *
     * @param vipUser 用户卡券
     * @return 结果
     */
    @Override
    public int updateVipUser(VipUser vipUser)
    {
        return vipUserMapper.updateVipUser(vipUser);
    }

    public VipUserVO selectUserSameVip(VipOrder order) {
        VipUserQuery query = new VipUserQuery();
        query.setUserId(order.getUserId());
        query.setVipId(order.getVipId());
        query.setType(order.getVipType());
        query.setDiscount(order.getVipDiscountValue());
        query.setAreaId(order.getVipAreaId());
        query.setEnableLimit(order.getVipEnableLimit());
        query.setLimitRound(order.getVipLimitRound());
        query.setLimitUnit(order.getVipLimitUnit());
        query.setLimitCount(order.getVipLimitCount());
        query.setMinAmount(order.getVipMinAmount());
        return this.selectOne(query);
    }

    public VipUserVO selectOne(VipUserQuery query) {
        PageHelper.startPage(1, 1);
        List<VipUserVO> list = vipUserMapper.selectVipUserList(query);
        return CollectionUtils.firstElement(list);
    }

    @Override   
    public int renew(Long id, Long days, Integer total) {
        if (id == null || days == null || total == null) {
            return 0;
        } 
        return vipUserMapper.renew(id, days, total);
    }

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

    @Override
    public int recoverVipCount(Long vipUserId, int count) {
        if (vipUserId == null || count <= 0) {
            return 0;
        }
        return vipUserMapper.recoverVipCount(vipUserId, count);
    }

    @Override
    public int deductVipCount(Long id, int count) {
        if (id == null || count <= 0) {
            return 0;
        }
        return vipUserMapper.deductVipCount(id, count);
    }

    @Override
    public int resetVipRoundCount(VipUserVO vipUser) {
        if (vipUser == null || vipUser.getId() == null) {
            return 0;
        }
        VipUser data = new VipUser();
        data.setId(vipUser.getId());
        data.setNextResetTime(VipUserUtil.getNextResetTime(vipUser));
        data.setRoundCount(0);
        return vipUserMapper.updateVipUser(data);
    }


}
