<?php

namespace app\model;

use think\Model;

class UuidRelation extends Model
{

	protected $connection = 'mysql';

	protected $pk = 'id';

	protected $name = 'uuid_relation';

	public static function getuuid($uid, $ptype)
	{
		$query = self::where(['weid' => weid(), 'ptype' => $ptype, 'uid' => $uid])->whereNotNull('uuid')->where('uuid','<>', '');
		$mob = $query->order('id desc')->find();

		if (!empty($mob)) {
			return $mob->uuid;
		} else {

			if (empty($uuid)) {
				$Membermob = new Member;
				$memberinfo = $Membermob->getUserByWechat();
				$uuid = $memberinfo['uuid'];

				if (!empty($memberinfo) && empty($uuid)) {

					if ($ptype == 'technical') {
						$Technical = \app\model\Technical::where('uid', $memberinfo['id'])->find();
						$uuid = $Technical->uuid;
					}

					if ($ptype == 'store') {
						$Store =  \app\model\Store::where('uid', $memberinfo['id'])->find();
						$uuid = $Store->uuid;
					}
					if ($uuid) {
						UuidRelation::create(['weid' => weid(), 'ptype' => $ptype, 'uuid' => $uuid, 'uid' => $memberinfo['id']]);
					}
					$data['api'] = Author()::getlan();
				}
			}
			return $uuid;
		}
	}

	public static function getuid($uuid)
	{
		$query = self::where(['weid' => weid(), 'uuid' => $uuid]);
		$mob = $query->order('id desc')->find();

		if (!empty($mob)) {
			return $mob->uid;
		}
	}
}
