package com.bofeng.controller.worker;

import com.bofeng.Consts;
import com.bofeng.JwtHelper;
import com.bofeng.entity.Order;
import com.bofeng.entity.Room;
import com.bofeng.entity.User;
import com.bofeng.service.RoomService;
import com.bofeng.service.UserService;
import com.bofeng.service.worker.WorkerService;
import com.yvan.Model;
import com.yvan.ModelOps;
import com.yvan.PageDb;
import com.yvan.mvc.Pd;
import com.yvan.platform.Conv;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.servlet.ModelAndView;

import java.util.List;

@RestController
@RequiresPermissions(Consts.USER_TYPE_WORKER)
public class RoomManageController {

    @Autowired
    private RoomService roomService;

    @Autowired
    private UserService userService;

    @Autowired
    private WorkerService workerService;

    @GetMapping("/worker/room_manage.html")
    public ModelAndView roomManage(){
        return new ModelAndView("/worker/room_manage.ftl");
    }

    @GetMapping("/worker/room")
    public Model<List<Room>> getRoom(PageDb pageDb) {
        User currentUser = userService.getUserById(Conv.NL(JwtHelper.getUserId()));
        return Model.newSuccess(pageDb, roomService.selectByPlaceId(currentUser.getPlaceId(),pageDb));
    }

    @GetMapping("/worker/room_enable.json")
    public ModelOps roomEnable(@Pd(name = "roomId") Long roomId){
        return ModelOps.newSuccess(workerService.enableRoomById(roomId));
    }

    @GetMapping("/worker/room_use.json")
    public ModelOps roomUse(@Pd(name = "roomId") Long roomId){
        return ModelOps.newSuccess(workerService.useRoomById(roomId));
    }

    @GetMapping("/worker/room_order.json")
    public Model<Order> roomOrder(@Pd(name = "roomId") Long roomId){
        Order order = workerService.getRoomOrderById(roomId);
        if (order != null) {
            return Model.newSuccess(order);
        }
        return Model.newFail("订单已经结账或不存在");
    }
}
