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

class AllwinStore extends Model{
    
    protected $pk    = 'id';
    protected $autoWriteTimestamp = true;
    protected $updateTime = false;
    
    //开通好店订单
    public function WorkerStoreOrder(){
        return $this->hasOne('WorkerStoreOrder','store_id','id');
    }

    //所有子门店
    public function chains(){
        return $this->hasMany('AllwinStoreChain','store_id');
    }

    //搜索好店名称
    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['mch_id']     = $param['mch_id'];
        $data['charges']    = $param['charges'];
        $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['tips']       = $param['tips'];
        $data['tags']       = empty($param['tags']) ? '': sbc2Dbc($param['tags']);
        $data['img']        = $param['img'];
        $data['imgs']       = json_encode($param['imgs']);;
        if(empty($param['id'])){
            $data['create_time']       = time();
            $data['member_miniapp_id'] = $param['member_miniapp_id'];
            $data['sort']              = 0;
            $data['code_id']           = $param['code_id'];
            return self::insertGetId($data);
        }else{
            self::update($data,['id'=>(int)$param['id']]);
            return $param['id'];
        }
    }


    //微信合伙人添加或编辑好店
    public static function wechatEdit($param){
        $data['manage_uid'] = trim($param['manage_uid']);
        $data['charges']    = trim($param['charges']);
        $data['name']       = trim($param['name']);
        $data['address']    = trim($param['address']);
        $data['telphone']   = trim($param['telphone']);
        $data['work_time']  = trim($param['work_time']);
        $data['tags']       = empty($param['tags']) ? '': trim(sbc2Dbc($param['tags']));
        $data['img']        = trim($param['img']);
        $data['imgs']       = json_encode(array(trim($param['img'])));
        $data['is_lock']    = 1;
        if(isset($param['id']) && $param['id'] != 0){
            self::update($data,['id'=>(int)$param['id']]);
            return $param['id'];
        }else{
            $data['create_time']       = time();
            $data['member_miniapp_id'] = $param['member_miniapp_id'];
            $data['sort']              = 0;
            $store_id = self::insertGetId($data);
            if($store_id > 0){
              $worker = Worker::where($param['member_miniapp_id'])->where(['uid' => $param['uid']])->find();
              if($worker){
                  if(strlen($worker->store_ids) > 0){
                      $worker->store_ids = $worker->store_ids.','.$store_id;
                  }else{
                      $worker->store_ids = $store_id;
                  }
                  Worker::where(['id' => $worker->id])->update(['store_ids' => $worker->store_ids]);
              }
            }
            return  $store_id;
        }
    }

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

     /**
     * 锁定或取消
     * @param integer $id
     */
    public static function isLock(int $id,$member_miniapp_id){
        $result = self::where(['id' => $id])->field('is_lock,manage_uid')->find();
        $data['is_lock'] = $result['is_lock'] ? 0 : 1;
        if($data['is_lock'] == 0) {
            //通知申请者到微信
            Inform::sms($result->manage_uid,$member_miniapp_id,['title' =>'业务进展通知','type' => '好店申请','content' =>'您的好店申请已经通过审核','state' => '成功']);
        }
        return self::where('id',$id)->update($data);
    } 


    //根据管理ID获取所属商户
    public static function manageStore(int $uid){
        return self::where(['manage_uid' => $uid,'is_lock' => 0])->field('id,name,address,telphone,cate_id,cate_sid')->find();  
    }

    /**
     * 读取商家详情(api)
     * @param integer 读取ID
     * @return json
     */
    public static function getView(array $where){
        $data = self::where($where)->find();
        if(empty($data)){
            return false;
        }else{
            if($data['latitude'] > 0 && $data['longitude'] > 0 ){
                $map = bdMap_to_txMap($data['latitude'],$data['longitude']);
                $data['longitude'] = $map['lng'];
                $data['latitude'] =  $map['lat'];;
            }
            $data['img'] = $data['img']."?x-oss-process=style/w100";
            $imgs = empty($data['imgs']) ? [] :json_decode($data['imgs']);
            $imga = [];
            foreach ($imgs as $key => $img) {
                $imga[$key] = $img."?x-oss-process=style/500";
            }
            $data['imgs'] = $imga;
            $data['create_time'] = util::ftime($data['create_time']);
            $data['tags'] = explode(',',$data['tags']); 
            $data['distance'] = '0km';   //经纬度距离判断
            $data['chains']   = $data['chains'];
            return $data;
        }
    }
}