weaver-work-cli 0.1.73 → 0.1.75
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +37 -1
- package/dist/chunks/{archive-THZV4ND4.js → archive-OZJ6PYUR.js} +14 -9
- package/dist/chunks/{archive-oa-PQ23OLEV.js → archive-oa-BHL5RK2F.js} +10 -7
- package/dist/chunks/{asset-RGPROZYH.js → asset-HKRFRQR2.js} +10 -7
- package/dist/chunks/{asset-idle-Z2PDX7NA.js → asset-idle-GGIR4KE7.js} +7 -4
- package/dist/chunks/{attachment-content-R3RPVOYE.js → attachment-content-4OV65NK5.js} +3 -3
- package/dist/chunks/{blog-7U3QZPIF.js → blog-HYDP4DV3.js} +10 -7
- package/dist/chunks/{caizhilian-3AB42GCV.js → caizhilian-JS4RQZ6Z.js} +10 -7
- package/dist/chunks/{calendar-KINM64E4.js → calendar-SWRY4DIT.js} +10 -7
- package/dist/chunks/chunk-257JXJGD.js +30 -0
- package/dist/chunks/{chunk-LLCMOZ5L.js → chunk-2AUFKU2D.js} +11 -11
- package/dist/chunks/chunk-2BZ7TY4M.js +49 -0
- package/dist/chunks/{chunk-WM33BSIL.js → chunk-2QCCXPNU.js} +1 -1
- package/dist/chunks/{chunk-TV46Q4NY.js → chunk-4FCBEWNB.js} +74 -68
- package/dist/chunks/{chunk-NUP4RYZK.js → chunk-5D2V7ZAN.js} +2 -2
- package/dist/chunks/{chunk-DRUND2PJ.js → chunk-5QUTQHXF.js} +225 -152
- package/dist/chunks/{chunk-4BXR6CNB.js → chunk-5ZJGPEIB.js} +2 -2
- package/dist/chunks/chunk-7DE3VMZD.js +35 -0
- package/dist/chunks/chunk-BNKUHN4N.js +1346 -0
- package/dist/chunks/{chunk-2CHGUYMG.js → chunk-BSR5TM7U.js} +4 -4
- package/dist/chunks/{chunk-4I26YMBY.js → chunk-ERZY6GYL.js} +7 -3
- package/dist/chunks/{chunk-4YXAK6OA.js → chunk-FEZK536Z.js} +12 -7
- package/dist/chunks/{chunk-F4HM25K4.js → chunk-I6NOHKEK.js} +1 -1
- package/dist/chunks/{chunk-JMJBPP4B.js → chunk-J7FAAHWH.js} +2 -2
- package/dist/chunks/{chunk-54ST55IJ.js → chunk-JJONWB5N.js} +11 -1
- package/dist/chunks/chunk-JOY3HZXL.js +467 -0
- package/dist/chunks/{chunk-GWJLAF6B.js → chunk-JWF2R6EQ.js} +0 -462
- package/dist/chunks/{chunk-BWYVS62Z.js → chunk-KEA4XQPO.js} +4 -4
- package/dist/chunks/chunk-KINXYJXM.js +205 -0
- package/dist/chunks/{chunk-IBSJJGXL.js → chunk-KWJEICD4.js} +2 -2
- package/dist/chunks/{chunk-GAMGDNTF.js → chunk-LJI4N6FV.js} +1 -1
- package/dist/chunks/{chunk-IU6UB2TD.js → chunk-MXCRKXOP.js} +48 -6
- package/dist/chunks/{chunk-PAFOLIYQ.js → chunk-NWYHCZTY.js} +1 -1
- package/dist/chunks/chunk-OJ4JPD25.js +428 -0
- package/dist/chunks/{chunk-L7TYG5SN.js → chunk-OQCK5SAU.js} +2 -2
- package/dist/chunks/chunk-PG5YSNYY.js +1166 -0
- package/dist/chunks/{chunk-XZBYRYWX.js → chunk-QJWVYOOV.js} +2 -2
- package/dist/chunks/{chunk-AMAPQVBT.js → chunk-QVKRNL7L.js} +286 -305
- package/dist/chunks/chunk-SU6IJD6D.js +49 -0
- package/dist/chunks/{chunk-ZWLWXHJC.js → chunk-TA2RBUTH.js} +2 -2
- package/dist/chunks/{chunk-6HT73VOI.js → chunk-U6Y32KEV.js} +2 -2
- package/dist/chunks/chunk-UUJK76HV.js +209 -0
- package/dist/chunks/{chunk-R3MK2RBN.js → chunk-VOMDD2C5.js} +1 -1
- package/dist/chunks/chunk-Y3C6MDL7.js +28 -0
- package/dist/chunks/chunk-ZM7RN2IM.js +38 -0
- package/dist/chunks/{chunk-2XED2FKY.js → chunk-ZUB4M4N3.js} +38 -36
- package/dist/chunks/{chunk-7QFVXP2X.js → chunk-ZWIMLT6P.js} +8 -7
- package/dist/chunks/cli-FULPXIKJ.js +226 -0
- package/dist/chunks/commands-BNWFNTD3.js +70 -0
- package/dist/chunks/{conflict-VPAR7E6F.js → conflict-5BKCDV2F.js} +2 -2
- package/dist/chunks/{contract-DDXAO2DZ.js → contract-WI23OXVC.js} +12446 -5964
- package/dist/chunks/{cowork-34QCHMDD.js → cowork-DZBBVNMQ.js} +11 -8
- package/dist/chunks/{create-flow-52PU2OBI.js → create-flow-7ZRBRKX7.js} +6 -6
- package/dist/chunks/{data-CLQ62QGD.js → data-PAZVE6QE.js} +6 -3
- package/dist/chunks/{ebuilder-form-W5R7Q22S.js → ebuilder-form-5SQXIGXN.js} +9 -6
- package/dist/chunks/{ehr-LCNAVMK5.js → ehr-CX5F4ZAU.js} +23 -16
- package/dist/chunks/{entry-mode-6Z3ZV3DE.js → entry-mode-I2BYNONZ.js} +3 -3
- package/dist/chunks/{env-RDKWAARB.js → env-ZAQZ7EHN.js} +2 -2
- package/dist/chunks/{esb-ANKPUU65.js → esb-DQTXDIBF.js} +10 -7
- package/dist/chunks/{factory-434B7RAT.js → factory-XRI6KZ7H.js} +1 -1
- package/dist/chunks/{fna-AKBSG5HI.js → fna-FK6HQVJL.js} +13 -8
- package/dist/chunks/{group-chat-PROTW22R.js → group-chat-MLWNYM2P.js} +3 -3
- package/dist/chunks/{host-WO4L5G5I.js → host-3EPREGXS.js} +6 -3
- package/dist/chunks/{host-XIYG4F4B.js → host-J74JGYAW.js} +6 -3
- package/dist/chunks/{host-7FJW4E7R.js → host-KUMU3DDK.js} +6 -3
- package/dist/chunks/{host-5R2SNFWD.js → host-MEC4VQZA.js} +6 -3
- package/dist/chunks/{host-PDJAKCT7.js → host-T6NNQGVZ.js} +6 -3
- package/dist/chunks/{host-CGXUFQO4.js → host-VI7Q45PM.js} +6 -3
- package/dist/chunks/{hrm-WGODDTEH.js → hrm-KKVAW4P5.js} +8 -5
- package/dist/chunks/{im-YKTKAJU3.js → im-47CIWQNL.js} +7 -4
- package/dist/chunks/{invoice-PVYCCCVM.js → invoice-CDYRBNIG.js} +21 -16
- package/dist/chunks/{jiuchuanhui-RKOQW7BY.js → jiuchuanhui-M7QTRHZK.js} +17 -10
- package/dist/chunks/{lazy-dispatch-DW45AFQ5.js → lazy-dispatch-FKQJZFI5.js} +22 -8
- package/dist/chunks/{mail-BNB5S4NW.js → mail-NRBS5N6Y.js} +11 -8
- package/dist/chunks/{meeting-O2KDJH66.js → meeting-WC5LD3IO.js} +22 -18
- package/dist/chunks/{minute-doc-JUCG56GY.js → minute-doc-OQUMDTKG.js} +4 -4
- package/dist/chunks/{okr-LWFGWOE7.js → okr-EHKSKMPL.js} +10 -7
- package/dist/chunks/{operations-532PN5OB.js → operations-V5VM5MS2.js} +7 -4
- package/dist/chunks/{performance-7W5COCFU.js → performance-ZJADXJPN.js} +6 -3
- package/dist/chunks/{plan-ZVY3IYTL.js → plan-OTDNBDJ7.js} +10 -7
- package/dist/chunks/{project-SA62B7MH.js → project-4XVBQ3HX.js} +10 -7
- package/dist/chunks/{reception-NGIONPQ3.js → reception-3FAHOOH7.js} +8 -4
- package/dist/chunks/{recruit-stage-WOSN6MGP.js → recruit-stage-3WTVWW2K.js} +199 -74
- package/dist/chunks/register-SQOPIR2G.js +16 -0
- package/dist/chunks/{registry-R72GWXOG.js → registry-F4YFZLOD.js} +7 -3
- package/dist/chunks/registry-NBTBXR4L.js +30 -0
- package/dist/chunks/{registry-N7P3W2RD.js → registry-TJGCVRCW.js} +7 -4
- package/dist/chunks/{room-recommend-KCGIVUFR.js → room-recommend-KL62XB2Y.js} +4 -4
- package/dist/chunks/root-YZG2TS5W.js +16 -0
- package/dist/chunks/{schedule-6UP27HQ4.js → schedule-XOISYUPB.js} +4 -4
- package/dist/chunks/{smart-extract-DDOQSPRB.js → smart-extract-UYGNV3BM.js} +15 -7
- package/dist/chunks/update-AV5VOXGY.js +13 -0
- package/dist/chunks/{workflow-V5ALD6QZ.js → workflow-V54LJPXL.js} +15 -10
- package/dist/cli.js +40 -0
- package/dist/cmd/action.js +3 -1
- package/dist/cmd/doctor/index.js +1 -1
- package/dist/cmd/e10/index.js +1 -1
- package/dist/cmd/identity/index.js +5 -8
- package/dist/cmd/index.js +2 -0
- package/dist/cmd/setup/index.js +1 -1
- package/dist/cmd/update/index.js +38 -0
- package/dist/core/manifest-view.js +1 -1
- package/dist/core/output.js +8 -1
- package/dist/core/runtime-agent.js +13 -0
- package/dist/core/update-notice.js +23 -0
- package/dist/index.js +105 -198
- package/dist/internal/e10/auth/agent-host-key.js +16 -0
- package/dist/internal/e10/auth/agent-hosts.js +45 -9
- package/dist/internal/e10/auth/commands.js +66 -36
- package/dist/internal/e10/auth/session.js +30 -15
- package/dist/internal/e10/auth/xiaoe.js +4 -20
- package/dist/internal/e10/auth-recovery.js +44 -4
- package/dist/internal/e10/context.js +5 -2
- package/dist/internal/update/bootstrap.js +96 -0
- package/dist/internal/update/npm.js +78 -0
- package/dist/internal/update/service.js +252 -0
- package/dist/internal/update/store.js +135 -0
- package/dist/shortcuts/archive/index.js +2 -2
- package/dist/shortcuts/archive/manifest.js +1 -0
- package/dist/shortcuts/archive/operations/search.js +3 -1
- package/dist/shortcuts/archive-oa/index.js +1 -1
- package/dist/shortcuts/asset/index.js +1 -1
- package/dist/shortcuts/asset-idle/index.js +1 -1
- package/dist/shortcuts/blog/index.js +1 -1
- package/dist/shortcuts/caizhilian/index.js +1 -1
- package/dist/shortcuts/calendar/index.js +1 -1
- package/dist/shortcuts/contract/data/bi-config.js +156 -0
- package/dist/shortcuts/contract/data/collection-ledger-html.js +189 -0
- package/dist/shortcuts/contract/data/collection-node-catalog.js +226 -0
- package/dist/shortcuts/contract/data/metric-catalog.js +154 -20
- package/dist/shortcuts/contract/data/report-renderers.js +414 -0
- package/dist/shortcuts/contract/data/report-spec.js +132 -0
- package/dist/shortcuts/contract/error-actions.js +114 -0
- package/dist/shortcuts/contract/host.js +12 -3
- package/dist/shortcuts/contract/index.js +17 -1
- package/dist/shortcuts/contract/manifest.js +398 -3
- package/dist/shortcuts/contract/ooxml/docx-table.js +48 -0
- package/dist/shortcuts/contract/operations/bi-preview.js +131 -0
- package/dist/shortcuts/contract/operations/bizreview-report.js +496 -0
- package/dist/shortcuts/contract/operations/collection-node.js +229 -0
- package/dist/shortcuts/contract/operations/docx-slots.js +735 -16
- package/dist/shortcuts/contract/operations/draft-build.js +940 -0
- package/dist/shortcuts/contract/operations/draft-submit.js +229 -0
- package/dist/shortcuts/contract/operations/first-screen.js +3 -2
- package/dist/shortcuts/contract/operations/generated-submit.js +175 -0
- package/dist/shortcuts/contract/operations/ledger-report.js +233 -0
- package/dist/shortcuts/contract/operations/ledger-stat.js +1400 -0
- package/dist/shortcuts/contract/operations/ledger.js +4 -1
- package/dist/shortcuts/contract/operations/metrics.js +9 -3
- package/dist/shortcuts/contract/operations/ops-draft.js +527 -0
- package/dist/shortcuts/contract/operations/page.js +9 -0
- package/dist/shortcuts/contract/operations/quote-submit.js +55 -229
- package/dist/shortcuts/contract/operations/registry.js +20 -0
- package/dist/shortcuts/contract/operations/sign-launch.js +256 -0
- package/dist/shortcuts/contract/operations/viewlink.js +334 -0
- package/dist/shortcuts/cowork/index.js +2 -2
- package/dist/shortcuts/ebuilder-form/index.js +1 -1
- package/dist/shortcuts/ehr/index.js +2 -2
- package/dist/shortcuts/ehr/manifest.js +1 -1
- package/dist/shortcuts/ehr/operations/recruit-position.js +7 -2
- package/dist/shortcuts/ehr/operations/recruit-write.js +7 -6
- package/dist/shortcuts/ehr/operations/recruit.js +318 -248
- package/dist/shortcuts/ehr/operations/shared.js +22 -2
- package/dist/shortcuts/esb/index.js +1 -1
- package/dist/shortcuts/fna/index.js +1 -1
- package/dist/shortcuts/fna/manifest.js +1 -0
- package/dist/shortcuts/fna/operations/expense-chart.js +3 -1
- package/dist/shortcuts/hrm/index.js +1 -1
- package/dist/shortcuts/invoice/index.js +1 -1
- package/dist/shortcuts/jiuchuanhui/index.js +2 -2
- package/dist/shortcuts/jiuchuanhui/manifest.js +3 -0
- package/dist/shortcuts/mail/index.js +2 -2
- package/dist/shortcuts/meeting/index.js +1 -1
- package/dist/shortcuts/meeting/manifest.js +1 -0
- package/dist/shortcuts/meeting/operations/eb/catalog.js +5 -3
- package/dist/shortcuts/meeting/operations/eb/conflict.js +3 -1
- package/dist/shortcuts/meeting/operations/eb/create-flow.js +10 -9
- package/dist/shortcuts/meeting/operations/eb/factory.js +2 -0
- package/dist/shortcuts/meeting/operations/eb/member-expand.js +3 -2
- package/dist/shortcuts/meeting/operations/invitation-publish.js +3 -3
- package/dist/shortcuts/meeting/operations/invitation.js +4 -4
- package/dist/shortcuts/meeting/operations/link.js +3 -1
- package/dist/shortcuts/okr/index.js +1 -1
- package/dist/shortcuts/performance/index.js +1 -1
- package/dist/shortcuts/plan/index.js +1 -1
- package/dist/shortcuts/project/index.js +1 -1
- package/dist/shortcuts/reception/manifest.js +2 -2
- package/dist/shortcuts/recruit-stage/context.js +10 -2
- package/dist/shortcuts/recruit-stage/index.js +16 -3
- package/dist/shortcuts/recruit-stage/operations/interview-arrange.js +1 -1
- package/dist/shortcuts/recruit-stage/operations/interview-feedback.js +1 -1
- package/dist/shortcuts/recruit-stage/operations/jd-create.js +17 -8
- package/dist/shortcuts/recruit-stage/operations/list.js +1 -1
- package/dist/shortcuts/recruit-stage/operations/resume-import.js +178 -35
- package/dist/shortcuts/recruit-stage/operations/search.js +2 -2
- package/dist/shortcuts/recruit-stage/operations/shortlist.js +4 -2
- package/dist/shortcuts/recruit-stage/shared.js +27 -53
- package/dist/shortcuts/smart-extract/manifest.js +1 -0
- package/dist/shortcuts/smart-extract/operations/fill.js +7 -2
- package/dist/shortcuts/smart-extract/operations/validate.js +4 -1
- package/dist/shortcuts/workflow/index.js +2 -2
- package/dist/shortcuts/workflow/manifest.js +1 -0
- package/dist/shortcuts/workflow/operations/aiDeepRead/render.js +4 -1
- package/dist/shortcuts/workflow/operations/deepRead.js +23 -2
- package/dist/shortcuts/workflow/operations/form.js +8 -2
- package/docs/contract.md +6 -1
- package/docs/e10-auth.md +25 -7
- package/docs/operation-manual.md +15 -0
- package/package.json +2 -2
- package/scripts/pysite/__pycache__/sitecustomize.cpython-314.pyc +0 -0
- package/skill-template/business-info.json +28 -0
- package/skill-template/domains/jinchengda.md +13 -1
- package/skills/weaver-e10-archive-oa/references/source-manifest.json +3 -3
- package/skills/weaver-e10-caizhilian/references/auth-and-install.md +2 -1
- package/skills/weaver-e10-caizhilian/references/source-manifest.json +3 -3
- package/skills/weaver-e10-esb/references/source-manifest.json +3 -3
- package/skills/weaver-e10-jinchengda/SKILL.md +90 -32
- package/skills/weaver-e10-jinchengda/references/contract-ai-review.md +45 -0
- package/skills/weaver-e10-jinchengda/references/contract-bizreview-report-template.html +68 -0
- package/skills/weaver-e10-jinchengda/references/contract-bizreview.md +216 -0
- package/skills/weaver-e10-jinchengda/references/contract-clause-negotiate.md +215 -0
- package/skills/weaver-e10-jinchengda/references/contract-collection-node.md +113 -0
- package/skills/weaver-e10-jinchengda/references/contract-command-index.md +19 -3
- package/skills/weaver-e10-jinchengda/references/contract-decisions.md +114 -0
- package/skills/weaver-e10-jinchengda/references/contract-doc-triage.md +53 -44
- package/skills/weaver-e10-jinchengda/references/contract-element-extract.md +1 -1
- package/skills/weaver-e10-jinchengda/references/contract-field-extract-other-flows.md +1 -1
- package/skills/weaver-e10-jinchengda/references/contract-firstscreen-guide.md +200 -0
- package/skills/weaver-e10-jinchengda/references/contract-ledger-catalog.md +18 -1
- package/skills/weaver-e10-jinchengda/references/contract-ledger-query-playbook.md +138 -3
- package/skills/weaver-e10-jinchengda/references/contract-ledger-report.md +158 -0
- package/skills/weaver-e10-jinchengda/references/contract-ledger-rules.md +3 -1
- package/skills/weaver-e10-jinchengda/references/contract-material-gap.md +139 -0
- package/skills/weaver-e10-jinchengda/references/contract-object-detect.md +2 -2
- package/skills/weaver-e10-jinchengda/references/contract-open-web-pages.md +10 -3
- package/skills/weaver-e10-jinchengda/references/contract-ops-draft.md +106 -0
- package/skills/weaver-e10-jinchengda/references/contract-performance-query.md +1 -1
- package/skills/weaver-e10-jinchengda/references/contract-quote-to-contract.md +176 -56
- package/skills/weaver-e10-jinchengda/references/contract-sign-launch.md +123 -0
- package/skills/weaver-e10-jinchengda/references/contract-smart-draft.md +410 -0
- package/skills/weaver-e10-jinchengda/references/contract-template-recommend.md +1 -1
- package/skills/weaver-e10-jinchengda/references/contract-viewlink.md +110 -0
- package/skills/weaver-e10-jinchengda/references/orchestration.md +38 -24
- package/skills/weaver-e10-meeting/SKILL.md +13 -0
- package/skills/weaver-e10-okr/SKILL.md +11 -0
- package/skills/weaver-e10-okr/references/source-manifest.json +7 -6
- package/skills/weaver-e10-performance/SKILL.md +131 -39
- package/skills/weaver-e10-performance/agents/openai.yaml +48 -17
- package/skills/weaver-e10-performance/references/default-assembly.md +3 -0
- package/skills/weaver-e10-performance/references/extraction-guide.md +17 -3
- package/skills/weaver-e10-performance/references/flow-assess.md +38 -5
- package/skills/weaver-e10-performance/references/scheme-setting-page.md +20 -3
- package/skills/weaver-e10-performance/scripts/assemble_scheme.py +461 -0
- package/skills/weaver-e10-performance/scripts/parse_appraisal_doc.py +41 -0
- package/skills/weaver-e10-plan/SKILL.md +10 -0
- package/skills/weaver-e10-plan/references/source-manifest.json +13 -13
- package/skills/weaver-e10-qiyecheng/SKILL.md +2 -2
- package/skills/weaver-e10-qiyecheng/references/source-manifest.json +9 -9
- package/skills/weaver-e10-recruit-stage/SKILL.md +188 -169
- package/skills/weaver-e10-recruit-stage/agents/openai.yaml +55 -37
- package/skills/weaver-e10-recruit-stage/lib/interview-conflict.mjs +3 -0
- package/skills/weaver-e10-recruit-stage/lib/interview-summary.mjs +168 -35
- package/skills/weaver-e10-recruit-stage/lib/recruit-shortlist.mjs +175 -7
- package/skills/weaver-e10-recruit-stage/lib/recruit-write-helper.mjs +3 -0
- package/skills/weaver-e10-recruit-stage/lib/recruit-xiaoe-file.mjs +15 -1
- package/skills/weaver-e10-recruit-stage/lib/report-attachment.mjs +38 -0
- package/skills/weaver-e10-recruit-stage/lib/xiaoe-session.mjs +5 -1
- package/skills/weaver-e10-recruit-stage/references/env-fixed-values.md +4 -0
- package/skills/weaver-e10-recruit-stage/references/env-ziyong.md +4 -0
- package/skills/weaver-e10-recruit-stage/references/position-templates.md +12 -8
- package/skills/weaver-e10-recruit-stage/references/recruit-arrange-interview.md +16 -11
- package/skills/weaver-e10-recruit-stage/references/recruit-capability-check.md +10 -6
- package/skills/weaver-e10-recruit-stage/references/recruit-check-install.md +5 -1
- package/skills/weaver-e10-recruit-stage/references/recruit-detail-process.md +4 -0
- package/skills/weaver-e10-recruit-stage/references/recruit-edit-interview-feedback.md +7 -3
- package/skills/weaver-e10-recruit-stage/references/recruit-failure-fallback.md +6 -1
- package/skills/weaver-e10-recruit-stage/references/recruit-interview-not-attended.md +7 -3
- package/skills/weaver-e10-recruit-stage/references/recruit-jd-create.md +31 -23
- package/skills/weaver-e10-recruit-stage/references/recruit-jd-template.md +4 -0
- package/skills/weaver-e10-recruit-stage/references/recruit-list-candidate.md +4 -0
- package/skills/weaver-e10-recruit-stage/references/recruit-list-interview.md +5 -1
- package/skills/weaver-e10-recruit-stage/references/recruit-maint.md +4 -0
- package/skills/weaver-e10-recruit-stage/references/recruit-match-candidates.md +16 -10
- package/skills/weaver-e10-recruit-stage/references/recruit-my-positions.md +6 -2
- package/skills/weaver-e10-recruit-stage/references/recruit-output-links.md +147 -34
- package/skills/weaver-e10-recruit-stage/references/recruit-platform-compat.md +4 -0
- package/skills/weaver-e10-recruit-stage/references/recruit-report-templates.md +16 -4
- package/skills/weaver-e10-recruit-stage/references/recruit-resolve-creator.md +4 -0
- package/skills/weaver-e10-recruit-stage/references/recruit-resolve-metadata.md +8 -4
- package/skills/weaver-e10-recruit-stage/references/recruit-resume-import-batch.md +6 -2
- package/skills/weaver-e10-recruit-stage/references/recruit-resume-import.md +24 -15
- package/skills/weaver-e10-recruit-stage/references/recruit-resume-parse.md +7 -3
- package/skills/weaver-e10-recruit-stage/references/recruit-search-by-tag.md +7 -3
- package/skills/weaver-e10-recruit-stage/references/recruit-search-candidate.md +8 -4
- package/skills/weaver-e10-recruit-stage/references/recruit-tag-recommend.md +5 -1
- package/skills/weaver-e10-recruit-stage/references/recruit-urge-feedback.md +5 -1
- package/skills/weaver-e10-recruit-stage/references/recruit-utterance-glossary.md +5 -1
- package/skills/weaver-e10-recruit-stage/templates/candidate-evidence.html +41 -28
- package/skills/weaver-e10-recruit-stage/templates/interview-summary.html +23 -26
- package/skills/weaver-e10-recruit-stage/templates/match-report.html +32 -30
- package/skills/weaver-e10-shared/SKILL.md +40 -5
- package/skills/weaver-e10-shared/references/e10-auth-and-session.md +11 -1
- package/skills/weaver-e10-shared/references/json-output-contract.md +144 -8
- package/skills/weaver-e10-shared/references/weaver-e10-installation.md +11 -0
- package/skills/weaver-e10-shijingran/references/source-manifest.json +3 -3
- package/skills/weaver-e10-wenshuding/references/source-manifest.json +3 -3
- package/skills/weaver-e10-workflow/references/source-manifest.json +3 -3
- package/skills/weaver-e10-yepiaotong/references/source-manifest.json +5 -5
- package/dist/chunks/chunk-ICP53R52.js +0 -2849
- package/dist/chunks/registry-RIQ67ZT7.js +0 -27
|
@@ -0,0 +1,1400 @@
|
|
|
1
|
+
import { mkdirSync, writeFileSync } from 'node:fs';
|
|
2
|
+
import { dirname } from 'node:path';
|
|
3
|
+
import { contractValidationError } from '../errors.js';
|
|
4
|
+
import { LEDGER_METRICS, findLedgerMetric, } from '../data/metric-catalog.js';
|
|
5
|
+
import { biNumber, buildBiConfig, sanitizeShortKey, } from '../data/bi-config.js';
|
|
6
|
+
import { resolveOperation } from './op-resolver.js';
|
|
7
|
+
/** 各台账的「签订日期」字段(2026-11-28 实测:BI 直接按天返回原值)。 */
|
|
8
|
+
const DATE_FIELDS = {
|
|
9
|
+
直销合同: { fieldId: '846297265322574162', dataKey: 'qdrq', text: '签订日期', shortKey: 'dim', dbFieldType: 'STRING', compType: 'Text' },
|
|
10
|
+
直销业绩: { fieldId: '846297265322574197', dataKey: 'qdrq', text: '签订日期', shortKey: 'dim', dbFieldType: 'STRING', compType: 'Text' },
|
|
11
|
+
渠道订单: { fieldId: '846297265322574307', dataKey: 'qdrq', text: '签订日期', shortKey: 'dim', dbFieldType: 'STRING', compType: 'Text' },
|
|
12
|
+
};
|
|
13
|
+
/** 分类维度字段(只登记已验证过 dataKey 的;未登记的台账明确报错,不猜)。 */
|
|
14
|
+
const CATEGORY_FIELDS = {
|
|
15
|
+
customer: {
|
|
16
|
+
直销合同: { fieldId: '846297265322574105', dataKey: 'khmc', text: '客户名称', shortKey: 'dim', dbFieldType: 'STRING', compType: 'Text' },
|
|
17
|
+
直销业绩: { fieldId: '846297265322574244', dataKey: 'khmc', text: '客户名称', shortKey: 'dim', dbFieldType: 'STRING', compType: 'Text' },
|
|
18
|
+
渠道订单: { fieldId: '846297265322574311', dataKey: 'khmc', text: '客户名称', shortKey: 'dim', dbFieldType: 'STRING', compType: 'Text' },
|
|
19
|
+
},
|
|
20
|
+
salesman: {
|
|
21
|
+
直销合同: { fieldId: '846297265322574070', dataKey: 'xsjl2', text: '销售经理', shortKey: 'dim', dbFieldType: 'STRING', compType: 'Text' },
|
|
22
|
+
渠道订单: { fieldId: '846297265322574329', dataKey: 'xsjl', text: '销售经理', shortKey: 'dim', dbFieldType: 'STRING', compType: 'Text' },
|
|
23
|
+
},
|
|
24
|
+
};
|
|
25
|
+
/**
|
|
26
|
+
* 分组行数说明(2026-11-28 定位结论):
|
|
27
|
+
*
|
|
28
|
+
* 同一份 config / 同一请求体,**返回行数取决于调用方的登录上下文**:
|
|
29
|
+
* 浏览器会话(`ETEAMSID=<PC 扫码会话>`)返回全量(实测客户维度 1309 行);
|
|
30
|
+
* CLI 会话(XiaoE 统一登录)返回图表序列档(实测同一请求 50 行)。
|
|
31
|
+
* 已用 curl 逐项二分确认:仅 `ETEAMSID` 一个 cookie 就能改变行数,config / 请求头 / `use=0` 均无关。
|
|
32
|
+
*
|
|
33
|
+
* 因此**不能**用「行数是否等于某个阈值」判断结果完整性——那是会话差异,不是截断。
|
|
34
|
+
* 完整性只由取数失败与告警表达(见下方 `incomplete` / `warnings`)。
|
|
35
|
+
*
|
|
36
|
+
* 另注:分组结果按**维度值**排序(`orderType:'DESC'` 实测是名称倒序,不是按度量),
|
|
37
|
+
* 所以排行一律由本地按度量排序后再取 TopN。
|
|
38
|
+
*/
|
|
39
|
+
/** 按粒度把「天级原值」收成桶标签:day 原值 / month 前 7 位 / year 前 4 位。 */
|
|
40
|
+
export function bucketByGrain(raw, grain) {
|
|
41
|
+
const text = String(raw ?? '').trim();
|
|
42
|
+
if (!text)
|
|
43
|
+
return '(日期缺失)';
|
|
44
|
+
if (grain === 'day')
|
|
45
|
+
return text;
|
|
46
|
+
if (grain === 'year')
|
|
47
|
+
return text.slice(0, 4);
|
|
48
|
+
return text.slice(0, 7);
|
|
49
|
+
}
|
|
50
|
+
const MONTH_PATTERN = /^(\d{4})-(\d{2})$/u;
|
|
51
|
+
const MAX_MONTHS = 36;
|
|
52
|
+
// ---------------------------------------------------------------- 基础工具
|
|
53
|
+
/** 金额/数值去千分位与货币符号;空值、非数字按 0(与 aggregate 一致)。 */
|
|
54
|
+
export function toAmount(value) {
|
|
55
|
+
if (typeof value === 'number')
|
|
56
|
+
return Number.isFinite(value) ? value : 0;
|
|
57
|
+
if (value === null || value === undefined)
|
|
58
|
+
return 0;
|
|
59
|
+
const text = String(value).replace(/[,,\s¥¥元]/gu, '');
|
|
60
|
+
if (!text)
|
|
61
|
+
return 0;
|
|
62
|
+
const parsed = Number(text);
|
|
63
|
+
return Number.isFinite(parsed) ? parsed : 0;
|
|
64
|
+
}
|
|
65
|
+
export function cellText(value) {
|
|
66
|
+
if (value === null || value === undefined)
|
|
67
|
+
return '';
|
|
68
|
+
if (typeof value === 'string')
|
|
69
|
+
return value.trim();
|
|
70
|
+
if (typeof value === 'number' || typeof value === 'boolean')
|
|
71
|
+
return String(value);
|
|
72
|
+
return '';
|
|
73
|
+
}
|
|
74
|
+
function round2(value) {
|
|
75
|
+
return Number(value.toFixed(2));
|
|
76
|
+
}
|
|
77
|
+
function isRecord(value) {
|
|
78
|
+
return typeof value === 'object' && value !== null && !Array.isArray(value);
|
|
79
|
+
}
|
|
80
|
+
/** `2026-01` + `2026-03` → `["2026-01","2026-02","2026-03"]`(升序、含两端)。 */
|
|
81
|
+
export function expandMonths(from, to) {
|
|
82
|
+
const start = parseMonth(from, 'from');
|
|
83
|
+
const end = parseMonth(to, 'to');
|
|
84
|
+
const months = [];
|
|
85
|
+
let year = start.year;
|
|
86
|
+
let month = start.month;
|
|
87
|
+
while (year < end.year || (year === end.year && month <= end.month)) {
|
|
88
|
+
months.push(`${year}-${String(month).padStart(2, '0')}`);
|
|
89
|
+
month += 1;
|
|
90
|
+
if (month > 12) {
|
|
91
|
+
month = 1;
|
|
92
|
+
year += 1;
|
|
93
|
+
}
|
|
94
|
+
}
|
|
95
|
+
if (months.length > MAX_MONTHS) {
|
|
96
|
+
throw contractValidationError('range_too_wide', `月份跨度 ${months.length} 个月,超过上限 ${MAX_MONTHS} 个月;请分年查询或收窄范围`);
|
|
97
|
+
}
|
|
98
|
+
return months;
|
|
99
|
+
}
|
|
100
|
+
function parseMonth(text, name) {
|
|
101
|
+
const matched = MONTH_PATTERN.exec(String(text).trim());
|
|
102
|
+
if (!matched) {
|
|
103
|
+
throw contractValidationError('month_invalid', `${name} 必须是 YYYY-MM 形式,收到「${text}」`);
|
|
104
|
+
}
|
|
105
|
+
const year = Number(matched[1]);
|
|
106
|
+
const month = Number(matched[2]);
|
|
107
|
+
if (month < 1 || month > 12) {
|
|
108
|
+
throw contractValidationError('month_invalid', `${name} 月份必须在 01~12,收到「${text}」`);
|
|
109
|
+
}
|
|
110
|
+
return { year, month };
|
|
111
|
+
}
|
|
112
|
+
/** 去年同期月份列表(逐月减 1 年)。 */
|
|
113
|
+
export function previousYearMonths(months) {
|
|
114
|
+
return months.map((month) => {
|
|
115
|
+
const { year, month: mm } = parseMonth(month, 'month');
|
|
116
|
+
return `${year - 1}-${String(mm).padStart(2, '0')}`;
|
|
117
|
+
});
|
|
118
|
+
}
|
|
119
|
+
function conditionValue(cond, month, person) {
|
|
120
|
+
if (cond.source === 'fixed')
|
|
121
|
+
return cond.value ?? '';
|
|
122
|
+
if (cond.source === 'period')
|
|
123
|
+
return month ?? '';
|
|
124
|
+
if (cond.source === 'person') {
|
|
125
|
+
if (!person)
|
|
126
|
+
return '';
|
|
127
|
+
if (cond.operator === 'like')
|
|
128
|
+
return person.name;
|
|
129
|
+
// inC 只认人员 ID:没有 ID 时**绝不能退回姓名**(会静默返回全量)
|
|
130
|
+
if (!person.id) {
|
|
131
|
+
throw contractValidationError('person_id_required', `条件「${cond.field}」是 person 字段(inC),必须传 person.id(im.person.resolve 的 uid);`
|
|
132
|
+
+ '只传姓名不会报错但会返回全量,属于比"查不到"更危险的静默失败');
|
|
133
|
+
}
|
|
134
|
+
return person.id;
|
|
135
|
+
}
|
|
136
|
+
return cond.value ?? '';
|
|
137
|
+
}
|
|
138
|
+
export function buildMetricGroups(metrics, person) {
|
|
139
|
+
const groups = new Map();
|
|
140
|
+
for (const metric of metrics) {
|
|
141
|
+
const conditions = metric.conditions.filter((cond) => {
|
|
142
|
+
const source = cond.source ?? 'fixed';
|
|
143
|
+
if (source === 'person' && !person)
|
|
144
|
+
return false;
|
|
145
|
+
return true;
|
|
146
|
+
});
|
|
147
|
+
if (metric.conditions.some((cond) => cond.source === 'person' && cond.required) && !person) {
|
|
148
|
+
throw contractValidationError('person_required', `${metric.label} 必须指定人员(person.id + person.name);请先用 im.person.resolve 把姓名解析成人员 ID`);
|
|
149
|
+
}
|
|
150
|
+
const signature = [
|
|
151
|
+
metric.ledger,
|
|
152
|
+
...conditions
|
|
153
|
+
.filter((cond) => (cond.source ?? 'fixed') === 'fixed')
|
|
154
|
+
.map((cond) => `fixed:${cond.fieldId}:${cond.operator}:${cond.value ?? ''}`),
|
|
155
|
+
...conditions
|
|
156
|
+
.filter((cond) => cond.source === 'person')
|
|
157
|
+
.map((cond) => `person:${cond.fieldId}:${cond.operator}`),
|
|
158
|
+
].join('|');
|
|
159
|
+
const existing = groups.get(signature);
|
|
160
|
+
if (existing) {
|
|
161
|
+
existing.metrics.push(metric);
|
|
162
|
+
continue;
|
|
163
|
+
}
|
|
164
|
+
groups.set(signature, {
|
|
165
|
+
key: signature,
|
|
166
|
+
ledger: metric.ledger,
|
|
167
|
+
listId: metric.listId,
|
|
168
|
+
objId: metric.objId,
|
|
169
|
+
viewKey: metric.viewKey,
|
|
170
|
+
metrics: [metric],
|
|
171
|
+
conditions,
|
|
172
|
+
});
|
|
173
|
+
}
|
|
174
|
+
return [...groups.values()];
|
|
175
|
+
}
|
|
176
|
+
/** 生成 `searchParamJson`(结构固定,与 playbook §2.2 一致)。 */
|
|
177
|
+
export function buildSearchParamJson(conditions, month, person) {
|
|
178
|
+
const commonSearchFields = {};
|
|
179
|
+
for (const cond of conditions) {
|
|
180
|
+
if (!cond.fieldId) {
|
|
181
|
+
throw contractValidationError('field_id_missing', `条件「${cond.field}」没有实测字段 ID,先运行时解析再统计`);
|
|
182
|
+
}
|
|
183
|
+
const value = conditionValue(cond, month, person);
|
|
184
|
+
if (!value) {
|
|
185
|
+
const source = cond.source ?? 'fixed';
|
|
186
|
+
if (source === 'person' && !cond.required)
|
|
187
|
+
continue;
|
|
188
|
+
throw contractValidationError('condition_value_missing', `条件「${cond.field}」取不到值(来源 ${source})`);
|
|
189
|
+
}
|
|
190
|
+
const key = `field_${cond.fieldId}`;
|
|
191
|
+
commonSearchFields[key] = [{ [key]: value, [`${key}_condition_type`]: cond.operator }];
|
|
192
|
+
}
|
|
193
|
+
return JSON.stringify({ searchType: '2', type: 'and', groupFields: {}, commonSearchFields });
|
|
194
|
+
}
|
|
195
|
+
export function createAccumulator() {
|
|
196
|
+
return { sums: {}, sets: {}, rows: 0 };
|
|
197
|
+
}
|
|
198
|
+
function ratioKeys(key) {
|
|
199
|
+
return { num: `${key}::num`, den: `${key}::den` };
|
|
200
|
+
}
|
|
201
|
+
/** 从口径目录里解析「台账 + 字段名 → 字段 ID / 返回键」。 */
|
|
202
|
+
export function resolveFilterField(ledger, field) {
|
|
203
|
+
for (const metric of LEDGER_METRICS) {
|
|
204
|
+
if (metric.ledger !== ledger)
|
|
205
|
+
continue;
|
|
206
|
+
for (const cond of metric.conditions) {
|
|
207
|
+
if (cond.field === field && cond.fieldId && cond.dataKey) {
|
|
208
|
+
return { fieldId: cond.fieldId, dataKey: cond.dataKey };
|
|
209
|
+
}
|
|
210
|
+
}
|
|
211
|
+
if (metric.metricField === field && metric.metricFieldId && metric.metricDataKey) {
|
|
212
|
+
return { fieldId: metric.metricFieldId, dataKey: metric.metricDataKey };
|
|
213
|
+
}
|
|
214
|
+
if (metric.distinct?.field === field && metric.distinct.fieldId && metric.distinct.dataKey) {
|
|
215
|
+
return { fieldId: metric.distinct.fieldId, dataKey: metric.distinct.dataKey };
|
|
216
|
+
}
|
|
217
|
+
}
|
|
218
|
+
return null;
|
|
219
|
+
}
|
|
220
|
+
/** `where` 过滤:`khmc=泛微` 形式,子串匹配;`!=` 表示不含。 */
|
|
221
|
+
export function matchWhere(row, where) {
|
|
222
|
+
for (const raw of where) {
|
|
223
|
+
const text = String(raw).trim();
|
|
224
|
+
if (!text)
|
|
225
|
+
continue;
|
|
226
|
+
const negated = text.includes('!=');
|
|
227
|
+
const [field, ...rest] = text.split(text.includes('!=') ? '!=' : '=');
|
|
228
|
+
const expected = rest.join(text.includes('!=') ? '!=' : '=').trim();
|
|
229
|
+
if (!field?.trim() || !expected)
|
|
230
|
+
continue;
|
|
231
|
+
const actual = cellText(row[field.trim()]);
|
|
232
|
+
const hit = actual.includes(expected);
|
|
233
|
+
if (negated ? hit : !hit)
|
|
234
|
+
return false;
|
|
235
|
+
}
|
|
236
|
+
return true;
|
|
237
|
+
}
|
|
238
|
+
export function accumulate(acc, rows, metrics, where = []) {
|
|
239
|
+
for (const row of rows) {
|
|
240
|
+
if (where.length && !matchWhere(row, where))
|
|
241
|
+
continue;
|
|
242
|
+
acc.rows += 1;
|
|
243
|
+
for (const metric of metrics) {
|
|
244
|
+
if (metric.aggregation === 'sum') {
|
|
245
|
+
const dataKey = metric.metricDataKey ?? '';
|
|
246
|
+
acc.sums[metric.key] = (acc.sums[metric.key] ?? 0) + toAmount(row[dataKey]);
|
|
247
|
+
}
|
|
248
|
+
else if (metric.aggregation === 'countDistinct') {
|
|
249
|
+
const dataKey = metric.distinct?.dataKey ?? metric.metricDataKey ?? '';
|
|
250
|
+
const value = cellText(row[dataKey]);
|
|
251
|
+
if (!value)
|
|
252
|
+
continue;
|
|
253
|
+
const set = acc.sets[metric.key] ?? new Set();
|
|
254
|
+
set.add(value);
|
|
255
|
+
acc.sets[metric.key] = set;
|
|
256
|
+
}
|
|
257
|
+
else if (metric.aggregation === 'ratio') {
|
|
258
|
+
const { num, den } = ratioKeys(metric.key);
|
|
259
|
+
acc.sums[num] = (acc.sums[num] ?? 0) + toAmount(row[metric.ratio?.numerator.dataKey ?? '']);
|
|
260
|
+
acc.sums[den] = (acc.sums[den] ?? 0) + toAmount(row[metric.ratio?.denominator.dataKey ?? '']);
|
|
261
|
+
}
|
|
262
|
+
}
|
|
263
|
+
}
|
|
264
|
+
}
|
|
265
|
+
/** 汇总值:sum→元(2 位小数)、countDistinct→整数、ratio→百分数(2 位小数,分母 0 时 null)。 */
|
|
266
|
+
export function finalize(acc, metrics) {
|
|
267
|
+
const values = {};
|
|
268
|
+
for (const metric of metrics) {
|
|
269
|
+
if (metric.aggregation === 'sum') {
|
|
270
|
+
values[metric.key] = round2(acc.sums[metric.key] ?? 0);
|
|
271
|
+
}
|
|
272
|
+
else if (metric.aggregation === 'countDistinct') {
|
|
273
|
+
values[metric.key] = (acc.sets[metric.key] ?? new Set()).size;
|
|
274
|
+
}
|
|
275
|
+
else if (metric.aggregation === 'ratio') {
|
|
276
|
+
const { num, den } = ratioKeys(metric.key);
|
|
277
|
+
const denominator = acc.sums[den] ?? 0;
|
|
278
|
+
const numerator = acc.sums[num] ?? 0;
|
|
279
|
+
values[metric.key] = denominator === 0 ? null : round2((numerator / denominator) * 100);
|
|
280
|
+
}
|
|
281
|
+
}
|
|
282
|
+
return values;
|
|
283
|
+
}
|
|
284
|
+
/** 把用户入参归一化成可执行规格(纯本地,不取数)。 */
|
|
285
|
+
export function resolveStatSpec(input) {
|
|
286
|
+
const rawMetrics = Array.isArray(input.metrics)
|
|
287
|
+
? input.metrics
|
|
288
|
+
: typeof input.metric === 'string' && input.metric
|
|
289
|
+
? [input.metric]
|
|
290
|
+
: [];
|
|
291
|
+
const keys = rawMetrics.map((item) => String(item).trim()).filter(Boolean);
|
|
292
|
+
if (keys.length === 0) {
|
|
293
|
+
throw contractValidationError('metrics_required', `必须指定 metrics;可用口径:${LEDGER_METRICS.map((item) => `${item.key}(${item.label})`).join('、')}`);
|
|
294
|
+
}
|
|
295
|
+
const metrics = [];
|
|
296
|
+
for (const key of keys) {
|
|
297
|
+
const metric = findLedgerMetric(key);
|
|
298
|
+
if (!metric) {
|
|
299
|
+
throw contractValidationError('metric_unknown', `未知统计口径「${key}」;可用:${LEDGER_METRICS.map((item) => `${item.key}(${item.label})`).join('、')}`);
|
|
300
|
+
}
|
|
301
|
+
if (!metrics.some((item) => item.key === metric.key))
|
|
302
|
+
metrics.push(metric);
|
|
303
|
+
}
|
|
304
|
+
const explicitPeriods = Array.isArray(input.periods) ? input.periods.map((item) => String(item).trim()) : [];
|
|
305
|
+
let months;
|
|
306
|
+
if (explicitPeriods.length) {
|
|
307
|
+
months = explicitPeriods.map((month) => {
|
|
308
|
+
parseMonth(month, 'periods');
|
|
309
|
+
return month;
|
|
310
|
+
}).sort();
|
|
311
|
+
}
|
|
312
|
+
else {
|
|
313
|
+
const from = typeof input.from === 'string' ? input.from.trim() : '';
|
|
314
|
+
const to = typeof input.to === 'string' ? input.to.trim() : '';
|
|
315
|
+
if (!from || !to) {
|
|
316
|
+
throw contractValidationError('range_required', '必须指定 from / to(YYYY-MM),或显式传 periods');
|
|
317
|
+
}
|
|
318
|
+
months = expandMonths(from, to);
|
|
319
|
+
}
|
|
320
|
+
const compareRaw = typeof input.compare === 'string' ? input.compare.trim() : 'none';
|
|
321
|
+
if (!['yoy', 'none'].includes(compareRaw)) {
|
|
322
|
+
throw contractValidationError('compare_invalid', 'compare 只支持 yoy(去年同期)或 none');
|
|
323
|
+
}
|
|
324
|
+
const compare = compareRaw;
|
|
325
|
+
const personRaw = input.person;
|
|
326
|
+
const where = Array.isArray(input.where) ? input.where.map((item) => String(item)) : [];
|
|
327
|
+
// 服务端过滤:如 合同种类=新签合同(可用来算「新签合同的签约总额 / 回款率」)
|
|
328
|
+
const filters = [];
|
|
329
|
+
if (input.filters !== undefined) {
|
|
330
|
+
if (!Array.isArray(input.filters)) {
|
|
331
|
+
throw contractValidationError('filters_invalid', 'filters 必须是数组');
|
|
332
|
+
}
|
|
333
|
+
input.filters.forEach((raw, index) => {
|
|
334
|
+
const at = `filters[${index}]`;
|
|
335
|
+
if (!isRecord(raw))
|
|
336
|
+
throw contractValidationError('filters_invalid', `${at} 必须是对象`);
|
|
337
|
+
const value = String(raw.value ?? '').trim();
|
|
338
|
+
if (!value)
|
|
339
|
+
throw contractValidationError('filters_invalid', `${at}.value 必填`);
|
|
340
|
+
const operator = raw.operator === undefined ? 'eq' : String(raw.operator);
|
|
341
|
+
if (!['eq', 'like', 'startWith'].includes(operator)) {
|
|
342
|
+
throw contractValidationError('filters_invalid', `${at}.operator 只支持 eq / like / startWith`);
|
|
343
|
+
}
|
|
344
|
+
const field = raw.field === undefined ? undefined : String(raw.field).trim();
|
|
345
|
+
const fieldId = raw.fieldId === undefined ? undefined : String(raw.fieldId).trim();
|
|
346
|
+
const dataKey = raw.dataKey === undefined ? undefined : String(raw.dataKey).trim();
|
|
347
|
+
if (!field && !fieldId) {
|
|
348
|
+
throw contractValidationError('filters_invalid', `${at} 至少要给 field(字段中文名)或 fieldId`);
|
|
349
|
+
}
|
|
350
|
+
filters.push({
|
|
351
|
+
...(field ? { field } : {}),
|
|
352
|
+
...(fieldId ? { fieldId } : {}),
|
|
353
|
+
...(dataKey ? { dataKey } : {}),
|
|
354
|
+
operator: operator,
|
|
355
|
+
value,
|
|
356
|
+
});
|
|
357
|
+
});
|
|
358
|
+
}
|
|
359
|
+
// `where` 是**本地**过滤,BI 引擎在服务端算完聚合后拿不到原始行 → 必须拒绝而不是静默忽略
|
|
360
|
+
// (检查放在 engine 解析之后)
|
|
361
|
+
// 多人员:people 与 person 合并成一个列表(people 在内层 OR 成「A 或 B」)
|
|
362
|
+
const people = [];
|
|
363
|
+
const pushPerson = (raw, label) => {
|
|
364
|
+
if (!raw || typeof raw !== 'object' || Array.isArray(raw)) {
|
|
365
|
+
throw contractValidationError('person_invalid', `${label} 必须是 { id, name } 或 { name } 对象`);
|
|
366
|
+
}
|
|
367
|
+
const record = raw;
|
|
368
|
+
const id = String(record.id ?? '').trim();
|
|
369
|
+
const name = String(record.name ?? '').trim();
|
|
370
|
+
if (!name)
|
|
371
|
+
throw contractValidationError('person_name_invalid', `${label}.name 必填`);
|
|
372
|
+
if (id && !/^\d{6,32}$/u.test(id)) {
|
|
373
|
+
throw contractValidationError('person_id_invalid', `${label}.id 必须是人员 ID(im.person.resolve 的 uid)`);
|
|
374
|
+
}
|
|
375
|
+
if (people.some((item) => item.name === name && item.id === (id || null)))
|
|
376
|
+
return;
|
|
377
|
+
people.push({ id: id || null, name });
|
|
378
|
+
};
|
|
379
|
+
if (personRaw !== undefined && personRaw !== null && personRaw !== '')
|
|
380
|
+
pushPerson(personRaw, 'person');
|
|
381
|
+
if (input.people !== undefined) {
|
|
382
|
+
if (!Array.isArray(input.people))
|
|
383
|
+
throw contractValidationError('person_invalid', 'people 必须是数组');
|
|
384
|
+
input.people.forEach((raw, index) => pushPerson(raw, `people[${index}]`));
|
|
385
|
+
}
|
|
386
|
+
const primaryPerson = people[0] ?? null;
|
|
387
|
+
const engineRaw = typeof input.engine === 'string' ? input.engine.trim() : 'rows';
|
|
388
|
+
if (!['rows', 'bi'].includes(engineRaw)) {
|
|
389
|
+
throw contractValidationError('engine_invalid', 'engine 只支持 rows(明细分页)或 bi(服务端聚合)');
|
|
390
|
+
}
|
|
391
|
+
const engine = engineRaw;
|
|
392
|
+
let bi = null;
|
|
393
|
+
if (engine === 'bi') {
|
|
394
|
+
const raw = (isRecord(input.bi) ? input.bi : {});
|
|
395
|
+
const pageId = String(raw.pageId ?? input.biPageId ?? '').trim();
|
|
396
|
+
const dataSetId = String(raw.dataSetId ?? input.biDataSetId ?? '').trim();
|
|
397
|
+
const analyzeId = String(raw.analyzeId ?? input.biAnalyzeId ?? '').trim();
|
|
398
|
+
const datasetAppId = String(raw.datasetAppId ?? input.biDatasetAppId ?? '').trim() || undefined;
|
|
399
|
+
if (!pageId || !dataSetId || !analyzeId) {
|
|
400
|
+
throw contractValidationError('bi_context_required', 'engine=bi 必须提供 bi.pageId / bi.dataSetId / bi.analyzeId(三者都在 BI 组件预览请求的 URL 与表单里,'
|
|
401
|
+
+ '例如 pageId 与 dataSetId、analyzeId;缺任何一个都取不到聚合)。也可用 engine=rows 走明细聚合。');
|
|
402
|
+
}
|
|
403
|
+
bi = { pageId, dataSetId, analyzeId, ...(datasetAppId ? { datasetAppId } : {}) };
|
|
404
|
+
}
|
|
405
|
+
// `where` 是本地过滤,BI 引擎在服务端算完聚合后拿不到原始行 → 必须拒绝,不能静默忽略
|
|
406
|
+
if (engine === 'bi' && where.length) {
|
|
407
|
+
throw contractValidationError('where_unsupported_on_bi', 'engine=bi 时 where(本地过滤)无法生效(聚合在服务端完成);请改用 filters(服务端过滤),'
|
|
408
|
+
+ '例如 filters:[{"field":"合同种类","value":"新签合同"}],或改用 engine=rows');
|
|
409
|
+
}
|
|
410
|
+
// 维度:BI 默认按「签订日期」(2026-11-28 用户口径:合同时间维度用签订日期,天=原值);
|
|
411
|
+
// rows 引擎只会按「合同业绩年月」分桶,默认保持 month 以向后兼容。
|
|
412
|
+
const dimensionRaw = typeof input.dimension === 'string' && input.dimension.trim()
|
|
413
|
+
? input.dimension.trim()
|
|
414
|
+
: (engine === 'bi' ? 'date' : 'month');
|
|
415
|
+
if (!['date', 'month', 'customer', 'salesman'].includes(dimensionRaw)) {
|
|
416
|
+
throw contractValidationError('dimension_invalid', 'dimension 只支持 date(签订日期)/ month(合同业绩年月)/ customer(客户)/ salesman(销售经理)');
|
|
417
|
+
}
|
|
418
|
+
const dimension = dimensionRaw;
|
|
419
|
+
const grainRaw = typeof input.grain === 'string' && input.grain.trim() ? input.grain.trim() : 'month';
|
|
420
|
+
if (!['day', 'month', 'year'].includes(grainRaw)) {
|
|
421
|
+
throw contractValidationError('grain_invalid', 'grain 只支持 day / month / year');
|
|
422
|
+
}
|
|
423
|
+
const grain = grainRaw;
|
|
424
|
+
// 多人员只能走 BI:明细引擎的 searchParamJson 是 AND,拼不出「A 或 B」
|
|
425
|
+
if (engine === 'rows' && people.length > 1) {
|
|
426
|
+
throw contractValidationError('multi_person_requires_bi', `people 有 ${people.length} 人:明细引擎的条件只能是 AND,拼不出「A 或 B」;请改用 engine=bi(BI 用嵌套 OR 组),或逐人分别统计后相加`);
|
|
427
|
+
}
|
|
428
|
+
// 明细引擎只会按「合同业绩年月」分桶;换维度必须走 BI
|
|
429
|
+
if (engine === 'rows' && dimension !== 'month') {
|
|
430
|
+
throw contractValidationError('dimension_requires_bi', `dimension=${dimension} 需要 BI 引擎(按签订日期/客户/销售经理分组);rows 引擎只支持 dimension=month(合同业绩年月)`);
|
|
431
|
+
}
|
|
432
|
+
return {
|
|
433
|
+
metrics,
|
|
434
|
+
person: primaryPerson,
|
|
435
|
+
people,
|
|
436
|
+
where,
|
|
437
|
+
filters,
|
|
438
|
+
fetch: input.fetch !== false && input.dryRun !== true,
|
|
439
|
+
engine,
|
|
440
|
+
bi,
|
|
441
|
+
dimension,
|
|
442
|
+
grain,
|
|
443
|
+
window: {
|
|
444
|
+
from: months[0],
|
|
445
|
+
to: months[months.length - 1],
|
|
446
|
+
months,
|
|
447
|
+
compare,
|
|
448
|
+
previousMonths: compare === 'yoy' ? previousYearMonths(months) : [],
|
|
449
|
+
dimension,
|
|
450
|
+
grain,
|
|
451
|
+
},
|
|
452
|
+
};
|
|
453
|
+
}
|
|
454
|
+
function planEntry(month, previousYear, group, person) {
|
|
455
|
+
return {
|
|
456
|
+
month,
|
|
457
|
+
previousYear,
|
|
458
|
+
ledger: group.ledger,
|
|
459
|
+
listId: group.listId,
|
|
460
|
+
objId: group.objId,
|
|
461
|
+
metrics: group.metrics.map((metric) => metric.key),
|
|
462
|
+
conditions: group.conditions.map((cond) => ({
|
|
463
|
+
field: cond.field,
|
|
464
|
+
fieldId: cond.fieldId ?? '',
|
|
465
|
+
operator: cond.operator,
|
|
466
|
+
value: conditionValue(cond, month, person),
|
|
467
|
+
})),
|
|
468
|
+
searchParamJson: buildSearchParamJson(group.conditions, month, person),
|
|
469
|
+
};
|
|
470
|
+
}
|
|
471
|
+
/**
|
|
472
|
+
* 执行统计(取数与聚合全在本地完成)。
|
|
473
|
+
*
|
|
474
|
+
* `fetcher` 由 operation 注入 `contract.performance.list.query`;测试可注入假实现,
|
|
475
|
+
* 因此本函数是纯函数式的(除 fetcher 外无副作用),可直接断言计划与数值。
|
|
476
|
+
*/
|
|
477
|
+
export async function runStat(spec, fetcher) {
|
|
478
|
+
const { metrics, window: win, person, where, fetch } = spec;
|
|
479
|
+
const groups = buildMetricGroups(metrics, person);
|
|
480
|
+
// filters 在明细引擎里转为本地过滤(语义与 BI 下推一致:等值/子串)
|
|
481
|
+
const whereByLedger = new Map();
|
|
482
|
+
for (const group of groups) {
|
|
483
|
+
const extra = [];
|
|
484
|
+
for (const filter of spec.filters ?? []) {
|
|
485
|
+
const dataKey = filter.dataKey
|
|
486
|
+
?? (filter.field ? resolveFilterField(group.ledger, filter.field)?.dataKey : undefined);
|
|
487
|
+
if (!dataKey) {
|
|
488
|
+
throw contractValidationError('filter_field_unknown', `过滤条件「${filter.field ?? filter.value}」在台账「${group.ledger}」里解析不到返回键;`
|
|
489
|
+
+ '请给出口径目录中已有的字段名,或显式传 dataKey');
|
|
490
|
+
}
|
|
491
|
+
extra.push(`${dataKey}=${filter.value}`);
|
|
492
|
+
}
|
|
493
|
+
whereByLedger.set(group.ledger, [...where, ...extra]);
|
|
494
|
+
}
|
|
495
|
+
const plan = [];
|
|
496
|
+
for (const month of win.months) {
|
|
497
|
+
for (const group of groups)
|
|
498
|
+
plan.push(planEntry(month, false, group, person));
|
|
499
|
+
}
|
|
500
|
+
for (const month of win.previousMonths) {
|
|
501
|
+
for (const group of groups)
|
|
502
|
+
plan.push(planEntry(month, true, group, person));
|
|
503
|
+
}
|
|
504
|
+
const warnings = [];
|
|
505
|
+
const incompleteMetrics = new Set();
|
|
506
|
+
const totalsAcc = createAccumulator();
|
|
507
|
+
const previousAcc = createAccumulator();
|
|
508
|
+
const periods = [];
|
|
509
|
+
const previousPeriods = [];
|
|
510
|
+
let executed = 0;
|
|
511
|
+
let failed = 0;
|
|
512
|
+
const notes = [...new Set(metrics.map((metric) => metric.note))];
|
|
513
|
+
const payloadBase = {
|
|
514
|
+
window: win,
|
|
515
|
+
metrics: metrics.map((metric) => ({
|
|
516
|
+
key: metric.key,
|
|
517
|
+
label: metric.label,
|
|
518
|
+
ledger: metric.ledger,
|
|
519
|
+
aggregation: metric.aggregation,
|
|
520
|
+
note: metric.note,
|
|
521
|
+
})),
|
|
522
|
+
groups: groups.map((group) => ({
|
|
523
|
+
ledger: group.ledger,
|
|
524
|
+
metrics: group.metrics.map((metric) => metric.key),
|
|
525
|
+
conditions: group.conditions.map((cond) => `${cond.field} ${cond.operator} ${conditionValue(cond, '<月>', person) || '<人员>'}`),
|
|
526
|
+
})),
|
|
527
|
+
notes,
|
|
528
|
+
};
|
|
529
|
+
if (!fetch) {
|
|
530
|
+
return {
|
|
531
|
+
...payloadBase,
|
|
532
|
+
plan,
|
|
533
|
+
periods: [],
|
|
534
|
+
previousPeriods: [],
|
|
535
|
+
series: win.months.map((month) => ({
|
|
536
|
+
month,
|
|
537
|
+
values: {},
|
|
538
|
+
previousYear: null,
|
|
539
|
+
yoy: {},
|
|
540
|
+
})),
|
|
541
|
+
totals: {},
|
|
542
|
+
previousTotals: {},
|
|
543
|
+
compare: {},
|
|
544
|
+
summary: { planned: plan.length, executed: 0, failed: 0 },
|
|
545
|
+
incompleteMetrics: [],
|
|
546
|
+
warnings: ['dryRun:只输出查询计划,未取数(fetch=false)。'],
|
|
547
|
+
};
|
|
548
|
+
}
|
|
549
|
+
const runMonth = async (month, accumulator) => {
|
|
550
|
+
const values = {};
|
|
551
|
+
const queries = [];
|
|
552
|
+
let incomplete = false;
|
|
553
|
+
for (const group of groups) {
|
|
554
|
+
const forPrevious = accumulator === previousAcc;
|
|
555
|
+
const entry = plan.find((item) => item.month === month
|
|
556
|
+
&& item.ledger === group.ledger
|
|
557
|
+
&& item.previousYear === forPrevious
|
|
558
|
+
&& item.metrics.join(',') === group.metrics.map((metric) => metric.key).join(','));
|
|
559
|
+
if (!entry)
|
|
560
|
+
continue;
|
|
561
|
+
let result;
|
|
562
|
+
try {
|
|
563
|
+
result = await fetcher({
|
|
564
|
+
month,
|
|
565
|
+
ledger: group.ledger,
|
|
566
|
+
listId: group.listId,
|
|
567
|
+
objId: group.objId,
|
|
568
|
+
metrics: group.metrics,
|
|
569
|
+
searchParamJson: entry.searchParamJson,
|
|
570
|
+
});
|
|
571
|
+
executed += 1;
|
|
572
|
+
}
|
|
573
|
+
catch (error) {
|
|
574
|
+
failed += 1;
|
|
575
|
+
const message = error instanceof Error ? error.message : String(error);
|
|
576
|
+
const type = error?.type;
|
|
577
|
+
if (type === 'auth')
|
|
578
|
+
throw error;
|
|
579
|
+
incomplete = true;
|
|
580
|
+
for (const metric of group.metrics)
|
|
581
|
+
incompleteMetrics.add(metric.key);
|
|
582
|
+
queries.push({ ledger: group.ledger, fetched: 0, total: null, truncated: true, error: message });
|
|
583
|
+
warnings.push(`${month} ${group.ledger} 取数失败:${message}`);
|
|
584
|
+
for (const metric of group.metrics)
|
|
585
|
+
values[metric.key] = null;
|
|
586
|
+
continue;
|
|
587
|
+
}
|
|
588
|
+
const groupAcc = createAccumulator();
|
|
589
|
+
accumulate(groupAcc, result.rows, group.metrics, whereByLedger.get(group.ledger) ?? where);
|
|
590
|
+
const groupValues = finalize(groupAcc, group.metrics);
|
|
591
|
+
const truncated = result.truncated || (result.total !== null && result.rows.length < result.total);
|
|
592
|
+
queries.push({
|
|
593
|
+
ledger: group.ledger,
|
|
594
|
+
fetched: result.rows.length,
|
|
595
|
+
total: result.total,
|
|
596
|
+
truncated,
|
|
597
|
+
});
|
|
598
|
+
if (truncated) {
|
|
599
|
+
incomplete = true;
|
|
600
|
+
for (const metric of group.metrics) {
|
|
601
|
+
incompleteMetrics.add(metric.key);
|
|
602
|
+
values[metric.key] = null;
|
|
603
|
+
}
|
|
604
|
+
warnings.push(`${month} ${group.ledger} 命中 ${result.total ?? '未知'} 条、仅取回 ${result.rows.length} 条(truncated),`
|
|
605
|
+
+ `该窗口指标已置空;请按人员 / 机构 / 客户收窄后重跑,或提高 pageSize×maxPages。`);
|
|
606
|
+
continue;
|
|
607
|
+
}
|
|
608
|
+
for (const [key, value] of Object.entries(groupValues))
|
|
609
|
+
values[key] = value;
|
|
610
|
+
accumulate(accumulator, result.rows, group.metrics, whereByLedger.get(group.ledger) ?? where);
|
|
611
|
+
}
|
|
612
|
+
return { month, values, queries, incomplete };
|
|
613
|
+
};
|
|
614
|
+
for (const month of win.months)
|
|
615
|
+
periods.push(await runMonth(month, totalsAcc));
|
|
616
|
+
for (const month of win.previousMonths)
|
|
617
|
+
previousPeriods.push(await runMonth(month, previousAcc));
|
|
618
|
+
const totals = finalize(totalsAcc, metrics);
|
|
619
|
+
const previousTotals = finalize(previousAcc, metrics);
|
|
620
|
+
for (const key of incompleteMetrics) {
|
|
621
|
+
totals[key] = null;
|
|
622
|
+
previousTotals[key] = null;
|
|
623
|
+
}
|
|
624
|
+
return assembleStatPayload({
|
|
625
|
+
spec,
|
|
626
|
+
payloadBase,
|
|
627
|
+
plan,
|
|
628
|
+
periods,
|
|
629
|
+
previousPeriods,
|
|
630
|
+
totals,
|
|
631
|
+
previousTotals,
|
|
632
|
+
summary: { planned: plan.length, executed, failed },
|
|
633
|
+
incompleteMetrics: [...incompleteMetrics],
|
|
634
|
+
warnings,
|
|
635
|
+
});
|
|
636
|
+
}
|
|
637
|
+
/** 两种引擎共用的收口逻辑:同比、逐月序列、告警。保证 rows / bi 输出结构完全一致。 */
|
|
638
|
+
export function assembleStatPayload(input) {
|
|
639
|
+
const { spec, payloadBase, plan, periods, previousPeriods, totals, previousTotals, warnings } = input;
|
|
640
|
+
const metrics = spec.metrics;
|
|
641
|
+
const win = spec.window;
|
|
642
|
+
const compare = {};
|
|
643
|
+
for (const metric of metrics) {
|
|
644
|
+
const current = totals[metric.key] ?? null;
|
|
645
|
+
const previous = previousTotals[metric.key] ?? null;
|
|
646
|
+
const delta = current !== null && previous !== null ? round2(current - previous) : null;
|
|
647
|
+
const pct = current !== null && previous !== null && previous !== 0
|
|
648
|
+
? round2(((current - previous) / previous) * 100)
|
|
649
|
+
: null;
|
|
650
|
+
compare[metric.key] = { current, previous, delta, pct };
|
|
651
|
+
}
|
|
652
|
+
const previousByMonth = new Map(previousPeriods.map((item) => [item.month, item]));
|
|
653
|
+
const series = periods.map((period, index) => {
|
|
654
|
+
const previousMonth = win.previousMonths[index] ?? null;
|
|
655
|
+
const previous = previousMonth ? previousByMonth.get(previousMonth) ?? null : null;
|
|
656
|
+
const yoy = {};
|
|
657
|
+
for (const metric of metrics) {
|
|
658
|
+
const current = period.values[metric.key] ?? null;
|
|
659
|
+
const prev = previous?.values[metric.key] ?? null;
|
|
660
|
+
yoy[metric.key] = current !== null && prev !== null && prev !== 0
|
|
661
|
+
? round2(((current - prev) / prev) * 100)
|
|
662
|
+
: null;
|
|
663
|
+
}
|
|
664
|
+
return {
|
|
665
|
+
month: period.month,
|
|
666
|
+
values: period.values,
|
|
667
|
+
previousYear: previous ? { month: previous.month, values: previous.values } : null,
|
|
668
|
+
yoy,
|
|
669
|
+
};
|
|
670
|
+
});
|
|
671
|
+
if (periods.length > 0 && periods.every((period) => period.incomplete)) {
|
|
672
|
+
warnings.push('所有月份均不完整,结论不可用;请先收窄范围再重跑。');
|
|
673
|
+
}
|
|
674
|
+
return {
|
|
675
|
+
...payloadBase,
|
|
676
|
+
plan,
|
|
677
|
+
periods,
|
|
678
|
+
previousPeriods,
|
|
679
|
+
series,
|
|
680
|
+
totals,
|
|
681
|
+
previousTotals,
|
|
682
|
+
compare,
|
|
683
|
+
summary: input.summary,
|
|
684
|
+
incompleteMetrics: input.incompleteMetrics,
|
|
685
|
+
warnings,
|
|
686
|
+
};
|
|
687
|
+
}
|
|
688
|
+
function yearsOf(months) {
|
|
689
|
+
return [...new Set(months.map((month) => month.slice(0, 4)))].sort();
|
|
690
|
+
}
|
|
691
|
+
function toBiCondition(cond, person) {
|
|
692
|
+
const value = conditionValue(cond, null, person);
|
|
693
|
+
const operator = cond.operator === 'inC' ? 'eq' : cond.operator;
|
|
694
|
+
return { field: cond.field, fieldId: cond.fieldId, operator, value };
|
|
695
|
+
}
|
|
696
|
+
/**
|
|
697
|
+
* BI 引擎:服务端聚合,不拉明细。
|
|
698
|
+
*
|
|
699
|
+
* 请求形态(实测最优):
|
|
700
|
+
* - **逐月**:`dimensions=[年月]`,一次拿整年(跨年则按年各一次);
|
|
701
|
+
* - **窗口合计**:`dimensions=[]`,一次拿该年合计;跨年按年累加(SUM/比率分子分母可加);
|
|
702
|
+
* - **去重计数**:逐月用 `DISTINCTCOUNT`;合计用「按去重字段分组 + COUNT」,把多行并集交给本地去重
|
|
703
|
+
* (SUM 可加、去重不可加,这是唯一能保证「整窗去重」的口径)。
|
|
704
|
+
*
|
|
705
|
+
* 边界:person 字段(`inC`)在 BI 里未验证,遇到直接报错要求改用 rows 引擎;
|
|
706
|
+
* 分组不补 0,缺失月份由本地按区间补零(sum/计数补 0,比率补 null)。
|
|
707
|
+
*/
|
|
708
|
+
export async function runStatViaBi(spec, bi, call, fetchEnabled = true) {
|
|
709
|
+
const { metrics, window: win, person } = spec;
|
|
710
|
+
const groups = buildMetricGroups(metrics, person);
|
|
711
|
+
const isCategoryAll = spec.dimension === 'customer' || spec.dimension === 'salesman';
|
|
712
|
+
const warnings = [];
|
|
713
|
+
const incomplete = new Set();
|
|
714
|
+
const notes = [...new Set(metrics.map((metric) => metric.note))];
|
|
715
|
+
/**
|
|
716
|
+
* 发请求前的前置校验:分类维度下「分组字段 = 被去重字段」的组合必须立刻拒绝,
|
|
717
|
+
* 不能等跑到那个分组才发现(前面分组已经白发了请求)。
|
|
718
|
+
* 判据只比字段 ID:按客户分组数客户恒为 1;按销售经理分组数客户有意义。
|
|
719
|
+
*/
|
|
720
|
+
if (spec.dimension !== 'date' && spec.dimension !== 'month') {
|
|
721
|
+
const dimFieldId = CATEGORY_FIELDS[spec.dimension]?.[groups[0]?.ledger ?? '']?.fieldId
|
|
722
|
+
?? Object.values(CATEGORY_FIELDS[spec.dimension] ?? {}).find(Boolean)?.fieldId;
|
|
723
|
+
for (const group of groups) {
|
|
724
|
+
const groupDimFieldId = CATEGORY_FIELDS[spec.dimension]?.[group.ledger]?.fieldId ?? dimFieldId;
|
|
725
|
+
for (const metric of group.metrics) {
|
|
726
|
+
if (metric.aggregation !== 'countDistinct')
|
|
727
|
+
continue;
|
|
728
|
+
const distinctFieldId = metric.distinct?.fieldId ?? metric.metricFieldId;
|
|
729
|
+
if (groupDimFieldId && distinctFieldId === groupDimFieldId) {
|
|
730
|
+
throw contractValidationError('dimension_distinct_unsupported', `dimension=${spec.dimension} 的分组字段就是「${metric.distinct?.field ?? metric.metricField}」,`
|
|
731
|
+
+ `再对它去重计数每行恒为 1(没有信息量);请改用 dimension=date/month,`
|
|
732
|
+
+ `或换用「按销售经理分组」这类与被去重字段不同的维度`);
|
|
733
|
+
}
|
|
734
|
+
}
|
|
735
|
+
}
|
|
736
|
+
}
|
|
737
|
+
const payloadBase = {
|
|
738
|
+
window: win,
|
|
739
|
+
engine: 'bi',
|
|
740
|
+
metrics: metrics.map((metric) => ({
|
|
741
|
+
key: metric.key,
|
|
742
|
+
label: metric.label,
|
|
743
|
+
ledger: metric.ledger,
|
|
744
|
+
aggregation: metric.aggregation,
|
|
745
|
+
note: metric.note,
|
|
746
|
+
})),
|
|
747
|
+
groups: groups.map((group) => ({
|
|
748
|
+
ledger: group.ledger,
|
|
749
|
+
metrics: group.metrics.map((metric) => metric.key),
|
|
750
|
+
conditions: group.conditions.map((cond) => `${cond.field} ${cond.operator} ${conditionValue(cond, '<月>', person) || '<人员>'}`),
|
|
751
|
+
})),
|
|
752
|
+
notes,
|
|
753
|
+
};
|
|
754
|
+
let executed = 0;
|
|
755
|
+
let failed = 0;
|
|
756
|
+
const configs = [];
|
|
757
|
+
/** 本期 / 去年同期分两张桶表:分类维度的桶标签相同,混在一张表里会把去年加进今年。 */
|
|
758
|
+
const currentBuckets = new Map();
|
|
759
|
+
const prevBuckets = new Map();
|
|
760
|
+
/** 比率的分子/分母按桶留底:合计必须由窗口内桶累加,不能按整年取。 */
|
|
761
|
+
const currentNumDen = new Map();
|
|
762
|
+
const prevNumDen = new Map();
|
|
763
|
+
const totalsCurrent = {};
|
|
764
|
+
const totalsPrevious = {};
|
|
765
|
+
for (const group of groups) {
|
|
766
|
+
// person 字段(inC)在 BI 里同样按「人员 ID + eq」传(2026-11-28 用户口径:人员浏览按钮的
|
|
767
|
+
// 值就是 ID)。toBiCondition 已把 inC 映射成 eq,这里只需保证 ID 一定在。
|
|
768
|
+
const personConds = group.conditions.filter((cond) => cond.source === 'person');
|
|
769
|
+
const people = spec.people.length ? spec.people : (person ? [person] : []);
|
|
770
|
+
for (const cond of personConds) {
|
|
771
|
+
if (cond.operator === 'inC' && people.some((item) => !item.id)) {
|
|
772
|
+
throw contractValidationError('bi_person_id_required', `${cond.field} 是人员字段,BI 过滤要用人员 ID(person.id / people[].id,即 im.person.resolve 的 uid);只给姓名会取到全量`);
|
|
773
|
+
}
|
|
774
|
+
}
|
|
775
|
+
const period = group.conditions.find((cond) => cond.source === 'period');
|
|
776
|
+
if (!period?.fieldId || !period.dataKey) {
|
|
777
|
+
throw contractValidationError('bi_period_field_missing', 'BI 引擎要求口径带「期间字段」(含字段 ID 与返回键),当前口径缺这两项');
|
|
778
|
+
}
|
|
779
|
+
const periodField = {
|
|
780
|
+
fieldId: period.fieldId,
|
|
781
|
+
dataKey: period.dataKey,
|
|
782
|
+
text: period.field,
|
|
783
|
+
shortKey: 'dim',
|
|
784
|
+
dbFieldType: 'STRING',
|
|
785
|
+
compType: 'Text',
|
|
786
|
+
};
|
|
787
|
+
// 维度三选一:签订日期(时间)/ 业绩年月(时间)/ 分类字段
|
|
788
|
+
const isCategory = spec.dimension === 'customer' || spec.dimension === 'salesman';
|
|
789
|
+
let dim;
|
|
790
|
+
let filterField;
|
|
791
|
+
if (spec.dimension === 'date') {
|
|
792
|
+
const dateField = DATE_FIELDS[group.ledger];
|
|
793
|
+
if (!dateField) {
|
|
794
|
+
throw contractValidationError('dimension_unsupported', `台账「${group.ledger}」还没有登记「签订日期」字段(只有 ${Object.keys(DATE_FIELDS).join('、')} 已验证);请改用 dimension=month`);
|
|
795
|
+
}
|
|
796
|
+
dim = dateField;
|
|
797
|
+
filterField = dateField;
|
|
798
|
+
}
|
|
799
|
+
else if (spec.dimension === 'month') {
|
|
800
|
+
dim = periodField;
|
|
801
|
+
filterField = periodField;
|
|
802
|
+
}
|
|
803
|
+
else {
|
|
804
|
+
const catField = CATEGORY_FIELDS[spec.dimension][group.ledger];
|
|
805
|
+
if (!catField) {
|
|
806
|
+
throw contractValidationError('dimension_unsupported', `台账「${group.ledger}」还没有登记「${spec.dimension === 'customer' ? '客户名称' : '销售经理'}」维度字段;`
|
|
807
|
+
+ `只有 ${Object.keys(CATEGORY_FIELDS[spec.dimension]).join('、')} 已验证`);
|
|
808
|
+
}
|
|
809
|
+
dim = catField;
|
|
810
|
+
filterField = periodField;
|
|
811
|
+
}
|
|
812
|
+
// 分类维度没有时间桶,窗口必须能**整年**落到期间条件上,否则会串进窗口外月份。
|
|
813
|
+
// 允许两种形态(与页面入参一致):① 单月窗口 → `年月 eq <YYYY-MM>`;② 整年窗口 → `年月 like <YYYY>`。
|
|
814
|
+
// 其它形态(如 1~11 月)会多算 12 月,明确拒绝而不猜。
|
|
815
|
+
const isSingleMonth = win.months.length === 1;
|
|
816
|
+
const coversFullYear = (year) => {
|
|
817
|
+
const months = win.months.filter((month) => month.startsWith(`${year}-`));
|
|
818
|
+
return months.length === 12;
|
|
819
|
+
};
|
|
820
|
+
if (isCategory && !isSingleMonth) {
|
|
821
|
+
const badYear = yearsOf(win.months).find((year) => !coversFullYear(year));
|
|
822
|
+
if (badYear) {
|
|
823
|
+
throw contractValidationError('dimension_partial_window_unsupported', `dimension=${spec.dimension}(分类维度)只支持「单月窗口」或「整年窗口」(YYYY-01 ~ YYYY-12):`
|
|
824
|
+
+ `当前窗口在 ${badYear} 年不是整年,条件落到「合同业绩年月」上会多算窗口外月份。`
|
|
825
|
+
+ `请把 from/to 对齐单月或整年,或改用 dimension=date 按天/月取。`);
|
|
826
|
+
}
|
|
827
|
+
}
|
|
828
|
+
const bucketOf = (row) => {
|
|
829
|
+
const raw = cellText(row[dim.shortKey]);
|
|
830
|
+
if (isCategory)
|
|
831
|
+
return raw || '(空)';
|
|
832
|
+
return bucketByGrain(raw, spec.grain);
|
|
833
|
+
};
|
|
834
|
+
const currentMonthSet = new Set(win.months);
|
|
835
|
+
const prevMonthSet = new Set(win.previousMonths);
|
|
836
|
+
const windowYears = new Set(yearsOf(win.months));
|
|
837
|
+
const prevWindowYears = new Set(yearsOf(win.previousMonths));
|
|
838
|
+
const inWindow = (bucket, isPrev) => {
|
|
839
|
+
if (isCategory)
|
|
840
|
+
return true;
|
|
841
|
+
const months = isPrev ? prevMonthSet : currentMonthSet;
|
|
842
|
+
const years = isPrev ? prevWindowYears : windowYears;
|
|
843
|
+
if (spec.grain === 'year')
|
|
844
|
+
return years.has(bucket);
|
|
845
|
+
if (spec.grain === 'month' || spec.dimension === 'month')
|
|
846
|
+
return months.has(bucket);
|
|
847
|
+
// day 粒度:按所属月判断是否落在窗口内
|
|
848
|
+
return months.has(bucket.slice(0, 7));
|
|
849
|
+
};
|
|
850
|
+
const baseConds = group.conditions
|
|
851
|
+
.filter((cond) => cond.source !== 'period' && cond.source !== 'person')
|
|
852
|
+
.map((cond) => toBiCondition(cond, person));
|
|
853
|
+
// 服务端过滤下推(如 合同种类=新签合同);字段名按台账解析,解析不到就报错,不猜
|
|
854
|
+
for (const filter of spec.filters) {
|
|
855
|
+
let fieldId = filter.fieldId ?? '';
|
|
856
|
+
let text = filter.field ?? '';
|
|
857
|
+
if (!fieldId || !text) {
|
|
858
|
+
const resolved = filter.field ? resolveFilterField(group.ledger, filter.field) : null;
|
|
859
|
+
if (resolved) {
|
|
860
|
+
fieldId = fieldId || resolved.fieldId;
|
|
861
|
+
// BI 条件里的显示名用字段中文名;未给中文名时用返回键占位(仅用于排障展示)
|
|
862
|
+
text = text || filter.field || resolved.dataKey;
|
|
863
|
+
}
|
|
864
|
+
else if (!fieldId) {
|
|
865
|
+
throw contractValidationError('filter_field_unknown', `过滤条件「${filter.field ?? filter.value}」在台账「${group.ledger}」里解析不到字段 ID;`
|
|
866
|
+
+ '请给出口径目录中已有的字段名(如 合同种类、客户名称),或显式传 fieldId');
|
|
867
|
+
}
|
|
868
|
+
}
|
|
869
|
+
baseConds.push({ field: text, fieldId, operator: filter.operator ?? 'eq', value: filter.value });
|
|
870
|
+
}
|
|
871
|
+
const personLeaves = personConds.length && people.length
|
|
872
|
+
? people.map((item) => toBiCondition(personConds[0], item))
|
|
873
|
+
: [];
|
|
874
|
+
/**
|
|
875
|
+
* 期间条件:时间维度按年 `like <YYYY>`(筛选字段与分组字段同源,避免筛选/分组不同源);
|
|
876
|
+
* 分类维度按窗口形态给「单月 eq」或「整年 like」。
|
|
877
|
+
*/
|
|
878
|
+
const filterFor = (year) => {
|
|
879
|
+
const periodConds = [];
|
|
880
|
+
if (isCategory && isSingleMonth) {
|
|
881
|
+
periodConds.push({
|
|
882
|
+
field: filterField.text,
|
|
883
|
+
fieldId: filterField.fieldId,
|
|
884
|
+
operator: 'eq',
|
|
885
|
+
value: win.months[0],
|
|
886
|
+
isPeriod: true,
|
|
887
|
+
});
|
|
888
|
+
}
|
|
889
|
+
else {
|
|
890
|
+
periodConds.push({
|
|
891
|
+
field: filterField.text,
|
|
892
|
+
fieldId: filterField.fieldId,
|
|
893
|
+
operator: 'like',
|
|
894
|
+
value: year,
|
|
895
|
+
isPeriod: true,
|
|
896
|
+
});
|
|
897
|
+
}
|
|
898
|
+
if (personLeaves.length > 1) {
|
|
899
|
+
return { conditions: [...baseConds, ...periodConds], orGroups: [personLeaves] };
|
|
900
|
+
}
|
|
901
|
+
return { conditions: [...baseConds, ...personLeaves, ...periodConds] };
|
|
902
|
+
};
|
|
903
|
+
// 指标 → BI 度量
|
|
904
|
+
const measurePlan = new Map();
|
|
905
|
+
const measures = [];
|
|
906
|
+
const distinctFieldOf = new Map();
|
|
907
|
+
for (const metric of group.metrics) {
|
|
908
|
+
const k = sanitizeShortKey(metric.key);
|
|
909
|
+
if (metric.aggregation === 'sum') {
|
|
910
|
+
if (!metric.metricFieldId || !metric.metricDataKey) {
|
|
911
|
+
throw contractValidationError('bi_metric_field_missing', `${metric.label} 缺字段 ID / 返回键,无法生成 BI 度量`);
|
|
912
|
+
}
|
|
913
|
+
measures.push({
|
|
914
|
+
fieldId: metric.metricFieldId,
|
|
915
|
+
dataKey: metric.metricDataKey,
|
|
916
|
+
text: metric.metricField,
|
|
917
|
+
shortKey: `m_${k}`,
|
|
918
|
+
aggregator: 'SUM',
|
|
919
|
+
dbFieldType: 'NUMBER',
|
|
920
|
+
compType: 'Money',
|
|
921
|
+
});
|
|
922
|
+
measurePlan.set(metric.key, { kind: 'sum', main: `m_${k}` });
|
|
923
|
+
}
|
|
924
|
+
else if (metric.aggregation === 'ratio') {
|
|
925
|
+
const num = metric.ratio?.numerator;
|
|
926
|
+
const den = metric.ratio?.denominator;
|
|
927
|
+
if (!num?.fieldId || !num.dataKey || !den?.fieldId || !den.dataKey) {
|
|
928
|
+
throw contractValidationError('bi_metric_field_missing', `${metric.label} 的分子/分母缺字段 ID,无法生成 BI 度量`);
|
|
929
|
+
}
|
|
930
|
+
measures.push({ fieldId: num.fieldId, dataKey: num.dataKey, text: num.field, shortKey: `n_${k}`, aggregator: 'SUM', dbFieldType: 'NUMBER', compType: 'Money' }, { fieldId: den.fieldId, dataKey: den.dataKey, text: den.field, shortKey: `d_${k}`, aggregator: 'SUM', dbFieldType: 'NUMBER', compType: 'Money' });
|
|
931
|
+
measurePlan.set(metric.key, { kind: 'ratio', num: `n_${k}`, den: `d_${k}` });
|
|
932
|
+
}
|
|
933
|
+
else {
|
|
934
|
+
const distinct = metric.distinct ?? { field: metric.metricField, fieldId: metric.metricFieldId, dataKey: metric.metricDataKey };
|
|
935
|
+
if (!distinct.fieldId || !distinct.dataKey) {
|
|
936
|
+
throw contractValidationError('bi_metric_field_missing', `${metric.label} 缺去重字段 ID,无法生成 BI 度量`);
|
|
937
|
+
}
|
|
938
|
+
/**
|
|
939
|
+
* 「去重计数」只在**分组字段 ≠ 被去重字段**时有意义(发请求前就判定,避免白跑):
|
|
940
|
+
* - 按客户分组 + 数客户 → 每行恒为 1(一个客户就是一家客户),是假数据,直接报错;
|
|
941
|
+
* - 按销售经理分组 + 数客户 → 每个销售经理名下几个新签客户,有意义,正常计算。
|
|
942
|
+
* 判据只比字段 ID,不按维度名一刀切(2026-11-28 修正:原先按「分类维度」一律拒绝,误伤了销售经理)。
|
|
943
|
+
*/
|
|
944
|
+
if (spec.dimension !== 'date' && spec.dimension !== 'month' && dim.fieldId === distinct.fieldId) {
|
|
945
|
+
throw contractValidationError('dimension_distinct_unsupported', `dimension=${spec.dimension} 的分组字段就是「${distinct.field}」,再对它去重计数每行恒为 1(没有信息量);`
|
|
946
|
+
+ `请改用 dimension=date/month,或换用「按销售经理分组」这类与被去重字段不同的维度`);
|
|
947
|
+
}
|
|
948
|
+
measures.push({
|
|
949
|
+
fieldId: distinct.fieldId,
|
|
950
|
+
dataKey: distinct.dataKey,
|
|
951
|
+
text: distinct.field,
|
|
952
|
+
shortKey: `m_${k}`,
|
|
953
|
+
aggregator: 'DISTINCTCOUNT',
|
|
954
|
+
dbFieldType: 'STRING',
|
|
955
|
+
compType: 'Text',
|
|
956
|
+
});
|
|
957
|
+
measurePlan.set(metric.key, { kind: 'distinct', main: `m_${k}` });
|
|
958
|
+
distinctFieldOf.set(metric.key, {
|
|
959
|
+
fieldId: distinct.fieldId,
|
|
960
|
+
dataKey: distinct.dataKey,
|
|
961
|
+
text: distinct.field,
|
|
962
|
+
shortKey: 'dv',
|
|
963
|
+
dbFieldType: 'STRING',
|
|
964
|
+
compType: 'Text',
|
|
965
|
+
});
|
|
966
|
+
}
|
|
967
|
+
}
|
|
968
|
+
const allYears = yearsOf([...win.months, ...win.previousMonths]);
|
|
969
|
+
const currentYears = new Set(yearsOf(win.months));
|
|
970
|
+
const prevYears = new Set(yearsOf(win.previousMonths));
|
|
971
|
+
// ① 逐月(按年)
|
|
972
|
+
for (const year of allYears) {
|
|
973
|
+
const config = buildBiConfig({
|
|
974
|
+
objId: group.objId,
|
|
975
|
+
datasetAppId: bi.datasetAppId,
|
|
976
|
+
datasetText: group.ledger,
|
|
977
|
+
...filterFor(year),
|
|
978
|
+
measures,
|
|
979
|
+
dimensions: [dim],
|
|
980
|
+
});
|
|
981
|
+
configs.push({ ledger: group.ledger, year, purpose: 'monthly', config });
|
|
982
|
+
if (!fetchEnabled)
|
|
983
|
+
continue;
|
|
984
|
+
let rows = [];
|
|
985
|
+
try {
|
|
986
|
+
rows = await call({ config, ledger: group.ledger, year, purpose: 'monthly' });
|
|
987
|
+
executed += 1;
|
|
988
|
+
}
|
|
989
|
+
catch (error) {
|
|
990
|
+
failed += 1;
|
|
991
|
+
const message = error instanceof Error ? error.message : String(error);
|
|
992
|
+
for (const metric of group.metrics)
|
|
993
|
+
incomplete.add(metric.key);
|
|
994
|
+
warnings.push(`${group.ledger} ${year} 年逐月取数失败:${message}`);
|
|
995
|
+
continue;
|
|
996
|
+
}
|
|
997
|
+
for (const row of rows) {
|
|
998
|
+
const label = bucketOf(row);
|
|
999
|
+
const isPrev = !currentYears.has(year);
|
|
1000
|
+
if (!label || !inWindow(label, isPrev))
|
|
1001
|
+
continue;
|
|
1002
|
+
const bucketMap = isPrev ? prevBuckets : currentBuckets;
|
|
1003
|
+
const numDenMap = isPrev ? prevNumDen : currentNumDen;
|
|
1004
|
+
const bucket = bucketMap.get(label) ?? {};
|
|
1005
|
+
for (const [key, plan] of measurePlan) {
|
|
1006
|
+
if (plan.kind === 'ratio') {
|
|
1007
|
+
// 只累加分子/分母,桶的比率在累计后重算(天级行不能直接取单行比率)
|
|
1008
|
+
const num = biNumber(row[plan.num]);
|
|
1009
|
+
const den = biNumber(row[plan.den]);
|
|
1010
|
+
const acc = numDenMap.get(label) ?? new Map();
|
|
1011
|
+
const prev = acc.get(key) ?? { num: 0, den: 0 };
|
|
1012
|
+
acc.set(key, { num: prev.num + num, den: prev.den + den });
|
|
1013
|
+
numDenMap.set(label, acc);
|
|
1014
|
+
}
|
|
1015
|
+
else if (plan.kind === 'sum') {
|
|
1016
|
+
// 天级行滚进同一个月桶时必须累加,覆盖写会把一个月写成最后一天的值
|
|
1017
|
+
bucket[key] = round2((bucket[key] ?? 0) + biNumber(row[plan.main]));
|
|
1018
|
+
}
|
|
1019
|
+
else if (spec.dimension === 'month') {
|
|
1020
|
+
// 业绩年月维度的 DISTINCTCOUNT 本身就是桶值(每月一行),直接取用;
|
|
1021
|
+
// 按签订日期时行是天级,「每天的去重数」不可相加,改由 ③ 的客户×时间桶回填。
|
|
1022
|
+
bucket[key] = Math.round(biNumber(row[plan.main]));
|
|
1023
|
+
}
|
|
1024
|
+
}
|
|
1025
|
+
bucketMap.set(label, bucket);
|
|
1026
|
+
}
|
|
1027
|
+
}
|
|
1028
|
+
// 比率:分子/分母按桶累计后重算桶值(天级行不能直接取单行比率)
|
|
1029
|
+
for (const metric of group.metrics) {
|
|
1030
|
+
if (metric.aggregation !== 'ratio')
|
|
1031
|
+
continue;
|
|
1032
|
+
const pairs = [
|
|
1033
|
+
[currentBuckets, currentNumDen],
|
|
1034
|
+
[prevBuckets, prevNumDen],
|
|
1035
|
+
];
|
|
1036
|
+
for (const [buckets, numDens] of pairs) {
|
|
1037
|
+
for (const [label, bucket] of buckets) {
|
|
1038
|
+
const acc = numDens.get(label)?.get(metric.key);
|
|
1039
|
+
bucket[metric.key] = !acc || acc.den === 0 ? null : round2((acc.num / acc.den) * 100);
|
|
1040
|
+
}
|
|
1041
|
+
}
|
|
1042
|
+
}
|
|
1043
|
+
// ② 窗口合计:**只累加窗口内的桶**(曾按整年取合计,把窗口外月份也算进来,实测差 26,805.08)
|
|
1044
|
+
const windowTargets = [
|
|
1045
|
+
{ bucketMap: currentBuckets, numDenMap: currentNumDen, target: totalsCurrent, labels: [...currentBuckets.keys()], isPrev: false },
|
|
1046
|
+
{ bucketMap: prevBuckets, numDenMap: prevNumDen, target: totalsPrevious, labels: [...prevBuckets.keys()], isPrev: true },
|
|
1047
|
+
];
|
|
1048
|
+
for (const { bucketMap, numDenMap, target, labels } of windowTargets) {
|
|
1049
|
+
if (!labels.length)
|
|
1050
|
+
continue;
|
|
1051
|
+
for (const metric of group.metrics) {
|
|
1052
|
+
if (incomplete.has(metric.key)) {
|
|
1053
|
+
target[metric.key] = null;
|
|
1054
|
+
continue;
|
|
1055
|
+
}
|
|
1056
|
+
const plan = measurePlan.get(metric.key);
|
|
1057
|
+
if (plan.kind === 'sum') {
|
|
1058
|
+
let sum = 0;
|
|
1059
|
+
for (const label of labels)
|
|
1060
|
+
sum += bucketMap.get(label)?.[metric.key] ?? 0;
|
|
1061
|
+
target[metric.key] = round2(sum);
|
|
1062
|
+
}
|
|
1063
|
+
else if (plan.kind === 'ratio') {
|
|
1064
|
+
let num = 0;
|
|
1065
|
+
let den = 0;
|
|
1066
|
+
for (const label of labels) {
|
|
1067
|
+
const acc = numDenMap.get(label)?.get(metric.key);
|
|
1068
|
+
if (acc) {
|
|
1069
|
+
num += acc.num;
|
|
1070
|
+
den += acc.den;
|
|
1071
|
+
}
|
|
1072
|
+
}
|
|
1073
|
+
target[metric.key] = den === 0 ? null : round2((num / den) * 100);
|
|
1074
|
+
}
|
|
1075
|
+
}
|
|
1076
|
+
}
|
|
1077
|
+
// ③ 去重计数合计:按「去重字段 + 年月」两维取行,只并窗口内月份的客户
|
|
1078
|
+
for (const metric of group.metrics) {
|
|
1079
|
+
if (metric.aggregation !== 'countDistinct')
|
|
1080
|
+
continue;
|
|
1081
|
+
const distinct = distinctFieldOf.get(metric.key);
|
|
1082
|
+
for (const { labels: windowLabels, target, isPrev: windowIsPrev } of windowTargets) {
|
|
1083
|
+
if (!windowLabels.length)
|
|
1084
|
+
continue;
|
|
1085
|
+
if (incomplete.has(metric.key)) {
|
|
1086
|
+
target[metric.key] = null;
|
|
1087
|
+
continue;
|
|
1088
|
+
}
|
|
1089
|
+
const names = new Set();
|
|
1090
|
+
const perBucket = new Map();
|
|
1091
|
+
let distinctFailed = false;
|
|
1092
|
+
// 年份必须来自**统计窗口**,不能从桶标签推:分类维度的桶标签是「张三」这类名字,
|
|
1093
|
+
// 取 slice(0,4) 会变成 `合同业绩年月 like 张三`,请求必然空转。
|
|
1094
|
+
const distinctYears = isCategory
|
|
1095
|
+
? yearsOf(windowIsPrev ? win.previousMonths : win.months)
|
|
1096
|
+
: [...new Set(windowLabels.map((label) => label.slice(0, 4)))].sort();
|
|
1097
|
+
for (const year of distinctYears) {
|
|
1098
|
+
const config = buildBiConfig({
|
|
1099
|
+
objId: group.objId,
|
|
1100
|
+
datasetAppId: bi.datasetAppId,
|
|
1101
|
+
datasetText: group.ledger,
|
|
1102
|
+
...filterFor(year),
|
|
1103
|
+
measures: [{ ...distinct, shortKey: 'n', aggregator: 'COUNT' }],
|
|
1104
|
+
// 去重合计要「客户 × 时间桶」两维:只并窗口内桶的客户
|
|
1105
|
+
dimensions: [distinct, dim],
|
|
1106
|
+
});
|
|
1107
|
+
configs.push({ ledger: group.ledger, year, purpose: 'distinct', config });
|
|
1108
|
+
if (!fetchEnabled)
|
|
1109
|
+
continue;
|
|
1110
|
+
try {
|
|
1111
|
+
const rows = await call({ config, ledger: group.ledger, year, purpose: 'distinct' });
|
|
1112
|
+
executed += 1;
|
|
1113
|
+
for (const row of rows) {
|
|
1114
|
+
const label = bucketOf(row);
|
|
1115
|
+
if (!label || !inWindow(label, windowIsPrev))
|
|
1116
|
+
continue;
|
|
1117
|
+
const name = cellText(row[distinct.shortKey]);
|
|
1118
|
+
if (!name)
|
|
1119
|
+
continue;
|
|
1120
|
+
names.add(name);
|
|
1121
|
+
const set = perBucket.get(label) ?? new Set();
|
|
1122
|
+
set.add(name);
|
|
1123
|
+
perBucket.set(label, set);
|
|
1124
|
+
}
|
|
1125
|
+
}
|
|
1126
|
+
catch (error) {
|
|
1127
|
+
failed += 1;
|
|
1128
|
+
distinctFailed = true;
|
|
1129
|
+
warnings.push(`${group.ledger} ${year} 年去重取数失败:${error instanceof Error ? error.message : String(error)}`);
|
|
1130
|
+
}
|
|
1131
|
+
}
|
|
1132
|
+
if (distinctFailed) {
|
|
1133
|
+
incomplete.add(metric.key);
|
|
1134
|
+
target[metric.key] = null;
|
|
1135
|
+
}
|
|
1136
|
+
else {
|
|
1137
|
+
target[metric.key] = names.size;
|
|
1138
|
+
// dimension=month 的逐月 DISTINCTCOUNT 本身就是桶值(每月一行),不能用并集覆盖;
|
|
1139
|
+
// 其它维度(按签订日期是天级行、按销售经理是分组行)桶值都由「被去重字段 × 分组字段」两维回填。
|
|
1140
|
+
if (spec.dimension !== 'month') {
|
|
1141
|
+
const bucketMap = windowIsPrev ? prevBuckets : currentBuckets;
|
|
1142
|
+
for (const [label, bucket] of bucketMap) {
|
|
1143
|
+
bucket[metric.key] = perBucket.get(label)?.size ?? 0;
|
|
1144
|
+
}
|
|
1145
|
+
}
|
|
1146
|
+
}
|
|
1147
|
+
}
|
|
1148
|
+
}
|
|
1149
|
+
}
|
|
1150
|
+
// ③ 组装结果:桶标签排序后转成 periods(字段名仍叫 month,值就是桶标签)
|
|
1151
|
+
const buildPeriod = (label, bucketMap) => {
|
|
1152
|
+
const bucket = bucketMap.get(label);
|
|
1153
|
+
const values = {};
|
|
1154
|
+
for (const metric of metrics) {
|
|
1155
|
+
if (incomplete.has(metric.key)) {
|
|
1156
|
+
values[metric.key] = null;
|
|
1157
|
+
continue;
|
|
1158
|
+
}
|
|
1159
|
+
const value = bucket?.[metric.key];
|
|
1160
|
+
if (value === undefined) {
|
|
1161
|
+
values[metric.key] = metric.aggregation === 'ratio' ? null : 0;
|
|
1162
|
+
}
|
|
1163
|
+
else {
|
|
1164
|
+
values[metric.key] = value;
|
|
1165
|
+
}
|
|
1166
|
+
}
|
|
1167
|
+
return { month: label, values, queries: [], incomplete: false };
|
|
1168
|
+
};
|
|
1169
|
+
const sortLabels = (labels) => [...labels].sort((a, b) => (a < b ? -1 : a > b ? 1 : 0));
|
|
1170
|
+
if (!fetchEnabled) {
|
|
1171
|
+
return {
|
|
1172
|
+
...payloadBase,
|
|
1173
|
+
plan: [],
|
|
1174
|
+
periods: [],
|
|
1175
|
+
previousPeriods: [],
|
|
1176
|
+
series: [],
|
|
1177
|
+
totals: {},
|
|
1178
|
+
previousTotals: {},
|
|
1179
|
+
compare: {},
|
|
1180
|
+
summary: { planned: configs.length, executed: 0, failed: 0 },
|
|
1181
|
+
incompleteMetrics: [],
|
|
1182
|
+
warnings: ['dryRun:只输出 BI 配置,未取数(engine=bi)。'],
|
|
1183
|
+
biConfigs: configs,
|
|
1184
|
+
};
|
|
1185
|
+
}
|
|
1186
|
+
// 时间维度按月时补齐窗口内空月(否则没数据的月份整行消失,报表轴会跳空);
|
|
1187
|
+
// 天级不补(一年 365 桶没有意义),分类维度不补。
|
|
1188
|
+
if (!isCategoryAll && (spec.dimension === 'month' || spec.grain === 'month')) {
|
|
1189
|
+
for (const label of win.months)
|
|
1190
|
+
if (!currentBuckets.has(label))
|
|
1191
|
+
currentBuckets.set(label, {});
|
|
1192
|
+
for (const label of win.previousMonths)
|
|
1193
|
+
if (!prevBuckets.has(label))
|
|
1194
|
+
prevBuckets.set(label, {});
|
|
1195
|
+
}
|
|
1196
|
+
const periods = sortLabels([...currentBuckets.keys()]).map((label) => buildPeriod(label, currentBuckets));
|
|
1197
|
+
const previousPeriods = sortLabels([...prevBuckets.keys()]).map((label) => buildPeriod(label, prevBuckets));
|
|
1198
|
+
// ④ 去年同期对齐:标签按维度平移(分类维度同名、年粒度 -1 年、月/日粒度 -1 年)
|
|
1199
|
+
const shiftLabel = (label) => {
|
|
1200
|
+
if (isCategoryAll)
|
|
1201
|
+
return label;
|
|
1202
|
+
if (spec.grain === 'year' || spec.dimension === 'month') {
|
|
1203
|
+
return `${Number(label.slice(0, 4)) - 1}${label.slice(4)}`;
|
|
1204
|
+
}
|
|
1205
|
+
// day:保留月日,只换年
|
|
1206
|
+
return `${Number(label.slice(0, 4)) - 1}${label.slice(4)}`;
|
|
1207
|
+
};
|
|
1208
|
+
const previousByLabel = new Map(previousPeriods.map((item) => [item.month, item]));
|
|
1209
|
+
const series = periods.map((period) => {
|
|
1210
|
+
const previous = previousByLabel.get(shiftLabel(period.month)) ?? null;
|
|
1211
|
+
const yoy = {};
|
|
1212
|
+
for (const metric of metrics) {
|
|
1213
|
+
const current = period.values[metric.key] ?? null;
|
|
1214
|
+
const prev = previous?.values[metric.key] ?? null;
|
|
1215
|
+
yoy[metric.key] = current !== null && prev !== null && prev !== 0
|
|
1216
|
+
? round2(((current - prev) / prev) * 100)
|
|
1217
|
+
: null;
|
|
1218
|
+
}
|
|
1219
|
+
return {
|
|
1220
|
+
month: period.month,
|
|
1221
|
+
values: period.values,
|
|
1222
|
+
previousYear: previous ? { month: previous.month, values: previous.values } : null,
|
|
1223
|
+
yoy,
|
|
1224
|
+
};
|
|
1225
|
+
});
|
|
1226
|
+
const compareOut = {};
|
|
1227
|
+
for (const metric of metrics) {
|
|
1228
|
+
const current = totalsCurrent[metric.key] ?? null;
|
|
1229
|
+
const previous = totalsPrevious[metric.key] ?? null;
|
|
1230
|
+
const delta = current !== null && previous !== null ? round2(current - previous) : null;
|
|
1231
|
+
const pct = current !== null && previous !== null && previous !== 0
|
|
1232
|
+
? round2(((current - previous) / previous) * 100)
|
|
1233
|
+
: null;
|
|
1234
|
+
compareOut[metric.key] = { current, previous, delta, pct };
|
|
1235
|
+
}
|
|
1236
|
+
if (periods.length > 0 && periods.every((period) => period.incomplete)) {
|
|
1237
|
+
warnings.push('所有分组均不完整,结论不可用;请先收窄范围再重跑。');
|
|
1238
|
+
}
|
|
1239
|
+
return {
|
|
1240
|
+
...payloadBase,
|
|
1241
|
+
plan: [],
|
|
1242
|
+
periods,
|
|
1243
|
+
previousPeriods,
|
|
1244
|
+
series,
|
|
1245
|
+
totals: totalsCurrent,
|
|
1246
|
+
previousTotals: totalsPrevious,
|
|
1247
|
+
compare: compareOut,
|
|
1248
|
+
summary: { planned: configs.length, executed, failed },
|
|
1249
|
+
incompleteMetrics: [...incomplete],
|
|
1250
|
+
warnings,
|
|
1251
|
+
};
|
|
1252
|
+
}
|
|
1253
|
+
// ---------------------------------------------------------------- operation
|
|
1254
|
+
export const contractLedgerStatOperation = {
|
|
1255
|
+
name: 'contract.ledger.stat',
|
|
1256
|
+
risk: 'read',
|
|
1257
|
+
inputSchema: {
|
|
1258
|
+
type: 'object',
|
|
1259
|
+
additionalProperties: false,
|
|
1260
|
+
properties: {
|
|
1261
|
+
metrics: {
|
|
1262
|
+
type: 'array',
|
|
1263
|
+
items: { type: 'string' },
|
|
1264
|
+
description: '口径键或中文名:signedAmount(签约总额) / contractReceived(合同已收款) / newSignedCustomers(新签客户数) / receiptRate(回款率) / contractPerformance(合同业绩) / eteammsPerformance(eteams业绩)',
|
|
1265
|
+
},
|
|
1266
|
+
metric: { type: 'string', description: '单口径写法(等价 metrics: [x])' },
|
|
1267
|
+
from: { type: 'string', description: '起始月份 YYYY-MM(含)' },
|
|
1268
|
+
to: { type: 'string', description: '结束月份 YYYY-MM(含)' },
|
|
1269
|
+
periods: { type: 'array', items: { type: 'string' }, description: '显式月份列表;给了就忽略 from/to' },
|
|
1270
|
+
compare: { type: 'string', enum: ['yoy', 'none'], description: '对比方式:yoy=去年同期;默认 none' },
|
|
1271
|
+
person: {
|
|
1272
|
+
type: 'object',
|
|
1273
|
+
description: '{ id, name }:人员 ID 必须是 im.person.resolve 的 uid;传姓名不报错但会串成全量',
|
|
1274
|
+
properties: { id: { type: 'string' }, name: { type: 'string' } },
|
|
1275
|
+
},
|
|
1276
|
+
people: {
|
|
1277
|
+
type: 'array',
|
|
1278
|
+
description: '多人员合计(仅 engine=bi):每人 { id, name },合成一个 OR 组(A 或 B);人员字段必须给 id',
|
|
1279
|
+
items: { type: 'object', properties: { id: { type: 'string' }, name: { type: 'string' } } },
|
|
1280
|
+
},
|
|
1281
|
+
where: { type: 'array', items: { type: 'string' }, description: '本地过滤(仅 engine=rows),形如 khmc=泛微;!= 表示不含。engine=bi 请用 filters' },
|
|
1282
|
+
filters: {
|
|
1283
|
+
type: 'array',
|
|
1284
|
+
description: '服务端过滤条件(如 合同种类=新签合同,可算「新签合同的签约总额/回款率」);engine=bi 下推、engine=rows 转本地过滤',
|
|
1285
|
+
items: {
|
|
1286
|
+
type: 'object',
|
|
1287
|
+
properties: {
|
|
1288
|
+
field: { type: 'string', description: '字段中文名,如 合同种类 / 客户名称 / 销售经理' },
|
|
1289
|
+
fieldId: { type: 'string', description: '显式字段 ID(field 解析不到时用)' },
|
|
1290
|
+
dataKey: { type: 'string', description: '显式返回键(engine=rows 本地过滤需要)' },
|
|
1291
|
+
operator: { type: 'string', enum: ['eq', 'like', 'startWith'], description: '默认 eq' },
|
|
1292
|
+
value: { type: 'string', description: '取值,如 新签合同 / 续签合同' },
|
|
1293
|
+
},
|
|
1294
|
+
required: ['value'],
|
|
1295
|
+
},
|
|
1296
|
+
},
|
|
1297
|
+
pageSize: { type: 'integer', minimum: 1, maximum: 200, description: '每页条数,默认 200' },
|
|
1298
|
+
maxPages: { type: 'integer', minimum: 1, maximum: 50, description: '单窗最多翻页数,默认 10(2000 条上限)' },
|
|
1299
|
+
engine: {
|
|
1300
|
+
type: 'string',
|
|
1301
|
+
enum: ['rows', 'bi'],
|
|
1302
|
+
description: 'rows=明细分页聚合(默认,几十万行会慢);bi=走 E10 BI 聚合接口,一次请求按月出数(需 bi 上下文)',
|
|
1303
|
+
},
|
|
1304
|
+
bi: {
|
|
1305
|
+
type: 'object',
|
|
1306
|
+
description: 'engine=bi 时必填:{ pageId, dataSetId, analyzeId, datasetAppId? },取自 BI 组件预览请求',
|
|
1307
|
+
properties: {
|
|
1308
|
+
pageId: { type: 'string' },
|
|
1309
|
+
dataSetId: { type: 'string' },
|
|
1310
|
+
analyzeId: { type: 'string' },
|
|
1311
|
+
datasetAppId: { type: 'string' },
|
|
1312
|
+
},
|
|
1313
|
+
},
|
|
1314
|
+
dryRun: { type: 'boolean', description: '只输出查询计划不取数' },
|
|
1315
|
+
detail: { type: 'boolean', description: '计划里带上 searchParamJson 与条件明细(排查用;默认紧凑形态,避免撑爆 stdout)' },
|
|
1316
|
+
out: { type: 'string', maxLength: 4096, description: '把统计结果 JSON 写到该路径(供 contract.ledger.report 直接用)' },
|
|
1317
|
+
},
|
|
1318
|
+
required: ['metrics'],
|
|
1319
|
+
},
|
|
1320
|
+
async execute(input, context) {
|
|
1321
|
+
const spec = resolveStatSpec(input);
|
|
1322
|
+
const pageSize = typeof input.pageSize === 'number' && input.pageSize > 0
|
|
1323
|
+
? Math.min(Math.floor(input.pageSize), 200)
|
|
1324
|
+
: 200;
|
|
1325
|
+
const maxPages = typeof input.maxPages === 'number' && input.maxPages > 0
|
|
1326
|
+
? Math.min(Math.floor(input.maxPages), 50)
|
|
1327
|
+
: 10;
|
|
1328
|
+
const fetcher = async (params) => {
|
|
1329
|
+
const operation = resolveOperation('contract.performance.list.query');
|
|
1330
|
+
const result = await operation.execute({
|
|
1331
|
+
apiPrefix: '/api/ebuilder',
|
|
1332
|
+
listId: params.listId,
|
|
1333
|
+
appId: '0',
|
|
1334
|
+
objId: params.objId,
|
|
1335
|
+
searchParamJson: params.searchParamJson,
|
|
1336
|
+
pageSize,
|
|
1337
|
+
maxPages,
|
|
1338
|
+
}, context);
|
|
1339
|
+
const data = (result?.data ?? {});
|
|
1340
|
+
const rows = Array.isArray(data.rows) ? data.rows : [];
|
|
1341
|
+
const pagination = (data.pagination ?? {});
|
|
1342
|
+
const total = typeof pagination.total === 'number' ? pagination.total : null;
|
|
1343
|
+
return {
|
|
1344
|
+
rows,
|
|
1345
|
+
total,
|
|
1346
|
+
truncated: pagination.truncated === true || (total !== null && rows.length < total),
|
|
1347
|
+
};
|
|
1348
|
+
};
|
|
1349
|
+
const biCaller = async (params) => {
|
|
1350
|
+
if (!spec.bi)
|
|
1351
|
+
throw contractValidationError('bi_context_required', 'engine=bi 缺少 bi 上下文');
|
|
1352
|
+
const operation = resolveOperation('contract.ledger.bi');
|
|
1353
|
+
const result = await operation.execute({
|
|
1354
|
+
pageId: spec.bi.pageId,
|
|
1355
|
+
dataSetId: spec.bi.dataSetId,
|
|
1356
|
+
analyzeId: spec.bi.analyzeId,
|
|
1357
|
+
...(spec.bi.datasetAppId ? { datasetAppId: spec.bi.datasetAppId } : {}),
|
|
1358
|
+
config: params.config,
|
|
1359
|
+
}, context);
|
|
1360
|
+
const response = (result?.data?.response ?? {});
|
|
1361
|
+
const rows = Array.isArray(response.data) ? response.data : [];
|
|
1362
|
+
return rows;
|
|
1363
|
+
};
|
|
1364
|
+
const payload = spec.engine === 'bi'
|
|
1365
|
+
? await runStatViaBi(spec, spec.bi, biCaller, spec.fetch)
|
|
1366
|
+
: await runStat(spec, fetcher);
|
|
1367
|
+
// 计划默认只给紧凑形态(月份/台账/口径),`searchParamJson` 与条件明细走 detail:
|
|
1368
|
+
// 44 条计划全带 searchParamJson 会撑爆 stdout 预算(28k)并被截断成 ref 预览。
|
|
1369
|
+
const detail = input.detail === true;
|
|
1370
|
+
const plan = detail
|
|
1371
|
+
? payload.plan
|
|
1372
|
+
: payload.plan.map((entry) => ({
|
|
1373
|
+
month: entry.month,
|
|
1374
|
+
previousYear: entry.previousYear,
|
|
1375
|
+
ledger: entry.ledger,
|
|
1376
|
+
metrics: entry.metrics,
|
|
1377
|
+
}));
|
|
1378
|
+
if (typeof input.out === 'string' && input.out) {
|
|
1379
|
+
mkdirSync(dirname(input.out), { recursive: true });
|
|
1380
|
+
writeFileSync(input.out, `${JSON.stringify(payload, null, 2)}\n`, 'utf8');
|
|
1381
|
+
}
|
|
1382
|
+
const status = spec.fetch && payload.summary.executed === 0 && payload.summary.failed > 0
|
|
1383
|
+
? 'PARTIAL'
|
|
1384
|
+
: 'COMPLETE';
|
|
1385
|
+
// 默认输出裁剪:`periods` / `previousPeriods` 与 `series` 内容重复,且 11 个月就把 payload
|
|
1386
|
+
// 顶到 31KB,超过 stdout 预算(28k)会退化成 ref 预览。默认只留 series + 合计 + 同比,
|
|
1387
|
+
// 逐月取数明细(每窗 fetched/total)与 periods 走 detail,或看 `out` 落盘文件。
|
|
1388
|
+
const { periods, previousPeriods, ...core } = payload;
|
|
1389
|
+
return {
|
|
1390
|
+
status,
|
|
1391
|
+
data: {
|
|
1392
|
+
...core,
|
|
1393
|
+
plan,
|
|
1394
|
+
...(detail ? { periods, previousPeriods } : {}),
|
|
1395
|
+
...(typeof input.out === 'string' && input.out ? { out: input.out } : {}),
|
|
1396
|
+
queryParams: { pageSize, maxPages, maxRowsPerWindow: pageSize * maxPages, detail },
|
|
1397
|
+
},
|
|
1398
|
+
};
|
|
1399
|
+
},
|
|
1400
|
+
};
|