<?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\ais\model;
use app\common\facade\Inform;
use think\Model;
use util\Util;

class AisStore extends Model{
    
    protected $autoWriteTimestamp = true;
    protected $updateTime         = false;
    protected $json               = ['citypath','imgs']; //数组存到数据库变JSON

    //加入的群组
    public function group(){
        return $this->belongsToMany('AisStoreGroup','AisStoreUnion','group_id','store_id');
    }

    //所属管理员
    public function user(){
        return $this->hasOne('app\common\model\SystemUser','id','manage_uid');
    }

    public function cate(){
        return $this->hasOne('AisStoreCate','id','cate_id');
    }

    //连锁门店
    public function chain(){
        return $this->hasMany('AisStoreChain','store_id','id');
    }

    //Tags字符串转数组
    public function getTagsAttr($value,$data){
        if(empty($data['tags'])){
            return [];
        }
        return explode(',',$data['tags']);
    }

    //搜索好店名称
    public function searchNameAttr($query,$value){
        if(!empty($value)){
            $query->where('name','like', '%'.$value .'%');
        }
    }

    //添加或编辑
    public static function edit($param){
        $data['manage_uid'] = $param['manage_uid'];
        $data['cate_id']    = $param['cate_id'];
        $data['cate_sid']   = $param['cate_sid'];
        $data['name']       = $param['name'];
        $data['address']    = $param['address'];
        $data['longitude']  = $param['longitude'];
        $data['latitude']   = $param['latitude'];
        $data['telphone']   = $param['telphone'];
        $data['citypath']   = $param['citypath'];
        $data['citycode']   = $param['citycode'];
        $data['img']        = $param['img'];
        $data['mch_id']     = $param['mch_id'];
        $data['charge']     = $param['charge'];
        $data['tags']       = empty($param['tags']) ? '': sbc2Dbc($param['tags']);
        $data['imgs']       = json_encode($param['imgs']);
        if(empty($param['id'])){
            $data['member_miniapp_id'] = $param['member_miniapp_id'];
            $data['is_top']            = 0;
            $data['create_time']       = time();
            $data['sort']              = 0;
            return self::insertGetId($data);
        }else{
            self::update($data,['id'=>(int)$param['id']]);
            return $param['id']; 
        }
    }

     /**
     * 置顶或取消
     * @param integer $id
     */
    public static function isTop(int $id){
        $result = self::where(['id' => $id])->field('is_top')->find();
        $result->is_top = $result->is_top ? 0 : 1;
        return $result->save();
    } 

     /**
     * 锁定或取消
     * @param integer $id
     */
    public static function isLock(int $id,$member_miniapp_id){
        $result = self::where(['id' => $id])->field('is_lock')->find();
        $result->is_lock = $result->is_lock ? 0 : 1;
        return $result->save();
    } 
    

    //根据管理ID获取所属商户
    public static function manageStore(int $uid){
        return self::with(['chain'	=> function($query) {
            $query->field('id,store_id,title,address,telphone,longitude,latitude');
        }])->where(['manage_uid' => $uid])->field('citycode,id,name,address,img,telphone,longitude,latitude,mch_id')->find();  
    }
}