<?php
namespace Business\Store;
/**
 * User: liuyuzhen
 * Date: 2019/1/9
 * Time: 17:09
 * Description:
 */

class ClassStoreServiceModel extends \Business\AbstractModel{
    private $classStoreField = 'store_id as storeId,store_name as storeName,store_label as storeLabel,store_intro as storeIntro,store_longitude,store_latitude,gc_id as gcId,gc_name as gcName,sign_scope';

    public function init() {
    }


    public function getRecommendStores($where,$memberId){

        $addressDao = \DAO\AddressModel::getInstance();
        $validFlag = $addressDao->getValidAddress($where);
        if(!$validFlag){
            \Error\ErrorModel::throwException($addressDao->errorCode);
        }
        $condition['lat'] = $where['lat'];
        $condition['lng'] = $where['lng'];
        $condition['cityCode'] = $where['cityCode'];
        $gcId = $where['gcId'];

        \DAO\AddressModel::getInstance()->getValidAddress($condition);
        if(!$gcId){
            \Error\ErrorModel::throwException(\Error\CodeConfigModel::emptyGcId);
        }
        /*\Our\RedisHelper::cachedFunction(\Redis\Db6\ClassStoreRedisModel::getInstance(), array(&$classStoreModel, 'getList'), array(array('gc_id'=>$gcId),'store_id,fav_time'), \Our\ApiConst::oneHour,array($memberId));*/
        $storeList = $this->getStoreListByGcId($gcId,$condition,$memberId);
        return $storeList;
    }

    public function getStoreListByGcId($parentGcId,$position,$memberId){
        $lat = $position['lat'];
        $lng = $position['lng'];
        $classStoreModel = \DAO\Store\ClassStoreModel::getInstance();
        $storeList = \Our\RedisHelper::cachedFunction(\Redis\Db6\ClassStoreRedisModel::getInstance(), array(&$classStoreModel, 'getList'), array(array('gc_parent_id'=>$parentGcId),$this->classStoreField), \Our\ApiConst::oneHour,array($parentGcId));
        $newGcStoreList = array();
        if($storeList){
            $newStoreList = array();
            $newGcList = array();
            foreach($storeList as $store){
                $gcId = $store['gcId'];
                $store_lat = $store['store_latitude'];
                $store_lnt = $store['store_longitude'];
                $gcName = htmlspecialchars_decode($store['gcName']);
                $storeScope = $store['sign_scope'];
                unset($store['store_latitude']);
                unset($store['store_longitude']);
                unset($store['sign_scope']);
                unset($store['gcId']);
                unset($store['gcName']);
                $store['storeLabel'] =\Our\ImageUtil::getStoreLabel($store['storeLabel'], \Our\ImageConst::storeLabel,213);
                $store['storeDistance'] = \Our\CommonExtension::getDistance($lat,$lng,$store_lat,$store_lnt);
                if(!isset($newStoreList[$store['storeId']])){
                    $newStoreList[$store['storeId']] = $store;
                }
                if(!isset($newGcList[$gcId])){
                    $newGcList[$gcId] = $gcName;
                }
                if(isset($newGcStoreList[$gcId]['inAreaFlag'])){
                    continue;
                }

                $inAreaFlag = false;
                if($storeScope){
                    $inAreaFlag = \Our\CommonExtension::locationInArea($lat,$lng,$storeScope);
                }
                if(!isset($newGcStoreList[$gcId])){
                    $newGcStoreList[$gcId]['gcName'] = $gcName;
                    $newGcStoreList[$gcId]['gcImg'] = \Our\ImageUtil::getCategoryHeadPic($gcId);
                }
                if($inAreaFlag){
                    $newGcStoreList[$gcId]['inAreaFlag'] = $inAreaFlag;
                    $newGcStoreList[$gcId]['stores']= $store;
                }else{
                    if(isset($newGcStoreList[$gcId]['storeDistance'])){
                        if($store['storeDistance']<$newGcStoreList[$gcId]['storeDistance']){
                            $newGcStoreList[$gcId]['storeDistance'] = $store['storeDistance'];
                            $newGcStoreList[$gcId]['stores']= $store;
                        }
                    }else{
                        $newGcStoreList[$gcId]['storeDistance'] = $store['storeDistance'];
                        $newGcStoreList[$gcId]['stores']= $store;
                    }
                }
            }
            $memberStores = \Business\Store\StoreServiceModel::getInstance()->getPurchasedStoreClass($memberId,$parentGcId);
            if($memberStores){
                foreach($memberStores as $key => $storeId){
                    $returnData[$key] = array();
                    $purchasedStore = $newStoreList[$storeId];
                    $returnData[$key]['gcName'] = $newGcList[$key];
                    $returnData[$key]['gcImg'] = \Our\ImageUtil::getCategoryHeadPic($key);
                    unset($purchasedStore['gcName']);
                    unset($purchasedStore['gcId']);
                    $purchasedStore['isPurchased'] = \Our\ApiConst::one;
                    $returnData[$key]['stores'][$storeId] = $purchasedStore;
                }
            }
            foreach($newGcStoreList as $gcIdKey=>$storeInfo){
                $tempStore = $storeInfo['stores'];
                if(isset($returnData[$gcIdKey])){
                    if(isset($returnData[$gcIdKey]['stores'][$tempStore['storeId']])){
                        continue;
                    }
                    $tempStore['isPurchased'] = \Our\ApiConst::zero;
                    $returnData[$gcIdKey]['stores'][$tempStore['storeId']] = $tempStore;
                }else{
                    unset($storeInfo['storeDistance']);
                    $storeInfo['stores'] = array();
                    $tempStore['isPurchased'] = \Our\ApiConst::zero;
                    $storeInfo['stores'][$tempStore['storeId']] = $tempStore;
                    $returnData[$gcIdKey] = $storeInfo;
                }
            }
            $return = array();
            foreach($newGcList as $key=>$gcNameTemp){
                array_push($return,$returnData[$key]);
            }
            foreach($return as &$temp){
                $temp['stores'] = array_values($temp['stores']);
            }
            return $return;
        }
        return $newGcStoreList;

    }

    /**
     * git
     */
    private static $_instance = null;

    /**
     * 单例模式获取类实例
     *
     */
    public static function getInstance() {
        if (!(self::$_instance instanceof self)) {
            self::$_instance = new self();
        }

        return self::$_instance;
    }
}
