package com.keyware.shandan.datasource.entity;

import com.baomidou.mybatisplus.annotation.OrderBy;
import com.baomidou.mybatisplus.annotation.TableField;
import com.baomidou.mybatisplus.annotation.TableId;
import com.baomidou.mybatisplus.annotation.TableName;
import com.keyware.shandan.common.entity.BaseEntity;
import com.keyware.shandan.common.util.StringUtils;
import com.keyware.shandan.datasource.enums.ConnType;
import com.keyware.shandan.datasource.utils.DriverJarLoader;
import lombok.Data;
import lombok.EqualsAndHashCode;
import org.apache.ibatis.type.EnumTypeHandler;
import org.springframework.util.DigestUtils;

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

/**
 * 驱动信息实体类
 *
 * @author Administrator
 * @since 2021/7/28
 */
@Data
@EqualsAndHashCode(callSuper = true)
@TableName("B_DRIVERS")
public class DBDriverVo extends BaseEntity implements Serializable {
    private static final long serialVersionUID = 294620321088456773L;

    /**
     * 主键
     */
    @TableId("ID")
    private String id;
    /**
     * 驱动名称
     */
    @TableField("NAME")
    private String name;
    /**
     * 驱动存储路径
     */
    @TableField("PATH")
    private String path;
    /**
     * 驱动类名
     */
    @TableField("DRIVER_CLASS")
    private String driverClass;
    /**
     * 驱动版本
     */
    @TableField("VERSION")
    private String version;
    /**
     * 文件的MD5值
     */
    @TableField("MD5")
    private String md5;
    /**
     * 连接方式
     */
    @TableField(value = "CONN_TYPE")
    private String connType;
    /**
     * 是否内置驱动
     */
    @TableField("INNER")
    private Boolean inner;

    public DBDriverVo() {
    }

    public DBDriverVo(File file) throws IOException {
        this.connType = ConnType.JDBC.name();
        this.name = file.getName();
        this.path = file.getName();
        this.version = DriverJarLoader.jarVersion(file.getAbsolutePath());
        FileInputStream fis = new FileInputStream(file);
        this.md5 = DigestUtils.md5DigestAsHex(fis);
        this.id = this.md5;
        this.inner = false;
        fis.close();
    }

    public boolean isInnerDriver(){
        return this.inner;
    }
    public ConnType getConnTypeEnum(){
        if (StringUtils.hasText(this.connType)) {
            return ConnType.valueOf(this.connType);
        }
        return null;
    }
}
