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

import com.zt.plat.module.infra.api.businessfile.dto.BusinessFileWithUrlRespDTO;
import com.zt.plat.module.qms.common.service.BusinessFileService;
import com.zt.plat.module.qms.office.supplier.controller.vo.SupplierPropertiesPageReqVO;
import com.zt.plat.module.qms.office.supplier.controller.vo.SupplierPropertiesRespVO;
import com.zt.plat.module.qms.office.supplier.controller.vo.SupplierPropertiesSaveReqVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;
import jakarta.annotation.Resource;
import org.springframework.validation.annotation.Validated;
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.SupplierPropertiesDO;
import com.zt.plat.module.qms.office.supplier.service.SupplierPropertiesService;

@Tag(name = "管理后台 - 供应商属性")
@RestController
@RequestMapping("/qms/supplier-properties")
@Validated
@DeptDataPermissionIgnore(enable = "true")
@FileUploadController(source = "qms.supplierproperties")
public class SupplierPropertiesController extends AbstractFileUploadController implements BusinessControllerMarker{

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

    @Resource
    private SupplierPropertiesService supplierPropertiesService;

    @Autowired
    private BusinessFileService businessFileService;

    @PostMapping("/create")
    @Operation(summary = "创建供应商属性")
//    @PreAuthorize("@ss.hasPermission('qms:supplier-properties:create')")
    public CommonResult<SupplierPropertiesRespVO> createSupplierProperties(@Valid @RequestBody SupplierPropertiesSaveReqVO createReqVO) {
        return success(supplierPropertiesService.createSupplierProperties(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新供应商属性")
//    @PreAuthorize("@ss.hasPermission('qms:supplier-properties:update')")
    public CommonResult<SupplierPropertiesRespVO> updateSupplierProperties(@Valid @RequestBody SupplierPropertiesSaveReqVO updateReqVO) {
        SupplierPropertiesRespVO respVO = supplierPropertiesService.updateSupplierProperties(updateReqVO);
        return success(respVO);
    }

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

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

    @GetMapping("/get")
    @Operation(summary = "获得供应商属性")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
//    @PreAuthorize("@ss.hasPermission('qms:supplier-properties:query')")
    public CommonResult<SupplierPropertiesRespVO> getSupplierProperties(@RequestParam("id") Long id) {
        SupplierPropertiesDO supplierProperties = supplierPropertiesService.getSupplierProperties(id);
        CommonResult<List<BusinessFileWithUrlRespDTO>> result = businessFileService.getBusinessFileList(id);
        SupplierPropertiesRespVO respVO = BeanUtils.toBean(supplierProperties, SupplierPropertiesRespVO.class);
        respVO.setBusinessFileRet(result);
        return success(respVO);
    }

    @GetMapping("/page")
    @Operation(summary = "获得供应商属性分页")
//    @PreAuthorize("@ss.hasPermission('qms:supplier-properties:query')")
    public CommonResult<PageResult<SupplierPropertiesRespVO>> getSupplierPropertiesPage(@Valid SupplierPropertiesPageReqVO pageReqVO) {
        PageResult<SupplierPropertiesDO> pageResult = supplierPropertiesService.getSupplierPropertiesPage(pageReqVO);
        return success(BeanUtils.toBean(pageResult, SupplierPropertiesRespVO.class));
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出供应商属性 Excel")
//    @PreAuthorize("@ss.hasPermission('qms:supplier-properties:export')")
    @ApiAccessLog(operateType = EXPORT)
    public void exportSupplierPropertiesExcel(@Valid SupplierPropertiesPageReqVO pageReqVO,
              HttpServletResponse response) throws IOException {
        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
        List<SupplierPropertiesDO> list = supplierPropertiesService.getSupplierPropertiesPage(pageReqVO).getList();
        // 导出 Excel
        ExcelUtils.write(response, "供应商属性.xls", "数据", SupplierPropertiesRespVO.class,
                        BeanUtils.toBean(list, SupplierPropertiesRespVO.class));
    }

}