<?php

namespace Message\Lib;

use Dever;
use Passport\Src\User;
use Passport\Src\Login;

class Data
{
	public function __construct()
	{
        # 获取用户信息
        //$user = new User();
        //$this->user = $user->data();
	}

	/**
     * 获取我的消息
     * 
     * @return mixed
     */
	public function read($uid, $type = false, $update = false, $status = false)
	{
        if (!$uid) {
            Dever::alert('错误的用户信息');
        }
		$prefix = defined('DEVER_PROJECT') && DEVER_PROJECT != 'default' ? DEVER_PROJECT . '_' : '';
		$outbox = $prefix . 'message_outbox';
		$inbox = $prefix . 'message_inbox';

		$where = ' and a.state = 1';
        $where .= ' and a.type <= 10';
		# 读取outbox里的数据
		$sql = 'select a.name,a.content,a.id,a.type,a.uid from '.$outbox.' as a where not exists(select oid from '.$inbox.' where a.id = oid and uid = '.$uid.')' . $where . ' ';

        $state = Dever::db('message/inbox')->query($sql);
		$outbox = $state->fetchAll();

		if ($outbox) {
			foreach ($outbox as $k => $v) {
                $insert['add_uid'] = $uid;
                $insert['add_oid'] = $v['id'];
                $insert['add_status'] = 1;
                $insert['add_type'] = $v['type'];
				//$insert['add_site'] = $v['site'];
				$insert['add_from_uid'] = $v['uid'];
				$insert['add_name'] = $v['name'];
				$insert['add_content'] = $v['content'];
                Dever::load('message/inbox-insert', $insert);
            }
		}
        if ($type) {
            $param['option_type'] = $type;
        }
        $param['option_uid'] = $uid;
        if ($status > 0) {
            $param['option_status'] = $status;
            return Dever::load('message/inbox-total', $param);
        }
		$data = Dever::load('message/inbox-getAll', $param);

        if ($update) {
            foreach ($data as $k => $v) {
                Dever::load('message/inbox-update', array('where_id' => $v['id'], 'set_status' => 2));
            }
        }

		return $data;
	}

    /**
     * 查看我的新消息
     * 
     * @return mixed
     */
    public function num($uid)
    {
        return $this->read($uid, false, false, 1);
    }

	/**
     * 查看我的消息
     * 
     * @return mixed
     */
	public function view($uid, $id)
	{
		if ($id > 0) {
			$info = Dever::load('message/inbox-one', array('option_uid' => $uid, 'option_id' => $id));
			if ($info) {
				Dever::load('message/inbox-update', array('where_id' => $id, 'set_status' => 2));

				$data = Dever::load('message/inbox-one', $id);

				return $data;
			}
		}

		Dever::alert('错误的消息信息');
	}

	/**
     * 推送消息
     * 
     * @return mixed
     */
	public function push($uid, $to_uid, $name, $content, $type = 1)
	{
        //$site 	    = intval(Dever::input('site', -1));

        if (!is_numeric($type)) {
            Dever::alert('错误的消息类型');
        }
        /*
        if(!is_numeric($site))
        {
            Dever::alert('错误的站点ID');
        }
        */
        if ($type > 10 && !is_numeric($uid)) {
            Dever::alert('错误的发件人id');
        }
        if ($type > 10 && !$to_uid) {
           	Dever::alert('错误的收件人id');
        }
        if (!$content) {
            Dever::alert('错误的消息内容');
        }

        if (is_numeric($uid) && $content && is_numeric($type)) {
        	if ($type < 10) {
                $uid = -1;
            }

            $data['add_uid'] = $uid;
            $data['add_name'] = $name;
            $data['add_content'] = $content;
            $data['add_type'] = $type;
            //$data['add_site'] = $site;
            $id = Dever::load('message/outbox-insert', $data);

            if ($id > 0) {
            	if ($to_uid) {
                    $to_uid = explode(',', $to_uid);
                    foreach ($to_uid as $k => $v) {
                    	$insert['add_uid'] = $v;
                        $insert['add_oid'] = $id;
                        $insert['add_status'] = 1;
                        $insert['add_type'] = $data['add_type'];
                        //$insert['add_site'] = $site;
                        $insert['add_from_uid'] = $data['add_uid'];
                        $insert['add_name'] = $data['add_name'];
                        $insert['add_content'] = $data['add_content'];
                        //$insert['add_origin'] = $origin;
                        Dever::load('message/inbox-insert', $insert);

                    	//这里可以设置发送push
                    }
                }
            }
        }
	}
}
