package com.czg.controller.user;

import cn.hutool.core.bean.BeanUtil;
import com.czg.account.dto.points.OrderDeductionPointsDTO;
import com.czg.account.dto.points.PointsBasicSettingDTO;
import com.czg.account.entity.MemberPoints;
import com.czg.account.entity.PointsBasicSetting;
import com.czg.account.param.ConsumeAwardPointsParam;
import com.czg.account.param.PayedDeductPointsParam;
import com.czg.account.service.MemberPointsService;
import com.czg.account.service.PointsBasicSettingService;
import com.czg.resp.CzgResult;
import com.czg.sa.StpKit;
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("/user/points/memberPoints")
public class UMemberPointsController {

    private final MemberPointsService memberPointsService;

    private final PointsBasicSettingService pointsBasicSettingService;

    /**
     * 000-积分基本设置详情
     */
    @GetMapping("pointsBasicSetting")
    public CzgResult<PointsBasicSettingDTO> getPointsBasicSetting() {
        Long shopId = StpKit.USER.getShopId(0L);
        PointsBasicSetting entity = pointsBasicSettingService.getById(shopId);
        PointsBasicSettingDTO data = BeanUtil.copyProperties(entity, PointsBasicSettingDTO.class);
        return CzgResult.success(data);
    }

    /**
     * 001-会员积分账户信息
     */
    @GetMapping("myPoints")
    public CzgResult<MemberPoints> getMemberPoints() {
        long shopUserId = StpKit.USER.getLoginIdAsLong();
        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-根据积分计算可抵扣金额
     *
     * @param shopUserId  用户id
     * @param orderAmount 订单金额
     * @param points      使用积分
     */
    @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();
    }
}
