package com.qmth.themis.business.bean.cloudmark;

import com.fasterxml.jackson.annotation.JsonInclude;
import com.fasterxml.jackson.databind.annotation.JsonSerialize;
import com.fasterxml.jackson.databind.ser.std.ToStringSerializer;
import com.qmth.themis.business.enums.CloudMarkFileUploadTypeEnum;
import com.qmth.themis.common.exception.BusinessException;
import io.swagger.annotations.ApiModelProperty;
import org.apache.commons.codec.digest.DigestUtils;

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.Serializable;
import java.util.Objects;

/**
 * @Description: 推送云阅卷文件接口参数
 * @Param:
 * @return:
 * @Author: wangliang
 * @Date: 2022/9/28
 */
@JsonInclude(JsonInclude.Include.NON_NULL)
public class FileUploadParams extends BaseParams implements Serializable {

    @ApiModelProperty(value = "云阅卷考试id")
    @JsonSerialize(using = ToStringSerializer.class)
    private Long examId;

    @ApiModelProperty(value = "准考证号")
    private String examNumber;

    @ApiModelProperty(value = "文件")
    private File file;

    @ApiModelProperty(value = "文件md5")
    private String md5;

    @ApiModelProperty(value = "文件格式")
    private String format;

    @ApiModelProperty(value = "科目代码")
    private String subjectCode;

    @ApiModelProperty(value = "文件类型")
    private CloudMarkFileUploadTypeEnum type;

    public FileUploadParams() {

    }

    public FileUploadParams(Long orgId, Long examId, String examNumber, File file, CloudMarkFileUploadTypeEnum type) throws IOException {
        super(orgId, null, null);
        this.examId = examId;
        this.examNumber = examNumber;
        this.file = file;
        this.type = type;
        this.md5 = DigestUtils.md5Hex(new FileInputStream(file));
    }

    public FileUploadParams(Long orgId, Long examId, String subjectCode, CloudMarkFileUploadTypeEnum format, File file, CloudMarkFileUploadTypeEnum type) throws IOException {
        super(orgId, null, null);
        this.examId = examId;
        this.subjectCode = subjectCode;
        this.format = format.getCode();
        this.file = file;
        this.type = type;
        this.md5 = DigestUtils.md5Hex(new FileInputStream(file));
    }

    public CloudMarkFileUploadTypeEnum getType() {
        return type;
    }

    public void setType(CloudMarkFileUploadTypeEnum type) {
        this.type = type;
    }

    public void vaildParams() {
        if (Objects.isNull(this.type)) {
            throw new BusinessException("文件类型不能为空");
        }
        switch (this.type) {
            case JSON:
                if (Objects.isNull(this.examNumber)) {
                    throw new BusinessException("准考证号不能为空");
                }
                break;
            case PAPER:
                if (Objects.isNull(this.subjectCode)) {
                    throw new BusinessException("科目编码不能为空");
                }
                if (Objects.isNull(this.format)) {
                    throw new BusinessException("文件类型不能为空");
                }
                break;
            default:
                break;
        }
        if (Objects.isNull(this.examId)) {
            throw new BusinessException("云阅卷考试id不能为空");
        }
        if (Objects.isNull(this.file)) {
            throw new BusinessException("文件不能为空");
        }
        if (Objects.isNull(this.md5)) {
            throw new BusinessException("文件md5不能为空");
        }
    }

    public File getFile() {
        return file;
    }

    public void setFile(File file) {
        this.file = file;
    }

    public String getMd5() {
        return md5;
    }

    public void setMd5(String md5) {
        this.md5 = md5;
    }

    public String getFormat() {
        return format;
    }

    public void setFormat(String format) {
        this.format = format;
    }

    public String getSubjectCode() {
        return subjectCode;
    }

    public void setSubjectCode(String subjectCode) {
        this.subjectCode = subjectCode;
    }

    public Long getExamId() {
        return examId;
    }

    public void setExamId(Long examId) {
        this.examId = examId;
    }

    public String getExamNumber() {
        return examNumber;
    }

    public void setExamNumber(String examNumber) {
        this.examNumber = examNumber;
    }
}
