疯狂的狮子li
2021-06-13 d98faaffee6627655bf584a79e968656012adf84
ruoyi-generator/src/main/java/com/ruoyi/generator/service/GenTableServiceImpl.java
@@ -3,13 +3,15 @@
import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.lang.Validator;
import cn.hutool.core.util.StrUtil;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ruoyi.common.constant.Constants;
import com.ruoyi.common.constant.GenConstants;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.exception.CustomException;
import com.ruoyi.common.utils.JsonUtils;
import com.ruoyi.common.utils.PageUtils;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.common.utils.file.FileUtils;
import com.ruoyi.generator.domain.GenTable;
@@ -65,6 +67,16 @@
        return genTable;
    }
    @Override
    public TableDataInfo<GenTable> selectPageGenTableList(GenTable genTable) {
        return PageUtils.buildDataInfo(baseMapper.selectPageGenTableList(PageUtils.buildPage(), genTable));
    }
    @Override
    public TableDataInfo<GenTable> selectPageDbTableList(GenTable genTable) {
        return PageUtils.buildDataInfo(baseMapper.selectPageDbTableList(PageUtils.buildPage(), genTable));
    }
    /**
     * 查询业务列表
     *
@@ -117,12 +129,21 @@
    @Override
    @Transactional
    public void updateGenTable(GenTable genTable) {
        String options = JSON.toJSONString(genTable.getParams());
        String options = JsonUtils.toJsonString(genTable.getParams());
        genTable.setOptions(options);
        int row = baseMapper.updateById(genTable);
        if (row > 0) {
            for (GenTableColumn cenTableColumn : genTable.getColumns()) {
                genTableColumnMapper.updateById(cenTableColumn);
                genTableColumnMapper.update(cenTableColumn,
                        new LambdaUpdateWrapper<GenTableColumn>()
                                .set(cenTableColumn.getIsPk() == null, GenTableColumn::getIsPk, null)
                                .set(cenTableColumn.getIsIncrement() == null, GenTableColumn::getIsIncrement, null)
                                .set(cenTableColumn.getIsInsert() == null, GenTableColumn::getIsInsert, null)
                                .set(cenTableColumn.getIsEdit() == null, GenTableColumn::getIsEdit, null)
                                .set(cenTableColumn.getIsList() == null, GenTableColumn::getIsList, null)
                                .set(cenTableColumn.getIsQuery() == null, GenTableColumn::getIsQuery, null)
                        .set(cenTableColumn.getIsRequired() == null, GenTableColumn::getIsRequired, null)
                                .eq(GenTableColumn::getColumnId,cenTableColumn.getColumnId()));
            }
        }
    }
@@ -241,12 +262,8 @@
                StringWriter sw = new StringWriter();
                Template tpl = Velocity.getTemplate(template, Constants.UTF8);
                tpl.merge(context, sw);
                try {
                    String path = getGenPath(table, template);
                    FileUtils.writeStringToFile(new File(path), sw.toString(), Constants.UTF8);
                } catch (IOException e) {
                    throw new CustomException("渲染模板失败,表名:" + table.getTableName());
                }
                String path = getGenPath(table, template);
                FileUtils.writeUtf8String(sw.toString(), path);
            }
        }
    }
@@ -343,13 +360,12 @@
    @Override
    public void validateEdit(GenTable genTable) {
        if (GenConstants.TPL_TREE.equals(genTable.getTplCategory())) {
            String options = JSON.toJSONString(genTable.getParams());
            JSONObject paramsObj = JSONObject.parseObject(options);
            if (Validator.isEmpty(paramsObj.getString(GenConstants.TREE_CODE))) {
         Map<String, Object> paramsObj = genTable.getParams();
            if (Validator.isEmpty(paramsObj.get(GenConstants.TREE_CODE))) {
                throw new CustomException("树编码字段不能为空");
            } else if (Validator.isEmpty(paramsObj.getString(GenConstants.TREE_PARENT_CODE))) {
            } else if (Validator.isEmpty(paramsObj.get(GenConstants.TREE_PARENT_CODE))) {
                throw new CustomException("树父编码字段不能为空");
            } else if (Validator.isEmpty(paramsObj.getString(GenConstants.TREE_NAME))) {
            } else if (Validator.isEmpty(paramsObj.get(GenConstants.TREE_NAME))) {
                throw new CustomException("树名称字段不能为空");
            } else if (GenConstants.TPL_SUB.equals(genTable.getTplCategory())) {
                if (Validator.isEmpty(genTable.getSubTableName())) {
@@ -407,13 +423,13 @@
     * @param genTable 设置后的生成对象
     */
    public void setTableFromOptions(GenTable genTable) {
        JSONObject paramsObj = JSONObject.parseObject(genTable.getOptions());
      Map<String, String> paramsObj = JsonUtils.parseMap(genTable.getOptions());
        if (Validator.isNotNull(paramsObj)) {
            String treeCode = paramsObj.getString(GenConstants.TREE_CODE);
            String treeParentCode = paramsObj.getString(GenConstants.TREE_PARENT_CODE);
            String treeName = paramsObj.getString(GenConstants.TREE_NAME);
            String parentMenuId = paramsObj.getString(GenConstants.PARENT_MENU_ID);
            String parentMenuName = paramsObj.getString(GenConstants.PARENT_MENU_NAME);
            String treeCode = paramsObj.get(GenConstants.TREE_CODE);
            String treeParentCode = paramsObj.get(GenConstants.TREE_PARENT_CODE);
            String treeName = paramsObj.get(GenConstants.TREE_NAME);
            String parentMenuId = paramsObj.get(GenConstants.PARENT_MENU_ID);
            String parentMenuName = paramsObj.get(GenConstants.PARENT_MENU_NAME);
            genTable.setTreeCode(treeCode);
            genTable.setTreeParentCode(treeParentCode);
@@ -437,4 +453,4 @@
        }
        return genPath + File.separator + VelocityUtils.getFileName(template, table);
    }
}
}