package com.xjrsoft.module.room.controller;

import cn.dev33.satoken.annotation.SaCheckPermission;
import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.xjrsoft.common.model.result.RT;
import com.xjrsoft.common.page.ConventPage;
import com.xjrsoft.common.page.PageOutput;
import com.xjrsoft.module.room.dto.AddRoomTeacherAppointDto;
import com.xjrsoft.module.room.dto.RoomTeacherAppointPageDto;
import com.xjrsoft.module.room.entity.RoomTeacherAppoint;
import com.xjrsoft.module.room.service.IRoomTeacherAppointService;
import com.xjrsoft.module.room.vo.RoomTeacherAppointPageVo;
import com.xjrsoft.module.room.vo.RoomTeacherAppointVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.AllArgsConstructor;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.validation.Valid;
import java.util.List;

/**
* @title: 宿管任命
* @Author dzx
* @Date: 2023-12-29
* @Version 1.0
*/
@RestController
@RequestMapping("/room" + "/roomTeacherAppoint")
@Api(value = "/room"  + "/roomTeacherAppoint",tags = "宿管任命代码")
@AllArgsConstructor
public class RoomTeacherAppointController {


    private final IRoomTeacherAppointService roomTeacherAppointService;

    @GetMapping(value = "/page")
    @ApiOperation(value="宿管任命列表(分页)")
    @SaCheckPermission("roomteacherappoint:detail")
    public RT<PageOutput<RoomTeacherAppointPageVo>> page(@Valid RoomTeacherAppointPageDto dto){

        Page<RoomTeacherAppointPageVo> page = roomTeacherAppointService.getPage(new Page<>(dto.getLimit(), dto.getSize()), dto);
        PageOutput<RoomTeacherAppointPageVo> pageOutput = ConventPage.getPageOutput(page, RoomTeacherAppointPageVo.class);
        return RT.ok(pageOutput);
    }

    @GetMapping(value = "/info")
    @ApiOperation(value="根据id查询宿管任命信息")
    @SaCheckPermission("roomteacherappoint:detail")
    public RT<RoomTeacherAppointVo> info(@RequestParam Long id){
        RoomTeacherAppoint roomTeacherAppoint = roomTeacherAppointService.getById(id);
        if (roomTeacherAppoint == null) {
           return RT.error("找不到此数据！");
        }
        return RT.ok(BeanUtil.toBean(roomTeacherAppoint, RoomTeacherAppointVo.class));
    }


    @PostMapping
    @ApiOperation(value = "新增宿管任命")
    @SaCheckPermission("roomteacherappoint:add")
    public RT<Boolean> add(@Valid @RequestBody AddRoomTeacherAppointDto dto){
        boolean isSuccess = roomTeacherAppointService.saveBatch(dto);
        return RT.ok(isSuccess);
    }

    @DeleteMapping
    @ApiOperation(value = "删除宿管任命")
    @SaCheckPermission("roomteacherappoint:delete")
    public RT<Boolean> delete(@Valid @RequestBody List<Long> ids){
        return RT.ok(roomTeacherAppointService.clearAppoint(ids));

    }
}