package cn.iocoder.yudao.module.system.controller.admin.biz;
import cn.iocoder.yudao.framework.security.core.LoginUser;
import cn.iocoder.yudao.framework.tenant.core.aop.TenantIgnore;
import cn.iocoder.yudao.module.system.api.bpm.vo.ElderlyRetreatRecordPageReqApiVO;
import cn.iocoder.yudao.module.system.controller.admin.biz.vo.ElderlyRetreatRecordRespVO;
import cn.iocoder.yudao.module.system.controller.admin.biz.vo.ElderlyRetreatRecordSaveReqVO;
import cn.iocoder.yudao.module.system.controller.admin.biz.vo.RetreatPrintRespVO;
import cn.iocoder.yudao.module.system.dal.dataobject.biz.ElderlyRetreatRecordDO;
import cn.iocoder.yudao.module.system.service.biz.RetreatRecordService;
import org.springframework.web.bind.annotation.*;
import javax.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import org.springframework.security.access.prepost.PreAuthorize;
import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Operation;

import javax.validation.*;
import javax.servlet.http.*;
import java.util.*;
import java.io.IOException;

import cn.iocoder.yudao.framework.common.pojo.PageParam;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.pojo.CommonResult;
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;
import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success;

import cn.iocoder.yudao.framework.excel.core.util.ExcelUtils;

import cn.iocoder.yudao.framework.apilog.core.annotation.ApiAccessLog;
import static cn.iocoder.yudao.framework.apilog.core.enums.OperateTypeEnum.*;
import static cn.iocoder.yudao.framework.security.core.util.SecurityFrameworkUtils.getLoginUser;
import static cn.iocoder.yudao.framework.security.core.util.SecurityFrameworkUtils.getLoginUserId;

/**
 * @Author: jinyd
 * @CreateTime: 2024/8/13  9:02
 * @Description:
 * @Version: 4.0
 */


@Tag(name = "管理后台 - 长者退住申请记录")
@RestController
@RequestMapping("/elderly/retreatRecord")
@Validated
public class RetreatRecordController {

    @Resource
    private RetreatRecordService retreatRecordService;

    @PostMapping("/create")
    @Operation(summary = "创建长者退住申请记录")
    @PreAuthorize("@ss.hasPermission('elderly:retreat-record:create')")
    public CommonResult<Long> createRetreatRecord(@Valid @RequestBody ElderlyRetreatRecordSaveReqVO createReqVO) {
        return success(retreatRecordService.createRetreatRecord(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新长者退住申请记录")
    @PreAuthorize("@ss.hasPermission('elderly:retreat-record:update')")
    public CommonResult<Boolean> updateRetreatRecord(@Valid @RequestBody ElderlyRetreatRecordSaveReqVO updateReqVO) {
        retreatRecordService.updateRetreatRecord(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除长者退住申请记录")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('elderly:retreat-record:delete')")
    public CommonResult<Boolean> deleteRetreatRecord(@RequestParam("id") Long id) {
        retreatRecordService.deleteRetreatRecord(id);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得长者退住申请记录")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('elderly:retreat-record:query')")
    public CommonResult<ElderlyRetreatRecordRespVO> getRetreatRecord(@RequestParam("id") Long id) {
        ElderlyRetreatRecordDO retreatRecord = retreatRecordService.getRetreatRecord(id);
        return success(BeanUtils.toBean(retreatRecord, ElderlyRetreatRecordRespVO.class));
    }

    @GetMapping("/page")
    @Operation(summary = "获得长者退住申请记录分页")
    @PreAuthorize("@ss.hasPermission('elderly:retreat-record:query')")
    public CommonResult<PageResult<ElderlyRetreatRecordRespVO>> getRetreatRecordPage(@Valid ElderlyRetreatRecordPageReqApiVO pageReqVO) {
        PageResult<ElderlyRetreatRecordRespVO> pageResult = retreatRecordService.getRetreatRecordPage(pageReqVO);
        return success(BeanUtils.toBean(pageResult, ElderlyRetreatRecordRespVO.class));
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出长者退住申请记录 Excel")
    @PreAuthorize("@ss.hasPermission('elderly:retreat-record:export')")
    @ApiAccessLog(operateType = EXPORT)
    public void exportRetreatRecordExcel(@Valid ElderlyRetreatRecordPageReqApiVO pageReqVO,
                                         HttpServletResponse response) throws IOException {
        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
        List<ElderlyRetreatRecordRespVO> list = retreatRecordService.getRetreatRecordPage(pageReqVO).getList();
        // 导出 Excel
        ExcelUtils.write(response, "长者退住申请记录.xls", "数据", ElderlyRetreatRecordRespVO.class,
                BeanUtils.toBean(list, ElderlyRetreatRecordRespVO.class));
    }

    @GetMapping("/detail")
    @Operation(summary = "详情")
    @Parameter(name = "id", description = "退住申请主键", required = true, example = "1024")
    @Parameter(name = "queryType", description = "查询标识，0-详情，1-打印", required = true, example = "1024")
//    @PreAuthorize("@ss.hasPermission('check-out:detail')")
    @TenantIgnore
    public CommonResult<RetreatPrintRespVO> getRetreatRecordDetail(@RequestParam("id") Long id,@RequestParam("queryType") int queryType) {
        RetreatPrintRespVO retreatPrintRespVO = retreatRecordService.getRetreatRecordDetail(id, queryType);
        return success(retreatPrintRespVO);
    }

}
