package com.qmth.teachcloud.common.bean.params;

import com.fasterxml.jackson.databind.annotation.JsonSerialize;
import com.fasterxml.jackson.databind.ser.std.ToStringSerializer;
import com.qmth.teachcloud.common.annotation.DBVerify;
import com.qmth.teachcloud.common.enums.ExceptionResultEnum;
import io.swagger.annotations.ApiModelProperty;
import org.hibernate.validator.constraints.Length;
import org.hibernate.validator.constraints.Range;

import javax.validation.constraints.NotNull;
import java.io.Serializable;
import java.util.Optional;

/**
 * @Description: 基础学期参数
 * @Author: CaoZixuan
 * @Date: 2021-11-01
 */
public class BasicSemesterParams implements Serializable {

    @JsonSerialize(using = ToStringSerializer.class)
    @ApiModelProperty(value = "学期id（编辑时必填）")
    @DBVerify(value = "学期id（编辑时必填）")
    private Long id;

    @ApiModelProperty(value = "学期名称")
    @NotNull(message = "请输入学期名称")
    @Length(min = 1, message = "请输入学期名称")
    @DBVerify(value = "学期名称", required = true, min = 1, max = 50)
    private String semesterName;

    @ApiModelProperty(value = "学期开始时间")
    @NotNull(message = "请选择学期开始时间")
    @Range(min = 1L, message = "请选择学期开始时间")
    @DBVerify(value = "学期开始时间", required = true, min = 1)
    private Long startTime;

    @ApiModelProperty(value = "学期结束时间")
    @NotNull(message = "请选择学期结束时间")
    @Range(min = 1L, message = "请选择学期结束时间")
    @DBVerify(value = "学期结束时间", required = true, min = 1)
    private Long endTime;

    @ApiModelProperty(value = "启用/禁用，ture：启用，false：禁用，默认启用")
    Boolean enable = true;

    public BasicSemesterParams() {

    }

    public BasicSemesterParams(String semesterName, Long startTime, Long endTime) {
        this.semesterName = semesterName;
        this.startTime = startTime;
        this.endTime = endTime;
    }

    public BasicSemesterParams(Long id, String semesterName, Long startTime, Long endTime) {
        this.id = id;
        this.semesterName = semesterName;
        this.startTime = startTime;
        this.endTime = endTime;
    }

    /**
     * 参数校验
     */
    public void validParams() {
        Optional.ofNullable(this.getSemesterName()).orElseThrow(() -> ExceptionResultEnum.PARAMS_ERROR.exception("学期名称为空"));
        Optional.ofNullable(this.getStartTime()).orElseThrow(() -> ExceptionResultEnum.PARAMS_ERROR.exception("学期开始时间为空"));
        Optional.ofNullable(this.getEndTime()).orElseThrow(() -> ExceptionResultEnum.PARAMS_ERROR.exception("学期结束时间为空"));
    }

    public Boolean getEnable() {
        return enable;
    }

    public void setEnable(Boolean enable) {
        this.enable = enable;
    }

    public Long getId() {
        return id;
    }

    public void setId(Long id) {
        this.id = id;
    }

    public String getSemesterName() {
        return semesterName;
    }

    public void setSemesterName(String semesterName) {
        this.semesterName = semesterName;
    }

    public Long getStartTime() {
        return startTime;
    }

    public void setStartTime(Long startTime) {
        this.startTime = startTime;
    }

    public Long getEndTime() {
        return endTime;
    }

    public void setEndTime(Long endTime) {
        this.endTime = endTime;
    }
}
