2014-01-15 17:28:53 +08:00
# encoding: utf-8
2014-01-10 09:18:09 +08:00
# Trustie - education management software
# Copyright (C) 2013-2014
class StoresController < ApplicationController
layout 'base_stores'
2014-01-14 15:01:11 +08:00
def search
2014-08-13 15:37:28 +08:00
begin
2014-07-10 09:01:48 +08:00
q = " % #{ params [ :name ] . strip } % "
2014-10-15 09:54:54 +08:00
( redirect_to stores_url , :notice = > l ( :label_sumbit_empty ) ; return ) if params [ :name ] . blank?
2014-01-20 08:59:17 +08:00
2014-07-10 09:01:48 +08:00
result = find_public_attache q
2014-01-20 08:59:17 +08:00
@searched_attach = paginateHelper result
@result_all_count = result . count ;
2014-08-13 15:37:28 +08:00
rescue Exception = > e
#render 'stores'
2014-10-15 09:54:54 +08:00
redirect_to stores_url
2014-08-13 15:37:28 +08:00
end
2014-01-20 08:59:17 +08:00
end
def find_public_attache keywords
# StoresController#search 将每条文件都查出来,再次进行判断过滤。---> resultSet.to_a.map
# 此时内容不多速度还可, 但文件增长, 每条判断多则进行3-4次表连接。
# 现在还木有思路 药丸
resultSet = Attachment . where ( " attachments.container_type IS NOT NULL AND filename LIKE :like " , like : " % #{ keywords } % " ) .
2014-01-15 10:40:37 +08:00
reorder ( " created_on DESC " )
2014-01-17 10:18:20 +08:00
2014-12-02 17:06:17 +08:00
result = resultSet . to_a . dup
2014-01-17 10:18:20 +08:00
2014-12-02 17:06:17 +08:00
resultSet . to_a . map { | res |
if ( res . container . nil? ||
( res . container . class . to_s == " Project " && res . container . is_public == false ) ||
( res . container . has_attribute? ( :project ) && res . container . project . is_public == false ) ||
( res . container . class . to_s == " HomeworkAttach " && res . container . bid . reward_type == 3 ) ||
false
) || ( res . container . is_a? ( Course ) && res . container . is_public == 0 )
result . delete ( res )
end
}
result
2014-01-14 15:01:11 +08:00
end
LIMIT = 12 unless const_defined? ( :LIMIT )
2014-01-10 09:18:09 +08:00
def index
2014-01-16 09:47:50 +08:00
@projects_attach = project_classification ( 0 ) . take ( LIMIT )
2014-07-17 16:06:10 +08:00
@courses_attach = Attachment . includes ( :course ) . where ( " courses.is_public = 1 " ) .
2014-07-07 16:52:39 +08:00
where ( container_type : 'Course' ) .
limit ( LIMIT )
2014-01-16 09:47:50 +08:00
# @projects_attach = Attachment.includes(:project).where("projects.project_type=? AND projects.is_public = ?", 0, 1).
# reorder("#{Attachment.table_name}.downloads DESC").
# limit(LIMIT)
2014-01-13 16:34:24 +08:00
2014-01-16 09:47:50 +08:00
# @courses_attach = Attachment.includes(:project).where("projects.project_type=? AND projects.is_public = ?", 1, 1).
# reorder("#{Attachment.table_name}.downloads DESC").
# limit(LIMIT)
2014-01-16 21:05:04 +08:00
# 悲剧 下面竞赛里没文件
# @homeworks_attach = Attachment.
# joins("LEFT JOIN homework_attaches ON homework_attaches.id=attachments.container_id
# LEFT JOIN bids ON homework_attaches.bid_id=bids.id").
# where("container_type = 'HomeworkAttach' AND bids.reward_type <> 3").
# reorder("downloads DESC").
# limit(LIMIT)
@homeworks_attach = join_tools_project Message , 0
2014-01-13 16:24:13 +08:00
@memos_attach = Attachment . where ( " container_type = 'Memo' " ) .
2014-01-15 20:06:49 +08:00
reorder ( " downloads DESC " ) .
2014-01-13 16:24:13 +08:00
limit ( LIMIT )
2014-01-13 20:26:40 +08:00
@attach_array = Array . new
@attach_array . push ( @projects_attach , @courses_attach , @homeworks_attach , @memos_attach )
2014-01-14 15:41:56 +08:00
@str_arr = [ l ( :label_project_deposit ) ,
l ( :label_course_practice ) ,
2014-01-16 21:05:04 +08:00
l ( :label_borad_project ) , #l(:label_contest_innovate),
2014-01-14 15:41:56 +08:00
l ( :label_forum ) ]
2014-01-10 09:18:09 +08:00
end
2015-07-02 10:41:08 +08:00
#缺失文件列表
def lost_file
attachments = [ ]
Attachment . where ( " container_id is not null and container_type is not null and container_type <> 'Bid' and container_type <> 'HomeworkAttach' " ) . each do | attachment |
unless File . exist? ( attachment . diskfile )
attachments << attachment
end
end
respond_to do | format |
format . xls {
send_data ( homework_to_xls ( attachments ) , :type = > " text/excel;charset=utf-8; header=present " ,
:filename = > " #{ l ( :label_file_lost_list ) } .xls " )
}
end
end
2014-01-11 09:08:15 +08:00
2014-01-13 16:34:24 +08:00
private
2014-01-13 20:26:40 +08:00
2014-01-13 16:34:24 +08:00
def project_classification project_type = 0
2014-01-16 09:47:50 +08:00
# pro_attach = Attachment.joins("LEFT JOIN projects ON attachments.container_id = projects.id").
# where("attachments.container_type='Project' AND projects.is_public=1 AND projects.project_type=#{project_type}").
# reorder("downloads DESC").
# limit(LIMIT)
pro_attach = join_tools_project Project , project_type
2014-01-14 20:48:43 +08:00
doc_attach = join_tools_project Document , project_type
issue_attach = join_tools_project Issue , project_type
2014-01-16 21:05:04 +08:00
mess_attach = [ ] #join_tools_project Message, project_type
2014-01-16 09:47:50 +08:00
news_attach = join_tools_project News , project_type
vers_attach = join_tools_project Version , project_type
wiki_attach = join_tools_project WikiPage , project_type
2014-01-13 20:26:40 +08:00
2014-01-16 09:47:50 +08:00
tmp = pro_attach
tmp = pro_attach + doc_attach + issue_attach + mess_attach + news_attach + vers_attach + wiki_attach
2014-01-13 21:02:45 +08:00
tmp . sort { | a , b | b . downloads < = > a . downloads }
2014-01-13 20:26:40 +08:00
end
def join_tools_project tableName , project_type = 0
2014-01-13 21:02:45 +08:00
Attachment . joins ( str_join_table ( tableName ) ) .
2014-01-16 09:47:50 +08:00
where ( " attachments.container_type=' #{ tableName . to_s } ' AND projects.is_public=1 AND projects.project_type= #{ project_type } " ) .
2014-01-13 21:02:45 +08:00
reorder ( 'downloads DESC' ) .
limit ( LIMIT )
2014-01-13 16:34:24 +08:00
end
2014-01-14 20:48:43 +08:00
def str_join_table tableClass
2014-01-16 09:47:50 +08:00
case tableClass . to_s
when 'Project'
" LEFT JOIN projects ON attachments.container_id = projects.id "
when 'Document' , 'Issue' , 'Version' , 'News' # 连接子表即有 project_id 字段,即两层连接
" LEFT JOIN #{ tableClass . table_name } ON attachments.container_id = #{ tableClass . table_name } .id
LEFT JOIN projects ON #{tableClass.table_name}.project_id = projects.id"
when 'Message' # 三层连接
" LEFT JOIN #{ tableClass . table_name } ON attachments.container_id = #{ tableClass . table_name } .id
LEFT JOIN boards ON boards . id = #{tableClass.table_name}.board_id
LEFT JOIN projects ON boards . project_id = projects . id "
when 'WikiPage' # 三层连接
" LEFT JOIN #{ tableClass . table_name } ON attachments.container_id = #{ tableClass . table_name } .id
LEFT JOIN wikis ON wikis . id = #{tableClass.table_name}.wiki_id
LEFT JOIN projects ON wikis . project_id = projects . id "
else
end
2014-01-13 16:34:24 +08:00
end
2015-07-02 10:41:08 +08:00
#作品列表转换为excel
def homework_to_xls attachments
xls_report = StringIO . new
book = Spreadsheet :: Workbook . new
sheet1 = book . create_worksheet :name = > " homework "
blue = Spreadsheet :: Format . new :color = > :blue , :weight = > :bold , :size = > 10
sheet1 . row ( 0 ) . default_format = blue
sheet1 . row ( 0 ) . concat ( [ " 文件ID " , " 文件名 " , " 硬盘路径 " , " 上传时间 " , " 是否公开 " , " 所属对象 " , " 所属对象Id " ] )
count_row = 1
attachments . each do | attachment |
sheet1 [ count_row , 0 ] = attachment . id
sheet1 [ count_row , 1 ] = attachment . filename
sheet1 [ count_row , 2 ] = attachment . diskfile
sheet1 [ count_row , 3 ] = format_time ( attachment . created_on )
sheet1 [ count_row , 4 ] = ( attachment . is_public == 1 || attachment . is_public ) ? " 是 " :" 否 "
sheet1 [ count_row , 5 ] = attachment . container_type
sheet1 [ count_row , 6 ] = attachment . container_id
count_row += 1
end
book . write xls_report
xls_report . string
end
2014-01-10 09:18:09 +08:00
end