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

import com.zanxiang.game.data.serve.pojo.vo.GameListVO;
import com.zanxiang.game.data.serve.service.IGameListService;
import lombok.extern.slf4j.Slf4j;
import org.nutz.dao.Dao;
import org.nutz.dao.Sqls;
import org.nutz.dao.sql.Sql;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Map;

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

    @Autowired
    private Dao dao;

    @Override
    public GameListVO getGameList() {
        //查询游戏列表
        Sql gameListSql = Sqls.create("""
                SELECT
                	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
                ) a
                WHERE a.source_system = 'ZX_ONE'
                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
                	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
                ) a
                WHERE a.source_system = 'ZX_ONE'
                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
                	super_game_id,
                	super_game_name
                FROM (
                	SELECT
                		a.source_system ,
                		IFNULL(a.super_game_id, a.id) as super_game_id ,
                		IFNULL(b.name, a.game_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
                ) a
                WHERE a.source_system = 'ZX_ONE'
                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();
    }

}
