package com.zanxiang.game.data.serve.service.impl;

import com.zanxiang.erp.security.util.SecurityUtil;
import com.zanxiang.game.data.serve.component.DataPowerComponent;
import com.zanxiang.game.data.serve.pojo.dto.ChoiceListDTO;
import com.zanxiang.game.data.serve.pojo.vo.GameListVO;
import com.zanxiang.game.data.serve.service.IGameListService;
import com.zanxiang.game.module.base.ServerInfo;
import com.zanxiang.game.module.base.pojo.vo.GameAuthUserVO;
import com.zanxiang.game.module.base.rpc.GameAuthRpc;
import com.zanxiang.module.util.JsonUtil;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.collections4.CollectionUtils;
import org.apache.commons.lang3.StringUtils;
import org.apache.dubbo.config.annotation.DubboReference;
import org.nutz.dao.Cnd;
import org.nutz.dao.Dao;
import org.nutz.dao.Sqls;
import org.nutz.dao.sql.Criteria;
import org.nutz.dao.sql.Sql;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;

/**
 * @author tianhua
 * @time 2023/9/25
 * @Description
 **/
@Service
@Slf4j
public class GameListServiceImpl implements IGameListService {

    @Autowired
    private Dao dao;
    @Autowired
    private DataPowerComponent dataPowerComponent;

    @DubboReference(providedBy = ServerInfo.SERVER_DUBBO_NAME)
    private GameAuthRpc gameAuthRpc;

    /**
     * 游戏列表接口 查看所有游戏、父游戏、超父游戏列表
     *
     * @param dto
     * @return
     */
    @Override
    public GameListVO getGameList(ChoiceListDTO dto) {
        GameAuthUserVO userGameInfo = gameAuthRpc.getGameAuthByUserIds().getData();
        List<Long> gameIds = dataPowerComponent.getFullGameIdList(userGameInfo.getGameIdList());
        log.error("userId:{},下拉框游戏列表gameIds:{}", SecurityUtil.getUserId(), JsonUtil.toString(gameIds));
        if (CollectionUtils.isEmpty(gameIds)) {
            return null;
        }
        Criteria cri = Cnd.cri();
        if (StringUtils.isBlank(dto.getSourceSystem())) {
            //默认查询“ZX_ONE”数据
            dto.setSourceSystem("ZX_ONE");
        }
        if (StringUtils.isNotBlank(dto.getSourceSystem())) {
            //不同的数据源
            cri.where().andEquals("source_system", dto.getSourceSystem());
        }
        Criteria gameCri = Cnd.cri();
        if (CollectionUtils.isNotEmpty(gameIds)) {
            gameCri.where().andInList("a.id", gameIds);
        }

        //查询游戏列表
        Sql gameListSql = Sqls.create("""
                SELECT
                    source_system,
                	id,
                	game_name,
                	parent_game_id,
                	parent_game_name,
                	super_game_id,
                	super_game_name
                FROM (
                	SELECT
                		a.source_system ,
                		a.id,
                		a.game_name,
                		IFNULL(a.parent_id, a.id) as parent_game_id ,
                		IFNULL(b.game_name, a.game_name ) as parent_game_name,
                		IFNULL(a.super_game_id, a.id) as super_game_id ,
                		IFNULL(c.name, a.game_name ) as super_game_name
                	FROM dm_game_order.t_game a
                	LEFT JOIN dm_game_order.t_game b
                	on a.source_system = b.source_system AND a.parent_id = b.id
                	LEFT JOIN dm_game_order.t_game_super c
                	on a.source_system = c.source_system AND a.super_game_id = c.id
                	""" + gameCri + """
                ) a
                """ + cri +
                """
                        GROUP BY id, game_name, parent_game_id, source_system, parent_game_name, super_game_id, super_game_name;
                        """);
        gameListSql.setCallback(Sqls.callback.maps());
        dao.execute(gameListSql);

        //查询父游戏列表
        Sql parentGameListSql = Sqls.create("""
                SELECT
                    source_system,
                	parent_game_id,
                	parent_game_name,
                	super_game_id,
                	super_game_name
                FROM (
                	SELECT
                		a.source_system ,
                		IFNULL(a.parent_id, a.id) as parent_game_id ,
                		IFNULL(b.game_name, a.game_name ) as parent_game_name,
                		IFNULL(a.super_game_id, a.id) as super_game_id ,
                		IFNULL(c.name, a.game_name ) as super_game_name
                	FROM dm_game_order.t_game a
                	LEFT JOIN dm_game_order.t_game b
                	on a.source_system = b.source_system AND a.parent_id = b.id
                	LEFT JOIN dm_game_order.t_game_super c
                	on a.source_system = c.source_system AND a.super_game_id = c.id
                	""" + gameCri + """
                ) a
                """ + cri +
                """
                        GROUP BY parent_game_id, source_system, parent_game_name, super_game_id, super_game_name;
                        """);
        parentGameListSql.setCallback(Sqls.callback.maps());
        dao.execute(parentGameListSql);

        //查询超父游戏列表
        Sql superGameListSql = Sqls.create("""
                SELECT
                    source_system,
                	super_game_id,
                	super_game_name
                FROM (
                	SELECT
                		a.source_system ,
                		a.super_game_id as super_game_id ,
                     	b.name as super_game_name
                	FROM dm_game_order.t_game a
                	LEFT JOIN dm_game_order.t_game_super b
                	on a.source_system = b.source_system AND a.super_game_id = b.id
                	where a.super_game_id is not null
                ) a
                """ + cri +
                """
                        GROUP BY super_game_id, source_system, super_game_name;
                        """);
        superGameListSql.setCallback(Sqls.callback.maps());
        dao.execute(superGameListSql);

        return GameListVO.builder()
                .gameList(gameListSql.getList(Map.class))
                .parentGameList(parentGameListSql.getList(Map.class))
                .superGameList(superGameListSql.getList(Map.class))
                .build();
    }

}
