|
@@ -20,6 +20,10 @@ use super::partner::customer_scope_pred;
|
|
|
const SUBJECT_TYPES: [&str; 3] = ["enterprise", "individual", "sole_proprietor"];
|
|
const SUBJECT_TYPES: [&str; 3] = ["enterprise", "individual", "sole_proprietor"];
|
|
|
/// 乐观锁初始版本(列由 `20261107000006_customer_contact_columns.sql` 补建)。
|
|
/// 乐观锁初始版本(列由 `20261107000006_customer_contact_columns.sql` 补建)。
|
|
|
const ROW_VERSION_INIT: i64 = 1;
|
|
const ROW_VERSION_INIT: i64 = 1;
|
|
|
|
|
+/// 客户名称上限(与前端建档/编辑同口径)。
|
|
|
|
|
+const CUSTOMER_NAME_MAX: usize = 64;
|
|
|
|
|
+/// 客户备注上限(对齐 `customer.remark character varying(512)`,拒在入参处,⛔ 透传 UDS 原文)。
|
|
|
|
|
+const CUSTOMER_REMARK_MAX: usize = 512;
|
|
|
|
|
|
|
|
/// 读一行的公共封装(⛔ 把"查不到"与"读失败"混同=前者 `Ok(None)`、后者 `Err`)。
|
|
/// 读一行的公共封装(⛔ 把"查不到"与"读失败"混同=前者 `Ok(None)`、后者 `Err`)。
|
|
|
pub(super) async fn one_row(state: &AppState, sql: &str, params: &[Value]) -> Result<Option<Value>, BosError> {
|
|
pub(super) async fn one_row(state: &AppState, sql: &str, params: &[Value]) -> Result<Option<Value>, BosError> {
|
|
@@ -564,17 +568,25 @@ async fn check_same_province(
|
|
|
}))
|
|
}))
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
-/// 写侧越权判定:客户不在本账号可见范围(或已软删)⇒ 拒。
|
|
|
|
|
|
|
+/// 写侧越权判定:先判对象是否存在(`60005`),再判可见范围(`KLT-S403`/`60066`)。
|
|
|
///
|
|
///
|
|
|
/// ⚠️ 客户§3.7 把**写侧**越权单列为 `KLT-C007`,但该码在全局错误码表未在册
|
|
/// ⚠️ 客户§3.7 把**写侧**越权单列为 `KLT-C007`,但该码在全局错误码表未在册
|
|
|
-/// ([`KltCode`] 只有 `C001`/`C008`)⇒ 本轮按 `KLT-S403` 承载,缺口随本轮登记,
|
|
|
|
|
-/// ⛔ 自行新增错误码。
|
|
|
|
|
|
|
+/// ⇒ 本轮仍按 `KLT-S403` 承载;⛔ 用「无权」掩盖「不存在」(D-11)。
|
|
|
async fn assert_visible(
|
|
async fn assert_visible(
|
|
|
state: &AppState,
|
|
state: &AppState,
|
|
|
auth: &AuthContext,
|
|
auth: &AuthContext,
|
|
|
customer_id: &str,
|
|
customer_id: &str,
|
|
|
verb: &str,
|
|
verb: &str,
|
|
|
) -> Result<(), BosError> {
|
|
) -> Result<(), BosError> {
|
|
|
|
|
+ let mut exists = Params::default();
|
|
|
|
|
+ let eph = exists.push(Value::String(customer_id.to_string()));
|
|
|
|
|
+ let exists_sql = format!(
|
|
|
|
|
+ "SELECT c.customer_id FROM customer c \
|
|
|
|
|
+ WHERE c.customer_id = {eph}::text AND c.deleted_at IS NULL LIMIT 1"
|
|
|
|
|
+ );
|
|
|
|
|
+ if one_row(state, &exists_sql, exists.values()).await?.is_none() {
|
|
|
|
|
+ return Err(BosError::not_found(format!("客户 {customer_id} 不存在或已注销")));
|
|
|
|
|
+ }
|
|
|
let mut p = Params::default();
|
|
let mut p = Params::default();
|
|
|
let mut preds: Vec<String> = vec!["c.deleted_at IS NULL".to_string()];
|
|
let mut preds: Vec<String> = vec!["c.deleted_at IS NULL".to_string()];
|
|
|
let pred = customer_scope_pred(state, auth, &mut p, "c.customer_id").await?;
|
|
let pred = customer_scope_pred(state, auth, &mut p, "c.customer_id").await?;
|
|
@@ -586,11 +598,55 @@ async fn assert_visible(
|
|
|
Some(_) => Ok(()),
|
|
Some(_) => Ok(()),
|
|
|
None => Err(BosError::klt(
|
|
None => Err(BosError::klt(
|
|
|
KltCode::S403,
|
|
KltCode::S403,
|
|
|
- format!("客户 {customer_id} 不存在或无权{verb}"),
|
|
|
|
|
|
|
+ format!("客户 {customer_id} 不在可见范围,无权{verb}"),
|
|
|
)),
|
|
)),
|
|
|
}
|
|
}
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
|
|
+/// 联系电话(商务/技术/财务):11 位手机号,或含区号固话(数字/空格/-/(),7–20 字符)。
|
|
|
|
|
+fn check_contact_phone(field: &str, raw: Option<&str>) -> Result<(), BosError> {
|
|
|
|
|
+ let Some(v) = raw.map(str::trim).filter(|s| !s.is_empty()) else {
|
|
|
|
|
+ return Ok(());
|
|
|
|
|
+ };
|
|
|
|
|
+ let mobile_ok = v.len() == 11 && v.chars().all(|c| c.is_ascii_digit()) && v.starts_with('1');
|
|
|
|
|
+ let landline_ok = (7..=20).contains(&v.chars().count())
|
|
|
|
|
+ && v.chars()
|
|
|
|
|
+ .all(|c| c.is_ascii_digit() || matches!(c, ' ' | '-' | '(' | ')'));
|
|
|
|
|
+ if mobile_ok || landline_ok {
|
|
|
|
|
+ Ok(())
|
|
|
|
|
+ } else {
|
|
|
|
|
+ Err(BosError::bad_params(format!(
|
|
|
|
|
+ "{field} 格式不符:应为 11 位手机号或含区号固话,收到「{v}」"
|
|
|
|
|
+ )))
|
|
|
|
|
+ }
|
|
|
|
|
+}
|
|
|
|
|
+
|
|
|
|
|
+fn check_customer_name(name: &str) -> Result<(), BosError> {
|
|
|
|
|
+ let n = name.chars().count();
|
|
|
|
|
+ if n == 0 {
|
|
|
|
|
+ return Err(BosError::bad_params("客户名称不能为空"));
|
|
|
|
|
+ }
|
|
|
|
|
+ if n > CUSTOMER_NAME_MAX {
|
|
|
|
|
+ return Err(BosError::bad_params(format!(
|
|
|
|
|
+ "客户名称最多 {CUSTOMER_NAME_MAX} 字,实收 {n} 字"
|
|
|
|
|
+ )));
|
|
|
|
|
+ }
|
|
|
|
|
+ Ok(())
|
|
|
|
|
+}
|
|
|
|
|
+
|
|
|
|
|
+fn check_remark(raw: Option<&str>) -> Result<(), BosError> {
|
|
|
|
|
+ let Some(v) = raw else {
|
|
|
|
|
+ return Ok(());
|
|
|
|
|
+ };
|
|
|
|
|
+ let n = v.chars().count();
|
|
|
|
|
+ if n > CUSTOMER_REMARK_MAX {
|
|
|
|
|
+ return Err(BosError::bad_params(format!(
|
|
|
|
|
+ "客户备注最多 {CUSTOMER_REMARK_MAX} 字,实收 {n} 字"
|
|
|
|
|
+ )));
|
|
|
|
|
+ }
|
|
|
|
|
+ Ok(())
|
|
|
|
|
+}
|
|
|
|
|
+
|
|
|
/// `/customer/create@v1` 请求体(openapi:2212,`required`=前四项)。
|
|
/// `/customer/create@v1` 请求体(openapi:2212,`required`=前四项)。
|
|
|
#[derive(Debug, Deserialize)]
|
|
#[derive(Debug, Deserialize)]
|
|
|
pub struct CustomerCreate {
|
|
pub struct CustomerCreate {
|
|
@@ -711,13 +767,16 @@ async fn do_create(
|
|
|
RoleCode::Admin,
|
|
RoleCode::Admin,
|
|
|
RoleCode::OpsMain,
|
|
RoleCode::OpsMain,
|
|
|
RoleCode::OpsSub,
|
|
RoleCode::OpsSub,
|
|
|
|
|
+ RoleCode::TeamLead,
|
|
|
RoleCode::Ops,
|
|
RoleCode::Ops,
|
|
|
])?;
|
|
])?;
|
|
|
let name = body.customer_name.trim();
|
|
let name = body.customer_name.trim();
|
|
|
- if name.is_empty() {
|
|
|
|
|
- return Err(BosError::bad_params("客户名称不能为空"));
|
|
|
|
|
- }
|
|
|
|
|
|
|
+ check_customer_name(name)?;
|
|
|
check_subject(&body.subject_type)?;
|
|
check_subject(&body.subject_type)?;
|
|
|
|
|
+ check_remark(body.remark.as_deref())?;
|
|
|
|
|
+ check_contact_phone("联系电话", body.contact_mobile.as_deref())?;
|
|
|
|
|
+ check_contact_phone("技术联系人电话", body.tech_contact_mobile.as_deref())?;
|
|
|
|
|
+ check_contact_phone("财务联系人电话", body.finance_contact_mobile.as_deref())?;
|
|
|
// 登记材料(营业执照影像):**选填**、非企业拒收、成对纪律——与主体类型同族**入参**校验,
|
|
// 登记材料(营业执照影像):**选填**、非企业拒收、成对纪律——与主体类型同族**入参**校验,
|
|
|
// 必须排在挂载事实/平台调用之前(错参不留半成品、也不打挂树与建号)。
|
|
// 必须排在挂载事实/平台调用之前(错参不留半成品、也不打挂树与建号)。
|
|
|
let license = resolve_license(&body.subject_type, body.license.as_ref())?;
|
|
let license = resolve_license(&body.subject_type, body.license.as_ref())?;
|
|
@@ -1692,6 +1751,10 @@ struct CustomerRow {
|
|
|
#[serde(default)]
|
|
#[serde(default)]
|
|
|
#[serde(deserialize_with = "de::as_opt_string")]
|
|
#[serde(deserialize_with = "de::as_opt_string")]
|
|
|
address: Option<String>,
|
|
address: Option<String>,
|
|
|
|
|
+ /// 乐观锁版本(详情口下发,编辑口带回)。
|
|
|
|
|
+ #[serde(default)]
|
|
|
|
|
+ #[serde(deserialize_with = "de::as_opt_string")]
|
|
|
|
|
+ row_version: Option<String>,
|
|
|
/// 密文只用于本进程内解密成脱敏值,⛔ 出现在任何出参。
|
|
/// 密文只用于本进程内解密成脱敏值,⛔ 出现在任何出参。
|
|
|
#[serde(default)]
|
|
#[serde(default)]
|
|
|
#[serde(deserialize_with = "de::as_opt_string")]
|
|
#[serde(deserialize_with = "de::as_opt_string")]
|
|
@@ -1742,13 +1805,21 @@ pub async fn get_list(
|
|
|
.filter(|s| !s.is_empty())
|
|
.filter(|s| !s.is_empty())
|
|
|
.or_else(|| body.acct_status.as_deref().map(str::trim).filter(|s| !s.is_empty()));
|
|
.or_else(|| body.acct_status.as_deref().map(str::trim).filter(|s| !s.is_empty()));
|
|
|
if let Some(raw) = status_filter {
|
|
if let Some(raw) = status_filter {
|
|
|
- let normalized = normalize_acct_status_filter(raw);
|
|
|
|
|
|
|
+ let normalized = match normalize_acct_status_filter(raw) {
|
|
|
|
|
+ Ok(v) => v,
|
|
|
|
|
+ Err(e) => return json_err(e),
|
|
|
|
|
+ };
|
|
|
if let Some(v) = p.push_eq(Some(normalized.as_str())) {
|
|
if let Some(v) = p.push_eq(Some(normalized.as_str())) {
|
|
|
preds.push(format!("c.acct_status = {v}::text"));
|
|
preds.push(format!("c.acct_status = {v}::text"));
|
|
|
}
|
|
}
|
|
|
}
|
|
}
|
|
|
- if let Some(v) = p.push_eq(body.subject_type.as_deref()) {
|
|
|
|
|
- preds.push(format!("c.subject_type = {v}::text"));
|
|
|
|
|
|
|
+ if let Some(raw) = body.subject_type.as_deref().map(str::trim).filter(|s| !s.is_empty()) {
|
|
|
|
|
+ if let Err(e) = check_subject(raw) {
|
|
|
|
|
+ return json_err(e);
|
|
|
|
|
+ }
|
|
|
|
|
+ if let Some(v) = p.push_eq(Some(raw)) {
|
|
|
|
|
+ preds.push(format!("c.subject_type = {v}::text"));
|
|
|
|
|
+ }
|
|
|
}
|
|
}
|
|
|
if let Some(region) = body.region_node_id.as_deref().map(str::trim).filter(|s| !s.is_empty()) {
|
|
if let Some(region) = body.region_node_id.as_deref().map(str::trim).filter(|s| !s.is_empty()) {
|
|
|
let ph = p.push(Value::String(region.to_string()));
|
|
let ph = p.push(Value::String(region.to_string()));
|
|
@@ -1938,13 +2009,16 @@ pub async fn get_list(
|
|
|
}))
|
|
}))
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
-/// 列表 `status`/`acct_status`:中文展示名 ↔ 库内码;未知值原样下推(命中 0 行,⛔ 静默丢弃参数)。
|
|
|
|
|
-fn normalize_acct_status_filter(raw: &str) -> String {
|
|
|
|
|
|
|
+/// 列表 `status`/`acct_status`:中文展示名 ↔ 库内码(`not_opened`/`in_use`/`stopped`)。
|
|
|
|
|
+/// 非法枚举 ⇒ `None`(调用方回 `60004`,⛔ 静默 0 行)。
|
|
|
|
|
+fn normalize_acct_status_filter(raw: &str) -> Result<String, BosError> {
|
|
|
match raw.trim() {
|
|
match raw.trim() {
|
|
|
- "未开通" => "not_opened".to_string(),
|
|
|
|
|
- "使用中" | "正常" | "在用" => "in_use".to_string(),
|
|
|
|
|
- "已停用" | "停用" => "suspended".to_string(),
|
|
|
|
|
- other => other.to_string(),
|
|
|
|
|
|
|
+ "未开通" | "not_opened" => Ok("not_opened".to_string()),
|
|
|
|
|
+ "使用中" | "正常" | "在用" | "in_use" => Ok("in_use".to_string()),
|
|
|
|
|
+ "已停用" | "停用" | "stopped" | "suspended" => Ok("stopped".to_string()),
|
|
|
|
|
+ other => Err(BosError::bad_params(format!(
|
|
|
|
|
+ "status/acct_status 只能是 not_opened/in_use/stopped(或中文:未开通/使用中/已停用),收到「{other}」"
|
|
|
|
|
+ ))),
|
|
|
}
|
|
}
|
|
|
}
|
|
}
|
|
|
|
|
|
|
@@ -1958,6 +2032,14 @@ pub async fn get(
|
|
|
RequireAuth(auth): RequireAuth,
|
|
RequireAuth(auth): RequireAuth,
|
|
|
DataBody(body): DataBody<CustomerGetReq>,
|
|
DataBody(body): DataBody<CustomerGetReq>,
|
|
|
) -> Response {
|
|
) -> Response {
|
|
|
|
|
+ let cid_raw = body.customer_id.trim();
|
|
|
|
|
+ if cid_raw.is_empty() {
|
|
|
|
|
+ return json_err(BosError::bad_params("查看客户必须带 customer_id"));
|
|
|
|
|
+ }
|
|
|
|
|
+ // D-11:不存在=60005,存在但不可见=60066,⛔ 混成一句「不存在或无权」。
|
|
|
|
|
+ if let Err(e) = assert_visible(&state, &auth, cid_raw, "查看").await {
|
|
|
|
|
+ return json_err(e);
|
|
|
|
|
+ }
|
|
|
let mut p = Params::default();
|
|
let mut p = Params::default();
|
|
|
// 设计口径:`license_image_retention_days` 的**唯一读取函数**是
|
|
// 设计口径:`license_image_retention_days` 的**唯一读取函数**是
|
|
|
// `license_storage::license_retention_days`(起算点=建档日,桶上⛔ 有到期规则)。
|
|
// `license_storage::license_retention_days`(起算点=建档日,桶上⛔ 有到期规则)。
|
|
@@ -1967,11 +2049,7 @@ pub async fn get(
|
|
|
// 注入面=0:取值已被 [`license_retention_days`] 归一到 `MIN..=MAX` 的整数。
|
|
// 注入面=0:取值已被 [`license_retention_days`] 归一到 `MIN..=MAX` 的整数。
|
|
|
let retention = license_retention_days();
|
|
let retention = license_retention_days();
|
|
|
let mut preds: Vec<String> = vec!["c.deleted_at IS NULL".to_string()];
|
|
let mut preds: Vec<String> = vec!["c.deleted_at IS NULL".to_string()];
|
|
|
- match customer_scope_pred(&state, &auth, &mut p, "c.customer_id").await {
|
|
|
|
|
- Ok(pred) => preds.push(pred),
|
|
|
|
|
- Err(e) => return json_err(e),
|
|
|
|
|
- }
|
|
|
|
|
- let cid = p.push_eq(Some(body.customer_id.as_str())).unwrap_or_else(|| "NULL".to_string());
|
|
|
|
|
|
|
+ let cid = p.push(Value::String(cid_raw.to_string()));
|
|
|
preds.push(format!("c.customer_id = {cid}::text"));
|
|
preds.push(format!("c.customer_id = {cid}::text"));
|
|
|
|
|
|
|
|
let sql = format!(
|
|
let sql = format!(
|
|
@@ -1979,7 +2057,7 @@ pub async fn get(
|
|
|
c.partner_id, pa.name AS partner_name, c.acct_status, c.quota_cents, c.created_at, c.updated_at, \
|
|
c.partner_id, pa.name AS partner_name, c.acct_status, c.quota_cents, c.created_at, c.updated_at, \
|
|
|
c.contact_name, c.contact_mobile, c.tech_contact_name, c.tech_contact_mobile, \
|
|
c.contact_name, c.contact_mobile, c.tech_contact_name, c.tech_contact_mobile, \
|
|
|
c.finance_contact_name, c.finance_contact_mobile, c.remark, c.address, \
|
|
c.finance_contact_name, c.finance_contact_mobile, c.remark, c.address, \
|
|
|
- c.identity_no_cipher, \
|
|
|
|
|
|
|
+ c.row_version, c.identity_no_cipher, \
|
|
|
c.license_object_key, c.license_file_name, \
|
|
c.license_object_key, c.license_file_name, \
|
|
|
(c.created_at < now() - INTERVAL '{retention} day') AS license_expired \
|
|
(c.created_at < now() - INTERVAL '{retention} day') AS license_expired \
|
|
|
FROM customer c LEFT JOIN operator op ON op.operator_id = c.operator_id \
|
|
FROM customer c LEFT JOIN operator op ON op.operator_id = c.operator_id \
|
|
@@ -1988,7 +2066,7 @@ pub async fn get(
|
|
|
);
|
|
);
|
|
|
let row = match state.db.query_one::<CustomerRow>(&sql, p.values()).await {
|
|
let row = match state.db.query_one::<CustomerRow>(&sql, p.values()).await {
|
|
|
Ok(Some(r)) => r,
|
|
Ok(Some(r)) => r,
|
|
|
- Ok(None) => return json_err(bos_core::BosError::not_found("客户不存在或无权查看")),
|
|
|
|
|
|
|
+ Ok(None) => return json_err(BosError::not_found(format!("客户 {cid_raw} 不存在或已注销"))),
|
|
|
Err(e) => return json_err(e),
|
|
Err(e) => return json_err(e),
|
|
|
};
|
|
};
|
|
|
let ids = [row.customer_id.clone()];
|
|
let ids = [row.customer_id.clone()];
|
|
@@ -2111,6 +2189,11 @@ pub async fn get(
|
|
|
"finance_contact_mobile": row.finance_contact_mobile,
|
|
"finance_contact_mobile": row.finance_contact_mobile,
|
|
|
"remark": row.remark,
|
|
"remark": row.remark,
|
|
|
"address": row.address,
|
|
"address": row.address,
|
|
|
|
|
+ "row_version": row
|
|
|
|
|
+ .row_version
|
|
|
|
|
+ .as_deref()
|
|
|
|
|
+ .and_then(|s| s.parse::<i64>().ok())
|
|
|
|
|
+ .unwrap_or(0),
|
|
|
"identity_no_masked": identity_masked,
|
|
"identity_no_masked": identity_masked,
|
|
|
},
|
|
},
|
|
|
"team_ids": teams.get(&row.customer_id).cloned().unwrap_or_default(),
|
|
"team_ids": teams.get(&row.customer_id).cloned().unwrap_or_default(),
|
|
@@ -2189,6 +2272,7 @@ async fn do_update(state: &AppState, auth: &AuthContext, body: CustomerUpdate) -
|
|
|
RoleCode::Admin,
|
|
RoleCode::Admin,
|
|
|
RoleCode::OpsMain,
|
|
RoleCode::OpsMain,
|
|
|
RoleCode::OpsSub,
|
|
RoleCode::OpsSub,
|
|
|
|
|
+ RoleCode::TeamLead,
|
|
|
RoleCode::Ops,
|
|
RoleCode::Ops,
|
|
|
])?;
|
|
])?;
|
|
|
let cid = match body.customer_id.as_deref().map(str::trim) {
|
|
let cid = match body.customer_id.as_deref().map(str::trim) {
|
|
@@ -2233,6 +2317,13 @@ async fn do_update(state: &AppState, auth: &AuthContext, body: CustomerUpdate) -
|
|
|
if let Some(s) = body.subject_type.as_deref().map(str::trim).filter(|s| !s.is_empty()) {
|
|
if let Some(s) = body.subject_type.as_deref().map(str::trim).filter(|s| !s.is_empty()) {
|
|
|
check_subject(s)?;
|
|
check_subject(s)?;
|
|
|
}
|
|
}
|
|
|
|
|
+ if let Some(name) = body.customer_name.as_deref().map(str::trim) {
|
|
|
|
|
+ check_customer_name(name)?;
|
|
|
|
|
+ }
|
|
|
|
|
+ check_remark(body.remark.as_deref())?;
|
|
|
|
|
+ check_contact_phone("联系电话", body.contact_mobile.as_deref())?;
|
|
|
|
|
+ check_contact_phone("技术联系人电话", body.tech_contact_mobile.as_deref())?;
|
|
|
|
|
+ check_contact_phone("财务联系人电话", body.finance_contact_mobile.as_deref())?;
|
|
|
// 识别号改写:归一化 → 平台 HMAC 查重(排除自身)→ 平台加密。变更依据**选填**。
|
|
// 识别号改写:归一化 → 平台 HMAC 查重(排除自身)→ 平台加密。变更依据**选填**。
|
|
|
let identity = match body.id_code.as_deref().map(str::trim).filter(|s| !s.is_empty()) {
|
|
let identity = match body.id_code.as_deref().map(str::trim).filter(|s| !s.is_empty()) {
|
|
|
Some(raw) => Some(seal_identity(state, raw, Some(&cid)).await?),
|
|
Some(raw) => Some(seal_identity(state, raw, Some(&cid)).await?),
|
|
@@ -2639,13 +2730,39 @@ async fn n_of(state: &AppState, cid: &str, tpl: &str) -> Result<i64, BosError> {
|
|
|
count_where(state, &tpl.replace("{p}", &ph), p.values()).await
|
|
count_where(state, &tpl.replace("{p}", &ph), p.values()).await
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
-/// 删除前置检查:dev **有表可查**的六项给真数,第 1 册点名的另四类源表不在骨架内
|
|
|
|
|
-/// ⇒ 一律列进 `unverifiable_checks` 并据此拒绝删除(⛔ 查不出=放行)。
|
|
|
|
|
|
|
+/// 删除前置检查(客户§3.6):
|
|
|
|
|
+/// * **阻塞项**=四类业务依赖(未办完工单/在途开停/活跃账单明细/分成明细)+
|
|
|
|
|
+/// 仍须人工先清的合同/未终止订购/分账号/期初归属快照;
|
|
|
|
|
+/// * 运维团队归属/区域挂载=删除级联清掉,只进 `verified_checks`、**不进 blockers**;
|
|
|
|
|
+/// * 四类依赖表已在库内可查 ⇒ 真数进 blockers,`unverifiable_checks` 恒空(D-10)。
|
|
|
async fn run_precheck(state: &AppState, cid: &str) -> Result<Precheck, BosError> {
|
|
async fn run_precheck(state: &AppState, cid: &str) -> Result<Precheck, BosError> {
|
|
|
let mut verified: Vec<Value> = Vec::new();
|
|
let mut verified: Vec<Value> = Vec::new();
|
|
|
let mut blockers: Vec<String> = Vec::new();
|
|
let mut blockers: Vec<String> = Vec::new();
|
|
|
- let mut parts: Vec<String> = vec!["pv1".to_string()];
|
|
|
|
|
- let checks: [(&str, &str, &str, &str); 6] = [
|
|
|
|
|
|
|
+ let mut parts: Vec<String> = vec!["pv2".to_string()];
|
|
|
|
|
+
|
|
|
|
|
+ // 信息项:删除级联会清(团队归属/区域挂载)⇒ 只核实、不阻塞。
|
|
|
|
|
+ let info: [(&str, &str, &str); 2] = [
|
|
|
|
|
+ (
|
|
|
|
|
+ "ops_team_belong",
|
|
|
|
|
+ "运维团队归属",
|
|
|
|
|
+ "SELECT COUNT(1) AS n FROM customer_team_belong WHERE customer_id = {p}::text \
|
|
|
|
|
+ AND is_current = TRUE",
|
|
|
|
|
+ ),
|
|
|
|
|
+ (
|
|
|
|
|
+ "region_mount",
|
|
|
|
|
+ "区域挂载",
|
|
|
|
|
+ "SELECT COUNT(1) AS n FROM customer_region_mount WHERE customer_id = {p}::text \
|
|
|
|
|
+ AND terminal_state = 'mounted'",
|
|
|
|
|
+ ),
|
|
|
|
|
+ ];
|
|
|
|
|
+ for (key, label, tpl) in info {
|
|
|
|
|
+ let n = n_of(state, cid, tpl).await?;
|
|
|
|
|
+ verified.push(json!({ "key": key, "label": label, "count": n, "blocking": false }));
|
|
|
|
|
+ parts.push(format!("{key}={n}"));
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ // 须先清的依赖:级联一期未覆盖完整注销通路 ⇒ 仍阻塞。
|
|
|
|
|
+ let hard: [(&str, &str, &str, &str); 4] = [
|
|
|
(
|
|
(
|
|
|
"sub_account",
|
|
"sub_account",
|
|
|
"分账号档案",
|
|
"分账号档案",
|
|
@@ -2656,7 +2773,7 @@ async fn run_precheck(state: &AppState, cid: &str) -> Result<Precheck, BosError>
|
|
|
"contract",
|
|
"contract",
|
|
|
"合同档案",
|
|
"合同档案",
|
|
|
"SELECT COUNT(1) AS n FROM contract WHERE customer_id = {p}::text",
|
|
"SELECT COUNT(1) AS n FROM contract WHERE customer_id = {p}::text",
|
|
|
- "客户名下仍有 {n} 份合同档案(dev `contract` 无软删列 ⇒ 无法排除已注销,按最保守口径计数)",
|
|
|
|
|
|
|
+ "客户名下仍有 {n} 份合同档案(须先处理)",
|
|
|
),
|
|
),
|
|
|
(
|
|
(
|
|
|
"subscription",
|
|
"subscription",
|
|
@@ -2671,61 +2788,59 @@ async fn run_precheck(state: &AppState, cid: &str) -> Result<Precheck, BosError>
|
|
|
"SELECT COUNT(1) AS n FROM billing_snapshot_customer WHERE customer_id = {p}::text",
|
|
"SELECT COUNT(1) AS n FROM billing_snapshot_customer WHERE customer_id = {p}::text",
|
|
|
"客户已有 {n} 个账期的期初归属快照=出账凭证,删除会切断账单溯源链",
|
|
"客户已有 {n} 个账期的期初归属快照=出账凭证,删除会切断账单溯源链",
|
|
|
),
|
|
),
|
|
|
|
|
+ ];
|
|
|
|
|
+ for (key, label, tpl, reason) in hard {
|
|
|
|
|
+ let n = n_of(state, cid, tpl).await?;
|
|
|
|
|
+ verified.push(json!({ "key": key, "label": label, "count": n, "blocking": n > 0 }));
|
|
|
|
|
+ parts.push(format!("{key}={n}"));
|
|
|
|
|
+ if n > 0 {
|
|
|
|
|
+ blockers.push(reason.replace("{n}", &n.to_string()));
|
|
|
|
|
+ }
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ // 客户§3.6 四类阻塞:真查 ticket/ticket_open_stop/bill_detail。
|
|
|
|
|
+ let design: [(&str, &str, &str, &str); 4] = [
|
|
|
(
|
|
(
|
|
|
- "ops_team_belong",
|
|
|
|
|
- "运维团队归属",
|
|
|
|
|
- "SELECT COUNT(1) AS n FROM customer_team_belong WHERE customer_id = {p}::text \
|
|
|
|
|
- AND is_current = TRUE",
|
|
|
|
|
- "客户仍挂靠 {n} 支运维团队",
|
|
|
|
|
|
|
+ "open_ticket",
|
|
|
|
|
+ "未办完工单",
|
|
|
|
|
+ "SELECT COUNT(1) AS n FROM ticket WHERE customer_id = {p}::text \
|
|
|
|
|
+ AND status IN ('unassigned','pending','processing','completed','rejected')",
|
|
|
|
|
+ "客户名下仍有 {n} 张未办完工单(须先办结或终止)",
|
|
|
),
|
|
),
|
|
|
(
|
|
(
|
|
|
- "region_mount",
|
|
|
|
|
- "区域挂载",
|
|
|
|
|
- "SELECT COUNT(1) AS n FROM customer_region_mount WHERE customer_id = {p}::text",
|
|
|
|
|
- "客户仍有 {n} 条区域挂载关系",
|
|
|
|
|
|
|
+ "open_stop_record",
|
|
|
|
|
+ "开通/开停记录",
|
|
|
|
|
+ "SELECT COUNT(1) AS n FROM ticket_open_stop WHERE customer_id = {p}::text \
|
|
|
|
|
+ AND (closed_flag IS NULL OR closed_flag = FALSE)",
|
|
|
|
|
+ "客户名下仍有 {n} 条未关闭的开通/开停记录",
|
|
|
|
|
+ ),
|
|
|
|
|
+ (
|
|
|
|
|
+ "unsettled_bill",
|
|
|
|
|
+ "未对账账单",
|
|
|
|
|
+ "SELECT COUNT(1) AS n FROM bill_detail WHERE customer_id = {p}::text \
|
|
|
|
|
+ AND status = 'active'",
|
|
|
|
|
+ "客户名下仍有 {n} 条活跃账单明细",
|
|
|
|
|
+ ),
|
|
|
|
|
+ (
|
|
|
|
|
+ "share_detail",
|
|
|
|
|
+ "分成明细",
|
|
|
|
|
+ "SELECT COUNT(1) AS n FROM bill_detail WHERE customer_id = {p}::text \
|
|
|
|
|
+ AND status = 'active' AND COALESCE(share_amount_cents, 0) <> 0",
|
|
|
|
|
+ "客户名下仍有 {n} 条分成明细",
|
|
|
),
|
|
),
|
|
|
];
|
|
];
|
|
|
- for (key, label, tpl, reason) in checks {
|
|
|
|
|
|
|
+ for (key, label, tpl, reason) in design {
|
|
|
let n = n_of(state, cid, tpl).await?;
|
|
let n = n_of(state, cid, tpl).await?;
|
|
|
- verified.push(json!({
|
|
|
|
|
- "key": key,
|
|
|
|
|
- "label": label,
|
|
|
|
|
- "count": n,
|
|
|
|
|
- "blocking": n > 0,
|
|
|
|
|
- }));
|
|
|
|
|
|
|
+ verified.push(json!({ "key": key, "label": label, "count": n, "blocking": n > 0 }));
|
|
|
parts.push(format!("{key}={n}"));
|
|
parts.push(format!("{key}={n}"));
|
|
|
if n > 0 {
|
|
if n > 0 {
|
|
|
blockers.push(reason.replace("{n}", &n.to_string()));
|
|
blockers.push(reason.replace("{n}", &n.to_string()));
|
|
|
}
|
|
}
|
|
|
}
|
|
}
|
|
|
- // 第 1 册「删除阻塞项」四类源表在 dev 骨架**不存在**(全仓建表清单⛔ 工单/开通记录/
|
|
|
|
|
- // 账单流水/分成明细)⇒ ⛔ 把"查不到"当成"没有依赖",逐项点名登记。
|
|
|
|
|
- let unverifiable = vec![
|
|
|
|
|
- json!({
|
|
|
|
|
- "key": "open_ticket",
|
|
|
|
|
- "label": "未办完工单",
|
|
|
|
|
- "why": "工单/流程实例表不在 dev 骨架(29 表清单无 ticket/flow)⇒ 无法判定",
|
|
|
|
|
- }),
|
|
|
|
|
- json!({
|
|
|
|
|
- "key": "open_stop_record",
|
|
|
|
|
- "label": "开通/开停记录",
|
|
|
|
|
- "why": "开通记录表不在 dev 骨架 ⇒ 无法判定是否存在在途开通",
|
|
|
|
|
- }),
|
|
|
|
|
- json!({
|
|
|
|
|
- "key": "unsettled_bill",
|
|
|
|
|
- "label": "未对账账单",
|
|
|
|
|
- "why": "账单流水/账期结算表不在 dev 骨架;billing_snapshot_customer 只是期初\
|
|
|
|
|
- 归属快照,不当作「账期内无未对账账单」的证据",
|
|
|
|
|
- }),
|
|
|
|
|
- json!({
|
|
|
|
|
- "key": "share_detail",
|
|
|
|
|
- "label": "分成明细",
|
|
|
|
|
- "why": "dev 仅有分成规则配置表 share_rule,无分成明细表 ⇒ 无法判定是否已产生分成",
|
|
|
|
|
- }),
|
|
|
|
|
- ];
|
|
|
|
|
|
|
+
|
|
|
Ok(Precheck {
|
|
Ok(Precheck {
|
|
|
verified,
|
|
verified,
|
|
|
- unverifiable,
|
|
|
|
|
|
|
+ unverifiable: Vec::new(),
|
|
|
blockers,
|
|
blockers,
|
|
|
version: parts.join("&"),
|
|
version: parts.join("&"),
|
|
|
})
|
|
})
|
|
@@ -2736,6 +2851,10 @@ pub async fn delete_precheck(
|
|
|
RequireAuth(auth): RequireAuth,
|
|
RequireAuth(auth): RequireAuth,
|
|
|
DataBody(body): DataBody<CustomerIdReq>,
|
|
DataBody(body): DataBody<CustomerIdReq>,
|
|
|
) -> Response {
|
|
) -> Response {
|
|
|
|
|
+ // 与写口同档(D-11):合伙人等只读身份⛔ 调预检拿快照。
|
|
|
|
|
+ if let Err(e) = auth.require_roles(&[RoleCode::Admin, RoleCode::OpsMain]) {
|
|
|
|
|
+ return json_err(e);
|
|
|
|
|
+ }
|
|
|
let cid = match body.customer_id.as_deref().map(str::trim) {
|
|
let cid = match body.customer_id.as_deref().map(str::trim) {
|
|
|
Some(s) if !s.is_empty() => s.to_string(),
|
|
Some(s) if !s.is_empty() => s.to_string(),
|
|
|
_ => return json_err(BosError::bad_params("删除前置校验必须带 customer_id")),
|
|
_ => return json_err(BosError::bad_params("删除前置校验必须带 customer_id")),
|
|
@@ -2745,7 +2864,7 @@ pub async fn delete_precheck(
|
|
|
}
|
|
}
|
|
|
match run_precheck(&state, &cid).await {
|
|
match run_precheck(&state, &cid).await {
|
|
|
Ok(p) => {
|
|
Ok(p) => {
|
|
|
- let can_delete = p.blockers.is_empty() && p.unverifiable.is_empty();
|
|
|
|
|
|
|
+ let can_delete = p.blockers.is_empty();
|
|
|
json_ok(json!({
|
|
json_ok(json!({
|
|
|
"customer_id": cid,
|
|
"customer_id": cid,
|
|
|
"can_delete": can_delete,
|
|
"can_delete": can_delete,
|
|
@@ -2754,11 +2873,11 @@ pub async fn delete_precheck(
|
|
|
"unverifiable_checks": p.unverifiable,
|
|
"unverifiable_checks": p.unverifiable,
|
|
|
"precheck_version": p.version,
|
|
"precheck_version": p.version,
|
|
|
"message": format!(
|
|
"message": format!(
|
|
|
- "可判定项 {} 项(其中阻塞 {} 项);不可判定项 {} 项 ⇒ 按「查不出=放行」\
|
|
|
|
|
- 口径拒绝删除",
|
|
|
|
|
|
|
+ "可判定项 {} 项(其中阻塞 {} 项);不可判定项 {} 项{}",
|
|
|
p.verified.len(),
|
|
p.verified.len(),
|
|
|
p.blockers.len(),
|
|
p.blockers.len(),
|
|
|
- p.unverifiable.len()
|
|
|
|
|
|
|
+ p.unverifiable.len(),
|
|
|
|
|
+ if can_delete { " ⇒ 可删除" } else { " ⇒ 不可删除" }
|
|
|
),
|
|
),
|
|
|
}))
|
|
}))
|
|
|
}
|
|
}
|
|
@@ -2766,11 +2885,9 @@ pub async fn delete_precheck(
|
|
|
}
|
|
}
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
-/// `/customer/delete@v1`:软删(`deleted_at`)。
|
|
|
|
|
|
|
+/// `/customer/delete@v1`:软删(`deleted_at`)+关闭当期团队归属+摘树标记。
|
|
|
///
|
|
///
|
|
|
-/// 本轮实况=**总是拒绝**:四类阻塞源表不在 dev 骨架 ⇒ 前置检查必有不可判定项,
|
|
|
|
|
-/// 按第 1 册口径(⛔ 查不出=放行)不执行 `deleted_at` 写入。出参仍给全貌,
|
|
|
|
|
-/// ⛔ 回 `deleted: true` 假装删成。
|
|
|
|
|
|
|
+/// 一期级联未覆盖平台账号注销/订购硬删;有 blockers 时整单拒绝。
|
|
|
pub async fn delete(
|
|
pub async fn delete(
|
|
|
State(state): State<AppState>,
|
|
State(state): State<AppState>,
|
|
|
RequireAuth(auth): RequireAuth,
|
|
RequireAuth(auth): RequireAuth,
|
|
@@ -2806,21 +2923,44 @@ pub async fn delete(
|
|
|
p.version
|
|
p.version
|
|
|
)));
|
|
)));
|
|
|
}
|
|
}
|
|
|
- let payload = json!({
|
|
|
|
|
- "customer_id": cid,
|
|
|
|
|
- "deleted": false,
|
|
|
|
|
- "blockers": p.blockers,
|
|
|
|
|
- "verified_checks": p.verified,
|
|
|
|
|
- "unverifiable_checks": p.unverifiable,
|
|
|
|
|
- "precheck_version": p.version,
|
|
|
|
|
- });
|
|
|
|
|
if !p.blockers.is_empty() {
|
|
if !p.blockers.is_empty() {
|
|
|
return json_err(BosError::bad_params(format!(
|
|
return json_err(BosError::bad_params(format!(
|
|
|
"客户 {cid} 不可删除:{}",
|
|
"客户 {cid} 不可删除:{}",
|
|
|
p.blockers.join(";")
|
|
p.blockers.join(";")
|
|
|
)));
|
|
)));
|
|
|
}
|
|
}
|
|
|
- json_ok(payload)
|
|
|
|
|
|
|
+ let stmts = vec![
|
|
|
|
|
+ DmlStatement::new(
|
|
|
|
|
+ "UPDATE customer SET deleted_at = NOW(), row_version = coalesce(row_version, 0) + 1, \
|
|
|
|
|
+ updated_at = NOW() \
|
|
|
|
|
+ WHERE customer_id = $1::text AND deleted_at IS NULL"
|
|
|
|
|
+ .to_string(),
|
|
|
|
|
+ vec![Value::String(cid.clone())],
|
|
|
|
|
+ ),
|
|
|
|
|
+ DmlStatement::new(
|
|
|
|
|
+ "UPDATE customer_team_belong SET is_current = FALSE, valid_to = NOW() \
|
|
|
|
|
+ WHERE customer_id = $1::text AND is_current = TRUE"
|
|
|
|
|
+ .to_string(),
|
|
|
|
|
+ vec![Value::String(cid.clone())],
|
|
|
|
|
+ ),
|
|
|
|
|
+ DmlStatement::new(
|
|
|
|
|
+ "UPDATE customer_region_mount SET terminal_state = 'deleting' \
|
|
|
|
|
+ WHERE customer_id = $1::text AND terminal_state = 'mounted'"
|
|
|
|
|
+ .to_string(),
|
|
|
|
|
+ vec![Value::String(cid.clone())],
|
|
|
|
|
+ ),
|
|
|
|
|
+ ];
|
|
|
|
|
+ if let Err(e) = state.db.dml_batch(&stmts).await {
|
|
|
|
|
+ return json_err(e);
|
|
|
|
|
+ }
|
|
|
|
|
+ json_ok(json!({
|
|
|
|
|
+ "customer_id": cid,
|
|
|
|
|
+ "deleted": true,
|
|
|
|
|
+ "blockers": p.blockers,
|
|
|
|
|
+ "verified_checks": p.verified,
|
|
|
|
|
+ "unverifiable_checks": p.unverifiable,
|
|
|
|
|
+ "precheck_version": p.version,
|
|
|
|
|
+ }))
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
/// `/customer/delete@v1` 请求体(openapi 只把 `customer_id` 列进 `required`;
|
|
/// `/customer/delete@v1` 请求体(openapi 只把 `customer_id` 列进 `required`;
|