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

import cn.iocoder.yudao.framework.apilog.core.annotation.ApiAccessLog;
import cn.iocoder.yudao.framework.common.pojo.CommonResult;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;
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.reservation.vo.ReservationBedPageReqVO;
import cn.iocoder.yudao.module.system.controller.admin.reservation.vo.ReservationBedRespVO;
import cn.iocoder.yudao.module.system.controller.admin.reservation.vo.ReservationBedSaveReqVO;
import cn.iocoder.yudao.module.system.controller.admin.reservation.vo.ReservationPayVO;
import cn.iocoder.yudao.module.system.controller.admin.reservation.vo.excel.ReservationBedExportExcelVO;
import cn.iocoder.yudao.module.system.dal.dataobject.reservation.ReservationBedDO;
import cn.iocoder.yudao.module.system.dal.dataobject.reservation.ReservationDepositDO;
import cn.iocoder.yudao.module.system.service.biz.BuildService;
import cn.iocoder.yudao.module.system.service.reservation.ReservationBedService;
import cn.iocoder.yudao.module.system.service.reservation.ReservationDepositService;
import cn.iocoder.yudao.module.system.service.tenant.TenantService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import org.springframework.web.bind.annotation.*;

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

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

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

import static cn.iocoder.yudao.framework.apilog.core.enums.OperateTypeEnum.EXPORT;
import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success;


@Tag(name = "管理后台 - 长者预约床位")
@RestController
@RequestMapping("/elderly/reservationBed")
@Validated
public class ReservationBedController {

    @Resource
    private ReservationBedService reservationBedService;
    @Resource
    private ReservationDepositService reservationDepositService;
    @Resource
    private BuildService buildService;
    @Resource
    private TenantService tenantService;

    @PostMapping("/create")
    @Operation(summary = "创建长者预约床位")
    @TenantIgnore
    public CommonResult<Boolean> createReservationBed(@Valid @RequestBody ReservationBedSaveReqVO createReqVO) {
        createReqVO.setTenantId(createReqVO.getTenantId() == null ? TenantContextHolder.getTenantId() : createReqVO.getTenantId());
        reservationBedService.createReservationBed(createReqVO);
        return success(true);
    }


    @PutMapping("/update")
    @Operation(summary = "更新长者预约床位")
    @TenantIgnore
    public CommonResult<Boolean> updateReservationBed(@Valid @RequestBody ReservationBedSaveReqVO updateReqVO) {
        reservationBedService.updateReservationBed(updateReqVO);
        return success(true);
    }
//
//    @DeleteMapping("/delete")
//    @Operation(summary = "删除长者预约床位")
//    @Parameter(name = "id", description = "编号", required = true)
//    @PreAuthorize("@ss.hasPermission('elderly:reservation-bed:delete')")
//    public CommonResult<Boolean> deleteReservationBed(@RequestParam("id") Long id) {
//        reservationBedService.deleteReservationBed(id);
//        return success(true);
//    }

    @GetMapping("/closeReservationBed")
    @Operation(summary = "关闭长者预约床位")
    @TenantIgnore
    public CommonResult<Boolean> closeReservationBed(@RequestParam("id") Long id) {
        reservationBedService.closeReservationBed(id);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得长者预约床位详情")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @TenantIgnore
    public CommonResult<ReservationBedRespVO> getReservationBed(@RequestParam("id") Long id) {
        ReservationBedRespVO reservationBed = reservationBedService.getReservationBed(id);
        return success(reservationBed);
    }

    @GetMapping("/page")
    @Operation(summary = "获得长者预约床位分页")
    @TenantIgnore
    public CommonResult<PageResult<ReservationBedRespVO>> getReservationBedPage(@Valid ReservationBedPageReqVO pageReqVO) {
        pageReqVO.setTenantIds(pageReqVO.getTenantIds() == null ? new Long[]{TenantContextHolder.getTenantId()} : pageReqVO.getTenantIds());
        PageResult<ReservationBedDO> pageResult = reservationBedService.getReservationBedPage(pageReqVO);
        PageResult<ReservationBedRespVO> pageBean = BeanUtils.toBean(pageResult, ReservationBedRespVO.class);
        for (ReservationBedRespVO respVO : pageBean.getList()) {
            respVO.setFullBedName(buildService.getFullBedNameByBedId(respVO.getBedId()));
            respVO.setTenantName(tenantService.getTenant(respVO.getTenantId()).getName());
        }
        return success(pageBean);
    }

    @GetMapping("/exportExcel")
    @Operation(summary = "导出长者预约床位 Excel")
    @ApiAccessLog(operateType = EXPORT)
    public void exportReservationBedExcel(@Valid ReservationBedPageReqVO pageReqVO,
                                          HttpServletResponse response) throws IOException {
//        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
        List<ReservationBedDO> list = reservationBedService.getReservationBedPage(pageReqVO).getList();
        // 导出 Excel
        ExcelUtils.write(response, "长者预约床位.xls", "数据", ReservationBedExportExcelVO.class,
                BeanUtils.toBean(list, ReservationBedExportExcelVO.class));
    }


    @PostMapping("/payFees")
    @Operation(summary = "预约入住缴费")
    @TenantIgnore
    public CommonResult<Boolean> payFees(@RequestBody ReservationPayVO reservationPayVO) {
        reservationDepositService.payFees(reservationPayVO);
        return success(true);
    }


    @GetMapping("/getReservationDetail")
    @Operation(summary = "获取预约入住押金详情")
    @TenantIgnore
    public CommonResult<ReservationBedRespVO> getReservationDetail(@RequestParam("reservationId") Long reservationId) {
        return success(reservationDepositService.getReservationDetail(reservationId));
    }

    @GetMapping("/refund")
    @Operation(summary = "预约入住押金退费")
    @TenantIgnore
    public CommonResult<Boolean> refund(@RequestParam("reservationId") Long reservationId) {
        reservationDepositService.refund(reservationId);
        return success(true);
    }

    @GetMapping("/getBedIdByIdCard")
    @Operation(summary = "通过长者身份证获取预约床位")
    @TenantIgnore
    public CommonResult<Long> getBedIdByIdCard(@RequestParam("idCard") String idCard){
        return success(reservationDepositService.getBedIdByIdCard(idCard));
    }
}