<?php
namespace Home\Model;
use Think\Model;

/**
 * 配置系统模型
 * @authors yc ()
 * @date    2017-08-02 11:15:25
 * @version $Id$
 */
class CmsModel extends Model {
    protected $autoCheckFields = false;

    public function _initialize()
    {
        parent::_initialize();
        $this->ScmsModel = D('Home/Scms');
        $this->connection = C('DB_OTHER.CMS');
    }

    public function getModel($key = '', $field = 'model_id')
    {
        static $model_key;
        if (!isset($model_key[$key])) {
            $this->ScmsModel = D('Home/Scms');
            $model_key[$key] = $this->ScmsModel->getModel($key);
            if (empty($model_key[$key])) {
                $map = array(
                    'model_key' => $key,
                );
                $model_key[$key] = $this->table('lie_model')->where($map)->field('model_id, model_name')->find();
            }
        }
        return $model_key[$key][$field];
    }

    private function formatwhere($map, $where)
    {
        if (is_string($where)) {
            if (!empty($where)) {
                $where = array('_string' => $where);
            } else {
                $where = array();
            }
        }
        $map = array_merge($map, $where);
        return $map;
    }

    /**
     * 获取基本配置
     * @return [type] [description]
     */
    public function getSetting()
    {
        $data = $this->table('lie_setting')->getField('set_key,set_val');
        return $data;
    }

    /**
     * 获取底部链接分类
     * @param  [type] $fcat_key [description]
     * @return [type]           [description]
     */
    public function getFooterLinkCat($fcat_key)
    {
        $this->ScmsModel = D('Home/Scms');
        $datas = $this->ScmsModel->getFooterLinkCat($fcat_key, 'fcat_id');
        if (!empty($datas)) {
            return $datas;
        }
        $map = array(
            'fcat_key' => $fcat_key,
        );
        $datas = $this->table('lie_footer_link_cat')->where($map)->limit($limit)->getField('fcat_id');
        return $datas;
    }

    /**
     * 获取底部链接分类列表
     * @param  string $where [description]
     * @param  string $limit [description]
     * @return [type]        [description]
     */
    public function getFooterLinkCatList($where = '', $limit = '')
    {
        $this->ScmsModel = D('Home/Scms');
        $parent_id = isset($where['parent_id']) ? $where['parent_id'] : 0;
        $datas = $this->ScmsModel->getFooterLinkCat('', '', $parent_id);
        if (!empty($datas)) {
            $datas = array_slice($datas, 0, $limit);
            return $datas;
        }
        $datas = $this->table('lie_footer_link_cat')->where($where)->limit($limit)
                            ->order('sort DESC')->field('fcat_id,fcat_name,parent_id')
                            ->select();
        return $datas;
    }

    /**
     * 获取底部链接
     * @param  [type] $fcat_key [description]
     * @return [type]           [description]
     */
    public function getFooterLink($fcat_id, $where = '', $limit = '')
    {
        $this->ScmsModel = D('Home/Scms');
        $datas = $this->ScmsModel->getFooterLink($fcat_id);
        if (!empty($datas)) {
            $dats = array_slice($datas, 0, $limit);
            return $datas;
        }
        $map = array(
            'fcat_id' => $fcat_id,
            'status' => 1,
        );
        $map = $this->formatwhere($map, $where);
        $datas = $this->table('lie_footer_link')->where($map)->limit($limit)->field('flink_name,url,class,tip')->select();
        return $datas;
    }

    /**
     * 获取基础分类ID
     * @param  [type] $tags  [description]
     * @param  string $field [description]
     * @return [type]        [description]
     */
    public function getBaseCat($tags, $field = 'bcat_id')
    {
        static $bcat;
        if (!isset($bcat[$tags])) {
            $map = array(
                'tags' => $tags,
            );
            $bcat[$tags] = $this->table('lie_base_cat')->where($map)->field('bcat_id, bcat_name')->find();
        }
        return $bcat[$tags][$field];
    }

    /**
     * 获取配置分类值
     * @param  [type] $tags  [description]
     * @param  string $where [description]
     * @param  string $limit [description]
     * @return [type]        [description]
     */
    public function getBaseList($tags, $where = '', $limit = '')
    {
        $bcat_id = $this->getBaseCat($tags);
        $map = array(
            'bcat_id' => $bcat_id,
            '_string' => "(status = 1) OR (status = 2 AND open_time >= {$_SERVER['REQUEST_TIME']} AND close_time <= {$_SERVER['REQUEST_TIME']})",
        );
        $map = $this->formatwhere($map, $where);
        $datas = $this->table('lie_base')->where($map)->limit($limit)->order('sort DESC')->field('*')->select();
        return $datas;
    }

}