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

import com.ruoyi.bst.app.domain.AppVO;

import com.ruoyi.bst.channel.domain.ChannelVO;

import com.ruoyi.bst.device.service.DeviceService;

import com.ruoyi.bst.order.domain.OrderQuery;
import com.ruoyi.bst.order.domain.OrderVO;
import com.ruoyi.bst.order.domain.bo.OrderCreateBO;

import com.ruoyi.bst.order.domain.dto.OrderCreateDTO;

import com.ruoyi.bst.order.domain.vo.OrderPrePriceVO;
import com.ruoyi.bst.order.mapper.OrderMapper;
import com.ruoyi.bst.order.service.OrderValidator;
import com.ruoyi.bst.order.utils.OrderUtil;
import com.ruoyi.bst.storeStaff.domain.enums.StoreStaffPermission;
import com.ruoyi.bst.suit.domain.SuitVO;
import com.ruoyi.bst.suit.domain.enums.SuitStatus;
import com.ruoyi.common.core.domain.vo.UserVO;
import com.ruoyi.common.utils.MathUtils;
import com.ruoyi.common.utils.ServiceUtil;
import com.ruoyi.common.utils.collection.CollectionUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Collections;
import java.util.HashSet;
import java.util.List;
import java.util.Objects;

@Service
@Slf4j
public class OrderValidatorImpl implements OrderValidator{

    @Autowired
    private OrderMapper orderMapper;

    @Override
    public void validate(OrderCreateBO bo) {
        ServiceUtil.assertion(bo == null, "参数错误");

        OrderCreateDTO dto = bo.getDto();
        ServiceUtil.assertion(dto == null, "参数错误");

        // 套餐
        SuitVO suit = bo.getSuit();
        ServiceUtil.assertion(suit == null, "ID为%s的套餐不存在", dto.getSuitId());
        ServiceUtil.assertion(suit.getStatus().equals(SuitStatus.DISABLED.getStatus()), "ID为%s的套餐未启用", dto.getSuitId());

        // 用户
        UserVO user = bo.getUser();
        ServiceUtil.assertion(user == null, "ID为%s的用户不存在", dto.getUserId());

        // 价格
        OrderPrePriceVO prePrice = bo.getPrePrice();
        ServiceUtil.assertion(prePrice == null, "套餐价格次数失败");

        // 是否和前端传输的套餐一致
        ServiceUtil.assertion(!MathUtils.equals(prePrice.getPayAmount(),dto.getPayAmount()), "当前套餐价格已发生变化，请刷新后重新下单");
        ServiceUtil.assertion(!Objects.equals(prePrice.getNumber(),dto.getNumber()), "当前套餐次数已发生变化，请刷新后重新下单");

        // 应用
        AppVO app = bo.getApp();
        ServiceUtil.assertion(app == null, "ID为%s的应用不存在", dto.getAppId());

        // 渠道
        ChannelVO channel = bo.getChannel();
        ServiceUtil.assertion(channel == null, "ID为%s的渠道不存在", dto.getChannelId());
        ServiceUtil.assertion(channel.getEnabled() == null || !channel.getEnabled(), "ID为%s的渠道暂不可用", channel.getChannelId());
        ServiceUtil.assertion(channel.getAppIds() == null || !channel.getAppIds().contains(app.getId()), "ID为%s的渠道不支持ID为%s的应用", channel.getChannelId(), app.getId());

    }

    @Override
    public boolean canPay(OrderVO order, Long userId) {
        return isUser(order, userId);
    }

    @Override
    public boolean canEnd(OrderVO order, Long userId) {
        return isUser(order, userId);
    }

    // 是否是下单用户
    private boolean isUser(OrderVO order, Long userId) {
        return order != null && userId != null && Objects.equals(order.getUserId(), userId);
    }


    @Override
    public boolean canOpenDevice(OrderVO order, Long userId) {
        return isUser(order, userId);
    }

    @Override
    public boolean canCloseDevice(OrderVO order, Long userId) {
        return isUser(order, userId);
    }


    @Override
    public boolean canOperate(Long orderId) {
        return hasPermission(Collections.singletonList(orderId));
    }

    private boolean hasPermission(List<Long> orderIds) {
        if (CollectionUtils.isEmptyElement(orderIds)) {
            return true;
        }
        OrderQuery query = new OrderQuery();
        query.setScope(true);
        query.setOrderIds(orderIds);
        query.addStorePermission(StoreStaffPermission.ORDER_EDIT.getCode());
        List<Long> list = orderMapper.selectIdByQuery(query);
        return new HashSet<>(list).containsAll(orderIds);
    }
}
