desc "同步天府学院数据, 执行顺序依次 import_teaches、 import_courses、 import_students、import_course_members" namespace :tfxy do xlsx =Roo::Spreadsheet.open(Rails.root.join("tfxy.xlsx").to_s) task import_teaches: :environment do desc "同步天府学院老师数据" xlsx =Roo::Spreadsheet.open(Rails.root.join("tfxy.xlsx").to_s) school = School.find_or_create_by(name: '西南财经大学天府学院') do |d| d.province = '四川省' d.city = '成都' end #导入教师用户 teach_data = xlsx.sheet(3) teach_data_last_row = teach_data.last_row 2.upto(teach_data_last_row) do |r| if teach_data.cell(r, 6).to_s&.strip.present? old_user = User.where("phone = '#{teach_data.cell(r, 5)}' OR mail = '#{teach_data.cell(r, 6).to_s.strip}' OR login = 'tfxy#{teach_data.cell(r, 1)}' " ).first else old_user = User.where("phone = '#{teach_data.cell(r, 5)}' OR login = 'tfxy#{teach_data.cell(r, 1)}' ").first end if !old_user.present? user = User.new( phone: teach_data.cell(r, 5), login: "tfxy#{teach_data.cell(r, 1)}", type: 'User', status: User::STATUS_ACTIVE, lastname: teach_data.cell(r, 2), nickname: teach_data.cell(r, 2), password: "Edu#{teach_data.cell(r, 5)}", mail: teach_data.cell(r, 6).to_s&.strip.present? ? teach_data.cell(r, 6).to_s&.strip : nil, profile_completed: true, professional_certification: 1, certification: 1, authentication: true, laboratory_id: Laboratory.find(1)&.id ) if user.save! departemnt = school.departments.find_or_create_by!(name: teach_data.cell(r, 4)) user.create_user_extension!( school_id: school.id, location: school.province, location_city: school.city, gender: teach_data.cell(r, 3) == '女' ? 1 : 0, identity: 0, technical_title: '教师', department_id: departemnt.id ) open_user = OpenUsers::Cas.find_or_initialize_by(uid: teach_data.cell(r, 1)) do |u| u.extra = {phone: teach_data.cell(r, 8)} u.user_id = user.id end p "-------user -----#{user.inspect}" open_user.save end else old_user.update( lastname: teach_data.cell(r, 2), nickname: teach_data.cell(r, 2), certification: true, professional_certification: true, authentication: true, profile_completed: true) OpenUsers::Cas.find_or_create_by(uid: teach_data.cell(r, 1)) do |u| u.extra = {phone: teach_data.cell(r, 5)} u.user_id = old_user.id end end end end task import_courses: :environment do desc "同步天府学院课堂数据" school = School.find_or_create_by(name: '西南财经大学天府学院') do |d| d.province = '四川省' d.city = '成都' end course_info = {"C程序设计与实践"=>4, "数据库技术应用"=>2, "数据库应用"=>2, "数据库原理"=>2, "数据库实践"=>2, "面向对象程序设计与实践"=>2, "JavaSE程序设计及实践(辅修)"=>4, "编译原理"=>2, "电路与电子技术"=>3, "传感器原理与应用"=>3, "物联网技术与应用"=>2, "人工智能实践"=>3, "计算机组成原理"=>2, "移动应用开发实训"=>4, "面向对象程序课程设计"=>1, "Web程序设计"=>2, "数据仓库与数据挖掘"=>2, "定量分析技术"=>4, "Python程序设计"=>4, "信息系统分析与设计"=>3, "数据处理综合实训"=>2, "绘画基础Ⅱ(色彩构成)"=>2, "数字图像处理"=>4, "虚拟现实技术及应用"=>4, "三维建模技术"=>4, "游戏技术基础"=>4, "传感器与单片机实训"=>2, "数据库原理与实践"=>4, "高级程序设计(二)"=>4, "数据结构与算法分析"=>4, "实训"=>1, "Linux 操作系统"=>4, "Java Web程序设计与实践"=>8, "软件测试与项目管理"=>4, "Android程序设计"=>8, "移动应用开发"=>4, "商业智能案例分析"=>4, "数据分析与挖掘"=>4, "Oracle DBA"=>4, "信息网络安全测试与评估"=>4, "Android编程"=>8, "电子线路CAD设计"=>2, "ARM接口技术"=>4, "嵌入式Linux应用系统设计"=>4, "信息系统开发实训"=>2, "绘画基础I(平面构成)"=>2, "智能信息处理技术"=>3} course_data = xlsx.sheet(4) course_data_last_row = course_data.last_row 2.upto(course_data_last_row) do |r| course_group = CourseGroup.find_or_initialize_by(invite_code: course_data.cell(r, 3)) teach_user = OpenUsers::Cas.find_by(uid: course_data.cell(r, 5))&.user puts "----teach_user-----uid-----#{course_data.cell(r, 5)}------#{teach_user.inspect}---" if !course_group.persisted? && teach_user.present? course = Course.find_or_initialize_by(school_id: school.id, name: course_data.cell(r, 2), invite_code: course_data.cell(r, 1)[3..7]) do |d| d.is_public = 0 d.tea_id = teach_user.id d.laboratory_id = Laboratory.find(1)&.id d.credit = course_info[course_data.cell(r, 1)] end if !course.persisted? # 创建课堂 course_list = CourseList.find_or_initialize_by(name: course_data.cell(r, 2), user_id: teach_user.id) do |d| d.is_admin = 0 end course.course_list = course_list course.invite_code = course_data.cell(r, 1)[3..7] course.save! CourseInfo.create!(user_id: teach_user.id, course_id: course.id) course.create_course_modules(["shixun_homework", "common_homework", "group_homework", "exercise", "attachment", "course_group", "video"]) end course_group.position = course.course_groups_count + 1 course_group.name = course_data.cell(r, 4) course_group.course = course course_group.save! puts "---course_group----#{course_group.inspect}" course_member = CourseMember.find_or_initialize_by(course_id: course.id, user_id: teach_user.id) do |d| d.role = 2 d.course_group_id = 0 end course_member.save! if !course_member.persisted? TeacherCourseGroup.create!(course_id: course.id, course_member_id: course_member.id, user_id: teach_user.id, course_group_id: course_group.id) end end end task import_students: :environment do desc "同步天府学院学生数据" student_data = xlsx.sheet(5) student_data_last_row = student_data.last_row school = School.find_or_create_by(name: '西南财经大学天府学院') do |d| d.province = '四川省' d.city = '成都' end 2.upto(student_data_last_row) do |r| # 邮箱存在 if student_data.cell(r, 9).to_s&.strip.present? old_user = User.where("phone = '#{student_data.cell(r, 8)}' OR mail = '#{student_data.cell(r, 9).to_s.strip}' OR login = 'tfxy#{student_data.cell(r, 1)}' " ).first else old_user = User.where("phone = '#{student_data.cell(r, 8)}' OR login = 'tfxy#{student_data.cell(r, 1)}' " ).first end if !old_user.present? user = User.new( phone: student_data.cell(r, 8), login: "tfxy#{student_data.cell(r, 1)}", type: 'User', status: User::STATUS_ACTIVE, lastname: student_data.cell(r, 2), nickname: student_data.cell(r, 2), password: "Edu#{student_data.cell(r, 1)}", mail: student_data.cell(r, 9).to_s&.strip.present? ? student_data.cell(r, 9).to_s&.strip : nil, profile_completed: true, professional_certification: 1, certification: 1, authentication: true, laboratory_id: Laboratory.find(1)&.id ) puts "----user #{user.inspect}" if user.save! departemnt = school.departments.find_or_create_by!(name: student_data.cell(r, 4)) user.create_user_extension!( school_id: school.id, location: school.province, location_city: school.city, gender: student_data.cell(r, 3) == '女' ? 1 : 0, identity: 1, student_id: student_data.cell(r, 1), department_id: departemnt.id ) OpenUsers::Cas.find_or_create_by(uid: student_data.cell(r, 1)) do |u| u.extra = {phone: student_data.cell(r, 8)} u.user_id = user.id end end else # 系统已有用户,关联sso认证用户 old_user.update( lastname: student_data.cell(r, 2), nickname: student_data.cell(r, 2), certification: true, professional_certification: true, authentication: true, profile_completed: true) OpenUsers::Cas.find_or_create_by(uid: student_data.cell(r, 1)) do |u| u.extra = {phone: student_data.cell(r, 8)} u.user_id = old_user.id end end end end task import_course_members: :environment do desc "天府学院学生到课程分班" course_member_data = xlsx.sheet(6) course_member_data_last_row = course_member_data.last_row school = School.find_or_create_by(name: '西南财经大学天府学院') do |d| d.province = '四川省' d.city = '成都' end 2.upto(course_member_data_last_row) do |r| course_group = CourseGroup.find_by(invite_code: course_member_data.cell(r, 5)) student = OpenUsers::Cas.find_by(uid: course_member_data.cell(r, 1))&.user puts "------student --- #{course_member_data.cell(r, 1)}--course group--#{course_member_data.cell(r, 5)}" if student.present? && course_group.present? course_member = CourseMember.find_or_initialize_by(course_id: course_group.course_id, user_id: student.id) do |d| d.role = 4 d.course_group_id = course_group.id end puts "--------course_member #{course_member.inspect}" course_member.save! if !course_member.persisted? end end end end