示例#1
0
 /**
  * 
  * Enter description here ...
  * @param unknown_type $params
  */
 public function Pages($params = array())
 {
     //设置默认参数
     $_defaults_params = array('allow_cache' => true, 'page' => isset($_GET['page']) ? intval($_GET['page']) : 1, 'pagesize' => 10);
     $params = array_merge($_defaults_params, $params);
     //有开启缓存功能,则从缓存中取数据, 如果有数据,则直接返回结果
     if ($params['allow_cache'] && isset($this->cache)) {
         $cacheKey = 'collect.rule.pages.' . serialize($params);
         $ret = $this->cache->get($cacheKey);
         if ($ret && is_array($ret)) {
             return $ret;
         }
     }
     //添加条件
     $builds = array('select' => 'COUNT(ct.collect_rule_id) AS COUNT', 'from' => array('{{collect_rule}}', 'ct'));
     if (isset($params['collect_rule_status']) && !empty($params['collect_rule_status'])) {
         $builds['where'][] = array('AND', 'ct.collect_rule_status=:collect_rule_status');
         $sql_params[':collect_rule_status'] = $params['collect_rule_status'];
     } else {
         $builds['where'][] = array('AND', 'ct.collect_rule_status>:collect_rule_status');
         $sql_params[':collect_rule_status'] = 0;
     }
     if (isset($params['collect_rule_id']) && !empty($params['collect_rule_id'])) {
         $builds['where'][] = array('AND', 'ct.collect_rule_id=:collect_rule_id');
         $sql_params[':collect_rule_id'] = $params['collect_rule_id'];
     }
     //
     if (isset($params['collect_rule_name']) && !empty($params['collect_rule_name'])) {
         $builds['where'][] = array('LIKE', 'ct.collect_rule_name', '%:collect_rule_name%');
         $sql_params[':collect_rule_name'] = $params['collect_rule_name'];
     }
     //
     //
     if (isset($params['searchKey']) && $params['searchKey']) {
         $builds['where'][] = array('OR', array('OR LIKE', 'ct.collect_rule_name', ":searchKey"));
         $sql_params[':searchKey'] = "%{$params['searchKey']}%";
     }
     $sql = $this->buildQuery($builds);
     //统计数量
     $count = $this->db->queryScalar($sql, $sql_params);
     //分页处理
     $pages = new CPagination($count);
     //设置分页大小
     $pages->pageSize = $params['pagesize'];
     if (isset($params['orderby']) && $params['orderby']) {
         $builds['order'] = $params['orderby'];
     } else {
         $builds['order'] = array('ct.collect_rule_dateline DESC');
     }
     $builds['select'] = 'ct.collect_rule_id, ct.collect_rule_name';
     $pages->applyLimit($builds);
     $sql = $this->buildQuery($builds);
     $ret = array('pages' => $pages, 'rows' => $this->db->queryAll($sql, $sql_params));
     //有开启缓存,则把结果添加到缓存中
     if ($params['allow_cache'] && isset($this->cache)) {
         $cacheTimeout = SettingModel::getSettingValue('COLLECT_TEMPLATE_PAGES_CACHE_TIME');
         $this->cache->set($cacheKey, json_encode($ret), $cacheTimeout);
         unset($cacheTimeout, $cacheKey);
     }
     return $ret;
 }