1 using System;
2 using System.Collections.Generic;
3 using System.Data;
4 using System.Data.SQLite;
5 using System.Linq;
6 using System.Text;
7 using Dapper;
8 using PaiXie.Utils;
9 namespace PaiXie.Pos.Client.Core
10 {
11 public class Data<T> where T : class
12 {
13 #region 构造函数
14
15 private static Data<T> _instance;
16 public static Data<T> GetInstance()
17 {
18 if (_instance == null)
19 {
20 _instance = new Data<T>();
21 }
22 return _instance;
23 }
24
25 #endregion
26
27 /// <summary>
28 /// 分页列表
29 /// </summary>
30 /// <param name="data"></param>
31 /// <param name="model"></param>
32 /// <param name="count"></param>
33 /// <param name="context"></param>
34 /// <param name="transaction"></param>
35 /// <returns></returns>
36 public List<T> GetQueryManyForPage(SelectBuilder data, T model, out int count, SQLiteConnection context = null, IDbTransaction transaction = null)
37 {
38 if (context == null) context = Db.GetInstance().Context();
39 string sqlStr = Db.GetInstance().GetSqlForSelectBuilder(data);
40 string sqlStrCount = Db.GetInstance().GetSqlForTotalBuilder(data);
41 List<T> list = context.Query<T>(sqlStr, model, transaction).ToList();
42 count = ZConvert.StrToInt(context.ExecuteScalar(sqlStrCount, model, transaction), 0);
43 return list;
44 }
45 /// <summary>
46 /// 添加
47 /// </summary>
48 /// <param name="query"></param>
49 /// <param name="model"></param>
50 /// <param name="context"></param>
51 /// <param name="transaction"></param>
52 /// <returns></returns>
53 public int Add(string query, T model, SQLiteConnection context = null, IDbTransaction transaction = null)
54 {
55 if (context == null) context = Db.GetInstance().Context();
56 // query += " ; select last_insert_rowid() newid";
57 // string id = context.ExecuteScalar(query, model, transaction).ToString();
58 // return ZConvert.StrToInt(id, 0);
59 int id = context.Execute(query, model, transaction);
60 return id;
61 }
62
63
64
65
66
67
68 /// <summary>
69 /// 修改
70 /// </summary>
71 /// <param name="query"></param>
72 /// <param name="model"></param>
73 /// <param name="context"></param>
74 /// <param name="transaction"></param>
75 /// <returns></returns>
76 public int Update(string query, T model, SQLiteConnection context = null, IDbTransaction transaction = null)
77 {
78 if (context == null) context = Db.GetInstance().Context();
79 int id = context.Execute(query, model, transaction);
80 return id;
81 }
82 /// <summary>
83 /// 删除
84 /// </summary>
85 /// <param name="tablename"></param>
86 /// <param name="wheresql"></param>
87 /// <param name="model"></param>
88 /// <param name="context"></param>
89 /// <param name="transaction"></param>
90 /// <returns></returns>
91 public int Delete(string tablename, string wheresql, T model, SQLiteConnection context = null, IDbTransaction transaction = null)
92 {
93 if (context == null) context = Db.GetInstance().Context();
94 string query = "DELETE FROM " + tablename + " WHERE " + wheresql;
95 int id = context.Execute(query, model, transaction);
96 return id;
97 }
98 /// <summary>
99 /// 获取单个实体
100 /// </summary>
101 /// <param name="tablename"></param>
102 /// <param name="wheresql"></param>
103 /// <param name="model"></param>
104 /// <param name="context"></param>
105 /// <param name="transaction"></param>
106 /// <returns></returns>
107 public T GetModel(string tablename, string wheresql, T model, SQLiteConnection context = null, IDbTransaction transaction = null)
108 {
109 if (context == null) context = Db.GetInstance().Context();
110 string query = "SELECT * FROM " + tablename + " WHERE " + wheresql + " LIMIT 0,1";
111 T obj = context.Query<T>(query, model, transaction).SingleOrDefault();
112 return obj;
113 }
114
115 /// <summary>
116 /// 实体列表
117 /// </summary>
118 /// <param name="query"></param>
119 /// <param name="model"></param>
120 /// <param name="context"></param>
121 /// <param name="transaction"></param>
122 /// <returns></returns>
123 public List<T> GetModelList(string query, T model, SQLiteConnection context = null, IDbTransaction transaction = null)
124 {
125 if (context == null) context = Db.GetInstance().Context();
126 List<T> list = context.Query<T>(query, model, transaction).ToList();
127 return list;
128 }
129
130
131
132
133
134
135
136
137
138
139
140
141
142 }
143 }