graduation_subitems = @graduation_subitems
ec_year = @_current_year
major = ec_year.ec_major_school.ec_major

max_support_length = graduation_subitems.map { |item| item.ec_course_supports.size }.max
last_column_index = 2 + max_support_length

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, vertical: :center }, border: { style: :thin, color: '000000' })
  content_style = style.add_style(sz: 11, height: 16, border: { style: :thin, color: '000000' })
  tip_style = style.add_style(sz: 11, height: 16, color: Axlsx::Color.new(rgb: 'FFFFA07A'))

  wb.add_worksheet(:name => '课程体系对毕业要求的支撑') do |sheet|
    sheet.add_row ['课程体系VS毕业要求'], style: title_style
    sheet.merge_cells sheet.rows.first.cells[(1..(3 + max_support_length - 1))]

    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 ['注：有对应关系的课程名称下方为其权重系数,一个指标点的权重系数之和必须等于1'], style: tip_style
    sheet.add_row ['注：“★” 表示关联度高']
    sheet.merge_cells sheet.rows[5].cells[(1..(3 + max_support_length - 1))]
    sheet.merge_cells sheet.rows[6].cells[(1..(3 + max_support_length - 1))]

    sheet.add_row []

    data = ['毕业要求', '', '课程名称']
    data[last_column_index] = '课程数量'
    sheet.add_row data, style: label_style
    course_columns = max_support_length.times.map { |i| "课程#{i + 1}" }
    sheet.add_row %w(一级 二级) + course_columns + ['∑目标值'], style: label_style
    sheet.merge_cells("A9:B9")
    # sheet.merge_cells sheet.rows[8].cells[(3..(3 + max_support_length - 1))]

    current_row = 11
    graduation_subitems.group_by(&:ec_graduation_requirement).each do |requirement, items|
      position = requirement.position
      start_row = current_row

      items.each do |item|
        course_data = [position, "#{position}.#{item.position}"]
        weight_data = ['', '']

        total_weight = item.ec_course_supports.sum do |support|
          course_data << support.ec_course.name.to_s
          weight_data << support.weight.to_s

          support.weight.to_f
        end

        course_data[last_column_index] = item.ec_course_supports.size
        weight_data[last_column_index] = total_weight.to_s

        styles = [label_style, label_style,].concat([content_style] * max_support_length)
        sheet.add_row course_data, style: styles
        sheet.add_row weight_data, style: styles

        sheet.merge_cells("B#{current_row}:B#{current_row + 1}")
        current_row += 2
      end

      sheet.merge_cells("A#{start_row}:A#{current_row - 1}")
    end
  end
end