This commit is contained in:
张成
2026-04-01 13:12:40 +08:00
parent e9fd55666f
commit 82432cdba8
9 changed files with 174 additions and 85 deletions

View File

@@ -1,5 +1,5 @@
/**
* 管理端列表筛选、导出、写入字段裁剪( controller_admin 配合 baseModel
* 管理端:筛选条件、写入字段裁剪(分页/导出在各 controller 内直接 findAndCountAll / findAll
*/
const Sequelize = require("sequelize");
const { Op } = Sequelize;
@@ -76,53 +76,7 @@ function normalize_for_write(model, data, { for_create } = {}) {
return out;
}
function list_query_extra(model_name, model) {
if (model_name === "biz_audit_log") {
const tn = model.tableName;
return {
attributes: {
include: [[model.sequelize.col(`${tn}.created_at`), "created_at"]],
},
};
}
return {};
}
async function find_page(model, model_name, body, extra_find_options = {}) {
const param = body.param || body;
const page_option = param.pageOption || {};
const seach_option = param.seachOption || {};
const page_num = parseInt(page_option.page, 10) || 1;
const page_size = parseInt(page_option.pageSize, 10) || 20;
const offset = (page_num - 1) * page_size;
const where = build_search_where(model, seach_option);
return model.findAndCountAll({
where,
offset,
limit: page_size,
order: [["id", "DESC"]],
...list_query_extra(model_name, model),
...extra_find_options,
});
}
async function find_for_export(model, model_name, body, extra_find_options = {}) {
const param = body.param || body;
const where = build_search_where(model, param.seachOption || {});
const rows = await model.findAll({
where,
limit: 10000,
order: [["id", "DESC"]],
...list_query_extra(model_name, model),
...extra_find_options,
});
return { rows };
}
module.exports = {
build_search_where,
normalize_for_write,
list_query_extra,
find_page,
find_for_export,
};