package com.srs.staff.service.impl;

import java.time.DayOfWeek;
import java.time.LocalDate;
import java.time.format.DateTimeFormatter;
import java.time.format.TextStyle;
import java.util.ArrayList;
import java.util.List;
import java.util.Locale;
import java.util.Objects;

import com.srs.common.exception.ServiceException;
import com.srs.common.utils.DateUtils;
import com.srs.common.utils.SecurityUtils;
import com.srs.staff.domain.StaffOneStopRoom;
import com.srs.staff.domain.StaffOneStopRoomOpeningHours;
import com.srs.staff.mapper.StaffOneStopRoomMapper;
import com.srs.staff.mapper.StaffOneStopRoomOpeningHoursMapper;
import org.springframework.beans.factory.annotation.Autowired;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;
import com.srs.staff.mapper.StaffOneStopRoomReservationMapper;
import com.srs.staff.domain.StaffOneStopRoomReservation;
import com.srs.staff.service.IStaffOneStopRoomReservationService;

/**
 * 一站式社区-功能房-预约记录Service业务层处理
 *
 * @author chc
 * @date 2024-08-12
 */
@Service
public class StaffOneStopRoomReservationServiceImpl extends ServiceImpl<StaffOneStopRoomReservationMapper, StaffOneStopRoomReservation> implements IStaffOneStopRoomReservationService {
    @Autowired
    private StaffOneStopRoomReservationMapper staffOneStopRoomReservationMapper;

    @Autowired
    private StaffOneStopRoomOpeningHoursMapper staffOneStopRoomOpeningHoursMapper;

    @Autowired
    private StaffOneStopRoomMapper staffOneStopRoomMapper;

    /**
     * 查询一站式社区-功能房-预约记录
     *
     * @param rtId 一站式社区-功能房-预约记录主键
     * @return 一站式社区-功能房-预约记录
     */
    @Override
    public StaffOneStopRoomReservation selectStaffOneStopRoomReservationByRtId(Long rtId) {
        return staffOneStopRoomReservationMapper.selectStaffOneStopRoomReservationByRtId(rtId);
    }

    @Override
    public List<StaffOneStopRoomOpeningHours> selectStaffOneStopRoomReservationByRtTime(String roomNo ,String rtTime) {
        DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd");
        LocalDate date = LocalDate.parse(rtTime, formatter);
        // 获取星期几
        DayOfWeek dayOfWeek = date.getDayOfWeek();
        // 将DayOfWeek转换为字符串，使用默认语言环境和全称样式
        String dayOfWeekString = dayOfWeek.getDisplayName(TextStyle.FULL, Locale.getDefault());

        // 获取功能房对应的预约时间段(根据功能房编号，和指定的星期几)
        List<StaffOneStopRoomOpeningHours> staffOneStopRoomOpeningHours = staffOneStopRoomOpeningHoursMapper.selectStaffOneStopRoomOpeningHoursByOhWeekday(roomNo, dayOfWeekString);

        // 获取这个功能房的使用预约记录（根据功能房编号，和指定的时间）
        List<StaffOneStopRoomReservation> roomReservations = staffOneStopRoomReservationMapper.selectStaffOneStopRoomReservationByRtTime(roomNo, rtTime);

        // 遍历预约记录，设置所有被预约的时间
        for (StaffOneStopRoomReservation reservation : roomReservations) {
            // 如果不等于2 证明预约时间有被占用
            if (reservation.getRtState() != 2 && reservation.getAuditStatus() != 2) {
                // 查询对应的开放时间
                StaffOneStopRoomOpeningHours hours = staffOneStopRoomOpeningHoursMapper.selectStaffOneStopRoomOpeningHoursByRtNo(reservation.getRtNo());

                // 遍历开放时间列表，设置已被预约的时间
                for (StaffOneStopRoomOpeningHours roomOpeningHours : staffOneStopRoomOpeningHours) {
                    // 找到对应的预约时间段
                    if (Objects.equals(roomOpeningHours.getOhId(), hours.getOhId())) {
                        // 标记上已经被占用
                        roomOpeningHours.setIsOccupy(1L);
                    }
                }
            }
        }

        return staffOneStopRoomOpeningHours;
    }

    /**
     * 查询一站式社区-功能房-预约记录列表
     *
     * @param staffOneStopRoomReservation 一站式社区-功能房-预约记录
     * @return 一站式社区-功能房-预约记录
     */
    @Override
    public List<StaffOneStopRoomReservation> selectStaffOneStopRoomReservationList(StaffOneStopRoomReservation staffOneStopRoomReservation) {
        return staffOneStopRoomReservationMapper.selectStaffOneStopRoomReservationList(staffOneStopRoomReservation);
    }

    /**
     * 新增一站式社区-功能房-预约记录
     *
     * @param staffOneStopRoomReservation 一站式社区-功能房-预约记录
     * @return 结果
     */
    @Override
    public int insertStaffOneStopRoomReservation(StaffOneStopRoomReservation staffOneStopRoomReservation) {
        staffOneStopRoomReservation.setRtCreatTime(DateUtils.getNowDate());
        staffOneStopRoomReservation.setCreateBy(SecurityUtils.getUsername());
        staffOneStopRoomReservation.setCreateTime(DateUtils.getNowDate());
        if (staffOneStopRoomReservation.getRtCreatRole() == null) {
            staffOneStopRoomReservation.setRtCreatRole(SecurityUtils.getLoginUser().getUser().getNickName());
        }
        StaffOneStopRoom stopRoom = staffOneStopRoomMapper.selectStaffOneStopRoomByRoomNo(staffOneStopRoomReservation.getRoomNo());
        if (!Objects.equals(stopRoom.getRoomStatus(), "1")) {
            throw new ServiceException("功能房暂未开放", 500);
        }
        return staffOneStopRoomReservationMapper.insertStaffOneStopRoomReservation(staffOneStopRoomReservation);
    }

    /**
     * 修改一站式社区-功能房-预约记录
     *
     * @param staffOneStopRoomReservation 一站式社区-功能房-预约记录
     * @return 结果
     */
    @Override
    public int updateStaffOneStopRoomReservation(StaffOneStopRoomReservation staffOneStopRoomReservation) {
        staffOneStopRoomReservation.setUpdateBy(SecurityUtils.getUsername());
        staffOneStopRoomReservation.setRtUpTime(DateUtils.getNowDate());
        List<StaffOneStopRoomOpeningHours> toUpdate = new ArrayList<>(staffOneStopRoomReservation.getStaffOneStopRoomOpeningHoursList());
        // 如果存在需要更新的记录，则执行批量更新
        if (!toUpdate.isEmpty()) {
            staffOneStopRoomOpeningHoursMapper.batchUpdateStaffOneStopRoomOpeningHours(toUpdate);
        }
        return staffOneStopRoomReservationMapper.updateStaffOneStopRoomReservation(staffOneStopRoomReservation);
    }

    /**
     * 批量删除一站式社区-功能房-预约记录
     *
     * @param rtIds 需要删除的一站式社区-功能房-预约记录主键
     * @return 结果
     */
    @Override
    public int deleteStaffOneStopRoomReservationByRtIds(Long[] rtIds) {
        return staffOneStopRoomReservationMapper.deleteStaffOneStopRoomReservationByRtIds(rtIds);
    }

    /**
     * 删除一站式社区-功能房-预约记录信息
     *
     * @param rtId 一站式社区-功能房-预约记录主键
     * @return 结果
     */
    @Override
    public int deleteStaffOneStopRoomReservationByRtId(Long rtId) {
        return staffOneStopRoomReservationMapper.deleteStaffOneStopRoomReservationByRtId(rtId);
    }
}
