sync: 同步 v1.0.4 开源代码并脱敏

This commit is contained in:
MiFaZhan
2026-10-29 14:52:43 +08:00
parent 32e4316ee4
commit 6893e21f07
136 changed files with 9851 additions and 436 deletions
@@ -0,0 +1,70 @@
package lifecycle
import (
"fmt"
"sort"
"strings"
"git.5iot.com/iotlcm-public/iotlcm-worker-svr/internal/model/entity"
lifecycle "git.5iot.com/iotlcm-public/iotlcm-worker-svr/internal/service/lifecycle"
)
// batchFailureSummary 生成中文失败汇总,替代原先的英文「N SIM cards failed」。
// 形如:共50张,成功28张,失败22张;主要失败原因:当前卡状态不支持该操作
func batchFailureSummary(total, success, fail int, reasons []string) string {
var builder strings.Builder
fmt.Fprintf(&builder, "共%d张,成功%d张,失败%d张", total, success, fail)
if reason := dominantReason(reasons); reason != "" {
builder.WriteString(";主要失败原因:")
builder.WriteString(reason)
}
return builder.String()
}
// dominantReason 取出现次数最多的失败原因;次数相同时按字典序,保证结果稳定可测。
func dominantReason(reasons []string) string {
counts := make(map[string]int, len(reasons))
for _, reason := range reasons {
trimmed := strings.TrimSpace(reason)
if trimmed == "" {
continue
}
counts[trimmed]++
}
if len(counts) == 0 {
return ""
}
keys := make([]string, 0, len(counts))
for key := range counts {
keys = append(keys, key)
}
sort.Slice(keys, func(i, j int) bool {
if counts[keys[i]] != counts[keys[j]] {
return counts[keys[i]] > counts[keys[j]]
}
return keys[i] < keys[j]
})
return keys[0]
}
// lifecycleFailureSummary 依据内存中的任务明细生成汇总。
func lifecycleFailureSummary(details []*lifecycle.Detail, success, fail int) string {
reasons := make([]string, 0, fail)
for _, detail := range details {
if detail != nil && strings.TrimSpace(detail.Reason) != "" {
reasons = append(reasons, detail.Reason)
}
}
return batchFailureSummary(len(details), success, fail, reasons)
}
// storedFailureSummary 依据已落库的批次明细生成汇总(重启批次的复机阶段使用)。
func storedFailureSummary(details []*entity.BatchDetail, success, fail int) string {
reasons := make([]string, 0, fail)
for _, detail := range details {
if detail != nil && strings.TrimSpace(detail.ErrorReason) != "" {
reasons = append(reasons, detail.ErrorReason)
}
}
return batchFailureSummary(len(details), success, fail, reasons)
}
@@ -0,0 +1,73 @@
package lifecycle
import (
"strings"
"testing"
"git.5iot.com/iotlcm-public/iotlcm-worker-svr/internal/model/entity"
lifecycle "git.5iot.com/iotlcm-public/iotlcm-worker-svr/internal/service/lifecycle"
)
// TestBatchFailureSummaryUsesChineseAndCounts 固化用户要求的失败文案:
// 必须是中文、带成功失败张数,并附带最主要的失败原因。
func TestBatchFailureSummaryUsesChineseAndCounts(t *testing.T) {
got := batchFailureSummary(50, 28, 22, []string{
"当前卡状态不支持该操作",
"当前卡状态不支持该操作",
"请求上游超时",
})
want := "共50张,成功28张,失败22张;主要失败原因:当前卡状态不支持该操作"
if got != want {
t.Fatalf("失败汇总=%q,期望%q", got, want)
}
if strings.Contains(got, "SIM cards failed") {
t.Fatal("仍包含英文文案")
}
}
// TestBatchFailureSummaryWithoutReason 覆盖没有具体原因的情况,不能拼出空的原因段。
func TestBatchFailureSummaryWithoutReason(t *testing.T) {
if got := batchFailureSummary(3, 1, 2, nil); got != "共3张,成功1张,失败2张" {
t.Fatalf("无原因时=%q", got)
}
if got := batchFailureSummary(3, 1, 2, []string{"", " "}); got != "共3张,成功1张,失败2张" {
t.Fatalf("空白原因应被忽略,实际=%q", got)
}
}
// TestDominantReasonStableOnTie 同频时按字节序取最小值,保证同一批次文案稳定。
// 注意这里是字节序而非拼音序:「乙」的 UTF-8 编码小于「甲」,所以同频时取「乙原因」。
func TestDominantReasonStableOnTie(t *testing.T) {
if got := dominantReason([]string{"甲原因", "乙原因"}); got != "乙原因" {
t.Fatalf("同频应按字节序取最小,实际=%q", got)
}
if got := dominantReason([]string{"少数", "多数", "多数"}); got != "多数" {
t.Fatalf("应取出现次数最多,实际=%q", got)
}
}
// TestStoredFailureSummarySkipsNilAndEmpty 覆盖重启复机阶段:落库明细里的空原因与空指针不能参与统计。
func TestStoredFailureSummarySkipsNilAndEmpty(t *testing.T) {
details := []*entity.BatchDetail{
{DetailStatus: 2},
nil,
{DetailStatus: 3, ErrorReason: "当前卡状态不支持该操作"},
{DetailStatus: 3, ErrorReason: ""},
}
got := storedFailureSummary(details, 1, 2)
if !strings.HasPrefix(got, "共4张,成功1张,失败2张") || !strings.Contains(got, "当前卡状态不支持该操作") {
t.Fatalf("落库汇总=%q", got)
}
}
// TestLifecycleFailureSummaryUsesDetailReason 覆盖停复机首阶段:内存明细的 Reason 作为主要失败原因。
func TestLifecycleFailureSummaryUsesDetailReason(t *testing.T) {
details := []*lifecycle.Detail{
{Reason: ""},
{Reason: "停机或预销户卡,不能办理该业务"},
}
got := lifecycleFailureSummary(details, 1, 1)
if got != "共2张,成功1张,失败1张;主要失败原因:停机或预销户卡,不能办理该业务" {
t.Fatalf("首阶段汇总=%q", got)
}
}
+3 -3
View File
@@ -211,7 +211,7 @@ func (r *Repository) SaveResult(ctx context.Context, task *lifecycle.Task, detai
if resolved.Success {
if detail.Card == nil {
resolved.Success = false
resolved.Message = "SIM card record is missing"
resolved.Message = "卡记录不存在,未办理"
} else {
result, err := tx.Ctx(ctx).Model(dao.TSimCardInfo.Table()).
Where(dao.TSimCardInfo.Columns().Id, detail.Card.ID).
@@ -231,7 +231,7 @@ func (r *Repository) SaveResult(ctx context.Context, task *lifecycle.Task, detai
}
if affected != 1 {
resolved.Success = false
resolved.Message = "SIM card local status changed"
resolved.Message = "卡本地状态已变更"
}
}
}
@@ -276,7 +276,7 @@ func (r *Repository) Finalize(ctx context.Context, task *lifecycle.Task) error {
failCount := len(task.Details) - successCount
batchAsyncStatus, runResult := batchResult(successCount, failCount)
if task.FailureReason == "" && failCount > 0 {
task.FailureReason = strconv.Itoa(failCount) + " SIM cards failed"
task.FailureReason = lifecycleFailureSummary(task.Details, successCount, failCount)
}
validCount := 0
if task.ValidationOK {
+4 -4
View File
@@ -135,7 +135,7 @@ func (r *Repository) SaveRestartValidationFailure(ctx context.Context, task *lif
continue
}
if strings.TrimSpace(detail.Reason) == "" {
detail.Reason = "restart validation failed; upstream request was skipped"
detail.Reason = "重启校验未通过,未发起上游请求"
}
outcome := lifecycle.Outcome{Message: detail.Reason}
result, err := tx.Ctx(ctx).Model(dao.BatchDetail.Table()).
@@ -175,7 +175,7 @@ func (r *Repository) SaveRestartResult(ctx context.Context, task *lifecycle.Task
if resolved.Success {
if detail.Card == nil {
resolved.Success = false
resolved.Message = "SIM card record is missing"
resolved.Message = "卡记录不存在,未办理"
} else {
result, err := tx.Ctx(ctx).Model(dao.TSimCardInfo.Table()).
Where(dao.TSimCardInfo.Columns().Id, detail.Card.ID).
@@ -195,7 +195,7 @@ func (r *Repository) SaveRestartResult(ctx context.Context, task *lifecycle.Task
}
if affected != 1 {
resolved.Success = false
resolved.Message = "SIM card local status changed"
resolved.Message = "卡本地状态已变更"
}
}
}
@@ -327,7 +327,7 @@ func (r *Repository) FinalizeRestart(ctx context.Context, batchNo string) error
batchAsyncStatus, runResult := batchResult(successCount, failCount)
failReason := ""
if failCount > 0 {
failReason = strconv.Itoa(failCount) + " SIM cards failed"
failReason = storedFailureSummary(details, successCount, failCount)
}
validCount := 0
if restartValidationOK(main.BatchInfo) {