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
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
using FLY.OBJComponents.IService;
using FLY.Weight.IService;
using FObjBase;
using FObjBase.Reflect;
using SQLite;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace FLY.Weight.Server.Model
{
public class BulkDbMix<TDb>:IBulkDbMixService
where TDb : IDbBase, new()
{
DBTable<TDb> dbTable;
SQLiteHelper sqliteHelper;
Func<Lc_Mix, TDb> mapLc2Db;
Func<TDb, Lc_Mix> mapDb2Lc;
public long LastId { get; private set; }
public int Index { get; private set; }
public event PropertyChangedEventHandler PropertyChanged;
public BulkDbMix()
{
}
public void Init(DBTable<TDb> dbTable, int index,
Func<Lc_Mix, TDb> mapLc2Db, Func<TDb, Lc_Mix> mapDb2Lc)
{
this.Index = index;
this.dbTable = dbTable;
this.sqliteHelper = dbTable.sqliteHelper;
this.mapLc2Db = mapLc2Db;
this.mapDb2Lc = mapDb2Lc;
//获取最后一行数据
LastId = dbTable.MaxID;
}
public void Add(Lc_Mix lc)
{
//添加 数据库必要的 field
lc.ID = dbTable.FreeID;
lc.Idx = Index;
//SQLs
List<string> sqls = new List<string>();
sqls.Add(SQLiteHelper.GetInsertCommandText(mapLc2Db(lc)));
sqliteHelper.QueryTranAsync(sqls);
LastId = lc.ID;
}
public void AddRange(IEnumerable<Lc_Mix> lcs)
{
//SQLs
List<string> sqls = new List<string>();
foreach (var lc in lcs)
{
//添加 数据库必要的 field
lc.ID = dbTable.FreeID;
lc.Idx = Index;
sqls.Add(SQLiteHelper.GetInsertCommandText(mapLc2Db(lc)));
}
sqliteHelper.QueryTranAsync(sqls);
LastId = lcs.Last().ID;
}
public async void GetTrend(Pack_GetTrendRequest request, AsyncCBHandler asyncDelegate, object asyncContext)
{
Pack_GetTrendReponse<Lc_Mix> reponse = new Pack_GetTrendReponse<Lc_Mix>();
reponse.Request = request;
if (request.Count > 400)//数量限制,避免死机
request.Count = 400;
else if (request.Count < 1)
request.Count = 1;
if (request.Interval < 1)//避免 /0
request.Interval = 1;
await Task.Factory.StartNew(() =>
{
List<TDb> dbs = null;
if (!request.IsSearchByTime)
{
if (request.Id <= 0)
dbs = dbTable.Find(
$"WHERE (ID % {request.Interval} = 0)" +
$" AND (Idx = {Index})" +
$" ORDER BY ID DESC" +
$" LIMIT {request.Count} OFFSET {-request.Id}");
else
dbs = dbTable.Find(
$"WHERE (ID <= {request.Id})" +
$" AND (ID % {request.Interval} = 0)" +
$" AND (Idx = {Index})" +
$" ORDER BY ID DESC" +
$" LIMIT {request.Count}");
}
else
{
dbs = dbTable.Find(
$"WHERE ( Time <= {request.Time.ToStringOfSQLiteFieldType()})" +
$" AND ( ID % {request.Interval} = 0)" +
$" AND ( Idx = {Index})" +
$" ORDER BY ID DESC" +
$" LIMIT {request.Count}");
}
if (dbs.Count() == 0)
return;
//转为 Lc_Mix
List<Lc_Mix> lcMixs = new List<Lc_Mix>();
foreach (var db in dbs) {
lcMixs.Add(mapDb2Lc(db));
}
//从尾向前排的!!!!
reponse.Values = lcMixs;
});
asyncDelegate(asyncContext, reponse);
}
}
}