|
|
@@ -11,7 +11,7 @@
|
|
|
//! 读侧⛔ 现取 `catalog_plan`(`to_json` 的 `plan_name` 走快照列)。
|
|
|
//! * 金额与状态一律走 [`bos_invariant`]:`monthly_settle_of`=月结算唯一折算点、
|
|
|
//! `subscription_status_of`=状态唯一判据、`in_use_quota_sum_batch`=额度求和唯一实现、
|
|
|
-//! `check_i6_subscription`=I6 唯一实现(违规即 `KLT-M224` 并带那份订阅的定位)。
|
|
|
+//! I6 已于 2026-11-19 停用(D-15:`sub_mode` 不再派生),码位 `KLT-M224` 保留不复用。
|
|
|
//!
|
|
|
//! ## 写侧事务落点(§5.2 六步)
|
|
|
//!
|
|
|
@@ -33,10 +33,10 @@ use bos_api::AppState;
|
|
|
use bos_core::page::PageQuery;
|
|
|
use bos_core::{BosError, KltCode, RoleCode};
|
|
|
use bos_invariant::{
|
|
|
- check_i12b_same_customer, check_i13_trial_months, check_i6_subscription, check_plan_available,
|
|
|
+ check_i12b_same_customer, check_i13_trial_months, check_plan_available,
|
|
|
check_subscription_deletable, contracts_opened, in_use_quota_sum_batch, monthly_settle_of,
|
|
|
- normalize_line_exits, sub_mode_of, subscription_status_of, ActiveSubscriptionRow, CrossRead,
|
|
|
- NormalizedExits, Opened, OpenedProbe, PlanRef, SubMode, SubscriptionBlocks, UseCategory,
|
|
|
+ normalize_line_exits, subscription_status_of, CrossRead,
|
|
|
+ NormalizedExits, Opened, OpenedProbe, PlanFamily, PlanRef, SubscriptionBlocks,
|
|
|
MAX_AMOUNT_CENTS,
|
|
|
};
|
|
|
use bos_platform::clients::uds::{de, DmlStatement};
|
|
|
@@ -60,7 +60,7 @@ const TERMS: [&str; 3] = ["yearly", "quarterly", "monthly"];
|
|
|
pub(crate) const IN_USE_STATUS: [&str; 2] = ["pending", "active"];
|
|
|
|
|
|
/// 六项只读派生列+变更链指针:新建载荷出现即 `11103`(§5.2 段 3/4、§2.3)。
|
|
|
-const CREATE_READ_ONLY: [&str; 8] = [
|
|
|
+const CREATE_READ_ONLY: [&str; 9] = [
|
|
|
"annual_price_cents",
|
|
|
"monthly_settle_cents",
|
|
|
"end_date",
|
|
|
@@ -69,6 +69,8 @@ const CREATE_READ_ONLY: [&str; 8] = [
|
|
|
"order_no",
|
|
|
"actual_opened_at",
|
|
|
"replaces_subscription_id",
|
|
|
+ // D-15:`item_family` 随所引合同条目带入并冻结(合同侧载荷已拒传),订阅侧⛔ 再收一份。
|
|
|
+ "item_family",
|
|
|
];
|
|
|
|
|
|
/// 升降配额外只读项:⛔ 换客户(`customer_id` 只做一致性校验),用途分类跟着新档位走
|
|
|
@@ -76,7 +78,7 @@ const CREATE_READ_ONLY: [&str; 8] = [
|
|
|
/// 契约(`openapi.yaml` `/subscription/change@v1`)列为必填「新合同/新条目」,此前误入拒收表
|
|
|
/// ⇒ 前端按契约提交必吃 `KLT-S20004`,升降配在界面与接口两侧都不可用(P5-D1)。
|
|
|
const CHANGE_READ_ONLY: [&str; 8] = [
|
|
|
- "use_category",
|
|
|
+ "plan_family",
|
|
|
"annual_price_cents",
|
|
|
"monthly_settle_cents",
|
|
|
"end_date",
|
|
|
@@ -107,16 +109,17 @@ pub(crate) const TICKET_UNCONFIGURED_NOTE: &str = "开户工单只读口本轮
|
|
|
|
|
|
/// 统一取列(写侧回读、列表、详情、I6 取数共用一份列集=⛔ 三处漂移)。
|
|
|
const SELECT_COLS: &str = "SELECT s.subscription_id, s.customer_id, s.contract_id, s.contract_item_id, \
|
|
|
- s.plan_id, s.plan_name, s.plan_kind, s.plan_owner_customer_id, s.status, s.use_category, s.sub_mode, \
|
|
|
+ s.plan_id, s.plan_name, s.plan_kind, s.plan_owner_customer_id, s.status, s.plan_family, s.sub_mode, \
|
|
|
s.line_exits, s.line_exit_key, s.start_date, s.end_date, s.term, s.trial_months, \
|
|
|
s.discount_monthly_cents, s.annual_price_cents, s.monthly_settle_cents, s.quota_slots, \
|
|
|
s.quantity, s.replaces_subscription_id, s.order_no, s.actual_opened_at, s.effective_date, \
|
|
|
s.expire_date, s.created_at, s.row_version";
|
|
|
|
|
|
-/// 档位取列(`p`=`catalog_plan`)。
|
|
|
+/// 档位取列(`p`=`catalog_plan`)。`plan_family` 是条目/订阅形态的**唯一来源**;
|
|
|
+/// 派生档整链已删(D-15)⇒ 不再有 `derived_from_contract_item_id`。
|
|
|
const PLAN_COLS: &str = "SELECT p.plan_id, p.plan_name, p.plan_kind, p.customer_id, p.status, \
|
|
|
- p.use_category, p.sub_account_quota, p.annual_price_cents, \
|
|
|
- p.monthly_settle_cents, p.line_exits, p.derived_from_contract_item_id, p.first_referred_at";
|
|
|
+ p.plan_family, p.sub_account_quota, p.annual_price_cents, \
|
|
|
+ p.monthly_settle_cents, p.line_exits, p.first_referred_at";
|
|
|
|
|
|
// ───────────────────────── 入参 ─────────────────────────
|
|
|
|
|
|
@@ -135,8 +138,8 @@ pub struct SubscriptionCreateReq {
|
|
|
pub start_date: Option<String>,
|
|
|
/// 唯一可录的周期入参;缺省按年(§5.2)。
|
|
|
pub term: Option<String>,
|
|
|
- /// 仅当所选档位未预置用途分类(合同条目派生档)时必录(§5.2)。
|
|
|
- pub use_category: Option<String>,
|
|
|
+ /// 交付形态(只读确认位):只能与所选档位的 `plan_family` 相同,不同即 `11103`。
|
|
|
+ pub plan_family: Option<String>,
|
|
|
pub trial_months: Option<i64>,
|
|
|
pub discount_monthly_cents: Option<i64>,
|
|
|
/// 档位份数(2026-11-17):缺省 1;仅可叠加档(叠加增值)允许 > 1,其余档给 > 1 即拒。
|
|
|
@@ -444,7 +447,8 @@ pub(crate) struct SubRow {
|
|
|
#[serde(deserialize_with = "de::as_string")]
|
|
|
pub(crate) status: String,
|
|
|
#[serde(deserialize_with = "de::as_opt_string")]
|
|
|
- pub(crate) use_category: Option<String>,
|
|
|
+ pub(crate) plan_family: Option<String>,
|
|
|
+ /// 历史值:三分类收敛后新行恒 `NULL`(I6 停用),读侧只做只读回显。
|
|
|
#[serde(deserialize_with = "de::as_opt_string")]
|
|
|
pub(crate) sub_mode: Option<String>,
|
|
|
pub(crate) line_exits: Option<Value>,
|
|
|
@@ -524,7 +528,7 @@ fn to_json(
|
|
|
"plan_owner_customer_id": r.plan_owner_customer_id,
|
|
|
"status": r.status,
|
|
|
"status_display": { "key": key, "label": label, "note": note },
|
|
|
- "use_category": r.use_category,
|
|
|
+ "plan_family": r.plan_family,
|
|
|
"sub_mode": r.sub_mode,
|
|
|
"line_exits": exits_json(r.line_exits.as_ref()),
|
|
|
"line_exit_key": r.line_exit_key,
|
|
|
@@ -638,7 +642,7 @@ pub(crate) async fn subs_of_customer(state: &AppState, customer_id: &str) -> Res
|
|
|
state.db.query_all::<SubRow>(&sql, p.values()).await
|
|
|
}
|
|
|
|
|
|
-/// 档位行(含可用区间与派生链)。`succession` 公共层读同一形态 ⇒ 字段 `pub(crate)`。
|
|
|
+/// 档位行(含可用区间与首次引用打点)。`succession` 公共层读同一形态 ⇒ 字段 `pub(crate)`。
|
|
|
#[derive(Deserialize)]
|
|
|
pub(crate) struct PlanRow {
|
|
|
#[serde(deserialize_with = "de::as_string")]
|
|
|
@@ -651,8 +655,9 @@ pub(crate) struct PlanRow {
|
|
|
pub(crate) customer_id: Option<String>,
|
|
|
#[serde(deserialize_with = "de::as_string")]
|
|
|
pub(crate) status: String,
|
|
|
+ /// 交付形态 `link`/`addon`:条目与订阅的形态**只从这里来**(库上 `ck_plan_family` 兜底)。
|
|
|
#[serde(deserialize_with = "de::as_opt_string")]
|
|
|
- pub(crate) use_category: Option<String>,
|
|
|
+ pub(crate) plan_family: Option<String>,
|
|
|
#[serde(deserialize_with = "de::as_opt_string")]
|
|
|
pub(crate) sub_account_quota: Option<String>,
|
|
|
#[serde(deserialize_with = "de::as_string")]
|
|
|
@@ -660,20 +665,30 @@ pub(crate) struct PlanRow {
|
|
|
#[serde(deserialize_with = "de::as_opt_string")]
|
|
|
pub(crate) monthly_settle_cents: Option<String>,
|
|
|
pub(crate) line_exits: Option<Value>,
|
|
|
- #[serde(deserialize_with = "de::as_opt_string")]
|
|
|
- pub(crate) derived_from_contract_item_id: Option<String>,
|
|
|
/// 首次引用打点:`Some` ⇒ 该档已被引用过(§5.2「已引用过的档位⛔ 下架」同源)。
|
|
|
#[serde(deserialize_with = "de::as_opt_string")]
|
|
|
pub(crate) first_referred_at: Option<String>,
|
|
|
}
|
|
|
|
|
|
impl PlanRow {
|
|
|
+ /// 交付形态(库上 `ck_plan_family` 已保证只有两值;缺失/脏值按 `link` 处理并落读侧告警)。
|
|
|
+ pub(crate) fn family(&self) -> PlanFamily {
|
|
|
+ match self.plan_family.as_deref().map(PlanFamily::parse) {
|
|
|
+ Some(Ok(f)) => f,
|
|
|
+ _ => {
|
|
|
+ tracing::warn!(plan_id = %self.plan_id, raw = ?self.plan_family, "档位交付形态缺失/非法,按 link 处理");
|
|
|
+ PlanFamily::Link
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
pub(crate) fn to_ref(&self) -> PlanRef {
|
|
|
PlanRef {
|
|
|
plan_id: self.plan_id.clone(),
|
|
|
plan_name: self.plan_name.clone(),
|
|
|
plan_kind: self.plan_kind.clone(),
|
|
|
owner_customer_id: opt_trim(self.customer_id.as_ref()).map(str::to_string),
|
|
|
+ plan_family: self.family(),
|
|
|
sub_account_quota: self
|
|
|
.sub_account_quota
|
|
|
.as_deref()
|
|
|
@@ -756,31 +771,26 @@ pub(crate) async fn contract_row(state: &AppState, contract_id: &str) -> Result<
|
|
|
state.db.query_one::<ContractRow>(&sql, p.values()).await
|
|
|
}
|
|
|
|
|
|
-/// 合同服务构成条目(`custom_spec`=合同条目派生档的来源)。`succession` 共用 ⇒ 字段 `pub(crate)`。
|
|
|
+/// 合同服务构成条目(`plan_id` 必填:条目只剩「档位 × 份数」)。`succession` 共用 ⇒ 字段 `pub(crate)`。
|
|
|
#[derive(Deserialize)]
|
|
|
pub(crate) struct ItemRow {
|
|
|
#[serde(deserialize_with = "de::as_string")]
|
|
|
pub(crate) item_id: String,
|
|
|
#[serde(deserialize_with = "de::as_string")]
|
|
|
pub(crate) contract_id: String,
|
|
|
- #[serde(deserialize_with = "de::as_string")]
|
|
|
- pub(crate) item_kind: String,
|
|
|
#[serde(deserialize_with = "de::as_opt_string")]
|
|
|
pub(crate) plan_id: Option<String>,
|
|
|
- /// 套餐式条目的档位份数(2026-11-17:随条目带入订阅 `subscription.quantity`)。
|
|
|
+ /// 档位份数(2026-11-17:随条目带入订阅 `subscription.quantity`)。
|
|
|
#[serde(deserialize_with = "de::as_opt_string")]
|
|
|
pub(crate) quantity: Option<String>,
|
|
|
+ /// 交付形态(落库列;=所引档位的 `plan_family`,应用层已校验一致)。
|
|
|
#[serde(deserialize_with = "de::as_opt_string")]
|
|
|
- pub(crate) derived_plan_id: Option<String>,
|
|
|
- #[serde(deserialize_with = "de::as_opt_string")]
|
|
|
- pub(crate) use_category: Option<String>,
|
|
|
- #[serde(deserialize_with = "de::as_opt_string")]
|
|
|
- pub(crate) annual_price_cents: Option<String>,
|
|
|
+ pub(crate) item_family: Option<String>,
|
|
|
pub(crate) line_exits: Option<Value>,
|
|
|
}
|
|
|
|
|
|
-const ITEM_COLS: &str = "SELECT i.item_id, i.contract_id, i.item_kind, i.plan_id, i.quantity, \
|
|
|
- i.derived_plan_id, i.use_category, i.annual_price_cents, i.line_exits";
|
|
|
+const ITEM_COLS: &str = "SELECT i.item_id, i.contract_id, i.plan_id, i.quantity, \
|
|
|
+ i.item_family, i.line_exits";
|
|
|
|
|
|
pub(crate) async fn items_of_contracts(
|
|
|
state: &AppState,
|
|
|
@@ -852,8 +862,8 @@ async fn opened_map_for(
|
|
|
|
|
|
/// `POST /subscription/plan_options@v1`。
|
|
|
///
|
|
|
-/// 两段候选=本客户自定义档(**含合同条目派生档**)+全部固定档,⛔ 他客户自定义档;
|
|
|
-/// 每行带只读 `sub_mode`(`sub_mode_of(用途分类)`)与可选性/不可选原因。
|
|
|
+/// 两段候选=本客户自定义档 + 全部固定档,⛔ 他客户自定义档;
|
|
|
+/// 每行带只读 `plan_family`(交付形态)与可选性/不可选原因。
|
|
|
pub async fn plan_options(
|
|
|
State(state): State<AppState>,
|
|
|
RequireAuth(auth): RequireAuth,
|
|
|
@@ -900,24 +910,13 @@ pub async fn plan_options(
|
|
|
.map(|o| {
|
|
|
let pr = o.to_ref();
|
|
|
let (available, reason) = plan_selectability(&pr, &cid);
|
|
|
- let derived = o.derived_from_contract_item_id.is_some();
|
|
|
- let sub_mode = match opt_trim(o.use_category.as_ref()) {
|
|
|
- Some(uc) => UseCategory::parse(uc)
|
|
|
- .ok()
|
|
|
- .map(|c| sub_mode_of(c).as_str().to_string()),
|
|
|
- None => None,
|
|
|
- };
|
|
|
let rc = referred.get(&o.plan_id).copied().unwrap_or(0);
|
|
|
json!({
|
|
|
"plan_id": o.plan_id,
|
|
|
"plan_name": o.plan_name,
|
|
|
"plan_kind": o.plan_kind,
|
|
|
"owner_customer_id": o.customer_id,
|
|
|
- "derived": derived,
|
|
|
- "source_label": if derived { Some("合同条目派生") } else { None },
|
|
|
- "derived_from_contract_item_id": o.derived_from_contract_item_id,
|
|
|
- "use_category": o.use_category,
|
|
|
- "sub_mode": sub_mode,
|
|
|
+ "plan_family": pr.plan_family.as_str(),
|
|
|
"annual_price_cents": as_num(&o.annual_price_cents),
|
|
|
"monthly_settle_cents": as_num(&o.monthly().to_string()),
|
|
|
"sub_account_quota": as_opt_num(o.sub_account_quota.as_deref()),
|
|
|
@@ -934,8 +933,8 @@ pub async fn plan_options(
|
|
|
"items": items,
|
|
|
"total": items.len(),
|
|
|
"notes": [
|
|
|
- "「订阅方式」由用途分类经 sub_mode_of 折算,前端不重实现;派生自定义档未预置用途分类时\
|
|
|
- sub_mode=null,新订时由运营必录用途分类。",
|
|
|
+ "「交付形态」(link/addon)与额度、带宽、出口一并随档位,前端不重实现;\
|
|
|
+ 附加项额度恒 0,条目/订阅份数 ≥1;跨境链路份数恒 1。",
|
|
|
TICKET_UNCONFIGURED_NOTE,
|
|
|
],
|
|
|
"columns_pending": COLUMNS_PENDING,
|
|
|
@@ -970,7 +969,7 @@ pub(crate) struct SubAuditView {
|
|
|
status: String,
|
|
|
plan_id: Option<String>,
|
|
|
plan_name: Option<String>,
|
|
|
- use_category: Option<String>,
|
|
|
+ plan_family: Option<String>,
|
|
|
sub_mode: Option<String>,
|
|
|
line_exits: Option<Value>,
|
|
|
term: Option<String>,
|
|
|
@@ -994,7 +993,7 @@ impl SubAuditView {
|
|
|
status: r.status.clone(),
|
|
|
plan_id: r.plan_id.clone(),
|
|
|
plan_name: r.plan_name.clone(),
|
|
|
- use_category: r.use_category.clone(),
|
|
|
+ plan_family: r.plan_family.clone(),
|
|
|
sub_mode: r.sub_mode.clone(),
|
|
|
line_exits: r.line_exits.clone(),
|
|
|
term: r.term.clone(),
|
|
|
@@ -1018,7 +1017,7 @@ impl SubAuditView {
|
|
|
"status": self.status,
|
|
|
"plan_id": self.plan_id,
|
|
|
"plan_name": self.plan_name,
|
|
|
- "use_category": self.use_category,
|
|
|
+ "plan_family": self.plan_family,
|
|
|
"sub_mode": self.sub_mode,
|
|
|
"line_exits": self.line_exits,
|
|
|
"term": self.term,
|
|
|
@@ -1203,11 +1202,7 @@ pub async fn contract_options(
|
|
|
Err(e) => return json_err(e),
|
|
|
}
|
|
|
}
|
|
|
- let plan_ids: Vec<String> = items
|
|
|
- .iter()
|
|
|
- .flat_map(|i| [i.plan_id.clone(), i.derived_plan_id.clone()])
|
|
|
- .flatten()
|
|
|
- .collect();
|
|
|
+ let plan_ids: Vec<String> = items.iter().filter_map(|i| i.plan_id.clone()).collect();
|
|
|
let plans = match plan_rows(&state, &plan_ids).await {
|
|
|
Ok(v) => v,
|
|
|
Err(e) => return json_err(e),
|
|
|
@@ -1219,7 +1214,7 @@ pub async fn contract_options(
|
|
|
.iter()
|
|
|
.filter(|i| i.contract_id == c.contract_id && !referenced.contains(&i.item_id))
|
|
|
.map(|i| {
|
|
|
- let pid = opt_trim(i.derived_plan_id.as_ref()).or_else(|| opt_trim(i.plan_id.as_ref()));
|
|
|
+ let pid = opt_trim(i.plan_id.as_ref());
|
|
|
let pr = pid.and_then(|id| plan_by_id.get(id)).map(|r| r.to_ref());
|
|
|
let (available, reason) = match pr.as_ref() {
|
|
|
Some(pr) => plan_selectability(pr, &cid),
|
|
|
@@ -1227,18 +1222,13 @@ pub async fn contract_options(
|
|
|
};
|
|
|
json!({
|
|
|
"item_id": i.item_id,
|
|
|
- "item_kind": i.item_kind,
|
|
|
"plan_id": pid,
|
|
|
"plan_name": pr.as_ref().map(|p| p.plan_name.clone()),
|
|
|
- "derived": i.item_kind == "custom_spec",
|
|
|
- "source_label": if i.item_kind == "custom_spec" { Some("合同条目派生") } else { None },
|
|
|
- "use_category": i.use_category,
|
|
|
- // 套餐式条目⛔ 存价(02 册 §2.2「价格随所引档位,条目上⛔ 不另存」)⇒ 库里是
|
|
|
- // NULL,这里从所引档位取;自定义式条目=业主录入值照实投影(P-47 入库口径)。
|
|
|
- "annual_price_cents": match i.annual_price_cents.as_deref() {
|
|
|
- Some(_) => as_opt_num(i.annual_price_cents.as_deref()),
|
|
|
- None => json!(pr.as_ref().map(|p| p.annual_price_cents)),
|
|
|
- },
|
|
|
+ "item_family": i.item_family,
|
|
|
+ // 份数仍在条目上(订阅 `quantity` 由它带入;`addon` 可 > 1,`link` 恒 1)。
|
|
|
+ "quantity": i.quantity,
|
|
|
+ // 价格随所引档位(条目上不再有价格列)⇒ 一律从档位投影。
|
|
|
+ "annual_price_cents": pr.as_ref().map(|p| p.annual_price_cents),
|
|
|
"line_exits": exits_json(i.line_exits.as_ref()),
|
|
|
"available": available,
|
|
|
"unavailable_reason": reason,
|
|
|
@@ -1289,30 +1279,22 @@ fn push_in_owned(p: &mut Params, col: &str, ids: &[String]) -> Result<String, Bo
|
|
|
|
|
|
// ───────────────────────── 写侧:共用折算 ─────────────────────────
|
|
|
|
|
|
-/// 用途分类:由套餐带入(⛔ 改写);派生自定义档未预置时由运营必录(§5.2)。
|
|
|
-fn category_of(payload: Option<&String>, plan: &PlanRow) -> Result<UseCategory, BosError> {
|
|
|
- match opt_trim(plan.use_category.as_ref()) {
|
|
|
- Some(v) => {
|
|
|
- let plan_cat = UseCategory::parse(v)?;
|
|
|
- if let Some(raw) = opt_trim(payload) {
|
|
|
- let got = UseCategory::parse(raw)?;
|
|
|
- if got != plan_cat {
|
|
|
- return Err(BosError::bad_params(format!(
|
|
|
- "use_category 由套餐用途分类带入({}),不改写为 {}",
|
|
|
- plan_cat.as_str(),
|
|
|
- got.as_str()
|
|
|
- )));
|
|
|
- }
|
|
|
- }
|
|
|
- Ok(plan_cat)
|
|
|
+/// 交付形态:**只读档位**(D-15:条目/订阅都不再是第二来源)。
|
|
|
+///
|
|
|
+/// 载荷里带了不同值 ⇒ `11103`(⛔ 静默改写);带了同值=确认回显,放行。
|
|
|
+fn family_of(payload: Option<&String>, plan: &PlanRow) -> Result<PlanFamily, BosError> {
|
|
|
+ let plan_family = plan.family();
|
|
|
+ if let Some(raw) = opt_trim(payload) {
|
|
|
+ let got = PlanFamily::parse(raw)?;
|
|
|
+ if got != plan_family {
|
|
|
+ return Err(BosError::bad_params(format!(
|
|
|
+ "plan_family 由套餐带入({}),不改写为 {}",
|
|
|
+ plan_family.as_str(),
|
|
|
+ got.as_str()
|
|
|
+ )));
|
|
|
}
|
|
|
- None => match opt_trim(payload) {
|
|
|
- Some(v) => UseCategory::parse(v),
|
|
|
- None => Err(BosError::bad_params(
|
|
|
- "use_category 必录:所选档位未预置用途分类(合同条目派生档),由运营选定后带入订阅方式",
|
|
|
- )),
|
|
|
- },
|
|
|
}
|
|
|
+ Ok(plan_family)
|
|
|
}
|
|
|
|
|
|
/// 周期与价格的派生结果(§5.2 段 3/4)。`succession` 公共层共用 ⇒ 字段 `pub(crate)`。
|
|
|
@@ -1367,35 +1349,6 @@ pub(crate) fn derive_exits(item: Option<&ItemRow>, plan: &PlanRow) -> Result<Nor
|
|
|
normalize_line_exits(&raw)
|
|
|
}
|
|
|
|
|
|
-/// I6 取数:该客户全部订阅(判据在 `check_i6_subscription` 内部,本处⛔ 预筛状态)。
|
|
|
-pub(crate) async fn i6_rows(state: &AppState, customer_id: &str, exclude: Option<&str>) -> Result<Vec<SubRow>, BosError> {
|
|
|
- Ok(subs_of_customer(state, customer_id)
|
|
|
- .await?
|
|
|
- .into_iter()
|
|
|
- .filter(|r| Some(r.subscription_id.as_str()) != exclude)
|
|
|
- .collect())
|
|
|
-}
|
|
|
-
|
|
|
-pub(crate) fn i6_borrow(rows: &[SubRow]) -> Vec<ActiveSubscriptionRow<'_>> {
|
|
|
- rows.iter()
|
|
|
- .map(|r| ActiveSubscriptionRow {
|
|
|
- subscription_id: r.subscription_id.as_str(),
|
|
|
- customer_id: r.customer_id.as_str(),
|
|
|
- plan_name: r.plan_name.as_deref().unwrap_or_default(),
|
|
|
- use_category: r.use_category.as_deref().unwrap_or_default(),
|
|
|
- line_exit_key: r.line_exit_key.as_deref().unwrap_or_default(),
|
|
|
- sub_mode: r.sub_mode.as_deref().unwrap_or_default(),
|
|
|
- status: r.status.as_str(),
|
|
|
- start_date: r.start_date.as_deref().unwrap_or_default(),
|
|
|
- end_date: r
|
|
|
- .end_date
|
|
|
- .as_deref()
|
|
|
- .or(r.expire_date.as_deref())
|
|
|
- .unwrap_or_default(),
|
|
|
- })
|
|
|
- .collect()
|
|
|
-}
|
|
|
-
|
|
|
/// 额度对齐目标(§5.6 唯一求和实现=`in_use_quota_sum_batch`,⛔ 本口另实现一套计数)。
|
|
|
///
|
|
|
/// `extra_plan` = 本次要写入/要摘除的订阅所引档位:`Some` 计入(新订、升降配),
|
|
|
@@ -1506,7 +1459,7 @@ pub(crate) async fn align_quota_for_customers(
|
|
|
let in_s: Vec<String> = IN_USE_STATUS.iter().map(|s| s.to_string()).collect();
|
|
|
let in_s = push_in(&mut p, "s.status", &in_s, "text");
|
|
|
let sql = format!(
|
|
|
- "SELECT s.customer_id, s.plan_id FROM subscription s \
|
|
|
+ "SELECT s.customer_id, s.plan_id, s.subscription_id, s.quota_slots FROM subscription s \
|
|
|
WHERE {in_c} AND {in_s} AND s.plan_id IS NOT NULL"
|
|
|
);
|
|
|
#[derive(Deserialize)]
|
|
|
@@ -1515,6 +1468,10 @@ pub(crate) async fn align_quota_for_customers(
|
|
|
customer_id: String,
|
|
|
#[serde(deserialize_with = "de::as_string")]
|
|
|
plan_id: String,
|
|
|
+ #[serde(deserialize_with = "de::as_string")]
|
|
|
+ subscription_id: String,
|
|
|
+ #[serde(deserialize_with = "de::as_string")]
|
|
|
+ quota_slots: String,
|
|
|
}
|
|
|
let sub_rows = state.db.query_all::<SubPlanRow>(&sql, p.values()).await?;
|
|
|
|
|
|
@@ -1567,12 +1524,18 @@ pub(crate) async fn align_quota_for_customers(
|
|
|
continue;
|
|
|
}
|
|
|
};
|
|
|
- // D-41(需求§4.2):额度目标一旦算出就顺手把分账号**补建**到位——⛔ 只在「额度发生
|
|
|
- // 变化」时才补(主账号晚于订阅开通、上一轮补建失败的客户,都靠这里在后续轮次收敛),
|
|
|
- // 且**只补不删**(缩减侧只记说明)。失败⛔ 影响额度结论,单独记一条等下一轮继续补。
|
|
|
- let seats = super::sub_provision::align_sub_accounts(state, cid, target).await;
|
|
|
- if let Some(reason) = &seats.failed {
|
|
|
- out.failed.push((cid.clone(), format!("分账号补建:{reason}")));
|
|
|
+ // D-41(需求§4.2)+M4:席位按**订阅**补建(同一客户多份订阅各管各的席位包)。
|
|
|
+ // ⛔ 只在「额度发生变化」时才补(主账号晚于订阅开通、上一轮补建失败的订阅,都靠这里
|
|
|
+ // 在后续轮次收敛),且**只补不删**(缩减侧只记说明)。失败⛔ 影响额度结论。
|
|
|
+ for r in sub_rows.iter().filter(|r| r.customer_id == *cid) {
|
|
|
+ let t = r.quota_slots.trim().parse::<i64>().unwrap_or(0);
|
|
|
+ let seats = super::sub_provision::align_seats_of(state, &r.subscription_id, t).await;
|
|
|
+ if let Some(reason) = &seats.failed {
|
|
|
+ out.failed.push((
|
|
|
+ cid.clone(),
|
|
|
+ format!("订阅 {} 分账号补建:{reason}", r.subscription_id),
|
|
|
+ ));
|
|
|
+ }
|
|
|
}
|
|
|
if before == target {
|
|
|
continue;
|
|
|
@@ -1609,7 +1572,7 @@ pub(crate) fn insert_stmt(v: &NewValues) -> DmlStatement {
|
|
|
"plan_name",
|
|
|
"plan_kind",
|
|
|
"plan_owner_customer_id",
|
|
|
- "use_category",
|
|
|
+ "plan_family",
|
|
|
"sub_mode",
|
|
|
"line_exits",
|
|
|
"line_exit_key",
|
|
|
@@ -1633,8 +1596,8 @@ pub(crate) fn insert_stmt(v: &NewValues) -> DmlStatement {
|
|
|
v.plan_owner_customer_id.clone().map(Value::String).unwrap_or(Value::Null),
|
|
|
"text",
|
|
|
),
|
|
|
- ph(&mut p, Value::String(v.use_category.clone()), "text"),
|
|
|
- ph(&mut p, Value::String(v.sub_mode.clone()), "text"),
|
|
|
+ ph(&mut p, Value::String(v.plan_family.clone()), "text"),
|
|
|
+ ph(&mut p, v.sub_mode.clone().map(Value::String).unwrap_or(Value::Null), "text"),
|
|
|
ph(&mut p, v.line_exits.clone(), "text::jsonb"),
|
|
|
ph(&mut p, Value::String(v.line_exit_key.clone()), "text"),
|
|
|
ph(&mut p, Value::String(v.term.clone()), "text"),
|
|
|
@@ -1705,8 +1668,9 @@ pub(crate) struct NewValues {
|
|
|
pub(crate) plan_name: String,
|
|
|
pub(crate) plan_kind: String,
|
|
|
pub(crate) plan_owner_customer_id: Option<String>,
|
|
|
- pub(crate) use_category: String,
|
|
|
- pub(crate) sub_mode: String,
|
|
|
+ pub(crate) plan_family: String,
|
|
|
+ /// 历史列:三分类收敛后新行恒 `None`(I6 停用,不再派生订阅方式)。
|
|
|
+ pub(crate) sub_mode: Option<String>,
|
|
|
pub(crate) line_exits: Value,
|
|
|
pub(crate) line_exit_key: String,
|
|
|
pub(crate) term: String,
|
|
|
@@ -1904,7 +1868,7 @@ pub async fn create(
|
|
|
None => None,
|
|
|
};
|
|
|
if let Some(i) = item {
|
|
|
- if let Some(ip) = opt_trim(i.plan_id.as_ref()).or_else(|| opt_trim(i.derived_plan_id.as_ref())) {
|
|
|
+ if let Some(ip) = opt_trim(i.plan_id.as_ref()) {
|
|
|
if ip != pid {
|
|
|
return json_err(BosError::klt(
|
|
|
KltCode::M228,
|
|
|
@@ -1952,7 +1916,7 @@ pub async fn create(
|
|
|
Ok(v) => v,
|
|
|
Err(e) => return json_err(e),
|
|
|
};
|
|
|
- let category = match category_of(body.use_category.as_ref(), &plan) {
|
|
|
+ let family = match family_of(body.plan_family.as_ref(), &plan) {
|
|
|
Ok(v) => v,
|
|
|
Err(e) => return json_err(e),
|
|
|
};
|
|
|
@@ -1963,40 +1927,19 @@ pub async fn create(
|
|
|
Ok(v) => v,
|
|
|
Err(e) => return json_err(e),
|
|
|
};
|
|
|
- // 档位份数(2026-11-17):缺省 1;只有可叠加档(用途分类=叠加增值)允许 > 1,
|
|
|
- // 其余档(仅可升降配/单独约定)份数恒 1 —— 一份条目一份订阅,数量不是条目数。
|
|
|
- let sub_mode = sub_mode_of(category);
|
|
|
+ // 份数(D-8 `ck_sub_qty`):`link` 恒 1(一条链路=一份),`addon` 1~9999。
|
|
|
let quantity = match body.quantity {
|
|
|
None => 1,
|
|
|
- Some(q) if q >= 1 => q,
|
|
|
- Some(q) => {
|
|
|
- return json_err(BosError::bad_params(format!(
|
|
|
- "quantity 必须是正整数(11103),收到 {q}"
|
|
|
- )))
|
|
|
- }
|
|
|
+ Some(q) => q,
|
|
|
};
|
|
|
- if quantity > 1 && sub_mode != SubMode::Stackable {
|
|
|
+ if !family.quantity_ok(quantity) {
|
|
|
return json_err(BosError::bad_params(format!(
|
|
|
- "档位 {} 的订阅方式为「{}」,份数必须为 1(收到 {quantity}):只有可叠加档(叠加增值)可按份叠加",
|
|
|
+ "档位 {} 的交付形态为「{}」,份数必须是{}(收到 {quantity})",
|
|
|
plan.plan_id,
|
|
|
- sub_mode.as_str()
|
|
|
+ family.as_str(),
|
|
|
+ if family == PlanFamily::Link { " 1" } else { " 1~9999" }
|
|
|
)));
|
|
|
}
|
|
|
- // ⑤ I6 ⇒ KLT-M224(带那份订阅的定位)
|
|
|
- let actives = match i6_rows(&state, &cid, None).await {
|
|
|
- Ok(v) => v,
|
|
|
- Err(e) => return json_err(e),
|
|
|
- };
|
|
|
- if let Err(e) = check_i6_subscription(
|
|
|
- &cid,
|
|
|
- category,
|
|
|
- &exits.line_exit_key,
|
|
|
- &period.start,
|
|
|
- &period.end,
|
|
|
- &i6_borrow(&actives),
|
|
|
- ) {
|
|
|
- return json_err(e);
|
|
|
- }
|
|
|
let id = match new_subscription_id(&state).await {
|
|
|
Ok(v) => v,
|
|
|
Err(e) => return json_err(e),
|
|
|
@@ -2009,8 +1952,8 @@ pub async fn create(
|
|
|
plan_name: plan.plan_name.clone(),
|
|
|
plan_kind: plan.plan_kind.clone(),
|
|
|
plan_owner_customer_id: opt_trim(plan.customer_id.as_ref()).map(str::to_string),
|
|
|
- use_category: category.as_str().to_string(),
|
|
|
- sub_mode: sub_mode.as_str().to_string(),
|
|
|
+ plan_family: family.as_str().to_string(),
|
|
|
+ sub_mode: None,
|
|
|
line_exits: Value::Array(exits.line_exits.iter().cloned().map(Value::String).collect()),
|
|
|
line_exit_key: exits.line_exit_key.clone(),
|
|
|
term: period.term.clone(),
|
|
|
@@ -2028,7 +1971,10 @@ pub async fn create(
|
|
|
replaces_subscription_id: None,
|
|
|
};
|
|
|
// 额度对齐目标=在册在用行 + 本行
|
|
|
- let mut for_quota = actives.clone();
|
|
|
+ let mut for_quota = match subs_of_customer(&state, &cid).await {
|
|
|
+ Ok(v) => v,
|
|
|
+ Err(e) => return json_err(e),
|
|
|
+ };
|
|
|
for_quota.push(SubRowPlaceholder::for_new(&id, &nv));
|
|
|
let target = match quota_target(&state, &cid, &for_quota).await {
|
|
|
Ok(v) => v,
|
|
|
@@ -2107,7 +2053,7 @@ pub async fn create(
|
|
|
// D-41(需求§4.2/客户册§5.2):订阅保存=额度变更落点,分账号由系统按套餐额度**自动补建**
|
|
|
// (⛔ 手工新增)。与交付工单同一形态:订阅主档先提交,补建在其后同步执行,失败⛔ 回滚
|
|
|
// 已提交的订阅,只把原因如实写回 `notes`+审计 `detail_json.sub_accounts`(⛔ 静默)。
|
|
|
- let seats = super::sub_provision::align_sub_accounts(&state, &cid, target).await;
|
|
|
+ let seats = super::sub_provision::align_seats_of(&state, &id, nv.quota_slots).await;
|
|
|
notes.push(seats.note());
|
|
|
// 后置审计 `subscription.create`(08 册 §8.1 CU-17/§8.3):此前该动作在 `bos_audit_log` 里
|
|
|
// 0 行=D-25。`before_json` 新增时为空;额度前后、两个金额与折算来源、order_no 冻结事实照记。
|
|
|
@@ -2116,7 +2062,7 @@ pub async fn create(
|
|
|
"layout": "multi",
|
|
|
"plan_id": nv.plan_id,
|
|
|
"plan_name_snapshot": nv.plan_name,
|
|
|
- "use_category": nv.use_category,
|
|
|
+ "plan_family": nv.plan_family,
|
|
|
"sub_mode": nv.sub_mode,
|
|
|
"line_exits": nv.line_exits,
|
|
|
"line_exit_key": nv.line_exit_key,
|
|
|
@@ -2170,7 +2116,7 @@ pub async fn create(
|
|
|
"annual_price_cents": period.annual,
|
|
|
"monthly_settle_cents": period.monthly,
|
|
|
"line_exit_key": exits.line_exit_key,
|
|
|
- "sub_mode": sub_mode_of(category).as_str(),
|
|
|
+ "plan_family": family.as_str(),
|
|
|
},
|
|
|
}),
|
|
|
)
|
|
|
@@ -2331,7 +2277,7 @@ pub async fn change(
|
|
|
let item = match opt_trim(body.contract_item_id.as_ref()) {
|
|
|
Some(iid) => match items.iter().find(|i| i.item_id == iid) {
|
|
|
Some(i) => {
|
|
|
- let bound = opt_trim(i.plan_id.as_ref()).or_else(|| opt_trim(i.derived_plan_id.as_ref()));
|
|
|
+ let bound = opt_trim(i.plan_id.as_ref());
|
|
|
if let Some(ip) = bound {
|
|
|
if ip != pid {
|
|
|
return json_err(BosError::klt(
|
|
|
@@ -2349,26 +2295,10 @@ pub async fn change(
|
|
|
))
|
|
|
}
|
|
|
},
|
|
|
- None => items.iter().find(|i| {
|
|
|
- opt_trim(i.plan_id.as_ref()) == Some(pid.as_str())
|
|
|
- || opt_trim(i.derived_plan_id.as_ref()) == Some(pid.as_str())
|
|
|
- }),
|
|
|
- };
|
|
|
- // 升降配下派生档必须已有用途分类(载荷⛔ 带 use_category)。
|
|
|
- let category = match opt_trim(plan.use_category.as_ref()) {
|
|
|
- Some(v) => match UseCategory::parse(v) {
|
|
|
- Ok(c) => c,
|
|
|
- Err(e) => return json_err(e),
|
|
|
- },
|
|
|
- None => {
|
|
|
- return json_err(BosError::klt(
|
|
|
- KltCode::M228,
|
|
|
- format!(
|
|
|
- "档位不可选:派生自定义档 {pid} 未预置用途分类,而升降配不录入 use_category(请先在第 1 步把分类带入档位)"
|
|
|
- ),
|
|
|
- ))
|
|
|
- }
|
|
|
+ None => items.iter().find(|i| opt_trim(i.plan_id.as_ref()) == Some(pid.as_str())),
|
|
|
};
|
|
|
+ // 交付形态只读档位(载荷⛔ 带 plan_family)。
|
|
|
+ let family = plan.family();
|
|
|
if let Err(e) = check_i13_trial_months(body.trial_months, body.discount_monthly_cents) {
|
|
|
return json_err(e);
|
|
|
}
|
|
|
@@ -2406,9 +2336,6 @@ pub async fn change(
|
|
|
Err(e) => return json_err(e),
|
|
|
};
|
|
|
let planned_once = std::slice::from_ref(&planned);
|
|
|
- if let Err(e) = super::succession::check_i6_final(&cid, &existing, planned_once, &[]) {
|
|
|
- return json_err(e);
|
|
|
- }
|
|
|
let nv = planned.new.clone();
|
|
|
let period = planned.period.clone();
|
|
|
let prev_end = planned.prev_end.clone();
|
|
|
@@ -2425,9 +2352,16 @@ pub async fn change(
|
|
|
Err(e) => return json_err(e),
|
|
|
};
|
|
|
let mut stmts: Vec<DmlStatement> = super::succession::change_dml(&planned);
|
|
|
- if before != target {
|
|
|
+ let has_quota = before != target;
|
|
|
+ if has_quota {
|
|
|
stmts.push(quota_stmt(&cid, target));
|
|
|
}
|
|
|
+ // M4:席位先改挂到新订阅(0 行=原订阅本就没有席位,属正常)——放在断言语句之后,
|
|
|
+ // ⛔ 计入 `assert_batch` 的期望行数。
|
|
|
+ stmts.push(super::succession::seat_migration_stmt(
|
|
|
+ &planned.orig.subscription_id,
|
|
|
+ &planned.new.subscription_id,
|
|
|
+ ));
|
|
|
let results = match state.db.dml_batch(&stmts).await {
|
|
|
Ok(r) => r,
|
|
|
Err(e) => return json_err(e),
|
|
|
@@ -2436,7 +2370,7 @@ pub async fn change(
|
|
|
.iter()
|
|
|
.map(|s| (*s, 1))
|
|
|
.collect();
|
|
|
- if stmts.len() > 3 {
|
|
|
+ if has_quota {
|
|
|
want.push(("客户额度对齐", 1));
|
|
|
}
|
|
|
if let Err(e) = assert_batch(&results, &want) {
|
|
|
@@ -2506,7 +2440,7 @@ pub async fn change(
|
|
|
notes.push(delivery.note.clone());
|
|
|
// D-41(需求§4.2):升降配=额度变更落点。升配侧按新额度补建席位;降配侧**只补不删**,
|
|
|
// 超出的席位⛔ 删号(随到期停机转停用),差异如实写回 `notes`+审计 `detail_json.sub_accounts`。
|
|
|
- let seats = super::sub_provision::align_sub_accounts(&state, &cid, target).await;
|
|
|
+ let seats = super::sub_provision::align_seats_of(&state, &id, nv.quota_slots).await;
|
|
|
notes.push(seats.note());
|
|
|
// 后置审计 `subscription.replace`(08 册 §8.1 CU-17/§8.3=`pair` 版式:原订阅 `end_date`
|
|
|
// 与 `status` 记原值→新值,另记原/新合同编号)。此前升降配全程 0 行审计。
|
|
|
@@ -2555,7 +2489,7 @@ pub async fn change(
|
|
|
"annual_price_cents": period.annual,
|
|
|
"monthly_settle_cents": period.monthly,
|
|
|
"line_exit_key": exits.line_exit_key,
|
|
|
- "sub_mode": sub_mode_of(category).as_str(),
|
|
|
+ "plan_family": family.as_str(),
|
|
|
"contract_id": contract_id,
|
|
|
"contract_item_id": nv.contract_item_id,
|
|
|
},
|
|
|
@@ -2579,8 +2513,8 @@ impl SubRowPlaceholder {
|
|
|
plan_kind: Some(v.plan_kind.clone()),
|
|
|
plan_owner_customer_id: v.plan_owner_customer_id.clone(),
|
|
|
status: v.status.clone(),
|
|
|
- use_category: Some(v.use_category.clone()),
|
|
|
- sub_mode: Some(v.sub_mode.clone()),
|
|
|
+ plan_family: Some(v.plan_family.clone()),
|
|
|
+ sub_mode: v.sub_mode.clone(),
|
|
|
line_exits: None,
|
|
|
line_exit_key: Some(v.line_exit_key.clone()),
|
|
|
start_date: Some(v.start_date.clone()),
|
|
|
@@ -2928,7 +2862,7 @@ pub async fn delete(
|
|
|
];
|
|
|
// D-41(需求§4.2):删除订阅=额度缩减侧 ⇒ 分账号**只补不删**,超出的席位一行不删(随到期
|
|
|
// 停机转停用),只把「为什么不删」如实写回 `notes`+审计 `detail_json.sub_accounts`。
|
|
|
- let seats = super::sub_provision::align_sub_accounts(&state, &r.customer_id, target).await;
|
|
|
+ let seats = super::sub_provision::align_seats_of(&state, &r.subscription_id, 0).await;
|
|
|
notes.push(seats.note());
|
|
|
// 后置审计 `subscription.delete`(08 册 §8.1 CU-19/§8.3):真删 ⇒ `before_json` 记完整关键字段
|
|
|
// 快照、`after_json` 为空;额度前后/释放值、账单口径未接线声明、打点不回滚声明照记。
|
|
|
@@ -3378,7 +3312,7 @@ pub async fn get(
|
|
|
},
|
|
|
"amount": money_json(&r),
|
|
|
"exits": {
|
|
|
- "use_category": r.use_category,
|
|
|
+ "plan_family": r.plan_family,
|
|
|
"sub_mode": r.sub_mode,
|
|
|
"line_exits": exits_json(r.line_exits.as_ref()),
|
|
|
"line_exit_key": r.line_exit_key,
|
|
|
@@ -3655,7 +3589,7 @@ mod tests {
|
|
|
|
|
|
#[test]
|
|
|
fn 新建载荷的只读派生列逐列点名拒收() {
|
|
|
- let m = map(r#"{"annual_price_cents":1,"line_exits":["美国"],"sub_mode":"upgrade","end_date":"2027-01-01","order_no":"X","actual_opened_at":"2026-01-01","monthly_settle_cents":2,"term":"monthly"}"#);
|
|
|
+ let m = map(r#"{"annual_price_cents":1,"line_exits":["美国"],"sub_mode":"upgrade","end_date":"2027-01-01","order_no":"X","actual_opened_at":"2026-01-01","monthly_settle_cents":2,"term":"monthly","item_family":"addon"}"#);
|
|
|
let e = reject_read_only(&m, &CREATE_READ_ONLY).unwrap_err();
|
|
|
let s = format!("{e}");
|
|
|
for k in [
|
|
|
@@ -3666,6 +3600,8 @@ mod tests {
|
|
|
"sub_mode",
|
|
|
"order_no",
|
|
|
"actual_opened_at",
|
|
|
+ // D-15:条目形态随条目带入,订阅载荷⛔ 再收
|
|
|
+ "item_family",
|
|
|
] {
|
|
|
assert!(s.contains(k), "未点名 {k}:{s}");
|
|
|
}
|
|
|
@@ -3702,8 +3638,8 @@ mod tests {
|
|
|
plan_kind: Some("fixed".to_string()),
|
|
|
plan_owner_customer_id: None,
|
|
|
status: status.to_string(),
|
|
|
- use_category: Some("office".to_string()),
|
|
|
- sub_mode: Some("upgrade".to_string()),
|
|
|
+ plan_family: Some("link".to_string()),
|
|
|
+ sub_mode: None,
|
|
|
line_exits: None,
|
|
|
line_exit_key: Some("中国香港".to_string()),
|
|
|
start_date: start.map(str::to_string),
|
|
|
@@ -3793,7 +3729,6 @@ mod tests {
|
|
|
!src.contains(concat!(".me", "ssage()")),
|
|
|
"BosError 不提供该访问器"
|
|
|
);
|
|
|
- assert!(src.contains("check_i6_subscription"), "I6 判据必须走不变量库");
|
|
|
assert!(src.contains("check_subscription_deletable"), "删除阻塞必须走不变量库(M227/M230 同源)");
|
|
|
}
|
|
|
}
|