package com.bocloud.ims.entity.resource.contentlibrary;

import com.alibaba.fastjson.annotation.JSONField;
import com.bocloud.common.encrypt.DESEncrptor;
import com.bocloud.common.encrypt.Encryptor;
import com.bocloud.common.utils.DateDeserializer;
import com.bocloud.common.utils.DateSerializer;
import com.bocloud.entity.annotations.Column;
import com.bocloud.entity.annotations.IgnoreAll;
import com.bocloud.entity.annotations.PK;
import com.bocloud.entity.annotations.Table;
import com.bocloud.entity.bean.GenericEntity;
import com.bocloud.entity.meta.PKStrategy;
import com.fasterxml.jackson.databind.annotation.JsonDeserialize;
import com.fasterxml.jackson.databind.annotation.JsonSerialize;
import lombok.*;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.format.annotation.DateTimeFormat;

import java.util.Date;

/**
 * 内容库模板
 *
 * @author wangyu
 * @version 5.0.0-SNAPSHOT
 * @since 2021/2/26
 */
@Data
@NoArgsConstructor
@AllArgsConstructor
@Table("content_library_item")
public class ContentLibraryItem extends GenericEntity {
    private static Logger logger = LoggerFactory.getLogger(ContentLibraryItem.class);
    @PK(PKStrategy.AUTO)
    private Long id;
    @Column("item_uuid")
    private String itemUuid;
    @Column("content_version")
    private String contentVersion;
    @Column("cached")
    private Boolean cached;
    @Column("size")
    private Long size;
    @Column("metadata_version")
    private String metadataVersion;
    @Column("version")
    private String version;
    @Column("type")
    private String type;
    @Column("library_uuid")
    private String libraryUuid;
    @Column("library_id")
    private Long libraryId;
    @Column(value = "creation_time", nullable = false)
    @JsonSerialize(using = DateSerializer.class)
    @JsonDeserialize(using = DateDeserializer.class)
    @DateTimeFormat(pattern = "yyyy-MM-dd HH:mm:ss")
    private Date creationTime;
    @Column(value = "last_modified_time", nullable = false)
    @JsonSerialize(using = DateSerializer.class)
    @JsonDeserialize(using = DateDeserializer.class)
    @DateTimeFormat(pattern = "yyyy-MM-dd HH:mm:ss")
    private Date lastModifiedTime;
    @Column("vendor_id")
    private Long vendorId;
    @Column("project_id")
    private Long projectId;
    @Column("tenant_id")
    private Long tenantId;
    @Column("password")
    private String password; // 密码
    @IgnoreAll
    private String projectName;
    @IgnoreAll
    private String tenantName;
    @IgnoreAll
    private String libraryName;

    @IgnoreAll
    @Getter(AccessLevel.NONE)
    @Setter(AccessLevel.NONE)
    private Encryptor encryptor = new DESEncrptor();
    /**
     * @param password the password to set
     */
    @JSONField(serialize = false)
    public void setPswd(String password) {
        if (!StringUtils.isEmpty(password)) {
            try {
                password = encryptor.encrypt(password, "BocloudCMP");
            } catch (Exception e) {
                logger.error("Get error message：{}", e.getMessage());
            }
        }
        this.password = password;
    }
    /**
     * @return the password
     */
    @JSONField(serialize = false)
    public String getPswd() {
        if (!StringUtils.isEmpty(password)) {
            try {
                return encryptor.decrypt(password, "BocloudCMP");
            } catch (Exception e) {
                logger.error("Get error message：{}", e.getMessage());
            }
        }
        return password;
    }
}
