package com.ruoyi.course.domain;


import com.baomidou.mybatisplus.annotation.IdType;
import com.baomidou.mybatisplus.annotation.TableId;
import com.baomidou.mybatisplus.annotation.TableName;
import com.ruoyi.common.utils.StringUtils;
import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
import lombok.Data;
import lombok.EqualsAndHashCode;
import lombok.experimental.Accessors;
import com.ruoyi.course.enums.GradeEnum;

import java.io.Serializable;
import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.stream.Collectors;

/**
 * <p>
 * 课程信息表
 * </p>
 *
 * @author xu
 * @since 2025-12-14
 */
@Data
@EqualsAndHashCode(callSuper = false)
@Accessors(chain = true)
@TableName("course")
@ApiModel(value="Course对象", description="课程信息表")
public class Course implements Serializable {

    private static final long serialVersionUID = 1L;

    @ApiModelProperty(value = "主键ID")
    @TableId(value = "id", type = IdType.AUTO)
    private Long id;

    @ApiModelProperty(value = "课程代码")
    private String courseCode;

    @ApiModelProperty(value = "课程名称")
    private String courseName;

    @ApiModelProperty(value = "课程类型(必修/选修/兴趣)")
    private String courseType;

    @ApiModelProperty(value = "所属学科")
    private String subject;

    @ApiModelProperty(value = "年级范围(高一/高二/高三/全部)")
    private String gradeLevel;

    @ApiModelProperty(value = "课时长度(分钟)")
    private Integer duration;

    @ApiModelProperty(value = "每周课时数")
    private Integer hoursPerWeek;

    @ApiModelProperty(value = "课程优先级(1=高优先级,2=中优先级,3=低优先级)")
    private Integer priority;

    @ApiModelProperty(value = "是否跨班级(0=不跨班,1=跨班)")
    private Integer crossClass;

    @ApiModelProperty(value = "状态(0=停用,1=启用)")
    private Integer status;

    @ApiModelProperty(value = "创建时间")
    private LocalDateTime createTime;

    @ApiModelProperty(value = "更新时间")
    private LocalDateTime updateTime;

    /**
     * 获取年级枚举列表
     */
    public List<GradeEnum> getGradeEnums() {
        if (StringUtils.isBlank(gradeLevel)) {
            return Collections.emptyList();
        }

        String[] codes = gradeLevel.split(",");
        List<GradeEnum> result = new ArrayList<>();
        for (String codeStr : codes) {
            try {
                int code = Integer.parseInt(codeStr.trim());
                GradeEnum grade = GradeEnum.fromCode(code);
                result.add(grade);
            } catch (Exception e) {

            }
        }
        return result;
    }

    /**
     * 设置年级枚举列表
     */
    public void setGradeEnums(List<GradeEnum> grades) {
        if (grades == null || grades.isEmpty()) {
            this.gradeLevel = "";
            return;
        }

        this.gradeLevel = grades.stream()
                .map(GradeEnum::getCode)
                .map(String::valueOf)
                .collect(Collectors.joining(","));
    }
}
