|
|
@@ -1595,9 +1595,8 @@ pub(crate) async fn align_quota_for_customers(
|
|
|
Ok(out)
|
|
|
}
|
|
|
|
|
|
-/// 新订阅行 INSERT(六项快照+派生价格;`order_no`/`actual_opened_at` 由工单竣工回传,本轮留空)。
|
|
|
-pub(crate) fn insert_stmt(v: &NewValues) -> DmlStatement {
|
|
|
- let mut p = Params::default();
|
|
|
+/// 组装新订阅 INSERT 的列与占位值(无条件/条件插共用)。
|
|
|
+fn insert_cols_vals(v: &NewValues, p: &mut Params) -> (Vec<&'static str>, Vec<String>) {
|
|
|
let ph = |p: &mut Params, val: Value, cast: &str| -> String {
|
|
|
let s = p.push(val);
|
|
|
if cast.is_empty() {
|
|
|
@@ -1627,53 +1626,60 @@ pub(crate) fn insert_stmt(v: &NewValues) -> DmlStatement {
|
|
|
"quantity",
|
|
|
];
|
|
|
let mut vals: Vec<String> = vec![
|
|
|
- ph(&mut p, Value::String(v.subscription_id.clone()), "text"),
|
|
|
- ph(&mut p, Value::String(v.customer_id.clone()), "text"),
|
|
|
- ph(&mut p, Value::String(v.status.clone()), "text"),
|
|
|
- ph(&mut p, Value::String(v.plan_id.clone()), "text"),
|
|
|
- ph(&mut p, Value::String(v.plan_name.clone()), "text"),
|
|
|
- ph(&mut p, Value::String(v.plan_kind.clone()), "text"),
|
|
|
+ ph(p, Value::String(v.subscription_id.clone()), "text"),
|
|
|
+ ph(p, Value::String(v.customer_id.clone()), "text"),
|
|
|
+ ph(p, Value::String(v.status.clone()), "text"),
|
|
|
+ ph(p, Value::String(v.plan_id.clone()), "text"),
|
|
|
+ ph(p, Value::String(v.plan_name.clone()), "text"),
|
|
|
+ ph(p, Value::String(v.plan_kind.clone()), "text"),
|
|
|
ph(
|
|
|
- &mut p,
|
|
|
+ p,
|
|
|
v.plan_owner_customer_id.clone().map(Value::String).unwrap_or(Value::Null),
|
|
|
"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"),
|
|
|
- ph(&mut p, Value::String(v.start_date.clone()), "date"),
|
|
|
- ph(&mut p, Value::String(v.end_date.clone()), "date"),
|
|
|
- ph(&mut p, Value::from(v.annual_price_cents), "bigint"),
|
|
|
- ph(&mut p, Value::from(v.monthly_settle_cents), "bigint"),
|
|
|
- ph(&mut p, Value::from(v.quota_slots), "int"),
|
|
|
- ph(&mut p, Value::from(v.quantity), "int"),
|
|
|
+ ph(p, Value::String(v.plan_family.clone()), "text"),
|
|
|
+ ph(p, v.sub_mode.clone().map(Value::String).unwrap_or(Value::Null), "text"),
|
|
|
+ ph(p, v.line_exits.clone(), "text::jsonb"),
|
|
|
+ ph(p, Value::String(v.line_exit_key.clone()), "text"),
|
|
|
+ ph(p, Value::String(v.term.clone()), "text"),
|
|
|
+ ph(p, Value::String(v.start_date.clone()), "date"),
|
|
|
+ ph(p, Value::String(v.end_date.clone()), "date"),
|
|
|
+ ph(p, Value::from(v.annual_price_cents), "bigint"),
|
|
|
+ ph(p, Value::from(v.monthly_settle_cents), "bigint"),
|
|
|
+ ph(p, Value::from(v.quota_slots), "int"),
|
|
|
+ ph(p, Value::from(v.quantity), "int"),
|
|
|
];
|
|
|
if let Some(cid) = v.contract_id.as_ref() {
|
|
|
cols.push("contract_id");
|
|
|
- vals.push(ph(&mut p, Value::String(cid.clone()), "text"));
|
|
|
+ vals.push(ph(p, Value::String(cid.clone()), "text"));
|
|
|
}
|
|
|
if let Some(iid) = v.contract_item_id.as_ref() {
|
|
|
cols.push("contract_item_id");
|
|
|
- vals.push(ph(&mut p, Value::String(iid.clone()), "text"));
|
|
|
+ vals.push(ph(p, Value::String(iid.clone()), "text"));
|
|
|
}
|
|
|
if let Some(t) = v.trial_months {
|
|
|
cols.push("trial_months");
|
|
|
- vals.push(ph(&mut p, Value::from(t), "int"));
|
|
|
+ vals.push(ph(p, Value::from(t), "int"));
|
|
|
}
|
|
|
if let Some(d) = v.discount_monthly_cents {
|
|
|
cols.push("discount_monthly_cents");
|
|
|
- vals.push(ph(&mut p, Value::from(d), "bigint"));
|
|
|
+ vals.push(ph(p, Value::from(d), "bigint"));
|
|
|
}
|
|
|
if let Some(rid) = v.replaces_subscription_id.as_ref() {
|
|
|
cols.push("replaces_subscription_id");
|
|
|
- vals.push(ph(&mut p, Value::String(rid.clone()), "text"));
|
|
|
+ vals.push(ph(p, Value::String(rid.clone()), "text"));
|
|
|
}
|
|
|
if let Some(pid) = v.parent_subscription_id.as_ref() {
|
|
|
cols.push("parent_subscription_id");
|
|
|
- vals.push(ph(&mut p, Value::String(pid.clone()), "text"));
|
|
|
+ vals.push(ph(p, Value::String(pid.clone()), "text"));
|
|
|
}
|
|
|
+ (cols, vals)
|
|
|
+}
|
|
|
+
|
|
|
+/// 新订阅行 INSERT(六项快照+派生价格;`order_no`/`actual_opened_at` 由工单竣工回传,本轮留空)。
|
|
|
+pub(crate) fn insert_stmt(v: &NewValues) -> DmlStatement {
|
|
|
+ let mut p = Params::default();
|
|
|
+ let (cols, vals) = insert_cols_vals(v, &mut p);
|
|
|
DmlStatement::new(
|
|
|
format!(
|
|
|
"INSERT INTO subscription ({}) VALUES ({})",
|
|
|
@@ -1684,6 +1690,31 @@ pub(crate) fn insert_stmt(v: &NewValues) -> DmlStatement {
|
|
|
)
|
|
|
}
|
|
|
|
|
|
+/// 升降配替换专用:仅当截原已落地(原行 `expired` 且 `end_date=prev_end`)才插入新订阅。
|
|
|
+///
|
|
|
+/// UDS 短批原子提交但**不因前句 0 行中止**(见 `uds.rs`);无条件 INSERT 会在错 `row_version`
|
|
|
+/// 时仍落新行(D-03)。条件插与工单侧「条件写+条件插」同法——截原失败 ⇒ INSERT 0 行。
|
|
|
+pub(crate) fn insert_replacement_stmt(v: &NewValues, orig_id: &str, prev_end: &str) -> DmlStatement {
|
|
|
+ let mut p = Params::default();
|
|
|
+ let (cols, vals) = insert_cols_vals(v, &mut p);
|
|
|
+ let orig = p.push(Value::String(orig_id.to_string()));
|
|
|
+ let cust = p.push(Value::String(v.customer_id.clone()));
|
|
|
+ let prev = p.push(Value::String(prev_end.to_string()));
|
|
|
+ DmlStatement::new(
|
|
|
+ format!(
|
|
|
+ "INSERT INTO subscription ({}) SELECT {} WHERE EXISTS (\
|
|
|
+ SELECT 1 FROM subscription s \
|
|
|
+ WHERE s.subscription_id = {orig}::text \
|
|
|
+ AND s.customer_id = {cust}::text \
|
|
|
+ AND s.status = 'expired' \
|
|
|
+ AND s.end_date = {prev}::date)",
|
|
|
+ cols.join(", "),
|
|
|
+ vals.join(", ")
|
|
|
+ ),
|
|
|
+ p.values().to_vec(),
|
|
|
+ )
|
|
|
+}
|
|
|
+
|
|
|
/// 首次引用打点(§5.2 第 4 步:同事务打点=引用冻结的生效点,⛔ 提交后异步补写)。
|
|
|
///
|
|
|
/// ⚠️ 写侧影响行数恒为 1:早期写的是 `… WHERE plan_id = $1 AND first_referred_at IS NULL`,
|
|
|
@@ -2171,6 +2202,15 @@ pub async fn create(
|
|
|
Ok(v) => v,
|
|
|
Err(e) => return json_err(e),
|
|
|
};
|
|
|
+ // D-08:写库前校验承接团队含即将生成的交付类型,避免订阅成功却静默漏单。
|
|
|
+ match ops::handlers::open_gen::predict_delivery_type(state.db.as_ref(), &cid, false).await {
|
|
|
+ Ok(tt) => {
|
|
|
+ if let Err(e) = ops::handlers::open_gen::assert_team_accepts_type(state.db.as_ref(), &cid, tt).await {
|
|
|
+ return json_err(e);
|
|
|
+ }
|
|
|
+ }
|
|
|
+ Err(e) => return json_err(e),
|
|
|
+ }
|
|
|
let mut stmts: Vec<DmlStatement> = Vec::new();
|
|
|
stmts.push(insert_stmt(&nv));
|
|
|
stmts.push(first_referred_stmt(&pid));
|
|
|
@@ -2553,6 +2593,23 @@ pub async fn change(
|
|
|
Ok(v) => v,
|
|
|
Err(e) => return json_err(e),
|
|
|
};
|
|
|
+ // D-08:写库前校验承接团队含即将生成的交付类型。
|
|
|
+ let original_delivered = orig
|
|
|
+ .actual_opened_at
|
|
|
+ .as_deref()
|
|
|
+ .is_some_and(|s| !s.trim().is_empty());
|
|
|
+ match ops::handlers::open_gen::predict_delivery_type(state.db.as_ref(), &cid, original_delivered)
|
|
|
+ .await
|
|
|
+ {
|
|
|
+ Ok(tt) => {
|
|
|
+ if let Err(e) =
|
|
|
+ ops::handlers::open_gen::assert_team_accepts_type(state.db.as_ref(), &cid, tt).await
|
|
|
+ {
|
|
|
+ return json_err(e);
|
|
|
+ }
|
|
|
+ }
|
|
|
+ Err(e) => return json_err(e),
|
|
|
+ }
|
|
|
let mut stmts: Vec<DmlStatement> = super::succession::change_dml(&planned);
|
|
|
let has_quota = before != target;
|
|
|
if has_quota {
|