<?php
namespace app\lib\model;
use think\facade\Log;

use think\Model;

class User extends Model
{
    // 设置当前模型对应的主键名
    protected $pk = 'id';

    // 开启自动写入时间戳字段
    protected $autoWriteTimestamp = true;

    // 设置json类型字段
    protected $json = ['mszh'] ;

    // 只读字段
    protected $readonly = ['username'];

    // 一对一关联 Dszh
    public function DszhHasOne() {
        return $this->hasOne(Dszh::class);
    }

    // 一对多关联 Dszh
    public function DszhHasMany()
    {
        return $this->hasMany(Dszh::class);
    }

    // 一对一关联 Mszh
    public function MszhHasOne() {
        return $this->hasOne(Mszh::class);
    }

    // 一对多关联 Mszh
    public function MszhHasMany()
    {
        return $this->hasMany(Mszh::class);
    }

    // 一对一关联 Pay
    public function PayHasOne() {
        return $this->hasOne(Pay::class);
    }

    // 一对多关联 Pay
    public function PayHasMany()
    {
        return $this->hasMany(Pay::class);
    }

    // 一对一关联 Order
    public function OrderHasOne() {
        return $this->hasOne(Order::class);
    }

    // 一对多关联 Order
    public function OrderHasMany()
    {
        return $this->hasMany(Order::class);
    }

    // 一对一关联 Money
    public function MoneyHasOne() {
        return $this->hasOne(Money::class);
    }

    // 一对多关联 Money
    public function MoneyHasMany()
    {
        return $this->hasMany(Money::class);
    }

    // 一对一关联 Chozhi
    public function ChozhiHasOne() {
        return $this->hasOne(Chozhi::class);
    }

    // 一对多关联 Chozhi
    public function ChozhiHasMany()
    {
        return $this->hasMany(Chozhi::class);
    }

    // 一对一关联 Level
    public function LevelHasOne() {
        return $this->hasOne(Level::class);
    }

    // 一对多关联 Level
    public function LevelHasMany()
    {
        return $this->hasMany(Level::class);
    }

    //删除后
    protected static function onAfterDelete($data)
    {
        try {
            Mszh::where('user_id',$data['id'])->delete();
            Dszh::where('user_id',$data['id'])->delete();
            Chozhi::where('user_id',$data['id'])->delete();
        } catch (\Exception $e) {
            trace($e->getMessage(), 'error');
        }
    }

}