package com.czg.controller.admin;

import com.czg.account.vo.PointsExchangeSummaryVo;
import com.czg.annotation.SaAdminCheckPermission;
import com.czg.log.annotation.OperationLog;
import com.czg.market.dto.MkPointsGoodsRecordDTO;
import com.czg.market.dto.MkPointsGoodsRecordQueryDTO;
import com.czg.market.service.MkPointsGoodsRecordService;
import com.czg.order.dto.CommonRefundDTO;
import com.czg.order.service.PointsGoodPayService;
import com.czg.resp.CzgResult;
import com.czg.sa.StpKit;
import com.mybatisflex.core.paginate.Page;
import jakarta.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;


/**
 * 积分商品
 *
 * @author ww
 */
@RestController
@RequestMapping("/admin/points/goodsRecord")
public class PointsGoodsRecordController {

    @Resource
    private MkPointsGoodsRecordService goodsRecordService;
    @Resource
    private PointsGoodPayService goodPayService;

    /**
     * 积分:积分商品:兑换记录
     */
    @GetMapping("page")
    @SaAdminCheckPermission(parentName = "积分相关", value = "points:goodsRecord:list", name = "积分-积分商品-兑换记录")
    public CzgResult<Page<MkPointsGoodsRecordDTO>> getGoodsRecordPage(MkPointsGoodsRecordQueryDTO param) {
        param.setShopId(StpKit.USER.getShopId());
        Page<MkPointsGoodsRecordDTO> page = goodsRecordService.getGoodsRecordPage(param);
        return CzgResult.success(page);
    }

    /**
     * 积分:积分商品:统计
     */
    @GetMapping("total")
    @SaAdminCheckPermission(parentName = "积分相关", value = "points:goodsRecord:total", name = "积分-积分商品-统计")
    public CzgResult<PointsExchangeSummaryVo> total(MkPointsGoodsRecordQueryDTO param) {
        param.setShopId(StpKit.USER.getShopId());
        PointsExchangeSummaryVo data = goodsRecordService.total(param);
        return CzgResult.success(data);
    }

    /**
     * 核销
     *
     * @param couponCode 兑换码
     */
    @PostMapping("checkout")
    @OperationLog("积分兑换-核销")
    @SaAdminCheckPermission(parentName = "积分相关", value = "points:goodsRecord:checkout", name = "积分-积分商品-核销")
    public CzgResult<Boolean> checkout(@RequestBody String couponCode) {
        return CzgResult.success(goodsRecordService.checkout(couponCode, StpKit.USER.getShopId()));
    }

    /**
     * 退单/同意退单
     */
    @PostMapping("/agreeRefund")
    public CzgResult<Boolean> agreeRefund(@RequestBody @Validated CommonRefundDTO param) {
        return CzgResult.success(goodPayService.agreeRefund(param, StpKit.USER.getShopId()));
    }

    /**
     * 驳回退单
     */
    @PostMapping("/rejectRefund")
    public CzgResult<Boolean> rejectRefund(@RequestBody @Validated CommonRefundDTO param) {
        return CzgResult.success(goodPayService.cancelRefund(param, null, StpKit.USER.getShopId()));
    }

}