package cn.iocoder.yudao.module.wms.controller.admin.productcategoryproperty;

import org.springframework.web.bind.annotation.*;
import javax.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import org.springframework.security.access.prepost.PreAuthorize;
import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Operation;

import javax.validation.constraints.*;
import javax.validation.*;
import javax.servlet.http.*;
import java.util.*;
import java.io.IOException;

import cn.iocoder.yudao.framework.common.pojo.PageParam;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.pojo.CommonResult;
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;
import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success;

import cn.iocoder.yudao.framework.excel.core.util.ExcelUtils;

import cn.iocoder.yudao.framework.operatelog.core.annotations.OperateLog;
import static cn.iocoder.yudao.framework.operatelog.core.enums.OperateTypeEnum.*;

import cn.iocoder.yudao.module.wms.controller.admin.productcategoryproperty.vo.*;
import cn.iocoder.yudao.module.wms.dal.dataobject.productcategoryproperty.ProductCategoryPropertyDO;
import cn.iocoder.yudao.module.wms.service.productcategoryproperty.ProductCategoryPropertyService;

@Tag(name = "管理后台 - 产品分类属性")
@RestController
@RequestMapping("/wms/product-category-property")
@Validated
public class ProductCategoryPropertyController {

    @Resource
    private ProductCategoryPropertyService productCategoryPropertyService;

    @PostMapping("/create")
    @Operation(summary = "创建产品分类属性")
    @PreAuthorize("@ss.hasPermission('wms:product-category-property:create')")
    public CommonResult<Long> createProductCategoryProperty(@Valid @RequestBody ProductCategoryPropertySaveReqVO createReqVO) {
        return success(productCategoryPropertyService.createProductCategoryProperty(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新产品分类属性")
    @PreAuthorize("@ss.hasPermission('wms:product-category-property:update')")
    public CommonResult<Boolean> updateProductCategoryProperty(@Valid @RequestBody ProductCategoryPropertySaveReqVO updateReqVO) {
        productCategoryPropertyService.updateProductCategoryProperty(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除产品分类属性")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('wms:product-category-property:delete')")
    public CommonResult<Boolean> deleteProductCategoryProperty(@RequestParam("id") Long id) {
        productCategoryPropertyService.deleteProductCategoryProperty(id);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得产品分类属性")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('wms:product-category-property:query')")
    public CommonResult<ProductCategoryPropertyRespVO> getProductCategoryProperty(@RequestParam("id") Long id) {
        ProductCategoryPropertyDO productCategoryProperty = productCategoryPropertyService.getProductCategoryProperty(id);
        return success(BeanUtils.toBean(productCategoryProperty, ProductCategoryPropertyRespVO.class));
    }

    @GetMapping("/page")
    @Operation(summary = "获得产品分类属性分页")
    @PreAuthorize("@ss.hasPermission('wms:product-category-property:query')")
    public CommonResult<PageResult<ProductCategoryPropertyRespVO>> getProductCategoryPropertyPage(@Valid ProductCategoryPropertyPageReqVO pageReqVO) {
        PageResult<ProductCategoryPropertyDO> pageResult = productCategoryPropertyService.getProductCategoryPropertyPage(pageReqVO);
        return success(BeanUtils.toBean(pageResult, ProductCategoryPropertyRespVO.class));
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出产品分类属性 Excel")
    @PreAuthorize("@ss.hasPermission('wms:product-category-property:export')")
    @OperateLog(type = EXPORT)
    public void exportProductCategoryPropertyExcel(@Valid ProductCategoryPropertyPageReqVO pageReqVO,
              HttpServletResponse response) throws IOException {
        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
        List<ProductCategoryPropertyDO> list = productCategoryPropertyService.getProductCategoryPropertyPage(pageReqVO).getList();
        // 导出 Excel
        ExcelUtils.write(response, "产品分类属性.xls", "数据", ProductCategoryPropertyRespVO.class,
                        BeanUtils.toBean(list, ProductCategoryPropertyRespVO.class));
    }

}