package com.srs.staff.service.impl;

import java.util.ArrayList;
import java.util.List;

import com.srs.common.exception.ServiceException;
import com.srs.common.utils.DateUtils;
import com.srs.common.utils.SecurityUtils;
import com.srs.common.utils.uuid.UUID;
import com.srs.staff.domain.StaffOneStopRoomOpeningHours;
import com.srs.staff.domain.StaffOneStopRoomReservation;
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.StaffOneStopRoomMapper;
import com.srs.staff.domain.StaffOneStopRoom;
import com.srs.staff.service.IStaffOneStopRoomService;

/**
 * 一站式社区-功能房Service业务层处理
 *
 * @author chc
 * @date 2024-08-12
 */
@Service
public class StaffOneStopRoomServiceImpl extends ServiceImpl<StaffOneStopRoomMapper, StaffOneStopRoom> implements IStaffOneStopRoomService {
    @Autowired
    private StaffOneStopRoomMapper staffOneStopRoomMapper;

    @Autowired
    private StaffOneStopRoomOpeningHoursMapper staffOneStopRoomOpeningHoursMapper;

    /**
     * 查询一站式社区-功能房
     *
     * @param roomId 一站式社区-功能房主键
     * @return 一站式社区-功能房
     */
    @Override
    public StaffOneStopRoom selectStaffOneStopRoomByRoomId(Long roomId) {
        return staffOneStopRoomMapper.selectStaffOneStopRoomByRoomId(roomId);
    }

    /**
     * 查询一站式社区-功能房列表
     *
     * @param staffOneStopRoom 一站式社区-功能房
     * @return 一站式社区-功能房
     */
    @Override
    public List<StaffOneStopRoom> selectStaffOneStopRoomList(StaffOneStopRoom staffOneStopRoom) {
        return staffOneStopRoomMapper.selectStaffOneStopRoomList(staffOneStopRoom);
    }

    /**
     * 新增一站式社区-功能房
     *
     * @param staffOneStopRoom 一站式社区-功能房
     * @return 结果
     */
    @Override
    public int insertStaffOneStopRoom(StaffOneStopRoom staffOneStopRoom) {
        staffOneStopRoom.setRoomNo(UUID.randomUUID().toString());
        staffOneStopRoom.setRoomCreatetime(DateUtils.getNowDate());
        staffOneStopRoom.setRoomUser(SecurityUtils.getLoginUser().getUser().getNickName());
        staffOneStopRoom.setRoomDepar(SecurityUtils.getLoginUser().getUser().getDept().getDeptName());
        return staffOneStopRoomMapper.insertStaffOneStopRoom(staffOneStopRoom);
    }

    /**
     * 修改一站式社区-功能房
     *
     * @param staffOneStopRoom 一站式社区-功能房
     * @return 结果
     */
    @Override
    public int updateStaffOneStopRoom(StaffOneStopRoom staffOneStopRoom) {
        staffOneStopRoom.setRoomUptime(DateUtils.getNowDate());
        staffOneStopRoom.setRoomUpuser(SecurityUtils.getLoginUser().getUser().getNickName());
        List<StaffOneStopRoomOpeningHours> toUpdate = new ArrayList<>();
        List<StaffOneStopRoomOpeningHours> toInsert = new ArrayList<>();
        List<Long> roomIdsToDelete = new ArrayList<>();

        // 检查并分离更新和插入的列表
        if (staffOneStopRoom.getStaffOneStopRoomOpeningHoursList() != null) { // 通常不需要这个检查，除非有可能为null
            for (StaffOneStopRoomOpeningHours hours : staffOneStopRoom.getStaffOneStopRoomOpeningHoursList()) {
                if (hours.getOhId() != null) {
                    // 如果标记为删除 1，则添加进删除数组
                    if (hours.getIsDel() == 1) {
                        roomIdsToDelete.add(hours.getOhId());
                    } else {
                        toUpdate.add(hours);
                    }
                } else {
                    hours.setRtNo(UUID.randomUUID().toString());
                    toInsert.add(hours);
                }
            }

            // 如果存在需要删除的记录，则执行批量删除
            if (!roomIdsToDelete.isEmpty()) {
                Long[] roomIdsArray = roomIdsToDelete.toArray(new Long[0]);
                staffOneStopRoomOpeningHoursMapper.deleteStaffOneStopRoomOpeningHoursByOhIds(roomIdsArray);
            }

            // 如果存在需要更新的记录，则执行批量更新
            if (!toUpdate.isEmpty()) {
                staffOneStopRoomOpeningHoursMapper.batchUpdateStaffOneStopRoomOpeningHours(toUpdate);
            }

            // 如果存在需要插入的记录，则执行批量插入
            if (!toInsert.isEmpty()) {
                staffOneStopRoomOpeningHoursMapper.insertStaffOneStopRoomOpeningHoursBatch(toInsert);
            }
        }

        // 更新staffOneStopRoom（这通常与上面的批量操作分开处理，因为它们可能属于不同的业务逻辑或事务）
        return staffOneStopRoomMapper.updateStaffOneStopRoom(staffOneStopRoom);
    }

    /**
     * 批量删除一站式社区-功能房
     *
     * @param roomIds 需要删除的一站式社区-功能房主键
     * @return 结果
     */
    @Override
    public int deleteStaffOneStopRoomByRoomIds(Long[] roomIds) {
        // 检查roomIds数组是否为空
        if (roomIds == null || roomIds.length == 0) {
            throw new ServiceException("房间ID数组不能为空", 500);
        }

        int deleteCount = 0;
        for (Long roomId : roomIds) {
            StaffOneStopRoom stopRoom = staffOneStopRoomMapper.selectStaffOneStopRoomByRoomId(roomId);
            if (stopRoom == null) {
                // 如果找不到对应的功能房，可以选择跳过或者记录日志
                continue; // 或者 throw new ServiceException("找不到功能房ID为" + roomId + "的功能房", 404);
            }

            boolean isRoomInUse = false;
            for (StaffOneStopRoomReservation roomReservation : stopRoom.getStaffOneStopRoomReservationList()) {
                if (roomReservation.getRtState() == 1) {
                    isRoomInUse = true;
                    break; // 找到正在使用中的功能房，跳出循环
                }
            }

            if (isRoomInUse) {
                // 如果功能房正在使用中，抛出异常
                throw new ServiceException("编号为" + roomId + "的功能房正在被使用中，不能删除", 500);
            } else {
                // 如果功能房未使用，执行删除操作
                int result = staffOneStopRoomMapper.deleteStaffOneStopRoomByRoomId(roomId);
                if (result > 0) {
                    deleteCount++; // 统计成功删除的数量
                }
            }
        }
        return deleteCount; // 返回成功删除的房间数量
    }

    /**
     * 删除一站式社区-功能房信息
     *
     * @param roomId 一站式社区-功能房主键
     * @return 结果
     */
    @Override
    public int deleteStaffOneStopRoomByRoomId(Long roomId) {
        StaffOneStopRoom stopRoom = staffOneStopRoomMapper.selectStaffOneStopRoomByRoomId(roomId);
        for (StaffOneStopRoomReservation roomReservation : stopRoom.getStaffOneStopRoomReservationList()) {
            if (roomReservation.getRtState() == 1) {
                throw new ServiceException("该功能房正在被使用中，不能删除", 500);
            }
        }
        return staffOneStopRoomMapper.deleteStaffOneStopRoomByRoomId(roomId);
    }
}
