<?php
/**
 * @link      http://www.u-bo.com
 * @copyright 南京友博网络科技有限公司 
 * @license   http://www.u-bo.com/license/
 */

namespace wap\models;

use Yii;
use yii\db\ActiveRecord;
use yii\db\Query;
use yii\helpers\Url;

/**
 * 系统通知模型
 *
 * @author legendjw http://blog.u-bo.com
 * @since 0.1
 */
class Announcement extends \yii\db\ActiveRecord
{
    const HAVE_READ = 1; //已读
    const NOT_READ = 0; //未读

    const TASK_TYPE = 1; //任务类别
    const RECEIVABLE_TYPE = 2; //回款类别
    const REIMBURSE_TYPE = 3; //报销类别
    

    /**
     * @inheritdoc
     */
    public static function tableName()
    {
        return '{{%announcement}}';
    }

    /**
     * 获取通知
     *
     * @param int $userId 用户id
     * @param int $limit 获取数量
     * @param array $order 排序
     */
    public static function getAnnounce($userId = null, $limit = 10, $order = ['is_read' => SORT_ASC, 'announcement_id' => SORT_DESC])
    {
        $userId = ($userId === null) ? Yii::$app->user->id : $userId ;
        $announces = static::find()
            ->where(['user_id' => $userId, 'status' => 1])
            ->orderBy($order)
            ->limit($limit)
            ->asArray();

        //取总数
        $countAnnounces = clone $announces;
        $countAnnounces->limit(null);
        $count = $countAnnounces->count();

        $announces = $announces->all();

        return ['announces' => $announces, 'count' => $count];
    }

    /**
     * 获取未读通知数量
     */
    public static function getNotReadNum()
    {
        $userId = Yii::$app->user->id;
        $num = static::find()
            ->where(['user_id' => $userId, 'status' => 1, 'is_read' => self::NOT_READ])
            ->count();
        return $num;
    }

}
