许鹏程
2023-06-29 e7ccc6e4243d7d7bfb4a950367b2cdc26428915f
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
package com.product.datasource.entity;
 
import com.product.core.dao.BaseDao;
import com.product.core.entity.FieldSetEntity;
import com.product.core.exception.BaseException;
import com.product.core.spring.context.SpringBeanUtil;
import com.product.data.entity.DatabaseEntity;
import com.product.datasource.config.DataBaseType;
import com.product.datasource.dao.Dao;
import com.product.datasource.service.RedisService;
 
import java.lang.reflect.Method;
 
/**
 * @Author cheng
 * @Date 2022/7/7 9:07
 * @Desc
 */
public class DataBaseEntity extends DatabaseEntity {
 
 
    private Dao dao;
 
    private RedisService redisService;
 
    private String uuid;
 
    private String connectionName;
 
    public String getUuid() {
        return uuid;
    }
 
    public RedisService getRedisService() {
        if (redisService == null) {
            redisService = new RedisService(this);
        }
        return redisService;
    }
 
 
    public DataBaseEntity(int dbType) {
        super(dbType);
    }
 
    public DataBaseEntity(FieldSetEntity fse) {
        super(fse);
        init(fse);
    }
 
    protected void init(FieldSetEntity fse) {
        super.init(fse);
        this.uuid = fse.getUUID();
        this.connectionName = fse.getString("sync_name");
    }
 
 
    public DataBaseEntity(String connectionConfigUid) throws BaseException {
        this(-1);
        Object bean = SpringBeanUtil.getBean("publicService");
        try {
            Method getBaseDao = bean.getClass().getMethod("getBaseDao");
            BaseDao baseDao = (BaseDao) getBaseDao.invoke(bean);
            FieldSetEntity fse = baseDao.getFieldSetEntity("product_sys_data_sync_manager", connectionConfigUid, false);
            this.init(fse);
        } catch (Exception e) {
            e.printStackTrace();
            throw new BaseException(e);
        }
    }
 
    private DataBaseType dbTypeTransition() {
        return DataBaseType.getDataBaseType(getDbTypeByInt());
    }
 
//    @Override
//    @Deprecated
//    public DatabaseType getDbType() {
//        throw new RuntimeException("不能直接获取 “DatabaseType“  ,请使用  “DataBaseEntity.getDataBaseType()” 方法");
//    }
 
    public DataBaseType getDataBaseType() {
        return dbTypeTransition();
    }
 
    /**
     * 获取一个新的Dao
     *
     * @return
     * @throws BaseException
     */
    public Dao newDao() throws BaseException {
        DataBaseType dataBaseType = this.getDataBaseType();
        if (dataBaseType == null) {
            return null;
        }
        return dataBaseType.getDao(this);
    }
 
    /**
     * 获取Dao,重复调用时将获取之前的Dao 对象,直至清空
     *
     * @return
     * @throws BaseException
     */
    public Dao getDao() throws BaseException {
        if (this.dao == null) {
            DataBaseType dataBaseType = this.getDataBaseType();
            if (dataBaseType == null) {
                return null;
            }
            this.dao = dataBaseType.getDao(this);
        }
        return this.dao;
    }
 
    public void setDao(Dao dao) {
        this.dao = dao;
    }
 
    public String getConnectionName() {
        return connectionName;
    }
}