package com.tangguo.service.impl;

import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.tangguo.common.exception.ServiceException;
import com.tangguo.common.utils.SecurityUtils;
import com.tangguo.domain.BbsUserPoints;
import com.tangguo.domain.BbsUserPointsDetail;
import com.tangguo.domain.dto.PointsDetail;
import com.tangguo.domain.vo.QwmhSysUserVO;
import com.tangguo.mapper.BbsUserPointsMapper;
import com.tangguo.service.IBbsUserPointsDetailService;
import com.tangguo.service.IBbsUserPointsService;
import org.springframework.security.core.parameters.P;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.List;
import java.util.Objects;

/**
 * 用户积分Service业务层处理
 *
 * @author ruoyi
 * @date 2025-08-29
 */
@Service
public class BbsUserPointsServiceImpl extends ServiceImpl<BbsUserPointsMapper, BbsUserPoints> implements IBbsUserPointsService {

    @Resource
    private BbsUserPointsMapper bbsUserPointsMapper;

    @Resource
    private IBbsUserPointsDetailService pointsDetailService;

    @Resource
    private BbsUserPointsServiceImpl userPointsServiceImpl;


    /**
     * 查询用户积分列表
     *
     * @param points 用户积分
     * @return 用户积分
     */
    @Override
    public List<BbsUserPoints> selectBbsUserPointsList(BbsUserPoints points) {
        return this.baseMapper.selectBbsUserPointsList(points);
    }


    /**
     * 查询用户积分
     *
     * @param userName 用户名
     * @return 积分
     */
    @Transactional(rollbackFor = Exception.class, propagation = Propagation.REQUIRES_NEW)
    @Override
    public BbsUserPoints getUserPoints(String userName) {
        BbsUserPoints dbUserPoints = this.baseMapper.selectUserPoints(userName);
        if (Objects.isNull(dbUserPoints)) {
            QwmhSysUserVO qwmhSysUser = this.baseMapper.selectQwmhUser(userName);
            if (Objects.nonNull(qwmhSysUser)) {
                dbUserPoints = new BbsUserPoints();
                dbUserPoints.setUserName(userName);
                dbUserPoints.setAccumulatedPoints(0);
                dbUserPoints.setCurrentPoints(0);
                this.save(dbUserPoints);
            } else {
                throw new ServiceException("查询用户积分失败，未查询到当前用户数据。");
            }
        }
        return dbUserPoints;
    }


    /**
     * 增加用户积分
     *
     * @param points 积分
     */
    @Transactional(rollbackFor = Exception.class)
    @Override
    public void addUserPoints(BbsUserPoints points) {
        String userName = points.getUserName();
        PointsDetail detail = new PointsDetail();
        detail.setUserName(userName);
        detail.setDetailPoints(points.getIncrOrDecrPoints());
        detail.setDetailName("后台手动增加");
        detail.setDescription(String.format("管理员【%s】后台手动增加用户【%s】积分", SecurityUtils.getUsername(), userName));
        this.incrUserPoints(detail);
    }


    /**
     * 扣减用户积分
     *
     * @param points 积分
     */
    @Transactional(rollbackFor = Exception.class)
    @Override
    public void deleteUserPoints(BbsUserPoints points) {
        String userName = points.getUserName();
        PointsDetail detail = new PointsDetail();
        detail.setUserName(userName);
        detail.setDetailPoints(points.getIncrOrDecrPoints());
        detail.setDetailName("后台手动扣减");
        detail.setDescription(String.format("管理员【%s】后台手动扣减用户【%s】积分", SecurityUtils.getUsername(), userName));
        this.decrUserPoints(detail);
    }


    /**
     * 增加用户积分
     *
     * @param detail   积分明细
     */
    @Override
    public void incrUserPoints(PointsDetail detail) {
        synchronized (detail.getUserName().intern()) {
            this.userPointsServiceImpl.privIncrUserPoints(detail);
        }
    }


    /**
     * 扣减用户积分
     *
     * @param detail   积分明细
     */
    @Override
    public void decrUserPoints(PointsDetail detail) {
        synchronized (detail.getUserName().intern()) {
            this.userPointsServiceImpl.privDecrUserPoints(detail);
        }
    }


    /**
     * 增加用户积分
     *
     * @param detail   积分明细
     */
    @Transactional(rollbackFor = Exception.class)
    public void privIncrUserPoints(PointsDetail detail) {
        int points = detail.getDetailPoints();
        if (points < 1) {
            throw new ServiceException("增加用户积分失败，增加的积分分值不能小于0。");
        }

        // 更新用户积分
        String userName = detail.getUserName();
        BbsUserPoints userPoints = this.userPointsServiceImpl.getUserPoints(userName);
        Integer currentPoints = userPoints.getCurrentPoints();
        int incrCurrentPoints = currentPoints + points;
        userPoints.setCurrentPoints(incrCurrentPoints);
        userPoints.setAccumulatedPoints(userPoints.getAccumulatedPoints() + points);
        this.updateById(userPoints);

        // 添加用户积分明细
        BbsUserPointsDetail newDetail = new BbsUserPointsDetail();
        newDetail.setUserName(userName);
        newDetail.setDetailName(detail.getDetailName());
        newDetail.setDetailCode(detail.getDetailCode());
        newDetail.setDetailPoints(points);
        newDetail.setBeforePoints(currentPoints);
        newDetail.setAfterPoints(incrCurrentPoints);
        newDetail.setDescription(detail.getDescription());
        this.pointsDetailService.save(newDetail);
    }


    /**
     * 扣减用户积分
     *
     * @param detail   积分明细
     */
    @Transactional(rollbackFor = Exception.class)
    public void privDecrUserPoints(PointsDetail detail) {
        int points = detail.getDetailPoints();
        if (points < 1) {
            throw new ServiceException("扣减用户积分失败，扣减的积分分值不能小于0。");
        }

        // 更新用户积分
        String userName = detail.getUserName();
        BbsUserPoints userPoints = this.userPointsServiceImpl.getUserPoints(userName);
        Integer currentPoints = userPoints.getCurrentPoints();
        if (points > currentPoints) {
            throw new ServiceException("扣减用户积分失败，当前用户可用积分不足。");
        }

        int decrCurrentPoints = currentPoints - points;
        userPoints.setCurrentPoints(decrCurrentPoints);
        this.updateById(userPoints);

        // 添加用户积分明细
        BbsUserPointsDetail newDetail = new BbsUserPointsDetail();
        newDetail.setUserName(userName);
        newDetail.setDetailName(detail.getDetailName());
        newDetail.setDetailCode(detail.getDetailCode());
        newDetail.setDetailPoints(points);
        newDetail.setBeforePoints(currentPoints);
        newDetail.setAfterPoints(decrCurrentPoints);
        newDetail.setDescription(detail.getDescription());
        this.pointsDetailService.save(newDetail);
    }

}
