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

import cn.hutool.core.util.ObjectUtil;
import com.ruoyi.common.constant.ServiceConstants;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.ServiceUtil;
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.feeRule.domain.FeeRuleVO;
import com.ruoyi.ss.feeRule.service.IFeeRuleService;
import com.ruoyi.ss.feeRule.service.IFeeRuleValidator;
import com.ruoyi.ss.order.domain.OrderVO;
import com.ruoyi.ss.order.service.IOrderService;
import com.ruoyi.ss.room.domain.BandRuleDTO;
import com.ruoyi.ss.room.domain.RoomVO;
import com.ruoyi.ss.room.mapper.RoomMapper;
import com.ruoyi.ss.room.service.IRoomAssembler;
import com.ruoyi.ss.room.service.IRoomService;
import com.ruoyi.ss.room.service.IRoomValidator;
import com.ruoyi.ss.toilet.domain.ToiletVO;
import com.ruoyi.ss.toilet.service.IToiletValidator;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;
import java.util.Optional;

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;

/**
 * 卫生间Service业务层处理
 *
 * @author qzz
 * @date 2024-11-20
 */
@Service
public class ToiletValidatorImpl implements IToiletValidator
{

    @Autowired
    private IOrderService orderService;

    /**
     * 开卫生间前校验
     *
     * @param toilet 卫生间对象
     * @return 卫生间
     */
    @Override
    public void preOpenToilet(ToiletVO toilet) {
        ServiceUtil.assertion(ObjectUtil.isNull(toilet), "卫生间不存在");
        String unlockCondition = toilet.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(), "无历史订单，不能开门，请先下单！");
        }
    }

}
