DbLinkServiceImpl.java 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228
  1. package jnpf.base.service.impl;
  2. import jnpf.base.service.SuperServiceImpl;
  3. import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
  4. import com.baomidou.mybatisplus.core.metadata.IPage;
  5. import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
  6. import jnpf.base.model.dblink.PaginationDbLink;
  7. import jnpf.constant.MsgCode;
  8. import jnpf.database.model.dto.PrepSqlDTO;
  9. import jnpf.database.model.entity.DbLinkEntity;
  10. import jnpf.base.mapper.DbLinkMapper;
  11. import jnpf.base.service.DbLinkService;
  12. import jnpf.database.source.DbBase;
  13. import jnpf.database.util.*;
  14. import jnpf.exception.DataException;
  15. import jnpf.util.RandomUtil;
  16. import jnpf.util.StringUtil;
  17. import jnpf.util.TenantHolder;
  18. import lombok.Cleanup;
  19. import org.apache.commons.lang3.math.NumberUtils;
  20. import org.springframework.beans.BeanUtils;
  21. import org.springframework.beans.factory.InitializingBean;
  22. import org.springframework.beans.factory.annotation.Autowired;
  23. import org.springframework.stereotype.Service;
  24. import com.baomidou.dynamic.datasource.annotation.DSTransactional;
  25. import java.sql.Connection;
  26. import java.util.Date;
  27. import java.util.List;
  28. /**
  29. * 数据连接
  30. *
  31. * @author JNPF开发平台组
  32. * @version V3.1.0
  33. * @copyright 引迈信息技术有限公司
  34. * @date 2019年9月27日 上午9:18
  35. */
  36. @Service
  37. public class DbLinkServiceImpl extends SuperServiceImpl<DbLinkMapper, DbLinkEntity> implements DbLinkService, InitializingBean {
  38. @Autowired
  39. private DbLinkService dblinkService;
  40. @Autowired
  41. private DataSourceUtil dataSourceUtils;
  42. @Override
  43. public List<DbLinkEntity> getList() {
  44. QueryWrapper<DbLinkEntity> queryWrapper = new QueryWrapper<>();
  45. queryWrapper.lambda().orderByAsc(DbLinkEntity::getSortCode)
  46. .orderByDesc(DbLinkEntity::getCreatorTime);
  47. return this.list(queryWrapper);
  48. }
  49. @Override
  50. public List<DbLinkEntity> getList(PaginationDbLink pagination) {
  51. // 定义变量判断是否需要使用修改时间倒序
  52. boolean flag = false;
  53. QueryWrapper<DbLinkEntity> queryWrapper = new QueryWrapper<>();
  54. if (StringUtil.isNotEmpty(pagination.getKeyword())) {
  55. flag = true;
  56. queryWrapper.lambda().and(
  57. t -> t.like(DbLinkEntity::getFullName, pagination.getKeyword())
  58. .or().like(DbLinkEntity::getHost, pagination.getKeyword())
  59. );
  60. }
  61. if (StringUtil.isNotEmpty(pagination.getDbType())) {
  62. flag = true;
  63. queryWrapper.lambda().eq(DbLinkEntity::getDbType, pagination.getDbType());
  64. }
  65. queryWrapper.lambda().orderByAsc(DbLinkEntity::getSortCode)
  66. .orderByDesc(DbLinkEntity::getCreatorTime);
  67. if (flag) {
  68. queryWrapper.lambda().orderByDesc(DbLinkEntity::getLastModifyTime);
  69. }
  70. Page<DbLinkEntity> page = new Page<>(pagination.getCurrentPage(), pagination.getPageSize());
  71. IPage<DbLinkEntity> iPage = this.page(page, queryWrapper);
  72. return pagination.setData(iPage.getRecords(), page.getTotal());
  73. }
  74. @Override
  75. public DbLinkEntity getInfo(String id) {
  76. QueryWrapper<DbLinkEntity> queryWrapper = new QueryWrapper<>();
  77. queryWrapper.lambda().eq(DbLinkEntity::getId, id);
  78. return this.getOne(queryWrapper);
  79. }
  80. @Override
  81. public boolean isExistByFullName(String fullName, String id) {
  82. QueryWrapper<DbLinkEntity> queryWrapper = new QueryWrapper<>();
  83. queryWrapper.lambda().eq(DbLinkEntity::getFullName, fullName);
  84. if (!StringUtil.isEmpty(id)) {
  85. queryWrapper.lambda().ne(DbLinkEntity::getId, id);
  86. }
  87. return this.count(queryWrapper) > 0;
  88. }
  89. @Override
  90. public void create(DbLinkEntity entity) {
  91. entity.setId(RandomUtil.uuId());
  92. this.save(entity);
  93. }
  94. @Override
  95. public boolean update(String id, DbLinkEntity entity) {
  96. entity.setId(id);
  97. return this.updateById(entity);
  98. }
  99. @Override
  100. public void delete(DbLinkEntity entity) {
  101. this.removeById(entity.getId());
  102. }
  103. @Override
  104. @DSTransactional
  105. public boolean first(String id) {
  106. boolean isOk = false;
  107. //获取要上移的那条数据的信息
  108. DbLinkEntity upEntity = this.getById(id);
  109. Long upSortCode = upEntity.getSortCode() == null ? 0 : upEntity.getSortCode();
  110. //查询上几条记录
  111. QueryWrapper<DbLinkEntity> queryWrapper = new QueryWrapper<>();
  112. queryWrapper.lambda()
  113. .lt(DbLinkEntity::getSortCode, upSortCode)
  114. .orderByDesc(DbLinkEntity::getSortCode);
  115. List<DbLinkEntity> downEntity = this.list(queryWrapper);
  116. if (downEntity.size() > 0) {
  117. //交换两条记录的sort值
  118. Long temp = upEntity.getSortCode();
  119. upEntity.setSortCode(downEntity.get(0).getSortCode());
  120. downEntity.get(0).setSortCode(temp);
  121. this.updateById(downEntity.get(0));
  122. this.updateById(upEntity);
  123. isOk = true;
  124. }
  125. return isOk;
  126. }
  127. @Override
  128. @DSTransactional
  129. public boolean next(String id) {
  130. boolean isOk = false;
  131. //获取要下移的那条数据的信息
  132. DbLinkEntity downEntity = this.getById(id);
  133. Long upSortCode = downEntity.getSortCode() == null ? 0 : downEntity.getSortCode();
  134. //查询下几条记录
  135. QueryWrapper<DbLinkEntity> queryWrapper = new QueryWrapper<>();
  136. queryWrapper.lambda()
  137. .gt(DbLinkEntity::getSortCode, upSortCode)
  138. .orderByAsc(DbLinkEntity::getSortCode);
  139. List<DbLinkEntity> upEntity = this.list(queryWrapper);
  140. if (upEntity.size() > 0) {
  141. //交换两条记录的sort值
  142. Long temp = downEntity.getSortCode();
  143. downEntity.setSortCode(upEntity.get(0).getSortCode());
  144. downEntity.setLastModifyTime(new Date());
  145. upEntity.get(0).setSortCode(temp);
  146. this.updateById(upEntity.get(0));
  147. this.updateById(downEntity);
  148. isOk = true;
  149. }
  150. return isOk;
  151. }
  152. @Override
  153. public boolean testDbConnection(DbLinkEntity entity) {
  154. //判断字典数据类型编码是否错误,大小写不敏感
  155. DbBase db = DbTypeUtil.getDb(entity);
  156. if(db == null){
  157. throw new DataException(MsgCode.DB001.get());
  158. }
  159. try{
  160. @Cleanup Connection conn = ConnUtil.getConn(entity.getUserName(), entity.getPassword(), ConnUtil.getUrl(entity));
  161. return conn != null;
  162. }catch (Exception e){
  163. e.printStackTrace();
  164. }
  165. return false;
  166. }
  167. /**
  168. * 设置数据源
  169. * @param dbLinkId 数据连接id
  170. * @throws DataException ignore
  171. */
  172. @Override
  173. public DbLinkEntity getResource(String dbLinkId) throws Exception {
  174. DbLinkEntity dbLinkEntity = new DbLinkEntity();
  175. //多租户是否开启
  176. if("0".equals(dbLinkId)){
  177. if(TenantDataSourceUtil.isTenantAssignDataSource()){
  178. // 默认数据库, 租户管理指定租户数据源
  179. dbLinkEntity = TenantDataSourceUtil.getTenantAssignDataSource(TenantHolder.getDatasourceId()).toDbLink(new DbLinkEntity());
  180. dbLinkEntity.setId("0");
  181. }else {
  182. // 默认数据库查询,从配置获取数据源信息
  183. BeanUtils.copyProperties(dataSourceUtils, dbLinkEntity);
  184. dbLinkEntity.setId("0");
  185. // 是系统默认的多租户
  186. TenantDataSourceUtil.initDataSourceTenantDbName(dbLinkEntity);
  187. }
  188. }else {
  189. try {
  190. DynamicDataSourceUtil.switchToDataSource(null);
  191. dbLinkEntity = dblinkService.getInfo(dbLinkId);
  192. }finally {
  193. DynamicDataSourceUtil.clearSwitchDataSource();
  194. }
  195. }
  196. // 添加并且切换数据源
  197. return dbLinkEntity;
  198. }
  199. @Override
  200. public void afterPropertiesSet(){
  201. PrepSqlDTO.DB_LINK_FUN = (dbLinkId)-> {
  202. try {
  203. return (DbLinkEntity) getResource(dbLinkId);
  204. } catch (Exception e) {
  205. e.printStackTrace();
  206. }
  207. return null;
  208. };
  209. }
  210. }