/*
 * @Author: Kane
 * @Date: 2023-09-06 10:41:34
 * @LastEditors: Kane
 * @FilePath: /desktop_archievement_backend/src/main/java/com/cpic/xim/web/controllers/rewards/RewardController.java
 * @Description: 坐席奖励相关的请求
 * 
 * Copyright (c) ${2022} by Kane, All Rights Reserved. 
 */
package com.cpic.xim.web.controllers.rewards;

import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import org.apache.ibatis.exceptions.PersistenceException;
import org.apache.ibatis.session.SqlSession;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import com.cpic.xim.mybatis.mapper.RewardsMapper;
import com.cpic.xim.mybatis.pojo.RewardGainer;
import com.cpic.xim.mybatis.pojo.RewardProject;
import com.cpic.xim.mybatis.utils.MybatisUtils;

@Controller
@RequestMapping( "/rewards" )
public class RewardController
{
    // 日志
    private static Logger logger = LoggerFactory.getLogger( RewardController.class );

    private RewardController()
    {}

    /**
     * 相应查询获奖项目清单的请求
     * @return 获奖项目清单的response对象
     */
    @PostMapping( "/query_reward_projects.do" )
    @ResponseBody
    public RewardProjectsResponse queryRewardProjecrts()
    {
        RewardProjectsResponse response = new RewardProjectsResponse();
        SqlSession session = null;

        logger.debug( "查询奖项清单。" );

        try
        {
            session = MybatisUtils.getSqlSession();
            session.clearCache();

            RewardsMapper mapper = session.getMapper( RewardsMapper.class );
            ArrayList<RewardProject> rewards = mapper.queryRewardProjects();

            response.setSuccess( true );
            response.setMessage( "查询成功！" );
            response.setRewardList( rewards );

            logger.debug( "查询奖项清单，" + response.getMessage() + "，" + rewards.toString() );
        }
        catch ( IOException error )
        {
            response.setSuccess( false );
            response.setMessage( "查询失败！" + error.getMessage() );
            response.setRewardList( null );

            logger.debug( "查询奖项清单，" + response.getMessage() + "，" + error.getMessage() );
        }

        return response;
    }

    @PostMapping( "/query_reward_gainers.do" )
    @ResponseBody
    RewardGainersResponse queryRewardGainers()
    {
        RewardGainersResponse response = new RewardGainersResponse();
        SqlSession session = null;

        try
        {
            session = MybatisUtils.getSqlSession();

            RewardsMapper mapper = session.getMapper( RewardsMapper.class );

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

            response.setSuccess( true );
            response.setMessage( "查询成功！" );
            response.setGainerList( gainers );
        }
        catch ( PersistenceException error )
        {
            response.setSuccess( false );
            response.setMessage( "查询失败！" + error.getMessage() );
            response.setGainerList( null );
        }
        catch ( IOException error )
        {
            response.setSuccess( false );
            response.setMessage( "查询失败！" + error.getMessage() );
            response.setGainerList( null );
        }

        return response;
    }

    @PostMapping( "/add_telsaler_reward.do" )
    @ResponseBody
    public AddTelsalerResponse addTelsalerReward( @RequestBody AddTelsalerRequest request )
    {
        AddTelsalerResponse response = new AddTelsalerResponse();
        SqlSession session = null;

        try
        {
            session = MybatisUtils.getSqlSession();

            RewardsMapper mapper = session.getMapper( RewardsMapper.class );
            HashMap<String, Object> params = new HashMap<>( 3 );

            params.put( "a_reward_index", request.getRewardIndex() );
            params.put( "a_telsaler_name", request.getTelsalerName() );

            mapper.addRewardTelSaler( params );
            session.commit();

            response.setSuccess( true );
            response.setMessage( "添加成功！" );
        }
        catch ( PersistenceException error )
        {
            if ( session != null )
            {
                session.rollback();
            }

            logger.error( "添加获奖坐席失败，原因：" + error.getMessage() );

            response.setSuccess( false );
            response.setMessage( error.getMessage() );
        }
        catch ( IOException error )
        {
            logger.error( "添加获奖坐席失败，原因：" + error.getMessage() );

            response.setSuccess( false );
            response.setMessage( error.getMessage() );
        }

        return response;
    }

    @PostMapping( "/delete_telsaler_reward.do" )
    @ResponseBody
    public DeleteTelsalerResponse deleteTelsalerReward( @RequestBody DeleteTelsalerRequest request )
    {
        DeleteTelsalerResponse response = new DeleteTelsalerResponse();
        SqlSession session = null;

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

            HashMap<String, Object> param = new HashMap<String, Object>( 3 );

            int[] records = request.getRecordID();

            for ( int index = 0; index < records.length; index++ )
            {
                param.put( "a_rec_id", records[index] );

                mapper.deleteRewardTelSaler( param );
            }

            response.setSuccess( true );
            response.setMessage( "删除成功！" );
        }
        catch ( IOException error )
        {
            logger.error( "删除获奖坐席失败，原因：" + error.getMessage() );

            response.setSuccess( false );
            response.setMessage( error.getMessage() );
        }
        catch ( PersistenceException error )
        {
            logger.error( "删除获奖坐席失败，原因：" + error.getMessage() );

            response.setSuccess( false );
            response.setMessage( error.getMessage() );
        }

        return response;
    }

    @PostMapping( "update_telsaler_reward.do" )
    @ResponseBody
    UpdateTelsalerResponse updateTelsalerReward( @RequestBody UpdateTelsalerRequest request )
    {
        UpdateTelsalerResponse response = new UpdateTelsalerResponse();
        SqlSession session = null;

        try
        {
            session = MybatisUtils.getSqlSession();
            RewardsMapper mapper = session.getMapper( RewardsMapper.class );
            HashMap<String, Object> params = new HashMap<>( 3 );

            params.put( "a_rec_id", request.getRecordID() );
            params.put( "a_telsaler_name", request.getTelsalerName() );
            params.put( "a_reward_index", request.getRewardIndex() );

            mapper.updateRewardTelSaler( params );

            response.setSuccess( true );
            response.setMessage( "更新成功！" );
        }
        catch ( IOException error )
        {
            logger.error( "更新获奖坐席失败，原因：" + error.getMessage() );

            response.setSuccess( false );
            response.setMessage( error.getMessage() );
        }
        catch ( PersistenceException error )
        {
            logger.error( "更新获奖坐席失败，原因：" + error.getMessage() );

            response.setSuccess( false );
            response.setMessage( error.getMessage() );
        }

        return response;
    }
}
