package com.ylx.giftCard.controller;

import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.ylx.common.core.domain.R;
import com.ylx.giftCard.domain.GiftCard;
import com.ylx.giftCard.domain.dto.GiftCardPurchaseDTO;
import com.ylx.giftCard.domain.vo.GiftCardDetailVO;
import com.ylx.giftCard.domain.vo.GiftCardVO;
import com.ylx.giftCard.service.IGiftCardService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.Map;

@RestController
@RequestMapping("/gift/card")
@Api(tags = {"购物卡"})
@Slf4j
public class GiftCardController {

    @Resource
    private IGiftCardService giftCardService;

    @GetMapping("/page")
    @ApiOperation("购物卡分页列表接口")
    public R<Page<GiftCardVO>> getGiftCardPage(Page<GiftCard> page) {
        Page<GiftCardVO> pageData = giftCardService.getGiftCardPage(page);
        return R.ok(pageData);
    }

    @PostMapping("/purchase")
    @ApiOperation("购物卡购买接口")
    public R<Map<String, Object>> purchaseGiftCard(@Validated @RequestBody GiftCardPurchaseDTO dto) {
        Map<String, Object> data =giftCardService.purchaseGiftCard(dto);
        return R.ok(data);
    }

    @GetMapping("/{id}/detail")
    @ApiOperation("获取购物卡详情")
    public R<GiftCardDetailVO> getGiftCardDetail(@PathVariable Long id) {
        GiftCardDetailVO detail = giftCardService.getGiftCardDetail(id);
        return ObjectUtil.isNotNull(detail) ? R.ok(detail) : R.fail("购物卡不存在或已被删除");
    }
}
