<?php

namespace Platform\Api\WorkOrder;

use Base\Domain\Problem as ProblemDomain;
use Base\Domain\WorkOrder as WorkOrderDomain;
use Platform\Common\Api;

/**
 * 工单
 */
class WorkOrder extends Api {

    public function getRules() {
        return array(
            'getModuleLst' => array(
            ),
            'getAreaCodeLst' => array(
            ),
            'addWorkOrder' => array(
                'title' => array('name' => 'title', 'require' => true, 'max' => '255', 'desc' => '工单标题'),
                'moduleId' => array('name' => 'module_id', 'require' => true, 'type'=>'int',   'desc' => '模块id'),
                'problemDesc' => array('name' => 'problem_desc', 'require' => true, 'desc' => '工单工单描述 '),
                'phonePre' => array('name' => 'phone_pre', 'default' => '', 'desc' => '手机前缀'),
                'phone' => array('name' => 'phone', 'require' => true, 'desc' => '联系手机'),
                'email' => array('name' => 'email', 'default' => '', 'desc' => '邮箱号码'),
                'fileIds' => array('name' => 'file_ids', 'default' => '', 'desc' => '附件ids 多个文件用，号隔开'),

            ),
            'lstWorkOrder' => array(
                'moduleId'=> array('name' => 'module_id', 'type' => 'int', 'desc' => '模块类型'),
                'status' => array('name' => 'status', 'type' => 'int', 'desc' => '审核状态：1待受理，2待回复 3已解决 4已关闭'),
                'title' => array('name' => 'title', 'desc' => '标题搜索'),
                'sortTime' => array('name' => 'sort_time', 'default' => 0, 'desc' => '时间排序 0降序 1升序'),
                'startTime' => array('name' => 'start_time', 'default' => 0, 'desc' => '时间范围搜索开始时间 格式：2021-01-13 16:27:09 或 2021-01-13'),
                'endTime' => array('name' => 'end_time', 'default' => 0, 'desc' => '时间范围搜索结束时间 格式：2021-01-13 16:27:09 或 2021-01-13'),
                'page' => array('name' => 'page', 'type' => 'int', 'default' => 1,  'desc' => '当前页数'),
                'perpage' => array('name' => 'perpage', 'type' => 'int', 'default' => 10, 'desc' => '每页多少条'),
            ),
            'detailWorkOrder' => array(
                'id' => array('name' => 'id', 'require' => true, 'type' => 'int', 'min' => 1, 'desc' => '工单id'),
            ),
            'updateChatRecodeWorkOrder' => array(
                'id' => array('name' => 'id', 'require' => true, 'desc' => '工单id'),
                'status' => array('name' => 'status', 'desc' => '审核'),
                'chatRecode' => array('name' => 'chat_recode','max' => '255', 'desc' => '回复'),
                'chatRecodeFile' => array('name' => 'chat_recode_file', 'max' => '255', 'desc' => "回复时的附件 json形式['url','url']"),
            )
        );
    }

    /**
     * 获得，模块列表接口
     * @desc 工单 获得，模块列表接口
     */
    public function getModuleLst(){
        $workOrderDomain = new WorkOrderDomain();
        return  $workOrderDomain->getModuleLst();
    }

    /**
     * 获取区号接口
     */
//    public function getAreaCodeLst(){
//        $workOrderDomain = new WorkOrderDomain();
//        return  $workOrderDomain->getAreaCodeLst();
//    }

    /**
     * 添加工单
     * @desc 添加工单
     */
    public function addWorkOrder(){
        $rs = array('id' => 0);
        $workOrderDomain = new WorkOrderDomain();

        $rs['id'] = $workOrderDomain->add($this->title, $this->moduleId, $this->problemDesc,$this->phone, $this->fileIds,$this->email,$this->phonePre);

        return  $rs['id'];
    }

    /**
     * 工单列表
     *@desc 工单列表
     */
    public function lstWorkOrder(){
        $workOrderDomain = new WorkOrderDomain();
        $searchWord = [
            'status'=>$this->unntilIsEmpty($this->status),
            'module_id'=>$this->unntilIsEmpty($this->moduleId),
        ];
        if (!empty($this->title)){
            $searchWord['title LIKE ?'] = "%{$this->title}%";
        }

        //时间范围限定
        if (!empty($this->startTime) && !empty($this->endTime) && ($this->startTime<$this->endTime) ){
            $searchWord['update_time > ?'] = $this->startTime;
            $searchWord['update_time < ?'] = $this->endTime;
        }

        //数组去空
        $searchWord =  array_filter($searchWord);
        $rs = $workOrderDomain->lst($searchWord, $this->page, $this->perpage, $this->sortTime);

        if (empty($rs)){
            $rs['total'] = 0;
            $rs['items'] = [];
        }else{
            $numi = 1;
            foreach ($rs['items'] as $k=>&$v){
                $v['sort_id'] = (($this->page-1)*$this->perpage)+$numi;
                //年月日时分秒去掉秒
                $v['add_time_y_m_d_h_i'] = date('Y-m-d H:i',strtotime($v['add_time']));
                $v['update_time_y_m_d_h_i'] = date('Y-m-d H:i',strtotime($v['update_time']));
                ++$numi;
            }
        }
        return $rs;
    }

    /**
     * 工单详情
     * @desc 工单列表
     *
     * @return int id 这是id
     */
    public function detailWorkOrder()
    {
        $workOrderDomain = new WorkOrderDomain();
        $rs = $workOrderDomain->detail($this->id);
        return $rs;
    }

    /**
     * 工单更新
     * @desc 局部更新
     */
    public function updateChatRecodeWorkOrder(){

        $workOrderDomain = new WorkOrderDomain();
        return $workOrderDomain->updateChatRecodeWorkOrder($this->id, $this->chatRecode, $this->status, $this->chatRecodeFile);

        return true;
    }

}
