package com.zt.plat.module.qms.common.data.controller.admin;

import com.zt.plat.module.qms.common.data.controller.vo.DataOpinionPageReqVO;
import com.zt.plat.module.qms.common.data.controller.vo.DataOpinionRespVO;
import com.zt.plat.module.qms.common.data.controller.vo.DataOpinionSaveReqVO;
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.common.data.dal.dataobject.DataOpinionDO;
import com.zt.plat.module.qms.common.data.service.DataOpinionService;

@Tag(name = "管理后台 - 审批意见")
@RestController
@RequestMapping("/qms/data-opinion")
@Validated
@FileUploadController(source = "qms.dataopinion")
public class DataOpinionController extends AbstractFileUploadController implements BusinessControllerMarker{

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

    @Resource
    private DataOpinionService dataOpinionService;

    @PostMapping("/create")
    @Operation(summary = "创建审批意见")
    public CommonResult<DataOpinionRespVO> createDataOpinion(@Valid @RequestBody DataOpinionSaveReqVO createReqVO) {
        return success(dataOpinionService.createDataOpinion(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新审批意见")
    public CommonResult<Boolean> updateDataOpinion(@Valid @RequestBody DataOpinionSaveReqVO updateReqVO) {
        dataOpinionService.updateDataOpinion(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除审批意见")
    @Parameter(name = "id", description = "编号", required = true)
    public CommonResult<Boolean> deleteDataOpinion(@RequestParam("id") Long id) {
        dataOpinionService.deleteDataOpinion(id);
        return success(true);
    }

    @DeleteMapping("/delete-list")
    @Parameter(name = "ids", description = "编号", required = true)
    @Operation(summary = "批量删除审批意见")
    @PreAuthorize("@ss.hasPermission('qms:data-opinion:delete')")
    public CommonResult<Boolean> deleteDataOpinionList(@RequestBody BatchDeleteReqVO req) {
        dataOpinionService.deleteDataOpinionListByIds(req.getIds());
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得审批意见")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    public CommonResult<DataOpinionRespVO> getDataOpinion(@RequestParam("id") Long id) {
        DataOpinionDO dataOpinion = dataOpinionService.getDataOpinion(id);
        return success(BeanUtils.toBean(dataOpinion, DataOpinionRespVO.class));
    }

    @GetMapping("/page")
    @Operation(summary = "获得审批意见分页")
    public CommonResult<PageResult<DataOpinionRespVO>> getDataOpinionPage(@Valid DataOpinionPageReqVO pageReqVO) {
        PageResult<DataOpinionDO> pageResult = dataOpinionService.getDataOpinionPage(pageReqVO);
        return success(BeanUtils.toBean(pageResult, DataOpinionRespVO.class));
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出审批意见 Excel")
    @PreAuthorize("@ss.hasPermission('qms:data-opinion:export')")
    @ApiAccessLog(operateType = EXPORT)
    public void exportDataOpinionExcel(@Valid DataOpinionPageReqVO pageReqVO,
              HttpServletResponse response) throws IOException {
        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
        List<DataOpinionDO> list = dataOpinionService.getDataOpinionPage(pageReqVO).getList();
        // 导出 Excel
        ExcelUtils.write(response, "审批意见.xls", "数据", DataOpinionRespVO.class, BeanUtils.toBean(list, DataOpinionRespVO.class));
    }

}