| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228 |
- package jnpf.base.service.impl;
- import jnpf.base.service.SuperServiceImpl;
- import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
- import com.baomidou.mybatisplus.core.metadata.IPage;
- import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
- import jnpf.base.model.dblink.PaginationDbLink;
- import jnpf.constant.MsgCode;
- import jnpf.database.model.dto.PrepSqlDTO;
- import jnpf.database.model.entity.DbLinkEntity;
- import jnpf.base.mapper.DbLinkMapper;
- import jnpf.base.service.DbLinkService;
- import jnpf.database.source.DbBase;
- import jnpf.database.util.*;
- import jnpf.exception.DataException;
- import jnpf.util.RandomUtil;
- import jnpf.util.StringUtil;
- import jnpf.util.TenantHolder;
- import lombok.Cleanup;
- import org.apache.commons.lang3.math.NumberUtils;
- import org.springframework.beans.BeanUtils;
- import org.springframework.beans.factory.InitializingBean;
- import org.springframework.beans.factory.annotation.Autowired;
- import org.springframework.stereotype.Service;
- import com.baomidou.dynamic.datasource.annotation.DSTransactional;
- import java.sql.Connection;
- import java.util.Date;
- import java.util.List;
- /**
- * 数据连接
- *
- * @author JNPF开发平台组
- * @version V3.1.0
- * @copyright 引迈信息技术有限公司
- * @date 2019年9月27日 上午9:18
- */
- @Service
- public class DbLinkServiceImpl extends SuperServiceImpl<DbLinkMapper, DbLinkEntity> implements DbLinkService, InitializingBean {
- @Autowired
- private DbLinkService dblinkService;
- @Autowired
- private DataSourceUtil dataSourceUtils;
- @Override
- public List<DbLinkEntity> getList() {
- QueryWrapper<DbLinkEntity> queryWrapper = new QueryWrapper<>();
- queryWrapper.lambda().orderByAsc(DbLinkEntity::getSortCode)
- .orderByDesc(DbLinkEntity::getCreatorTime);
- return this.list(queryWrapper);
- }
- @Override
- public List<DbLinkEntity> getList(PaginationDbLink pagination) {
- // 定义变量判断是否需要使用修改时间倒序
- boolean flag = false;
- QueryWrapper<DbLinkEntity> queryWrapper = new QueryWrapper<>();
- if (StringUtil.isNotEmpty(pagination.getKeyword())) {
- flag = true;
- queryWrapper.lambda().and(
- t -> t.like(DbLinkEntity::getFullName, pagination.getKeyword())
- .or().like(DbLinkEntity::getHost, pagination.getKeyword())
- );
- }
- if (StringUtil.isNotEmpty(pagination.getDbType())) {
- flag = true;
- queryWrapper.lambda().eq(DbLinkEntity::getDbType, pagination.getDbType());
- }
- queryWrapper.lambda().orderByAsc(DbLinkEntity::getSortCode)
- .orderByDesc(DbLinkEntity::getCreatorTime);
- if (flag) {
- queryWrapper.lambda().orderByDesc(DbLinkEntity::getLastModifyTime);
- }
- Page<DbLinkEntity> page = new Page<>(pagination.getCurrentPage(), pagination.getPageSize());
- IPage<DbLinkEntity> iPage = this.page(page, queryWrapper);
- return pagination.setData(iPage.getRecords(), page.getTotal());
- }
- @Override
- public DbLinkEntity getInfo(String id) {
- QueryWrapper<DbLinkEntity> queryWrapper = new QueryWrapper<>();
- queryWrapper.lambda().eq(DbLinkEntity::getId, id);
- return this.getOne(queryWrapper);
- }
- @Override
- public boolean isExistByFullName(String fullName, String id) {
- QueryWrapper<DbLinkEntity> queryWrapper = new QueryWrapper<>();
- queryWrapper.lambda().eq(DbLinkEntity::getFullName, fullName);
- if (!StringUtil.isEmpty(id)) {
- queryWrapper.lambda().ne(DbLinkEntity::getId, id);
- }
- return this.count(queryWrapper) > 0;
- }
- @Override
- public void create(DbLinkEntity entity) {
- entity.setId(RandomUtil.uuId());
- this.save(entity);
- }
- @Override
- public boolean update(String id, DbLinkEntity entity) {
- entity.setId(id);
- return this.updateById(entity);
- }
- @Override
- public void delete(DbLinkEntity entity) {
- this.removeById(entity.getId());
- }
- @Override
- @DSTransactional
- public boolean first(String id) {
- boolean isOk = false;
- //获取要上移的那条数据的信息
- DbLinkEntity upEntity = this.getById(id);
- Long upSortCode = upEntity.getSortCode() == null ? 0 : upEntity.getSortCode();
- //查询上几条记录
- QueryWrapper<DbLinkEntity> queryWrapper = new QueryWrapper<>();
- queryWrapper.lambda()
- .lt(DbLinkEntity::getSortCode, upSortCode)
- .orderByDesc(DbLinkEntity::getSortCode);
- List<DbLinkEntity> downEntity = this.list(queryWrapper);
- if (downEntity.size() > 0) {
- //交换两条记录的sort值
- Long temp = upEntity.getSortCode();
- upEntity.setSortCode(downEntity.get(0).getSortCode());
- downEntity.get(0).setSortCode(temp);
- this.updateById(downEntity.get(0));
- this.updateById(upEntity);
- isOk = true;
- }
- return isOk;
- }
- @Override
- @DSTransactional
- public boolean next(String id) {
- boolean isOk = false;
- //获取要下移的那条数据的信息
- DbLinkEntity downEntity = this.getById(id);
- Long upSortCode = downEntity.getSortCode() == null ? 0 : downEntity.getSortCode();
- //查询下几条记录
- QueryWrapper<DbLinkEntity> queryWrapper = new QueryWrapper<>();
- queryWrapper.lambda()
- .gt(DbLinkEntity::getSortCode, upSortCode)
- .orderByAsc(DbLinkEntity::getSortCode);
- List<DbLinkEntity> upEntity = this.list(queryWrapper);
- if (upEntity.size() > 0) {
- //交换两条记录的sort值
- Long temp = downEntity.getSortCode();
- downEntity.setSortCode(upEntity.get(0).getSortCode());
- downEntity.setLastModifyTime(new Date());
- upEntity.get(0).setSortCode(temp);
- this.updateById(upEntity.get(0));
- this.updateById(downEntity);
- isOk = true;
- }
- return isOk;
- }
- @Override
- public boolean testDbConnection(DbLinkEntity entity) {
- //判断字典数据类型编码是否错误,大小写不敏感
- DbBase db = DbTypeUtil.getDb(entity);
- if(db == null){
- throw new DataException(MsgCode.DB001.get());
- }
- try{
- @Cleanup Connection conn = ConnUtil.getConn(entity.getUserName(), entity.getPassword(), ConnUtil.getUrl(entity));
- return conn != null;
- }catch (Exception e){
- e.printStackTrace();
- }
- return false;
- }
- /**
- * 设置数据源
- * @param dbLinkId 数据连接id
- * @throws DataException ignore
- */
- @Override
- public DbLinkEntity getResource(String dbLinkId) throws Exception {
- DbLinkEntity dbLinkEntity = new DbLinkEntity();
- //多租户是否开启
- if("0".equals(dbLinkId)){
- if(TenantDataSourceUtil.isTenantAssignDataSource()){
- // 默认数据库, 租户管理指定租户数据源
- dbLinkEntity = TenantDataSourceUtil.getTenantAssignDataSource(TenantHolder.getDatasourceId()).toDbLink(new DbLinkEntity());
- dbLinkEntity.setId("0");
- }else {
- // 默认数据库查询,从配置获取数据源信息
- BeanUtils.copyProperties(dataSourceUtils, dbLinkEntity);
- dbLinkEntity.setId("0");
- // 是系统默认的多租户
- TenantDataSourceUtil.initDataSourceTenantDbName(dbLinkEntity);
- }
- }else {
- try {
- DynamicDataSourceUtil.switchToDataSource(null);
- dbLinkEntity = dblinkService.getInfo(dbLinkId);
- }finally {
- DynamicDataSourceUtil.clearSwitchDataSource();
- }
- }
- // 添加并且切换数据源
- return dbLinkEntity;
- }
- @Override
- public void afterPropertiesSet(){
- PrepSqlDTO.DB_LINK_FUN = (dbLinkId)-> {
- try {
- return (DbLinkEntity) getResource(dbLinkId);
- } catch (Exception e) {
- e.printStackTrace();
- }
- return null;
- };
- }
- }
|