|
@@ -0,0 +1,173 @@
|
|
|
+package com.gis.db.service.impl;
|
|
|
+
|
|
|
+import cn.hutool.core.lang.Validator;
|
|
|
+import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
|
|
|
+import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
|
|
|
+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.gis.common.base.entity.dto.PageDto;
|
|
|
+import com.gis.common.base.exception.BaseRuntimeException;
|
|
|
+import com.gis.common.constant.ErrorEnum;
|
|
|
+import com.gis.common.util.BaseUtil;
|
|
|
+import com.gis.common.util.Result;
|
|
|
+import com.gis.db.entity.dto.FieldDto;
|
|
|
+import com.gis.db.entity.po.FieldEntity;
|
|
|
+import com.gis.db.entity.po.TableEntity;
|
|
|
+import com.gis.db.mapper.DdlMapper;
|
|
|
+import com.gis.db.mapper.FieldMapper;
|
|
|
+import com.gis.db.service.DdlService;
|
|
|
+import com.gis.db.service.FieldService;
|
|
|
+import com.gis.db.service.TableService;
|
|
|
+import lombok.extern.slf4j.Slf4j;
|
|
|
+import org.springframework.beans.BeanUtils;
|
|
|
+import org.springframework.beans.factory.annotation.Autowired;
|
|
|
+import org.springframework.stereotype.Service;
|
|
|
+
|
|
|
+import java.util.*;
|
|
|
+
|
|
|
+/**
|
|
|
+ * Created by owen on 2022/3/10 0010 17:29
|
|
|
+ */
|
|
|
+@Slf4j
|
|
|
+@Service
|
|
|
+public class FieldServiceImpl extends ServiceImpl<FieldMapper, FieldEntity> implements FieldService {
|
|
|
+
|
|
|
+ @Autowired
|
|
|
+ DdlService ddlService;
|
|
|
+
|
|
|
+ @Autowired
|
|
|
+ TableService tableService;
|
|
|
+
|
|
|
+ @Autowired
|
|
|
+ DdlMapper ddlMapper;
|
|
|
+
|
|
|
+ @Override
|
|
|
+ public Result addField(List<FieldDto> param) {
|
|
|
+
|
|
|
+ FieldEntity entity ;
|
|
|
+ Long tableId = null;
|
|
|
+ ArrayList<FieldEntity> list = new ArrayList<>();
|
|
|
+ for (FieldDto dto : param) {
|
|
|
+ BaseRuntimeException.isHas(isExistByTableIdAndName(dto.getTableId(), dto.getName()), null, "字段重复添加");
|
|
|
+ entity = new FieldEntity();
|
|
|
+ BeanUtils.copyProperties(dto, entity);
|
|
|
+ list.add(entity);
|
|
|
+ tableId = dto.getTableId();
|
|
|
+ }
|
|
|
+ TableEntity tableEntity = tableService.getById(tableId);
|
|
|
+ BaseRuntimeException.isNull(tableEntity, ErrorEnum.FAILURE_CODE_3001);
|
|
|
+
|
|
|
+ this.saveBatch(list);
|
|
|
+
|
|
|
+ // 添加字段到表
|
|
|
+ ddlService.addField(list, tableEntity.getName());
|
|
|
+
|
|
|
+
|
|
|
+ return Result.success();
|
|
|
+ }
|
|
|
+
|
|
|
+ @Override
|
|
|
+ public Result testSql() {
|
|
|
+ //alter table ts_aa add age varchar(255);
|
|
|
+ String sql = "alter table ts_aa add age_1 varchar(255)";
|
|
|
+// ddlMapper.updateSql(sql);
|
|
|
+ return Result.success();
|
|
|
+ }
|
|
|
+
|
|
|
+ @Override
|
|
|
+ public Result getFieldData(Long tableId, PageDto param) {
|
|
|
+ BaseUtil.startPage(param);
|
|
|
+ IPage<Map> page = new Page<>(param.getPageNum() , param.getPageSize());
|
|
|
+// List<String> strs = Arrays.asList("id", "age_2", "age_1");
|
|
|
+// String tableName = "ts_aa";
|
|
|
+ TableEntity tableEntity = tableService.getById(tableId);
|
|
|
+ BaseRuntimeException.isNull(tableEntity, ErrorEnum.FAILURE_CODE_3001);
|
|
|
+ IPage<Map> resPage = getBaseMapper().page(tableEntity.getName(), getFileNames(tableId), page);
|
|
|
+
|
|
|
+ Map resultMap = new HashMap();
|
|
|
+ resultMap.put("fieldNames", getResultByTableId(tableId));
|
|
|
+ resultMap.put("fieldData", resPage);
|
|
|
+
|
|
|
+ return Result.success(resultMap);
|
|
|
+ }
|
|
|
+
|
|
|
+ @Override
|
|
|
+ public Result detail(Long tableId, Long id) {
|
|
|
+ TableEntity tableEntity = tableService.getById(tableId);
|
|
|
+ BaseRuntimeException.isNull(tableEntity, ErrorEnum.FAILURE_CODE_3001);
|
|
|
+
|
|
|
+ Map dataMap = getBaseMapper().detail(tableEntity.getName(), getFileNames(tableId), id);
|
|
|
+
|
|
|
+ return Result.success(dataMap);
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 根据表id批量删除字段(逻辑删除)
|
|
|
+ * @param tableIds
|
|
|
+ */
|
|
|
+ @Override
|
|
|
+ public void removeBatchByTableId(List<Integer> tableIds) {
|
|
|
+ for (Integer tableId : tableIds) {
|
|
|
+ LambdaUpdateWrapper<FieldEntity> updateWrapper = new LambdaUpdateWrapper<>();
|
|
|
+ updateWrapper.eq(FieldEntity::getTableId, tableId);
|
|
|
+ updateWrapper.set(FieldEntity::getIsDelete, 1);
|
|
|
+ this.update(updateWrapper);
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 判断该表是否已添加过字段
|
|
|
+ * @return
|
|
|
+ */
|
|
|
+ private Boolean isExistByTableIdAndName(Long tableId, String filedName){
|
|
|
+ LambdaQueryWrapper<FieldEntity> wrapper = new LambdaQueryWrapper<>();
|
|
|
+ wrapper.eq(FieldEntity::getTableId, tableId);
|
|
|
+ wrapper.eq(FieldEntity::getName, filedName);
|
|
|
+ long count = this.count(wrapper);
|
|
|
+ return count != 0;
|
|
|
+ }
|
|
|
+
|
|
|
+ private List<FieldEntity> findByTableId(Long tableId){
|
|
|
+ LambdaQueryWrapper<FieldEntity> wrapper = new LambdaQueryWrapper<>();
|
|
|
+ wrapper.eq(FieldEntity::getTableId, tableId);
|
|
|
+ List<FieldEntity> list = this.list(wrapper);
|
|
|
+ BaseRuntimeException.isEmpty(list, ErrorEnum.FAILURE_SYS_2011);
|
|
|
+ return list;
|
|
|
+ }
|
|
|
+
|
|
|
+ // 获取字段名称
|
|
|
+ private List<String> getFileNames(Long tableId){
|
|
|
+ List<FieldEntity> list = findByTableId(tableId);
|
|
|
+ ArrayList<String> fileNames = new ArrayList<>();
|
|
|
+ for (FieldEntity entity : list) {
|
|
|
+ fileNames.add(entity.getName());
|
|
|
+ }
|
|
|
+ fileNames.add("id");
|
|
|
+ fileNames.add("create_time");
|
|
|
+ fileNames.add("update_time");
|
|
|
+ fileNames.add("creator_id");
|
|
|
+ return fileNames;
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+ private Map getResultByTableId(Long tableId){
|
|
|
+ List<FieldEntity> list = findByTableId(tableId);
|
|
|
+
|
|
|
+ Map filedMap = new HashMap();
|
|
|
+ for (FieldEntity entity : list) {
|
|
|
+ filedMap.put(entity.getName(), entity.getType());
|
|
|
+ }
|
|
|
+
|
|
|
+ // 添加默认字段: 使用数据库下划线命名
|
|
|
+ filedMap.put("id", "int");
|
|
|
+ filedMap.put("create_time", "date");
|
|
|
+ filedMap.put("update_time", "date");
|
|
|
+ filedMap.put("creator_id", "int");
|
|
|
+
|
|
|
+ return filedMap;
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+}
|