package com.bofeng.service;

import com.baomidou.mybatisplus.toolkit.IdWorker;
import com.bofeng.dao.RoomMapper;
import com.bofeng.entity.Room;
import com.google.common.base.Strings;
import com.yvan.PageDb;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Service
@Transactional(readOnly = true)
public class RoomService {

    @Autowired
    private RoomMapper roomMapper;


    public List<Room> selectAll(PageDb pageDb) {
        return roomMapper.selectAll(pageDb);
    }

    public List<Room> selectByPlaceId(Long placeId,PageDb pageDb) {
        return roomMapper.selectByPlaceId(placeId,pageDb);
    }

    @Transactional
    public String updateById(Room room) {
        if (Strings.isNullOrEmpty(room.getRoomName())) {
            return "包厢名称不能为空";
        }
        int count = roomMapper.checkRoomNameCount(room);
        if (count > 0) {
            return "包厢名称不能重复";
        }
        roomMapper.updateById(room);
        return "";
    }

    @Transactional
    public String insert(Room room) {
        if (Strings.isNullOrEmpty(room.getRoomName())) {
            return "包厢名称不能为空";
        }
        room.setRoomId(IdWorker.getId());
        int count = roomMapper.checkRoomNameCount(room);
        if (count > 0) {
            return "包厢名称不能重复";
        }
        roomMapper.insert(room);
        return "";
    }

    public Room getByRoomId(long roomId) {
        return roomMapper.selectById(roomId);
    }

    @Transactional
    public int deleteById(Long roomId) {
        return roomMapper.deleteById(roomId);
    }
}
