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

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.productpromotion.vo.ProductPromotionPageReqVO;
import com.cf.imes.module.system.controller.admin.funds.productpromotion.vo.ProductPromotionRespVO;
import com.cf.imes.module.system.controller.admin.funds.productpromotion.vo.ProductPromotionSaveReqVO;
import com.cf.imes.module.system.dal.dataobject.funds.productpromotion.ProductPromotionDO;
import com.cf.imes.module.system.service.funds.productpromotion.ProductPromotionService;
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.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/product/promotion")
@Validated
public class ProductPromotionController {

    @Resource
    private ProductPromotionService productPromotionService;

    @PostMapping("")
    @Operation(summary = "新建优惠活动")
    public CommonResult<Long> createProductPromotion(@Valid @RequestBody ProductPromotionSaveReqVO createReqVO) {
        return success(productPromotionService.create(createReqVO));
    }

    @PutMapping("")
    @Operation(summary = "更新优惠活动")
    public CommonResult<Boolean> updateProductPromotion(@Valid @RequestBody ProductPromotionSaveReqVO updateReqVO) {
        productPromotionService.update(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/{id}")
    @Operation(summary = "删除优惠活动")
    @Parameter(name = "id", description = "编号", required = true)
    public CommonResult<Boolean> deleteProductPromotion(@PathVariable("id") Long id) {
        productPromotionService.delete(id);
        return success(true);
    }

    @GetMapping("/{id}")
    @Operation(summary = "获取优惠活动")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    public CommonResult<ProductPromotionRespVO> getProductPromotion(@PathVariable("id") Long id) {
        ProductPromotionDO giftMoneyDetailsDO = productPromotionService.get(id);
        return success(BeanUtils.toBean(giftMoneyDetailsDO, ProductPromotionRespVO.class));
    }

    @GetMapping("/page")
    @Operation(summary = "获取优惠活动分页")
    public CommonResult<PageResult<ProductPromotionRespVO>> getProductPromotionPage(@Valid ProductPromotionPageReqVO pageReqVO) {
        PageResult<ProductPromotionDO> pageResult = productPromotionService.getPage(pageReqVO);
        return success(BeanUtils.toBean(pageResult, ProductPromotionRespVO.class));
    }
}
