package com.bofeng.service;

import com.baomidou.mybatisplus.toolkit.IdWorker;
import com.bofeng.dao.PlaceMapper;
import com.bofeng.dao.RoomMapper;
import com.bofeng.entity.Place;
import com.google.common.base.Strings;
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 PlaceService {

    @Autowired
    private PlaceMapper placeMapper;

    @Autowired
    private RoomMapper roomMapper;

    public List<Place> selectAll() {
        return placeMapper.selectAll();
    }

    public List<Place> selectAllPlace() {
        return placeMapper.selectAllPlace();
    }

    @Transactional
    public String insert(Place place) {
        if (Strings.isNullOrEmpty(place.getPlaceName())) {
            return "场次名称不能为空";
        }
        place.setPlaceId(IdWorker.getId());
        int count = placeMapper.checkPlaceNameCount(place);
        if (count > 0) {
            return "场次名称不能重复";
        }
        placeMapper.insert(place);
        return "";
    }

    @Transactional
    public String updateById(Place place) {
        if (Strings.isNullOrEmpty(place.getPlaceName())) {
            return "场次名称不能为空";
        }
        int count = placeMapper.checkPlaceNameCount(place);
        if (count > 0) {
            return "场次名称不能重复";
        }
        placeMapper.updateById(place);
        return "";
    }

    public Place getById(Long placeId) {
        return placeMapper.selectById(placeId);
    }

    public List<Place> getByUserId(Long userId) {
        return placeMapper.selectByUserId(userId);
    }

    @Transactional
    public int deleteById(Long placeId) {
        int effect = 0;
        effect += roomMapper.deleteByPlaceId(placeId);
        effect += placeMapper.deleteById(placeId);
        return effect;
    }
}
