package cn.iocoder.yudao.module.system.controller.admin.biz;

import cn.iocoder.yudao.framework.common.pojo.CommonResult;
import cn.iocoder.yudao.framework.excel.core.util.ExcelUtils;
import cn.iocoder.yudao.framework.tenant.core.aop.TenantIgnore;
import cn.iocoder.yudao.framework.tenant.core.context.TenantContextHolder;
import cn.iocoder.yudao.module.system.controller.admin.biz.vo.*;
import cn.iocoder.yudao.module.system.controller.admin.biz.vo.excel.BuildImportExcelVO;
import cn.iocoder.yudao.module.system.controller.admin.biz.vo.excel.BuildScreenExcel;
import cn.iocoder.yudao.module.system.dal.dataobject.biz.BuildDO;
import cn.iocoder.yudao.module.system.dal.dataobject.biz.BuildRoomDO;
import cn.iocoder.yudao.module.system.service.biz.BuildService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import org.apache.commons.compress.utils.IOUtils;
import org.springframework.core.io.ClassPathResource;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.annotation.security.PermitAll;
import javax.servlet.http.HttpServletResponse;
import javax.validation.Valid;

import org.springframework.validation.annotation.Validated;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.util.List;
import java.util.Map;

import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success;


@Tag(name = "管理后台 - 房源配置管理")
@RestController
@RequestMapping("/build")
@Validated
public class BuildController {

    @Resource
    private BuildService buildService;


    @PostMapping("/create")
    @Operation(summary = "创建楼栋楼层")
    @TenantIgnore
    public CommonResult<Long> createBuild(@Valid @RequestBody BuildSaveReqVO createReqVO) {
        createReqVO.setTenantId(createReqVO.getTenantId() == null ? TenantContextHolder.getTenantId() : createReqVO.getTenantId());
        return success(buildService.createBuild(createReqVO));
    }


    @GetMapping("/getList")
    @Operation(summary = "查询楼栋楼层")
    @TenantIgnore
    public CommonResult<List<BuildDO>> getList(@RequestParam(value = "orgType",required = false) Integer orgType,
                                               @RequestParam(value = "tenantIds",required = false) Long[] tenantIds) {
        return success(buildService.getList(orgType,tenantIds));
    }


    @GetMapping("/getBuildById")
    @Operation(summary = "查询楼栋详情")
    @Parameter(name = "id", description = "楼栋ID", required = true, example = "1024")
    @TenantIgnore
    public CommonResult<BuildDO> getBuildById(@RequestParam("id") Long id) {
        return success(buildService.getBuildById(id));
    }


    @PostMapping("/editBuildInfo")
    @Operation(summary = "编辑楼栋信息")
    @TenantIgnore
    public CommonResult<Boolean> editBuildInfo(@RequestBody BuildDO build) {
        buildService.editBuildInfo(build);
        return success(true);
    }


    @DeleteMapping("/deleteBuildById")
    @Operation(summary = "删除楼栋")
    @Parameter(name = "id", description = "楼栋ID", required = true, example = "1024")
    @TenantIgnore
    public CommonResult<Boolean> deleteBuildById(@RequestParam("id") Long id) {
        buildService.deleteBuildById(id);
        return success(true);
    }


    @PostMapping("/createRoom")
    @Operation(summary = "创建房间")
    @TenantIgnore
    public CommonResult<Boolean> createRoom(@RequestBody BuildRoomDO room) {
        room.setTenantId(room.getTenantId() == null ? TenantContextHolder.getTenantId() : room.getTenantId());
        buildService.createRoom(room);
        return success(true);
    }


    @PostMapping("/editRoomInfo")
    @Operation(summary = "编辑房间信息")
    @TenantIgnore
    public CommonResult<Boolean> editRoomInfo(@RequestBody BuildRoomDO room) {
        buildService.editRoomInfo(room);
        return success(true);
    }

    @DeleteMapping("/deleteRoomById")
    @Operation(summary = "删除房间")
    @Parameter(name = "roomId", description = "房间ID", required = true, example = "1024")
    @TenantIgnore
    public CommonResult<Boolean> deleteRoomById(@RequestParam("roomId") Long roomId) {
        buildService.deleteRoomById(roomId);
        return success(true);
    }


    @GetMapping("/getRoomByFloorId")
    @Operation(summary = "根据楼层ID查询房间信息")
    @Parameter(name = "floorId", description = "楼层ID", required = true, example = "1024")
    @TenantIgnore
    public CommonResult<List<BuildRoomDO>> getRoomByFloorId(@RequestParam("floorId") Long floorId) {
        return success(buildService.getRoomByFloorId(floorId));
    }

    @GetMapping("/checkBedById")
    @Operation(summary = "根据ID查询床位是否被使用")
    @TenantIgnore
    public CommonResult<Boolean> checkBedById(@RequestParam("bedId") Long bedId){
        return success(buildService.checkBedById(bedId));
    }

    @GetMapping("/checkBuildFloorById")
    @Operation(summary = "根据ID校验楼层里面是否有床使用" )
    @TenantIgnore
    public CommonResult<Boolean> checkBuildFloorById(@RequestParam("floorId") Long floorId){
        return success(buildService.checkBuildFloorById(floorId));
    }



    @GetMapping("/downloadExcel")
    @Operation(summary = "下载床位导入模板")
    public void downloadFile(HttpServletResponse response) throws IOException {
        String filePath = "files/床位导入模板.xlsx";
        ClassPathResource classPathResource = new ClassPathResource(filePath);
        response.setContentType("application/octet-stream");
        response.setHeader("Content-Disposition", "attachment; filename=床位导入模板");
        IOUtils.copy(classPathResource.getInputStream(), response.getOutputStream());
        response.flushBuffer();
    }

    @PostMapping("/importBuildExcel")
    @Operation(summary = "导入楼栋")
    public CommonResult<Map<String, Object>> importBuildExcel(@RequestBody MultipartFile file) throws Exception {
        List<BuildImportExcelVO> list = ExcelUtils.read(file, BuildImportExcelVO.class);
        return success(buildService.importBuildExcel(list));
    }




    @GetMapping("/buildStatistic")
    @Operation(summary = "统计楼栋数据")
    @Parameter(name = "buildId", description = "楼栋ID", required = true, example = "1024")
    @TenantIgnore
    public CommonResult<Map<String, Object>> buildStatistic(@RequestParam("buildId") Long buildId) {
        return success(buildService.buildStatistic(buildId));
    }

    @GetMapping("/getBedList")
    @Operation(summary = "根据床位名称获取床位列表")
    @TenantIgnore
    public CommonResult<List<BuildBedVO>> getBedList(@RequestParam("bedName") String bedName,
                                                     @RequestParam(value = "status",required = false) Integer status,
                                                     @RequestParam(value = "orgType",required = false) Integer orgType,
                                                     @RequestParam(value = "tenantIds") Long[] tenantIds) {
        return success(buildService.getBedList(bedName,status,orgType,tenantIds));
    }

    @GetMapping("/getBedInfoById")
    @Operation(summary = "根据床位ID查询床位信息")
    @Parameter(name = "bedId", description = "床位ID", required = true, example = "1024")
    @TenantIgnore
    public CommonResult<BuildBedVO> getBedInfoById(@RequestParam("bedId") Long bedId) {
        return success(buildService.getBedInfoById(bedId));
    }


    @GetMapping("/getBuildData")
    @Operation(summary = "获取楼栋床位统计数据")
    @Parameter(name = "buildId", description = "楼栋ID", required = true, example = "1024")
    @TenantIgnore
    public CommonResult<Map<String,Object>> getBuildData(@RequestParam("buildId") Long buildId){
        return success(buildService.getBuildData(buildId));
    }


    @GetMapping("/getBuildFloorData")
    @Operation(summary = "获取楼层床位统计数据")
    @Parameter(name = "floodId", description = "楼层ID", required = true, example = "1024")
    @TenantIgnore
    public CommonResult<BuildFloorDataRespVO> getBuildFloorData(@RequestParam("floodId") Long floodId){
        return success(buildService.getBuildFloorData(floodId));
    }


    @PostMapping("/queryBuildRooms")
    @Operation(summary = "根据条件查询楼栋房间数据")
    @TenantIgnore
    public CommonResult<List<BuildRoomInfoVO>> queryBuildRooms(@RequestBody BuildRoomQueryVO buildRoomQueryVO){
        buildRoomQueryVO.setTenantIds(buildRoomQueryVO.getTenantIds() == null ? new Long[]{TenantContextHolder.getTenantId()} : buildRoomQueryVO.getTenantIds());
        return success(buildService.queryBuildRooms(buildRoomQueryVO));
    }


    @GetMapping("/exportScreenExcel")
    @Operation(summary = "导出显示屏excel")
    public void exportScreenExcel(@RequestParam Long buildId, HttpServletResponse response) throws IOException {
        List<BuildScreenExcel> list = buildService.exportScreenExcel(buildId);
        // 导出 Excel
        ExcelUtils.write(response,"显示屏数据.xls", "显示屏数据", BuildScreenExcel.class, list);
    }

    @GetMapping("/getNurseScreenDataByRoomCode")
    @Operation(summary = "获取护理门口屏数据")
    @PermitAll
    public CommonResult<NurseScreenDataVO> getNurseScreenDataByRoomCode(@RequestParam("roomCode") String roomCode) {
        return success(buildService.getNurseScreenDataByRoomCode(roomCode));
    }


    @GetMapping("/getElderByFloorId")
    @Operation(summary = "根据楼层ID查询长者列表")
    @TenantIgnore
    public CommonResult<List<BuildRoomInfoVO>>  getElderListByFloorId(@RequestParam("floorId") Long floorId) {
        return success(buildService.getElderListByFloorId(floorId));
    }

    @GetMapping("/findAllBuild")
    @Operation(summary = "获取所有楼栋房间数据")
    public CommonResult<List<BuildRespVO>> findAllBuild(){
        return success(buildService.findAllBuild());
    }

    @GetMapping("/getBuildFloorStatistics")
    @Operation(summary = "获取楼栋楼层统计信息")
    @TenantIgnore
    public CommonResult<BuildFloorStatisticsRespVO> getBuildFloorStatistics(
            @RequestParam(value = "tenantIds", required = false) Long[] tenantIds,
            @RequestParam(value = "month") String month) {
        if (tenantIds == null || tenantIds.length == 0) {
            tenantIds = new Long[]{TenantContextHolder.getTenantId()};
        }
        return success(buildService.getBuildFloorStatistics(tenantIds, month));
    }

    @GetMapping("/getFloorTree")
    @Operation(summary = "获取楼栋楼层房间树")
    @TenantIgnore
    @PermitAll
    public CommonResult<List<BuildFloorTreeRespVO>> getFloorTree(
            @RequestParam(value = "tenantId", required = false) Long tenantId) {
        return success(buildService.getFloorTree(tenantId));
    }

    @GetMapping("/getBedListByTenantIdAndKeyword")
    @Operation(summary = "获取某机构未入住床位号")
    @TenantIgnore
    @PermitAll
    public CommonResult<List<CheckInWaitBedSimpleRespVO>> getBedListByTenantIdAndKeyword(
            @RequestParam(value = "tenantId", required = false) Long tenantId,
            @RequestParam(value = "keyWord", required = false) String keyWord) {
        return success(buildService.getBedListByTenantIdAndKeyword(tenantId,keyWord));
    }
}