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

import cn.hutool.core.util.ObjectUtil;
import com.ruoyi.common.utils.ServiceUtil;
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.room.service.IRoomService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * 收费模板校验器业务层处理
 *
 * @author qzz
 * @date 2024-11-20
 */
@Service
public class FeeRuleValidatorImpl implements IFeeRuleValidator {

    @Autowired
    private IFeeRuleService feeRuleService;

    @Autowired
    private IRoomService roomService;

    /**
     * 校验是否存在基础收费模板
     *
     * @param ids 收费模板ids
     * @return 收费模板
     */
    @Override
    public boolean isExistBasicsRule(List<Long> ids) {
        if (ids.isEmpty()) {
            return false;
        }
        // 是否存在套餐模式为1的收费模版
        FeeRuleVO feeRuleVO = feeRuleService.isExistBasicsRule(ids);
        return !ObjectUtil.isNull(feeRuleVO);
    }

    @Override
    public boolean isExistRule(List<Long> ids) {
        if (ids.isEmpty()) {
            return false;
        }
        for(Long id: ids){
            FeeRuleVO feeRuleVO = feeRuleService.selectEFeeRuleByRuleId(id);
            ServiceUtil.assertion(feeRuleVO == null, "收费模板["+id+"]不存在");
        }
        return true;
    }

    /**
     * 删除套餐前的校验
     *
     * @param ruleIds 收费模板ids
     */
    @Override
    public void preDelRule(Long[] ruleIds) {
        for (Long ruleId : ruleIds){
            //  是否有应用的房间或设施
            List<String> roomNames = roomService.selectIsExistRule(ruleId);
            ServiceUtil.assertion(roomNames.isEmpty(), "该套餐已经被房间【%s】绑定，请先解绑！", roomNames);
        }
    }
}
