package com.sdut.labex.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.sdut.labex.entity.Room;
import com.sdut.labex.mapper.RoomMapper;
import com.sdut.labex.service.RoomService;
import com.sdut.labex.utils.ResVo;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author springforest
 * @description 针对表【room(房间)】的数据库操作Service实现
 * @createDate 2023-06-12 08:07:15
 */
@Service
public class RoomServiceImpl extends ServiceImpl<RoomMapper, Room>
        implements RoomService {

    @Resource
    private RoomMapper roomMapper;

    @Override
    public ResVo getAllRoom() {
        List<Room> list = roomMapper.selectList(null);
        Map<String, Object> map = new HashMap<>();
        map.put("list", list);
        return ResVo.ok(map);
    }

    @Override
    public ResVo deleteRoomById(int id) {
        if (roomMapper.selectById(id) == null) {
            return ResVo.error("该房间不存在");
        }

        try {
            roomMapper.deleteById(id);
        } catch (Exception e) {
            e.printStackTrace();
            return ResVo.error("删除失败");
        }
        return ResVo.ok("删除成功");
    }

    @Override
    public ResVo addRoom(Room room) {
        try {
            roomMapper.insert(room);
        } catch (Exception e) {
            e.printStackTrace();
            return ResVo.error("添加失败");
        }

        return ResVo.ok("添加成功");
    }

    @Override
    public ResVo updateRoom(Room room) {
        if (roomMapper.selectById(room.getId()) == null) {
            return ResVo.error("该房间不存在");
        }

        try {
            roomMapper.updateById(room);
        } catch (Exception e) {
            e.printStackTrace();
            return ResVo.error("更新失败");
        }
        return ResVo.ok("更新成功");
    }
}




