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

import cn.hutool.core.collection.CollUtil;
import com.zt.plat.module.qms.office.supplier.controller.vo.SupplierPageReqVO;
import com.zt.plat.module.qms.office.supplier.controller.vo.SupplierRespVO;
import com.zt.plat.module.qms.office.supplier.controller.vo.SupplierSaveReqVO;
import com.zt.plat.module.qms.office.supplier.dal.dataobject.SupplierPropertiesDO;
import org.springframework.web.bind.annotation.*;
import jakarta.annotation.Resource;

import org.springframework.security.access.prepost.PreAuthorize;
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.controller.vo.*;
import com.zt.plat.module.qms.office.supplier.dal.dataobject.SupplierDO;
import com.zt.plat.module.qms.office.supplier.service.SupplierService;

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

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

    @Resource
    private SupplierService supplierService;

    @PostMapping("/create")
    @Operation(summary = "创建供应商")
//    @PreAuthorize("@ss.hasPermission('qms:supplier:create')")
    public CommonResult<SupplierRespVO> createSupplier(@Valid @RequestBody SupplierSaveReqVO createReqVO) {
        return success(supplierService.createSupplier(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新供应商")
//    @PreAuthorize("@ss.hasPermission('qms:supplier:update')")
    public CommonResult<Boolean> updateSupplier(@Valid @RequestBody SupplierSaveReqVO updateReqVO) {
        supplierService.updateSupplier(updateReqVO);
        return success(true);
    }

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

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

    @GetMapping("/get")
    @Operation(summary = "获得供应商")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
//    @PreAuthorize("@ss.hasPermission('qms:supplier:query')")
    public CommonResult<SupplierExtendRespVO> getSupplier(@RequestParam("id") Long id) {
        SupplierExtendRespVO supplier = supplierService.getSupplier(id);

        return success(supplier);
    }

    @GetMapping("/page")
    @Operation(summary = "获得供应商分页")
//    @PreAuthorize("@ss.hasPermission('qms:supplier:query')")
    public CommonResult<PageResult<SupplierRespVO>> getSupplierPage(@Valid SupplierPageReqVO pageReqVO) {
        PageResult<SupplierDO> pageResult = supplierService.getSupplierPage(pageReqVO);
        return success(BeanUtils.toBean(pageResult, SupplierRespVO.class));
    }

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

}