<?php
/**
 * Created by PhpStorm.
 * User: tandunzhao
 * Date: 2017/11/17
 * Time: 下午4:44
 */

namespace App\Modules\Notice\Models;


use App\Modules\Book\Services\BookConfigService;
use Illuminate\Database\Eloquent\Model;

/**
 * Class Notice 公告
 * @package App\Modules\Notice\Models
 */
class Notice extends Model
{
    protected $tables = 'notices';

    protected $fillable = ['title', 'content', 'notice_type_id', 'is_popup', 'status'];

    /**
     * 根据 notice_type_id 获取公告列表
     * @param $notice_type_id
     * @param $status
     * @return mixed
     */
    static function getList($notice_type_id = '', $status = '')
    {
        $search_object = self::select(['notices.id','title','content','is_popup', 'status', 'notices.updated_at','notice_types.name as type_name', 'notice_types.id as type_id'])
            ->leftjoin('notice_types','notice_types.id','=','notices.notice_type_id')
            ->whereExists(function ($query) {
                $query->select('id')->from('notice_types')->whereRaw('notices.notice_type_id=notice_types.id');
            })
            ->orderBy('status','asc')
            ->orderBy('notices.id','desc');

        if($notice_type_id) $search_object->where('notice_type_id',$notice_type_id);
        if(is_numeric($status)) $search_object->where('status', $status);

        $data = $search_object->paginate();
        foreach ($data as $item) {
            if ($item && isset($item->content)) {
                $item->content = self::getReplyConent($item->content);
            }
        }
        return $data;
    }

    /**
     * 根据 notice_type_id 获取简易公告列表
     * @param $notice_type_id
     * @param $status
     * @return mixed
     */
    static function getSimpleNoticeList($notice_type_id = '', $status = '')
    {
        $search_object = self::select(['notices.id','title','is_popup', 'status', 'notices.updated_at','notice_types.name as type_name', 'notice_types.id as type_id'])
            ->leftjoin('notice_types','notice_types.id','=','notices.notice_type_id')
            ->whereExists(function ($query) {
                $query->select('id')->from('notice_types')->whereRaw('notices.notice_type_id=notice_types.id');
            })
            ->orderBy('status','asc')
            ->orderBy('notices.id','desc');

        if($notice_type_id) $search_object->where('notice_type_id',$notice_type_id);
        if(is_numeric($status)) $search_object->where('status', $status);

        $data = $search_object->paginate();
        return $data;
    }

    /**
     * 获取单个通知
     * @param $id
     * @return mixed
     */
    static function getItem($id) {
        $search_object = self::select(['notices.id','title','content','is_popup', 'status', 'notices.updated_at','notice_types.name as type_name', 'notice_types.id as type_id'])
            ->leftjoin('notice_types','notice_types.id','=','notices.notice_type_id')
            ->orderBy('status','desc')
            ->orderBy('notices.id','desc')
            ->where('notices.id', $id);
        $data = $search_object->first();
        if ($data && isset($data->content)) {
            $data->content = self::getReplyConent($data->content);
        }
        return $data;
    }

    static function getReplyConent($content)
    {
        //《《11444》》
        if ($content) {
            $findEnd = "》》";
            $findBegin = "《《";
            $findBeginIndex = strpos($content, $findBegin);
            if ($findBeginIndex !== false) {
                $findBeginIndex = $findBeginIndex + strlen($findBegin);
                $findEndIndex = strpos($content, $findEnd);
                if ($findEndIndex !== false) {
                    //获取图书id
                    $bid = substr($content, $findBeginIndex, $findEndIndex - $findBeginIndex);
                    if ($bid) {
                        //通过图书id获取图书的信息
                        $bookConfig = BookConfigService::getBookById($bid);
                        if ($bookConfig) {
                            //将特殊字符替换成 书本名称
                            $content = str_replace($findBegin . $bid . $findEnd, $bookConfig->book_name, $content);
                        }
                    }
                }
            }
        }
        return $content;
    }
}