que usr 查询声明 用户查询 #45
This commit is contained in:
@ -122,3 +122,9 @@ export async function dbt_u(
|
||||
else return null
|
||||
} catch { return null }
|
||||
}
|
||||
|
||||
export function cdt_a(
|
||||
{ amt, aug }: Cdt,
|
||||
): number {
|
||||
return aug ? aug.reduce((a, b) => a + b.amt, amt) : amt
|
||||
}
|
||||
|
@ -1,5 +1,8 @@
|
||||
import type { Usr } from "../eid/typ.ts"
|
||||
import { coll } from "../eid/db.ts"
|
||||
import { idadm, idnam } from "../eid/id.ts"
|
||||
import { id, idadm, idnam } from "../eid/id.ts"
|
||||
import { usr_r } from "../eid/usr.ts"
|
||||
import { cdt_a, rec_f, rec_n, rec_s } from "../eid/rec.ts";
|
||||
|
||||
export async function adm(
|
||||
) {
|
||||
@ -8,3 +11,33 @@ export async function adm(
|
||||
soc.sort((a, b) => a[0] - b[0])
|
||||
return { adm1, adm2, soc }
|
||||
}
|
||||
|
||||
export async function usr(
|
||||
f: Pick<Usr, "_id"> | Pick<Usr, "nam">,
|
||||
) {
|
||||
const u = await usr_r(f, { _id: 1, utc: 1, nam: 1, adm1: 1, adm2: 1, msg: 1 })
|
||||
if (!u) return null
|
||||
const now = Date.now()
|
||||
const [sec, cdt, ern, cdt_n, dbt_n, ern_n] = await Promise.all([ // deno-lint-ignore no-explicit-any
|
||||
idnam(coll.soc, await id(coll.soc, { sec: u._id } as any)),
|
||||
rec_f(coll.cdt, { usr: u._id }, { now }),
|
||||
rec_s(coll.ern, { usr: u._id }, {}),
|
||||
rec_n(coll.cdt, { usr: u._id }, {}),
|
||||
rec_n(coll.dbt, { usr: u._id }, {}),
|
||||
rec_n(coll.ern, { usr: u._id }, {}),
|
||||
])
|
||||
const [dbt, soc] = await Promise.all([
|
||||
Promise.all(cdt.map(c => rec_s(coll.dbt, { usr: c._id.usr, soc: c._id.soc }, { frm: c.utc.eft }))),
|
||||
idnam(coll.soc, [...cdt.map(c => c._id.soc), ...ern.map(r => r.soc)]).then(n => new Map(n)),
|
||||
])
|
||||
return {
|
||||
...u, sec,
|
||||
cdt: cdt.map((c, n) => ({
|
||||
nam: soc.get(c._id.soc)!,
|
||||
soc: c._id.soc,
|
||||
amt: cdt_a(c) - (dbt[n].length == 0 ? 0 : dbt[n][0].amt)
|
||||
})).sort((a, b) => b.amt - a.amt),
|
||||
ern: ern.map(n => ({ ...n, nam: soc.get(n.soc)! })).sort((a, b) => b.amt - a.amt),
|
||||
sum: { cdt: cdt_n, dbt: dbt_n, ern: ern_n },
|
||||
}
|
||||
}
|
||||
|
@ -1,13 +1,38 @@
|
||||
import type { Agd, Msg, Rec, Soc, Usr } from "../eid/typ.ts"
|
||||
import { json } from "../ont/json.ts"
|
||||
import { Ret } from "./can.ts"
|
||||
import { adm } from "./doc.ts"
|
||||
import { adm, usr } from "./doc.ts"
|
||||
|
||||
export type Que = {
|
||||
que: "adm"
|
||||
que: "adm",
|
||||
} | {
|
||||
que: "usr",
|
||||
usr: Usr["_id"],
|
||||
} | {
|
||||
que: "usr",
|
||||
nam: Usr["nam"],
|
||||
} | {
|
||||
que: "soc",
|
||||
soc: Soc["_id"],
|
||||
} | {
|
||||
que: "agd",
|
||||
agd: Agd["_id"],
|
||||
} | {
|
||||
que: "cdt" | "dbt" | "ern",
|
||||
usr: Rec["_id"]["usr"],
|
||||
utc: Rec["_id"]["utc"],
|
||||
} | {
|
||||
que: "cdt" | "dbt" | "ern",
|
||||
soc: Rec["_id"]["soc"],
|
||||
utc: Rec["_id"]["utc"],
|
||||
} | {
|
||||
que: "wsl" | "lit",
|
||||
msg: Msg["_id"] | 0,
|
||||
}
|
||||
|
||||
export type QueRet = {
|
||||
adm: Ret<typeof adm>
|
||||
adm: Ret<typeof adm>,
|
||||
usr: Ret<typeof usr>,
|
||||
}
|
||||
|
||||
export function que(
|
||||
@ -17,6 +42,7 @@ export function que(
|
||||
|
||||
if (q) switch (q.que) {
|
||||
case "adm": return adm()
|
||||
case "usr": return usr("nam" in q ? { nam: q.nam } : { _id: q.usr })
|
||||
}
|
||||
|
||||
return null
|
||||
|
@ -1,16 +1,44 @@
|
||||
import { db } from "../src/eid/db.ts"
|
||||
import { soc_c } from "../src/eid/soc.ts"
|
||||
import type { Cdt } from "../src/eid/typ.ts"
|
||||
import { coll, db } from "../src/eid/db.ts"
|
||||
import { soc_c, soc_u } from "../src/eid/soc.ts"
|
||||
import { QueRet, que } from "../src/pra/que.ts"
|
||||
import { assertEquals } from "./mod.test.ts"
|
||||
import { usr_c, usr_u } from "../src/eid/usr.ts"
|
||||
import { rec_c } from "../src/eid/rec.ts"
|
||||
|
||||
await db("tst", true)
|
||||
|
||||
Deno.test("que", async () => {
|
||||
await Promise.all([
|
||||
soc_c("俱乐部", "江苏", "苏州")
|
||||
])
|
||||
const nbr = ["11111111111", "11111111112"]
|
||||
const now = Date.now()
|
||||
const cdt: Cdt[] = [{
|
||||
_id: { usr: 2, soc: 1, utc: now - 20000 }, msg: "msg", amt: 10,
|
||||
utc: { eft: now - 20000, exp: now - 10000, agr: 0 }, sec: 2,
|
||||
}, {
|
||||
_id: { usr: 2, soc: 1, utc: now - 10000 }, msg: "msg", amt: 10,
|
||||
utc: { eft: now - 10000, exp: now + 10000, agr: 0 }, sec: 2,
|
||||
}, {
|
||||
_id: { usr: 2, soc: 1, utc: now }, msg: "msg", amt: 10,
|
||||
utc: { eft: now + 10000, exp: now + 20000, agr: 0 }, sec: 2,
|
||||
}]
|
||||
const usr: QueRet["usr"] = {
|
||||
_id: 2, utc: now, nam: "用户", adm1: "四川", adm2: "成都", msg: "", sec: [[1, "俱乐部"]],
|
||||
cdt: [{ nam: "俱乐部", soc: 1, amt: 0 }],
|
||||
ern: [{ nam: "俱乐部", soc: 1, amt: 30 }],
|
||||
sum: { cdt: 3, dbt: 3, ern: 3 },
|
||||
}
|
||||
assertEquals([
|
||||
1, 2, 1, 1, 1, ...cdt.flatMap(c => [c._id, c._id, c._id]),
|
||||
], await Promise.all([
|
||||
await usr_c(nbr[0], "四川", "成都"), await usr_c(nbr[1], "四川", "成都"),
|
||||
usr_u(2, { $set: { nam: "用户" } }),
|
||||
await soc_c("俱乐部", "江苏", "苏州"), soc_u(1, { $set: { sec: [1, 2] } }),
|
||||
...cdt.flatMap(c => [rec_c(coll.cdt, c), rec_c(coll.dbt, { ...c, amt: 5 }), rec_c(coll.ern, c)]),
|
||||
]))
|
||||
assertEquals(
|
||||
{ adm1: [["江苏", [1]]], adm2: [["苏州", [1]]], soc: [[1, "俱乐部"]] },
|
||||
await que(`?que="adm"`) as QueRet["adm"]
|
||||
await que(`?que="adm"`) as QueRet["adm"],
|
||||
)
|
||||
assertEquals(usr, { ...await que(`?que="usr"&usr=2`) as QueRet["usr"], utc: now })
|
||||
assertEquals(usr, { ...await que(`?que="usr"&nam="用户"`) as QueRet["usr"], utc: now })
|
||||
})
|
||||
|
Reference in New Issue
Block a user