Skip to content

Commit

Permalink
优化模型处理逻辑
Browse files Browse the repository at this point in the history
  • Loading branch information
wwsheng009 committed Jan 20, 2024
1 parent 00eb851 commit db2c0ff
Show file tree
Hide file tree
Showing 10 changed files with 371 additions and 329 deletions.
20 changes: 10 additions & 10 deletions scripts/amis/data/lib.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
const { FindCachedModelById } = Require("system.model_lib");
const { FindAndLoadYaoModelById } = Require("system.model_lib");
const { IsMysql } = Require("amis.lib_tool");
const { isDateTimeType } = Require("system.col_type");

Expand Down Expand Up @@ -60,7 +60,7 @@ function queryToQueryParam(modelIn, querysIn, queryParams) {

let columnMap = {};
if (typeof model === "string") {
model = FindCachedModelById(model);
model = FindAndLoadYaoModelById(model);
}
model.columns?.forEach((col) => {
columnMap[col.name] = col;
Expand Down Expand Up @@ -235,10 +235,11 @@ function queryToQueryParam(modelIn, querysIn, queryParams) {
return queryParam;
}

function getModelColumnMap(model) {
function getYaoModelColumnMap(model) {

let modelDsl = model;
if (typeof model === "string") {
modelDsl = FindCachedModelById(model);
modelDsl = FindAndLoadYaoModelById(model);
}

let columnMap = {};
Expand All @@ -261,25 +262,24 @@ function updateInputData(model, Data) {
if (typeof Data !== "object" || Data === null || Data === undefined) {
return Data;
}
const columnMap = getModelColumnMap(model);
const yaoColMap = getYaoModelColumnMap(model);

const hasUserId = columnMap["user_id"] !== null; // columns.some(col=>col.name = 'user_id')
const hasUserId = yaoColMap["user_id"] !== null; // columns.some(col=>col.name = 'user_id')
const user_id = Process("session.get", "user_id");

function updateLine(line) {
if (typeof line !== "object") {
return;
}
for (const key in columnMap) {
const modelCol = columnMap[key];
for (const key in yaoColMap) {
const modelCol = yaoColMap[key];
const type = modelCol.type.toUpperCase();
const field = line[key];
if (type == "UUID" && modelCol.primary == true && !field) {
// 自动生成uuid
line[key] = Process("utils.str.UUID");
continue;
}

if (!Object.hasOwnProperty.call(line, key)) {
continue;
}
Expand Down Expand Up @@ -312,7 +312,7 @@ function updateInputData(model, Data) {
break;
case "FLOAT":
case "DOUBLE":
case "DEMICAL":
case "DECIMAL":
case "UNSIGNEDFLOAT":
case "UNSIGNEDDOUBLE":
case "UNSIGNEDDECIMAL":
Expand Down
8 changes: 4 additions & 4 deletions scripts/amis/data/model.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
//使用models*类型的处理,这类处理器是直接对表数据的操作,相对tables*处理器,性能会更好
const { DotName } = Require("amis.lib_tool");
const { FindCachedModelById } = Require("system.model_lib");
const { FindAndLoadYaoModelById } = Require("system.model_lib");
const { queryToQueryParam, updateInputData, getArrayItem, mergeQueryObject } =
Require("amis.data.lib");

Expand Down Expand Up @@ -30,7 +30,7 @@ function dataSearch(model, pageIn, perPageIn, querysIn, queryParams, payload) {
perPage = getArrayItem(querys, "perPage") || 10;
}

const modelDsl = FindCachedModelById(model);
const modelDsl = FindAndLoadYaoModelById(model);

// 当是post请求是,payload生效
const queryParam = queryToQueryParam(modelDsl, querys, queryParams);
Expand Down Expand Up @@ -93,7 +93,7 @@ function dataSearch(model, pageIn, perPageIn, querysIn, queryParams, payload) {
}

function makeFake(modelId) {
const modelDsl = FindCachedModelById(modelId);
const modelDsl = FindAndLoadYaoModelById(modelId);

const fakeData = {};
modelDsl.columns.forEach((column) => {
Expand Down Expand Up @@ -229,7 +229,7 @@ function getData(model, id) {
//保存记录
//yao run scripts.amis.data.model.saveData
function saveData(modelId, payload) {
const modelDsl = FindCachedModelById(modelId);
const modelDsl = FindAndLoadYaoModelById(modelId);

payload = updateInputData(modelDsl, payload);

Expand Down
2 changes: 1 addition & 1 deletion scripts/amis/lib.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ const Exception = Error;
*/
function getModelDefinition(modelId, columnsIn) {
let model = Process(
"scripts.system.model.getModelById", //优先从数据库中加载,
"scripts.system.model.getDBModelById", //优先从数据库中加载,
DotName(modelId)
);
if (!model) {
Expand Down
5 changes: 5 additions & 0 deletions scripts/odata/lib/model.js
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,11 @@ function getModelsEntityset2() {
};
}

/**
* 转换嵌套的对象结构成扁平的列表结构
* @param {object} modelData
* @returns
*/
function modelDefinitionList(modelData) {
var list = [];

Expand Down
Loading

0 comments on commit db2c0ff

Please sign in to comment.