package com.bofeng.service.worker;

import com.bofeng.JwtHelper;
import com.bofeng.dao.OrderMapper;
import com.bofeng.dao.RoomMapper;
import com.bofeng.entity.Order;
import com.bofeng.entity.Room;
import com.bofeng.entity.User;
import com.bofeng.service.OrderService;
import com.bofeng.service.UserService;
import com.yvan.platform.Conv;
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 WorkerService {

    @Autowired
    private RoomMapper roomMapper;

    @Autowired
    private UserService userService;

    @Autowired
    private OrderMapper orderMapper;

    @Transactional
    public int enableRoomById(Long roomId) {

        User currentUser = userService.getUserById(Conv.NL(JwtHelper.getUserId()));

        Room currentRoom = roomMapper.selectById(roomId);

        if (currentUser.getPlaceId().equals(currentRoom.getPlaceId())){
            return roomMapper.enableRoomById(roomId) + orderMapper.discardByRoomId(roomId);
        }
        throw new RuntimeException("场地和包厢不匹配");
    }

    @Transactional
    public int useRoomById(Long roomId) {
        User currentUser = userService.getUserById(Conv.NL(JwtHelper.getUserId()));

        Room currentRoom = roomMapper.selectById(roomId);

        if (currentUser.getPlaceId().equals(currentRoom.getPlaceId())){
            return roomMapper.useRoomById(roomId);
        }
        throw new RuntimeException("场地和包厢不匹配");
    }

    public Order getRoomOrderById(Long roomId) {
        return orderMapper.selectByRoomId(roomId);
    }

//    public List<Room> getCurrentRoom(String userId){
//
//    }
}
