feat: 上传京东账单时自动软删除其他来源中的京东订单
This commit is contained in:
@@ -458,6 +458,41 @@ func (r *Repository) DeleteCleanedBillByID(id string) error {
|
||||
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 客户端(用于兼容旧代码)
|
||||
func (r *Repository) GetClient() *mongo.Client {
|
||||
return r.client
|
||||
|
||||
@@ -51,4 +51,9 @@ type BillRepository interface {
|
||||
|
||||
// CountRawByField 按字段统计原始数据数量
|
||||
CountRawByField(fieldName, value string) (int64, error)
|
||||
|
||||
// SoftDeleteJDRelatedBills 软删除描述中包含"京东-订单编号"的非京东账单
|
||||
// 用于避免京东账单与其他来源(微信、支付宝)账单重复计算
|
||||
// 返回: 删除数量、错误
|
||||
SoftDeleteJDRelatedBills() (int64, error)
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user