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

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.seer.teach.common.PageListBean;
import com.seer.teach.common.utils.PageConverterUtils;
import com.seer.teach.pay.admin.recharge.controller.resp.CoinRechargeDetailListResp;
import com.seer.teach.pay.admin.recharge.convert.CoinRechargeConvert;
import com.seer.teach.pay.admin.recharge.controller.resp.CoinRechargeResp;
import com.seer.teach.pay.admin.recharge.service.IAdminCoinRechargeService;
import com.seer.teach.pay.entity.CoinRechargeEntity;
import com.seer.teach.pay.mapper.CoinRechargeMapper;
import com.seer.teach.user.api.UserInfoServiceApi;
import com.seer.teach.user.api.dto.UserInfoDTO;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import java.util.Collections;
import java.util.List;
import java.util.Objects;

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

    private final UserInfoServiceApi userInfoServiceApi;

    @Override
    public PageListBean<CoinRechargeResp> getAllRechargeRecords(Integer size, Integer current, Integer playStatus,String rechargeSn,Integer userId) {
        IPage<CoinRechargeEntity> pageParams = new Page<>(current, size);
        IPage<CoinRechargeEntity> iPage = super.page(pageParams, new LambdaQueryWrapper<>(CoinRechargeEntity.class)
                .eq(Objects.nonNull(playStatus),CoinRechargeEntity::getPayStatus, playStatus)
                .eq(Objects.nonNull(userId),CoinRechargeEntity::getUserId, userId)
                .like(StringUtils.isNotBlank(rechargeSn),CoinRechargeEntity::getRechargeSn, rechargeSn)
                .orderByDesc(CoinRechargeEntity::getId));
        return PageConverterUtils.convertPageListBean(iPage, CoinRechargeConvert.INSTANCE::convertList);
    }

    @Override
    public CoinRechargeResp getRechargeRecordDetail(Integer id) {
        CoinRechargeEntity coinRechargeEntity = super.getById(id);

        UserInfoDTO userInfoDTO = userInfoServiceApi.getById(coinRechargeEntity.getUserId());

        return CoinRechargeConvert.INSTANCE.convertOne(coinRechargeEntity,userInfoDTO);
    }
}