package com.ruoyi.member.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ruoyi.common.config.DlRightsConfig;
import com.ruoyi.common.core.domain.DlBaseEntity;
import com.ruoyi.member.domain.MemberPoints;
import com.ruoyi.member.mapper.MemberPointsMapper;
import com.ruoyi.member.service.IMemberPointsService;
import io.lettuce.core.LMoveArgs;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * 博主积分变动明细Service业务层处理
 *
 * @author pqz
 * @date 2025-03-17
 */
@Service
public class MemberPointsServiceImpl extends ServiceImpl<MemberPointsMapper, MemberPoints> implements IMemberPointsService {
    @Autowired
    private MemberPointsMapper memberPointsMapper;


    /**
     * 分页查询积分变动明细
     *
     * @param userId 用户id
     * @param page   分页参数
     * @return com.baomidou.mybatisplus.core.metadata.IPage<com.ruoyi.member.domain.MemberPoints>
     * @author PQZ
     * @date 15:24 2025/3/29
     **/
    @Override
    public IPage<MemberPoints> queryListPage(Long userId, Page<MemberPoints> page) {
        return memberPointsMapper.queryListPage(userId,page);
    }

    /**
     * 新增积分变动记录
     *
     * @param memberPoints {@link MemberPoints}
     * @author PQZ
     * @date 15:29 2025/3/29
     **/
    @Override
    public void savePoints(MemberPoints memberPoints) {
        //查询当前用户最近变动记录
        LambdaQueryWrapper<MemberPoints> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.eq(DlBaseEntity::getDelFlag,0)
                .eq(MemberPoints::getUserId,memberPoints.getUserId())
                .orderByDesc(DlBaseEntity::getCreateTime);
        List<MemberPoints> list = list(lambdaQueryWrapper);
        //如果没有记录则为初始新增
        if (list.isEmpty()) {
            memberPoints.setBalance(memberPoints.getPoints());
        } else {
            //如果有记录则取最新记录中的剩余值进行计算后得出新值
            Integer oldBalance = list.get(0).getBalance();
            Integer newBalance;
            if ("1".equals(memberPoints.getType())) {
                newBalance = oldBalance + memberPoints.getPoints();
            } else {
                newBalance = oldBalance - memberPoints.getPoints();
            }
            memberPoints.setBalance(newBalance);
        }
        save(memberPoints);
    }

    /**
     * 获取积分
     *
     * @param userId 用户id
     * @return java.lang.Integer
     * @author PQZ
     * @date 16:11 2025/3/31
     **/
    @Override
    public Integer getBloggerBalance(Long userId) {
        LambdaQueryWrapper<MemberPoints> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper
                .eq(MemberPoints::getUserId,userId)
                .eq(DlBaseEntity::getDelFlag,0)
                .orderByDesc(DlBaseEntity::getCreateTime);
        List<MemberPoints> list = list(lambdaQueryWrapper);
        if (list.isEmpty()){
            return 0;
        } else {
            return list.get(0).getBalance();
        }
    }
}
