<?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;
use app\common\facade\Inform;
use app\allwin\model\AllwinStore;

class StoreWorker extends Model{
    
    protected $pk    = 'id';
    protected $table = 'ai_allwin_store_worker';

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

    //所属好店信息
    public function store(){
        return $this->hasOne('AllwinStore','id','store_id');
    }
 
     /**
     * 设置好店收银员
     * @param integer $id
     */
    public static function setWorker(int $id,$member_miniapp_id){
        $result = self::where(['id' => $id])->field('is_cashier,uid')->find();
        $data['is_cashier'] = $result['is_cashier'] ? 0 : 1;
        $str = '';
        if($data['is_cashier'] == 0){
            $str = '员工';
        }else{
            $str = '管理员';
        }
        //通知申请者到微信
        Inform::sms($result->uid,$member_miniapp_id,['title' =>'业务进展通知','type' => '身份变更','state' => '成功','content' =>'您的身份已变更为'.$str]);
        return self::where('id',$id)->update($data);
    } 

    /**
     * 判断是否好店收银员
     * @param integer $miniapp_id 当前引用ID
     * @param integer $uid 用户ID
     * @return object
     */
    public static function isManage(int $miniapp_id,int $uid){
        $result = StoreWorker::where(['member_miniapp_id' => $miniapp_id,'uid' => $uid,'is_cashier' => 1])->find();
        if(empty($result)){
            $store = AllwinStore::where(['manage_uid' => $uid,'is_lock' => 0])->find();
            if(empty($store)){
                return false;
            }
            $rel = (object)[];
            $rel->id                = $store->id;
            $rel->member_miniapp_id = $store->member_miniapp_id;
            $rel->uid               = $store->manage_uid;
            $rel->store_id          = $store->id;
            $rel->cate_id           = $store->cate_id;
            $rel->cate_sid          = $store->cate_sid;
            $rel->state             = $store->state;
            $rel->state_text        = $store->state_text;
            $rel->score             = $store->score;        
            $rel->name              = $store['name'];
            $rel->money_coupon      = $store->money_coupon;
            $rel->img               = $store->img;
            $rel->imgs              = $store->imgs;
            $rel->address           = $store->address;
            $rel->tags              = $store->tags;
            $rel->tips              = $store->tips;
            $rel->work_time         = $store->work_time;
            $rel->telphone          = $store->telphone;
            return $rel;
        }else{
            if($result->store->is_lock == 1){
                return false;
            }
            $rel = (object)[];
            $rel->id                = $result->store_id;
            $rel->member_miniapp_id = $result->member_miniapp_id;
            $rel->uid               = $result->uid;
            $rel->store_id          = $result->store_id;
            $rel->cate_id           = $result->store->cate_id;
            $rel->cate_sid          = $result->store->cate_sid;
            $rel->state             = $result->store->state;
            $rel->state_text        = $result->store->state_text;
            $rel->score             = $result->store->score;
            $rel->name              = $result->store['name'];
            $rel->money_coupon      = $result->store->money_coupon;
            $rel->img               = $result->store->img;
            $rel->imgs              = $result->store->imgs;
            $rel->address           = $result->store->address;
            $rel->tags              = $result->store->tags;
            $rel->tips              = $result->store->tips;
            $rel->work_time         = $result->store->work_time;
            $rel->telphone          = $result->store->telphone;
            return $rel;
        }
    } 
}
