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.product.ProductPageReqVO;
import com.cf.imes.module.system.controller.admin.funds.products.vo.product.ProductRespVO;
import com.cf.imes.module.system.controller.admin.funds.products.vo.product.ProductSaveReqVO;
import com.cf.imes.module.system.dal.dataobject.funds.products.ProductsDO;
import com.cf.imes.module.system.enums.products.ProductStatusEnum;
import com.cf.imes.module.system.service.funds.products.ProductsService;
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 static com.cf.imes.framework.common.pojo.CommonResult.success;

@Tag(name = "管理后台 - 产品管理")
@RestController
@RequestMapping("/system/products")
@Validated
public class ProductsController {


    @Resource
    private ProductsService productsService;

    @PutMapping("")
    @Operation(summary = "新建产品")
    @PreAuthorize("@ss.hasPermission('base:products:create')")
    public CommonResult<Long> createProducts(@RequestParam("productName") String productName) {
        return success(productsService.create(productName));
    }

    @PostMapping("")
    @Operation(summary = "更新产品")
    @PreAuthorize("@ss.hasPermission('base:products:update')")
    public CommonResult<Boolean> updateProducts(@Valid @RequestBody ProductSaveReqVO updateReqVO) {
        ProductsDO productsDO = BeanUtils.toBean(updateReqVO, ProductsDO.class);
        productsService.update(productsDO);
        return success(true);
    }

    @PostMapping("/freeze")
    @Operation(summary = "下架产品")
    @PreAuthorize("@ss.hasPermission('base:products:freeze')")
    public CommonResult<Boolean> freezeProducts(@Valid Long id) {
        ProductsDO productsDO = new ProductsDO().setId(id).setStatus(ProductStatusEnum.PRODUCT_DELIS.getStatus());
        productsService.update(productsDO);
        return success(true);
    }

    @PostMapping("/melt")
    @Operation(summary = "上架产品")
    @PreAuthorize("@ss.hasPermission('base:products:melt')")
    public CommonResult<Boolean> meltProducts(@Valid Long id) {
        ProductsDO productsDO = new ProductsDO().setId(id).setStatus(ProductStatusEnum.PRODUCT_UPDATES.getStatus());
        productsService.update(productsDO);
        return success(true);
    }


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

    @GetMapping("/{id}")
    @Operation(summary = "查询产品")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('base:products:query')")
    public CommonResult<ProductRespVO> getProducts(@PathVariable("id") Long id) {
        ProductsDO productRespVO = productsService.get(id);
        return success(BeanUtils.toBean(productRespVO, ProductRespVO.class));
    }

    @GetMapping("/page")
    @Operation(summary = "查询产品分页")
    @PreAuthorize("@ss.hasPermission('base:products:query')")
    public CommonResult<PageResult<ProductRespVO>> getProductsPage(@Valid ProductPageReqVO pageReqVO) {
        PageResult<ProductsDO> pageResult = productsService.getPage(pageReqVO);
        return success(BeanUtils.toBean(pageResult, ProductRespVO.class));
    }

}
