graduation_requirements = @graduation_requirements
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, alignment: { horizontal: :center })
  content_style = style.add_style(sz: 11, height: 16, border: { style: :thin, color: '000000' })

  wb.add_worksheet(name: '达成度-毕业要求综合评价报表') do |sheet|
    sheet.add_row ['达成度-毕业要求综合评价报表'], style: title_style
    sheet.merge_cells("A1:D1")

    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 ['毕业要求', '', '课程数量 ∑合格标准', '评价结果'], style: label_style
    sheet.add_row ['一级', '二级', '∑达成值', ''], style: label_style
    sheet.merge_cells("A7:B7")
    sheet.merge_cells("D7:D8")

    index = 9

    graduation_requirements.each do |requirement|
      start_index = index

      requirement.ec_graduation_subitems.each do |item|
        reach_criteria, actually_reach = 0.0, 0.0

        item.ec_course_supports.each do |support|
          reach_criteria += support.weights.to_f * ec_year.calculation_value.to_f
          actually_reach += support.ec_graduation_requirement_calculation&.real_value.to_f
        end

        size   = item.ec_course_supports.size
        status = actually_reach >= reach_criteria ? '已达成' : '未达成'

        base_arr = [requirement.position.to_s, "#{requirement.position}-#{item.position}", status]
        sheet.add_row base_arr.dup.insert(-2, size), style: content_style
        sheet.add_row base_arr.dup.insert(-2, format('%.03f', reach_criteria)), style: content_style
        sheet.add_row base_arr.dup.insert(-2, format('%.03f', actually_reach)), style: content_style

        sheet.merge_cells("B#{index}:B#{index + 2}")
        sheet.merge_cells("D#{index}:D#{index + 2}")

        index += 3
      end

      sheet.merge_cells("A#{start_index}:B#{index - 1}")
    end
  end
end