package cn.ysk.cashier.controller.points;

import cn.hutool.core.map.MapProxy;
import cn.ysk.cashier.dto.points.OrderDeductionPointsDTO;
import cn.ysk.cashier.mybatis.entity.TbMemberPoints;
import cn.ysk.cashier.mybatis.service.TbMemberPointsService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

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


/**
 * 会员积分
 *
 * @author Tankaikai tankaikai@aliyun.com
 * @since 2.0 2024-10-25
 */
@RestController
@RequestMapping("/api/points/member-points")
@Api(tags = "会员积分")
public class TbMemberPointsController {

    @Resource
    private TbMemberPointsService tbMemberPointsService;

    @GetMapping("page")
    @ApiOperation("分页")
    public ResponseEntity page(@RequestParam Map<String, Object> params) {
        Map<String, Object> page = tbMemberPointsService.page(params);
        return ResponseEntity.ok().body(page);
    }

    @GetMapping("{memberId}")
    @ApiOperation("获取会员积分等信息")
    public ResponseEntity getMemberPoints(@PathVariable("memberId") Long memberId) {
        TbMemberPoints memberPoints = tbMemberPointsService.getMemberPoints(memberId);
        return ResponseEntity.ok().body(memberPoints);
    }

    @GetMapping("calc-usable-points")
    @ApiOperation("获取订单可用积分及抵扣金额")
    public ResponseEntity getMemberUsablePoints(@RequestParam Long memberId, @RequestParam BigDecimal orderAmount) {
        OrderDeductionPointsDTO usablePoints = tbMemberPointsService.getMemberUsablePoints(memberId, orderAmount);
        return ResponseEntity.ok().body(usablePoints);
    }

    @GetMapping("calc-used-points")
    @ApiOperation("根据抵扣金额计算所需积分")
    public ResponseEntity calcUsedPoints(@RequestParam Long memberId, @RequestParam BigDecimal orderAmount, @RequestParam BigDecimal deductionAmount) {
        int points = tbMemberPointsService.calcUsedPoints(memberId, orderAmount, deductionAmount);
        return ResponseEntity.ok().body(points);
    }

    @GetMapping("calc-deduction-amount")
    @ApiOperation("根据积分计算可抵扣金额")
    public ResponseEntity calcDeductionAmount(@RequestParam Long memberId, @RequestParam BigDecimal orderAmount, @RequestParam Integer points) {
        BigDecimal deductionAmount = tbMemberPointsService.calcDeductionAmount(memberId, orderAmount, points);
        return ResponseEntity.ok().body(deductionAmount);
    }

    @PostMapping("payed-deduct-points")
    @ApiOperation("支付完成扣减积分（支付成功回调中使用）")
    public ResponseEntity deductPoints(@RequestBody Map<String, Object> params) {
        MapProxy proxy = MapProxy.create(params);
        Long memberId = proxy.getLong("memberId");
        Integer points = proxy.getInt("points");
        String content = proxy.getStr("content");
        Long orderId = proxy.getLong("orderId");
        boolean ret = tbMemberPointsService.deductPoints(memberId, points, content, orderId);
        return ResponseEntity.ok().body(ret);
    }

    @PostMapping("consume-award-points")
    @ApiOperation("消费赠送积分（支付成功回调中使用）")
    public ResponseEntity consumeAwardPoints(@RequestBody Map<String, Object> params) {
        MapProxy proxy = MapProxy.create(params);
        Long memberId = proxy.getLong("memberId");
        Long orderId = proxy.getLong("orderId");
        tbMemberPointsService.consumeAwardPoints(memberId, orderId);
        return ResponseEntity.ok().build();
    }

}