package com.czg.controller.admin;

import com.czg.account.dto.points.OrderDeductionPointsDTO;
import com.czg.account.entity.MemberPoints;
import com.czg.account.param.ConsumeAwardPointsParam;
import com.czg.account.param.MemberPointsParam;
import com.czg.account.param.PayedDeductPointsParam;
import com.czg.account.service.MemberPointsService;
import com.czg.log.annotation.OperationLog;
import com.czg.resp.CzgResult;
import com.mybatisflex.core.paginate.Page;
import lombok.AllArgsConstructor;
import org.springframework.web.bind.annotation.*;

import java.math.BigDecimal;


/**
 * 会员积分
 *
 * @author Tankaikai tankaikai@aliyun.com
 * @since 1.0 2025-02-25
 */
@AllArgsConstructor
@RestController
@RequestMapping("/admin/points/memberPoints")
public class MemberPointsController {

    private final MemberPointsService memberPointsService;

    /**
     * 分页
     */
    @GetMapping("page")
    @OperationLog("会员积分-分页")
    //@SaAdminCheckPermission("memberPoints:page")
    public CzgResult<Page<MemberPoints>> getMemberPointsPage(MemberPointsParam param) {
        Page<MemberPoints> page = memberPointsService.getMemberPointsPage(param);
        return CzgResult.success(page);
    }

    /**
     * 001-会员积分账户信息
     */
    @GetMapping("userPoints")
    public CzgResult<MemberPoints> getMemberPoints(@RequestParam Long shopUserId) {
        MemberPoints data = memberPointsService.getMemberPoints(shopUserId);
        return CzgResult.success(data);
    }

    /**
     * 002-获取订单可用积分及抵扣金额（支付页面使用）
     *
     * @param shopUserId      用户id
     * @param orderAmount 订单金额
     */
    @GetMapping("calcUsablePoints")
    public CzgResult<OrderDeductionPointsDTO> getMemberUsablePoints(@RequestParam Long shopUserId, @RequestParam BigDecimal orderAmount) {
        OrderDeductionPointsDTO usablePoints = memberPointsService.getMemberUsablePoints(shopUserId, orderAmount);
        return CzgResult.success(usablePoints);
    }

    /**
     * 004-根据抵扣金额计算所需积分
     *
     * @param shopUserId          用户id
     * @param orderAmount     订单金额
     * @param deductionAmount 抵扣金额
     */
    @GetMapping("calcUsedPoints")
    public CzgResult<Integer> calcUsedPoints(@RequestParam Long shopUserId, @RequestParam BigDecimal orderAmount, @RequestParam BigDecimal deductionAmount) {
        int points = memberPointsService.calcUsedPoints(shopUserId, orderAmount, deductionAmount);
        return CzgResult.success(points);
    }

    /**
     * 003-根据积分计算可抵扣金额
     */
    @GetMapping("calcDeductionAmount")
    public CzgResult<BigDecimal> calcDeductionAmount(@RequestParam Long shopUserId, @RequestParam BigDecimal orderAmount, @RequestParam Integer points) {
        BigDecimal deductionAmount = memberPointsService.calcDeductionAmount(shopUserId, orderAmount, points);
        return CzgResult.success(deductionAmount);
    }

    /**
     * 005-支付完成扣减积分（支付成功回调中使用）
     */
    @PostMapping("payedDeductPoints")
    public CzgResult<Boolean> deductPoints(@RequestBody PayedDeductPointsParam param) {
        //boolean ret = memberPointsService.deductPoints(param.getUserId(), param.getPoints(), param.getContent(), param.getOrderId());
        return CzgResult.success();
    }

    /**
     * 006-消费赠送积分（支付成功回调中使用）
     */
    @PostMapping("consumeAwardPoints")
    public CzgResult<Void> consumeAwardPoints(@RequestBody ConsumeAwardPointsParam param) {
        //memberPointsService.consumeAwardPoints(param.getUserId(), param.getOrderId());
        return CzgResult.success();
    }

}