package com.ics.admin.service.impl.meeting;

import java.util.Date;
import java.util.List;
import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ics.admin.domain.meeting.Reservation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ics.admin.mapper.meeting.RoomRecordMapper;
import com.ics.admin.domain.meeting.RoomRecord;
import com.ics.admin.service.meeting.IRoomRecordService;
import org.springframework.transaction.annotation.Transactional;

/**
 * 用户开门记录Service业务层处理
 * 
 * @author ics
 * @date 2024-03-11
 */
@Service
public class RoomRecordServiceImpl extends ServiceImpl<RoomRecordMapper, RoomRecord> implements IRoomRecordService {
    @Autowired
    private RoomRecordMapper roomRecordMapper;

    /**
     * 查询用户开门记录
     * 
     * @param id 用户开门记录ID
     * @return 用户开门记录
     */
    @Override
    public RoomRecord selectRoomRecordById(Long id) {
        return roomRecordMapper.selectById(id);
    }

    /**
     * 查询用户开门记录列表
     * 
     * @param roomRecord 用户开门记录
     * @return 用户开门记录
     */
    @Override
    public List<RoomRecord> selectRoomRecordList(RoomRecord roomRecord) {
        QueryWrapper queryWrapper = new QueryWrapper();
        queryWrapper.eq("device_id",roomRecord.getDeviceId());
        queryWrapper.orderByDesc("create_time");
        return roomRecordMapper.selectList(queryWrapper);
    }

    /**
     * 新增用户开门记录
     * 
     * @param roomRecord 用户开门记录
     * @return 结果
     */
    @Transactional
    @Override
    public int insertRoomRecord(RoomRecord roomRecord) {
        return roomRecordMapper.insert(roomRecord);
    }

    /**
     * 修改用户开门记录
     * 
     * @param roomRecord 用户开门记录
     * @return 结果
     */
    @Override
    public int updateRoomRecord(RoomRecord roomRecord) {
        return roomRecordMapper.updateById(roomRecord);
    }

    /**
     * 删除用户开门记录对象
     * 
     * @param ids 需要删除的数据ID
     * @return 结果
     */
    @Override
    public int deleteRoomRecordByIds(String ids) {
        String[] idsArray = StrUtil.split(ids,",");
        return roomRecordMapper.deleteBatchIds(CollUtil.toList(idsArray));
    }

    /**
     * 删除用户开门记录信息
     * 
     * @param id 用户开门记录ID
     * @return 结果
     */
    @Override
    public int deleteRoomRecordById(Long id) {
        return roomRecordMapper.deleteRoomRecordById(id);
    }



    @Override
    public IPage<RoomRecord> getOpenDoorRecord(RoomRecord roomRecord, Integer pageNum, Integer pageSize) {
        QueryWrapper<RoomRecord> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("user_id", roomRecord.getUserId());
        queryWrapper.orderByDesc("create_time");
        IPage<RoomRecord> pages = new Page<>(pageNum,pageSize);

        return roomRecordMapper.selectPage(pages,queryWrapper);

    }

    @Override
    public RoomRecord selectTimeByUserId(Date startTime, Date endTime, Long participantId) {

        QueryWrapper<RoomRecord> queryWrapper = new QueryWrapper<>();
        queryWrapper.between("create_time", startTime,endTime);
        queryWrapper.eq("user_id", participantId);
        queryWrapper.orderByDesc("create_time");
        queryWrapper.last("LIMIT 1");
        return roomRecordMapper.selectOne(queryWrapper);
    }
}
