package com.zt.plat.module.qms.resource.record.controller.admin.recordapplydetail;

import com.zt.plat.framework.datapermission.core.annotation.DeptDataPermissionIgnore;
import com.zt.plat.module.qms.resource.record.controller.admin.recordapplydetail.vo.RecordApplyDetailJoinVO;
import com.zt.plat.module.qms.resource.record.controller.admin.recordapplydetail.vo.RecordApplyDetailPageReqVO;
import com.zt.plat.module.qms.resource.record.controller.admin.recordapplydetail.vo.RecordApplyDetailRespVO;
import com.zt.plat.module.qms.resource.record.controller.admin.recordapplydetail.vo.RecordApplyDetailSaveReqVO;
import org.springframework.web.bind.annotation.*;
import jakarta.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import org.springframework.security.access.prepost.PreAuthorize;
import com.zt.plat.framework.business.interceptor.BusinessControllerMarker;
import com.zt.plat.framework.business.annotation.FileUploadController;
import com.zt.plat.framework.business.controller.AbstractFileUploadController;

import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Operation;

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

import com.zt.plat.framework.common.pojo.vo.BatchDeleteReqVO;
import com.zt.plat.framework.common.pojo.PageParam;
import com.zt.plat.framework.common.pojo.PageResult;
import com.zt.plat.framework.common.pojo.CommonResult;
import com.zt.plat.framework.common.util.object.BeanUtils;
import static com.zt.plat.framework.common.pojo.CommonResult.success;

import com.zt.plat.framework.excel.core.util.ExcelUtils;

import com.zt.plat.framework.apilog.core.annotation.ApiAccessLog;
import static com.zt.plat.framework.apilog.core.enums.OperateTypeEnum.*;

import com.zt.plat.module.qms.resource.record.dal.dataobject.recordapplydetail.RecordApplyDetailDO;
import com.zt.plat.module.qms.resource.record.service.recordapplydetail.RecordApplyDetailService;

@Tag(name = "管理后台 - 文件记录通用申请明细")
@RestController
@RequestMapping("/qms/record-apply-detail")
@Validated
@DeptDataPermissionIgnore(enable = "true")
@FileUploadController(source = "qms.recordapplydetail")
public class RecordApplyDetailController extends AbstractFileUploadController implements BusinessControllerMarker{

    static {
        FileUploadController annotation = RecordApplyDetailController.class.getAnnotation(FileUploadController.class);
        if (annotation != null) {
        setFileUploadInfo(annotation);
        }
    }

    @Resource
    private RecordApplyDetailService recordApplyDetailService;

    @PostMapping("/create")
    @Operation(summary = "创建文件记录通用申请明细")
    //@PreAuthorize("@ss.hasPermission('qms:record-apply-detail:create')")
    public CommonResult<RecordApplyDetailRespVO> createRecordApplyDetail(@Valid @RequestBody RecordApplyDetailSaveReqVO createReqVO) {
        return success(recordApplyDetailService.createRecordApplyDetail(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新文件记录通用申请明细")
    //@PreAuthorize("@ss.hasPermission('qms:record-apply-detail:update')")
    public CommonResult<Boolean> updateRecordApplyDetail(@Valid @RequestBody RecordApplyDetailSaveReqVO updateReqVO) {
        recordApplyDetailService.updateRecordApplyDetail(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除文件记录通用申请明细")
    @Parameter(name = "id", description = "编号", required = true)
    //@PreAuthorize("@ss.hasPermission('qms:record-apply-detail:delete')")
    public CommonResult<Boolean> deleteRecordApplyDetail(@RequestParam("id") Long id) {
        recordApplyDetailService.deleteRecordApplyDetail(id);
        return success(true);
    }

    @DeleteMapping("/delete-list")
    @Parameter(name = "ids", description = "编号", required = true)
    @Operation(summary = "批量删除文件记录通用申请明细")
                //@PreAuthorize("@ss.hasPermission('qms:record-apply-detail:delete')")
    public CommonResult<Boolean> deleteRecordApplyDetailList(@RequestBody BatchDeleteReqVO req) {
        recordApplyDetailService.deleteRecordApplyDetailListByIds(req.getIds());
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得文件记录通用申请明细")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    //@PreAuthorize("@ss.hasPermission('qms:record-apply-detail:query')")
    public CommonResult<RecordApplyDetailRespVO> getRecordApplyDetail(@RequestParam("id") Long id) {
        RecordApplyDetailDO recordApplyDetail = recordApplyDetailService.getRecordApplyDetail(id);
        return success(BeanUtils.toBean(recordApplyDetail, RecordApplyDetailRespVO.class));
    }

    @GetMapping("/page")
    @Operation(summary = "获得文件记录通用申请明细分页")
    //@PreAuthorize("@ss.hasPermission('qms:record-apply-detail:query')")
    public CommonResult<PageResult<RecordApplyDetailJoinVO>> getRecordApplyDetailPage(@Valid RecordApplyDetailPageReqVO pageReqVO) {
        PageResult<RecordApplyDetailJoinVO> recordApplyDetailJoinVOPageResult = recordApplyDetailService.selectDetailAndApplyList(pageReqVO);
        return success(BeanUtils.toBean(recordApplyDetailJoinVOPageResult, RecordApplyDetailJoinVO.class));
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出文件记录通用申请明细 Excel")
    //@PreAuthorize("@ss.hasPermission('qms:record-apply-detail:export')")
    @ApiAccessLog(operateType = EXPORT)
    public void exportRecordApplyDetailExcel(@Valid RecordApplyDetailPageReqVO pageReqVO,
              HttpServletResponse response) throws IOException {
        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
        List<RecordApplyDetailDO> list = recordApplyDetailService.getRecordApplyDetailPage(pageReqVO).getList();
        // 导出 Excel
        ExcelUtils.write(response, "文件记录通用申请明细.xls", "数据", RecordApplyDetailRespVO.class,
                        BeanUtils.toBean(list, RecordApplyDetailRespVO.class));
    }

}