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.common.utils.poi.ExcelUtil;
import com.srs.comprehensive.domain.SrsDormitory;
import com.srs.dormitory.domain.DmsDormitory;
import com.srs.dormitory.domain.Dto.AddDormitoryPhotosDto;
import com.srs.dormitory.domain.Dto.DmsSearch;
import com.srs.dormitory.domain.Dto.DormitoryDto;
import com.srs.dormitory.domain.Dto.ImportDormitoryModel;
import com.srs.dormitory.domain.Vo.DmsFdyStu;
import com.srs.dormitory.domain.Vo.DomInfo;
import com.srs.dormitory.domain.Vo.DormitoryVo;
import com.srs.dormitory.domain.Vo.ExportDormInfo;
import com.srs.dormitory.service.IDmsDormitoryService;
import com.srs.dormitory.utils.DmsOperationLog;
import com.srs.system.service.ISysPostService;
import com.srs.web.controller.common.RoleBool;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 宿舍信息表 前端控制器
 * </p>
 *
 * @author 猪油骨
 * @since 2023-07-14
 */
@Api(tags = "宿舍管理")
@RestController
@RequestMapping("/dormitory/dms-dormitory")
public class DmsDormitoryController extends BaseController {
    @Autowired
    private IDmsDormitoryService dmsDormitoryService;

    @Autowired
    ISysPostService _postService;


    @ApiOperation("导入宿舍")
    @PostMapping("/importDorm")
    public AjaxResult importDorm(@RequestBody List<DomInfo> param){
        boolean hasRole = RoleBool.isHigh(getUserId(), _postService);
        if (hasRole) {
            return dmsDormitoryService.importDorm(param);
        } else {
            return AjaxResult.error(401, "你无权做这些");
        }
    }


    @ApiOperation("统计住宿费用确认")
    @GetMapping("/countRecordConfirmStatus")
    public AjaxResult countRecordConfirmStatus(){
        boolean hasRole = RoleBool.isHigh(getUserId(), _postService);
        if (hasRole) {
            List<DomInfo> list = dmsDormitoryService.countRecordConfirmStatus();
            return AjaxResult.success(list);
        } else {
            return AjaxResult.error(401, "你无权做这些");
        }
    }


    @ApiOperation("统计园区住宿人数相关信息")
    @GetMapping("/countParkStuIn")
    public AjaxResult countParkStuIn(){
        boolean hasRole = RoleBool.isHigh(getUserId(), _postService);
        if (hasRole) {
            List<DomInfo> list = dmsDormitoryService.countParkStuIn();
            return AjaxResult.success(list);
        } else {
            return AjaxResult.error(401, "你无权做这些");
        }
    }


    @ApiOperation("批量开放给新生分配")
    @PostMapping("/manyOpen")
    public AjaxResult manyOpen(@RequestBody List<Long> ids){
        boolean hasRole = RoleBool.isHigh(getUserId(), _postService);
        if (hasRole) {
            return dmsDormitoryService.manyOpen(ids);
        } else {
            return AjaxResult.error(401, "你无权做这些");
        }
    }


    @ApiOperation("将所有未住满学生宿舍开放给新生分配")
    @PostMapping("/openNotFull")
    public AjaxResult openNotFull(){
        boolean hasRole = RoleBool.isHigh(getUserId(), _postService);
        if (hasRole) {
            return dmsDormitoryService.openNotFull();
        } else {
            return AjaxResult.error(401, "你无权做这些");
        }
    }



    @ApiOperation("宿舍信息")
    @GetMapping("/listDormInfo")
    public TableDataInfo listDormInfo(DmsSearch param) {
        boolean hasRole = RoleBool.isHigh(getUserId(), _postService);
        if (hasRole) {
            startPage();
            List<DomInfo> list = dmsDormitoryService.listDormInfo(param);
            return getDataTable(list);
        } else {
            List<String> list = new ArrayList<>();
            return getDataTable(list);
        }
    }


    @ApiOperation("统计性别入住")
    @GetMapping("/countGenderBed")
    public AjaxResult countGenderBed() {
        boolean hasRole = RoleBool.isHigh(getUserId(), _postService);
        if (hasRole) {
            return dmsDormitoryService.countGenderBed();
        } else {
            return AjaxResult.error(401, "你无权做这些");
        }
    }


    @ApiOperation("统计学院入住学生")
    @GetMapping("/countDeptBed")
    public AjaxResult countDeptBed() {
        boolean hasRole = RoleBool.isHigh(getUserId(), _postService);
        if (hasRole) {
            return dmsDormitoryService.countDeptBed();
        } else {
            return AjaxResult.error(401, "你无权做这些");
        }
    }

    @ApiOperation("统计在籍学生和入住学生")
    @GetMapping("/countStuIn")
    public AjaxResult countStuIn() {
        boolean hasRole = RoleBool.isHigh(getUserId(), _postService);
        if (hasRole) {
            return dmsDormitoryService.countStuIn();
        } else {
            return AjaxResult.error(401, "你无权做这些");
        }
    }


    @ApiOperation("释放未在校学生")
    @PostMapping("/cleanNotStu")
    public AjaxResult cleanNotStu() {
        boolean hasRole = RoleBool.isHigh(getUserId(), _postService);
        if (hasRole) {
            return dmsDormitoryService.cleanNotStu();
        } else {
            return AjaxResult.error(401, "你无权做这些");
        }
    }

    @ApiOperation("统计所有园区床位情况")
    @GetMapping("/countAllParkBed")
    public AjaxResult countAllParkBed() {
        boolean hasRole = RoleBool.isHigh(getUserId(), _postService);
        if (hasRole) {
            List<DomInfo> list = dmsDormitoryService.countParkBed(null);
            return AjaxResult.success(list);
        } else {
            return AjaxResult.error(401, "你无权做这些");
        }
    }

    @ApiOperation("导出宿舍床位数情况统计")
    @GetMapping("/exportDept")
    public AjaxResult exportDept(DmsSearch param) {
        boolean hasRole = RoleBool.isHigh(getUserId(), _postService);
        if (hasRole) {
            List<DmsFdyStu> list = dmsDormitoryService.exportDept(param);
            return AjaxResult.success(list);
        } else {
            return AjaxResult.error(401, "你无权做这些");
        }
    }

    @ApiOperation("导出辅导员学生宿舍情况统计表")
    @GetMapping("/exportFdy")
    public AjaxResult exportFdy(DmsSearch param) {
        boolean hasRole = RoleBool.isHigh(getUserId(), _postService);
        if (hasRole) {
            List<DmsFdyStu> list = dmsDormitoryService.exportFdy(param);
            return AjaxResult.success(list);
        } else {
            return AjaxResult.error(401, "你无权做这些");
        }
    }

    @ApiOperation("标记文明宿舍")
    @PostMapping("/signWenming/{id}")
    public AjaxResult signWenming(@PathVariable("id") Long id) {
        return dmsDormitoryService.signWenming(id);
    }


    @ApiOperation("修改宿舍是否是新生分配宿舍")
    @PostMapping("/changeDormIsNew")
    public AjaxResult changeDormIsNew(@RequestBody DmsDormitory param) {
        boolean hasRole = RoleBool.isHigh(getUserId(), _postService);
        if (hasRole) {
            return dmsDormitoryService.changeDormIsNew(param);
        } else {
            return AjaxResult.error(401, "你无权做这些");
        }
    }


    @ApiOperation("修改学生是否是宿舍长")
    @PostMapping("/changeHead/{stuNo}")
    public AjaxResult changeHead(@PathVariable("stuNo") String stuNo) {
        boolean hasRole = RoleBool.isHigh(getUserId(), _postService);
        if (hasRole) {
            return dmsDormitoryService.changeHead(stuNo);
        } else {
            return AjaxResult.error(401, "你无权做这些");
        }
    }

    @ApiOperation("列出宿舍的学生")
    @GetMapping("/listRoomStu/{id}")
    public AjaxResult listRoomStu(@PathVariable("id") Long id) {
        boolean hasRole = RoleBool.isHigh(getUserId(), _postService);
        if (hasRole) {
            DmsSearch param = new DmsSearch();
            param.dormitoryId = id;
            return dmsDormitoryService.listRoomStu(param);
        } else {
            return AjaxResult.error(401, "你无权做这些");
        }
    }


    @ApiOperation("通过楼层列出宿舍的学院居住情况")
    @GetMapping("/listRoomDeptLiveByFloor")
    public AjaxResult listRoomDeptLiveByFloor(DmsSearch param) {
        boolean hasRole = RoleBool.isHigh(getUserId(), _postService);
        if (hasRole) {
            List<DomInfo> list = dmsDormitoryService.listRoomDeptLive(param);
            return AjaxResult.success(list);
        } else {
            return AjaxResult.error(401, "你无权做这些");
        }
    }


    @ApiOperation("列出宿舍的学院居住情况")
    @GetMapping("/listRoomDeptLive")
    public TableDataInfo listRoomDeptLive(DmsSearch param) {
        boolean hasRole = RoleBool.isHigh(getUserId(), _postService);
        if (hasRole) {
            startPage();
            List<DomInfo> list = dmsDormitoryService.listRoomDeptLive(param);
            return getDataTable(list);
        } else {
            List<String> list = new ArrayList<>();
            return getDataTable(list);
        }
    }


    @ApiOperation("列出所有楼栋信息")
    @GetMapping("/listBuildingInfo")
    public AjaxResult listBuildingInfo() {
        return dmsDormitoryService.listBuildingInfo();
    }

    @ApiOperation("通过楼层列出新生房间")
    @GetMapping("/listAllRoomByFloor/{id}")
    public AjaxResult listAllRoomByFloor(@PathVariable("id") Long id) {
        return dmsDormitoryService.listAllRoomByFloor(id);
    }

    @ApiOperation("通过楼层列出新生房间")
    @GetMapping("/listRoomByFloor/{id}")
    public AjaxResult listRoomByFloor(@PathVariable("id") Long id) {
        return dmsDormitoryService.listRoomByFloor(id);
    }

    @ApiOperation("通过楼栋列出楼层")
    @GetMapping("/listFloorByBuilding/{id}")
    public AjaxResult listFloorByBuilding(@PathVariable("id") Long id) {
        return dmsDormitoryService.listFloorByBuilding(id);
    }


    @ApiOperation("通过园区列出楼栋")
    @GetMapping("/listBuildingByPark/{id}")
    public AjaxResult listBuildingByPark(@PathVariable("id") Long id) {
        return dmsDormitoryService.listBuildingByPark(id);
    }


    @ApiOperation("通过校区列出园区")
    @GetMapping("/listParkByCampus/{id}")
    public AjaxResult listParkByCampus(@PathVariable("id") Long id) {
        return dmsDormitoryService.listParkByCampus(id);
    }

    @ApiOperation("列出所有校区")
    @GetMapping("/listAllCampus")
    public AjaxResult listAllCampus() {
        return dmsDormitoryService.listAllCampus();
    }


    @PostMapping("/syncOccupancy")
    public AjaxResult syncOccupancy() {
        return dmsDormitoryService.syncOccupancy();
    }

    @PostMapping("/updateDorm")
    public AjaxResult updateDorm(@RequestBody DmsDormitory param) {
        return dmsDormitoryService.updateDorm(param);
    }


    //    @PreAuthorize("@ss.hasPermi('dormitory:dms-dormitory:add')")
    @DmsOperationLog(title = "宿舍管理", content = "批量添加宿舍")
    @PostMapping("/batch/add")
    public AjaxResult batchAdd(@RequestBody DormitoryDto dto) {
        return dmsDormitoryService.batchAdd(dto);
    }

    //    @PreAuthorize("@ss.hasPermi('dormitory:dms-dormitory:export')")
    @ApiOperation("导出宿舍信息表")
    @PostMapping("/export")
    public void export(HttpServletResponse response, SrsDormitory srsDormitory) {
        List<DormitoryVo> list = dmsDormitoryService.exportDormitoryList(null);
        ExcelUtil<DormitoryVo> util = new ExcelUtil<>(DormitoryVo.class);
        util.exportExcel(response, list, "宿舍信息数据");
    }


    @ApiOperation("导出宿舍的学院居住情况")
    @PostMapping("/exportListRoomDeptLive")
    public void exportListRoomDeptLive(HttpServletResponse response,DmsSearch param) {
        boolean hasRole = RoleBool.isHigh(getUserId(), _postService);
        if (hasRole) {
            List<ExportDormInfo> list = dmsDormitoryService.exportListRoomDeptLive(param);
            ExcelUtil<ExportDormInfo> util = new ExcelUtil<>(ExportDormInfo.class);
            util.exportExcel(response, list, "宿舍详细信息数据");
        }
    }



    //    @PreAuthorize("@ss.hasPermi('dormitory:dms-dormitory:list')")
    @ApiOperation(value = "查看宿舍列表", notes =
            "参数为空则查询全部,可分页查询" +
                    "请求示例:<br>"
                    + "{<br>"
                    + "&nbsp;&nbsp;&nbsp;&nbsp;\"floorId\": 0, // 楼层ID<br>"
                    + "&nbsp;&nbsp;&nbsp;&nbsp;\"buildingId\": 0, // 宿舍楼ID<br>"
                    + "}")
    @PostMapping("/list")
    public TableDataInfo getDormitoryList(@RequestBody Map<String, Object> params) {
        startPage();
        List<DormitoryVo> dormitoryList = dmsDormitoryService.getDormitoryList(params);
        return getDataTable(dormitoryList);
    }

    @PostMapping("/getCanDormList")
    public TableDataInfo getCanDormList(@RequestBody Map<String, Object> params) {
        startPage();
        List<DormitoryVo> dormitoryList = dmsDormitoryService.getCanDormList(params);
        return getDataTable(dormitoryList);
    }

    @GetMapping("/getAllDormList")
    public AjaxResult getAllDormList( DmsSearch params) {
        List<DormitoryVo> dormitoryList = dmsDormitoryService.getAllDormList(params);
        return AjaxResult.success(dormitoryList);
    }


    //    @PreAuthorize("@ss.hasPermi('dormitory:dms-dormitory:query')")
    @ApiOperation("查看宿舍详情")
    @GetMapping("/{id}")
    public AjaxResult getDmsDormitory(@PathVariable Long id) {
        return dmsDormitoryService.getDormitoryInfo(id);
    }

    //    @PreAuthorize("@ss.hasPermi('dormitory:dms-dormitory:add')")
    @ApiOperation(value = "添加宿舍", notes = "id和status不用填写", response = DormitoryDto.class)
    @PostMapping("/add")
    public AjaxResult addDmsDormitory(@RequestBody DormitoryDto dormitoryDto) {
        return dmsDormitoryService.addDmsDormitory(dormitoryDto, getUsername());
    }

    //    @PreAuthorize("@ss.hasPermi('dormitory:dms-dormitory:edit')")
    @ApiOperation(value = "修改宿舍信息", response = DormitoryDto.class)
    @PostMapping("/update")
    public AjaxResult updateDmsDormitory(@RequestBody DormitoryDto dormitoryDto) {
        return dmsDormitoryService.updateDmsDormitory(dormitoryDto, getUsername());
    }

    //    @PreAuthorize("@ss.hasPermi('dormitory:dms-dormitory:remove')")
    @ApiOperation(value = "根据id删除宿舍", notes = "会同时删除床位")
    @PostMapping("/delete/{id}")
    public AjaxResult deleteDmsDormitory(@PathVariable Long id) {
        return dmsDormitoryService.deleteDmsDormitory(id);
    }

    //    @PreAuthorize("@ss.hasPermi('dormitory:dms-dormitory:remove')")
    @ApiOperation("批量清空宿舍照片")
    @PostMapping("/batch/delete/photo")
    public AjaxResult batchDeleteDormitoryPhoto(@RequestBody List<Long> ids) {
        return dmsDormitoryService.batchDeleteDormitoryPhoto(ids);
    }

    //    @PreAuthorize("@ss.hasPermi('dormitory:dms-dormitory:remove')")
    @ApiOperation(value = "批量删除宿舍", notes = "会同时删除宿舍床位")
    @PostMapping("/delete")
    public AjaxResult batchDeleteDmsDormitory(@RequestBody List<Long> ids) {
        return dmsDormitoryService.batchDeleteDmsDormitory(ids);
    }

    //    @PreAuthorize("@ss.hasPermi('dormitory:dms-dormitory:add')")
    @ApiOperation("批量添加宿舍照片")
    @PostMapping("/batch/add/photos")
    public AjaxResult batchAddDormitoryPhoto(@RequestBody AddDormitoryPhotosDto dto) {
        return dmsDormitoryService.batchAddDormitoryPhoto(dto);
    }

    @ApiOperation("数据导入")
    @PostMapping("/import")
    public AjaxResult importDormitory(MultipartFile file) throws Exception {
        ExcelUtil<ImportDormitoryModel> util = new ExcelUtil<>(ImportDormitoryModel.class);
        List<ImportDormitoryModel> list = util.importExcel(file.getInputStream());
        return dmsDormitoryService.importDormitory(list);
    }

    @ApiOperation("查看宿舍学生")
    @GetMapping("/student/{id}")
    public AjaxResult getStudentById(@PathVariable Long id) {
        return dmsDormitoryService.getStudentById(id);
    }
}
