package com.czg.controller.user;

import com.czg.account.dto.points.PointsExchangeRecordDTO;
import com.czg.account.dto.points.PointsGoodsSettingDTO;
import com.czg.account.param.PointsExchangeCfParam;
import com.czg.account.param.PointsExchangeRecordParam;
import com.czg.account.param.PointsOrderCreateParam;
import com.czg.account.param.PointsOrderPayParam;
import com.czg.account.service.PointsExchangeRecordService;
import com.czg.account.service.PointsGoodsSettingService;
import com.czg.resp.CzgResult;
import com.czg.sa.StpKit;
import com.mybatisflex.core.paginate.Page;
import lombok.AllArgsConstructor;
import org.springframework.web.bind.annotation.*;


/**
 * 积分商城
 *
 * @author Tankaikai tankaikai@aliyun.com
 * @since 1.0 2025-02-25
 */
@AllArgsConstructor
@RestController
@RequestMapping("/user/points/mall")
public class UPointsMallController {

    private final PointsGoodsSettingService pointsGoodsSettingService;
    private final PointsExchangeRecordService pointsExchangeRecordService;

    /**
     * 商品列表
     */
    @GetMapping("/goods/page")
    public CzgResult<Page<PointsGoodsSettingDTO>> getPointsGoodsSettingPage(PointsGoodsSettingDTO param) {
        Page<PointsGoodsSettingDTO> data = pointsGoodsSettingService.getPointsGoodsSettingPage(param);
        return CzgResult.success(data);
    }

    /**
     * 生成订单
     */
    @PostMapping("/order/create")
    public CzgResult<PointsExchangeRecordDTO> create(@RequestBody PointsOrderCreateParam param) {
        PointsExchangeRecordDTO data = pointsExchangeRecordService.create(param);
        return CzgResult.success(data);
    }

    /**
     * 取消订单
     */
    @PostMapping("/order/cancel")
    public CzgResult<Void> cancel(@RequestBody PointsExchangeCfParam param) {
        pointsExchangeRecordService.cancel(param);
        return CzgResult.success();
    }

    /**
     * 支付订单
     */
    @PostMapping("/order/pay")
    public CzgResult<PointsExchangeRecordDTO> pay(@RequestBody PointsOrderPayParam param) {
        PointsExchangeRecordDTO data = pointsExchangeRecordService.pay(param);
        return CzgResult.success(data);
    }

    /**
     * 申请退单
     */
    @PostMapping("/order/refund")
    public CzgResult<Void> refund(@RequestBody PointsExchangeCfParam param) {
        pointsExchangeRecordService.refund(param);
        return CzgResult.success();
    }

    /**
     * 我的兑换记录
     */
    @GetMapping("/order/page")
    public CzgResult<Page<PointsExchangeRecordDTO>> getPointsExchangeRecordPage(PointsExchangeRecordParam param) {
        param.setShopUserId(StpKit.USER.getLoginIdAsLong());
        Page<PointsExchangeRecordDTO> page = pointsExchangeRecordService.getPointsExchangeRecordPage(param);
        return CzgResult.success(page);
    }
}