<?php
namespace app\index\controller;

/**
 * 报警类
 */
class Alarm extends Common
{


    /**
     * 敏感词报警
     *
     * @access public
     * @return array JsonString
     */
    public function sensitiveAlarm()
    {
        // 验证token.
        $tokenStatus = $this->verifyApiToken();
        $code        = -2;
        $msg         = '错误';
        if ($tokenStatus === false) {
            $msg = 'token错误';
            return json(['code' => $code, 'data' => [], 'msg' => $msg]);
        }

        try {
            $conversationId         = input('get.conversationId');
            $type                   = input('get.type');
            $number                 = input('get.number');
            $where['servicelog_id'] = $conversationId;
            // 获取数据.
            if ($type == '1') {
                $field = ['alarm_userSensitive'];
                $alarm = model('Alarm')->findAlarm($field, $where);
                // 修改或新增数据.
                if (empty($alarm) === true) {
                    $data['alarm_userSensitive'] = intval($number);
                    $data['servicelog_id']       = $conversationId;
                    model('Alarm')->addAlarm($data);
                } else {
                    $data['alarm_userSensitive'] = ($alarm['alarm_userSensitive'] + intval($number));
                    model('Alarm')->updateAlarm($where, $data);
                }
            } else {
                $field = ['alarm_serverSensitive'];
                $alarm = model('Alarm')->findAlarm($field, $where);
                // 修改或新增数据.
                if (empty($alarm) === true) {
                    $data['alarm_serverSensitive'] = intval($number);
                    $data['servicelog_id']         = $conversationId;
                    model('Alarm')->addAlarm($data);
                } else {
                    $data['alarm_serverSensitive'] = ($alarm['alarm_serverSensitive'] + intval($number));
                    model('Alarm')->updateAlarm($where, $data);
                }
            }//end if

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

    }//end sensitiveAlarm()


}
