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

import com.ruoyi.common.utils.ServiceUtil;
import com.ruoyi.ss.device.domain.vo.DeviceVO;
import com.ruoyi.ss.device.service.DeviceService;
import com.ruoyi.ss.equipment.domain.EquipmentVO;
import com.ruoyi.ss.equipment.service.IEquipmentService;
import com.ruoyi.ss.order.domain.OrderVO;
import com.ruoyi.ss.order.service.IOrderService;
import com.ruoyi.ss.store.domain.StoreVO;
import com.ruoyi.ss.store.service.IStoreService;
import com.ruoyi.ss.store.service.IStoreValidator;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

import static com.ruoyi.common.constant.ServiceConstants.LOCK_CONDITION_CURRENT_ORDER;
import static com.ruoyi.common.constant.ServiceConstants.LOCK_CONDITION_HISTORICAL_ORDER;
import static com.ruoyi.common.utils.SecurityUtils.getUserId;

@Slf4j
@Service
public class StoreValidatorImpl implements IStoreValidator {

    @Autowired
    private IStoreService storeService;

    @Autowired
    private IEquipmentService equipmentService;

    @Autowired
    private IOrderService orderService;

    /**
     * 开大门前的校验
     */
    @Override
    public void preOpenGate(Long storeId, String isVerify) {
        StoreVO storeVO = storeService.selectEStoreByStoreId(storeId);
        ServiceUtil.assertion(storeVO == null, "店铺不存在");
        ServiceUtil.assertion(storeVO.getGateId() == null, "店铺大门未绑定");

        if(!"false".equals(isVerify)){
            String unlockCondition = storeVO.getUnlockCondition(); // 有当前订单可进入
            if(LOCK_CONDITION_CURRENT_ORDER.equals(unlockCondition)){
                // 有当前订单可进入
                List<OrderVO> inProgressOrder = orderService.getInProgressOrderListByUserId(getUserId());
                ServiceUtil.assertion(inProgressOrder.isEmpty(), "当前无订单，不能开门，请先下单！");
            } else if(LOCK_CONDITION_HISTORICAL_ORDER.equals(unlockCondition)){
                // 有历史订单可进入
                List<OrderVO> historicalOrderList = orderService.getHistoricalOrderListByUserId(getUserId());
                ServiceUtil.assertion(historicalOrderList.isEmpty(), "无历史订单，不能开门，请先下单！");
            }
        }
    }
}
