This commit is contained in:
张成
2026-04-01 10:53:26 +08:00
parent c2205188d1
commit 03c5579c86
11 changed files with 211 additions and 580 deletions

View File

@@ -1,7 +1,6 @@
const stats = require("../service/biz_api_stats_service"); const stats = require("../service/biz_api_stats_service");
const crud = require("../service/biz_admin_crud"); const baseModel = require("../../middleware/baseModel");
const { find_page } = require("../service/biz_query_helpers");
module.exports = { module.exports = {
/** 按用户查询调用统计 */ /** 按用户查询调用统计 */
@@ -36,10 +35,10 @@ module.exports = {
ctx.success(data); ctx.success(data);
}, },
/** 调用日志分页列表(复用通用 CRUD */ /** 调用日志分页列表 */
"POST /biz_api_call_log/page": async (ctx) => { "POST /biz_api_call_log/page": async (ctx) => {
const body = ctx.getBody(); const body = ctx.getBody();
const data = await crud.page("biz_api_call_log", body); const { count, rows } = await find_page(baseModel.biz_api_call_log, "biz_api_call_log", body);
ctx.success({ rows: data.rows, count: data.count }); ctx.success({ rows, count });
}, },
}; };

View File

@@ -1,17 +1,15 @@
const crud = require("../service/biz_admin_crud"); const baseModel = require("../../middleware/baseModel");
const { find_page, find_for_export } = require("../service/biz_query_helpers");
module.exports = { module.exports = {
"POST /biz_audit_log/page": async (ctx) => { "POST /biz_audit_log/page": async (ctx) => {
const body = ctx.getBody(); const body = ctx.getBody();
const { count, rows } = await find_page(baseModel.biz_audit_log, "biz_audit_log", body);
ctx.success({ rows, count });
const data = await crud.page("biz_audit_log", body);
ctx.success({ rows: data.rows, count: data.count });
}, },
"POST /biz_audit_log/export": async (ctx) => { "POST /biz_audit_log/export": async (ctx) => {
const body = ctx.getBody(); const body = ctx.getBody();
const res = await crud.exportCsv("biz_audit_log", body); const res = await find_for_export(baseModel.biz_audit_log, "biz_audit_log", body);
ctx.success(res); ctx.success(res);
}, },
}; };

View File

@@ -1,16 +1,17 @@
const crud = require("../service/biz_admin_crud");
const baseModel = require("../../middleware/baseModel"); const baseModel = require("../../middleware/baseModel");
const { find_page, find_for_export, normalize_for_write } = require("../service/biz_query_helpers");
const audit = require("../service/biz_audit_service"); const audit = require("../service/biz_audit_service");
module.exports = { module.exports = {
"POST /biz_plan/page": async (ctx) => { "POST /biz_plan/page": async (ctx) => {
const body = ctx.getBody(); const body = ctx.getBody();
const data = await crud.page("biz_plan", body); const { count, rows } = await find_page(baseModel.biz_plan, "biz_plan", body);
ctx.success({ rows: data.rows, count: data.count }); ctx.success({ rows, count });
}, },
"POST /biz_plan/add": async (ctx) => { "POST /biz_plan/add": async (ctx) => {
const body = ctx.getBody(); const body = ctx.getBody();
const row = await crud.add("biz_plan", body); const payload = normalize_for_write(baseModel.biz_plan, body, { for_create: true });
const row = await baseModel.biz_plan.create(payload);
await audit.logAudit({ await audit.logAudit({
admin_user_id: audit.pickAdminId(ctx), admin_user_id: audit.pickAdminId(ctx),
action: "biz_plan.add", action: "biz_plan.add",
@@ -22,7 +23,11 @@ module.exports = {
}, },
"POST /biz_plan/edit": async (ctx) => { "POST /biz_plan/edit": async (ctx) => {
const body = ctx.getBody(); const body = ctx.getBody();
await crud.edit("biz_plan", body); const id = body.id;
if (id === undefined || id === null || id === "") throw new Error("缺少 id");
const payload = normalize_for_write(baseModel.biz_plan, body, { for_create: false });
delete payload.id;
await baseModel.biz_plan.update(payload, { where: { id } });
await audit.logAudit({ await audit.logAudit({
admin_user_id: audit.pickAdminId(ctx), admin_user_id: audit.pickAdminId(ctx),
action: "biz_plan.edit", action: "biz_plan.edit",
@@ -33,22 +38,29 @@ module.exports = {
}, },
"POST /biz_plan/del": async (ctx) => { "POST /biz_plan/del": async (ctx) => {
const body = ctx.getBody(); const body = ctx.getBody();
await crud.del("biz_plan", body); const id = body.id !== undefined ? body.id : body;
if (id === undefined || id === null || id === "") throw new Error("缺少 id");
await baseModel.biz_plan.destroy({ where: { id } });
await audit.logAudit({ await audit.logAudit({
admin_user_id: audit.pickAdminId(ctx), admin_user_id: audit.pickAdminId(ctx),
action: "biz_plan.del", action: "biz_plan.del",
resource_type: "biz_plan", resource_type: "biz_plan",
resource_id: body.id, resource_id: id,
}); });
ctx.success({}); ctx.success({});
}, },
"GET /biz_plan/detail": async (ctx) => { "GET /biz_plan/detail": async (ctx) => {
const q = ctx.query || {}; const q = ctx.query || {};
const row = await crud.detail("biz_plan", { id: q.id || q.ID }); const id = q.id || q.ID;
if (id === undefined || id === null || id === "") throw new Error("缺少 id");
const row = await baseModel.biz_plan.findByPk(id);
ctx.success(row); ctx.success(row);
}, },
"GET /biz_plan/all": async (ctx) => { "GET /biz_plan/all": async (ctx) => {
const rows = await crud.all("biz_plan"); const rows = await baseModel.biz_plan.findAll({
limit: 2000,
order: [["id", "DESC"]],
});
ctx.success(rows); ctx.success(rows);
}, },
"POST /biz_plan/toggle": async (ctx) => { "POST /biz_plan/toggle": async (ctx) => {
@@ -70,7 +82,7 @@ module.exports = {
}, },
"POST /biz_plan/export": async (ctx) => { "POST /biz_plan/export": async (ctx) => {
const body = ctx.getBody(); const body = ctx.getBody();
const res = await crud.exportCsv("biz_plan", body); const res = await find_for_export(baseModel.biz_plan, "biz_plan", body);
ctx.success(res); ctx.success(res);
}, },
}; };

View File

@@ -1,18 +1,19 @@
const crud = require("../service/biz_admin_crud");
const baseModel = require("../../middleware/baseModel"); const baseModel = require("../../middleware/baseModel");
const { find_page, find_for_export } = require("../service/biz_query_helpers");
const logic = require("../service/biz_subscription_logic"); const logic = require("../service/biz_subscription_logic");
const audit = require("../service/biz_audit_service"); const audit = require("../service/biz_audit_service");
module.exports = { module.exports = {
"POST /biz_subscription/page": async (ctx) => { "POST /biz_subscription/page": async (ctx) => {
const body = ctx.getBody(); const body = ctx.getBody();
const data = await crud.page("biz_subscription", body); const { count, rows } = await find_page(baseModel.biz_subscription, "biz_subscription", body);
ctx.success({ rows: data.rows, count: data.count }); ctx.success({ rows, count });
}, },
"GET /biz_subscription/detail": async (ctx) => { "GET /biz_subscription/detail": async (ctx) => {
const q = ctx.query || {}; const q = ctx.query || {};
const row = await crud.detail("biz_subscription", { id: q.id || q.ID }); const id = q.id || q.ID;
if (id === undefined || id === null || id === "") throw new Error("缺少 id");
const row = await baseModel.biz_subscription.findByPk(id);
ctx.success(row); ctx.success(row);
}, },
"GET /biz_subscription/by_user": async (ctx) => { "GET /biz_subscription/by_user": async (ctx) => {
@@ -74,7 +75,7 @@ module.exports = {
}, },
"POST /biz_subscription/export": async (ctx) => { "POST /biz_subscription/export": async (ctx) => {
const body = ctx.getBody(); const body = ctx.getBody();
const res = await crud.exportCsv("biz_subscription", body); const res = await find_for_export(baseModel.biz_subscription, "biz_subscription", body);
ctx.success(res); ctx.success(res);
}, },
}; };

View File

@@ -1,13 +1,13 @@
const crud = require("../service/biz_admin_crud"); const baseModel = require("../../middleware/baseModel");
const { find_page, find_for_export } = require("../service/biz_query_helpers");
const tokenLogic = require("../service/biz_token_logic"); const tokenLogic = require("../service/biz_token_logic");
const audit = require("../service/biz_audit_service"); const audit = require("../service/biz_audit_service");
module.exports = { module.exports = {
"POST /biz_token/page": async (ctx) => { "POST /biz_token/page": async (ctx) => {
const body = ctx.getBody(); const body = ctx.getBody();
const data = await crud.page("biz_api_token", body); const { count, rows } = await find_page(baseModel.biz_api_token, "biz_api_token", body);
ctx.success({ rows: data.rows, count: data.count }); ctx.success({ rows, count });
}, },
"POST /biz_token/create": async (ctx) => { "POST /biz_token/create": async (ctx) => {
const body = ctx.getBody(); const body = ctx.getBody();
@@ -44,7 +44,7 @@ module.exports = {
}, },
"POST /biz_token/export": async (ctx) => { "POST /biz_token/export": async (ctx) => {
const body = ctx.getBody(); const body = ctx.getBody();
const res = await crud.exportCsv("biz_api_token", body); const res = await find_for_export(baseModel.biz_api_token, "biz_api_token", body);
ctx.success(res); ctx.success(res);
}, },
}; };

View File

@@ -1,35 +1,44 @@
const crud = require("../service/biz_admin_crud"); const baseModel = require("../../middleware/baseModel");
const { find_page, find_for_export, normalize_for_write } = require("../service/biz_query_helpers");
module.exports = { module.exports = {
"POST /biz_usage/page": async (ctx) => { "POST /biz_usage/page": async (ctx) => {
const body = ctx.getBody(); const body = ctx.getBody();
const data = await crud.page("biz_usage_monthly", body); const { count, rows } = await find_page(baseModel.biz_usage_monthly, "biz_usage_monthly", body);
ctx.success({ rows: data.rows, count: data.count }); ctx.success({ rows, count });
}, },
"POST /biz_usage/add": async (ctx) => { "POST /biz_usage/add": async (ctx) => {
const body = ctx.getBody(); const body = ctx.getBody();
const row = await crud.add("biz_usage_monthly", body); const payload = normalize_for_write(baseModel.biz_usage_monthly, body, { for_create: true });
const row = await baseModel.biz_usage_monthly.create(payload);
ctx.success(row); ctx.success(row);
}, },
"POST /biz_usage/edit": async (ctx) => { "POST /biz_usage/edit": async (ctx) => {
const body = ctx.getBody(); const body = ctx.getBody();
await crud.edit("biz_usage_monthly", body); const id = body.id;
if (id === undefined || id === null || id === "") throw new Error("缺少 id");
const payload = normalize_for_write(baseModel.biz_usage_monthly, body, { for_create: false });
delete payload.id;
await baseModel.biz_usage_monthly.update(payload, { where: { id } });
ctx.success({}); ctx.success({});
}, },
"POST /biz_usage/del": async (ctx) => { "POST /biz_usage/del": async (ctx) => {
const body = ctx.getBody(); const body = ctx.getBody();
await crud.del("biz_usage_monthly", body); const id = body.id !== undefined ? body.id : body;
if (id === undefined || id === null || id === "") throw new Error("缺少 id");
await baseModel.biz_usage_monthly.destroy({ where: { id } });
ctx.success({}); ctx.success({});
}, },
"GET /biz_usage/detail": async (ctx) => { "GET /biz_usage/detail": async (ctx) => {
const q = ctx.query || {}; const q = ctx.query || {};
const row = await crud.detail("biz_usage_monthly", { id: q.id || q.ID }); const id = q.id || q.ID;
if (id === undefined || id === null || id === "") throw new Error("缺少 id");
const row = await baseModel.biz_usage_monthly.findByPk(id);
ctx.success(row); ctx.success(row);
}, },
"POST /biz_usage/export": async (ctx) => { "POST /biz_usage/export": async (ctx) => {
const body = ctx.getBody(); const body = ctx.getBody();
const res = await crud.exportCsv("biz_usage_monthly", body); const res = await find_for_export(baseModel.biz_usage_monthly, "biz_usage_monthly", body);
ctx.success(res); ctx.success(res);
}, },
}; };

View File

@@ -1,6 +1,5 @@
const Sequelize = require("sequelize"); const Sequelize = require("sequelize");
const crud = require("../service/biz_admin_crud"); const { find_for_export, normalize_for_write, build_search_where } = require("../service/biz_query_helpers");
const baseModel = require("../../middleware/baseModel"); const baseModel = require("../../middleware/baseModel");
const tokenLogic = require("../service/biz_token_logic"); const tokenLogic = require("../service/biz_token_logic");
const audit = require("../service/biz_audit_service"); const audit = require("../service/biz_audit_service");
@@ -15,7 +14,7 @@ module.exports = {
const pageSize = parseInt(pageOption.pageSize, 10) || 20; const pageSize = parseInt(pageOption.pageSize, 10) || 20;
const offset = (pageNum - 1) * pageSize; const offset = (pageNum - 1) * pageSize;
const model = baseModel.biz_user; const model = baseModel.biz_user;
const where = crud.buildSearchWhere(model, seachOption); const where = build_search_where(model, seachOption);
const { count, rows } = await model.findAndCountAll({ const { count, rows } = await model.findAndCountAll({
where, where,
offset, offset,
@@ -25,7 +24,7 @@ module.exports = {
include: [ include: [
[ [
Sequelize.literal( Sequelize.literal(
`(SELECT COUNT(*) FROM biz_api_token WHERE biz_api_token.user_id = biz_user.id)` `(SELECT COUNT(*) FROM biz_api_tokens WHERE biz_api_tokens.user_id = biz_user.id)`
), ),
"token_count", "token_count",
], ],
@@ -36,7 +35,8 @@ module.exports = {
}, },
"POST /biz_user/add": async (ctx) => { "POST /biz_user/add": async (ctx) => {
const body = ctx.getBody(); const body = ctx.getBody();
const row = await crud.add("biz_user", body); const payload = normalize_for_write(baseModel.biz_user, body, { for_create: true });
const row = await baseModel.biz_user.create(payload);
await audit.logAudit({ await audit.logAudit({
admin_user_id: audit.pickAdminId(ctx), admin_user_id: audit.pickAdminId(ctx),
biz_user_id: row.id, biz_user_id: row.id,
@@ -49,7 +49,11 @@ module.exports = {
}, },
"POST /biz_user/edit": async (ctx) => { "POST /biz_user/edit": async (ctx) => {
const body = ctx.getBody(); const body = ctx.getBody();
await crud.edit("biz_user", body); const id = body.id;
if (id === undefined || id === null || id === "") throw new Error("缺少 id");
const payload = normalize_for_write(baseModel.biz_user, body, { for_create: false });
delete payload.id;
await baseModel.biz_user.update(payload, { where: { id } });
await audit.logAudit({ await audit.logAudit({
admin_user_id: audit.pickAdminId(ctx), admin_user_id: audit.pickAdminId(ctx),
biz_user_id: body.id, biz_user_id: body.id,
@@ -61,7 +65,9 @@ module.exports = {
}, },
"POST /biz_user/del": async (ctx) => { "POST /biz_user/del": async (ctx) => {
const body = ctx.getBody(); const body = ctx.getBody();
await crud.del("biz_user", body); const id = body.id !== undefined ? body.id : body;
if (id === undefined || id === null || id === "") throw new Error("缺少 id");
await baseModel.biz_user.destroy({ where: { id } });
await audit.logAudit({ await audit.logAudit({
admin_user_id: audit.pickAdminId(ctx), admin_user_id: audit.pickAdminId(ctx),
biz_user_id: body.id, biz_user_id: body.id,
@@ -74,7 +80,8 @@ module.exports = {
"GET /biz_user/detail": async (ctx) => { "GET /biz_user/detail": async (ctx) => {
const q = ctx.query || {}; const q = ctx.query || {};
const id = q.id || q.ID; const id = q.id || q.ID;
const user = await crud.detail("biz_user", { id }); if (id === undefined || id === null || id === "") throw new Error("缺少 id");
const user = await baseModel.biz_user.findByPk(id);
if (!user) { if (!user) {
return ctx.fail("用户不存在"); return ctx.fail("用户不存在");
} }
@@ -100,7 +107,10 @@ module.exports = {
}); });
}, },
"GET /biz_user/all": async (ctx) => { "GET /biz_user/all": async (ctx) => {
const rows = await crud.all("biz_user"); const rows = await baseModel.biz_user.findAll({
limit: 2000,
order: [["id", "DESC"]],
});
ctx.success(rows); ctx.success(rows);
}, },
"POST /biz_user/disable": async (ctx) => { "POST /biz_user/disable": async (ctx) => {
@@ -133,7 +143,7 @@ module.exports = {
}, },
"POST /biz_user/export": async (ctx) => { "POST /biz_user/export": async (ctx) => {
const body = ctx.getBody(); const body = ctx.getBody();
const res = await crud.exportCsv("biz_user", body); const res = await find_for_export(baseModel.biz_user, "biz_user", body);
ctx.success(res); ctx.success(res);
}, },
"POST /biz_user/revoke_all_tokens": async (ctx) => { "POST /biz_user/revoke_all_tokens": async (ctx) => {

View File

@@ -1,196 +0,0 @@
/**
* 订阅模块通用 CRUD与 admin 约定 POST /{model}/page|add|edit|del GET /{model}/detail|all
*/
const baseModel = require("../../middleware/baseModel");
const { op } = baseModel;
function getModel(modelName) {
const m = baseModel[modelName];
if (!m) {
throw new Error(`模型不存在: ${modelName}`);
}
return m;
}
function normalizeForWrite(model, data, { forCreate } = {}) {
const attrs = model.rawAttributes;
const out = {};
for (const k of Object.keys(data || {})) {
if (!attrs[k]) continue;
let v = data[k];
if (v === "") {
if (k === "id" && forCreate) continue;
if (k.endsWith("_id") || k === "id") {
v = null;
} else if (attrs[k].allowNull) {
v = null;
}
}
if (k === "enabled_features" && typeof v === "string" && v.trim() !== "") {
try {
v = JSON.parse(v);
} catch (e) {
/* 保持原字符串,由 Sequelize 或 DB 报错 */
}
}
out[k] = v;
}
if (forCreate && out.id !== undefined && (out.id === "" || out.id === null)) {
delete out.id;
}
return out;
}
function buildSearchWhere(model, seachOption) {
const key = seachOption && seachOption.key;
const raw = seachOption && seachOption.value;
if (!key || raw === undefined || raw === null) return {};
const str = String(raw).trim();
if (str === "") return {};
const attr = model.rawAttributes[key];
if (!attr) {
return { [key]: { [op.like]: `%${str}%` } };
}
const typeKey = attr.type && attr.type.key;
if (typeKey === "BOOLEAN") {
if (str === "true" || str === "1" || str === "是") return { [key]: true };
if (str === "false" || str === "0" || str === "否") return { [key]: false };
return {};
}
if (typeKey === "ENUM") {
return { [key]: str };
}
if (
typeKey === "INTEGER" ||
typeKey === "BIGINT" ||
typeKey === "FLOAT" ||
typeKey === "DOUBLE" ||
typeKey === "DECIMAL"
) {
const n = Number(str);
if (!Number.isNaN(n)) return { [key]: n };
return {};
}
if (typeKey === "DATE" || typeKey === "DATEONLY") {
return { [key]: str };
}
return { [key]: { [op.like]: `%${str}%` } };
}
/** 模型未声明但表中存在的列(列表/导出需要带出) */
function extraListAttributes(modelName, model) {
if (modelName === "biz_audit_log") {
const tn = model.tableName;
return {
attributes: {
include: [[model.sequelize.col(`${tn}.created_at`), "created_at"]],
},
};
}
return {};
}
async function page(modelName, body) {
const model = getModel(modelName);
const param = body.param || body;
const pageOption = param.pageOption || {};
const seachOption = param.seachOption || {};
const pageNum = parseInt(pageOption.page, 10) || 1;
const pageSize = parseInt(pageOption.pageSize, 10) || 20;
const offset = (pageNum - 1) * pageSize;
const where = buildSearchWhere(model, seachOption);
const { count, rows } = await model.findAndCountAll({
where,
offset,
limit: pageSize,
order: [["id", "DESC"]],
...extraListAttributes(modelName, model),
});
return { rows, count };
}
async function add(modelName, body) {
const model = getModel(modelName);
const payload = normalizeForWrite(model, body, { forCreate: true });
const row = await model.create(payload);
return row;
}
async function edit(modelName, body) {
const model = getModel(modelName);
const id = body.id;
if (id === undefined || id === null || id === "") {
throw new Error("缺少 id");
}
const payload = normalizeForWrite(model, body, { forCreate: false });
delete payload.id;
await model.update(payload, { where: { id } });
return {};
}
async function del(modelName, body) {
const model = getModel(modelName);
const id = body.id !== undefined ? body.id : body;
if (id === undefined || id === null || id === "") {
throw new Error("缺少 id");
}
await model.destroy({ where: { id } });
return {};
}
async function detail(modelName, query) {
const model = getModel(modelName);
const id = query && (query.id || query.ID);
if (id === undefined || id === null || id === "") {
throw new Error("缺少 id");
}
const row = await model.findByPk(id, extraListAttributes(modelName, model));
return row;
}
async function all(modelName) {
const model = getModel(modelName);
const rows = await model.findAll({
limit: 2000,
order: [["id", "DESC"]],
...extraListAttributes(modelName, model),
});
return rows;
}
async function exportCsv(modelName, body) {
const model = getModel(modelName);
const param = body.param || body;
const where = buildSearchWhere(model, param.seachOption || {});
const rows = await model.findAll({
where,
limit: 10000,
order: [["id", "DESC"]],
...extraListAttributes(modelName, model),
});
return { rows };
}
module.exports = {
page,
add,
edit,
del,
detail,
all,
exportCsv,
buildSearchWhere,
};

View File

@@ -0,0 +1,128 @@
/**
* 列表筛选、写入字段裁剪、审计日志列表补列(供 admin 控制器直接配合 baseModel 使用)
*/
const Sequelize = require("sequelize");
const { Op } = Sequelize;
function build_search_where(model, seach_option) {
const key = seach_option && seach_option.key;
const raw = seach_option && seach_option.value;
if (!key || raw === undefined || raw === null) return {};
const str = String(raw).trim();
if (str === "") return {};
const attr = model.rawAttributes[key];
if (!attr) {
return { [key]: { [Op.like]: `%${str}%` } };
}
const type_key = attr.type && attr.type.key;
if (type_key === "BOOLEAN") {
if (str === "true" || str === "1" || str === "是") return { [key]: true };
if (str === "false" || str === "0" || str === "否") return { [key]: false };
return {};
}
if (type_key === "ENUM") {
return { [key]: str };
}
if (
type_key === "INTEGER" ||
type_key === "BIGINT" ||
type_key === "FLOAT" ||
type_key === "DOUBLE" ||
type_key === "DECIMAL"
) {
const n = Number(str);
if (!Number.isNaN(n)) return { [key]: n };
return {};
}
if (type_key === "DATE" || type_key === "DATEONLY") {
return { [key]: str };
}
return { [key]: { [Op.like]: `%${str}%` } };
}
function normalize_for_write(model, data, { for_create } = {}) {
const attrs = model.rawAttributes;
const out = {};
for (const k of Object.keys(data || {})) {
if (!attrs[k]) continue;
let v = data[k];
if (v === "") {
if (k === "id" && for_create) continue;
if (k.endsWith("_id") || k === "id") {
v = null;
} else if (attrs[k].allowNull) {
v = null;
}
}
if (k === "enabled_features" && typeof v === "string" && v.trim() !== "") {
try {
v = JSON.parse(v);
} catch (e) {
/* 保持原字符串 */
}
}
out[k] = v;
}
if (for_create && out.id !== undefined && (out.id === "" || out.id === null)) {
delete out.id;
}
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,
};

View File

@@ -3,337 +3,7 @@
* @param {Object} models - 所有模型对象 * @param {Object} models - 所有模型对象
*/ */
module.exports = (models) => { module.exports = (models) => {
// ========== 仓库主数据 ==========
// models["war_warehouse"].hasMany(models["war_laneway"], {
// foreignKey: "warehouse_id",
// sourceKey: "id",
// as: "laneways",
// });
// models["war_laneway"].belongsTo(models["war_warehouse"], {
// foreignKey: "warehouse_id",
// targetKey: "id",
// as: "warehouse",
// });
// models["war_warehouse"].hasMany(models["war_rack"], {
// foreignKey: "warehouse_id",
// sourceKey: "id",
// as: "racks",
// });
// models["war_rack"].belongsTo(models["war_warehouse"], {
// foreignKey: "warehouse_id",
// targetKey: "id",
// as: "warehouse",
// });
// models["war_warehouse"].hasMany(models["war_area"], {
// foreignKey: "warehouse_id",
// sourceKey: "id",
// as: "areas",
// });
// models["war_area"].belongsTo(models["war_warehouse"], {
// foreignKey: "warehouse_id",
// targetKey: "id",
// as: "warehouse",
// });
// models["war_warehouse"].hasMany(models["war_location"], {
// foreignKey: "warehouse_id",
// sourceKey: "id",
// as: "locations",
// });
// models["war_location"].belongsTo(models["war_warehouse"], {
// foreignKey: "warehouse_id",
// targetKey: "id",
// as: "warehouse",
// });
// // 货位从属(可选)
// models["war_area"].hasMany(models["war_location"], {
// foreignKey: "area_id",
// sourceKey: "id",
// as: "area_locations",
// });
// models["war_location"].belongsTo(models["war_area"], {
// foreignKey: "area_id",
// targetKey: "id",
// as: "area",
// });
// models["war_laneway"].hasMany(models["war_location"], {
// foreignKey: "laneway_id",
// sourceKey: "id",
// as: "laneway_locations",
// });
// models["war_location"].belongsTo(models["war_laneway"], {
// foreignKey: "laneway_id",
// targetKey: "id",
// as: "laneway",
// });
// models["war_rack"].hasMany(models["war_location"], {
// foreignKey: "rack_id",
// sourceKey: "id",
// as: "rack_locations",
// });
// models["war_location"].belongsTo(models["war_rack"], {
// foreignKey: "rack_id",
// targetKey: "id",
// as: "rack",
// });
// // ========== 托盘主数据 ==========
// models["try_tray_type"].hasMany(models["try_tray"], {
// foreignKey: "tray_type_id",
// sourceKey: "id",
// as: "trays",
// });
// models["try_tray"].belongsTo(models["try_tray_type"], {
// foreignKey: "tray_type_id",
// targetKey: "id",
// as: "tray_type",
// });
// // ========== 合作方与物料主数据 ==========
// models["par_customer"].hasMany(models["mat_material"], {
// foreignKey: "customer_id",
// sourceKey: "id",
// as: "materials",
// });
// models["mat_material"].belongsTo(models["par_customer"], {
// foreignKey: "customer_id",
// targetKey: "id",
// as: "customer",
// });
// models["par_supplier"].hasMany(models["mat_material"], {
// foreignKey: "supplier_id",
// sourceKey: "id",
// as: "materials",
// });
// models["mat_material"].belongsTo(models["par_supplier"], {
// foreignKey: "supplier_id",
// targetKey: "id",
// as: "supplier",
// });
// models["mat_type"].hasMany(models["mat_material"], {
// foreignKey: "material_type_id",
// sourceKey: "id",
// as: "materials",
// });
// models["mat_material"].belongsTo(models["mat_type"], {
// foreignKey: "material_type_id",
// targetKey: "id",
// as: "material_type",
// });
// models["mat_measure"].hasMany(models["mat_material"], {
// foreignKey: "measure_id",
// sourceKey: "id",
// as: "materials",
// });
// models["mat_material"].belongsTo(models["mat_measure"], {
// foreignKey: "measure_id",
// targetKey: "id",
// as: "measure",
// });
// // ========== 条码主数据 ==========
// models["mat_material"].hasMany(models["bar_code"], {
// foreignKey: "material_id",
// sourceKey: "id",
// as: "barcodes",
// });
// models["bar_code"].belongsTo(models["mat_material"], {
// foreignKey: "material_id",
// targetKey: "id",
// as: "material",
// });
// models["bar_type"].hasMany(models["bar_code"], {
// foreignKey: "bar_type_id",
// sourceKey: "id",
// as: "barcodes",
// });
// models["bar_code"].belongsTo(models["bar_type"], {
// foreignKey: "bar_type_id",
// targetKey: "id",
// as: "bar_type",
// });
// if (models["sys_user"]) {
// models["bar_code"].belongsTo(models["sys_user"], {
// foreignKey: "created_by_user_id",
// targetKey: "id",
// as: "created_by_user",
// });
// }
// // ========== 收货/发货业务 ==========
// models["rec_receiving"].belongsTo(models["war_warehouse"], {
// foreignKey: "warehouse_id",
// targetKey: "id",
// as: "warehouse",
// });
// models["rec_receiving"].hasMany(models["rec_receiving_item"], {
// foreignKey: "receiving_id",
// sourceKey: "id",
// as: "items",
// });
// models["rec_receiving_item"].belongsTo(models["rec_receiving"], {
// foreignKey: "receiving_id",
// targetKey: "id",
// as: "receiving",
// });
// models["rec_receiving_item"].belongsTo(models["mat_material"], {
// foreignKey: "material_id",
// targetKey: "id",
// as: "material",
// });
// models["rec_send"].belongsTo(models["war_warehouse"], {
// foreignKey: "warehouse_id",
// targetKey: "id",
// as: "warehouse",
// });
// models["rec_send"].hasMany(models["rec_send_item"], {
// foreignKey: "send_id",
// sourceKey: "id",
// as: "items",
// });
// models["rec_send_item"].belongsTo(models["rec_send"], {
// foreignKey: "send_id",
// targetKey: "id",
// as: "send",
// });
// models["rec_send_item"].belongsTo(models["mat_material"], {
// foreignKey: "material_id",
// targetKey: "id",
// as: "material",
// });
// // ========== 入库作业 ==========
// models["job_inbound"].belongsTo(models["war_warehouse"], {
// foreignKey: "warehouse_id",
// targetKey: "id",
// as: "warehouse",
// });
// models["job_inbound"].hasMany(models["job_inbound_item"], {
// foreignKey: "inbound_id",
// sourceKey: "id",
// as: "items",
// });
// models["job_inbound_item"].belongsTo(models["job_inbound"], {
// foreignKey: "inbound_id",
// targetKey: "id",
// as: "inbound",
// });
// models["job_inbound_item"].belongsTo(models["mat_material"], {
// foreignKey: "material_id",
// targetKey: "id",
// as: "material",
// });
// // ========== 出库作业 ==========
// models["job_outbound"].belongsTo(models["war_warehouse"], {
// foreignKey: "warehouse_id",
// targetKey: "id",
// as: "warehouse",
// });
// models["job_outbound"].hasMany(models["job_outbound_item"], {
// foreignKey: "outbound_id",
// sourceKey: "id",
// as: "items",
// });
// models["job_outbound_item"].belongsTo(models["job_outbound"], {
// foreignKey: "outbound_id",
// targetKey: "id",
// as: "outbound",
// });
// models["job_outbound_item"].belongsTo(models["mat_material"], {
// foreignKey: "material_id",
// targetKey: "id",
// as: "material",
// });
// // ========== 移库 ==========
// models["job_move"].belongsTo(models["war_warehouse"], {
// foreignKey: "warehouse_id",
// targetKey: "id",
// as: "warehouse",
// });
// models["job_move"].hasMany(models["job_move_item"], {
// foreignKey: "move_id",
// sourceKey: "id",
// as: "items",
// });
// models["job_move_item"].belongsTo(models["job_move"], {
// foreignKey: "move_id",
// targetKey: "id",
// as: "move",
// });
// models["job_move_item"].belongsTo(models["mat_material"], {
// foreignKey: "material_id",
// targetKey: "id",
// as: "material",
// });
// // ========== 调拨 ==========
// models["job_allocate"].belongsTo(models["war_warehouse"], {
// foreignKey: "from_warehouse_id",
// targetKey: "id",
// as: "from_warehouse",
// });
// models["job_allocate"].belongsTo(models["war_warehouse"], {
// foreignKey: "to_warehouse_id",
// targetKey: "id",
// as: "to_warehouse",
// });
// models["job_allocate"].hasMany(models["job_allocate_item"], {
// foreignKey: "allocate_id",
// sourceKey: "id",
// as: "items",
// });
// models["job_allocate_item"].belongsTo(models["job_allocate"], {
// foreignKey: "allocate_id",
// targetKey: "id",
// as: "allocate",
// });
// models["job_allocate_item"].belongsTo(models["mat_material"], {
// foreignKey: "material_id",
// targetKey: "id",
// as: "material",
// });
// // ========== 报损 ==========
// models["job_damage"].belongsTo(models["war_warehouse"], {
// foreignKey: "warehouse_id",
// targetKey: "id",
// as: "warehouse",
// });
// models["job_damage"].hasMany(models["job_damage_item"], {
// foreignKey: "damage_id",
// sourceKey: "id",
// as: "items",
// });
// models["job_damage_item"].belongsTo(models["job_damage"], {
// foreignKey: "damage_id",
// targetKey: "id",
// as: "damage",
// });
// models["job_damage_item"].belongsTo(models["mat_material"], {
// foreignKey: "material_id",
// targetKey: "id",
// as: "material",
// });
// // ========== 盘点 ==========
// models["job_count"].belongsTo(models["war_warehouse"], {
// foreignKey: "warehouse_id",
// targetKey: "id",
// as: "warehouse",
// });
// ========== 订阅模块biz_*========== // ========== 订阅模块biz_*==========
const m = models; const m = models;