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; } }