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.manage.pojo.dto.GameDTO;
import com.zanxiang.game.module.manage.pojo.params.GameUserConfigAddUpdateParam;
import com.zanxiang.game.module.manage.pojo.params.GameUserConfigListParam;
import com.zanxiang.game.module.manage.pojo.vo.GameUserConfigListVO;
import com.zanxiang.game.module.manage.service.IGameAuthService;
import com.zanxiang.game.module.manage.service.IGameService;
import com.zanxiang.game.module.manage.service.IGameUserConfigService;
import com.zanxiang.game.module.mybatis.entity.GameUserConfig;
import com.zanxiang.game.module.mybatis.mapper.GameUserConfigMapper;
import com.zanxiang.module.util.bean.BeanUtil;
import com.zanxiang.module.util.exception.BaseException;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import reactor.util.function.Tuple2;

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

/**
 * @author : lingfeng
 * @time : 2023-08-09
 * @description : 有效用户配置
 */
@Slf4j
@Service
public class GameUserConfigServiceImpl extends ServiceImpl<GameUserConfigMapper, GameUserConfig> implements IGameUserConfigService {

    @Autowired
    private IGameService gameService;

    @Autowired
    private IGameAuthService gameAuthService;

    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean addOrUpdate(GameUserConfigAddUpdateParam param) {
        //判断游戏配置不可重复
        if (super.count(new LambdaQueryWrapper<GameUserConfig>()
                .eq(GameUserConfig::getGameId, param.getGameId())
                .ne(param.getId() != null, GameUserConfig::getId, param.getId())
        ) > 0) {
            throw new BaseException("该游戏已经存在配置, 禁止添加或者修改");
        }
        //添加或者更新
        return super.saveOrUpdate(this.transform(param));
    }

    private GameUserConfig transform(GameUserConfigAddUpdateParam param) {
        return GameUserConfig.builder()
                .id(param.getId())
                .gameId(param.getGameId())
                .roleLevel(param.getRoleLevel())
                .createBy(param.getId() == null ? SecurityUtil.getUserId() : null)
                .createTime(param.getId() == null ? LocalDateTime.now() : null)
                .updateBy(SecurityUtil.getUserId())
                .updateTime(LocalDateTime.now())
                .build();
    }

    @Override
    public IPage<GameUserConfigListVO> listOfPage(GameUserConfigListParam param) {
        //游戏获取
        Tuple2<String, List<Long>> gameTuple = gameAuthService.getUserGameList(null);
        List<Long> gameIdList = gameTuple.getT2();
        if (CollectionUtils.isEmpty(gameIdList)) {
            return new Page<>();
        }
        //不包含参数游戏
        if (param.getGameId() != null && !gameIdList.contains(param.getGameId())) {
            return new Page<>();
        }
        return page(param.toPage(), new QueryWrapper<GameUserConfig>().lambda()
                .in(GameUserConfig::getGameId, gameIdList)
                .eq(param.getGameId() != null, GameUserConfig::getGameId, param.getGameId())
                .orderByDesc(GameUserConfig::getCreateTime)
        ).convert(this::toVo);
    }

    private GameUserConfigListVO toVo(GameUserConfig gameUserConfig) {
        if (Objects.isNull(gameUserConfig)) {
            return null;
        }
        GameUserConfigListVO gameUserConfigListVO = BeanUtil.copy(gameUserConfig, GameUserConfigListVO.class);
        GameDTO gameDTO = gameService.getById(gameUserConfigListVO.getGameId());
        gameUserConfigListVO.setGameName(gameDTO == null ? null : gameDTO.getName());
        return gameUserConfigListVO;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean deleteById(Long id) {
        return super.removeById(id);
    }
}
