<?php
/**
 * Created by PhpStorm.
 * User: zilongs
 * Date: 20-9-23
 * Time: 上午11:04
 */

namespace App\Models;

use DB;

class User extends BaseModel
{
    protected $appends = ['age'];
    protected $table = 'users';

    //从生日转成年龄
    public function getAgeAttribute()
    {
        return birthday_to_age($this->birthday);
    }
    public function getCouponNumAttribute()
    {
        return UserCoupon::where('user_id', $this->id)->where('status', 1)->where('expire_time', '>', time())->count();
    }

    public function patients()
    {
        return $this->hasMany(Patient::class);
    }

    public function order()
    {
        return $this->hasMany(Order::class);
    }
    //通过token获取用户信息
    public static function getUserByToken($is_exit = true)
    {
        $auth = request()->header('token');
        if (empty($auth)) {
            if (!$is_exit) {
                return [];
            }
            exit_out(null, 401, '验证失效，请重新登录');
        }

        $arr = aes_decrypt($auth);
        if (isset($arr['doctor_id'])){
            //说明是医生登陆！
            if (empty($arr['doctor_id'])) {
                exit_out(null, 401, '医生验证失效，请重新登录');
            }
            $user = DB::table('docters')->where('id', $arr['doctor_id'])->first();
            if (empty($user)){
                exit_out(null, 601, '该账号已被删除');
            }
            $user = object_array($user);

            return $user;
        }
        else{
            if (empty($arr['id'])) {
                exit_out(null, 401, '账号验证失效，请重新登录');
            }

            $user = User::where('id', $arr['id'])->first();
            if (empty($user)){
                exit_out(null, 601, '该账号已被删除');
            }
            $user = $user->toArray();

            if ($user['status'] == 0){
                exit_out(null, 602, '该账号已被冻结');
            }

            return $user;
        }
    }

    /**
     * 用户医生关注表
     * @return \Illuminate\Database\Eloquent\Relations\BelongsTo
     * @author Liu-Yh
     * Create By 2020/11/18 11:06
     */
    public function userDorter(){
        return $this->hasOne(UserDocter::class,'user_id','id')->select(['id', 'remark']);;
    }

    //改变用户余额
    public static function changeBalance($user_id, $change_balance, $type, $relation_id, $remark, $admin_user_id = 0)
    {
        $user = User::select(['balance'])->where('id', $user_id)->first();

        if ($type == 2) {
            User::where('id', $user_id)->update([
                'balance' => DB::raw('balance + '.$change_balance),
                'topup_balance'  => DB::raw('topup_balance + '.$change_balance),
            ]);
        }
        elseif ($type == 3) {
            User::where('id', $user_id)->update([
                'balance' => DB::raw('balance + '.$change_balance),
                'giving_balance'  => DB::raw('giving_balance + '.$change_balance),
            ]);
        }
        else {
            User::where('id', $user_id)->increment('balance', $change_balance);
        }

        UserBalanceLog::create([
            'user_id' => $user_id,
            'admin_user_id' => $admin_user_id,
            'type' => $type,
            'relation_id' => $relation_id,
            'before_balance' => $user['balance'],
            'change_balance' => $change_balance,
            'after_balance' => $user['balance'] + $change_balance,
            'remark' => $remark,
        ]);

        return true;
    }
    public function UserBalanceLog(){
        return $this->hasMany(UserBalanceLog::class);
    }
    public function patient(){
        return $this->hasMany(Patient::class,'user_id','id');
    }
}
