package org.xyzh.usercenter.controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.xyzh.common.core.domain.ResultDomain;
import org.xyzh.common.dto.usercenter.TbUserPoints;
import org.xyzh.common.dto.usercenter.TbPointsRecord;
import org.xyzh.usercenter.service.UCUserPointsService;

/**
 * @description 用户积分控制器
 * @filename UserPointsController.java
 * @author yslg
 * @copyright xyzh
 * @since 2025-10-15
 */
@RestController
@RequestMapping("/usercenter/points")
public class UserPointsController {

    @Autowired
    private UCUserPointsService userPointsService;

    /**
     * 获取用户积分信息
     */
    @GetMapping("/user/{userID}")
    public ResultDomain<TbUserPoints> getUserPoints(@PathVariable String userID) {
        return userPointsService.getUserPoints(userID);
    }

    /**
     * 更新用户积分
     */
    @PutMapping("/update")
    public ResultDomain<TbUserPoints> updateUserPoints(@RequestBody TbUserPoints userPoints) {
        return userPointsService.updateUserPoints(userPoints);
    }

    /**
     * 增加用户积分
     */
    @PostMapping("/add")
    public ResultDomain<TbUserPoints> addUserPoints(
            @RequestParam String userID,
            @RequestParam Integer points,
            @RequestParam Integer sourceType,
            @RequestParam String sourceID,
            @RequestParam(required = false) String description) {
        return userPointsService.addUserPoints(userID, points, sourceType, sourceID, description);
    }

    /**
     * 消费用户积分
     */
    @PostMapping("/consume")
    public ResultDomain<TbUserPoints> consumeUserPoints(
            @RequestParam String userID,
            @RequestParam Integer points,
            @RequestParam Integer sourceType,
            @RequestParam String sourceID,
            @RequestParam(required = false) String description) {
        return userPointsService.consumeUserPoints(userID, points, sourceType, sourceID, description);
    }

    /**
     * 获取用户积分记录
     */
    @GetMapping("/records/{userID}")
    public ResultDomain<TbPointsRecord> getPointsRecords(
            @PathVariable String userID,
            @RequestParam(required = false) Integer type,
            @RequestParam(required = false) Integer sourceType) {
        return userPointsService.getPointsRecords(userID, type, sourceType);
    }

    /**
     * 添加积分记录
     */
    @PostMapping("/record")
    public ResultDomain<TbPointsRecord> addPointsRecord(@RequestBody TbPointsRecord pointsRecord) {
        return userPointsService.addPointsRecord(pointsRecord);
    }
}
