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

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
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.base.pojo.enums.GameAuthEnum;
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.dto.UserDTO;
import com.zanxiang.game.module.manage.pojo.params.UserNameAuthListParam;
import com.zanxiang.game.module.manage.pojo.vo.UserCardVO;
import com.zanxiang.game.module.manage.service.*;
import com.zanxiang.game.module.mybatis.entity.GameUserRole;
import com.zanxiang.game.module.mybatis.entity.UserCard;
import com.zanxiang.game.module.mybatis.mapper.UserCardMapper;
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 : 2022-09-26
 * @description : 用户实名认证
 */
@Slf4j
@Service
public class UserCardServiceImpl extends ServiceImpl<UserCardMapper, UserCard> implements IUserCardService {

    @Autowired
    private IUserService userService;

    @Autowired
    private IAgentService agentService;

    @Autowired
    private IGameUserRoleService gameUserRoleService;

    @Autowired
    private IGameService gameService;

    @Autowired
    private IGameAuthService gameAuthService;

    @Override
    public IPage<UserCardVO> getUserCardList(UserNameAuthListParam param) {
        //游戏获取
        Tuple2<String, List<Long>> gameTuple = gameAuthService.getUserGameList(null);
        List<Long> gameIdList = gameTuple.getT2();
        //渠道获取
        Tuple2<List<Long>, List<AgentDTO>> tuple2 = agentService.getUserAgent(null, null, null);
        List<Long> agentIdList = tuple2.getT1();
        if (CollectionUtils.isEmpty(gameIdList)) {
            if (!SecurityUtil.isAdmin() || Objects.equals(gameTuple.getT1(), GameAuthEnum.MANAGE.getValue())) {
                return new Page<>();
            }
        }
        if (CollectionUtils.isEmpty(agentIdList)) {
            return new Page<>();
        }
        return page(param.toPage(), new QueryWrapper<UserCard>().lambda()
                .in(!SecurityUtil.isAdmin(), UserCard::getGameId, gameIdList)
                .in(CollectionUtils.isNotEmpty(agentIdList), UserCard::getAgentId, agentIdList)
                .eq(Strings.isNotBlank(param.getUserName()), UserCard::getUsername, param.getUserName())
                .eq(Strings.isNotBlank(param.getNickname()), UserCard::getNickname, param.getNickname())
                .ge(param.getBeginDate() != null, UserCard::getRegTime, param.getBeginDate() == null ? null : LocalDateTime.of(param.getBeginDate(), LocalTime.MIN))
                .le(param.getEndDate() != null, UserCard::getRegTime, param.getEndDate() == null ? null : LocalDateTime.of(param.getEndDate(), LocalTime.MAX))
                .eq(Strings.isNotBlank(param.getRealName()), UserCard::getCardName, param.getRealName())
                .orderByDesc(UserCard::getCreateTime))
                .convert(this::toVo);
    }

    private UserCardVO toVo(UserCard userCard) {
        if (Objects.isNull(userCard)) {
            return null;
        }
        UserCardVO userCardVO = BeanUtil.copy(userCard, UserCardVO.class);
        userCardVO.setCardId(userCard.getShowCardId());
        userCardVO.setCardName(userCard.getShowCardName());
        //最近角色
        UserDTO userDTO = userService.getById(userCard.getUserId());
        GameUserRole gameUserRole = gameUserRoleService.getLastGameUserRoleName(userDTO.getId(), userDTO.getGameId());
        userCardVO.setLastGameRoleName(gameUserRole == null ? null : gameUserRole.getRoleName());
        //游戏名称
        userCardVO.setGameId(gameUserRole == null ? null : gameUserRole.getGameId());
        if (userCardVO.getGameId() != null) {
            GameDTO gameDTO = gameService.getById(userCardVO.getGameId());
            userCardVO.setGameName(gameDTO == null ? null : gameDTO.getName());
        }
        return userCardVO;
    }

    @Override
    public UserCardVO getByUserId(Long userId) {
        UserCard userCard = this.getOne(new LambdaQueryWrapper<UserCard>()
                .eq(UserCard::getUserId, userId)
                .orderByDesc(UserCard::getCreateTime)
                .last("limit 1")
        );
        return this.toVo(userCard);
    }

}
