Administrator 4 年之前
父節點
當前提交
194f533b6f

+ 2 - 2
boman-modules/boman-gen/src/main/java/com/boman/gen/mapper/GenTableColumnMapper.java

@@ -32,7 +32,7 @@ public interface GenTableColumnMapper
      * @param columnId
      * @return
      */
-    public GenTableColumn selectGenTableColumnListByColumnId(Long columnId);
+    public GenTableColumn selectGenTableColumnByColumnId(Long columnId);
 
     /**
      * 查询业务字段列表
@@ -88,5 +88,5 @@ public interface GenTableColumnMapper
     * @author tjf
     * @Date 2021/3/25
     */
-    public GenTableColumn checkColumnNameUnique(String columnName);
+    public GenTableColumn checkColumnNameUnique(GenTableColumn genTableColumn);
 }

+ 4 - 0
boman-modules/boman-gen/src/main/java/com/boman/gen/mapper/TableSqlMapper.java

@@ -17,4 +17,8 @@ public interface TableSqlMapper {
 
 
     public TableSql selectTableSqlByTableId(Long id);
+
+    public int testExists(String tableName);
+
+    public int deleteTableSqlByTableIds(Long[] ids);
 }

+ 66 - 7
boman-modules/boman-gen/src/main/java/com/boman/gen/service/GenTableColumnServiceImpl.java

@@ -5,9 +5,14 @@ import java.util.List;
 import java.util.concurrent.locks.Condition;
 
 import com.boman.common.core.constant.UserConstants;
+import com.boman.common.core.utils.DateUtils;
+import com.boman.common.core.utils.SecurityUtils;
 import com.boman.common.core.utils.StringUtils;
+import com.boman.common.log.enums.BusinessType;
 import com.boman.gen.domain.GenTable;
+import com.boman.gen.domain.TableSql;
 import com.boman.gen.mapper.GenTableMapper;
+import com.boman.gen.mapper.TableSqlMapper;
 import org.springframework.beans.factory.annotation.Autowired;
 import org.springframework.stereotype.Service;
 import com.boman.common.core.text.Convert;
@@ -25,6 +30,8 @@ public class GenTableColumnServiceImpl implements IGenTableColumnService {
     private GenTableColumnMapper genTableColumnMapper;
     @Autowired
     private GenTableMapper genTableMapper;
+    @Autowired
+    private TableSqlMapper tableSqlMapper;
 
     /**
      * 查询业务字段列表
@@ -39,12 +46,13 @@ public class GenTableColumnServiceImpl implements IGenTableColumnService {
 
     /**
      * 根据gen_table_column的id查询字段对象
+     *
      * @param columnId
      * @return
      */
     @Override
     public GenTableColumn selectGenTableColumnListByColumnId(Long columnId) {
-        return   genTableColumnMapper.selectGenTableColumnListByColumnId(columnId);
+        return genTableColumnMapper.selectGenTableColumnByColumnId(columnId);
     }
 
     /**
@@ -70,8 +78,10 @@ public class GenTableColumnServiceImpl implements IGenTableColumnService {
      */
     @Override
     public int insertGenTableColumn(GenTableColumn genTableColumn) {
+        int i = genTableColumnMapper.insertGenTableColumn(genTableColumn);
         isAk(genTableColumn);
-        return genTableColumnMapper.insertGenTableColumn(genTableColumn);
+        insertCreateLog(genTableColumn, BusinessType.INSERT);
+        return i;
     }
 
     /**
@@ -83,11 +93,37 @@ public class GenTableColumnServiceImpl implements IGenTableColumnService {
     @Override
     public int updateGenTableColumn(GenTableColumn genTableColumn) {
         isAk(genTableColumn);
+        insertCreateLog(genTableColumn, BusinessType.UPDATE);
         return genTableColumnMapper.updateGenTableColumn(genTableColumn);
     }
 
+    /**
+     * 插入表sql日志
+     */
+    public void insertCreateLog(GenTableColumn genTableColumn, BusinessType type) {
+        GenTable genTable = genTableMapper.selectGenTableById(genTableColumn.getTableId());
+        //新增业务字段的同时新增一条建表sql修改日志
+        TableSql tableSql = tableSqlMapper.selectTableSqlByTableId(genTableColumn.getTableId());
+        String createLog = tableSql.getCreateLog();
+        StringBuffer sb = new StringBuffer(createLog);
+        //ALTER TABLE table_name ADD COLUMN column_name VARCHAR(100) DEFAULT NULL COMMENT '新加字段';
+        sb.append("\r\n").append(DateUtils.getTime()).append(" ").append(SecurityUtils.getUsername()).append(" ALTER TABLE ").append(genTable.getTableName());
+        if (BusinessType.INSERT.equals(type)){
+            sb.append(" ADD COLUMN ").append(genTableColumn.getColumnName()).append(" ").append(genTableColumn.getColumnType());
+            sb = genTableColumn.getDefaultValue() == null ? sb.append(" DEFAULT NULL COMMENT ") : sb.append(" DEFAULT '").append(genTableColumn.getDefaultValue()).append("' COMMENT '");
+            sb.append(genTableColumn.getColumnComment()).append("';");
+        }else if(BusinessType.UPDATE.equals(type)){
+            sb.append(" MODIFY ").append(genTableColumn.getColumnName()).append(" ").append(genTableColumn.getColumnType()).append(";");
+        }else if(BusinessType.DELETE.equals(type)){
+            sb.append(" DROP ").append(genTableColumn.getColumnName()).append(";");
+        }
+        tableSql.setCreateLog(sb.toString());
+        tableSqlMapper.updateTableSql(tableSql);
+    }
+
     /**
      * 判断对象是否设置显示和输入字段
+     *
      * @param genTableColumn
      */
     public void isAk(GenTableColumn genTableColumn) {
@@ -95,16 +131,33 @@ public class GenTableColumnServiceImpl implements IGenTableColumnService {
         String isIn = genTableColumn.getIsIn();
         //是否是显示
         String isOut = genTableColumn.getIsOut();
+        List<GenTableColumn> genTableColumns = genTableColumnMapper.selectGenTableColumnListByTableId(genTableColumn.getTableId());
         GenTable genTable = new GenTable();
-        if (UserConstants.YES.equals(isIn)){
+        if (UserConstants.YES.equals(isIn)) {
+            //判断是否已经存在输入字段且不是同一个字段
+            for (GenTableColumn tableColumn : genTableColumns) {
+                if (UserConstants.YES.equals(tableColumn.getIsIn()) && !tableColumn.getColumnId().equals(genTableColumn.getColumnId())){
+                    tableColumn.setIsIn("N");
+                    genTableColumnMapper.updateGenTableColumn(tableColumn);
+                    break;
+                }
+            }
             genTable.setTableId(genTableColumn.getTableId());
             genTable.setAkColumn(genTableColumn.getColumnId());
         }
-        if (UserConstants.YES.equals(isOut)){
+        if (UserConstants.YES.equals(isOut)) {
+            //判断是否已经存在显示字段且不是同一个字段
+            for (GenTableColumn tableColumn : genTableColumns) {
+                if (UserConstants.YES.equals(tableColumn.getIsOut()) && !tableColumn.getColumnId().equals(genTableColumn.getColumnId())){
+                    tableColumn.setIsOut("N");
+                    genTableColumnMapper.updateGenTableColumn(tableColumn);
+                    break;
+                }
+            }
             genTable.setTableId(genTableColumn.getTableId());
             genTable.setDkColumn(genTableColumn.getColumnId());
         }
-        if (genTable.getTableId() != null){
+        if (genTable.getTableId() != null) {
             genTableMapper.updateGenTable(genTable);
         }
     }
@@ -113,7 +166,7 @@ public class GenTableColumnServiceImpl implements IGenTableColumnService {
     /**
      * 删除业务字段对象
      *
-     * @param ids 需要删除的数据ID
+     * @param ids 表id
      * @return 结果
      */
     @Override
@@ -123,11 +176,17 @@ public class GenTableColumnServiceImpl implements IGenTableColumnService {
 
     /**
      * 删除业务字段对象
+     *
      * @param ids 字段id
      * @return
      */
     @Override
     public int deleteGenTableColumnByColumnIds(String ids) {
+        Long[] longs = Convert.toLongArray(ids);
+        for (Long aLong : longs) {
+            GenTableColumn genTableColumn = genTableColumnMapper.selectGenTableColumnByColumnId(aLong);
+            insertCreateLog(genTableColumn,BusinessType.DELETE);
+        }
         return genTableColumnMapper.deleteGenTableColumnByColumnIds(Convert.toLongArray(ids));
     }
 
@@ -140,7 +199,7 @@ public class GenTableColumnServiceImpl implements IGenTableColumnService {
     @Override
     public String checkColumnNameUnique(GenTableColumn genTableColumn) {
         Long columnId = StringUtils.isNull(genTableColumn.getColumnId()) ? -1L : genTableColumn.getColumnId();
-        GenTableColumn column = genTableColumnMapper.checkColumnNameUnique(genTableColumn.getColumnName());
+        GenTableColumn column = genTableColumnMapper.checkColumnNameUnique(genTableColumn);
         if (StringUtils.isNotNull(column) && !column.getColumnId().equals(columnId)) {
             return UserConstants.NOT_UNIQUE;
         }

+ 10 - 3
boman-modules/boman-gen/src/main/java/com/boman/gen/service/GenTableServiceImpl.java

@@ -14,6 +14,7 @@ import java.util.zip.ZipOutputStream;
 import com.boman.common.core.constant.UserConstants;
 import com.boman.common.core.utils.DateUtils;
 import com.boman.common.core.web.domain.AjaxResult;
+import com.boman.common.log.enums.BusinessType;
 import com.boman.gen.domain.TableSql;
 
 import com.boman.system.api.RemoteSysMenuService;
@@ -65,6 +66,9 @@ public class GenTableServiceImpl implements IGenTableService {
     @Autowired
     private TableSqlServiceImpl tableSqlService;
 
+    @Autowired
+    private IGenTableColumnService tableColumnService;
+
     /**
      * 查询业务信息
      *
@@ -173,7 +177,7 @@ public class GenTableServiceImpl implements IGenTableService {
         int row = genTableMapper.updateGenTable(genTable);
         if (row > 0) {
             for (GenTableColumn cenTableColumn : genTable.getColumns()) {
-                genTableColumnMapper.updateGenTableColumn(cenTableColumn);
+                tableColumnService.updateGenTableColumn(cenTableColumn);
             }
         }
     }
@@ -185,10 +189,11 @@ public class GenTableServiceImpl implements IGenTableService {
      * @return 结果
      */
     @Override
-    @Transactional
+    @Transactional(rollbackFor = Exception.class)
     public void deleteGenTableByIds(Long[] tableIds) {
         genTableMapper.deleteGenTableByIds(tableIds);
         genTableColumnMapper.deleteGenTableColumnByIds(tableIds);
+        tableSqlService.deleteTableSqlByTableIds(tableIds);
     }
 
     /**
@@ -462,6 +467,7 @@ public class GenTableServiceImpl implements IGenTableService {
         genTableColumnLog.setHtmlType("input");
         genTableColumnLog.setSort(num);
         genTableColumnLog.setCreateBy("admin");
+        genTableColumnLog.setHtmlType("HR");
         genTableColumnLog.setCreateTime(DateUtils.getNowDate());
         genTableColumnMapper.insertGenTableColumn(genTableColumnLog);
 
@@ -476,6 +482,7 @@ public class GenTableServiceImpl implements IGenTableService {
         genTableColumnBaseInfo.setHtmlType("input");
         genTableColumnBaseInfo.setSort(20);
         genTableColumnBaseInfo.setCreateBy("admin");
+        genTableColumnBaseInfo.setHtmlType("HR");
         genTableColumnBaseInfo.setCreateTime(DateUtils.getNowDate());
         genTableColumnMapper.insertGenTableColumn(genTableColumnBaseInfo);
 
@@ -496,7 +503,7 @@ public class GenTableServiceImpl implements IGenTableService {
             genTableColumn.setIsInsert("1");
             genTableColumn.setQueryType("EQ");
             genTableColumn.setHtmlType(htmlType[i]);
-            genTableColumn.setSort(num + 1);
+            genTableColumn.setSort(num + i);
             genTableColumn.setCreateBy("admin");
             genTableColumn.setCreateTime(DateUtils.getNowDate());
             genTableColumn.setHrParentId(genTableColumnBaseInfo.getColumnId());

+ 8 - 0
boman-modules/boman-gen/src/main/java/com/boman/gen/service/ITableSqlService.java

@@ -37,4 +37,12 @@ public interface ITableSqlService {
      * @return
      */
     public AjaxResult selectTableSqlByTableId(Long id);
+
+    /**
+     * 删除建表语句
+     * @param ids
+     * @return
+     */
+    public int deleteTableSqlByTableIds(Long[] ids);
+
 }

+ 30 - 10
boman-modules/boman-gen/src/main/java/com/boman/gen/service/TableSqlServiceImpl.java

@@ -89,16 +89,25 @@ public class TableSqlServiceImpl implements ITableSqlService {
     @Override
     public AjaxResult implementSql(TableSql tableSql) {
         String createSql = tableSql.getCreateSql();
+        GenTable genTable = genTableMapper.selectGenTableById(tableSql.getTableId());
         try {
-            jdbcTemplate.execute(createSql);
+            int i = tableSqlMapper.testExists(genTable.getTableName());
+            if (i >= 0){
+                return AjaxResult.error("当前表已存在");
+            }
         } catch (DataAccessException e) {
-            e.printStackTrace();
-            return AjaxResult.error("当前表已存在");
+            try {
+                jdbcTemplate.execute(createSql);
+                String createLog = tableSql.getCreateLog();
+                StringBuffer sb = new StringBuffer(createLog);
+                sb.append("\r\n").append(DateUtils.getTime()).append(" ").append(SecurityUtils.getUsername()).append(" 执行建表语句");
+                tableSql.setCreateLog(sb.toString());
+                tableSqlMapper.updateTableSql(tableSql);
+            } catch (DataAccessException ex) {
+                ex.printStackTrace();
+                return AjaxResult.error("语句有误");
+            }
         }
-        String createLog = tableSql.getCreateLog();
-        StringBuffer sb = new StringBuffer(createLog);
-        sb.append(DateUtils.getTime()).append(" ").append(SecurityUtils.getUsername()).append(" 执行建表语句");
-        tableSqlMapper.updateTableSql(tableSql);
         return AjaxResult.success();
     }
 
@@ -114,6 +123,16 @@ public class TableSqlServiceImpl implements ITableSqlService {
         return AjaxResult.success(tableSql);
     }
 
+    /**
+     * 删除建表语句
+     * @param ids
+     * @return
+     */
+    @Override
+    public int deleteTableSqlByTableIds(Long[] ids) {
+        return  tableSqlMapper.deleteTableSqlByTableIds(ids);
+    }
+
 
     /**
      * 生成建表语句
@@ -130,12 +149,11 @@ public class TableSqlServiceImpl implements ITableSqlService {
         //拼接建表语句
         StringBuffer sb = new StringBuffer("create table if not exists ");
         sb.append(tableName).append(" (\r\n");
-
         for (GenTableColumn genTableColumn : genTableColumns) {
             if (!"HR".equals(genTableColumn.getHtmlType())){
                 sb.append(genTableColumn.getColumnName()).append(" ").append(genTableColumn.getColumnType());
-                sb = genTableColumn.getDefaultValue() == null ? sb.append(" DEFAULT NULL COMMENT ") : sb.append(" DEFAULT '").append(genTableColumn.getDefaultValue()).append("' COMMENT '");
-                sb.append(genTableColumn.getColumnComment()).append("'").append(",\r\n");
+                sb = genTableColumn.getDefaultValue() == null ? sb.append(" DEFAULT NULL COMMENT '") : sb.append(" DEFAULT '").append(genTableColumn.getDefaultValue()).append("' COMMENT '");
+                sb.append(genTableColumn.getColumnComment()).append("'").append(",").append("\r\n");
                 if (genTableColumn.getIsPk().equals(UserConstants.INCREMENT)) {
                     primaryKey = genTableColumn.getColumnName();
                 }
@@ -143,6 +161,8 @@ public class TableSqlServiceImpl implements ITableSqlService {
         }
         if (StringUtils.isNotBlank(primaryKey)) {
             sb.append(" PRIMARY KEY (").append(primaryKey).append(" ) USING BTREE\r\n");
+        }else {
+            sb.replace(sb.lastIndexOf(","),sb.length(),"\r\n");
         }
         sb.append(") ENGINE=InnoDB AUTO_INCREMENT=1 DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci ROW_FORMAT=DYNAMIC COMMENT='").append(tableName).append("';");
         return sb.toString();

+ 3 - 3
boman-modules/boman-gen/src/main/resources/mapper/generator/GenTableColumnMapper.xml

@@ -49,7 +49,7 @@ PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
         order by sort
     </select>
 
-    <select id="selectGenTableColumnListByColumnId" parameterType="GenTableColumn" resultMap="GenTableColumnResult">
+    <select id="selectGenTableColumnByColumnId" parameterType="GenTableColumn" resultMap="GenTableColumnResult">
         <include refid="selectGenTableColumnVo"/>
         where column_id = #{columnId}
        limit 1
@@ -179,9 +179,9 @@ PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
     </delete>
 
 
-    <select id="checkColumnNameUnique" parameterType="String" resultMap="GenTableColumnResult">
+    <select id="checkColumnNameUnique" parameterType="GenTableColumn" resultMap="GenTableColumnResult">
         <include refid="selectGenTableColumnVo"/>
-        where column_name = #{columnName} limit 1
+        where column_name = #{columnName} and table_id = #{tableId} limit 1
     </select>
     
     <delete id="deleteGenTableColumns">

+ 12 - 0
boman-modules/boman-gen/src/main/resources/mapper/generator/TableSqlMapper.xml

@@ -26,6 +26,11 @@ PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
         <include refid="selectTableSqlVo"></include>
         where table_id = #{id} limit 1
     </select>
+
+    <select id="testExists" parameterType="String" resultType="java.lang.Integer">
+        select count(1) from ${tableName}
+    </select>
+
     <insert id="insertTableSql" parameterType="TableSql" useGeneratedKeys="true" keyProperty="id">
         insert into table_sql (
 			<if test="tableId != null and tableId != ''">table_id,</if>
@@ -58,4 +63,11 @@ PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
         </set>
         where id = #{id}
     </update>
+
+    <delete id="deleteTableSqlByTableIds" parameterType="Long">
+        delete from table_sql where table_id in
+        <foreach collection="array" item="tableId" open="(" separator="," close=")">
+            #{tableId}
+        </foreach>
+    </delete>
 </mapper>