<?php

namespace App\Models;

use App\Models\BaseModel;

/**
 * @description 我的线索
 * @author  system;
 * @version    1.0
 * @date 2018-11-20 02:55:06
 *
 */
class UserThreadsModel extends BaseModel
{
    /**
     * 数据表名
     *
     * @var string
     *
     */
    protected $table = 'user_threads';
    /**
     * 主键
     */
    protected $primaryKey = 'id';

    //分页
    protected $perPage = PAGE_NUMS;

    /**
     * 可以被集体附值的表的字段
     *
     * @var string
     */
    protected $fillable = [
        'ower_id',
        'contact_id',
        'company_id',
        'status',
        'remark'
    ];

    /**
     * 获得关联的企业信息
     */
    public function company(){
        return $this->belongsTo('App\Models\CompanyInfoModel');
    }

    /***
     * 获得线索所有者信息
     * @return \Illuminate\Database\Eloquent\Relations\BelongsTo
     */
    public function ower(){
        return $this->belongsTo('App\Models\AdminUserModel','ower_id');
    }

    public function contact(){
        return $this->belongsTo('App\Models\CompanyContactsModel','contact_id');
    }

    /*
     * 获得线索的进度
     * */
    public function progress(){
        return $this->hasMany('App\Models\ThreadsProgressModel','threads_id');
    }

    /***
     * 获取线索最新跟进
     * @return string
     */
    public function latestProgress(){
        return ThreadsProgressModel::where('threads_id',$this->id)->count()?ThreadsProgressModel::where('threads_id',$this->id)->latest()->first()->remark:'暂无跟进';
    }
}