<?php

namespace app\admin\model;

use think\Model;


class UserRelation extends Model
{

    

    

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

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

    // 追加属性
    protected $append = [
        'sex_text',
        'subscribe_text',
        'subscribe_time_text',
        'user_type_text'
    ];
    

    
    public function getSexList()
    {
        return ['1' => __('Sex 1'), '2' => __('Sex 2'), '0' => __('Sex 0')];
    }

    public function getSubscribeList()
    {
        return ['0' => __('Subscribe 0'), '1' => __('Subscribe 1')];
    }

    public function getUserTypeList()
    {
        return ['0' => __('User_type 0'), '1' => __('User_type 1'), '2' => __('User_type 2'), '3' => __('User_type 3')];
    }


    public function getSexTextAttr($value, $data)
    {
        $value = $value ? $value : (isset($data['sex']) ? $data['sex'] : '');
        $list = $this->getSexList();
        return isset($list[$value]) ? $list[$value] : '';
    }


    public function getSubscribeTextAttr($value, $data)
    {
        $value = $value ? $value : (isset($data['subscribe']) ? $data['subscribe'] : '');
        $list = $this->getSubscribeList();
        return isset($list[$value]) ? $list[$value] : '';
    }


    public function getSubscribeTimeTextAttr($value, $data)
    {
        $value = $value ? $value : (isset($data['subscribe_time']) ? $data['subscribe_time'] : '');
        return is_numeric($value) ? date("Y-m-d H:i:s", $value) : $value;
    }


    public function getUserTypeTextAttr($value, $data)
    {
        $value = $value ? $value : (isset($data['user_type']) ? $data['user_type'] : '');
        $list = $this->getUserTypeList();
        return isset($list[$value]) ? $list[$value] : '';
    }

    protected function setSubscribeTimeAttr($value)
    {
        return $value === '' ? null : ($value && !is_numeric($value) ? strtotime($value) : $value);
    }
    public static function openidToUid($openid,$field="openid")
    {
        $model = new self;
        return $model->where($field,$openid)->value('user_id');
    }

}
