package com.srs.web.controller.dormitory;

import com.srs.common.core.controller.BaseController;
import com.srs.common.core.domain.AjaxResult;
import com.srs.common.core.page.TableDataInfo;
import com.srs.dormitory.domain.DmsDormitory;
import com.srs.dormitory.domain.DmsDormitoryDept;
import com.srs.dormitory.domain.Dto.DeptAllocationDto;
import com.srs.dormitory.domain.Dto.DmsSearch;
import com.srs.dormitory.domain.Dto.FastDto;
import com.srs.dormitory.domain.Dto.SelectDormDto;
import com.srs.dormitory.domain.Vo.ExportDmsReg;
import com.srs.dormitory.service.IDmsDormitoryDeptService;
import com.srs.system.service.ISysPostService;
import com.srs.web.controller.common.RoleBool;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.AllArgsConstructor;
import org.apache.ibatis.annotations.Param;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;


/**
 * <p>
 * 学院宿舍关联表 前端控制器
 * </p>
 *
 * @author 猪油骨
 * @since 2023-08-29
 */
@RestController
@RequestMapping("/dormitory/dms-dormitory-dept")
@Api(tags = "管理员分配学院和宿舍")
@AllArgsConstructor
public class DmsDormitoryDeptController extends BaseController {
    private final IDmsDormitoryDeptService dormitoryDeptService;

    @Autowired
    ISysPostService _postService;
    @GetMapping("/exportSum")
    @ApiOperation("导出新生分配计划汇总表")
    public AjaxResult exportSum(){
        boolean hasRole = RoleBool.isHigh(getUserId(), _postService);
        if (hasRole) {
            List<ExportDmsReg> list = dormitoryDeptService.listSum();
            return AjaxResult.success(list);
        } else {
            return AjaxResult.error(401, "你无权做这些");
        }
    }

    @PostMapping("/lockDept")
    @ApiOperation("锁定学院分配")
    public AjaxResult lockDept(@RequestBody DmsDormitoryDept param){
        boolean hasRole = RoleBool.isHigh(getUserId(), _postService);
        if (hasRole) {
            return dormitoryDeptService.lockDept(param);
        } else {
            return AjaxResult.error(401, "你无权做这些");
        }
    }


    @PostMapping("/lockAllDept")
    @ApiOperation("锁定所有学院分配")
    public AjaxResult lockAllDept(){
        boolean hasRole = RoleBool.isHigh(getUserId(), _postService);
        if (hasRole) {
            return dormitoryDeptService.lockAllDept();
        } else {
            return AjaxResult.error(401, "你无权做这些");
        }
    }


    @PostMapping("/cancelDept")
    @ApiOperation("取消学院分配")
    public AjaxResult cancelDept(@RequestBody DmsDormitoryDept param){
        boolean hasRole = RoleBool.isHigh(getUserId(), _postService);
        if (hasRole) {
            return dormitoryDeptService.cancelDept(param);
        } else {
            return AjaxResult.error(401, "你无权做这些");
        }
    }


    @GetMapping("/getDeptDetail/{deptCode}")
    @ApiOperation("获取学院宿舍详情")
    public AjaxResult getDeptDetail(@PathVariable("deptCode") String deptCode){
        boolean hasRole = RoleBool.isHigh(getUserId(), _postService);
        if (hasRole) {
            DmsSearch param = new DmsSearch();
            param.deptCode = deptCode;
            return dormitoryDeptService.deptBuildingCount(param);
        } else {
            return AjaxResult.error(401, "你无权做这些");
        }
    }




    @PostMapping("/doFast")
    @ApiOperation("快捷分配")
    public AjaxResult doFast(@RequestBody FastDto param){
        boolean hasRole = RoleBool.isHigh(getUserId(), _postService);
        if (hasRole) {
            return dormitoryDeptService.doFast(param);
        } else {
            return AjaxResult.error(401, "你无权做这些");
        }
    }


    @GetMapping("/getDeptBedCount")
    @ApiOperation("获取各学院床位数")
    public AjaxResult getDeptBedCount(){
        boolean hasRole = RoleBool.isHigh(getUserId(), _postService);
        if (hasRole) {
            return dormitoryDeptService.getDeptBedCount();
        } else {
            return AjaxResult.error(401, "你无权做这些");
        }
    }




    @PostMapping("/changeDeptBed")
    public AjaxResult changeDeptBed(@RequestBody List<DmsDormitoryDept> param){
        return dormitoryDeptService.changeDeptBed(param);
    }


    /**
     * 管理员分配学院宿舍
     *
     * @param dto 学院宿舍分配DTO
     * @return 操作结果
     */
    @PostMapping("/allocation")
    @ApiOperation("管理员分配学院宿舍")
    public AjaxResult allocation(@Validated @RequestBody DeptAllocationDto dto) {
        return dormitoryDeptService.allocation(dto);
    }

    @PostMapping("/unbind/{dormitoryIds}/{academyCode}")
    @ApiOperation("管理员取消学院与宿舍的绑定")
    public AjaxResult unbind(@PathVariable List<Long> dormitoryIds, @PathVariable List<String> academyCode) {
        return toAjax(dormitoryDeptService.unbind(dormitoryIds, academyCode));
    }

    /**
     * 管理员查看宿舍
     *
     * @param dto 宿舍查询DTO
     * @return 宿舍列表信息
     */
    @GetMapping("/getDormitory")
    @ApiOperation("管理员查看宿舍")
    public TableDataInfo getDormitory(@Validated SelectDormDto dto) {
        return dormitoryDeptService.getDormitory(dto);
    }

    /**
     * 获取学院信息列表
     *
     * @return 学院信息列表，每个元素是一个包含学院代码（XYDM）和学院名称（XY）的 Map 对象
     */
    @ApiOperation("管理员查看学院")
    @GetMapping("/getDept")
    public AjaxResult getDept() {
        return dormitoryDeptService.getDept();
    }

    @ApiOperation("管理员查看校区")
    @GetMapping("/getCampus")
    public AjaxResult getCampus() {
        return dormitoryDeptService.getCampus();
    }

    @ApiOperation("管理员查看园区")
    @GetMapping("/getPark")
    public TableDataInfo getPark(Long campusId) {
        return dormitoryDeptService.getPark(campusId);
    }

    /**
     * 管理员查看宿舍楼信息
     *
     * @return 包含宿舍楼相关信息的数据表格
     */
    @ApiOperation("管理员查看宿舍楼")
    @GetMapping("/getBuilding")
    public TableDataInfo getBuilding(Long parkId) {
        return dormitoryDeptService.getBuilding(parkId);
    }

    /**
     * 根据楼栋ID查询楼层信息
     *
     * @param buildingId 楼栋ID
     * @return 楼层信息表格数据
     */
    @GetMapping("/getFloor")
    @ApiOperation("根据楼栋ID查看楼层")
    public TableDataInfo getFloor(@RequestParam Long buildingId) {
        return dormitoryDeptService.getFloor(buildingId);
    }

    @GetMapping("/viewTeacher")
    @ApiOperation("根据学院id查看老师")
    public TableDataInfo viewTeacher() {
        return dormitoryDeptService.viewTeacher();
    }

    @ApiOperation("分配宿舍给辅导员")
    @PostMapping("/assignTeachers")
    public AjaxResult assignTeachers(@Param("dormId") Long dormId, @Param("employeeId") Long employeeId) {
        return dormitoryDeptService.assignTeachers(dormId, employeeId);
    }

    @ApiOperation("根据学院id查询宿舍")
    @GetMapping("/viewDormitory")
    public TableDataInfo viewDormitory() {
        return dormitoryDeptService.viewDormitory();
    }





}
