package com.ningdatech.pmapi.leave.entity.po;

import com.ningdatech.basic.exception.BizException;
import com.ningdatech.pmapi.leave.entity.enumeration.LeaveTypeEnum;
import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
import lombok.Data;
import org.apache.commons.lang3.ObjectUtils;

import javax.validation.constraints.NotBlank;
import javax.validation.constraints.NotNull;
import java.time.LocalDateTime;
import java.util.List;

/**
 * <p>
 * LeaveCreatePo
 * </p>
 *
 * @author WendyYang
 * @since 10:30 2022/8/11
 */
@Data
@ApiModel("请假创建实体")
public class LeaveCreateReq {

    @ApiModelProperty("请假类型：1 长期请假、2 固定时段请假、3 临时请假")
    @NotNull(message = "请假类型不能为空")
    private Integer type;

    @ApiModelProperty("请假开始时间")
    private LocalDateTime startTime;

    @ApiModelProperty("请假结束时间")
    private LocalDateTime endTime;

    @ApiModelProperty("请假说明")
    @NotBlank(message = "请假说明不能为空")
    private String postscript;

    @ApiModelProperty("附件ID")
    private List<Long> attachments;

    @ApiModelProperty("固定时段")
    private List<Integer> fixedType;

    @ApiModelProperty("会议ID")
    private Long meetingId;

    @ApiModelProperty("专家ID：管理员替专家请假时传参")
    private Long expertId;

    public void paramCheck(LeaveTypeEnum leaveTypeEnum) {
        if (leaveTypeEnum.equals(LeaveTypeEnum.TEMPORARY)) {
            if (this.getMeetingId() == null) {
                throw new BizException("会议ID不能为空");
            }
        } else {
            if (ObjectUtils.anyNull(this.getStartTime(), this.getEndTime())) {
                throw new BizException("开始时间或结束时间不能为空");
            }
            if (!this.getStartTime().isBefore(this.getEndTime())) {
                throw new BizException("无效的请假时间");
            }
            if (leaveTypeEnum.equals(LeaveTypeEnum.FIXED_TERM)) {
                if (this.getFixedType() == null || this.getFixedType().isEmpty()) {
                    throw new BizException("固定时段不能为空");
                }
            }
        }
    }

}
