<?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\ais\model;
use app\common\facade\Inform;
use app\ais\model\Levels;
use app\ais\model\Vip;
use think\Model;
use util\Util;
use filter\Filter;
use think\db\Where;

class AisInfo extends Model{

    protected $pk = 'id';


    //用户
    public function user(){
        return $this->hasOne('app\common\model\SystemUser','id','uid');
    }
    
    //一对多管理回复的评论
    public function comments(){
        return $this->hasMany('AisInfoReply','info_id','id');
    }

    //所属栏目
    public function cate(){
        return $this->hasOne('AisInfoCate','id','cate_id');
    }

    //是否关注
    public function follow(){
        return $this->hasOne('AisInfoFollow','like_uid','uid');
    }
    
    //是否认证号
    public function mp(){
        return $this->hasOne('AisInfoMp','uid','uid');
    }

    //搜索器
    public function searchThemesAttr($query,$value){
        $value = Filter::filter_escape($value);
        if(!empty($value)){
            $query->where('themes','like', '%'. $value .'%');
        }
    }

    //Tags字符串转数组
//    public function getImagesAttr($value){
//        $imga = [];
//        foreach ($value as $key => $img) {
//            $imga[$key] = $img;
//        }
//        return $imga;
//    }

    /**
     * 锁定
     * @param integer $id
     */
    public static function lock(int $id,$member_miniapp_id){
        $result = self::where('id',$id)->find();
        $result->is_lock = $result->is_lock ? 0 : 1;
        return $result->save();
    }

   /**
     * 指定
     * @param integer $id
     */
    public static function top(int $id){
        $result = self::where('id',$id)->find();
        $result->is_top = $result->is_top ? 0 : 1;
        return $result->save();
    }


   /**
     * API的信息列表查询
     * @param integer $id
     */
    public static function apiLists($where,$order,$user,$keyword = ''){
        $rel = AisInfo::with([
            'user' => function($query) {
                $query->field('id,nickname,face');
            },
            'cate' => function($query) {
                $query->field('id,tpl_id,title');
            },
            'mp'   => function($query) {
                $query->field('id,uid,logo,title,is_vip');
            }
        ])->withSearch(['themes'],['themes' => $keyword])->where($where)->order($order)->paginate(10);
        $data = [];
        foreach ($rel as $key => $value) {
            $data[$key]                = $value;
            if(empty($value->cate->tpl)){
                $data[$key]['button_name'] = '立即下单';
                $data[$key]['tips']        = '任何有用户发布,下单后请电话再次确认';
                $data[$key]['is_shop']     = 0;
            }else{
                $data[$key]['button_name'] = $value->cate->tpl->button_name;
                $data[$key]['tips']        = $value->cate->tpl->tips;
                $data[$key]['is_shop']     = $value->cate->tpl->is_shop;
            }
            $data[$key]['images']      = $value->images;
            $data[$key]['images_len']  = count($value->images);
            $data[$key]['fields']      = empty($value->fields) ? [] : json_decode($value->fields,true);
            $data[$key]['create_time'] = util::ftime($value->create_time);
            $data[$key]['is_mp']       = empty($value->mp)?0:1;
            //是否点赞
            $data[$key]['is_like']     = 0; 
            $data[$key]['like_face']   = empty($value->like_face) ? [] : json_decode($value->like_face,true);
            //判断是否被关注
            $data[$key]['is_follow']   = !empty($value->follow) && ! empty($user)  && $value->follow->uid = $user->id ? 1:0;  
        }
        return $data;  
    }

    //添加或编辑
    public static function postThemes($param){
        $data['themes']            = $param['content'];
        $data['telphone']          = $param['telphone'];
        $data['top_money']         = $param['top_money'];
        $data['topday']            = $param['topday'];
        $data['fields']            = $param['fields'];
        $data['images']            = $param['imgs'];
        $data['uid']               = $param['uid'];
        $data['cate_id']           = $param['cate_id'];
        $data['store_id']          = $param['store_id'];
        $data['order_no']          = $param['order_no'];
        $data['price']             = (int)$param['price'];
        $data['is_get']            = $param['is_get'];
        $data['create_time']       = time();
        $data['views']             = rand(1,500);
        $data['is_lock']           = 0;
        $data['member_miniapp_id'] = $param['member_miniapp_id'];
        return self::create($data);
    } 
}