<?php
/**
 * 工单模型
 */
class Workorder_model extends CI_Model {

    private $collection_name = 'workorder';

	public function __construct()
	{
		parent::__construct();
    }

    public function get_workorder_with_id($id){
        return $this->mongo_db->where(array("workorder_id"=> $id))->find_one($this->collection_name);
    }

    public function aggregate($begin_datetime,$end_datetime,$group,$project=NULL,$where=NULL,$limit=NULL,$offset=NULL,$order = NULL){
	    return $this->mongo_db->where_between("create_time",$begin_datetime,$end_datetime)->where($where)->order_by($order)->limit($limit)->offset($offset)->aggregate($this->collection_name,$group,$project);
    }

    public function filter_finished_workorder($now){
        return $this->mongo_db->set_wheres(array('status' => "50", 'end_time' => array('$lt'=>$now)))->set("status","60")->update_all($this->collection_name);
    }

    public function list_workorder($limit,$offset,$like = NULL,$where = NULL,$order = NULL){
        if(!$order){
            $order = array("create_time"=>"DESC");
        }
        if($like && $where){
            if(is_array($where) && array_key_exists("type",$where)) {
                $where['type']=$this->mongo_db->create_like($where['type']);
                $where_or = array(
                    'workorder_name'=>$this->mongo_db->create_like($like),
                    'workorder_id'=>$this->mongo_db->create_like($like),
                    'remark_list.remark_content'=>$this->mongo_db->create_like($like)
                );
            }else{
                $where_or = array(
                    'workorder_name'=>$this->mongo_db->create_like($like),
                    'workorder_id'=>$this->mongo_db->create_like($like),
                    'remark_list.remark_content'=>$this->mongo_db->create_like($like)
                );
            }
            return  $this->mongo_db->where_or($where_or)->where($where)->order_by($order)->limit($limit)->offset($offset)->get($this->collection_name);
        }else if($like && !$where){
            $where_or = array('workorder_name'=>$this->mongo_db->create_like($like),'workorder_id'=>$this->mongo_db->create_like($like),'remark_list.remark_content'=>$this->mongo_db->create_like($like));
            return  $this->mongo_db->where_or($where_or)->order_by($order)->limit($limit)->offset($offset)->get($this->collection_name);
        }else if(!$like && $where){
            if(is_array($where) && array_key_exists("type",$where)){
                $where['type'] = $this->mongo_db->create_like($where['type']);
                return  $this->mongo_db->where($where)->order_by($order)->limit($limit)->offset($offset)->get($this->collection_name);
            }else{
                return  $this->mongo_db->where($where)->order_by($order)->limit($limit)->offset($offset)->get($this->collection_name);
            }
        }else{
            return  $this->mongo_db->order_by($order)->limit($limit)->offset($offset)->get($this->collection_name);
        }
    }



    /**
     * 获取数据条数
     */
    public function count_workorder($like = NULL,$where = NULL){
        if($like && $where){
            if(is_array($where) && array_key_exists("type",$where)) {
                $type = $where['type'];
                unset($where['type']);
                $where_or = array('workorder_name'=>$this->mongo_db->create_like($like),'workorder_id'=>$this->mongo_db->create_like($like),'remark_content'=>$this->mongo_db->create_like($like),'remark_list.remark_content'=>$this->mongo_db->create_like($like),'type'=>$this->mongo_db->create_like($type));
            }else{
                $where_or = array('workorder_name'=>$this->mongo_db->create_like($like),'workorder_id'=>$this->mongo_db->create_like($like),'remark_content'=>$this->mongo_db->create_like($like),'remark_list.remark_content'=>$this->mongo_db->create_like($like));
            }
            return $this->mongo_db->where_or($where_or)->where($where)->count($this->collection_name);
        }else if($like && !$where){
            $where_or = array('workorder_name'=>$this->mongo_db->create_like($like),'workorder_id'=>$this->mongo_db->create_like($like),'remark_content'=>$this->mongo_db->create_like($like),'remark_list.remark_content'=>$this->mongo_db->create_like($like));
            return $this->mongo_db->where_or($where_or)->count($this->collection_name);
        }else if(!$like && $where){
            if(is_array($where) && array_key_exists("type",$where)) {
                $where['type'] = $this->mongo_db->create_like($where['type']);
                return $this->mongo_db->where($where)->count($this->collection_name);
            }else{
                return $this->mongo_db->where($where)->count($this->collection_name);
            }
        }else{
            return $this->mongo_db->count($this->collection_name);
        }
    }

    /**
     *新增工单
     */
    public function insert_workorder($workorder_data){
		return $this->mongo_db->insert($this->collection_name, $workorder_data);
    }

    /**
     *更新工单 
     */
    public function update_workorder($workorder_data){
        $data['filter'] = array("workorder_id"=>$workorder_data['workorder_id']);
        $data['update'] = $workorder_data;
        return $this->mongo_db->update($this->collection_name,$data);
    }

    /**
     * 删除工单
     */
    public function delete_workorder($id){
        return $this->mongo_db->where(array("workorder_id"=>$id))->delete($this->collection_name);
    }

    /**
     * 删除多个工单
     */
    public function delete_all_workorder($ids){
        return $this->mongo_db->where_in("workorder_id",$ids)->delete_all($this->collection_name);
    }
    

}