<?php

namespace app\model;

use think\model\relation\BelongsToMany;

class Account extends Base
{
    public const STATUS_NORMAL    = 'normal'; //正常
    public const STATUS_DISABLE   = 'disable';//禁用
    public const GENDER_UNDEFINED = 0; // 未知
    public const GENDER_MALE      = 1; // 男性
    public const GENDER_FEMALE    = 2; // 女性

    // 生成个人补充信息:邀请码、用户编号
    public static function onAfterInsert($item)
    {
        $item->invite_code = md5($item->id, false);
        $item->coding      = date('y').str_pad((string) $item->id, 10, '0', STR_PAD_LEFT);
        $item->save();
    }

    /**
     * 时间修改器:生日
     *
     * @param $value
     * @return null|mixed
     */
    public function setBirthdayAttr($value)
    {
        return empty($value) ? null : $value;
    }

    /**
     * 时间获取器:生日
     * @param $value
     * @return string
     */
    public function getBirthdayAttr($value)
    {
        return empty($value) ? '' : $value;
    }

    /**
     * 客户标签
     *
     * @return BelongsToMany
     */
    public function tags(): BelongsToMany
    {
        return $this->belongsToMany(AccountTag::class, 'account_tag_pivot', 'tag_id', 'account_id');
    }
}