package com.seer.teach.pay.service.impl;

import cn.dev33.satoken.stp.StpUtil;
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.seer.teach.pay.mapper.CoinRechargeMapper;
import com.seer.teach.pay.entity.CoinRechargeEntity;
import com.seer.teach.pay.service.ICoinRechargeService;
import org.springframework.stereotype.Service;

import java.util.Objects;

/**
 * <p>
 *  学豆充值记录服务实现类
 * </p>
 *
 * @author Captain
 * @since 2025-06-17
 */
@Service
public class CoinRechargeServiceImpl extends ServiceImpl<CoinRechargeMapper, CoinRechargeEntity> implements ICoinRechargeService {

    @Override
    public CoinRechargeEntity myRechargeDetail(Integer rechargeId) {
        int userId = StpUtil.getLoginIdAsInt();
        CoinRechargeEntity coinRecharge = super.getOne(new LambdaQueryWrapper<CoinRechargeEntity>()
                .eq(CoinRechargeEntity::getUserId, userId)
                .eq(CoinRechargeEntity::getId, rechargeId));
        return coinRecharge;
    }

    @Override
    public IPage<CoinRechargeEntity> myRechargeList(Integer size, Integer current, Integer payStatus) {
        int userId = StpUtil.getLoginIdAsInt();
        IPage<CoinRechargeEntity> pageParams = new Page<>(current, size);
        IPage<CoinRechargeEntity> iPage = super.page(pageParams, new LambdaQueryWrapper<CoinRechargeEntity>()
                .eq(Objects.nonNull(payStatus), CoinRechargeEntity::getPayStatus, payStatus)
                .eq(CoinRechargeEntity::getUserId, userId).orderByDesc(CoinRechargeEntity::getRechargeTime));
        return iPage;
    }

    @Override
    public CoinRechargeEntity getOneByRechargeSn(String rechargeSn) {
        return super.getOne(new LambdaQueryWrapper<CoinRechargeEntity>().eq(CoinRechargeEntity::getRechargeSn, rechargeSn));
    }
}
