| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155 |
- package com.template.services.impl;
- import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
- import com.baomidou.mybatisplus.core.metadata.IPage;
- import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
- import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
- import com.template.common.utils.CommonUtil;
- import com.template.mapper.SmartDataClassMapper;
- import com.template.mapper.SmartDataSourceMapper;
- import com.template.model.pojo.SmartDataClass;
- import com.template.model.pojo.SmartDataSource;
- import com.template.model.result.PageUtils;
- import com.template.services.SmartDataSourceService;
- import org.springframework.beans.factory.annotation.Autowired;
- import org.springframework.stereotype.Service;
- import org.springframework.util.StringUtils;
- import java.util.List;
- import java.util.Map;
- /**
- * <p>
- * 数据源配置 服务实现类
- * </p>
- *
- * @author ceshi
- * @since 2023-12-05
- */
- @Service
- public class SmartDataSourceServiceImpl extends ServiceImpl<SmartDataSourceMapper, SmartDataSource> implements SmartDataSourceService {
- @Autowired
- private SmartDataSourceMapper smartDataSourceMapper;
- @Autowired
- private SmartDataClassMapper smartDataClassMapper;
- @Override
- public Map<String, String> insertSmartDataSource(SmartDataSource smartDataSource) {
- // 检测参数,还有是否存在重复记录
- Map<String, String> stringStringMap = validateParams(smartDataSource, "insert");
- if (stringStringMap != null) {
- return stringStringMap;
- }
- // 必填的参数不为null,则进行新增操作
- int result = smartDataSourceMapper.insert(smartDataSource);
- if (result > 0) {
- return CommonUtil.getReturnMap(String.valueOf(result), "数据源添加成功!");
- } else {
- return CommonUtil.getReturnMap(String.valueOf(result), "数据源添加失败!");
- }
- }
- // 检测验证参数,和数据库中是否存在重复记录
- private Map<String, String> validateParams(SmartDataSource smartDataSource, String action) {
- // 看是否有相同的记录
- QueryWrapper<SmartDataSource> queryWrapper = new QueryWrapper<>();
- if ("update".equals(action)) {
- queryWrapper.like(smartDataSource.getDsId() != null, "ds_id", smartDataSource.getDsId());
- }
- queryWrapper.like(smartDataSource.getDsClsId() != null, "ds_cls_id", smartDataSource.getDsClsId());
- queryWrapper.like(StringUtils.hasText(smartDataSource.getDsName()), "ds_name", smartDataSource.getDsName());
- queryWrapper.like(StringUtils.hasText(smartDataSource.getDsUrl()), "ds_url", smartDataSource.getDsUrl());
- queryWrapper.like(StringUtils.hasText(smartDataSource.getDsUser()), "ds_user", smartDataSource.getDsUser());
- queryWrapper.like(StringUtils.hasText(smartDataSource.getDsPassword()), "ds_password", smartDataSource.getDsPassword());
- queryWrapper.like(smartDataSource.getDsStatus() != null, "ds_status", smartDataSource.getDsStatus());
- queryWrapper.like(StringUtils.hasText(smartDataSource.getDsDescrition()), "ds_descrition", smartDataSource.getDsDescrition());
- List<SmartDataSource> smartDataSources = smartDataSourceMapper.selectList(queryWrapper);
- if (smartDataSources.size() > 0) {
- if ("update".equals(action)) {
- return CommonUtil.getReturnMap("0", "数据未修改,请修改后再提交!");
- } else {
- return CommonUtil.getReturnMap("0", "有重复记录!");
- }
- }
- // 检测必要参数是否为null
- if ("update".equals(action)) {
- if (smartDataSource.getDsId() == null) {
- return CommonUtil.getReturnMap("0", "【数据源id】不能为空!");
- }
- SmartDataSource sdc = smartDataSourceMapper.selectById(smartDataSource.getDsId());
- if (sdc == null) {
- return CommonUtil.getReturnMap("0", "要修改的【数据源】不存在!");
- }
- }
- // 检测必要参数是否为null
- if (smartDataSource.getDsClsId() == null) {
- return CommonUtil.getReturnMap("0", "【数据源类型id】不能为空!");
- }
- SmartDataClass smartDataClass = smartDataClassMapper.selectById(smartDataSource.getDsClsId());
- if (smartDataClass == null) {
- return CommonUtil.getReturnMap("0", "选择的【数据源类型】不存在!");
- }
- if (smartDataSource.getDsName() == null) {
- return CommonUtil.getReturnMap("0", "【数据源名称】不能为空!");
- }
- if (smartDataSource.getDsUrl() == null) {
- return CommonUtil.getReturnMap("0", "【数据源连接地址】不能为空!");
- }
- if (smartDataSource.getDsUser() == null) {
- return CommonUtil.getReturnMap("0", "【数据源用户】不能为空!");
- }
- if (smartDataSource.getDsPassword() == null) {
- return CommonUtil.getReturnMap("0", "【数据源密码】不能为空!");
- }
- if (smartDataSource.getDsDescrition() == null) {
- return CommonUtil.getReturnMap("0", "【数据源描述】不能为空!");
- }
- return null;
- }
- @Override
- public Map<String, String> updateSmartDataSource(SmartDataSource smartDataSource) {
- // 检测参数,还有是否存在重复记录
- Map<String, String> stringStringMap = validateParams(smartDataSource, "insert");
- if (stringStringMap != null) {
- return stringStringMap;
- }
- int result = smartDataSourceMapper.updateById(smartDataSource);
- if (result > 0) {
- return CommonUtil.getReturnMap(String.valueOf(result), "数据源修改成功!");
- } else {
- return CommonUtil.getReturnMap(String.valueOf(result), "数据源修改失败!");
- }
- }
- @Override
- public PageUtils<SmartDataSource> queryPageSmartDataSources(int currentPage, int pageCount, SmartDataSource smartDataSource) {
- Page<SmartDataSource> page = new Page<>(currentPage, pageCount);
- QueryWrapper<SmartDataSource> queryWrapper = new QueryWrapper<>();
- queryWrapper.like(StringUtils.hasText(smartDataSource.getDsName()), "ds_name", smartDataSource.getDsName());
- queryWrapper.like(StringUtils.hasText(smartDataSource.getDsUrl()), "ds_url", smartDataSource.getDsUrl());
- queryWrapper.like(smartDataSource.getDsStatus() != null, "ds_status", smartDataSource.getDsStatus());
- queryWrapper.like(StringUtils.hasText(smartDataSource.getDsDescrition()), "ds_descrition", smartDataSource.getDsDescrition());
- queryWrapper.orderByDesc("ds_update_time");
- IPage<SmartDataSource> result = smartDataSourceMapper.selectPage(page, queryWrapper);
- return new PageUtils<>(result);
- }
- @Override
- public int deleteSmartDataSourceById(int id) {
- int result = smartDataSourceMapper.deleteById(id);
- return result;
- }
- @Override
- public SmartDataSource getSmartById(int id) {
- SmartDataSource result = smartDataSourceMapper.selectById(id);
- return result;
- }
- }
|