/** * 在给定几个置顶数据ID的情况下,分页查询 * @return [type] [description] */ public static function getDataByPageWithTops($model, $where, $tops = array(), $top_field = 'id', $order = NULL, $fields = array(), $offset = 0, $page_size = 10, $no_total = FALSE) { //如果限制条件中有置顶所在的字段限制,置顶失效 if (empty($tops) || isset($where[$top_field])) { return self::getDataByPage($model, $where, $order, $fields, $offset, $page_size, $no_total); } if (!empty($fields)) { if (is_array($fields)) { $fields[] = $top_field; $fields = array_unique($fields); $fields = '`' . implode('`,`', $fields) . '`'; } else { $fields .= ",`{$top_field}`"; } } $total_num = 0; //查询数据总数,这个是不变的 if (!$no_total) { $total_num = $model->selectCount($where); } //查询当前条件下的top数据 $top_where = $where; $top_where[$top_field] = $tops; $top_rows = $model->select($top_where, array('select' => $fields)); if (empty($top_rows)) { //外部给了限制条件,没有top数据 return self::getDataByPage($model, $where, $order, $fields, $offset, $page_size, $no_total); } $tmp_top_rows = array(); $top_rows = Helper_Array::changeKey($top_rows, $top_field); foreach ($tops as $top_field_data) { if (empty($top_rows[$top_field_data])) { continue; } $tmp_top_rows[$top_field_data] = $top_rows[$top_field_data]; } $top_rows = $tmp_top_rows; //替换一下 //最后要返回的数据,先默认展现所有置顶数据 $ret = $top_rows; //计算原查询的一些条件。 $tops_num = count($top_rows); $curr_page_show_tops_num = $tops_num - $offset; $db_offset = 0; if ($curr_page_show_tops_num < 0) { $db_offset = abs($curr_page_show_tops_num); $ret = array(); //没有top数据需要展示了 } elseif ($curr_page_show_tops_num >= $page_size) { return array($total_num, array_slice($top_rows, $offset, $page_size)); } elseif ($curr_page_show_tops_num > 0 && $curr_page_show_tops_num < $page_size) { $ret = array_slice($ret, $offset, $curr_page_show_tops_num, TRUE); } $db_limit = $page_size + $tops_num; //查询DB数据 $db_total_num = 0; $db_rows = array(); $attrs = array('limit' => $db_limit, 'offset' => $db_offset); if (!empty($fields)) { $attrs['select'] = $fields; } if (!empty($order)) { $attrs['order_by'] = $order; } $db_rows = $model->select($where, $attrs); if (empty($db_rows)) { $db_rows = array(); } //合并数据返回 $need_db_data_num = $page_size - count($ret); foreach ($db_rows as $d) { $top_field_data = $d[$top_field]; if (isset($ret[$top_field_data])) { continue; //已经在top数据里边了 } else { $ret[$top_field_data] = $d; $need_db_data_num -= 1; } if ($need_db_data_num <= 0) { break; } } return array($total_num, $ret); }
public static function resortByOtherArr($arr, $other_arr, $field = 'id') { $arr = Helper_Array::changeKey($arr, $field); $ret = array(); foreach ($other_arr as $field_data) { if (isset($arr[$field_data])) { $ret[$field_data] = $arr[$field_data]; } else { continue; } } return $ret; }
/** * 查询 * $cache_time = 0不使用缓存 */ public function select($where = array(), $attrs = array(), $cache_time = 0) { $use_cache = $cache_time > 0; $datas = array(); $cacke_key = NULL; if ($use_cache) { ksort($where); ksort($attrs); $cache_key = $this->_conditionToCacheKey($where, $attrs); $datas = $this->_cache->get($cache_key); if (!empty($datas)) { return $datas; } } $datas = parent::select($where, $attrs); if (empty($datas)) { return $datas; } $datas = Helper_Array::changeKey($datas, $this->_primary_id); if ($use_cache) { $this->_cache->set($cache_key, $datas, $cache_time); } return $datas; }
/** * 返回结构 * array( * 'ds_id' => array('tags' => '', 'unit' => '', ...) * ... * ) * * */ public static function getDataStreamsByDeviceData($device_data) { if (empty($device_data['datastreams'])) { return array(); } $datastreams = $device_data['datastreams']; return Helper_Array::changeKey($datastreams, 'id'); }