/*
 * @Author: Kane
 * @Date: 2023-11-01 16:48:56
 * @LastEditors: Kane
 * @FilePath: /desktop_archievement_backend/src/main/java/com/cpic/xim/web/controllers/archievement/bi/BIReportController.java
 * @Description: BI报表相关的controller
 * 
 * Copyright (c) ${2023} by Kane, All Rights Reserved. 
 */
package com.cpic.xim.web.controllers.archievement.bi;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
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.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import com.cpic.xim.mybatis.mapper.QueryBIArchievementDataMapper;
import com.cpic.xim.mybatis.pojo.BIDepartmentAttachingRateRecord;
import com.cpic.xim.mybatis.pojo.BIDepartmentRenewalRateRecord;
import com.cpic.xim.mybatis.pojo.BITelsalerAttachingRateRecord;
import com.cpic.xim.mybatis.pojo.BITelsalerRenewalRateRecord;
import com.cpic.xim.mybatis.utils.MybatisUtils;

@Controller
@RequestMapping( method = RequestMethod.POST, path = "/archievement" )
public class BIReportController
{
    private static Logger logger = LoggerFactory.getLogger( BIReportController.class );

    @PostMapping( path = "/bi_telsaler_attachingrate.do" )
    @ResponseBody
    public QueryTelsalerAttachingRateReportResponse queryTelsalerAttachingRateRepor()
    {
        QueryTelsalerAttachingRateReportResponse response =
                new QueryTelsalerAttachingRateReportResponse();
        SqlSession session = null;
        QueryBIArchievementDataMapper mapper = null;

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

            session.clearCache();
            List<BITelsalerAttachingRateRecord> records =
                    mapper.queryBITelsalerAttachingRate();

            response.setSuccess( true );
            response.setMessage( "查询成功" );
            response.setRecords( records );
        }
        catch ( IOException error )
        {
            logger.error("查询BI坐席渗透率报表出现IOException异常，异常内容：", error);

            response.setSuccess( false );
            response.setMessage( "查询失败，原因" + error.getMessage() );
            response.setRecords( null );
        }
        catch ( PersistenceException error )
        {
            logger.error("查询BI坐席渗透率报表出现PersistenceException异常，异常内容：", error);
            
            response.setSuccess( false );
            response.setMessage( "查询失败，原因" + error.getMessage() );
            response.setRecords( null );
        }

        return response;
    }

    @PostMapping( path="/bi_telsaler_renewalrate.do" )
    @ResponseBody
    public QueryTelsalerRenewalRateReportResponse queryTelsalerRenewalRateReport()
    {
        QueryTelsalerRenewalRateReportResponse response = new QueryTelsalerRenewalRateReportResponse();
        SqlSession session = null;
        QueryBIArchievementDataMapper mapper = null;

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

            session.clearCache();
            ArrayList<BITelsalerRenewalRateRecord> records = mapper.queryBITesalerRenewalRate();

            response.setSuccess( true );
            response.setMessage("查询成功");
            response.setRecords(records);
        }
        catch ( IOException error )
        {
            logger.error("查询BI坐席续保率报表出现IOException异常，异常内容：", error);

            response.setSuccess( false );
            response.setMessage( "查询失败，原因" + error.getMessage() );
            response.setRecords( null );
        }
        catch ( PersistenceException error )
        {
            logger.error("查询BI坐席续保率报表出现PersistenceException异常，异常内容：", error);
            
            response.setSuccess( false );
            response.setMessage( "查询失败，原因" + error.getMessage() );
            response.setRecords( null );
        }

        return response;
    }

    @PostMapping( path = "/bi_department_attachingrate.do" )
    @ResponseBody
    public QueryDepartmentAttachingRateReportResponse queryDepartmentAttachingRateRepor()
    {
        QueryDepartmentAttachingRateReportResponse response =
                new QueryDepartmentAttachingRateReportResponse();
        SqlSession session = null;
        QueryBIArchievementDataMapper mapper = null;

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

            session.clearCache();
            ArrayList<BIDepartmentAttachingRateRecord> records =
                    mapper.queryBIDepartmentAttachingRate();

            response.setSuccess( true );
            response.setMessage( "查询成功" );
            response.setRecords( records );
        }
        catch ( IOException error )
        {
            logger.error("查询BI机构渗透率报表出现IOException异常，异常内容：", error);

            response.setSuccess( false );
            response.setMessage( "查询失败，原因" + error.getMessage() );
            response.setRecords( null );
        }
        catch ( PersistenceException error )
        {
            logger.error("查询BI机构渗透率报表出现PersistenceException异常，异常内容：", error);
            
            response.setSuccess( false );
            response.setMessage( "查询失败，原因" + error.getMessage() );
            response.setRecords( null );
        }

        return response;
    }

    @PostMapping( path = "/bi_department_renewalrate.do" )
    @ResponseBody
    public QueryDepartmentRenewalRateReportResponse queryDepartmentRenewalRateRepor()
    {
        QueryDepartmentRenewalRateReportResponse response =
                new QueryDepartmentRenewalRateReportResponse();
        SqlSession session = null;
        QueryBIArchievementDataMapper mapper = null;

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

            session.clearCache();
            ArrayList<BIDepartmentRenewalRateRecord> records =
                    mapper.queryBIDepartmentRenewalRate();

            response.setSuccess( true );
            response.setMessage( "查询成功" );
            response.setRecords( records );
        }
        catch ( IOException error )
        {
            logger.error("查询BI机构渗透率报表出现IOException异常，异常内容：", error);

            response.setSuccess( false );
            response.setMessage( "查询失败，原因" + error.getMessage() );
            response.setRecords( null );
        }
        catch ( PersistenceException error )
        {
            logger.error("查询BI机构渗透率报表出现PersistenceException异常，异常内容：", error);
            
            response.setSuccess( false );
            response.setMessage( "查询失败，原因" + error.getMessage() );
            response.setRecords( null );
        }

        return response;
    }
}
