|
@@ -234,10 +234,14 @@ pub struct CatalogCreateReq {
|
|
|
/// 静默接受越权改列,故显式收下再进同一处载荷 diff 判定(§3.6)。
|
|
/// 静默接受越权改列,故显式收下再进同一处载荷 diff 判定(§3.6)。
|
|
|
///
|
|
///
|
|
|
/// `plan_desc` 用 [`Value`]:缺键=不动;`null`/空串=清空成库内 `NULL`(业主 2026-11-22)。
|
|
/// `plan_desc` 用 [`Value`]:缺键=不动;`null`/空串=清空成库内 `NULL`(业主 2026-11-22)。
|
|
|
|
|
+///
|
|
|
|
|
+/// ⚠️ 须用 [`deser_present_json`]:普通 `Option<Value>` 在 serde 下会把显式 `null`
|
|
|
|
|
+/// 读成 `None`(与缺键同态),清空路径永远走不到。
|
|
|
#[derive(Deserialize)]
|
|
#[derive(Deserialize)]
|
|
|
pub struct CatalogUpdateReq {
|
|
pub struct CatalogUpdateReq {
|
|
|
pub plan_id: Option<String>,
|
|
pub plan_id: Option<String>,
|
|
|
pub plan_name: Option<String>,
|
|
pub plan_name: Option<String>,
|
|
|
|
|
+ #[serde(default, deserialize_with = "deser_present_json")]
|
|
|
pub plan_desc: Option<Value>,
|
|
pub plan_desc: Option<Value>,
|
|
|
pub annual_price_cents: Option<i64>,
|
|
pub annual_price_cents: Option<i64>,
|
|
|
/// 月结算标准由年价派生,⛔ 声明字段——走 `extra`+`READONLY_KEYS` 显式拒收。
|
|
/// 月结算标准由年价派生,⛔ 声明字段——走 `extra`+`READONLY_KEYS` 显式拒收。
|
|
@@ -563,6 +567,14 @@ fn default_quota(family: &str) -> i64 {
|
|
|
}
|
|
}
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
|
|
+/// 键存在时一律进 `Some`(含 JSON `null`);缺键走 `#[serde(default)]` → `None`。
|
|
|
|
|
+fn deser_present_json<'de, D>(deserializer: D) -> Result<Option<Value>, D::Error>
|
|
|
|
|
+where
|
|
|
|
|
+ D: serde::Deserializer<'de>,
|
|
|
|
|
+{
|
|
|
|
|
+ Ok(Some(Value::deserialize(deserializer)?))
|
|
|
|
|
+}
|
|
|
|
|
+
|
|
|
/// `plan_desc` 局部更新三态:缺键=不动;`null`/空串=清空;有字=写入。
|
|
/// `plan_desc` 局部更新三态:缺键=不动;`null`/空串=清空;有字=写入。
|
|
|
fn plan_desc_patch(raw: &Option<Value>) -> Result<Option<Option<String>>, BosError> {
|
|
fn plan_desc_patch(raw: &Option<Value>) -> Result<Option<Option<String>>, BosError> {
|
|
|
match raw {
|
|
match raw {
|
|
@@ -787,8 +799,8 @@ fn check_bandwidth(
|
|
|
}
|
|
}
|
|
|
return Ok((None, None));
|
|
return Ok((None, None));
|
|
|
}
|
|
}
|
|
|
- let up = up.ok_or_else(|| BosError::bad_params("请填写上行带宽(承诺带宽时上下行须成对填写)"))?;
|
|
|
|
|
- let down = down.ok_or_else(|| BosError::bad_params("请填写下行带宽(承诺带宽时上下行须成对填写)"))?;
|
|
|
|
|
|
|
+ let up = up.ok_or_else(|| BosError::bad_params("请填写上行带宽"))?;
|
|
|
|
|
+ let down = down.ok_or_else(|| BosError::bad_params("请填写下行带宽(须与上行成对填写)"))?;
|
|
|
for (name, v) in [("bandwidth_up_mbps", up), ("bandwidth_down_mbps", down)] {
|
|
for (name, v) in [("bandwidth_up_mbps", up), ("bandwidth_down_mbps", down)] {
|
|
|
if !(BW_MIN..=BW_MAX).contains(&v) {
|
|
if !(BW_MIN..=BW_MAX).contains(&v) {
|
|
|
return Err(BosError::bad_params(format!(
|
|
return Err(BosError::bad_params(format!(
|
|
@@ -827,7 +839,7 @@ fn validate_cost_items(items: &[CostItemReq]) -> Result<Vec<CleanCostItem>, Cata
|
|
|
// 「份数不生效」伪装成「已保存」)。差额登 18 册。
|
|
// 「份数不生效」伪装成「已保存」)。差额登 18 册。
|
|
|
if it.extra.contains_key("copies") {
|
|
if it.extra.contains_key("copies") {
|
|
|
fields.push(field_entry(
|
|
fields.push(field_entry(
|
|
|
- "cost_items[].copies",
|
|
|
|
|
|
|
+ "items[].copies",
|
|
|
Some(i),
|
|
Some(i),
|
|
|
"一期 plan_cost_item 无 copies 列,份数语义未落库(差额登 18 册)",
|
|
"一期 plan_cost_item 无 copies 列,份数语义未落库(差额登 18 册)",
|
|
|
));
|
|
));
|
|
@@ -835,13 +847,13 @@ fn validate_cost_items(items: &[CostItemReq]) -> Result<Vec<CleanCostItem>, Cata
|
|
|
let name = match norm(it.item_name.as_deref()) {
|
|
let name = match norm(it.item_name.as_deref()) {
|
|
|
Some(n) => n,
|
|
Some(n) => n,
|
|
|
None => {
|
|
None => {
|
|
|
- fields.push(field_entry("cost_items[].item_name", Some(i), "成本项名称不能为空"));
|
|
|
|
|
|
|
+ fields.push(field_entry("items[].item_name", Some(i), "成本项名称不能为空"));
|
|
|
continue;
|
|
continue;
|
|
|
}
|
|
}
|
|
|
};
|
|
};
|
|
|
if name.chars().count() > COST_NAME_MAX {
|
|
if name.chars().count() > COST_NAME_MAX {
|
|
|
fields.push(field_entry(
|
|
fields.push(field_entry(
|
|
|
- "cost_items[].item_name",
|
|
|
|
|
|
|
+ "items[].item_name",
|
|
|
Some(i),
|
|
Some(i),
|
|
|
&format!("成本项名称超长(上限 {COST_NAME_MAX} 字符)"),
|
|
&format!("成本项名称超长(上限 {COST_NAME_MAX} 字符)"),
|
|
|
));
|
|
));
|
|
@@ -850,14 +862,14 @@ fn validate_cost_items(items: &[CostItemReq]) -> Result<Vec<CleanCostItem>, Cata
|
|
|
Some(v) if v >= 0 && v <= MAX_AMOUNT_CENTS => v,
|
|
Some(v) if v >= 0 && v <= MAX_AMOUNT_CENTS => v,
|
|
|
Some(v) => {
|
|
Some(v) => {
|
|
|
fields.push(field_entry(
|
|
fields.push(field_entry(
|
|
|
- "cost_items[].amount_cents",
|
|
|
|
|
|
|
+ "items[].amount_cents",
|
|
|
Some(i),
|
|
Some(i),
|
|
|
&format!("金额须在 0~{MAX_AMOUNT_CENTS} 分之间(收到 {v})"),
|
|
&format!("金额须在 0~{MAX_AMOUNT_CENTS} 分之间(收到 {v})"),
|
|
|
));
|
|
));
|
|
|
continue;
|
|
continue;
|
|
|
}
|
|
}
|
|
|
None => {
|
|
None => {
|
|
|
- fields.push(field_entry("cost_items[].amount_cents", Some(i), "金额不能为空"));
|
|
|
|
|
|
|
+ fields.push(field_entry("items[].amount_cents", Some(i), "金额不能为空"));
|
|
|
continue;
|
|
continue;
|
|
|
}
|
|
}
|
|
|
};
|
|
};
|
|
@@ -865,7 +877,7 @@ fn validate_cost_items(items: &[CostItemReq]) -> Result<Vec<CleanCostItem>, Cata
|
|
|
if let Some(u) = unit.as_deref() {
|
|
if let Some(u) = unit.as_deref() {
|
|
|
if !COST_BILLING_UNITS.contains(&u) {
|
|
if !COST_BILLING_UNITS.contains(&u) {
|
|
|
fields.push(field_entry(
|
|
fields.push(field_entry(
|
|
|
- "cost_items[].billing_unit",
|
|
|
|
|
|
|
+ "items[].billing_unit",
|
|
|
Some(i),
|
|
Some(i),
|
|
|
&format!("billing_unit 只接受 {},收到:{u}", COST_BILLING_UNITS.join("/")),
|
|
&format!("billing_unit 只接受 {},收到:{u}", COST_BILLING_UNITS.join("/")),
|
|
|
));
|
|
));
|
|
@@ -875,7 +887,7 @@ fn validate_cost_items(items: &[CostItemReq]) -> Result<Vec<CleanCostItem>, Cata
|
|
|
if let Some(c) = cat.as_deref() {
|
|
if let Some(c) = cat.as_deref() {
|
|
|
if !COST_CATEGORIES.contains(&c) {
|
|
if !COST_CATEGORIES.contains(&c) {
|
|
|
fields.push(field_entry(
|
|
fields.push(field_entry(
|
|
|
- "cost_items[].category",
|
|
|
|
|
|
|
+ "items[].category",
|
|
|
Some(i),
|
|
Some(i),
|
|
|
&format!("category 只接受 {},收到:{c}", COST_CATEGORIES.join("/")),
|
|
&format!("category 只接受 {},收到:{c}", COST_CATEGORIES.join("/")),
|
|
|
));
|
|
));
|
|
@@ -885,7 +897,7 @@ fn validate_cost_items(items: &[CostItemReq]) -> Result<Vec<CleanCostItem>, Cata
|
|
|
if let Some(r) = remark.as_deref() {
|
|
if let Some(r) = remark.as_deref() {
|
|
|
if r.chars().count() > COST_REMARK_MAX {
|
|
if r.chars().count() > COST_REMARK_MAX {
|
|
|
fields.push(field_entry(
|
|
fields.push(field_entry(
|
|
|
- "cost_items[].remark",
|
|
|
|
|
|
|
+ "items[].remark",
|
|
|
Some(i),
|
|
Some(i),
|
|
|
&format!("备注超长(上限 {COST_REMARK_MAX} 字符)"),
|
|
&format!("备注超长(上限 {COST_REMARK_MAX} 字符)"),
|
|
|
));
|
|
));
|
|
@@ -906,7 +918,7 @@ fn validate_cost_items(items: &[CostItemReq]) -> Result<Vec<CleanCostItem>, Cata
|
|
|
for (idx, c) in clean.iter().enumerate() {
|
|
for (idx, c) in clean.iter().enumerate() {
|
|
|
if clean[..idx].iter().any(|p| p.item_name == c.item_name) {
|
|
if clean[..idx].iter().any(|p| p.item_name == c.item_name) {
|
|
|
dup_fields.push(field_entry(
|
|
dup_fields.push(field_entry(
|
|
|
- "cost_items[].item_name",
|
|
|
|
|
|
|
+ "items[].item_name",
|
|
|
Some(idx as i64),
|
|
Some(idx as i64),
|
|
|
&format!("同一档位内成本项名称重复:{}", c.item_name),
|
|
&format!("同一档位内成本项名称重复:{}", c.item_name),
|
|
|
));
|
|
));
|
|
@@ -1214,10 +1226,19 @@ async fn cost_items_of(db: &UdsClient, plan_id: &str) -> Result<Vec<Value>, BosE
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
/// 批量汇总档内成本项金额(R-1 custom 列):全部项金额已配齐 ⇒ `Some(sum)`,否则 `None`。
|
|
/// 批量汇总档内成本项金额(R-1 custom 列):全部项金额已配齐 ⇒ `Some(sum)`,否则 `None`。
|
|
|
|
|
+///
|
|
|
|
|
+/// 行级 `Err(msg)`=该档「取数失败」;整批 UDS 失败由 [`cost_sums_degraded`] 落成各自定义档
|
|
|
|
|
+/// `failed`(⛔ 把整张台账打成 `json_err`——会让其它列一并不可见)。
|
|
|
async fn plan_cost_sums(
|
|
async fn plan_cost_sums(
|
|
|
db: &UdsClient,
|
|
db: &UdsClient,
|
|
|
plan_ids: &[String],
|
|
plan_ids: &[String],
|
|
|
) -> Result<HashMap<String, Result<Option<i64>, String>>, BosError> {
|
|
) -> Result<HashMap<String, Result<Option<i64>, String>>, BosError> {
|
|
|
|
|
+ // 复测闭环注入:只开本机探针用,⛔ 生产配置。
|
|
|
|
|
+ if std::env::var("KLT_BOS_INJECT_PLAN_COST_FAIL").ok().as_deref() == Some("1") {
|
|
|
|
|
+ return Err(BosError::internal(
|
|
|
|
|
+ "注入:月成本取数失败(KLT_BOS_INJECT_PLAN_COST_FAIL=1)",
|
|
|
|
|
+ ));
|
|
|
|
|
+ }
|
|
|
let mut out: HashMap<String, Result<Option<i64>, String>> = HashMap::new();
|
|
let mut out: HashMap<String, Result<Option<i64>, String>> = HashMap::new();
|
|
|
if plan_ids.is_empty() {
|
|
if plan_ids.is_empty() {
|
|
|
return Ok(out);
|
|
return Ok(out);
|
|
@@ -1243,13 +1264,34 @@ async fn plan_cost_sums(
|
|
|
out.insert(r.plan_id, Ok(Some(cents)));
|
|
out.insert(r.plan_id, Ok(Some(cents)));
|
|
|
}
|
|
}
|
|
|
None => {
|
|
None => {
|
|
|
- out.insert(r.plan_id, Ok(None));
|
|
|
|
|
|
|
+ let msg = format!(
|
|
|
|
|
+ "月成本汇总不可解析:plan_id={} total_cents={:?}",
|
|
|
|
|
+ r.plan_id, r.total_cents
|
|
|
|
|
+ );
|
|
|
|
|
+ out.insert(r.plan_id, Err(msg));
|
|
|
}
|
|
}
|
|
|
}
|
|
}
|
|
|
}
|
|
}
|
|
|
Ok(out)
|
|
Ok(out)
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
|
|
+/// 成本汇总失败 ⇒ 自定义档逐行 `failed`;固定档仍走「按组合」(不进本表)。
|
|
|
|
|
+fn cost_sums_degraded(
|
|
|
|
|
+ custom_ids: &[String],
|
|
|
|
|
+ result: Result<HashMap<String, Result<Option<i64>, String>>, BosError>,
|
|
|
|
|
+) -> HashMap<String, Result<Option<i64>, String>> {
|
|
|
|
|
+ match result {
|
|
|
|
|
+ Ok(m) => m,
|
|
|
|
|
+ Err(e) => {
|
|
|
|
|
+ let msg = format!("月成本取数失败:{e}");
|
|
|
|
|
+ custom_ids
|
|
|
|
|
+ .iter()
|
|
|
|
|
+ .map(|id| (id.clone(), Err(msg.clone())))
|
|
|
|
|
+ .collect()
|
|
|
|
|
+ }
|
|
|
|
|
+ }
|
|
|
|
|
+}
|
|
|
|
|
+
|
|
|
fn cost_readout_for(plan_kind: &str, plan_id: &str, sums: &HashMap<String, Result<Option<i64>, String>>) -> PlanCostReadout {
|
|
fn cost_readout_for(plan_kind: &str, plan_id: &str, sums: &HashMap<String, Result<Option<i64>, String>>) -> PlanCostReadout {
|
|
|
if plan_kind != "custom" {
|
|
if plan_kind != "custom" {
|
|
|
return PlanCostReadout::ByCombination;
|
|
return PlanCostReadout::ByCombination;
|
|
@@ -1774,7 +1816,10 @@ async fn do_create(
|
|
|
let audit_after = plan_snapshot(&row);
|
|
let audit_after = plan_snapshot(&row);
|
|
|
// 回读真值再应答(`/dml/batch@v1` ⛔ 回 `RETURNING`)
|
|
// 回读真值再应答(`/dml/batch@v1` ⛔ 回 `RETURNING`)
|
|
|
let list = cost_items_of(state.db.as_ref(), &plan_id).await?;
|
|
let list = cost_items_of(state.db.as_ref(), &plan_id).await?;
|
|
|
- let sums = plan_cost_sums(state.db.as_ref(), &[plan_id.clone()]).await.unwrap_or_default();
|
|
|
|
|
|
|
+ let sums = cost_sums_degraded(
|
|
|
|
|
+ &[plan_id.clone()],
|
|
|
|
|
+ plan_cost_sums(state.db.as_ref(), &[plan_id.clone()]).await,
|
|
|
|
|
+ );
|
|
|
let cost = cost_readout_for(row.plan_kind.as_str(), &plan_id, &sums);
|
|
let cost = cost_readout_for(row.plan_kind.as_str(), &plan_id, &sums);
|
|
|
let mut v = plan_json(row, 0, &cost);
|
|
let mut v = plan_json(row, 0, &cost);
|
|
|
let saved_count = list.len();
|
|
let saved_count = list.len();
|
|
@@ -2119,7 +2164,10 @@ async fn do_update(
|
|
|
.ok_or_else(|| BosError::internal(format!("档位更新后回读失败:plan_id={plan_id}")))?;
|
|
.ok_or_else(|| BosError::internal(format!("档位更新后回读失败:plan_id={plan_id}")))?;
|
|
|
let n = referred_count(state.db.as_ref(), &plan_id).await?;
|
|
let n = referred_count(state.db.as_ref(), &plan_id).await?;
|
|
|
let audit_after = plan_snapshot(&row);
|
|
let audit_after = plan_snapshot(&row);
|
|
|
- let sums = plan_cost_sums(state.db.as_ref(), &[plan_id.clone()]).await.unwrap_or_default();
|
|
|
|
|
|
|
+ let sums = cost_sums_degraded(
|
|
|
|
|
+ &[plan_id.clone()],
|
|
|
|
|
+ plan_cost_sums(state.db.as_ref(), &[plan_id.clone()]).await,
|
|
|
|
|
+ );
|
|
|
let cost = cost_readout_for(row.plan_kind.as_str(), &plan_id, &sums);
|
|
let cost = cost_readout_for(row.plan_kind.as_str(), &plan_id, &sums);
|
|
|
let mut v = plan_json(row, n, &cost);
|
|
let mut v = plan_json(row, n, &cost);
|
|
|
if cost_items.is_some() {
|
|
if cost_items.is_some() {
|
|
@@ -2299,6 +2347,11 @@ async fn do_plan_cost_save(
|
|
|
auth: &bos_scope::AuthContext,
|
|
auth: &bos_scope::AuthContext,
|
|
|
b: &PlanCostSaveReq,
|
|
b: &PlanCostSaveReq,
|
|
|
) -> Result<Value, CatalogErr> {
|
|
) -> Result<Value, CatalogErr> {
|
|
|
|
|
+ // 键面粗放(opsSub 可持写键)⇒ 服务侧角色闸统一 `KLT-P301`,⛔ 让 AC `11001` 独吞文案。
|
|
|
|
|
+ auth.require_roles(&[RoleCode::Admin, RoleCode::OpsMain])
|
|
|
|
|
+ .map_err(|_| {
|
|
|
|
|
+ BosError::forbidden_role("成本项维护仅总运营/运营主账号可用(03 章 §3.5)")
|
|
|
|
|
+ })?;
|
|
|
let plan_id = norm(b.plan_id.as_deref()).ok_or_else(|| {
|
|
let plan_id = norm(b.plan_id.as_deref()).ok_or_else(|| {
|
|
|
BosError::bad_params("plan_id 不能为空")
|
|
BosError::bad_params("plan_id 不能为空")
|
|
|
})?;
|
|
})?;
|
|
@@ -2416,7 +2469,8 @@ struct FacetRow {
|
|
|
cnt: Option<String>,
|
|
cnt: Option<String>,
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
-/// 台账 facets(R-14):在「base 谓词 ∧ `plan_kind='custom'`」上聚合建档团队/建档人清单。
|
|
|
|
|
|
|
+/// 台账 facets(R-14):在**与列表同一套 base 谓词**上聚合建档团队/建档人清单
|
|
|
|
|
+/// (含 `plan_kind` 筛选;⛔ 再硬加 `custom`——否则同一信封里 `facets` 与 `total` 不同域)。
|
|
|
///
|
|
///
|
|
|
/// ⛔ 用当前页行凑选项:翻一页选项就变一批,且筛选后的行集根本凑不出「已选项」。
|
|
/// ⛔ 用当前页行凑选项:翻一页选项就变一批,且筛选后的行集根本凑不出「已选项」。
|
|
|
/// 参数取自调用方已绑定的 `values`(必须是**未追加** `created_by`/`created_team_id` 的那份)。
|
|
/// 参数取自调用方已绑定的 `values`(必须是**未追加** `created_by`/`created_team_id` 的那份)。
|
|
@@ -2425,9 +2479,11 @@ async fn facets_of(
|
|
|
base: &[String],
|
|
base: &[String],
|
|
|
values: &[Value],
|
|
values: &[Value],
|
|
|
) -> Result<Value, BosError> {
|
|
) -> Result<Value, BosError> {
|
|
|
- let mut w = base.to_vec();
|
|
|
|
|
- w.push("p.plan_kind = 'custom'::text".to_string());
|
|
|
|
|
- let where_sql = format!(" WHERE {}", w.join(" AND "));
|
|
|
|
|
|
|
+ let where_sql = if base.is_empty() {
|
|
|
|
|
+ String::new()
|
|
|
|
|
+ } else {
|
|
|
|
|
+ format!(" WHERE {}", base.join(" AND "))
|
|
|
|
|
+ };
|
|
|
// D-PKG-01/17:`created_team_id IS NULL`(总部)也进 facets;标签=`CREATED_TEAM_NAME_EXPR`(同源)。
|
|
// D-PKG-01/17:`created_team_id IS NULL`(总部)也进 facets;标签=`CREATED_TEAM_NAME_EXPR`(同源)。
|
|
|
let team_sql = format!(
|
|
let team_sql = format!(
|
|
|
"SELECT COALESCE(p.created_team_id, '{CREATED_TEAM_HQ_SENTINEL}') AS id, \
|
|
"SELECT COALESCE(p.created_team_id, '{CREATED_TEAM_HQ_SENTINEL}') AS id, \
|
|
@@ -2536,10 +2592,10 @@ pub async fn get_list(
|
|
|
.filter(|r| r.plan_kind == "custom")
|
|
.filter(|r| r.plan_kind == "custom")
|
|
|
.map(|r| r.plan_id.clone())
|
|
.map(|r| r.plan_id.clone())
|
|
|
.collect();
|
|
.collect();
|
|
|
- let cost_sums = match plan_cost_sums(state.db.as_ref(), &custom_ids).await {
|
|
|
|
|
- Ok(m) => m,
|
|
|
|
|
- Err(e) => return json_err(e),
|
|
|
|
|
- };
|
|
|
|
|
|
|
+ let cost_sums = cost_sums_degraded(
|
|
|
|
|
+ &custom_ids,
|
|
|
|
|
+ plan_cost_sums(state.db.as_ref(), &custom_ids).await,
|
|
|
|
|
+ );
|
|
|
let items: Vec<Value> = rows
|
|
let items: Vec<Value> = rows
|
|
|
.into_iter()
|
|
.into_iter()
|
|
|
.map(|r| {
|
|
.map(|r| {
|
|
@@ -2592,10 +2648,10 @@ pub async fn get(
|
|
|
Ok(v) => v,
|
|
Ok(v) => v,
|
|
|
Err(e) => return json_err(e),
|
|
Err(e) => return json_err(e),
|
|
|
};
|
|
};
|
|
|
- let sums = match plan_cost_sums(state.db.as_ref(), &[plan_id.clone()]).await {
|
|
|
|
|
- Ok(m) => m,
|
|
|
|
|
- Err(e) => return json_err(e),
|
|
|
|
|
- };
|
|
|
|
|
|
|
+ let sums = cost_sums_degraded(
|
|
|
|
|
+ &[plan_id.clone()],
|
|
|
|
|
+ plan_cost_sums(state.db.as_ref(), &[plan_id.clone()]).await,
|
|
|
|
|
+ );
|
|
|
let cost = cost_readout_for(row.plan_kind.as_str(), &plan_id, &sums);
|
|
let cost = cost_readout_for(row.plan_kind.as_str(), &plan_id, &sums);
|
|
|
let mut v = plan_json(row, n, &cost);
|
|
let mut v = plan_json(row, n, &cost);
|
|
|
v["cost_items"] = json!(cost_items);
|
|
v["cost_items"] = json!(cost_items);
|
|
@@ -2756,6 +2812,29 @@ mod tests {
|
|
|
assert_eq!(cross_state(&CrossRead::<i64>::Failed("t".into())), "failed");
|
|
assert_eq!(cross_state(&CrossRead::<i64>::Failed("t".into())), "failed");
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
|
|
+ #[test]
|
|
|
|
|
+ fn 成本汇总失败降级为逐行failed() {
|
|
|
|
|
+ let ids = vec!["P-c1".into(), "P-c2".into()];
|
|
|
|
|
+ let m = cost_sums_degraded(&ids, Err(BosError::internal("UDS 断连")));
|
|
|
|
|
+ assert_eq!(m.len(), 2);
|
|
|
|
|
+ for id in &ids {
|
|
|
|
|
+ let PlanCostReadout::Failed(note) = cost_readout_for("custom", id, &m) else {
|
|
|
|
|
+ panic!("{id} 应 failed");
|
|
|
|
|
+ };
|
|
|
|
|
+ assert!(note.contains("月成本取数失败"), "{note}");
|
|
|
|
|
+ let mut r = row();
|
|
|
|
|
+ r.plan_id = id.clone();
|
|
|
|
|
+ r.plan_kind = "custom".into();
|
|
|
|
|
+ let v = plan_json(r, 0, &PlanCostReadout::Failed(note));
|
|
|
|
|
+ assert_eq!(v["plan_cost_state"], json!("failed"));
|
|
|
|
|
+ assert_eq!(v["plan_cost_cents"], Value::Null);
|
|
|
|
|
+ }
|
|
|
|
|
+ assert_eq!(
|
|
|
|
|
+ plan_cost_state(&cost_readout_for("fixed", "P-001", &m)),
|
|
|
|
|
+ "by_combination"
|
|
|
|
|
+ );
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
#[test]
|
|
#[test]
|
|
|
fn plan_desc_三态_null清空() {
|
|
fn plan_desc_三态_null清空() {
|
|
|
assert_eq!(plan_desc_patch(&None).unwrap(), None);
|
|
assert_eq!(plan_desc_patch(&None).unwrap(), None);
|
|
@@ -2774,6 +2853,44 @@ mod tests {
|
|
|
assert!(plan_desc_patch(&Some(json!(123))).is_err());
|
|
assert!(plan_desc_patch(&Some(json!(123))).is_err());
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
|
|
+ #[test]
|
|
|
|
|
+ fn plan_desc_反序列化区分缺键与显式null() {
|
|
|
|
|
+ // 缺键 ⇒ None(不动)
|
|
|
|
|
+ let missing: CatalogUpdateReq =
|
|
|
|
|
+ serde_json::from_value(json!({"plan_id": "P-001"})).unwrap();
|
|
|
|
|
+ assert!(missing.plan_desc.is_none());
|
|
|
|
|
+ // 显式 null ⇒ Some(Null)(清空),⛔ 不得被 Option 吃成 None
|
|
|
|
|
+ let nulled: CatalogUpdateReq =
|
|
|
|
|
+ serde_json::from_value(json!({"plan_id": "P-001", "plan_desc": null})).unwrap();
|
|
|
|
|
+ assert_eq!(nulled.plan_desc, Some(Value::Null));
|
|
|
|
|
+ assert_eq!(plan_desc_patch(&nulled.plan_desc).unwrap(), Some(None));
|
|
|
|
|
+ // 空串同效清空
|
|
|
|
|
+ let empty: CatalogUpdateReq =
|
|
|
|
|
+ serde_json::from_value(json!({"plan_id": "P-001", "plan_desc": ""})).unwrap();
|
|
|
|
|
+ assert_eq!(
|
|
|
|
|
+ plan_desc_patch(&empty.plan_desc).unwrap(),
|
|
|
|
|
+ Some(None)
|
|
|
|
|
+ );
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ #[test]
|
|
|
|
|
+ fn 成本项fields前缀与入参键items对齐() {
|
|
|
|
|
+ let err = validate_cost_items(&[CostItemReq {
|
|
|
|
|
+ item_name: Some("x".into()),
|
|
|
|
|
+ amount_cents: None,
|
|
|
|
|
+ billing_unit: None,
|
|
|
|
|
+ category: None,
|
|
|
|
|
+ remark: None,
|
|
|
|
|
+ sort_no: None,
|
|
|
|
|
+ extra: HashMap::new(),
|
|
|
|
|
+ }]);
|
|
|
|
|
+ let CatalogErr::WithData(_, data) = err.unwrap_err() else {
|
|
|
|
|
+ panic!("期望 fields 聚合");
|
|
|
|
|
+ };
|
|
|
|
|
+ let field = data["fields"][0]["field"].as_str().unwrap_or("");
|
|
|
|
|
+ assert_eq!(field, "items[].amount_cents");
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
#[test]
|
|
#[test]
|
|
|
fn 锁定与可改清单两形态() {
|
|
fn 锁定与可改清单两形态() {
|
|
|
let (l, e) = locked_editable(false);
|
|
let (l, e) = locked_editable(false);
|