package com.zt.plat.module.qms.office.supplier.controller.admin;

import com.zt.plat.module.qms.business.reportdoc.controller.vo.ReportDocumentMainRespVO;
import com.zt.plat.module.qms.business.reportdoc.controller.vo.ReportDocumentMainSaveReqVO;
import com.zt.plat.module.qms.office.supplier.controller.vo.SupplierEvaluationPageReqVO;
import com.zt.plat.module.qms.office.supplier.controller.vo.SupplierEvaluationRespVO;
import com.zt.plat.module.qms.office.supplier.controller.vo.SupplierEvaluationSaveReqVO;
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 com.zt.plat.framework.datapermission.core.annotation.DeptDataPermissionIgnore;

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.office.supplier.dal.dataobject.SupplierEvaluationDO;
import com.zt.plat.module.qms.office.supplier.service.SupplierEvaluationService;

@Tag(name = "管理后台 - 供应商评价")
@RestController
@RequestMapping("/qms/supplier-evaluation")
@Validated
@DeptDataPermissionIgnore(enable = "true")
@FileUploadController(source = "qms.supplierevaluation")
public class SupplierEvaluationController extends AbstractFileUploadController implements BusinessControllerMarker{

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

    @Resource
    private SupplierEvaluationService supplierEvaluationService;

    @PostMapping("/create")
    @Operation(summary = "创建供应商评价")
//    @PreAuthorize("@ss.hasPermission('qms:supplier-evaluation:create')")
    public CommonResult<SupplierEvaluationRespVO> createSupplierEvaluation(@Valid @RequestBody SupplierEvaluationSaveReqVO createReqVO) {
        return success(supplierEvaluationService.createSupplierEvaluation(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新供应商评价")
//    @PreAuthorize("@ss.hasPermission('qms:supplier-evaluation:update')")
    public CommonResult<Boolean> updateSupplierEvaluation(@Valid @RequestBody SupplierEvaluationSaveReqVO updateReqVO) {
        supplierEvaluationService.updateSupplierEvaluation(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除供应商评价")
    @Parameter(name = "id", description = "编号", required = true)
//    @PreAuthorize("@ss.hasPermission('qms:supplier-evaluation:delete')")
    public CommonResult<Boolean> deleteSupplierEvaluation(@RequestParam("id") Long id) {
        supplierEvaluationService.deleteSupplierEvaluation(id);
        return success(true);
    }

    @DeleteMapping("/delete-list")
    @Parameter(name = "ids", description = "编号", required = true)
    @Operation(summary = "批量删除供应商评价")
//                @PreAuthorize("@ss.hasPermission('qms:supplier-evaluation:delete')")
    public CommonResult<Boolean> deleteSupplierEvaluationList(@RequestBody BatchDeleteReqVO req) {
        supplierEvaluationService.deleteSupplierEvaluationListByIds(req.getIds());
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得供应商评价")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
//    @PreAuthorize("@ss.hasPermission('qms:supplier-evaluation:query')")
    public CommonResult<SupplierEvaluationRespVO> getSupplierEvaluation(@RequestParam("id") Long id) {
        SupplierEvaluationDO supplierEvaluation = supplierEvaluationService.getSupplierEvaluation(id);
        return success(BeanUtils.toBean(supplierEvaluation, SupplierEvaluationRespVO.class));
    }

    @GetMapping("/page")
    @Operation(summary = "获得供应商评价分页")
//    @PreAuthorize("@ss.hasPermission('qms:supplier-evaluation:query')")
    public CommonResult<PageResult<SupplierEvaluationRespVO>> getSupplierEvaluationPage(@Valid SupplierEvaluationPageReqVO pageReqVO) {
        PageResult<SupplierEvaluationRespVO> pageResult = supplierEvaluationService.getSupplierEvaluationPage(pageReqVO);
        return success(pageResult);
    }

    @PostMapping("/createProcessInstance")
    @Operation(summary = "发起流程")
    public CommonResult<SupplierEvaluationRespVO> createProcessInstance(@Valid @RequestBody SupplierEvaluationSaveReqVO createReqVO) {
        return supplierEvaluationService.createProcessInstance(createReqVO);
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出供应商评价 Excel")
    @PreAuthorize("@ss.hasPermission('qms:supplier-evaluation:export')")
    @ApiAccessLog(operateType = EXPORT)
    public void exportSupplierEvaluationExcel(@Valid SupplierEvaluationPageReqVO pageReqVO,
              HttpServletResponse response) throws IOException {
        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
        List<SupplierEvaluationRespVO> list = supplierEvaluationService.getSupplierEvaluationPage(pageReqVO).getList();
        // 导出 Excel
        ExcelUtils.write(response, "供应商评价.xls", "数据", SupplierEvaluationRespVO.class,
                        BeanUtils.toBean(list, SupplierEvaluationRespVO.class));
    }

}