package jnpf.model.certificate.req;

import jnpf.model.certificate.po.CertificateInstanceEntity;
import jnpf.model.warningnotice.enums.CertificateTypeEnum;
import jnpf.util.FtbUtil;
import lombok.Data;
import org.hibernate.validator.constraints.Length;

import javax.validation.Valid;
import javax.validation.constraints.Max;
import javax.validation.constraints.Min;
import javax.validation.constraints.NotBlank;
import javax.validation.constraints.NotNull;
import java.io.Serializable;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * 证照实例新增参数。
 */
@Data
public class CertificateInstanceAddReq implements Serializable {

    private static final long serialVersionUID = 1L;

    @NotNull(message = "证照类型不能为空")
    private CertificateTypeEnum type;

    @Length(max = 50, message = "模板ID长度不能超过50")
    private String templateId;

    @NotNull(message = "主体类型不能为空")
    @Min(value = 1, message = "主体类型范围必须在1到3之间")
    @Max(value = 3, message = "主体类型范围必须在1到3之间")
    private Integer subjectType;

    @NotBlank(message = "主体ID不能为空")
    @Length(max = 50, message = "主体ID长度不能超过50")
    private String subjectId;

    @Length(max = 100, message = "证照编号长度不能超过100")
    private String certificateNo;

    @Length(max = 20, message = "发证日期长度不能超过20")
    private String issueDate;

    @Length(max = 20, message = "到期日期长度不能超过20")
    private String expireDate;

    @NotNull(message = "是否长期不能为空")
    @Min(value = 0, message = "是否长期只能为0或1")
    @Max(value = 1, message = "是否长期只能为0或1")
    private Integer isLongTerm;

    @Length(max = 1000, message = "证书图片长度不能超过1000")
    private String certificateImage;

    @Valid
    private List<CertificateInstanceItemReq> itemList = new ArrayList<>();

    public CertificateInstanceEntity convert(Date issueDateValue, Date expireDateValue, Integer statusValue) {
        CertificateInstanceEntity entity = new CertificateInstanceEntity();
        entity.setId(FtbUtil.getId());
        entity.setCertificateType(this.type == null ? null : this.type.getType());
        entity.setTemplateId(this.templateId == null ? null : this.templateId.trim());
        entity.setSubjectType(this.subjectType);
        entity.setSubjectId(this.subjectId == null ? null : this.subjectId.trim());
        entity.setCertificateNo(this.certificateNo == null ? null : this.certificateNo.trim());
        entity.setIssueDate(issueDateValue);
        entity.setExpireDate(expireDateValue);
        entity.setIsLongTerm(this.isLongTerm);
        entity.setStatus(statusValue);
        entity.setCertificateImage(this.certificateImage == null ? null : this.certificateImage.trim());
        entity.setEnabledMark(0);
        return entity;
    }
}
