package com.game.business.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;

import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

import com.game.business.domain.AppGame;
import com.game.business.service.IAppGameService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.game.business.mapper.AppGameCommissionMapper;
import com.game.business.domain.AppGameCommission;
import com.game.business.service.IAppGameCommissionService;
import com.game.common.annotation.DataSource;
import com.game.common.enums.DataSourceType;

/**
 * 游戏佣金配置Service业务层处理
 *
 * @author game
 * @date 2024-06-23
 */
@Service
public class AppGameCommissionServiceImpl extends ServiceImpl<AppGameCommissionMapper, AppGameCommission> implements IAppGameCommissionService {
    @Autowired
    private AppGameCommissionMapper appGameCommissionMapper;

    @Autowired
    private IAppGameService appGameService;

    /**
     * 查询游戏佣金配置
     *
     * @param id 游戏佣金配置主键
     * @return 游戏佣金配置
     */
    @Override
    public AppGameCommission selectAppGameCommissionById(Long id) {
        return appGameCommissionMapper.selectAppGameCommissionById(id);
    }

    /**
     * 查询游戏佣金配置列表
     *
     * @param appGameCommission 游戏佣金配置
     * @return 游戏佣金配置
     */
    @Override
    public List<AppGameCommission> selectAppGameCommissionList(AppGameCommission appGameCommission) {
        return appGameCommissionMapper.selectAppGameCommissionList(appGameCommission);
    }

    /**
     * 新增游戏佣金配置
     *
     * @param appGameCommission 游戏佣金配置
     * @return 结果
     */
    @Override
    public int insertAppGameCommission(AppGameCommission appGameCommission) {
            return appGameCommissionMapper.insertAppGameCommission(appGameCommission);
    }

    /**
     * 修改游戏佣金配置
     *
     * @param appGameCommission 游戏佣金配置
     * @return 结果
     */
    @Override
    public int updateAppGameCommission(AppGameCommission appGameCommission) {
        return appGameCommissionMapper.updateAppGameCommission(appGameCommission);
    }

    /**
     * 批量删除游戏佣金配置
     *
     * @param ids 需要删除的游戏佣金配置主键
     * @return 结果
     */
    @Override
    public int deleteAppGameCommissionByIds(Long[] ids) {
        return appGameCommissionMapper.deleteAppGameCommissionByIds(ids);
    }

    /**
     * 删除游戏佣金配置信息
     *
     * @param id 游戏佣金配置主键
     * @return 结果
     */
    @Override
    public int deleteAppGameCommissionById(Long id) {
        return appGameCommissionMapper.deleteAppGameCommissionById(id);
    }

    @Override
    public List<AppGameCommission> getByIds(List<Long> ids) {
        LambdaQueryWrapper<AppGameCommission> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.in(AppGameCommission::getUserId, ids);
        List<AppGameCommission> list = appGameCommissionMapper.selectList(queryWrapper);
        if(null != list && list.size() > 0){
            List<Long> gameIds = list.stream().map(e->e.getGameId()).collect(Collectors.toList());
            AppGame queryGame = new AppGame();
//            queryGame.setStatus(1L);
            List<AppGame> games = appGameService.selectAppGameList(queryGame);
            for(AppGame game:games){
                Map<String,Boolean> userMap = new HashMap<>();
                for(AppGameCommission appGameCommission : list){
                    if(game.getId().equals(appGameCommission.getGameId())){
                        if(null == userMap.get(appGameCommission.getUserId())){
                            userMap.put(String.valueOf(appGameCommission.getUserId()),true);
                        }
                        appGameCommission.setGameName(game.getName());
                    }
                }

                List<Long> unIds = ids.stream().filter(e->null==userMap.get(String.valueOf(e))).collect(Collectors.toList());
                for(Long unId:unIds){
                    //未设置过该游戏
                    AppGameCommission appGameCommission = new AppGameCommission();
                    appGameCommission.setGameId(game.getId());
                    appGameCommission.setGameRate(0.00);
                    appGameCommission.setGameName(game.getName());
                    appGameCommission.setUserId(unId);
                    list.add(appGameCommission);
                }
            }
        }
        return list;
    }
}