<?php

namespace app\admin\model\fastim;

use think\Model;
use addons\fastim\library\Common;


class Session extends Model
{


    // 表名
    protected $name = 'fastim_session';

    // 自动写入时间戳字段
    protected $autoWriteTimestamp = 'int';

    // 定义时间戳字段名
    protected $createTime = 'createtime';
    protected $updateTime = false;
    protected $deleteTime = false;

    // 追加属性
    protected $append = [
        'type_text',
        'chat_info'
    ];


    public function getTypeList()
    {
        return ['single' => __('Type single'), 'group' => __('Type group'), 'service' => __('Type service')];
    }


    public function getTypeTextAttr($value, $data)
    {
        $value = $value ? $value : (isset($data['type']) ? $data['type'] : '');
        $list  = $this->getTypeList();
        return isset($list[$value]) ? $list[$value] : '';
    }

    public function getChatInfoAttr($value, $data)
    {
        $info = false;
        if ($data['type'] == 'group') {
            $info = Common::getImGroupChat($data['chat_id']);
        } elseif ($data['type'] == 'service') {
            $info = \think\Db::name('fastim_service')->where('id', $data['chat_id'])->find();
        }

        return $info;
    }

    public function user()
    {
        return $this->belongsTo('app\admin\model\fastim\User', 'user_one', 'id', [], 'LEFT')->setEagerlyType(0);
    }

    public function usertwo()
    {
        return $this->belongsTo('app\admin\model\fastim\User', 'user_two', 'id', [], 'LEFT')->setEagerlyType(0);
    }
}
