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

import cn.iocoder.yudao.framework.common.pojo.CommonResult;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
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.service.biz.ElderlyChangeRecordService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.validation.Valid;

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

@Tag(name = "管理后台 - 床位变更记录")
@RestController
@RequestMapping("/elderly/bedChangeRecord")
@Validated
public class BedChangeRecordController {

    @Resource
    private ElderlyChangeRecordService changeRecordService;

    @GetMapping("/detail")
    @Operation(summary = "获得床位变更记录详情")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('bed-change:detail')")
    @TenantIgnore
    public CommonResult<BedChangeRecordRespVO> getBedChangeRecord(@RequestParam("id") Long id,
                                                                  @RequestParam("status") Integer status,
                                                                  @RequestParam("isDetail") Boolean isDetail) {
        BedChangeRecordRespVO bedChangeRecord = changeRecordService.detail(id, status, isDetail);
        return success(bedChangeRecord);
    }

    @GetMapping("/findElderlyPage")
    @Operation(summary = "长者信息列表分页")
    @TenantIgnore
    public CommonResult<PageResult<BedChangeElderlyRespVO>> findElderlyPage(@Valid ElderlyInfoPageReqVO pageVO) {
        pageVO.setTenantIds(pageVO.getTenantIds() == null ? new Long[]{TenantContextHolder.getTenantId()} : pageVO.getTenantIds());
        PageResult<BedChangeElderlyRespVO> pageResult = changeRecordService.findElderlyPage(pageVO);
        return success(pageResult);
    }

    @GetMapping("/getElderlyById")
    @Operation(summary = "根据长者ID获取长者信息")
    @Parameter(name = "elderId", description = "长者编号", required = true, example = "1024")
    @TenantIgnore
    public CommonResult<BedChangeElderlyRespVO> getElderlyById(@RequestParam("elderId") Long elderId) {
        BedChangeElderlyRespVO respVO = changeRecordService.getElderlyById(elderId);
        return success(respVO);
    }

    @GetMapping("/checkPrivateRoom")
    @Operation(summary = "校验是否可以包房")
    @Parameter(name = "bedId", description = "床位id", required = true, example = "1024")
    @Parameter(name = "elderId", description = "申请该床位包房的长者id", required = true, example = "1024")
    public CommonResult<Boolean> checkPrivateRoom(@RequestParam("bedId") Long bedId,
                                                  @RequestParam("elderId") Long elderId) {
        Boolean result = changeRecordService.checkPrivateRoom(bedId, elderId);
        return success(result);
    }

    @PostMapping("/swap")
    @Operation(summary = "床位对调")
    @TenantIgnore
    public CommonResult<Boolean> swapBed(@Valid @RequestBody BedSwapReqVO reqVO) {
        changeRecordService.swapBed(reqVO);
        return success(true);
    }

}
