<?php
/**
 * @copyright   Copyright (c) 2017 https://www.sapixx.com All rights reserved.
 * @license     Licensed (http://www.apache.org/licenses/LICENSE-2.0).
 * @author      pillar<ltmn@qq.com>
 * 天下共赢用户和好店的关系
 */
namespace app\allwin\model;
use think\Model;

class StoreUser extends Model{

    protected $pk = 'id';
    protected $table = 'ai_allwin_user';

    //和主用户表绑定关系
    public function user(){
        return $this->hasOne('app\common\model\SystemUser','id','uid');
    }

    //查询用户所属店铺
    public function store(){
        return $this->hasOne('AllwinStore','id','store_id');
    }

    //获取用户所在店铺ID
    public function getUserStoreId(int $uid){
        return self::where(['uid' => $uid])->field('store_id')->find();
    }

    //添加或编辑
    public static function editer(int $uid,int $store_id){
        $rel = self::where(['uid' => $uid])->find();
        if(empty($rel)){
            self::insert(['uid' => $uid,'store_id' => $store_id]);
        }else{
            $rel->uid      = $uid;
            $rel->store_id = $store_id;
            $rel->save();
        }
    }   
}