<?php
namespace App\Models;
use DB;
class Manager extends BaseModel {

	protected $table = "message";
	public $timestamps = false;

	function getlist($list = 10, $where = '') {
		$data = $this->orderBy('add_time', 'desc');
		if (!empty($where) && is_array($where)) {
			$data = $data->where($where);
		}
		$data = $data->paginate($list);

		if (!$data) {
			return -4010010022; //没有数据
		}
		return $data->toArray();
	}
	//获取信息
	function getMsg($value, $type) {
		$fild = $this->getfild($type);
		$data = $this->where($fild, $value)->orderBy('add_time', 'desc')->limit(10)->get();
		if (!$data) {
			return -4010010122; //没有数据
		}
		return $data->toArray();

	}
	function getDmsg($value, $type) {
		$fild = $this->getfild($type);
		$data = $this->where($fild, $value)->first();
		if (!$data) {
			return -4010010122; //没有数据
		}
		return $data->toArray();
	}
	function getfild($k) {
		$a = array(
			1 => 'id',
			2 => 'type',
		);
		return $a[$k];
	}
	function updateMsg($data, $id) {
		$res = $this->where('id', $id)->update($data);
		if (!$res) {
			return -4010011122;
		}
		return 1;
	}

    function updateMessage($sender, $reciver, $title, $content, $type)
    {

        $data['add_time']=date('Y-m-d H:i:s');

        $data['formname']=$sender;
        $data['toname']=$reciver;
        $data['title'] = $title;
        $data['type'] =$type;
        $data['content'] = $content;
        //普通消息
        if ($data['type'] == 2){
            $dataName=explode(',',$data['toname']);
            foreach ($dataName as $v){
                $data['toname']=$v;
                $this->addNotice($data);
            }
        }
        //系统消息
        if ($data['type'] == 1){
            $data['formname']='system';
            $data['toname']='all';
            $this->addNotice($data);
        }


        //系统通知
        if ($data['type'] == 3){
            $data['formname']='notice';
            $data['toname']='all';
            $this->addNotice($data);

        }

        //系统通知
        if ($data['type'] == 4){
            $data['formname']='admin';
            $data['toname']='admin_all';
            $this->addNotice($data);

        }

    }



	//添加公告数据
    function addNotice($data){
//        DB::connection()->enableQueryLog();
        $res = $this->insertGetId ($data);
//         $queries = DB::getQueryLog();
//         print_r($queries);
        if(!$res){
            return -6030001222;
        }
        return $res;
    }



    function getlistd($where) {

            $data = $this->where($where[0], $where[1])->orderBy('add_time', 'desc')->limit(5)->get();


        if (!$data) {
            return -4010011122;
        }
        return $data->toArray();
    }

}
