Skip to content

Commit

Permalink
feat-DictCategoryController-增加初始化数据字典功能
Browse files Browse the repository at this point in the history
  • Loading branch information
aruis committed Sep 25, 2024
1 parent fbe638d commit 7417545
Show file tree
Hide file tree
Showing 15 changed files with 195 additions and 25 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ void testSort() {
});

assertEquals(4, response.getSize());
assertEquals(3, response.getList().get(2).get("n_sort"));
assertEquals(3, response.getList().get(2).get("n_order"));

//把C移动到AB之间
String sortRes = given()
Expand All @@ -78,8 +78,8 @@ void testSort() {
Map<String, ?> c = testController.view(idC);
Map<String, ?> b = testController.view(idB);

assertEquals(new BigDecimal(2), c.get("n_sort"));
assertEquals(new BigDecimal(3), b.get("n_sort"));
assertEquals(new BigDecimal(2), c.get("n_order"));
assertEquals(new BigDecimal(3), b.get("n_order"));

given()
.queryParam("prevId", idD)
Expand All @@ -90,8 +90,8 @@ void testSort() {
.extract()
.asString();

assertEquals(new BigDecimal(3), testController.view(idD).get("n_sort"));
assertEquals(new BigDecimal(4), testController.view(idA).get("n_sort"));
assertEquals(new BigDecimal(3), testController.view(idD).get("n_order"));
assertEquals(new BigDecimal(4), testController.view(idA).get("n_order"));
}

}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -103,8 +103,8 @@ void testTreeANotShowMe() {
assertEquals("A.b", response.getLast().getLabel());

//修改顺序
testController.update(response.getFirst().getId(), Map.of("n_sort", 2));
testController.update(response.getLast().getId(), Map.of("n_sort", 1));
testController.update(response.getFirst().getId(), Map.of("n_order", 2));
testController.update(response.getLast().getId(), Map.of("n_order", 1));

List<TreeNode> response2 = given()
.queryParam("rootID", aID)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,19 @@ public class TestDictController {

String base = PlatformConst.BASE_PATH;

@Test
void testPlaform() {
List<TreeNode> response = given()
.get("%s/dict/tree".formatted(base))
.then()
.statusCode(200)
.extract()
.as(new TypeRef<>() {
});

assertTrue(!response.isEmpty());
}

@Test
void testDictCategoryAdd() {
given()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
public interface ISortAbility extends ISelectAbility, IUpdateAbility {

default SortColumn getSortColumn() {
return SortColumn.SORT;
return SortColumn.ORDER;
}

@GET
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,9 @@ public interface ITableCreateAbility {

TableWrapper getTableWrapper();

default void onTableCreated(boolean isFirst) {
}

@Transactional
@PostConstruct
default void create(IDatabaseOperations db) {
Expand All @@ -30,8 +33,10 @@ default void create(IDatabaseOperations db) {
ability.getSignColumns().forEach(wrapper::addColumn);
}

new TableBuilder(db).build(wrapper);
boolean build = new TableBuilder(db).build(wrapper);
this.onTableCreated(build);
}

}


Original file line number Diff line number Diff line change
Expand Up @@ -35,8 +35,8 @@ default SortColumn getDefatultSortColumn() {
return ability.getSortColumn();
}

if (checkColumn(SortColumn.SORT.getColumnName())) {
return SortColumn.SORT;
if (checkColumn(SortColumn.ORDER.getColumnName())) {
return SortColumn.ORDER;
}
if (checkColumn(SortColumn.CREATE.getColumnName())) {
return SortColumn.CREATE;
Expand Down Expand Up @@ -78,8 +78,8 @@ default String getSelectSql() {
starSql.append(",%s.%s as %s ".formatted(referenceTableTempName, column, alias));
});
StringBuilder other = new StringBuilder();
info.getOtherConditions().forEach((s1, s2) -> {
other.append(" and %s.%s = %s ".formatted(referenceTableTempName, s1, s2));
info.getOtherConditions().forEach((column, value) -> {
other.append(" and %s.%s = %s ".formatted(referenceTableTempName, column, value));
});

joinSql.append("\n left join %s.%s as %s on %s.%s = %s.%s %s "
Expand Down
7 changes: 6 additions & 1 deletion my-core/src/main/java/net/ximatai/muyun/core/Scaffold.java
Original file line number Diff line number Diff line change
Expand Up @@ -31,10 +31,15 @@ public EventBus getEventBus() {
}

@PostConstruct
void init() {
protected void init() {
if (this instanceof ITableCreateAbility ability) {
ability.create(getDatabaseOperations());
}
afterInit();
}

protected void afterInit() {

}

}
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
public class SortColumn {

public static final SortColumn CREATE = new SortColumn(Column.CREATE, Type.DESC);
public static final SortColumn SORT = new SortColumn(Column.SORT, Type.ASC);
public static final SortColumn ORDER = new SortColumn(Column.ORDER, Type.ASC);

private Column column;
private final String columnName;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ public enum Type {
.setType(Type.VARCHAR)
.setIndexed();

public static final Column SORT = new Column("n_sort")
public static final Column ORDER = new Column("n_order")
.setSequence()
.setIndexed();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,9 +29,6 @@ public class DepartmentController extends ScaffoldForPlatform implements ITreeAb
@Inject
Provider<OrganizationController> organizationProvider;

@Inject
DictController dictController;

@Override
public String getMainTable() {
return "org_department";
Expand All @@ -45,15 +42,13 @@ public TableWrapper getTableWrapper() {
.addColumn("v_name", "名称")
.addColumn("v_remark", "备注")
.addColumn(Column.of("id_at_org_organization").setComment("所属机构").setNullable(false))
.addColumn("dict_dept_type", "部门类型")
.addColumn("id_at_auth_user__leader", "部门主管");
}

@Override
public List<ReferenceInfo> getReferenceList() {
return List.of(
organizationProvider.get().toReferenceInfo("id_at_org_organization"),
dictController.toReferenceInfo("dict_dept_type")
organizationProvider.get().toReferenceInfo("id_at_org_organization")
);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
import net.ximatai.muyun.model.ChildTableInfo;
import net.ximatai.muyun.model.TreeNode;
import net.ximatai.muyun.platform.ScaffoldForPlatform;
import net.ximatai.muyun.platform.model.DictCategory;

import java.util.List;
import java.util.Map;
Expand Down Expand Up @@ -78,4 +79,29 @@ public void check(Map body, boolean isUpdate) {
throw new MyException("数据字典类目ID不能包含大写字母");
}
}

@Override
public void onTableCreated(boolean isFirst) {
List.of(
new DictCategory("muyun_dir", "平台自用", 0),
new DictCategory("platform_dir", "平台业务", 1)
).forEach(dictCategory -> {
this.putDictCategory(dictCategory, true);
});
}

public void putDictCategory(DictCategory dictCategory, boolean isLock) {
Map<String, ?> category = null;
try {
category = this.view(dictCategory.getId());
} catch (Exception ignored) {

}

if (category == null) {
this.create(dictCategory.toMap());
} else if (isLock) {
this.update(dictCategory.getId(), dictCategory.toMap());
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,8 @@ public TableWrapper getTableWrapper() {
.addColumn("id_at_app_dictcategory")
.addColumn("v_value")
.addColumn("v_name")
.addColumn("v_remark");
.addColumn("v_remark")
.addIndex(List.of("id_at_app_dictcategory", "v_value"), true);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@
import net.ximatai.muyun.model.ChildTableInfo;
import net.ximatai.muyun.model.ReferenceInfo;
import net.ximatai.muyun.platform.ScaffoldForPlatform;
import net.ximatai.muyun.platform.model.Dict;
import net.ximatai.muyun.platform.model.DictCategory;

import java.util.List;

Expand All @@ -27,9 +29,21 @@ public class OrganizationController extends ScaffoldForPlatform implements ITree
@Inject
DictController dictController;

@Inject
DictCategoryController dictCategoryController;

@Inject
DepartmentController departmentController;

@Override
protected void afterInit() {
dictCategoryController.putDictCategory(
new DictCategory("org_type", "PLATFORM_DIR", "机构类型", 0).setDictList(List.of(
new Dict("jituan", "集团公司"),
new Dict("erjigongsi", "二级公司")
)), true);
}

@Override
public String getMainTable() {
return "org_organization";
Expand All @@ -45,7 +59,7 @@ public TableWrapper getTableWrapper() {
.addColumn("v_shortname", "简称")
.addColumn("v_code", "编码")
.addColumn("v_address", "地址")
.addColumn("dict_orga_type", "机构类型")
.addColumn("dict_org_type", "机构类型")
.addColumn("v_introduction", "简介")
.addColumn("v_contact_person", "联系人")
.addColumn("v_contact_phone", "联系电话");
Expand All @@ -59,7 +73,7 @@ public List<ChildTableInfo> getChildren() {
@Override
public List<ReferenceInfo> getReferenceList() {
return List.of(
dictController.toReferenceInfo("dict_orga_type")
dictController.toReferenceInfo("dict_org_type")
);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
package net.ximatai.muyun.platform.model;

import java.util.HashMap;
import java.util.Map;

public class Dict {
private String categoryID;
private String value;
private String name;
private int order;

public Dict(String value, String name) {
this.value = value;
this.name = name;
}

public Dict setOrder(int order) {
this.order = order;
return this;
}

public Dict setCategoryID(String categoryID) {
this.categoryID = categoryID;
return this;
}

public Map<String, Object> toMap() {
Map map = new HashMap();
map.put("id_at_app_dictcategory", categoryID);
map.put("v_value", value);
map.put("v_name", name);
map.put("n_order", order);
return map;
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
package net.ximatai.muyun.platform.model;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class DictCategory {
private String id;
private String pid;
private String name;
private int order;
private List<Dict> dictList;

public DictCategory(String id, String name) {
this.id = id;
this.name = name;
}

public DictCategory(String id, String name, int order) {
this.id = id;
this.name = name;
this.order = order;
}

public DictCategory(String id, String pid, String name, int order) {
this.id = id;
this.pid = pid;
this.name = name;
this.order = order;
}

public String getId() {
return id;
}

public String getPid() {
return pid;
}

public String getName() {
return name;
}

public int getOrder() {
return order;
}

public List<Dict> getDictList() {
return dictList;
}

public DictCategory setDictList(List<Dict> dictList) {
for (int i = 0; i < dictList.size(); i++) {
Dict dict = dictList.get(i);
dict.setCategoryID(this.id);
dict.setOrder(i);
}

this.dictList = dictList;
return this;
}

public Map toMap() {
HashMap<String, Object> map = new HashMap<>();
map.put("id", this.id);
map.put("pid", this.pid);
map.put("v_name", this.name);
map.put("n_order", this.order);
if (this.dictList != null) {
map.put("app_dict", this.dictList.stream().map(Dict::toMap).toList());
}

return map;
}
}

0 comments on commit 7417545

Please sign in to comment.