package com.red.circle.other.infra.database.rds.service.lottery;

import com.alibaba.nacos.common.utils.StringUtils;
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.red.circle.other.infra.database.rds.dao.activity.LotteryBackpackLogDAO;
import com.red.circle.other.infra.database.rds.entity.LotteryBackpackLog;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

/**
 * 抽奖背包流水数据库服务
 */
@Service
@RequiredArgsConstructor
@Slf4j
public class LotteryBackpackLogDatabaseService {

    private final LotteryBackpackLogDAO lotteryBackpackLogDAO;

    /**
     * 保存流水记录
     */
    public void save(LotteryBackpackLog log) {
        try {
            lotteryBackpackLogDAO.insert(log);
        } catch (Exception e) {

        }
    }

    /**
     * 分页查询流水
     */
    public IPage<LotteryBackpackLog> queryPage(Long userId, String itemType, Integer changeType,
                                               String source, Integer pageNum, Integer pageSize) {
        LambdaQueryWrapper<LotteryBackpackLog> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(LotteryBackpackLog::getUserId, userId);

        if (StringUtils.hasText(itemType)) {
            wrapper.eq(LotteryBackpackLog::getItemType, itemType);
        }

        if (changeType != null) {
            wrapper.eq(LotteryBackpackLog::getChangeType, changeType);
        }

        if (StringUtils.hasText(source)) {
            wrapper.eq(LotteryBackpackLog::getSource, source);
        }

        wrapper.orderByDesc(LotteryBackpackLog::getCreatedAt);

        Page<LotteryBackpackLog> page = new Page<>(pageNum, pageSize);
        return lotteryBackpackLogDAO.selectPage(page, wrapper);
    }

}