forked from NewLifeX/X
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathEntityDeferredQueue.cs
More file actions
63 lines (57 loc) · 1.88 KB
/
Copy pathEntityDeferredQueue.cs
File metadata and controls
63 lines (57 loc) · 1.88 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using NewLife.Model;
namespace XCode.Model
{
/// <summary>实体延迟队列。缓冲合并对象,批量处理</summary>
public class EntityDeferredQueue : DeferredQueue
{
#region 属性
/// <summary>是否批量保存。默认true,使用数据库批量更新操作</summary>
public Boolean BatchSave { get; set; } = true;
#endregion
#region 方法
/// <summary>处理一批</summary>
/// <param name="list"></param>
public override Int32 Process(IList<Object> list)
{
if (list.Count == 0) return 0;
if (list.Count == 1) return (list[0] as IEntity).Save();
// 区分Update和Upsert
var rs = 0;
var us = new List<IEntity>();
var ns = new List<IEntity>();
var ss = new List<IEntity>();
foreach (IEntity item in list)
{
if (item != null)
{
if (BatchSave)
{
// 来自数据库,更新
if (item.IsFromDatabase)
us.Add(item);
// 空主键,插入
else if (item.IsNullKey)
ns.Add(item);
// 其它 Upsert
else
ss.Add(item);
}
else
{
rs += item.Save();
}
}
}
if (us.Count > 0) rs += us.Update(true);
if (ns.Count > 0) rs += ns.Insert(true);
if (ss.Count > 0) rs += ss.Valid().Upsert();
return rs;
}
#endregion
}
}