package com.ruoyi.system.service.impl;

import java.util.List;

import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.utils.DateUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.system.mapper.ConferenceRoomOrderMapper;
import com.ruoyi.system.domain.ConferenceRoomOrder;
import com.ruoyi.system.service.IConferenceRoomOrderService;

/**
 * 会议室预约Service业务层处理
 *
 * @author ruoyi
 * @date 2024-07-23
 */
@Service
public class ConferenceRoomOrderServiceImpl implements IConferenceRoomOrderService {
    @Autowired
    private ConferenceRoomOrderMapper conferenceRoomOrderMapper;

    /**
     * 查询会议室预约
     *
     * @param conferenceRoomOrderId 会议室预约主键
     * @return 会议室预约
     */
    @Override
    public ConferenceRoomOrder selectConferenceRoomOrderByConferenceRoomOrderId(Long conferenceRoomOrderId) {
        return conferenceRoomOrderMapper.selectConferenceRoomOrderByConferenceRoomOrderId(conferenceRoomOrderId);
    }

    /**
     * 查询会议室预约列表
     *
     * @param conferenceRoomOrder 会议室预约
     * @return 会议室预约
     */
    @Override
    public List<ConferenceRoomOrder> selectConferenceRoomOrderList(ConferenceRoomOrder conferenceRoomOrder) {
        return conferenceRoomOrderMapper.selectConferenceRoomOrderList(conferenceRoomOrder);
    }

    /**
     * 新增会议室预约
     *
     * @param conferenceRoomOrder 会议室预约
     * @return 结果
     */
    @Override
    public AjaxResult insertConferenceRoomOrder(ConferenceRoomOrder conferenceRoomOrder) {
        //判断当前时间段是否有预约记录
        ConferenceRoomOrder conferenceRoomOrderNew  = new  ConferenceRoomOrder();
        //会议室id
        conferenceRoomOrderNew.setConferenceRoomId(conferenceRoomOrder.getConferenceRoomId());
        conferenceRoomOrderNew.setStartTime(conferenceRoomOrder.getStartTime());
        List<ConferenceRoomOrder> conferenceRoomOrders = conferenceRoomOrderMapper.selectConferenceRoomOrderByStartTime(conferenceRoomOrderNew);
        if (conferenceRoomOrders != null && conferenceRoomOrders.size() > 0){
            return AjaxResult.error("当前已经被预约");
        }
        conferenceRoomOrder.setCreateTime(DateUtils.getNowDate());
        int rows = conferenceRoomOrderMapper.insertConferenceRoomOrder(conferenceRoomOrder);
        return rows > 0 ? AjaxResult.success() : AjaxResult.error();
    }

    /**
     * 修改会议室预约
     *
     * @param conferenceRoomOrder 会议室预约
     * @return 结果
     */
    @Override
    public AjaxResult updateConferenceRoomOrder(ConferenceRoomOrder conferenceRoomOrder) {
        //判断当前时间段是否有预约记录
        ConferenceRoomOrder conferenceRoomOrderNew  = new  ConferenceRoomOrder();
        //会议室id
        conferenceRoomOrderNew.setConferenceRoomId(conferenceRoomOrder.getConferenceRoomId());
        conferenceRoomOrderNew.setStartTime(conferenceRoomOrder.getStartTime());
        List<ConferenceRoomOrder> conferenceRoomOrders = conferenceRoomOrderMapper.selectConferenceRoomOrderByStartTime(conferenceRoomOrderNew);
        if (conferenceRoomOrders != null && conferenceRoomOrders.size() > 0){
            return AjaxResult.error("当前已经被预约");
        }
        conferenceRoomOrder.setUpdateTime(DateUtils.getNowDate());
        int rows = conferenceRoomOrderMapper.updateConferenceRoomOrder(conferenceRoomOrder);
        return rows > 0 ? AjaxResult.success() : AjaxResult.error();
    }

    /**
     * 批量删除会议室预约
     *
     * @param conferenceRoomOrderIds 需要删除的会议室预约主键
     * @return 结果
     */
    @Override
    public int deleteConferenceRoomOrderByConferenceRoomOrderIds(Long[] conferenceRoomOrderIds) {
        return conferenceRoomOrderMapper.deleteConferenceRoomOrderByConferenceRoomOrderIds(conferenceRoomOrderIds);
    }

    /**
     * 删除会议室预约信息
     *
     * @param conferenceRoomOrderId 会议室预约主键
     * @return 结果
     */
    @Override
    public int deleteConferenceRoomOrderByConferenceRoomOrderId(Long conferenceRoomOrderId) {
        return conferenceRoomOrderMapper.deleteConferenceRoomOrderByConferenceRoomOrderId(conferenceRoomOrderId);
    }
}
