/*
 * @Author: Kane
 * @Date: 2023-05-15 14:59:26
 * @LastEditors: Kane
 * @FilePath: /desktop_archievement_backend/src/test/java/com/cpic/xim/DesktopArchievement/test/DesktopArchievementTest.java
 * @Description: 
 * 
 * Copyright (c) ${2022} by Kane, All Rights Reserved. 
 */
package com.cpic.xim.DesktopArchievement.test;

import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertTrue;
import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.HashMap;
import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;
import org.junit.Test;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import com.cpic.xim.mybatis.mapper.ArchievementMapper;
import com.cpic.xim.mybatis.mapper.RewardsMapper;
import com.cpic.xim.mybatis.pojo.MensualArchievementItem;
import com.cpic.xim.mybatis.pojo.RewardGainer;
import com.cpic.xim.mybatis.pojo.RewardProject;
import com.cpic.xim.mybatis.utils.MybatisUtils;
import com.cpic.xim.utils.ranking.CallerRankingList;

@SuppressWarnings( "all" )
public class DesktopArchievementTest
{
    private static String configFilePath = "mybatis/mybatis-config.xml";
    private static Logger logger = LoggerFactory.getLogger( DesktopArchievementTest.class );

    @Test
    public void testCallerRankingList()
    {
        CallerRankingList rankingList = null;

        try
        {
            rankingList = CallerRankingList.getCallerRankingList( "QDI", "2023", "06" );
        }
        catch ( IOException error )
        {
            String message = "获取排行榜失败，原因：" + error.getMessage();

            logger.error( message, error );
        }

        assertNotNull( rankingList );
    }

    @Test
    public void testDepartmentArchievement()
    {
        try
        {
            InputStream configFile = Resources.getResourceAsStream( configFilePath );
            SqlSessionFactoryBuilder builder = new SqlSessionFactoryBuilder();
            SqlSessionFactory factory = builder.build( configFile );
            SqlSession session = factory.openSession();

            ArchievementMapper mapper = session.getMapper( ArchievementMapper.class );
            ArrayList<MensualArchievementItem> mensual = null;
            HashMap<String, Object> params = new HashMap<>();

            params.put( "a_department_code", "QDI" );

            mapper.getDepartmentArchievement( params );

            mensual = (ArrayList<MensualArchievementItem>) params.get( "a_mensual_cur" );
            Integer total = (Integer) params.get( "a_total" );

            System.out.println( params.toString() );
            System.out.println( mensual.toString() );

            assertTrue( params.size() > 1 );
        }
        catch ( IOException error )
        {
            assert (false);
        }
    }

    @Test
    public void testCallerArchievement()
    {
        try
        {
            SqlSession session = MybatisUtils.getSqlSession();
            ArchievementMapper mapper = session.getMapper( ArchievementMapper.class );

            ArrayList<MensualArchievementItem> mensual = null;
            HashMap<String, Object> params = new HashMap<>();

            params.put( "a_caller_code", "61136" );

            mapper.getCallerArchievement( params );

            Object obj = params.get( "a_mensual_cur" );
            mensual = (ArrayList<MensualArchievementItem>) params.get( "a_mensual_cur" );

            System.out.println( params.toString() );
            System.out.println( mensual.toString() );
            System.out.println( obj.getClass() );

            assertTrue( params.size() > 1 );
        }
        catch ( IOException error )
        {
            assert (false);
        }
    }

    @Test
    public void testRewardProjects()
    {
        try
        {
            SqlSession session = MybatisUtils.getSqlSession();
            RewardsMapper mapper = session.getMapper( RewardsMapper.class );

            ArrayList<RewardProject> rewards = mapper.queryRewardProjects();

            System.out.println( rewards );
            assert (rewards.isEmpty() == false);

        }
        catch ( IOException error )
        {
            assert (false);
        }
    }

    @Test
    public void testQueryGainers()
    {
        SqlSession session = null;

        try
        {
            session = MybatisUtils.getSqlSession();
            RewardsMapper mapper = session.getMapper( RewardsMapper.class );

            ArrayList<RewardGainer> gainers = mapper.queryRewardTelsaler();

            System.out.println( gainers );
            
            assert( gainers != null );
        }
        catch ( IOException error )
        {
            System.out.println( error.getMessage() );
            assert (false);
        }
    }
}
