<?php

namespace App\Models;

use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;

class AgentUser extends BaseModel
{
    use HasFactory;
    public $fillable = ['user_id','level','can_restaurant'];

    public const AgentLevelNot = 0; //非批发商
    public const AgentLevelReady = 1; //准批发商
    public const AgentLevelBeing = 1; //批发商
    public const AgentStaff = 3;    //员工
    public const AgentVIP = 4;  //vip

    const NotCanRestaurant = 0; //不可订餐
    const CanRestaurant = 1; //可订餐

    public static function agentName($level)
    {
        switch ($level) {
            case self::AgentLevelNot:
                $name = "客户";
                break;
            case self::AgentLevelBeing:
                $name = "批发商";
                break;
            case self::AgentLevelReady:
                $name = "批发商";
                break;
            case self::AgentStaff:
                $name = "员工";
                break;
            case self::AgentVIP:
                $name = "VIP";
                break;
            default:
                $name = "客户";
        }
        return $name;
    }

    public function user()
    {
        return $this->belongsTo(User::class, 'user_id', 'id');
    }

    //介绍人 用户购买活动票记录
    public function recommendUserLog()
    {
        return $this->hasOne(RecommendUser::class, 'user_id', 'user_id');
    }
    //介绍人
    public function recommendUser()
    {
        return $this->hasOneThrough(User::class, RecommendUser::class, 'user_id', 'id', 'user_id', 'recommend_user_id');
    }

    public function activityOrders()
    {
        return $this->hasMany(ActivityOrder::class, 'user_id', 'user_id')->paid();
    }

    public function introduceActivityOrders()
    {
        return $this->hasMany(ActivityOrder::class, 'introduce_user_id', 'user_id')->paid();
    }

    public function introduceAgentOrders()
    {
        return $this->hasMany(AgentOrder::class, 'from_user_id', 'user_id')->paid();
    }

    public function agentOrders()
    {
        return $this->hasMany(AgentOrder::class, 'user_id', 'user_id')->paid();
    }

    public function wechat()
    {
        return $this->hasOne(Wechat::class, 'user_id', 'user_id')->where('type', 'mp')->select("id",'user_id', 'openid', 'unionid');
    }

    public function workWechat()
    {
        return $this->hasOne(Wechat::class, 'user_id', 'user_id')->where('type', 'work')->select("id",'user_id', 'openid', 'unionid');
    }

    public function officialWechat()
    {
        return $this->hasOne(Wechat::class, 'user_id', 'user_id')->where('type', 'official')->select("id",'user_id', 'openid', 'unionid');
    }

    /**
     * VIP.员工只能通过后台去设置等级
     * 客户和批发商 通过购买商品。活动获得等级
     * @param $user_id 用户id
     * @param $target_level 目标等级
     * @param $desc 描述
     * @param $change 高等级是否，切换到低等级
     */
    public static function changeLevel($user_id, $target_level, $desc, $change=false)
    {
        //修改h5账号
        self::changeLevelV2($user_id, $target_level, $desc, $change);

        //修改小程序账号
        $user = User::find($user_id);
        if(empty($user)) return;
        //小程序用户
        $mp_user = User::where('mobile', $user->mobile)->where("id",'<>', $user->id)->whereHas("wechat")->first();
        if ($mp_user) {
            self::changeLevelV2($mp_user->id, $target_level, $desc, $change);
        }
    }

    public static function changeLevelV2($user_id, $target_level, $desc, $change=false)
    {
        //当前等级
        $agent_user = self::where("user_id", $user_id)->first();
        if (!$agent_user) {
            $current_level = 0;
            self::create(["user_id"=>$user_id, 'level'=>$target_level]);
        }else {
            $current_level = $agent_user->level;
            if ($change) {
                $agent_user->update(["level"=>$target_level]);
            }else {
                //当前为等级是员工或者VIP，等级不变
                if (in_array($current_level,[AgentUser::AgentVIP, self::AgentStaff])) {
                    $target_level = $current_level;
                }
                $agent_user->update(["level"=>$target_level]);
            }
        }
        if ($target_level != $current_level || $target_level == 0) {
            AgentUserLog::create(["user_id"=>$user_id, 'before_level'=>$current_level, 'after_level'=>$target_level, 'desc'=>$desc]);
        }
    }
}
