package com.xjrsoft.module.room.service.impl;

import cn.dev33.satoken.stp.StpUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.github.yulichang.base.MPJBaseServiceImpl;
import com.xjrsoft.module.room.dto.AddRoomTeacherAppointDto;
import com.xjrsoft.module.room.dto.RoomTeacherAppointPageDto;
import com.xjrsoft.module.room.entity.RoomTeacherAppoint;
import com.xjrsoft.module.room.mapper.RoomTeacherAppointMapper;
import com.xjrsoft.module.room.service.IRoomTeacherAppointService;
import com.xjrsoft.module.room.vo.RoomTeacherAppointPageVo;
import lombok.AllArgsConstructor;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

/**
* @title: 宿管任命
* @Author dzx
* @Date: 2023-12-29
* @Version 1.0
*/
@Service
@AllArgsConstructor
public class RoomTeacherAppointServiceImpl extends MPJBaseServiceImpl<RoomTeacherAppointMapper, RoomTeacherAppoint> implements IRoomTeacherAppointService {
    private final RoomTeacherAppointMapper roomTeacherAppointMapper;
    @Override
    public Page<RoomTeacherAppointPageVo> getPage(Page<RoomTeacherAppointPageDto> page, RoomTeacherAppointPageDto dto) {
        return roomTeacherAppointMapper.getPage(page, dto);
    }

    @Override
    public Boolean clearAppoint(List<Long> ids) {
        LambdaQueryWrapper<RoomTeacherAppoint> wrapper = Wrappers.lambdaQuery(RoomTeacherAppoint.class).in(RoomTeacherAppoint::getRoomId, ids);
        roomTeacherAppointMapper.delete(wrapper);
        return true;
    }

    @Override
    public Boolean saveBatch(AddRoomTeacherAppointDto dto) {
        roomTeacherAppointMapper.delete(new QueryWrapper<RoomTeacherAppoint>().lambda().in(RoomTeacherAppoint::getRoomId, dto.getRoomIds()));
        Long createUserId = StpUtil.getLoginIdAsLong();
        Date createDate = new Date();
        for (Long roomId : dto.getRoomIds()) {
            for (Long userId : dto.getUserIds()) {
                RoomTeacherAppoint appoint = new RoomTeacherAppoint();
                appoint.setUserId(createUserId);
                appoint.setCreateDate(createDate);
                appoint.setDeleteMark(0);
                appoint.setEnabledMark(1);
                appoint.setUserId(userId);
                appoint.setRoomId(roomId);
                roomTeacherAppointMapper.insert(appoint);
            }
        }
        return true;
    }
}
