<?php
/**
 *------Create thems Model------
 *------SCWPHP  Version 1.0.0------
 *------Dev Model Jions------
 *------Create Time 2017-06-21 06:03:42------
 */

namespace App\Commons\Model;

use \System\Model;

class Message extends Model
{


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

    //lm("message",'commons')->updateMessage();
    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)
    {

        $res = lm ('message', 'Commons')->insert ($data);

        if (!$res) {
            return -6030001222;
        }
    }

    public function add($title, $content, $from, $to, $type = 1, $addStatus = 2)
    {
        $data = ['title' => $title,
            'content' => $content,
            'formname' => $from,
            'toname' => $to,
            'add_time' => date ('Y-m-d H:i:s'),
            'type' => $type,
            'add_status' => $addStatus
        ];
        return self::insert ($data);
    }
}
