package com.zanxiang.game.module.manage.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.CollectionUtils;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.zanxiang.erp.security.util.SecurityUtil;
import com.zanxiang.game.module.manage.pojo.dto.AgentDTO;
import com.zanxiang.game.module.manage.pojo.dto.GameDTO;
import com.zanxiang.game.module.manage.pojo.params.GameRemitLogListParam;
import com.zanxiang.game.module.manage.pojo.vo.GameRemitLogVO;
import com.zanxiang.game.module.manage.service.IAgentService;
import com.zanxiang.game.module.manage.service.IGameAuthService;
import com.zanxiang.game.module.manage.service.IGameRemitLogService;
import com.zanxiang.game.module.manage.service.IGameService;
import com.zanxiang.game.module.mybatis.entity.GameRemitLog;
import com.zanxiang.game.module.mybatis.mapper.GameRemitLogMapper;
import com.zanxiang.module.util.bean.BeanUtil;
import lombok.extern.slf4j.Slf4j;
import org.apache.logging.log4j.util.Strings;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import reactor.util.function.Tuple2;

import java.time.LocalDateTime;
import java.time.LocalTime;
import java.util.List;
import java.util.Objects;

/**
 * @author : lingfeng
 * @time : 2023-07-17
 * @description : 游戏提现记录
 */
@Slf4j
@Service
public class GameRemitLogServiceImpl extends ServiceImpl<GameRemitLogMapper, GameRemitLog> implements IGameRemitLogService {

    @Autowired
    private IGameService gameService;

    @Autowired
    private IAgentService agentService;

    @Autowired
    private IGameAuthService gameAuthService;

    @Override
    public IPage<GameRemitLogVO> listOfPage(GameRemitLogListParam param) {
        //游戏获取
        Tuple2<String, List<Long>> gameTuple = gameAuthService.getUserGameList(null);
        List<Long> gameIdList = gameTuple.getT2();
        if (!SecurityUtil.isAdmin() && CollectionUtils.isEmpty(gameIdList)) {
            return new Page<>();
        }
        //渠道获取
        Tuple2<List<Long>, List<AgentDTO>> tuple2 = agentService.getUserAgent(null, null, null);
        List<Long> agentIdList = tuple2.getT1();
        if (CollectionUtils.isEmpty(agentIdList)) {
            return new Page<>();
        }
        //查询
        return page(param.toPage(), new QueryWrapper<GameRemitLog>().lambda()
                .in(!SecurityUtil.isAdmin(), GameRemitLog::getGameId, gameIdList)
                .in(CollectionUtils.isNotEmpty(agentIdList), GameRemitLog::getAgentId, agentIdList)
                .eq(param.getUserId() != null, GameRemitLog::getUserId, param.getUserId())
                .eq(param.getGameId() != null, GameRemitLog::getGameId, param.getGameId())
                .eq(Strings.isNotBlank(param.getMerchantOrderNo()), GameRemitLog::getMerchantOrderNo, param.getMerchantOrderNo())
                .ge(param.getStartTime() != null, GameRemitLog::getCreatedTime, param.getStartTime() == null ? null : LocalDateTime.of(param.getStartTime(), LocalTime.MIN))
                .le(param.getEndTime() != null, GameRemitLog::getCreatedTime, param.getEndTime() == null ? null : LocalDateTime.of(param.getEndTime(), LocalTime.MAX))
                .orderByDesc(GameRemitLog::getCreatedTime)
        ).convert(this::toVo);
    }

    private GameRemitLogVO toVo(GameRemitLog gameRemitLog) {
        if (Objects.isNull(gameRemitLog)) {
            return null;
        }
        GameRemitLogVO gameRemitLogVO = BeanUtil.copy(gameRemitLog, GameRemitLogVO.class);
        GameDTO gameDTO = gameService.getById(gameRemitLog.getGameId());
        gameRemitLogVO.setGameName(gameDTO == null ? null : gameDTO.getName());
        return gameRemitLogVO;
    }
}
