Skip to content

Instantly share code, notes, and snippets.

@msg7086
Created November 23, 2015 00:33
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save msg7086/60e806bcccc00a49ecf4 to your computer and use it in GitHub Desktop.
Save msg7086/60e806bcccc00a49ecf4 to your computer and use it in GitHub Desktop.
def get_list sort: 'new', cate: nil, type: nil, ch: nil, kind: 'normal', cid: 0, page: 0, per_page: 10
data = Soft
pcate = root_key && cached_get(root_key)
data = data.where(['charIndex(?, categoryPath) > 0', "/#{pcate.ID}/"]) if !pcate.nil?
data = data.where(['charIndex(?, categoryPath) > 0 or charIndex(?, categoryPath) > 0)', *ch_args]) if !ch_args.empty?
if cate
cate_ent = cached_get(root_key, cate)
data = data.where(category_id: cate_ent.ID)
end
cid = cid.to_i
if cid > 0
cate_ent = cached_get(cid)
data = data.where(category_id: cid)
end
kind_args = get_kind(kind)
data = data.where(kind_args[:where]) if kind_args[:where]
order = kind_args[:sort] || order_by(sort)
data.order(order) if order
# Pagination
page = [0, page.to_i].max
per_page = [10, per_page.to_i].max
data = data.paginate(page: page, per_page: per_page)
data.map do |d|
{ 'id' => d.id,
'name' => d.name,
'version' => d.version,
'cid' => d.categoryid,
'cpath' => PathService.GetListPath(d.category),
'cname' => d.category.Name,
'path' => PathService.GetPath(d.category, d.id),
'dtimes' => d.viewtimes
}
end
end
private
def root_key type
case type
when 'appsoft' then 'iphones'
when 'appgame' then 'iphoneg'
when 'azsoft' then 'soft'
when 'azgame' then 'game'
else nil
end
end
def ch_args ch
case ch
when 'az' then [cached_get('game').ID, cached_get('soft').ID]
when 'app' then [cached_get('appgame').ID, cached_get('appsoft').ID]
else []
end.map {|s| "/#{s}/"}
end
def order_by sort
case sort
when 'new' then {id: :desc}
when 'hot' then {viewTimes: :desc, id: :desc}
when 'rank' then {downWeekTimes: :desc, id: :desc}
else nil
end
end
def get_kind kind
case kind
when 'number' then {where: 'number > 0', sort: {number: :desc, id: :desc}}
when 'hprec' then {where: 'recommend = 1 and homepage = 1 and number = 0', sort: {viewTimes: :desc, id: :desc}}
when 'recnothp' then {where: 'recommend = 1 and homepage = 0 and number = 0', sort: {viewTimes: :desc, id: :desc}}
when 'rec' then {where: 'recommend = 1'}
when 'all' then {}
else {where: 'recommend = 0 and homepage = 0 and number = 0'}
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment