Files
wechatWeb/api/controller_admin/biz_plan.js
张成 03c5579c86 1
2026-04-01 10:53:26 +08:00

89 lines
3.1 KiB
JavaScript

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");
module.exports = {
"POST /biz_plan/page": async (ctx) => {
const body = ctx.getBody();
const { count, rows } = await find_page(baseModel.biz_plan, "biz_plan", body);
ctx.success({ rows, count });
},
"POST /biz_plan/add": async (ctx) => {
const body = ctx.getBody();
const payload = normalize_for_write(baseModel.biz_plan, body, { for_create: true });
const row = await baseModel.biz_plan.create(payload);
await audit.logAudit({
admin_user_id: audit.pickAdminId(ctx),
action: "biz_plan.add",
resource_type: "biz_plan",
resource_id: row.id,
detail: { plan_code: row.plan_code },
});
ctx.success(row);
},
"POST /biz_plan/edit": async (ctx) => {
const body = ctx.getBody();
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({
admin_user_id: audit.pickAdminId(ctx),
action: "biz_plan.edit",
resource_type: "biz_plan",
resource_id: body.id,
});
ctx.success({});
},
"POST /biz_plan/del": async (ctx) => {
const body = ctx.getBody();
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({
admin_user_id: audit.pickAdminId(ctx),
action: "biz_plan.del",
resource_type: "biz_plan",
resource_id: id,
});
ctx.success({});
},
"GET /biz_plan/detail": async (ctx) => {
const q = ctx.query || {};
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);
},
"GET /biz_plan/all": async (ctx) => {
const rows = await baseModel.biz_plan.findAll({
limit: 2000,
order: [["id", "DESC"]],
});
ctx.success(rows);
},
"POST /biz_plan/toggle": async (ctx) => {
const body = ctx.getBody();
const id = body.id;
if (id == null) return ctx.fail("缺少 id");
const row = await baseModel.biz_plan.findByPk(id);
if (!row) return ctx.fail("套餐不存在");
const next = row.status === "active" ? "inactive" : "active";
await row.update({ status: next });
await audit.logAudit({
admin_user_id: audit.pickAdminId(ctx),
action: "biz_plan.toggle",
resource_type: "biz_plan",
resource_id: id,
detail: { status: next },
});
ctx.success({ status: next });
},
"POST /biz_plan/export": async (ctx) => {
const body = ctx.getBody();
const res = await find_for_export(baseModel.biz_plan, "biz_plan", body);
ctx.success(res);
},
};