<?php
if ( ! defined('BASEPATH')) exit('No direct script access allowed');
require_once("Aliyunsms.php");
/**
 * 消息发送类
 * Class Notices
 */
class Notices{

    private $CI;
    private $setting;
    private $Aliyunsms;

    public $warning_type = array(
        "10" => "失陷事件",
        "20" => "脆弱性",
        "30" => "残余攻击"
    );

    public $warning_level = array(
        "10" => "已失陷",
        "20" => "高可疑",
        "30" => "低可疑",
        "40" => "高危",
        "50" => "中危",
        "60" => "低危"
    );

    function __construct()
    {
        $this->CI = & get_instance();
        $this->CI->load->driver('cache', array('adapter' => 'apc', 'backup' => 'file'));
        $this->setting = $this->CI->cache->get('setting');
        $this->CI->load->helper(array('sms','date'));
        $this->CI->load->library('mailer');
        $this->CI->mailer->set_config($this->setting);
        $this->CI->load->model("noticeslog_model");
        $this->CI->load->model("user_model");
        if($this->setting['is_sms'] == "1" && $this->setting['sms_type'] == '20' && $this->setting['product'] =='Dysmsapi'){
            $this->Aliyunsms = new Aliyunsms();
        }
    }

    /**
     * 生成工单
     * @param $workorder
     */
    public function create_workorder($workorder){
        $info = array();
        $info['sms_code'] = "FAILED";
        $info['email_code'] = "FAILED";
        if(count($workorder['remark_list'])<1) {
            $workorder['remark_list'][0]=array("remark_content"=>"");
        }
        $receive_user = $this->CI->user_model->get_user_with_user_id($workorder['receive_user_id']);
        if($this->setting['is_sms'] && in_array("10",$workorder['send_type'])){
            $sms_content = '你有新工单需要处理,工单ID:'.$workorder['workorder_id'].',工单名称:'.$workorder['workorder_name'].',类型:'.$workorder['type'].',备注:'.$workorder['remark_list'][count($workorder['remark_list'])-1]['remark_content'];
            $notices_log = $this->CI->noticeslog_model->get_model();
            $notices_log['log_id'] = $this->create_id();
            $notices_log['type'] = "sms";
            $notices_log['mobile'] = $receive_user['mobile'];
            $notices_log['content'] = $sms_content;
            if($this->setting['sms_type'] == '10' ){
                if(send_by_modem($this->setting,$receive_user['mobile'],$sms_content)){
                    $notices_log['status'] = "1";
                    $info['sms_code'] = "OK";
                }else{
                    $notices_log['status'] = "0";
                    $info['sms_msg'] = "短信发送失败!端口被占用或者配置错误!";
                }
            }elseif($this->setting['sms_type'] == '20' ){
                $sms_content = array(
                    "workorder_id"=>$workorder['workorder_id'],
                    "workorder_name"=>strlen($workorder['workorder_name'])<=18?$workorder['workorder_name']:substr($workorder['workorder_name'],0,18)."..",
                    "workorder_type"=>$workorder['type'],
                    "remark_content"=>strlen($workorder['remark_list'][count($workorder['remark_list'])-1]['remark_content'])<=18?$workorder['remark_list'][count($workorder['remark_list'])-1]['remark_content']:substr($workorder['remark_list'][count($workorder['remark_list'])-1]['remark_content'],0,18).".."
                );
                $resp = $this->Aliyunsms->sendSms($receive_user['mobile'],$this->setting['template_codes'][0],$sms_content);
                if($resp['Code'] == 'OK'){
                    $notices_log['status'] = "1";
                    $info['sms_code'] = "OK";
                }else{
                    $notices_log['status'] = "0";
                    $info['sms_msg'] = $resp['Message'];
                }
            }
            $notices_log['create_time'] = new MongoDB\BSON\UTCDateTime(time()*1000);
            $this->CI->noticeslog_model->save_notices_log($notices_log);
        }
        if($this->setting['is_email'] && in_array("20",$workorder['send_type'])){
            $notices_log = $this->CI->noticeslog_model->get_model();
            $notices_log['log_id'] = $this->create_id();
            $notices_log['type'] = "email";
            $notices_log['recipient'] = $receive_user['email'];
            $notices_log['subject'] = "系统有新工单请处理,工单ID:".$workorder['workorder_id'];
            $email_content = '<div style="font-size:12px;">';
            $email_content .='<p style="white-space: normal; background-color: rgb(255, 255, 255);">'.$receive_user['name'].',您好!</p>';
            $email_content .='<p style="white-space: normal; background-color: rgb(255, 255, 255); text-indent: 2em;">系统有新工单请处理:</p>';
            $email_content .='<table align="left" border="1" cellspacing="0" cellpadding="0" style="font-size:12px;"><thead>';
            $email_content .='<tr class="firstRow"><td valign="top" rowspan="1" colspan="5" style="word-break: break-all;">';
            $email_content .='<strong>工单ID:'.$workorder['workorder_id'].'</strong><strong>工单名称:'.$workorder['workorder_name'].'</strong>';
            $email_content .='</td></tr>';
            $email_content .='<tr>';
            $email_content .='<th width="147" valign="middle" style="word-break: break-all;" align="center"><strong>工单名称</strong></th>';
            $email_content .='<th width="147" valign="middle" style="word-break: break-all;" align="center"><strong>类型</strong></th>';
            $email_content .='<th width="147" valign="middle" style="word-break: break-all;" align="center"><strong>等级</strong></th>';
            $email_content .='<th width="147" valign="middle" style="word-break: break-all;" align="center"><strong>时间</strong></th>';
            $email_content .='<th width="147" valign="middle" style="word-break: break-all;" align="center"><strong>告警详情</strong></th>';
            $email_content .='</tr></thead><tbody>';
            foreach($workorder['warning_list'] as $key=>$val) {
                $email_content .= '<tr>';
                $email_content .= '<td valign="top" colspan="1" rowspan="1">'.$val['warning_name'].'</td>';
                $email_content .= '<td valign="middle" colspan="1" rowspan="1" align="center">'.$this->warning_type[$val['type']].'</td>';
                $email_content .= '<td valign="middle" colspan="1" rowspan="1" align="center">'.$this->warning_level[$val['level']].'</td>';
                $email_content .= '<td valign="middle" colspan="1" rowspan="1" align="center">'.date("Y-m-d H:i:s",$val['create_time']->toDateTime()->getTimestamp()).'</td>';
                $email_content .= '<td valign="top" colspan="1" rowspan="1">'.$val['content'].'</td>';
                $email_content .= '</tr>';
            }
            $email_content .='<tr>';
            if(count($workorder['remark_list'])>0) {
                $email_content .= '<td valign="top" rowspan="1" colspan="5" style="word-break: break-all;"><strong>备注:</strong>' . $workorder['remark_list'][count($workorder['remark_list']) - 1]['remark_content'] . '</td>';
            }else{
                $email_content .= '<td valign="top" rowspan="1" colspan="5" style="word-break: break-all;">&nbsp;</td>';
            }
            $email_content .='</tr>';
            $email_content .='</tbody></table>';
            $email_content .='<p><br/>&nbsp;</p><p><br/>&nbsp;</p>';
            $email_content .='<p style="text-align: right;">系统管理员&nbsp;&nbsp;<br>';
            $email_content .=date("Y年m月d日",time()).'</p>';
            $email_content .='</div>';
            $notices_log['content'] = $email_content;
            $resp = $this->CI->mailer->send_email($receive_user['email'],$notices_log['subject'],$notices_log['content']);
            if($resp['code'] == 'OK'){
                $notices_log['status'] = "1";
                $info['email_code'] = "OK";
            }else{
                $notices_log['status'] = "0";
                $info['email_msg'] = $resp['msg'];
            }
            $notices_log['create_time'] = new MongoDB\BSON\UTCDateTime(time()*1000);
            $this->CI->noticeslog_model->save_notices_log($notices_log);
        }
        return $info;
    }

    /**
     * 签收工单
     * @param $workorder
     * @param $remark
     */
    public function sign_workorder($workorder,$remark){
        $info = array();
        $info['sms_code'] = "FAILED";
        $info['email_code'] = "FAILED";
        $receive_user = $this->CI->user_model->get_user_with_user_id($workorder['receive_user_id']);
        $admin_list = $this->CI->user_model->select_users("1");
        $sso_admin_list = $this->CI->user_model->set_collection_name("sso_users")->select_users("1");
        $mobiles = array();
        $emails = array();
        foreach ($admin_list as $key => $val){
            $mobiles[] = $val['mobile'];
            $emails[] = $val['email'];
        }
        foreach ($sso_admin_list as $key => $val){
            $mobiles[] = $val['mobile'];
            $emails[] = $val['email'];
        }
        if($this->setting['is_sms'] && in_array("10",$workorder['send_type'])){
            $sms_content = '工单ID:'.$workorder['workorder_id'].',工单名称:'.$workorder['workorder_name'].',类型:'.$workorder['type'].',已由责任人'.$receive_user['name'].'签收,备注:'.$remark['remark_content'];
            for($i = 0;$i<count($mobiles);$i++) {
                $notices_log = $this->CI->noticeslog_model->get_model();
                $notices_log['type'] = "sms";
                $notices_log['content'] = $sms_content;
                $notices_log['log_id'] = $this->create_id();
                $notices_log['mobile'] = $mobiles[$i];
                if($this->setting['sms_type'] == '10' ){
                    if(send_by_modem($this->setting, $notices_log['mobile'], $sms_content)){
                        $notices_log['status'] = "1";
                        $info['sms_code'] = "OK";
                    }else{
                        $notices_log['status'] = "0";
                        $info['sms_msg'] = "短信发送失败!端口被占用或者配置错误!";
                    }
                }elseif($this->setting['sms_type'] == '20' ){
                    $sms_content = array(
                        "workorder_id"=>$workorder['workorder_id'],
                        "workorder_name"=>strlen($workorder['workorder_name'])<=18?$workorder['workorder_name']:substr($workorder['workorder_name'],0,18)."..",
                        "workorder_type"=>$workorder['type'],
                        "receive_user_name"=>$receive_user['name'],
                        "remark_content"=>strlen($workorder['remark_list'][count($workorder['remark_list'])-1]['remark_content'])<=18?$workorder['remark_list'][count($workorder['remark_list'])-1]['remark_content']:substr($workorder['remark_list'][count($workorder['remark_list'])-1]['remark_content'],0,18).".."
                    );
                    $resp = $this->Aliyunsms->sendSms($notices_log['mobile'],$this->setting['template_codes'][1],$sms_content);
                    if($resp['Code'] == 'OK'){
                        $notices_log['status'] = "1";
                        $info['sms_code'] = "OK";
                    }else{
                        $notices_log['status'] = "0";
                        $info['sms_msg'] = $resp['Message'];
                    }
                }
                $notices_log['create_time'] = new MongoDB\BSON\UTCDateTime(time()*1000);
                $this->CI->noticeslog_model->save_notices_log($notices_log);
            }
        }
        if($this->setting['is_email'] && in_array("20",$workorder['send_type'])){
            $notices_log = $this->CI->noticeslog_model->get_model();
            $notices_log['log_id'] = $this->create_id();
            $notices_log['type'] = "email";
            $notices_log['subject'] = "工单ID:".$workorder['workorder_id']."已由责任人:".$receive_user['name']."签收";
            $email_content = '<div style="font-size:12px;">';
            $email_content .='<p style="white-space: normal; background-color: rgb(255, 255, 255);">系统管理员,您好!</p>';
            $email_content .='<p style="white-space: normal; background-color: rgb(255, 255, 255); text-indent: 2em;">以下工单已由责任人:'.$receive_user['name'].'签收:</p>';
            $email_content .='<table align="left" border="1" cellspacing="0" cellpadding="0" style="font-size:12px;"><thead>';
            $email_content .='<tr class="firstRow"><td valign="top" rowspan="1" colspan="5" style="word-break: break-all;">';
            $email_content .='<strong>工单ID:'.$workorder['workorder_id'].'</strong><strong>工单名称:'.$workorder['workorder_name'].'</strong>';
            $email_content .='</td></tr>';
            $email_content .='<tr>';
            $email_content .='<th width="147" valign="middle" style="word-break: break-all;" align="center"><strong>工单名称</strong></th>';
            $email_content .='<th width="147" valign="middle" style="word-break: break-all;" align="center"><strong>类型</strong></th>';
            $email_content .='<th width="147" valign="middle" style="word-break: break-all;" align="center"><strong>等级</strong></th>';
            $email_content .='<th width="147" valign="middle" style="word-break: break-all;" align="center"><strong>时间</strong></th>';
            $email_content .='<th width="147" valign="middle" style="word-break: break-all;" align="center"><strong>告警详情</strong></th>';
            $email_content .='</tr></thead><tbody>';
            foreach($workorder['warning_list'] as $key=>$val) {
                $email_content .= '<tr>';
                $email_content .= '<td valign="top" colspan="1" rowspan="1">'.$val['warning_name'].'</td>';
                $email_content .= '<td valign="middle" colspan="1" rowspan="1" align="center">'.$this->warning_type[$val['type']].'</td>';
                $email_content .= '<td valign="middle" colspan="1" rowspan="1" align="center">'.$this->warning_level[$val['level']].'</td>';
                $email_content .= '<td valign="middle" colspan="1" rowspan="1" align="center">'.date("Y-m-d H:i:s",$val['create_time']->toDateTime()->getTimestamp()).'</td>';
                $email_content .= '<td valign="top" colspan="1" rowspan="1">'.$val['content'].'</td>';
                $email_content .= '</tr>';
            }
            $email_content .='<tr>';
            $email_content .='<td valign="top" rowspan="1" colspan="5" style="word-break: break-all;"><strong>备注:</strong>'.$remark['remark_content'].'</td>';
            $email_content .='</tr>';
            $email_content .='</tbody></table>';
            $email_content .='<p><br/>&nbsp;</p><p><br/>&nbsp;</p>';
            $email_content .='<p style="text-align: right;">责任人:'.$receive_user['name'].'&nbsp;<br>';
            $email_content .=date("Y年m月d日",time()).'</p>';
            $email_content .='</div>';
            $notices_log['recipient'] = implode(",",$emails);
            $notices_log['content'] = $email_content;
            for ($i=0;$i<count($emails);$i++) {
                $this->CI->mailer->add_recipient($emails[$i]);
            }
            $resp = $this->CI->mailer->send_email("", $notices_log['subject'], $notices_log['content']);
            if($resp['code'] == 'OK'){
                $notices_log['status'] = "1";
                $info['email_code'] = "OK";
            }else{
                $notices_log['status'] = "0";
                $info['email_msg'] = $resp['msg'];
            }
            $notices_log['create_time'] = new MongoDB\BSON\UTCDateTime(time()*1000);
            $this->CI->noticeslog_model->save_notices_log($notices_log);
        }
        return $info;
    }

    /**
     * 核查工单未发现问题
     * @param $workorder
     * @param $remark
     */
    public function check_workorder($workorder,$remark){
        $info = array();
        $info['sms_code'] = "FAILED";
        $info['email_code'] = "FAILED";
        $receive_user = $this->CI->user_model->get_user_with_user_id($workorder['receive_user_id']);
        $admin_list = $this->CI->user_model->select_users("1");
        $sso_admin_list = $this->CI->user_model->set_collection_name("sso_users")->select_users("1");
        $mobiles = array();
        $emails = array();
        foreach ($admin_list as $key => $val){
            $mobiles[] = $val['mobile'];
            $emails[] = $val['email'];
        }
        foreach ($sso_admin_list as $key => $val){
            $mobiles[] = $val['mobile'];
            $emails[] = $val['email'];
        }
        if($this->setting['is_sms'] && in_array("10",$workorder['send_type'])){
            $sms_content = '工单ID:'.$workorder['workorder_id'].',工单名称:'.$workorder['workorder_name'].',类型:'.$workorder['type'].',已由责任人'.$receive_user['name'].'核查无问题,备注:'.$remark['remark_content'];
            for($i = 0;$i<count($mobiles);$i++) {
                $notices_log = $this->CI->noticeslog_model->get_model();
                $notices_log['type'] = "sms";
                $notices_log['content'] = $sms_content;
                $notices_log['log_id'] = $this->create_id();
                $notices_log['mobile'] = $mobiles[$i];
                if($this->setting['sms_type'] == '10' ){
                    if(send_by_modem($this->setting, $notices_log['mobile'], $sms_content)){
                        $notices_log['status'] = "1";
                        $info['sms_code'] = "OK";
                    }else{
                        $notices_log['status'] = "0";
                        $info['sms_msg'] = "短信发送失败!端口被占用或者配置错误!";
                    }
                }elseif($this->setting['sms_type'] == '20' ){
                    $sms_content = array(
                        "workorder_id"=>$workorder['workorder_id'],
                        "workorder_name"=>strlen($workorder['workorder_name'])<=18?$workorder['workorder_name']:substr($workorder['workorder_name'],0,18)."..",
                        "workorder_type"=>$workorder['type'],
                        "receive_user_name"=>$receive_user['name'],
                        "remark_content"=>strlen($workorder['remark_list'][count($workorder['remark_list'])-1]['remark_content'])<=18?$workorder['remark_list'][count($workorder['remark_list'])-1]['remark_content']:substr($workorder['remark_list'][count($workorder['remark_list'])-1]['remark_content'],0,18).".."
                    );
                    $resp = $this->Aliyunsms->sendSms($notices_log['mobile'],$this->setting['template_codes'][2],$sms_content);
                    if($resp['Code'] == 'OK'){
                        $notices_log['status'] = "1";
                        $info['sms_code'] = "OK";
                    }else{
                        $notices_log['status'] = "0";
                        $info['sms_msg'] = $resp['Message'];
                    }
                }
                $notices_log['create_time'] = new MongoDB\BSON\UTCDateTime(time()*1000);
                $this->CI->noticeslog_model->save_notices_log($notices_log);
            }
        }
        if($this->setting['is_email'] && in_array("20",$workorder['send_type'])){
            $notices_log = $this->CI->noticeslog_model->get_model();
            $notices_log['log_id'] = $this->create_id();
            $notices_log['type'] = "email";
            $notices_log['subject'] = "工单ID:".$workorder['workorder_id']."已由责任人:".$receive_user['name']."核查无问题";
            $email_content = '<div style="font-size:12px;">';
            $email_content .='<p style="white-space: normal; background-color: rgb(255, 255, 255);">系统管理员,您好!</p>';
            $email_content .='<p style="white-space: normal; background-color: rgb(255, 255, 255); text-indent: 2em;">以下工单已由责任人:'.$receive_user['name'].'核查无问题:</p>';
            $email_content .='<table align="left" border="1" cellspacing="0" cellpadding="0" style="font-size:12px;"><thead>';
            $email_content .='<tr class="firstRow"><td valign="top" rowspan="1" colspan="5" style="word-break: break-all;">';
            $email_content .='<strong>工单ID:'.$workorder['workorder_id'].'</strong><strong>工单名称:'.$workorder['workorder_name'].'</strong>';
            $email_content .='</td></tr>';
            $email_content .='<tr>';
            $email_content .='<th width="147" valign="middle" style="word-break: break-all;" align="center"><strong>工单名称</strong></th>';
            $email_content .='<th width="147" valign="middle" style="word-break: break-all;" align="center"><strong>类型</strong></th>';
            $email_content .='<th width="147" valign="middle" style="word-break: break-all;" align="center"><strong>等级</strong></th>';
            $email_content .='<th width="147" valign="middle" style="word-break: break-all;" align="center"><strong>时间</strong></th>';
            $email_content .='<th width="147" valign="middle" style="word-break: break-all;" align="center"><strong>告警详情</strong></th>';
            $email_content .='</tr></thead><tbody>';
            foreach($workorder['warning_list'] as $key=>$val) {
                $email_content .= '<tr>';
                $email_content .= '<td valign="top" colspan="1" rowspan="1">'.$val['warning_name'].'</td>';
                $email_content .= '<td valign="middle" colspan="1" rowspan="1" align="center">'.$this->warning_type[$val['type']].'</td>';
                $email_content .= '<td valign="middle" colspan="1" rowspan="1" align="center">'.$this->warning_level[$val['level']].'</td>';
                $email_content .= '<td valign="middle" colspan="1" rowspan="1" align="center">'.date("Y-m-d H:i:s",$val['create_time']->toDateTime()->getTimestamp()).'</td>';
                $email_content .= '<td valign="top" colspan="1" rowspan="1">'.$val['content'].'</td>';
                $email_content .= '</tr>';
            }
            $email_content .='<tr>';
            $email_content .='<td valign="top" rowspan="1" colspan="5" style="word-break: break-all;"><strong>备注:</strong>'.$remark['remark_content'] .'</td>';
            $email_content .='</tr>';
            $email_content .='</tbody></table>';
            $email_content .='<p><br/>&nbsp;</p><p><br/>&nbsp;</p>';
            $email_content .='<p style="text-align: right;">责任人:'.$receive_user['name'].'&nbsp;<br>';
            $email_content .=date("Y年m月d日",time()).'</p>';
            $email_content .='</div>';
            $notices_log['recipient'] = implode(",",$emails);
            $notices_log['content'] = $email_content;
            for ($i=0;$i<count($emails);$i++) {
                $this->CI->mailer->add_recipient($emails[$i]);
            }
            if($remark['remark_file']){
                $attachments = getcwd().'/static/upload'.$remark['remark_file'];
            }else{
                $attachments = null;
            }
            $resp = $this->CI->mailer->send_email("", $notices_log['subject'], $notices_log['content'],$attachments);
            if($resp['code'] == 'OK'){
                $notices_log['status'] = "1";
                $info['email_code'] = "OK";
            }else{
                $notices_log['status'] = "0";
                $info['email_msg'] = $resp['msg'];
            }
            $notices_log['create_time'] = new MongoDB\BSON\UTCDateTime(time()*1000);
            $this->CI->noticeslog_model->save_notices_log($notices_log);
        }
        return $info;
    }

    /**
     * 工单已处理
     * @param $workorder
     * @param $remark
     */
    public function doit_workorder($workorder,$remark){
        $info = array();
        $info['sms_code'] = "FAILED";
        $info['email_code'] = "FAILED";
        $receive_user = $this->CI->user_model->get_user_with_user_id($workorder['receive_user_id']);
        $admin_list = $this->CI->user_model->select_users("1");
        $sso_admin_list = $this->CI->user_model->set_collection_name("sso_users")->select_users("1");
        $mobiles = array();
        $emails = array();
        foreach ($admin_list as $key => $val){
            $mobiles[] = $val['mobile'];
            $emails[] = $val['email'];
        }
        foreach ($sso_admin_list as $key => $val){
            $mobiles[] = $val['mobile'];
            $emails[] = $val['email'];
        }
        if($this->setting['is_sms'] && in_array("10",$workorder['send_type'])){
            $sms_content = '工单ID:'.$workorder['workorder_id'].',工单名称:'.$workorder['workorder_name'].',类型:'.$workorder['type'].',已由责任人'.$receive_user['name'].'处理完成,备注:'.$remark['remark_content'];
            for($i = 0;$i<count($mobiles);$i++) {
                $notices_log = $this->CI->noticeslog_model->get_model();
                $notices_log['type'] = "sms";
                $notices_log['content'] = $sms_content;
                $notices_log['log_id'] = $this->create_id();
                $notices_log['mobile'] = $mobiles[$i];
                if($this->setting['sms_type'] == '10' ){
                    if(send_by_modem($this->setting, $notices_log['mobile'], $sms_content)){
                        $notices_log['status'] = "1";
                        $info['sms_code'] = "OK";
                    }else{
                        $notices_log['status'] = "0";
                        $info['sms_msg'] = "短信发送失败!端口被占用或者配置错误!";
                    }
                }elseif($this->setting['sms_type'] == '20' ){
                    $sms_content = array(
                        "workorder_id"=>$workorder['workorder_id'],
                        "workorder_name"=>strlen($workorder['workorder_name'])<=18?$workorder['workorder_name']:substr($workorder['workorder_name'],0,18)."..",
                        "workorder_type"=>$workorder['type'],
                        "receive_user_name"=>$receive_user['name'],
                        "remark_content"=>strlen($workorder['remark_list'][count($workorder['remark_list'])-1]['remark_content'])<=18?$workorder['remark_list'][count($workorder['remark_list'])-1]['remark_content']:substr($workorder['remark_list'][count($workorder['remark_list'])-1]['remark_content'],0,18).".."
                    );
                    $resp = $this->Aliyunsms->sendSms($notices_log['mobile'],$this->setting['template_codes'][3],$sms_content);
                    if($resp['Code'] == 'OK'){
                        $notices_log['status'] = "1";
                    }else{
                        $notices_log['status'] = "0";
                        $info['sms_msg'] = $resp['Message'];
                    }
                }
                $notices_log['create_time'] = new MongoDB\BSON\UTCDateTime(time()*1000);
                $this->CI->noticeslog_model->save_notices_log($notices_log);
            }
        }
        if($this->setting['is_email'] && in_array("20",$workorder['send_type'])){
            $notices_log = $this->CI->noticeslog_model->get_model();
            $notices_log['log_id'] = $this->create_id();
            $notices_log['type'] = "email";
            $notices_log['subject'] = "工单ID:".$workorder['workorder_id']."已由责任人:".$receive_user['name']."处理完成";
            $email_content = '<div style="font-size:12px;">';
            $email_content .='<p style="white-space: normal; background-color: rgb(255, 255, 255);">系统管理员,您好!</p>';
            $email_content .='<p style="white-space: normal; background-color: rgb(255, 255, 255); text-indent: 2em;">以下工单已由责任人:'.$receive_user['name'].'处理完成:</p>';
            $email_content .='<table align="left" border="1" cellspacing="0" cellpadding="0" style="font-size:12px;"><thead>';
            $email_content .='<tr class="firstRow"><td valign="top" rowspan="1" colspan="5" style="word-break: break-all;">';
            $email_content .='<strong>工单ID:'.$workorder['workorder_id'].'</strong><strong>工单名称:'.$workorder['workorder_name'].'</strong>';
            $email_content .='</td></tr>';
            $email_content .='<tr>';
            $email_content .='<th width="147" valign="middle" style="word-break: break-all;" align="center"><strong>工单名称</strong></th>';
            $email_content .='<th width="147" valign="middle" style="word-break: break-all;" align="center"><strong>类型</strong></th>';
            $email_content .='<th width="147" valign="middle" style="word-break: break-all;" align="center"><strong>等级</strong></th>';
            $email_content .='<th width="147" valign="middle" style="word-break: break-all;" align="center"><strong>时间</strong></th>';
            $email_content .='<th width="147" valign="middle" style="word-break: break-all;" align="center"><strong>告警详情</strong></th>';
            $email_content .='</tr></thead><tbody>';
            foreach($workorder['warning_list'] as $key=>$val) {
                $email_content .= '<tr>';
                $email_content .= '<td valign="top" colspan="1" rowspan="1">'.$val['warning_name'].'</td>';
                $email_content .= '<td valign="middle" colspan="1" rowspan="1" align="center">'.$this->warning_type[$val['type']].'</td>';
                $email_content .= '<td valign="middle" colspan="1" rowspan="1" align="center">'.$this->warning_level[$val['level']].'</td>';
                $email_content .= '<td valign="middle" colspan="1" rowspan="1" align="center">'.date("Y-m-d H:i:s",$val['create_time']->toDateTime()->getTimestamp()).'</td>';
                $email_content .= '<td valign="top" colspan="1" rowspan="1">'.$val['content'].'</td>';
                $email_content .= '</tr>';
            }
            $email_content .='<tr>';
            $email_content .='<td valign="top" rowspan="1" colspan="5" style="word-break: break-all;"><strong>备注:</strong>'.$remark['remark_content'] .'</td>';
            $email_content .='</tr>';
            $email_content .='</tbody></table>';
            $email_content .='<p><br/>&nbsp;</p><p><br/>&nbsp;</p>';
            $email_content .='<p style="text-align: right;">责任人:'.$receive_user['name'].'&nbsp;<br>';
            $email_content .=date("Y年m月d日",time()).'</p>';
            $email_content .='</div>';
            $notices_log['recipient'] = implode(",",$emails);
            $notices_log['content'] = $email_content;
            for ($i=0;$i<count($emails);$i++) {
                $this->CI->mailer->add_recipient($emails[$i]);
            }
            if($remark['remark_file']){
                $attachments = getcwd().'/static/upload'.$remark['remark_file'];
            }else{
                $attachments = null;
            }
            $resp = $this->CI->mailer->send_email("", $notices_log['subject'], $notices_log['content'],$attachments);
            if($resp['code'] == 'OK'){
                $notices_log['status'] = "1";
                $info['email_code'] = "OK";
            }else{
                $notices_log['status'] = "0";
                $info['email_msg'] = $resp['msg'];
            }
            $notices_log['create_time'] = new MongoDB\BSON\UTCDateTime(time()*1000);
            $this->CI->noticeslog_model->save_notices_log($notices_log);
        }
        return $info;
    }

    /**
     * 驳回工单
     * @param $workorder
     * @param $remark
     */
    public function reject_workorder($workorder,$remark){
        $info = array();
        $info['sms_code'] = "FAILED";
        $info['email_code'] = "FAILED";
        $receive_user = $this->CI->user_model->get_user_with_user_id($workorder['receive_user_id']);
        if($this->setting['is_sms'] && in_array("10",$workorder['send_type'])){
            $sms_content = '你的工单已由管理员驳回,请重新处理。工单ID:'.$workorder['workorder_id'].',工单名称:'.$workorder['workorder_name'].',类型:'.$workorder['type'].',备注:'.$remark['remark_content'];
            $notices_log = $this->CI->noticeslog_model->get_model();
            $notices_log['log_id'] = $this->create_id();
            $notices_log['type'] = "sms";
            $notices_log['mobile'] = $receive_user['mobile'];
            $notices_log['content'] = $sms_content;
            if($this->setting['sms_type'] == '10' ){
                if(send_by_modem($this->setting, $receive_user['mobile'], $sms_content)){
                    $notices_log['status'] = "1";
                    $info['sms_code'] = "OK";
                }else{
                    $notices_log['status'] = "0";
                    $info['sms_msg'] = "短信发送失败!端口被占用或者配置错误!";
                }
            }elseif($this->setting['sms_type'] == '20' ){
                $sms_content = array(
                    "workorder_id"=>$workorder['workorder_id'],
                    "workorder_name"=>strlen($workorder['workorder_name'])<=18?$workorder['workorder_name']:substr($workorder['workorder_name'],0,18)."..",
                    "workorder_type"=>$workorder['type'],
                    "remark_content"=>strlen($workorder['remark_list'][count($workorder['remark_list'])-1]['remark_content'])<=18?$workorder['remark_list'][count($workorder['remark_list'])-1]['remark_content']:substr($workorder['remark_list'][count($workorder['remark_list'])-1]['remark_content'],0,18).".."
                );
                $resp = $this->Aliyunsms->sendSms($receive_user['mobile'],$this->setting['template_codes'][4],$sms_content);
                if($resp['Code'] == 'OK'){
                    $notices_log['status'] = "1";
                    $info['sms_code'] = "OK";
                }else{
                    $notices_log['status'] = "0";
                    $info['sms_msg'] = $resp['Message'];
                }
            }
            $notices_log['create_time'] = new MongoDB\BSON\UTCDateTime(time()*1000);
            $this->CI->noticeslog_model->save_notices_log($notices_log);
        }
        if($this->setting['is_email'] && in_array("20",$workorder['send_type'])){
            $notices_log = $this->CI->noticeslog_model->get_model();
            $notices_log['log_id'] = $this->create_id();
            $notices_log['type'] = "email";
            $notices_log['recipient'] = $receive_user['email'];
            $notices_log['subject'] = "你的工单已由管理员驳回,请重新处理,工单ID:".$workorder['workorder_id'];
            $email_content = '<div style="font-size:12px;">';
            $email_content .='<p style="white-space: normal; background-color: rgb(255, 255, 255);">'.$receive_user['name'].',您好!</p>';
            $email_content .='<p style="white-space: normal; background-color: rgb(255, 255, 255); text-indent: 2em;">以下工单已由管理员驳回,请重新处理:</p>';
            $email_content .='<table align="left" border="1" cellspacing="0" cellpadding="0" style="font-size:12px;"><thead>';
            $email_content .='<tr class="firstRow"><td valign="top" rowspan="1" colspan="5" style="word-break: break-all;">';
            $email_content .='<strong>工单ID:'.$workorder['workorder_id'].'</strong><strong>工单名称:'.$workorder['workorder_name'].'</strong>';
            $email_content .='</td></tr>';
            $email_content .='<tr>';
            $email_content .='<th width="147" valign="middle" style="word-break: break-all;" align="center"><strong>工单名称</strong></th>';
            $email_content .='<th width="147" valign="middle" style="word-break: break-all;" align="center"><strong>类型</strong></th>';
            $email_content .='<th width="147" valign="middle" style="word-break: break-all;" align="center"><strong>等级</strong></th>';
            $email_content .='<th width="147" valign="middle" style="word-break: break-all;" align="center"><strong>时间</strong></th>';
            $email_content .='<th width="147" valign="middle" style="word-break: break-all;" align="center"><strong>告警详情</strong></th>';
            $email_content .='</tr></thead><tbody>';
            foreach($workorder['warning_list'] as $key=>$val) {
                $email_content .= '<tr>';
                $email_content .= '<td valign="top" colspan="1" rowspan="1">'.$val['warning_name'].'</td>';
                $email_content .= '<td valign="middle" colspan="1" rowspan="1" align="center">'.$this->warning_type[$val['type']].'</td>';
                $email_content .= '<td valign="middle" colspan="1" rowspan="1" align="center">'.$this->warning_level[$val['level']].'</td>';
                $email_content .= '<td valign="middle" colspan="1" rowspan="1" align="center">'.date("Y-m-d H:i:s",$val['create_time']->toDateTime()->getTimestamp()).'</td>';
                $email_content .= '<td valign="top" colspan="1" rowspan="1">'.$val['content'].'</td>';
                $email_content .= '</tr>';
            }
            $email_content .='<tr>';
            $email_content .='<td valign="top" rowspan="1" colspan="5" style="word-break: break-all;"><strong>备注:</strong>'.$remark['remark_content'] .'</td>';
            $email_content .='</tr>';
            $email_content .='</tbody></table>';
            $email_content .='<p><br/>&nbsp;</p><p><br/>&nbsp;</p>';
            $email_content .='<p style="text-align: right;">系统管理员&nbsp;&nbsp;<br>';
            $email_content .=date("Y年m月d日",time()).'</p>';
            $email_content .='</div>';
            $notices_log['content'] = $email_content;
            $resp = $this->CI->mailer->send_email($receive_user['email'],$notices_log['subject'],$notices_log['content']);
            if($resp['code'] == 'OK'){
                $notices_log['status'] = "1";
                $info['email_code'] = "OK";
            }else{
                $notices_log['status'] = "0";
                $info['email_msg'] = $resp['msg'];
            }
            $notices_log['create_time'] = new MongoDB\BSON\UTCDateTime(time()*1000);
            $this->CI->noticeslog_model->save_notices_log($notices_log);
        }
        return $info;
    }

    /**
     * 完成工单
     * @param $workorder
     */
    public function finish_workorder($workorder){
        $info = array();
        $info['sms_code'] = "FAILED";
        $info['email_code'] = "FAILED";
        $receive_user = $this->CI->user_model->get_user_with_user_id($workorder['receive_user_id']);
        if($this->setting['is_sms'] && in_array("10",$workorder['send_type'])){
            $sms_content = '你的工单已由管理员结束,工单ID:'.$workorder['workorder_id'].',工单名称:'.$workorder['workorder_name'].',类型:'.$workorder['type'];
            $notices_log = $this->CI->noticeslog_model->get_model();
            $notices_log['log_id'] = $this->create_id();
            $notices_log['type'] = "sms";
            $notices_log['mobile'] = $receive_user['mobile'];
            $notices_log['content'] = $sms_content;
            if($this->setting['sms_type'] == '10' ){
                if(send_by_modem($this->setting, $receive_user['mobile'], $sms_content)){
                    $notices_log['status'] = "1";
                    $info['sms_code'] = "OK";
                }else{
                    $notices_log['status'] = "0";
                    $info['sms_msg'] = "短信发送失败!端口被占用或者配置错误!";
                }
            }elseif($this->setting['sms_type'] == '20' ){
                $sms_content = array(
                    "workorder_id"=>$workorder['workorder_id'],
                    "workorder_name"=>strlen($workorder['workorder_name'])<=18?$workorder['workorder_name']:substr($workorder['workorder_name'],0,18)."..",
                    "workorder_type"=>$workorder['type']
                );
                $resp = $this->Aliyunsms->sendSms($receive_user['mobile'],$this->setting['template_codes'][4],$sms_content);
                if($resp['Code'] == 'OK'){
                    $notices_log['status'] = "1";
                    $info['sms_code'] = "OK";
                }else{
                    $notices_log['status'] = "0";
                    $info['sms_msg'] = $resp['Message'];
                }
            }
            $notices_log['create_time'] = new MongoDB\BSON\UTCDateTime(time()*1000);
            $this->CI->noticeslog_model->save_notices_log($notices_log);
        }
        if($this->setting['is_email'] && in_array("20",$workorder['send_type'])){
            $notices_log = $this->CI->noticeslog_model->get_model();
            $notices_log['log_id'] = $this->create_id();
            $notices_log['type'] = "email";
            $notices_log['recipient'] = $receive_user['email'];
            $notices_log['subject'] = "你的工单已由管理员结束,工单ID:".$workorder['workorder_id'];
            $email_content = '<div style="font-size:12px;">';
            $email_content .='<p style="white-space: normal; background-color: rgb(255, 255, 255);">'.$receive_user['name'].',您好!</p>';
            $email_content .='<p style="white-space: normal; background-color: rgb(255, 255, 255); text-indent: 2em;">以下工单已由管理员确认结束:</p>';
            $email_content .='<table align="left" border="1" cellspacing="0" cellpadding="0" style="font-size:12px;"><thead>';
            $email_content .='<tr class="firstRow"><td valign="top" rowspan="1" colspan="5" style="word-break: break-all;">';
            $email_content .='<strong>工单ID:'.$workorder['workorder_id'].'</strong><strong>工单名称:'.$workorder['workorder_name'].'</strong>';
            $email_content .='</td></tr>';
            $email_content .='<tr>';
            $email_content .='<th width="147" valign="middle" style="word-break: break-all;" align="center"><strong>工单名称</strong></th>';
            $email_content .='<th width="147" valign="middle" style="word-break: break-all;" align="center"><strong>类型</strong></th>';
            $email_content .='<th width="147" valign="middle" style="word-break: break-all;" align="center"><strong>等级</strong></th>';
            $email_content .='<th width="147" valign="middle" style="word-break: break-all;" align="center"><strong>时间</strong></th>';
            $email_content .='<th width="147" valign="middle" style="word-break: break-all;" align="center"><strong>告警详情</strong></th>';
            $email_content .='</tr></thead><tbody>';
            foreach($workorder['warning_list'] as $key=>$val) {
                $email_content .= '<tr>';
                $email_content .= '<td valign="top" colspan="1" rowspan="1">'.$val['warning_name'].'</td>';
                $email_content .= '<td valign="middle" colspan="1" rowspan="1" align="center">'.$this->warning_type[$val['type']].'</td>';
                $email_content .= '<td valign="middle" colspan="1" rowspan="1" align="center">'.$this->warning_level[$val['level']].'</td>';
                $email_content .= '<td valign="middle" colspan="1" rowspan="1" align="center">'.date("Y-m-d H:i:s",$val['create_time']->toDateTime()->getTimestamp()).'</td>';
                $email_content .= '<td valign="top" colspan="1" rowspan="1">'.$val['content'].'</td>';
                $email_content .= '</tr>';
            }
            $email_content .='</tbody></table>';
            $email_content .='<p><br/>&nbsp;</p><p><br/>&nbsp;</p>';
            $email_content .='<p style="text-align: right;">系统管理员&nbsp;&nbsp;<br>';
            $email_content .=date("Y年m月d日",time()).'</p>';
            $email_content .='</div>';
            $notices_log['content'] = $email_content;
            $resp = $this->CI->mailer->send_email($receive_user['email'],$notices_log['subject'],$notices_log['content']);
            if($resp['code'] == 'OK'){
                $notices_log['status'] = "1";
                $info['email_code'] = "OK";
            }else{
                $notices_log['status'] = "0";
                $info['email_msg'] = $resp['msg'];
            }
            $notices_log['create_time'] = new MongoDB\BSON\UTCDateTime(time()*1000);
            $this->CI->noticeslog_model->save_notices_log($notices_log);
        }
        return $info;
    }

    /**
     * 生成ID
     * @return string
     */
    public function create_id()
    {
        return date('Ymd') . substr(implode(NULL, array_map('ord', str_split(substr(uniqid(), 7, 13), 1))), 0, 8);
    }
}