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

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.biz.vo.AskLeavePageReqVO;
import cn.iocoder.yudao.module.system.controller.admin.biz.vo.ElderlyAskLeaveExportExcelVO;
import cn.iocoder.yudao.module.system.dal.dataobject.biz.ElderlyAskLeaveDO;
import cn.iocoder.yudao.module.system.service.biz.ElderlyAskLeaveService;
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 java.util.Objects;

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


@Tag(name = "管理后台 - 老人请假外出管理")
@RestController
@RequestMapping("/elderly/askLeave")
@Validated
public class ElderlyAskLeaveController {

    @Resource
    private ElderlyAskLeaveService elderlyAskLeaveService;


    @PostMapping("/create")
    @Operation(summary = "创建老人请假管理")
    @TenantIgnore
    public CommonResult<Boolean> createAskLeave(@Valid @RequestBody ElderlyAskLeaveDO createReqVO) {
        createReqVO.setTenantId(createReqVO.getTenantId() == null ? TenantContextHolder.getTenantId() : createReqVO.getTenantId());
        elderlyAskLeaveService.createAskLeave(createReqVO);
        return success(true);
    }


    @PostMapping("/edit")
    @Operation(summary = "编辑老人请假管理")
    @TenantIgnore
    public CommonResult<Boolean> editAskLeave(@RequestBody ElderlyAskLeaveDO elderlyAskLeaveDO) {
        elderlyAskLeaveService.editAskLeave(elderlyAskLeaveDO);
        return success(true);
    }


    @DeleteMapping("/delete")
    @Operation(summary = "删除老人请假管理")
    @Parameter(name = "id", description = "外出请假记录ID", required = true, example = "1024")
    public CommonResult<Boolean> delete(@RequestParam("id") Long id) {
        elderlyAskLeaveService.delete(id);
        return success(true);
    }


    @GetMapping("/findPage")
    @Operation(summary = "老人外出请假列表")
    @TenantIgnore
    public CommonResult<PageResult<ElderlyAskLeaveDO>> findPage(@Valid AskLeavePageReqVO reqVO) {
        reqVO.setTenantIds(Objects.isNull(reqVO.getTenantIds()) ? new Long[]{TenantContextHolder.getTenantId()} : reqVO.getTenantIds());
        PageResult<ElderlyAskLeaveDO> pageResult = elderlyAskLeaveService.findPage(reqVO);
        return success(BeanUtils.toBean(pageResult, ElderlyAskLeaveDO.class));
    }


    @GetMapping("/getDetail")
    @Operation(summary = "老人外出请假详情")
    @Parameter(name = "id", description = "外出请假ID", required = true, example = "1024")
    @TenantIgnore
    public CommonResult<ElderlyAskLeaveDO> getDetail(@RequestParam("id") Long id) {
        return success(elderlyAskLeaveService.getDetail(id));
    }


    @GetMapping("/exportExcel")
    @Operation(summary = "导出老人外出请假Excel")
    @TenantIgnore
    @ApiAccessLog(operateType = cn.iocoder.yudao.framework.apilog.core.enums.OperateTypeEnum.EXPORT)
    public void exportExcel(@Valid AskLeavePageReqVO reqVO, HttpServletResponse response) throws IOException {
        List<ElderlyAskLeaveExportExcelVO> list = elderlyAskLeaveService.getExportList(reqVO);
        // 导出 Excel
        ExcelUtils.write(response, "老人外出请假.xls", "数据", ElderlyAskLeaveExportExcelVO.class, list);
    }


//
//    @PutMapping("/update")
//    @Operation(summary = "更新老人请假管理")
//    @PreAuthorize("@ss.hasPermission('elderly:ask-leave:update')")
//    public CommonResult<Boolean> updateAskLeave(@Valid @RequestBody AskLeaveSaveReqVO updateReqVO) {
//        askLeaveService.updateAskLeave(updateReqVO);
//        return success(true);
//    }
//
//    @DeleteMapping("/delete")
//    @Operation(summary = "删除老人请假管理")
//    @Parameter(name = "id", description = "编号", required = true)
//    @PreAuthorize("@ss.hasPermission('elderly:ask-leave:delete')")
//    public CommonResult<Boolean> deleteAskLeave(@RequestParam("id") Long id) {
//        askLeaveService.deleteAskLeave(id);
//        return success(true);
//    }
//
//    @GetMapping("/get")
//    @Operation(summary = "获得老人请假管理")
//    @Parameter(name = "id", description = "编号", required = true, example = "1024")
//    @PreAuthorize("@ss.hasPermission('elderly:ask-leave:query')")
//    public CommonResult<AskLeaveRespVO> getAskLeave(@RequestParam("id") Long id) {
//        AskLeaveDO askLeave = askLeaveService.getAskLeave(id);
//        return success(BeanUtils.toBean(askLeave, AskLeaveRespVO.class));
//    }
//
//    @GetMapping("/page")
//    @Operation(summary = "获得老人请假管理分页")
//    @PreAuthorize("@ss.hasPermission('elderly:ask-leave:query')")
//    public CommonResult<PageResult<AskLeaveRespVO>> getAskLeavePage(@Valid AskLeavePageReqVO pageReqVO) {
//        PageResult<AskLeaveDO> pageResult = askLeaveService.getAskLeavePage(pageReqVO);
//        return success(BeanUtils.toBean(pageResult, AskLeaveRespVO.class));
//    }
//
//    @GetMapping("/export-excel")
//    @Operation(summary = "导出老人请假管理 Excel")
//    @PreAuthorize("@ss.hasPermission('elderly:ask-leave:export')")
//    @ApiAccessLog(operateType = EXPORT)
//    public void exportAskLeaveExcel(@Valid AskLeavePageReqVO pageReqVO,
//              HttpServletResponse response) throws IOException {
//        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
//        List<AskLeaveDO> list = askLeaveService.getAskLeavePage(pageReqVO).getList();
//        // 导出 Excel
//        ExcelUtils.write(response, "老人请假管理.xls", "数据", AskLeaveRespVO.class,
//                        BeanUtils.toBean(list, AskLeaveRespVO.class));
//    }

}