<?php
namespace DAO\GoodsClass;
/**
 * User: liuyuzhen
 * Date: 2018/7/18
 * Time: 10:58
 * Description:
 */
class StoreOnlineGoodsClassModel extends \DAO\AbstractModel{
    /**
     * 表名
     *
     * @var string
     */
    protected $_tableName = 'han_store_online_goods_class';

    /**
     * 主键
     *
     * @var string
     */
    protected $_primaryKey = 'id';

    public function init(){

    }

    public function getList($where,$field = \Our\NameConst::allField){
        $this->setDb($this->dbName);
        $result = $this->db->select($field)->from($this->_tableName)->where($where)->fetchAll();
        return $result;
    }


    public function getStoreOnlineGoodsClassesByGcIdFromCache($gcId,$storeId,$field = \Our\NameConst::allField){
        $gcIds = \Our\RedisHelper::cachedFunction(\Redis\Db6\StoreOnlineGoodsClassRedisModel::getInstance(),array(&$this, 'getStoreOnlineGoodsClassesByGcId'),array($gcId,$storeId,$field),\Our\ApiConst::oneDaySecond,array($storeId));
        return $gcIds;
    }

    public function getStoreOnlineGoodsClassesByGcId($gcId,$storeId,$field = \Our\NameConst::allField){
        $where['gc_id_1'] = $gcId;
        $where['store_id'] = $storeId;
        $list = $this->getList($where);
        if($list){
            $platThirdGcIds = array();
            $storeThirdGcIds = array();
            $secondGcIds = array_unique(array_column($list,'gc_id_2'));
            $treeGcIds =array();
            foreach($list as $temp){
                if($temp['gc_from']==\Our\ApiConst::zero){
                    $platThirdGcIds[] = $temp['gc_id_3'];
                }else{
                    $storeThirdGcIds[] = $temp['gc_id_3'];
                }
                if(!(isset($treeGcIds[$temp['gc_id_2']][$temp['gc_from']])&&in_array($temp['gc_id_3'], $treeGcIds[$temp['gc_id_2']][$temp['gc_from']]))){
                    $treeGcIds[$temp['gc_id_2']][$temp['gc_from']][] = $temp['gc_id_3'];
                }
            }
            $platThirdGcIds = array_unique($platThirdGcIds);
            $storeThirdGcIds = array_unique($storeThirdGcIds);
            return array('secondGcIds'=>$secondGcIds,'treeGcIds'=>$treeGcIds,'platThirdGcIds'=>$platThirdGcIds,'storeThirdGcIds'=>$storeThirdGcIds);
        }
        return false;
    }

    /**
     * 检查获取店铺一级在售分类传参是否正确
     */
    public function checkStoreTopClasses($data){
        if(!isset($data['storeId'])){
            \Error\ErrorModel::throwException(\Error\CodeConfigModel::emptyStoreIdForOnlineTopClass);
        }
    }
    /**
     * 检查获取店铺下级在售分类及广告传参是否正确
     */
    public function checkStoreChildrenClasses($data){
        if(!isset($data['storeId'])){
            \Error\ErrorModel::throwException(\Error\CodeConfigModel::emptyStoreIdForOnlineChildrenClass);
        }
        if(!isset($data['gcId'])){
            \Error\ErrorModel::throwException(\Error\CodeConfigModel::emtpyGcIdForOnlineChildrenClass);
        }
    }
    /**
     * 类实例
     */
    private static $_instance = null;

    /**
     * 获取类实例
     */
    public static function getInstance($dbName= \Our\DbNameConst::salveDBConnectName) {
        if (!(self::$_instance instanceof self)) {
            self::$_instance = new self($dbName);
        }

        return self::$_instance;
    }
}