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

import java.time.LocalDateTime;
import java.util.List;
import java.util.Objects;

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

import com.ruoyi.common.utils.ServiceUtil;
import com.ruoyi.common.utils.collection.CollectionUtils;
import com.ruoyi.ss.store.domain.StoreVo;
import com.ruoyi.ss.store.service.StoreValidator;
import com.ruoyi.ss.user.domain.SmUserVO;
import com.ruoyi.ss.user.service.UserValidator;
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.service.VipService;
import com.ruoyi.ss.vip.service.VipValidator;

/**
 * @author wjh
 * 2024/12/9
 */
@Service
public class VipValidatorImpl implements VipValidator {

    @Autowired
    private VipService vipService;

    @Autowired
    private UserValidator userValidator;

    @Autowired
    private StoreValidator storeValidator;

    /**
     * 新增、更新前校验
     *
     * @param data
     */
    @Override
    public void beforeCheck(Vip data) {
        ServiceUtil.assertion(data == null, "参数错误");

    }


    @Override
    public void afterCheck(VipVO vo) {
        if (vo == null) {
            return;
        }

        ServiceUtil.assertion(!Objects.equals(vo.getStoreMchId(), vo.getVipMchId()), "当前VIP等级与店铺商户不一致");
    }

    @Override
    public boolean canView(VipVO vo, Long userId) {
        return vo != null && userId != null && Objects.equals(vo.getStoreMchId(), userId);
    }

    @Override
    public boolean canOperateAll(List<VipVO> vipList, Long userId) {
        if (CollectionUtils.isEmptyElement(vipList) || userId == null) {
            return false;
        }
        return vipList.stream().allMatch(vo -> this.canOperate(vo, userId));
    }

    /**
     * 是否允许操作VIP
     */
    private boolean canOperate(VipVO vo, Long userId) {
        return vo != null && userId != null && Objects.equals(vo.getVipMchId(), userId);
    }

    /**
     * 校验用户是否重复成为店铺会员
     * @param id 会员ID
     * @param userId 用户ID
     */
    private void checkRepeatUserStore(Long id, Long userId) {
        if (userId == null) {
            return;
        }
        VipQuery query = new VipQuery();
        query.setUserId(userId);
        query.setExcludeId(id);
        ServiceUtil.assertion(vipService.selectCount(query) > 0, "当前用户已经是店铺VIP，无法重复绑定");;
    }

    @Override
    public boolean isStore(VipVO vip, StoreVo store) {
        return vip != null && store != null && Objects.equals(vip.getStoreId(), store.getStoreId());
    }

    /**
     * 是否允许使用在当前用户
     *
     * @param vip
     * @param user
     */
    @Override
    public boolean isUser(VipVO vip, SmUserVO user) {
        return vip != null && user != null && Objects.equals(vip.getUserId(), user.getUserId());
    }

    @Override
    public boolean isInValid(VipVO vip) {
        LocalDateTime now = LocalDateTime.now();
        return vip != null
                && vip.getEndTime() != null
                && vip.getStartTime() != null
                && vip.getEndTime().isBefore(now)
                && vip.getStartTime().isAfter(now);
    }
}
