<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2020/5/20
 * Time: 19:57
 */

namespace app\index\controller;


use app\common\controller\Frontend;
use app\index\model\Store;
use think\Db;

class Shop extends Frontend
{
    protected $noNeedLogin = ['detail'];
    protected $noNeedRight = ['*'];

    public function _initialize()
    {
        parent::_initialize(); // TODO: Change the autogenerated stub
        $this->view->assign('is_search', 0);
        $this->view->assign('is_active', 0);
        $this->view->assign('title', '店铺详情');
    }

    public function detail(){
        $user_id = $this->auth->id;
        $store_id = $this->request->param('store_id',0,'intval');
        if(empty($store_id)){
            $this->error('缺少必要参数');
        }
        $storeModel = new Store();
        $data = $storeModel->findData(['id'=>$store_id]);
        //收藏
        $is_collect = "2";
        $encrypt = '2';
        if(!empty($user_id)){
            $storecollect = Db::name('storecollect')->where(['user_id'=>$user_id,'stores_id'=>$data['id']])->find();
            if(!empty($storecollect)){
                $is_collect = "1";
            }
            $userModel = new \app\index\model\User();
            $user = $userModel->findData(['id'=>$user_id]);
            if(!empty($user)){
                if($user['is_svip'] == '1' || ($user['is_vip'] == '1' && in_array($user['province_id'],$user['province_ids']))){
                    $encrypt = '1';
                }
            }
        }
        $data['is_collect'] = $is_collect;
        //当前用户若不是会员加密部分信息
        if($encrypt == '2'){
            $data['phone'] = "***********";
            $data['address'] = "***********";
        }
        $this->assign('data',$data);
        dump(collection($data)->toArray());
        return $this->fetch();
    }

    public function collect(){
        $user_id = $this->auth->id;
        $store_id = $this->request->param('store_id',0,'intval');
        if(empty($store_id)){
            $this->error('缺少必要参数');
        }
        $data = Db::name('storecollect')->where(['user_id'=>$user_id,'stores_id'=>$store_id])->find();
        $arr['user_id'] = $user_id;
        $arr['stores_id'] = $store_id;
        $arr['updatetime'] = time();
        if(empty($data)){
            $arr['createtime'] = time();
            $result = Db::name('storecollect')->insert($arr);
        }else{
            $result = Db::name('storecollect')->where(['id'=>$data['id']])->update($arr);
        }
        if(empty($result)){
            $this->error('sql执行失败');
        }
        $this->success('SUCCESS');
    }
}