<?php
declare (strict_types = 1);

namespace app\admin\model\v1;

use app\common\model\ArticleLeaveMessageBaseModel;
use think\model\concern\SoftDelete;

/**
 * @mixin \think\Model
 */
class ArticleLeaveMessageModel extends ArticleLeaveMessageBaseModel
{
    // 启用软删除
    use SoftDelete;
    // 软删除标记数据字段
    protected $deleteTime = 'deleted_at';

    // 关联文章
    public function article()
    {
        return $this->belongsTo(ArticleModel::class, 'article_id', 'id');
    }

    // 审核状态查询
    public function scopeIsAudited($query, $is_audited)
    {
        if (is_null($is_audited)) {
            return;
        }
        $query->where('is_audited', '=', $is_audited);
    }

    // 搜索留言内容
    public function searchCreatedAtAttr($query, $value, $data)
    {
        if (empty($value)) {
            return;
        }

        $val = explode(',', $value);
        if (count($val) > 1) {
            $query->whereBetweenTime('created_at', $val[0], $val[1]);
        } else {
            $query->whereTime('created_at', '>=', $val[0]);
        }
    }
}
