<?php
namespace app\service\controller;

/**
 * 客服系统会话历史类
 */
class History extends Common
{


    /**
     * 获取会话历史
     *
     * @access public
     * @return array JsonString
     */
    public function historyList()
    {
        // 验证token.
        $tokenStatus = $this->verifyToken();
        $code        = -2;
        $msg         = '错误';
        if ($tokenStatus === false) {
            $msg = 'token错误';
            return json(['code' => $code, 'data' => [], 'msg' => $msg]);
        }

        try {
            // 获取用户信息.
            $getUserInfo     = $this->getUserInfo();
            $serviceLogField = [
                'user_id',
                'user_name',
                'user_avatar',
                'user_ip',
                'start_time',
                'end_time',
                'name',
                'website',
                'system',
                'browse',
                'a.status',
                'evaluate_id',
                'intime',
                'servicelog_id',
            ];
            // 关联信息.
            $serviceLogJoin['groups b'] = 'a.group_id = b.id';
            $serviceLogWhere['kf_id']   = $getUserInfo->id;
            // 分页.
            $currentPage = input('post.currentPage', '1');
            $pageSize    = input('post.pageSize', '10');
            $start       = input('post.start');
            $end         = input('post.end');
            $userName    = input('post.user_name');
            $startTime   = strtotime(date('Y-m-d').'-6 day');
            $endTime     = strtotime(date('Y-m-d').'+1 day');
            if (empty($start) === false && empty($end) === false) {
                $startTime = strtotime($start);
                $endTime   = strtotime($end);
            }

            if (empty($userName) === false) {
                $serviceLogWhere['user_name'] = $userName;
            }

            $serviceLogWhere['start_time'] = [
                'between',
                [
                    $startTime,
                    $endTime,
                ],
            ];
            $offset = (($currentPage - 1) * $pageSize);
            // 获取用户信息.
            $serviceLog      = model('ServiceLog')->selectServiceLog(
                $serviceLogField,
                $offset,
                $pageSize,
                $serviceLogWhere,
                $serviceLogJoin
            );
            $countServiceLog = model('ServiceLog')->countServiceLog($serviceLogWhere);
            $evaluate        = model('Evaluate')->getEvaluate();
            foreach ($serviceLog as $k => $v) {
                foreach ($evaluate as $va) {
                    if ($v->evaluate_id == $va->evaluate_id) {
                        $serviceLog[$k]->evaluate_name = $va->evaluate_name;
                    }
                }
            }

            $result['total']       = $countServiceLog;
            $result['countPage']   = (ceil(($result['total']) / $pageSize));
            $result['currentPage'] = $currentPage;
            $result['list']        = $serviceLog;
            $result['pageSize']    = $pageSize;

            return json(['code' => 1, 'data' => $result, 'msg' => '成功']);
        } catch (\Exception $e) {
            return json(['code' => $code, 'data' => [], 'msg' => $msg]);
        }//end try

    }//end historyList()


    /**
     * 获取会话历史详细
     *
     * @access public
     * @return array JsonString
     */
    public function historyInfo()
    {
        // 验证token.
        $tokenStatus = $this->verifyToken();
        $code        = -2;
        $msg         = '错误';
        if ($tokenStatus === false) {
            $msg = 'token错误';
            return json(['code' => $code, 'data' => [], 'msg' => $msg]);
        }

        try {
            // 获取用户信息.
            $servicelogId = input('get.servicelog_id');
            $chatLogField = ['*'];
            // 关联信息.
            $chatLogWhere['servicelog_id'] = $servicelogId;
            // 分页.
            $currentPage = input('get.currentPage', '1');
            $pageSize    = input('get.pageSize', '10');
            $offset      = (($currentPage - 1) * $pageSize);
            // 获取用户信息.
            $chatLog      = model('ChatLog')->selectChatLog($chatLogField, $offset, $pageSize, $chatLogWhere);
            $countChatLog = model('ChatLog')->countChatLog($chatLogWhere);
            // 查询报警信息.
            $alarmField                    = [
                'evaluate_id',
                'alarm_corresponding',
                'alarm_cvtOvertime',
                'alarm_userSensitive',
                'alarm_serverSensitive',
                'alarm_respond',
            ];
            $alarmWhere['b.servicelog_id'] = $servicelogId;
            $serviceLogJoin['service_log b'] = 'a.servicelog_id = b.servicelog_id';
            $alarm = model('Alarm')->findAlarm($alarmField, $alarmWhere, $serviceLogJoin);
            // 查询系统设置.
            $systemconfig = model('Systemconfig')->selectSystemconfig($alarmField, $alarmWhere, $serviceLogJoin);
            // 响应超时.
            $verifyReturnTime = 0;
            // 会话超时.
            $cvtOvertime = 0;
            foreach ($systemconfig as $v) {
                if ($v['systemconfig_enName'] == 'verifyReturnTime') {
                    if ($v['systemconfig_data'] < $alarm['alarm_corresponding']) {
                        $verifyReturnTime = 1;
                    }
                }

                if ($v['systemconfig_enName'] == 'verifyAllTime') {
                    if ($v['systemconfig_data'] < $alarm['alarm_cvtOvertime']) {
                        $cvtOvertime = 1;
                    }
                }
            }


            $result['total']       = $countChatLog;
            $result['countPage']   = (ceil(($result['total']) / $pageSize));
            $result['currentPage'] = $currentPage;
            $result['list']        = $chatLog;
            $result['pageSize']    = $pageSize;
            $result['alarm']       = [
                'evaluate_id'      => $alarm['evaluate_id'],
                'verifyReturnTime' => $verifyReturnTime,
                'cvtOvertime'      => $cvtOvertime,
                'serverSensitive'  => $alarm['alarm_serverSensitive'] ? 1 : 2,
                'userSensitive'    => $alarm['alarm_userSensitive'] ? 1 : 2,
                'respond'          => $alarm['alarm_respond'],
            ];

            return json(['code' => 1, 'data' => $result, 'msg' => '成功']);
        } catch (\Exception $e) {
            return json(['code' => $code, 'data' => [], 'msg' => $msg]);
        }//end try

    }//end historyInfo()


    /**
     * 获取用户会话历史详细
     *
     * @access public
     * @return array JsonString
     */
    public function userHistory()
    {
        // 验证token.
        $tokenStatus = $this->verifyToken();
        $code        = -2;
        $msg         = '错误';
        if ($tokenStatus === false) {
            $msg = 'token错误';
            return json(['code' => $code, 'data' => [], 'msg' => $msg]);
        }

        try {
            // 获取用户ID.
            $accountId    = input('get.account_id');
            $chatLogField = ['*'];
            // 关联信息.
            $chatLogWhere['from_id'] = $accountId;
            $chatLogWhereOr['to_id'] = $accountId;
            // 分页.
            $currentPage = input('get.currentPage', '1');
            $pageSize    = input('get.pageSize', '10');
            $offset      = (($currentPage - 1) * $pageSize);
            // 获取用户信息.
            $chatLog      = model('ChatLog')->userChatLog(
                $chatLogField,
                $chatLogWhere,
                $chatLogWhereOr,
                $offset,
                $pageSize
            );
            $countChatLog = model('ChatLog')->userChatLogCount($chatLogWhere, $chatLogWhereOr);

            $result['total']       = $countChatLog;
            $result['countPage']   = (ceil(($result['total']) / $pageSize));
            $result['currentPage'] = $currentPage;
            $result['list']        = $chatLog;
            $result['pageSize']    = $pageSize;

            return json(['code' => 1, 'data' => $result, 'msg' => '成功']);
        } catch (\Exception $e) {
            return json(['code' => $code, 'data' => [], 'msg' => $msg]);
        }//end try

    }//end userHistory()


}
