package com.ruoyi.app.user;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.ruoyi.app.user.dto.PointOutput;
import com.ruoyi.common.annotation.Anonymous;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.R;
import com.ruoyi.common.utils.MessageUtils;
import com.ruoyi.system.domain.SysPointControl;
import com.ruoyi.system.domain.SysPointTier;
import com.ruoyi.system.domain.UserWallet;
import com.ruoyi.system.service.ISysPointControlService;
import com.ruoyi.system.service.ISysPointTierService;
import com.ruoyi.system.service.IUserWalletService;
import com.ruoyi.system.utils.Auth;
import com.ruoyi.system.utils.JwtUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestHeader;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.math.BigDecimal;

/**
 * 用户积分
 * Controller
 *
 * @author ruoyi
 * @date 2025-05-09
 */
@Api("用户积分")
@RestController
@RequestMapping("/system/userPoint")
public class UserPointController extends BaseController {
    @Autowired
    private ISysPointControlService pointControlService;
    @Autowired
    private ISysPointTierService    pointTierService;
    @Autowired
    private IUserWalletService userWalletService;
    @Anonymous
    @Auth
    @ApiOperation("下单返回用户积分信息")
    @RequestMapping("/getPoint")
    public R<PointOutput> getPoint(@RequestHeader String token) {
        JwtUtil jwtUtil = new JwtUtil();
        String uid = jwtUtil.getusid(token);
        SysPointControl pointControl = pointControlService.getOne(new LambdaQueryWrapper<SysPointControl>().eq(SysPointControl::getId, 1L));
        //未开启下单使用积分
        if (pointControl.getOrderUseEnable().equals(1L)) {
            UserWallet wallet = userWalletService.selectByUserId(Long.valueOf(uid));
            if(wallet!=null) {
                Long point = wallet.getPointsWallet();
                PointOutput pointOutput = new PointOutput();
                pointOutput.setPoints(BigDecimal.valueOf(point));
                pointOutput.setBl(pointControl.getVndPerpoint());
                return R.ok(pointOutput);
            }else{
                PointOutput pointOutput = new PointOutput();
                pointOutput.setPoints(BigDecimal.ZERO);
                pointOutput.setBl(pointControl.getVndPerpoint());
            }
        }
        return R.fail(MessageUtils.message("no.points.use.fail"));

    }

}
