feat: 上传京东账单时自动软删除其他来源中的京东订单
This commit is contained in:
@@ -15,6 +15,7 @@ import (
|
|||||||
|
|
||||||
"billai-server/config"
|
"billai-server/config"
|
||||||
"billai-server/model"
|
"billai-server/model"
|
||||||
|
"billai-server/repository"
|
||||||
"billai-server/service"
|
"billai-server/service"
|
||||||
)
|
)
|
||||||
|
|
||||||
@@ -254,11 +255,29 @@ func Upload(c *gin.Context) {
|
|||||||
}
|
}
|
||||||
service.CleanupExtractedFiles(extractedFiles)
|
service.CleanupExtractedFiles(extractedFiles)
|
||||||
|
|
||||||
// 13. 返回成功响应
|
// 13. 如果是京东账单,软删除其他来源中包含"京东-订单编号"的记录
|
||||||
|
var jdRelatedDeleted int64
|
||||||
|
if billType == "jd" {
|
||||||
|
repo := repository.GetRepository()
|
||||||
|
if repo != nil {
|
||||||
|
deleted, err := repo.SoftDeleteJDRelatedBills()
|
||||||
|
if err != nil {
|
||||||
|
fmt.Printf("⚠️ 软删除京东关联记录失败: %v\n", err)
|
||||||
|
} else if deleted > 0 {
|
||||||
|
jdRelatedDeleted = deleted
|
||||||
|
fmt.Printf("🗑️ 已软删除 %d 条其他来源中的京东关联记录\n", deleted)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// 14. 返回成功响应
|
||||||
message := fmt.Sprintf("处理成功,新增 %d 条记录", cleanedCount)
|
message := fmt.Sprintf("处理成功,新增 %d 条记录", cleanedCount)
|
||||||
if dedupResult.DuplicateCount > 0 {
|
if dedupResult.DuplicateCount > 0 {
|
||||||
message = fmt.Sprintf("处理成功,新增 %d 条,跳过 %d 条重复记录", cleanedCount, dedupResult.DuplicateCount)
|
message = fmt.Sprintf("处理成功,新增 %d 条,跳过 %d 条重复记录", cleanedCount, dedupResult.DuplicateCount)
|
||||||
}
|
}
|
||||||
|
if jdRelatedDeleted > 0 {
|
||||||
|
message = fmt.Sprintf("%s,标记删除 %d 条重复的京东订单", message, jdRelatedDeleted)
|
||||||
|
}
|
||||||
|
|
||||||
c.JSON(http.StatusOK, model.UploadResponse{
|
c.JSON(http.StatusOK, model.UploadResponse{
|
||||||
Result: true,
|
Result: true,
|
||||||
@@ -270,6 +289,7 @@ func Upload(c *gin.Context) {
|
|||||||
RawCount: rawCount,
|
RawCount: rawCount,
|
||||||
CleanedCount: cleanedCount,
|
CleanedCount: cleanedCount,
|
||||||
DuplicateCount: dedupResult.DuplicateCount,
|
DuplicateCount: dedupResult.DuplicateCount,
|
||||||
|
JDRelatedDeleted: jdRelatedDeleted,
|
||||||
},
|
},
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -8,6 +8,7 @@ type UploadData struct {
|
|||||||
RawCount int `json:"raw_count,omitempty"` // 存储到原始数据集合的记录数
|
RawCount int `json:"raw_count,omitempty"` // 存储到原始数据集合的记录数
|
||||||
CleanedCount int `json:"cleaned_count,omitempty"` // 存储到清洗后数据集合的记录数
|
CleanedCount int `json:"cleaned_count,omitempty"` // 存储到清洗后数据集合的记录数
|
||||||
DuplicateCount int `json:"duplicate_count,omitempty"` // 重复跳过的记录数
|
DuplicateCount int `json:"duplicate_count,omitempty"` // 重复跳过的记录数
|
||||||
|
JDRelatedDeleted int64 `json:"jd_related_deleted,omitempty"` // 软删除的京东关联记录数(其他来源中描述包含京东订单号的记录)
|
||||||
}
|
}
|
||||||
|
|
||||||
// UploadResponse 上传响应
|
// UploadResponse 上传响应
|
||||||
|
|||||||
@@ -458,6 +458,41 @@ func (r *Repository) DeleteCleanedBillByID(id string) error {
|
|||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// SoftDeleteJDRelatedBills 软删除描述中包含"京东-订单编号"的非京东账单
|
||||||
|
// 用于避免京东账单与其他来源(微信、支付宝)账单重复计算
|
||||||
|
func (r *Repository) SoftDeleteJDRelatedBills() (int64, error) {
|
||||||
|
if r.cleanedCollection == nil {
|
||||||
|
return 0, fmt.Errorf("cleaned collection not initialized")
|
||||||
|
}
|
||||||
|
|
||||||
|
ctx, cancel := context.WithTimeout(context.Background(), 30*time.Second)
|
||||||
|
defer cancel()
|
||||||
|
|
||||||
|
// 筛选条件:
|
||||||
|
// 1. 账单类型不是 jd(只处理微信、支付宝等其他来源)
|
||||||
|
// 2. 描述中包含"京东-订单编号"
|
||||||
|
// 3. 尚未被删除
|
||||||
|
filter := bson.M{
|
||||||
|
"bill_type": bson.M{"$ne": "jd"},
|
||||||
|
"description": bson.M{"$regex": "京东-订单编号", "$options": ""},
|
||||||
|
"is_deleted": bson.M{"$ne": true},
|
||||||
|
}
|
||||||
|
|
||||||
|
update := bson.M{
|
||||||
|
"$set": bson.M{
|
||||||
|
"is_deleted": true,
|
||||||
|
"updated_at": time.Now(),
|
||||||
|
},
|
||||||
|
}
|
||||||
|
|
||||||
|
result, err := r.cleanedCollection.UpdateMany(ctx, filter, update)
|
||||||
|
if err != nil {
|
||||||
|
return 0, fmt.Errorf("soft delete JD related bills failed: %w", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
return result.ModifiedCount, nil
|
||||||
|
}
|
||||||
|
|
||||||
// GetClient 获取 MongoDB 客户端(用于兼容旧代码)
|
// GetClient 获取 MongoDB 客户端(用于兼容旧代码)
|
||||||
func (r *Repository) GetClient() *mongo.Client {
|
func (r *Repository) GetClient() *mongo.Client {
|
||||||
return r.client
|
return r.client
|
||||||
|
|||||||
@@ -51,4 +51,9 @@ type BillRepository interface {
|
|||||||
|
|
||||||
// CountRawByField 按字段统计原始数据数量
|
// CountRawByField 按字段统计原始数据数量
|
||||||
CountRawByField(fieldName, value string) (int64, error)
|
CountRawByField(fieldName, value string) (int64, error)
|
||||||
|
|
||||||
|
// SoftDeleteJDRelatedBills 软删除描述中包含"京东-订单编号"的非京东账单
|
||||||
|
// 用于避免京东账单与其他来源(微信、支付宝)账单重复计算
|
||||||
|
// 返回: 删除数量、错误
|
||||||
|
SoftDeleteJDRelatedBills() (int64, error)
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user