excel解析用户

This commit is contained in:
huang 2017-09-14 19:58:13 +08:00
parent b5f8da0ece
commit 0f2bb1734f
6 changed files with 93 additions and 103 deletions

View File

@ -14,9 +14,7 @@ unless RUBY_PLATFORM =~ /w32/
gem 'nokogiri'
end
# gem 'simple-spreadsheet'
# gem 'spreadsheet'
# gem 'parseexcel'
gem 'simple_xlsx_reader'
gem 'wechat',path: 'lib/wechat'
gem 'grack', path:'lib/grack'
gem 'gitlab', path: 'lib/gitlab-cli'

View File

@ -35,12 +35,8 @@ class CoursesController < ApplicationController
before_filter :require_login, :only => [:join, :unjoin]
#before_filter :allow_join, :only => [:join]
# require 'spreadsheet'
# require "parseexcel"
# # # require 'sequel'
# require 'win32ole'
# require 'simple-spreadsheet'
# require 'simple-spreadsheet'
require 'bundler/setup'
require 'simple_xlsx_reader'
# params[:search] 搜索课程的名称
# params[:select] "join" "create" "all" 参与的 创建的 所有的课程
@ -96,108 +92,72 @@ class CoursesController < ApplicationController
end
end
# REDO性能需要优化这块是管理员后台任务不影响用户使用
def sync_students
s=SimpleSpreadsheet::Workbook.read('files/2017/09/huang.xls')
puts s.last_row #prints 5
attachments = params[:attachments]
attachments.each do |attachment|
attachment = Attachment.find_by_token_only(attachment[1]['token'])
attachment.container_id = @course.id
attachment.container_type = "ImportStudent"
attachment.save
path = attachment.disk_directory
name = attachment.disk_filename
list = readData("files/#{path}/#{name}")
list.each do |line|
# excel内容加入到记录表中
import_student = ImportStudent.where(:name => line[0], :student_number => line[1], :course_id => @course.id).first
if import_student.blank?
import_student = ImportStudent.create(:name => line[0], :student_number => line[1], :status => 0, :school_id => school_id, :course_id => @course.id)
end
u = User.find_by_sql("SELECT u.id, u.firstname, u.lastname FROM `users` u, `user_extensions` ue where u.id = ue.user_id and student_id='#{line[0].to_s}'").first
# 如果数据库中已有该用户的注册信息,则更新相应的字段,如果没有信息,则直接将记录添加到课程成员中
if u.present?
username = u.firstname.blank? ? u.lastname : u.firstname + u.lastname
# 如果匹配上的用户已经是课程成员
user = User.find(u.id)
# if user.
else
course_member = Member.create(:course_id => @course.id, :project_id => 0, :course_group_id => 0, :is_collect => 1, :import_student_id => import_student.id)
MemberRole.create(:member_id => course_member.id, :role_id => 10, :is_current => 1)
StudentsForCourse.create(:course_id => @course.id)
end
# ue = UserExtensions.where(:student_id => line[0]).first
# 如果改用户已注册,则直接把改用户拉入到课程
# 并记录,已防改用户下次注册的时候冲突
# 未注册直接穿件用户status为0表示初始未注册状态 1表示已注册 2表示有多个账号需要处理
# workbook = Spreadsheet::ParseExcel.parse('files/2017/09/huang.xls')
# worksheet = workbook.worksheet(0)
#
# worksheet.each { |row|
# j=0
# i=0
# if row != nil
# row.each { |cell|
# if cell != nil
# contents = cell.to_s('latin1')
# puts "Row: #{j} Cell: #{i} #{contents}"
# end
# i = i+1
# }
# j = j +1
# end
# }
# Spreadsheet.client_encoding = "UTF-8"
# Spreadsheet.open('files/2017/09/111.xls')(0)
# logger.info(format.name)
# book.worksheet(0).each do |row|
# break if row[0].nil?
# puts row.join(',')
# end
# Spreadsheet.open('files/2017/09/huang.xls').sst.each do |sst|
# logger.info(sst.ole)
# end
# Spreadsheet.client_encoding = "UTF-8"
# book = Spreadsheet.open('files/2017/09/1112013.xls')
# sheet1 = book.worksheet('Sheet2') # can use an index or worksheet name
# sheet1.each do |row|
# break if row[0].nil? # if first cell empty
# puts row.join(',') # looks like it calls "to_s" on each cell's Value
# end
# excel = WIN32OLE::new('excel.Application')
# # path = "#{File.dirname(__FILE__)}../files/2017/09/lvyou.xls"
# path = File.expand_path("../../../files/2017/09/lvyou.xls", __FILE__)
# # path = "#{Dir.pwd}/files/2017/09/lvyou.xls"
# # path = "#{File.dirname(__FILE__)}/../../files/2017/09/lvyou.xls"
# workbook = excel.Workbooks.Open("files/2017/09/1112013.xls")
# worksheet = workbook.Worksheets(1) #定位到第一个sheet
# worksheet.Select
# Spreadsheet::ParseExcel.parse("files/2017/09/lvyou.xls")
# Spreadsheet.client_encoding = "UTF-8"
# book = Spreadsheet.open("files/2017/09/lvyou.xls")
# sheet1 = book.worksheet("Sheet2")
# sheet1.each do |row|
# j=0
# i=0
# if row != nil
# row.each { |cell|
# if cell != nil
# contents = cell.to_s('latin1')
# puts "Row: #{j} Cell: #{i} #{contents}"
# end
# i = i+1
# }
# j = j +1
# end
# end
@organization = Organization.find(params[:organization_id])
@addTag=false
# atttchment_type = 0为logo 1为banner
if params[:logo]
attachments = Attachment.attach_filesex(@organization, params[:attachments], false)
else
attachments = Attachment.attach_filesex(@organization, params[:attachments], true)
# 把Excel中数据添加到课程成员如果系统有成员和Excel中信息匹配上则更新
if u.present? && username == line[0]
course_member = Member.create(:course_id => @course.id, :project_id => 0, :course_group_id => 0, :is_collect => 1,
:import_student_id => import_student.id, :user_id => u.id)
MemberRole.create(:member_id => course_member.id, :role_id => 10, :is_current => 1)
StudentsForCourse.create(:course_id => @course.id, :student_id => u.id)
import_student.update_column(:status, 1)
else
course_member = Member.create(:course_id => @course.id, :project_id => 0, :course_group_id => 0, :is_collect => 1, :import_student_id => import_student.id)
MemberRole.create(:member_id => course_member.id, :role_id => 10, :is_current => 1)
StudentsForCourse.create(:course_id => @course.id)
end
end
end
# TODO: 临时用 nyan
sort_init 'created_on', 'desc'
sort_update 'created_on' => "#{Attachment.table_name}.created_on",
'filename' => "#{Attachment.table_name}.filename",
'size' => "#{Attachment.table_name}.filesize",
'downloads' => "#{Attachment.table_name}.downloads"
@containers = [Organization.includes(:attachments).reorder("#{Attachment.table_name}.created_on DESC").find(@organization.id)]
show_attachments @containers
@tag_list = attachment_tag_list @all_attachments
@attachtype = 0
@contenttype = 0
render :json => {:data => "success"}
end
def readData(xlsx)
doc = SimpleXlsxReader.open(xlsx)
sheet = doc.sheets.first
sheet.rows.inject([]) { |l, row|
if row.size >= 3 && row[1] && row[2] && row[1].size == '201705130701'.size
l << [row[1], row[2]]
else
l
end
}
end
# 邀请码停用/启用
def set_invite_code_halt
if User.current.allowed_to?(:as_teacher, @course) || User.current.admin?

View File

@ -0,0 +1,4 @@
class ImportStudent < ActiveRecord::Base
attr_accessible :course_id, :name, :school_id, :status, :student_number
# status 如果用户没有注册状态为01表示用户已有注册且只有一个账号2表示用户已有注册但是有两个一样的账号
end

View File

@ -0,0 +1,13 @@
class CreateImportStudents < ActiveRecord::Migration
def change
create_table :import_students do |t|
t.string :name
t.string :student_number
t.integer :course_id
t.integer :school_id
t.integer :status, :default => 0
t.timestamps
end
end
end

View File

@ -0,0 +1,10 @@
FactoryGirl.define do
factory :import_student do
name "MyString"
student_number "MyString"
course_id 1
school_id 1
status 1
end
end

View File

@ -0,0 +1,5 @@
require 'rails_helper'
RSpec.describe ImportStudent, :type => :model do
pending "add some examples to (or delete) #{__FILE__}"
end