<?php
defined('BASEPATH') OR exit('No direct script access allowed');

/**
 * Class 工单管理类
 */
class Workorder extends MY_Controller
{
    function __construct()
    {
        parent::__construct();
        $this->load->model("warning_model");
        $this->load->model("workorder_model");
        $this->load->model("user_model");
        $this->load->library('MY_pagination');
        $this->load->library('notices');
        $this->load->helper(array('url', 'form', 'date'));
        $this->assign("warning_type", $this->warning_type);
        $this->assign("warning_level", $this->warning_level);
        $this->assign("warning_status", $this->warning_status);
        $this->assign("send_type", $this->send_type);
        $this->assign("workorder_status", $this->workorder_status);
        $now = new DateTime();
        $now = $now->modify("-".$this->setting['save_time']."day");
        $now = new \MongoDB\BSON\UTCDateTime($now->getTimestamp()*1000);
        $this->workorder_model->filter_finished_workorder($now);
    }

    /**
     * 各个状态工单数量
     */
    private function counts()
    {
        $count10 = $this->workorder_model->count_workorder(null, $wheres = array('status' => "10"));
        $count20 = $this->workorder_model->count_workorder(null, $wheres = array('status' => "20"));
        $count30 = $this->workorder_model->count_workorder(null, $wheres = array('status' => "30"));
        $count40 = $this->workorder_model->count_workorder(null, $wheres = array('status' => "40"));
        $count50 = $this->workorder_model->count_workorder(null, $wheres = array('status' => "50"));
        $this->assign("count10", $count10);
        $this->assign("count20", $count20);
        $this->assign("count30", $count30);
        $this->assign("count40", $count40);
        $this->assign("count50", $count50);
    }

    /**
     * 工单列表页
     */
    public function index()
    {
        $url = site_url("workorder/index?");
        $wheres = array();
        $order_info = array();
        $keyword = $this->input->get("keyword", TRUE);
        $type = $this->input->get("type", TRUE);
        $branch_id = $this->input->get("branch_id",TRUE);
        $status = $this->input->get("status", TRUE);
        $order = $this->input->get("order", TRUE);
        $page_num = $this->input->get("per_page", TRUE);
        $page_size = $this->input->get("page_size", TRUE);
        if ($keyword) {
            $url .= "&keyword=" . $keyword;
        }
        if ($status) {
            $wheres = array('$and' => array(array('status' => array('$ne' => "50")), array('status' => $status)));
            $url .= "&status=" . $status;
        } else {
            $wheres = array('$and' => array(array('status' => array('$ne' => "50")), array('status' => array('$ne' => "60"))));
        }

        if ($type && array_key_exists($type,$this->warning_type)) {
            $wheres['type'] = $this->warning_type[$type];
            $url .= "&type=" . $type;
        }
        if($branch_id){
            $wheres['branch.branch_id'] = $branch_id;
            $url .= "&branch_id=".$branch_id;
        }
        if ($order) {
            $orders = explode(" ", $order);
            if (count($orders) == 2) {
                $order_info[$orders[0]] = $orders[1];
                $url .= "&order=" . $order;
            }
        }
        if ($page_size) {
            $this->page_size = $page_size;
            $url .= "&page_size=" . $page_size;
        }
        if ($this->session->user_type != "1") {
            $wheres['receive_user_id'] = $this->session->user_id;
        }
        $count = $this->workorder_model->count_workorder($keyword, $wheres);
        $this->assign("count", $count);
        $config = $this->page_config($count, $this->page_size, $url);
        $this->my_pagination->initialize($config);
        if ($page_num && $page_num > 1) {
            $offset = (intval($page_num) - 1) * $this->page_size;
        } else {
            $offset = 0;
        }
        $workorder_list = $this->workorder_model->list_workorder($this->page_size, $offset, $keyword, $wheres, $order_info);
        $workorders = array();
        foreach ($workorder_list as $key => $workorder) {
            $workorder['receive_user'] = $this->user_model->get_user_with_user_id($workorder['receive_user_id']);
            $remarks = array();
            foreach ($workorder['remark_list'] as $key => $remark) {
                $remark['remark_user'] = $this->user_model->get_user_with_user_id($remark['remark_user_id']);
                $remarks[] = $remark;
            }
            $workorder['remark_list'] = $remarks;
            if(count($workorder['remark_list'])>0) {
                $workorder['last_remark_user'] = $remarks[count($remarks) - 1]['remark_user'];
                $workorder['last_remark_content'] = $remarks[count($remarks) - 1]['remark_content'];
                $workorder['last_remark_time'] = $remarks[count($remarks) - 1]['remark_time'];
            }else{
                $workorder['last_remark_user'] = "";
                $workorder['last_remark_content'] = "";
                $workorder['last_remark_time'] = "";
            }
            $workorders[] = $workorder;
        }
        $this->counts();
        $this->assign("keyword", $keyword);
        $this->assign("type", $type);
        $this->assign("branch_id",$branch_id);
        $this->assign("status", $status);
        $this->assign("order", $order);
        $this->assign("page_size", $this->page_size);
        $this->assign("page", $this->my_pagination->create_pages());
        $this->assign("workorder_list", $workorders);
        $this->display("workorder/index.html");
    }

    /**
     * 已完成工单列表页
     */
    public function finished()
    {
        $url = site_url("workorder/finished?");
        $wheres = NULL;
        $order_info = array();
        $keyword = $this->input->get("keyword", TRUE);
        $type = $this->input->get("type", TRUE);
        $branch_id = $this->input->get("branch_id",TRUE);
        $order = $this->input->get("order", TRUE);
        $page_num = $this->input->get("per_page", TRUE);
        $page_size = $this->input->get("page_size", TRUE);
        if ($keyword) {
            $url .= "&keyword=" . $keyword;
        }
        $wheres = array('$and' => array(array('status' => array('$ne' => "60")), array('status' => "50")));
        if ($type) {
            $wheres['type'] = $type;
            $url .= "&type=" . $type;
        }
        if($branch_id){
            $wheres['branch.branch_id'] = $branch_id;
            $url .= "&branch_id=".$branch_id;
        }
        if ($order) {
            $orders = explode(" ", $order);
            if (count($orders) == 2) {
                $order_info[$orders[0]] = $orders[1];
                $url .= "&order=" . $order;
            }
        }
        if ($page_size) {
            $this->page_size = $page_size;
            $url .= "&page_size=" . $page_size;
        }
        if ($this->session->user_type != "1") {
            $wheres['receive_user_id'] = $this->session->user_id;
        }
        $count = $this->workorder_model->count_workorder($keyword, $wheres);
        $this->assign("count", $count);
        $config = $this->page_config($count, $this->page_size, $url);
        $this->my_pagination->initialize($config);
        if ($page_num && $page_num > 1) {
            $offset = (intval($page_num) - 1) * $this->page_size;
        } else {
            $offset = 0;
        }
        $workorder_list = $this->workorder_model->list_workorder($this->page_size, $offset, $keyword, $wheres, $order_info);
        $workorders = array();
        foreach ($workorder_list as $key => $workorder) {
            $workorder['receive_user'] = $this->user_model->get_user_with_user_id($workorder['receive_user_id']);
            $remarks = array();
            foreach ($workorder['remark_list'] as $key => $remark) {
                $remark['remark_user'] = $this->user_model->get_user_with_user_id($remark['remark_user_id']);
                $remarks[] = $remark;
            }
            $workorder['remark_list'] = $remarks;
            if(count($workorder['remark_list'])>0) {
                $workorder['last_remark_user'] = $remarks[count($remarks) - 1]['remark_user'];
                $workorder['last_remark_content'] = $remarks[count($remarks) - 1]['remark_content'];
                $workorder['last_remark_time'] = $remarks[count($remarks) - 1]['remark_time'];
            }else{
                $workorder['last_remark_user'] = "";
                $workorder['last_remark_content'] = "";
                $workorder['last_remark_time'] = "";
            }
            $workorders[] = $workorder;
        }
        $this->assign("keyword", $keyword);
        $this->assign("type", $type);
        $this->assign("branch_id",$branch_id);
        $this->assign("order", $order);
        $this->assign("page_size", $this->page_size);
        $this->assign("page", $this->my_pagination->create_pages());
        $this->assign("workorder_list", $workorders);
        $this->display("workorder/finished.html");
    }

    /**
     * 创建工单
     */
    public function create()
    {
        $warning_ids = $this->input->get("ids");
        $ids = explode(",", $warning_ids);
        $warning_list = array();
        $workorder_name = "";
        $warning_level = "50";
        $branch_id = null;
        foreach ($ids as $key => $val) {
            if($val) {
                $warning = $this->warning_model->get_warning_with_objectid($val);
                if ($warning) {
                    if (intval($warning['level']) <= intval($warning_level)) {
                        $warning_level = $warning['level'];
                        $workorder_name = $warning['warning_name'];
                    }
                    $warning_list[] = $warning;
                    $branch_id = $warning['branch']['branch_id'];
                }
            }
        }
        $user_list = $this->user_model->select_users("2",$branch_id);
        $sso_user_list = $this->user_model->set_collection_name("sso_users")->select_users("2",$branch_id);
        $this->assign("user_list", $user_list);
        $this->assign("sso_user_list", $sso_user_list);
        $this->assign("workorder_name", $workorder_name);
        $this->assign("id", $this->create_id());
        $this->assign("warning_ids", $warning_ids);
        $this->assign("warning_list", $warning_list);
        $this->display("workorder/create.html");
    }

    /**
     * 保存工单
     */
    public function save()
    {
        $is_validate = false;
        $data = array();
        $data['workorder_id'] = $this->input->post("workorder_id", true);
        $data['workorder_name'] = $this->input->post("workorder_name", true);
        $warning_names = $this->input->post("warning_name", true);
        $contents = $this->input->post("content", true);
        $warning_ids = rtrim($this->input->post("warning_ids", true), ",");
        $ids = explode(",", $warning_ids);
        $warning_type = array();
        $branch = null;
        foreach ($ids as $key => $val) {
            $warning = $this->warning_model->get_warning_with_objectid($val);
            if ($warning) {
                $warning_type[] = $warning['type'];
                $warning['warning_name'] = $warning_names[$warning['_id']];
                $warning['content'] = $contents[$warning['_id']];
                $warning_list[] = $warning;
                $branch = $warning['branch'];
            }
        }
        $workorder_warning_type = "";
        $warning_type = array_flip(array_flip($warning_type));
        $index = 0;
        foreach ($warning_type as $key => $val) {
            if ($index < count($warning_type) - 1) {
                if(array_key_exists($val,$this->warning_type)) {
                    $workorder_warning_type .= $this->warning_type[$val] . "、";
                }else{
                    $workorder_warning_type .= $val."、";
                }
            } else {
                if(array_key_exists($val,$this->warning_type)) {
                    $workorder_warning_type .= $this->warning_type[$val];
                }else{
                    $workorder_warning_type .= $val;
                }
            }
            $index++;
        }
        $data['type'] = $workorder_warning_type;
        $data['warning_list'] = $warning_list;
        if ($this->input->post("remark_content", true)) {
            $remarks['remark_content'] = $this->input->post("remark_content", true);
            $remarks['remark_time'] = new MongoDB\BSON\UTCDateTime(time()*1000);
            $remarks['remark_user_id'] = $this->session->user_id;
            $data['remark_list'][] = $remarks;
        } else {
            $data['remark_content'] = "";
            $data['remark_time'] = "";
            $data['remark_user_id'] = "";
            $data['remark_list'] = array();
        }
        $data['send_type'] = $this->input->post("send_type", true);
        if (count($data['send_type']) >= 1) {
            $is_validate = true;
        } else {
            $this->response(array("msg" => "请选择下发方式!", "icon" => 2));
        }
        if ($is_validate) {
            if ($this->input->post("receive_user", true)) {
                $user_info = $this->user_model->get_user_with_user_id($this->input->post("receive_user", true));
                if (!$user_info) {
                    $user_info = $this->user_model->set_collection_name("sso_users")->get_user_with_user_id($this->input->post("receive_user", true));
                }
                if ($user_info) {
                    if ($user_info['status'] == "10") {
                        $data['receive_user_id'] = $this->input->post("receive_user", true);
                        $data['branch'] = $branch;
                        $data['status'] = "10";
                        $data['create_time'] = new MongoDB\BSON\UTCDateTime(time()*1000);
                        $data['end_time'] = "";
                        if ($this->workorder_model->insert_workorder($data)) {
                            $this->update_warning_status($ids);
                            $resp = $this->notices->create_workorder($data);
                            $msg = "工单生成成功!";
                            $icon = 1;
                            if($resp['sms_code'] != 'OK'){
                                $icon = 0;
                                $msg .= "短信发送失败!错误信息:".$resp['sms_msg'];
                            }
                            if($resp['email_code'] != 'OK'){
                                $icon = 0;
                                $msg .= "邮件发送失败!错误信息:".$resp['email_msg'];
                            }
                            $this->response(array("msg" => $msg, "icon" => $icon));
                        } else {
                            $this->response(array("msg" => "工单生成失败!请刷新后重试。", "icon" => 2));
                        }
                    } else {
                        $this->response(array("msg" => "责任人被停用不能指定该责任人!", "icon" => 2));
                    }
                } else {
                    $this->response(array("msg" => "责任人不存在或者已经被删除!", "icon" => 2));
                }
            } else {
                $this->response(array("msg" => "请选择责任人!", "icon" => 2));
            }
        }
    }

    public function update_warning_status($ids){
        if(!is_array($ids)){
            $ids = explode(",", $ids);
        }
        foreach ($ids as $key => $val) {
            $wheres = array('_id' => new  MongoDB\BSON\objectID($val));
            $this->warning_model->set_val("status", $wheres, "30");
        }
    }

    /**
     * 查看工单详情
     * @param $workorder_id 工单ID
     */
    public function view($workorder_id)
    {
        $workorder = $this->workorder_model->get_workorder_with_id($workorder_id);
        $workorder['receive_user'] = $this->user_model->get_user_with_user_id($workorder['receive_user_id']);
        $remarks = array();
        foreach ($workorder['remark_list'] as $key => $remark) {
            $remark['remark_user'] = $this->user_model->get_user_with_user_id($remark['remark_user_id']);
            $remarks[] = $remark;
        }
        $workorder['remark_list'] = $remarks;
        /*$workorder['last_remark_user'] = $remarks[count($remarks) - 1]['remark_user'];
        $workorder['last_remark_content'] = $remarks[count($remarks) - 1]['remark_content'];
        $workorder['last_remark_time'] = $remarks[count($remarks) - 1]['remark_time'];*/
        $this->assign("workorder", $workorder);
        $this->display("workorder/view.html");
    }

    /**
     * 导出工单
     * @param $workorder_id 工单ID
     */
    public function export($workorder_id)
    {
        $workorder = $this->workorder_model->get_workorder_with_id($workorder_id);
        $workorder['receive_user'] = $this->user_model->get_user_with_user_id($workorder['receive_user_id']);
        $remarks = array();
        if (array_key_exists($workorder['branch']['branch_id'], $this->branch_array)) {
            $workorder['branch'] = $this->branch_array[$workorder['branch']['branch_id']]['branch_name'];
        } else {
            $workorder['branch'] = '未配置分支';
        }
        $send_type = "";
        foreach ($workorder['send_type'] as $type) {
            $send_type .= $this->send_type[$type] . "&nbsp;";
        }
        $workorder['send_type'] = $send_type;
        $workorder['status'] = $this->workorder_status[$workorder['status']];
        foreach ($workorder['remark_list'] as $key => $remark) {
            $remark['remark_user'] = $this->user_model->get_user_with_user_id($remark['remark_user_id']);
            $remarks[] = $remark;
        }
        $workorder['remark_list'] = $remarks;
        $workorder['create_time'] = $workorder['create_time']->toDateTime()->format('Y-m-d h:i:s');
        $warnings = '<table style="width: 100%; margin: 0 auto;border-collapse:collapse;"><thead><tr><th style="word-break: break-all; border-width: 1px; border-style: solid; border-color: windowtext;">告警事件名称</th><th style="word-break: break-all; border-width: 1px; border-style: solid; border-color: windowtext;">事件类型</th><th style="word-break: break-all; border-width: 1px; border-style: solid; border-color: windowtext;">主机等级</th><th style="word-break: break-all; border-width: 1px; border-style: solid; border-color: windowtext;">时间</th><th style="word-break: break-all; border-width: 1px; border-style: solid; border-color: windowtext;">告警内容</th></tr></thead><tbody>';
        foreach ($workorder['warning_list'] as $key => $warning) {
            if(!array_key_exists($warning['type'],$this->warning_type)) $warning['type']="unknown";
            $warnings .= '<tr><td style="word-break: break-all; border-width: 1px; border-style: solid; border-color: windowtext;" >' . $warning['warning_name'] . '</td><td style="word-break: break-all; border-width: 1px; border-style: solid; border-color: windowtext;" >' . $this->warning_type[$warning['type']] . '</td><td style="word-break: break-all; border-width: 1px; border-style: solid; border-color: windowtext;" >' . $this->warning_level[$warning['level']] . '</td><td style="word-break: break-all; border-width: 1px; border-style: solid; border-color: windowtext;" >' . $warning['create_time']->toDateTime()->format('Y-m-d h:i:s') . '</td><td style="word-break: break-all; border-width: 1px; border-style: solid; border-color: windowtext;" >' . $warning['content'] . '</td></tr>';
        }
        $warnings .= '</tbody></table>';
        $remarks = '<table style="width: 100%; margin: 0 auto;border-collapse:collapse;"><thead><tr><th style="word-break: break-all; border-width: 1px; border-style: solid; border-color: windowtext; width:10%;" >备注人</th><th style="word-break: break-all; border-width: 1px; border-style: solid; border-color: windowtext;width:20%;" >备注时间</th><th style="word-break: break-all; border-width: 1px; border-style: solid; border-color: windowtext;" >备注内容</th></tr></thead><tbody>';
        foreach ($workorder['remark_list'] as $key => $remark) {
            $remarks .= '<tr><td style="word-break: break-all; border-width: 1px; border-style: solid; border-color: windowtext;" >' . $remark['remark_user']['name'] . '</td><td style="word-break: break-all; border-width: 1px; border-style: solid; border-color: windowtext;" >' . $remark['remark_time']->toDateTime()->format('Y-m-d h:i:s') . '</td><td style="word-break: break-all; border-width: 1px; border-style: solid; border-color: windowtext;" >' . $remark['remark_content'] . '</td>';
        }
        $remarks .= '</tbody></table>';
        $workorder['warnings'] = $warnings;
        $workorder['remarks'] = $remarks;
        $this->assign("workorder", $workorder);
        $this->display("export/workorder.html");
    }

    /**
     * 删除工单
     * @param $workorder_id 工单ID
     */
    public function delete($workorder_id)
    {
        $msg = array();
        $workorder_info = $this->workorder_model->get_workorder_with_id($workorder_id);
        if ($workorder_info) {
            $ids = array();
            foreach ($workorder_info['warning_list'] as $key => $val) {
                $ids[] = $val['warning_id'];
            }
            $wheres = array('warning_id' => array('$in' => $ids));
            $this->warning_model->set_val("status", $wheres, "20");
            $this->workorder_model->delete_workorder($workorder_id);
            $msg['msg'] = "工单删除成功!";
            $msg['icon'] = 1;
        } else {
            $msg['msg'] = "工单不存在或者已经被删除!";
            $msg['icon'] = 2;
        }
        $this->response($msg);
    }

    /**
     * 结束工单
     * @param $workorder_id 工单ID
     */
    public function finish($workorder_id)
    {
        $msg = array();
        if ($this->session->user_type == "1") {
            $workorder_info = $this->workorder_model->get_workorder_with_id($workorder_id);
            if ($workorder_info) {
                $workorder_info['status'] = "50";
                $workorder_info['end_time'] = new \MongoDB\BSON\UTCDateTime(time()*1000);
                $ids = array();
                foreach ($workorder_info['warning_list'] as $key => $val) {
                    $ids[] = $val['warning_id'];
                }
                $wheres = array('warning_id' => array('$in' => $ids));
                $this->warning_model->set_val("status", $wheres, "40");
                $resp = $this->notices->finish_workorder($workorder_info);
                $this->workorder_model->update_workorder($workorder_info);
                $msg['msg'] = "结束工单操作成功!";
                $msg['icon'] = 1;
                if($resp['sms_code'] != 'OK'){
                    $msg['icon'] = 0;
                    $msg['msg'] .= "短信发送失败!错误信息:".$resp['sms_msg'];
                }
                if($resp['email_code'] != 'OK'){
                    $msg['icon'] = 0;
                    $msg['msg'] .= "邮件发送失败!错误信息:".$resp['email_msg'];
                }
            } else {
                $msg['msg'] = "工单不存在或者已经被删除!";
                $msg['icon'] = 2;
            }
        } else {
            $msg['msg'] = "没有权限结束工单!";
            $msg['icon'] = 2;
        }
        $this->response($msg);
    }

    /**
     * 驳回工单
     * @param $workorder_id 工单ID
     */
    public function reject($workorder_id)
    {
        $msg = array();
        $remarks = array("remark_content" => "");
        if ($this->session->user_type == "1") {
            $workorder_info = $this->workorder_model->get_workorder_with_id($workorder_id);
            if ($workorder_info) {
                if ($this->input->post("remark_content", true)) {
                    $remarks['remark_content'] = $this->input->post("remark_content", true);
                    $remarks['remark_time'] = new MongoDB\BSON\UTCDateTime(time()*1000);
                    $remarks['remark_user_id'] = $this->session->user_id;
                    $workorder_info['remark_list'][] = $remarks;
                }
                $workorder_info['status'] = "40";
                $resp = $this->notices->reject_workorder($workorder_info, $remarks);
                $this->workorder_model->update_workorder($workorder_info);
                $msg['msg'] = "驳回工单操作成功!";
                $msg['icon'] = 1;
                if($resp['sms_code'] != 'OK'){
                    $msg['icon'] = 0;
                    $msg['msg'] .= "短信发送失败!错误信息:".$resp['sms_msg'];
                }
                if($resp['email_code'] != 'OK'){
                    $msg['icon'] = 0;
                    $msg['msg'] .= "邮件发送失败!错误信息:".$resp['email_msg'];
                }
            } else {
                $msg['msg'] = "工单不存在或者已经被删除!";
                $msg['icon'] = 2;
            }
        } else {
            $msg['msg'] = "没有权限驳回工单!";
            $msg['icon'] = 2;
        }
        $this->response($msg);
    }

    /**
     * 签收工单
     * @param $workorder_id 工单ID
     */
    public function sign($workorder_id)
    {
        $msg = array();
        $remarks = array("remark_content" => "");
        if ($this->session->user_type == "2") {
            $workorder_info = $this->workorder_model->get_workorder_with_id($workorder_id);
            if ($workorder_info) {
                if ($this->input->post("remark_content", true)) {
                    $remarks['remark_content'] = $this->input->post("remark_content", true);
                    $remarks['remark_time'] = new MongoDB\BSON\UTCDateTime(time()*1000);
                    $remarks['remark_user_id'] = $this->session->user_id;
                    $workorder_info['remark_list'][] = $remarks;
                }
                $workorder_info['status'] = "20";
                $resp = $this->notices->sign_workorder($workorder_info, $remarks);
                $this->workorder_model->update_workorder($workorder_info);
                $msg['msg'] = "签收工单操作成功!";
                $msg['icon'] = 1;
                if($resp['sms_code'] != 'OK'){
                    $msg['icon'] = 0;
                    $msg['msg'] .= "短信发送失败!错误信息:".$resp['sms_msg'];
                }
                if($resp['email_code'] != 'OK'){
                    $msg['icon'] = 0;
                    $msg['msg'] .= "邮件发送失败!错误信息:".$resp['email_msg'];
                }
            } else {
                $msg['msg'] = "工单不存在或者已经被删除!";
                $msg['icon'] = 2;
            }
        } else {
            $msg['msg'] = "只有工程师才可以签收工单!";
            $msg['icon'] = 2;
        }
        $this->response($msg);
    }

    /**
     * 处理工单
     * @param $workorder_id 工单ID
     */
    public function review($workorder_id)
    {
        $msg = array();
        $remarks = array("remark_content" => "");
        $action = $this->input->post("action", true);
        if ($this->session->user_type == "2") {
            $workorder_info = $this->workorder_model->get_workorder_with_id($workorder_id);
            if ($workorder_info) {
                if ($this->input->post("remark_content", true)) {
                    $remarks['remark_content'] = $this->input->post("remark_content", true);
                    $remarks['remark_time'] = new MongoDB\BSON\UTCDateTime(time()*1000);
                    $remarks['remark_user_id'] = $this->session->user_id;
                    $remarks['remark_file'] = $this->input->post("remark_file", true);
                    $workorder_info['remark_list'][] = $remarks;
                }
                $workorder_info['status'] = "30";
                if ($action == "check") {
                    $resp = $this->notices->check_workorder($workorder_info, $remarks);
                } elseif ($action == "doit") {
                    $resp = $this->notices->doit_workorder($workorder_info, $remarks);
                }
                $this->workorder_model->update_workorder($workorder_info);
                $msg['msg'] = "工单处理完成等待复核!";
                $msg['icon'] = 1;
                if($resp['sms_code'] != 'OK'){
                    $msg['icon'] = 0;
                    $msg['msg'] .= "短信发送失败!错误信息:".$resp['sms_msg'];
                }
                if($resp['email_code'] != 'OK'){
                    $msg['icon'] = 0;
                    $msg['msg'] .= "邮件发送失败!错误信息:".$resp['email_msg'];
                }
            } else {
                $msg['msg'] = "工单不存在或者已经被删除!";
                $msg['icon'] = 2;
            }
        } else {
            $msg['msg'] = "只有工程师才可以处理工单!";
            $msg['icon'] = 2;
        }
        $this->response($msg);
    }

    /**
     * 工单附件上传
     */
    public function upload()
    {
        $time = time();
        $year = date('Y', $time);
        $month = date('m', $time);
        $day = date('d', $time);
        $subpath = "/{$year}/{$month}{$day}/";
        $upload_path = getcwd() . '/static/upload' . $subpath;
        if (!file_exists($upload_path)) {
            mkdir($upload_path, 0777, true);
        }
        $config['upload_path'] = $upload_path;
        $config['allowed_types'] = 'gif|jpg|png|rar|zip|7z|doc|docx|xls|xlsx|pdf|crv'; //允许上传的文件类型
        $config['max_size'] = 5120;  //设置上传文件大小 5M
        $config['file_name'] = date('YmdHis', $time) . mt_rand(100, 999);
        $this->load->library('upload', $config);
        if ($this->upload->do_upload('remark_file')) {
            $file = $this->upload->data();
            $this->response(array("code" => 1, "file" => $subpath . $file['orig_name']));
        } else {
            $error = array('code' => 0, 'error' => strip_tags($this->upload->display_errors()));
            $this->response($error);
        }
    }

}