course_targets = @course_targets.includes(
  ec_course_achievement_methods: :ec_course_evaluation,
  ec_graduation_subitems: :ec_graduation_requirement
)
ec_year = @_current_year
major = ec_year.ec_major_school.ec_major

wb = xlsx_package.workbook
wb.styles do |style|
  title_style = style.add_style(sz: 16, height: 20, b: true)
  ec_year_style = style.add_style(sz: 10, height: 14)
  label_style = style.add_style(sz: 11, b: true, bg_color: '90EE90',
                                alignment: { horizontal: :center }, border: { style: :thin, color: '000000' })
  content_style = style.add_style(sz: 11, height: 16, border: { style: :thin, color: '000000' })

  name = "#{@_current_course.name}课程目标"
  wb.add_worksheet(name: name) do |sheet|
    sheet.add_row name, style: title_style

    sheet.add_row []
    sheet.add_row []

    sheet.add_row ['专业代码', major.code], style: ec_year_style
    sheet.add_row ['专业名称', major.name], style: ec_year_style
    sheet.add_row ['学年', "#{ec_year.year}学年"], style: ec_year_style

    sheet.add_row []
    sheet.add_row []

    index = 9
    course_targets.each do |target|
      sheet.add_row ['课程目标', "目标#{target.position}：#{target.content}"], style: [label_style, content_style]
    end
    sheet.merge_cells("A#{index}:A#{index + course_targets.size}")

    sheet.add_row []
    sheet.add_row []

    sheet.add_row %w(毕业要求 二级指标点 权重(∑=1) 课程目标), style: label_style
    course_targets.each do |target|
      target.ec_graduation_subitems.each do |item|
        requirement = item.ec_graduation_requirement

        data = [
          "#{requirement.position}：#{requirement.content}",
          "#{requirement.position}-#{item.position}：#{item.content}",
          target.weight,
          "目标#{target.position}"
        ]
        sheet.add_row data, style: content_style
      end
    end

    sheet.add_row []
    sheet.add_row []

    sheet.add_row %w(教学目标 评价方法 评价依据 达成标准(分)), style: label_style
    course_targets.each do |target|
      achievement_methods = []
      names = []

      target.ec_course_achievement_methods.each do |achievement|
        names << achievement.ec_course_evaluation.name
        achievement_methods << "#{achievement.ec_course_evaluation.name}（#{achievement.percentage}%）"
      end

      data = ["目标#{target.position}", achievement_methods.join('+'), names.join('+'), target.standard_grade]
      sheet.add_row data, style: content_style
    end
  end
end