package com.cf.imes.module.system.controller.admin.funds.products;

import com.cf.imes.framework.common.pojo.CommonResult;
import com.cf.imes.framework.common.pojo.PageResult;
import com.cf.imes.framework.common.util.object.BeanUtils;
import com.cf.imes.module.system.controller.admin.funds.products.vo.productDetails.ProductDetailsPageReqVO;
import com.cf.imes.module.system.controller.admin.funds.products.vo.productDetails.ProductDetailsRespVO;
import com.cf.imes.module.system.controller.admin.funds.products.vo.productDetails.ProductDetailsSaveReqVO;
import com.cf.imes.module.system.dal.dataobject.funds.products.ProductsDetailDO;
import com.cf.imes.module.system.service.funds.products.ProductsDetailService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import jakarta.annotation.Resource;
import jakarta.validation.Valid;
import java.util.List;

import static com.cf.imes.framework.common.pojo.CommonResult.success;

@Tag(name = "管理后台 - 产品明细管理")
@RestController
@RequestMapping("/system/product-details")
@Validated
public class ProductsDetailController {

    @Resource
    private ProductsDetailService productDetailsService;

    @PutMapping("")
    @Operation(summary = "新建产品明细")
    @PreAuthorize("@ss.hasPermission('base:productDetails:create')")
    public CommonResult<Long> createProductsDetails(@Valid @RequestBody ProductDetailsSaveReqVO createReqVO) {
        return success(productDetailsService.create(createReqVO));
    }

    @PostMapping("")
    @Operation(summary = "更新产品明细")
    @PreAuthorize("@ss.hasPermission('base:productDetails:update')")
    public CommonResult<Boolean> updateProductsDetails(@Valid @RequestBody ProductDetailsSaveReqVO updateReqVO) {
        productDetailsService.update(updateReqVO);
        return success(true);
    }

    @DeleteMapping("")
    @Operation(summary = "删除产品明细")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermissionDetails('base:productDetails:delete')")
    public CommonResult<Boolean> deleteProducts(@RequestParam("id") Long id) {
        productDetailsService.delete(id);
        return success(true);
    }

    @GetMapping("/{id}")
    @Operation(summary = "查询产品明细")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('base:productDetails:query')")
    public CommonResult<ProductsDetailDO> getProductsDetail(@PathVariable("id") Long id) {
        return success(productDetailsService.get(id));
    }

    @GetMapping("/page")
    @Operation(summary = "查询产品明细分页")
    @PreAuthorize("@ss.hasPermission('base:productDetails:query')")
    public CommonResult<PageResult<ProductDetailsRespVO>> getProductsDetailsPage(@Valid ProductDetailsPageReqVO pageReqVO) {
        PageResult<ProductsDetailDO> pageResult = productDetailsService.getPage(pageReqVO);
        return success(BeanUtils.toBean(pageResult, ProductDetailsRespVO.class));
    }

}
