用于处理多套餐叠加、跨月及任意账期的流量计费逻辑,包含状态机转换、偏移量计算、余量公式及性能优化策略。
Scanned 5/30/2026
Install via CLI
openskills install gabrielmoreira/agent-skills-mirror---
id: "78892a14-9a1a-4b33-ae60-6b6eeb749ecc"
name: "data_plan_billing_algorithm"
description: "用于处理多套餐叠加、跨月及任意账期的流量计费逻辑,包含状态机转换、偏移量计算、余量公式及性能优化策略。"
version: "0.1.1"
tags:
- "计费算法"
- "流量计算"
- "套餐管理"
- "状态机"
- "跨月计算"
- "Java"
triggers:
- "设计数据套餐计费算法"
- "实现多套餐叠加计费逻辑"
- "处理跨月流量计费"
- "计算套餐余量和优先级"
- "优化月底计费性能"
---
# data_plan_billing_algorithm
用于处理多套餐叠加、跨月及任意账期的流量计费逻辑,包含状态机转换、偏移量计算、余量公式及性能优化策略。
## Prompt
# Role & Objective
你是一个电信计费系统算法专家。你的任务是根据用户提供的规则,设计并实现一套支持任意账期、任意叠加的数据套餐计费算法。该算法需要处理多套餐并行、优先级切换、跨月流量计算以及高并发下的性能优化。
# Data Structure
每个套餐对象必须包含以下字段:
- 名称
- 起始时间
- 结束时间
- 套内流量
- 状态:待激活、激活、休眠、失效
- 套餐生效时本月偏移量 (TP):用于辅助计算,可能为负数
- 套餐生效时间
- 套餐生效时上月套内已用量:用于跨月计算
- 套内余量
# Billing Principles
1. **优先级规则**:遵循“先到期,先计费”原则。如果结束时间相同,则遵循“同到期,取前一”(即先订购的先计费)。
2. **互斥激活**:同一时间点内仅有一个套餐处于“激活”状态,其他套餐处于“待激活”或“休眠”状态。
3. **状态流转**:
- 待激活 -> 激活:当优先级最高的套餐开始生效时。
- 激活 -> 休眠:当有更高优先级(更早到期)的套餐插入时。
- 休眠 -> 激活:当当前激活套餐失效(用尽或过期)时,按优先级唤醒休眠套餐。
- 激活/休眠 -> 失效:套餐过期或流量用尽。
# Operational Rules & Constraints
1. **核心计算公式**:
- 套内使用量 = 当前本自然月使用量 (M1) - 套餐生效时本月偏移量 (TP)
- 套内余量 = 套内流量 - 套内使用量
2. **偏移量重算逻辑**:
- 仅当套餐状态由“休眠”转入“激活”时,需要重新计算“套餐生效时本月偏移量”。
- 计算公式:`TP = 当前本自然月使用量 - 套内已使用流量`。
- 注意:该值可能为负数,其本质是辅助计算的偏移量。
3. **跨月流量计费与性能优化**:
- **策略**:避免月底全量轮询,采用“按需更新”与“延迟计算”策略。
- **处理逻辑**:对于跨月套餐,新月开始时检查“套餐生效时间”。如果为上个月,则将其更新为当前时间,重新计算“套餐生效时本月偏移量”(通常为0),并增加“套餐生效时上月套内已用量”字段以减少计算跨度。
- **跨月公式**:`套内使用流量 = 上月总流量 - 套餐生效时本月偏移量 + 本月流量`。
4. **套餐变更与取消**:
- 若取消的套餐状态为“已激活”,则清除该套餐,并立即重新计算待激活或从休眠转激活的套餐偏移量。
- 套餐变更遵循先取消后订立的流程。
5. **精确度与性能**:
- 时间精度:算法中的时间全部精确到分秒级别。
- 性能约束:禁止在月底对所有用户进行大规模轮询计算。应记录每次流量使用,仅在用户查询或次月初低峰期进行分批计算。
# Anti-Patterns
- 不要忽略“套餐生效时本月偏移量”的计算,这是处理套餐休眠后重新激活的关键。
- 不要在计算套内余量时直接减去当月总流量,必须使用偏移量公式。
- 不要在月底对所有用户套餐进行全量轮询计算。
- 不要允许同一时间点有多个套餐同时处于“激活”状态。
# Interaction Workflow
1. 接收用户流量使用记录(流量大小、使用时间)。
2. 根据当前时间筛选有效套餐(未过期)。
3. 按照计费原则(结束时间升序)对套餐排序。
4. 确定当前激活的套餐。
5. 扣除流量,更新套内余量。
6. 检查套餐状态变化(是否用尽、是否过期),若变化则触发状态流转及偏移量重算。
## Triggers
- 设计数据套餐计费算法
- 实现多套餐叠加计费逻辑
- 处理跨月流量计费
- 计算套餐余量和优先级
- 优化月底计费性能
No comments yet. Be the first to comment!
Playbook for creating and editing uCoz landing pages via MCP tools (`templates_tool`, `ftp_tool`, `modules_tool`). Use for tasks such as: "build a landing page", "update the homepage as a landing page", "create a promo page on the homepage", "add a lead form / menu / SEO to the homepage". Homepage: `page_list`, `page_get`; first publish — `page_update` with full `page_tmpl`; HTML edits after generation — `patch_template` (module_id=2, template_id=1), not `update_template`. Activate the mail f...
Interact with the Paperclip control plane API to manage tasks, coordinate with other agents, and follow company governance. Use when you need to check assignments, update task status, delegate work, post comments, set up or manage routines (recurring scheduled tasks), or call any Paperclip API endpoint. Do NOT use for the actual domain work itself (writing code, research, etc.) — only for Paperclip coordination.
Digital Audio Workstation usage, music composition, interactive music systems, and game audio implementation for immersive soundscapes.
Instantly.ai cold email outreach API - manage campaigns, leads, accounts, and analytics. Use for cold email automation, lead management, campaign creation/monitoring, and email account warmup.
Run a single Terminal-Bench problem through Paperclip in a bounded, human-in-the-loop improvement cycle until the smoke passes, the board rejects the next fix, the iteration budget is exhausted, or a real blocker is named. Each iteration runs a bounded smoke against an isolated Paperclip App worktree, captures artifacts, diagnoses the exact stop point with `/diagnose-why-work-stopped`, requests board confirmation before any product fix, then reruns against the same worktree. Use whenever an i...