<?php
/**
 * @copyright   Copyright (c) 2017 https://www.sapixx.com All rights reserved.
 * @license     Licensed (http://www.apache.org/licenses/LICENSE-2.0).
 * @author      pillar<ltmn@qq.com>
 * 知识库
 */
namespace app\bestbao\model;
use think\Model;

class BestbaoAsk extends Model{

 
    //栏目名称
    public function category(){
        return $this->hasOne('BestbaoAskCategory','id','category_id');
    }

    //搜索
    public function searchTitleAttr($query, $value, $data){
        if(!empty($value)){
            $query->where('title','like', $value .'%');
        }
    }
    

    //添加或编辑
    public static function edit($param){
        $data['title']       = $param['title'];
        $data['content']     = $param['content'];
        $data['img']         = $param['img'];
        $data['imgs']        = json_encode($param['imgs']);
        $data['answer']      = $param['answer'];
        $data['reimg']       = json_encode($param['reimg']);
        $data['price']       = $param['price'];
        $data['update_time'] = time();
        if(isset($param['id'])){
            return self::update($data,['id'=>(int)$param['id']]);
        }else{
            $data['create_time']       = time();
            $data['member_miniapp_id'] = $param['member_miniapp_id'];
            return self::insert($data);
        }
    }


    //推荐
    public  static function ad(int $id){
        $result = self::where(['id' => $id])->find();
        $data['is_ad'] = $result['is_ad'] ? 0 : 1;
        return self::where('id',$id)->update($data);
    }

    //置顶
    public  static function top(int $id){
        $result = self::where(['id' => $id])->find();
        $data['is_top'] = $result['is_top'] ? 0 : 1;
        return self::where('id',$id)->update($data);
    }
}