admin
2020-06-22 924bdf1c9fb74babf2438d5545db3594756625d1
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
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Data.SQLite;
using System.Windows.Forms;
using WindowsFormsApp1.entity.tb;
using WindowsFormsApp1.entity;
 
namespace WindowsFormsApp1.utils
{
    class SQLiteDataBaseUtil
    {
 
        public SQLiteConnection getConnection() {
            return new SQLiteConnection("Data Source=BKZ;Version=3;");
        }
 
        SQLiteConnection con;
        private static SQLiteDataBaseUtil instance;
        public static SQLiteDataBaseUtil getInstance() {
            if (instance == null)
            {
                SQLiteConnection.CreateFile("BKZ");//创建数据库
                instance = new SQLiteDataBaseUtil();
                instance.con = new SQLiteConnection("Data Source=BKZ;Version=3;");
                instance.init();
            }
            return instance;
        }
 
 
        //初始化数据库
        private void init() {
            CreateTBAccountTable();
            CreateLoginTable();
            CreateConfigTable();
        }
 
 
        public void execUpdate(String sql) {
            con.Open();
            SQLiteCommand command = new SQLiteCommand(sql, con);
            command.ExecuteNonQuery();
            con.Close();
        }
 
 
 
 
 
        /*
         * 
         * 淘宝账号管理
         * 
         */
 
        private void CreateTBAccountTable() {
            SQLiteConnection con = getConnection();
            con.Open();
            string sql = "CREATE TABLE  IF NOT EXISTS tb_account_info (`index` int, accountName varchar(256), tbUid varchar(128), loginState int)";
            SQLiteCommand command = new SQLiteCommand(sql, con);
            command.ExecuteNonQuery();
            con.Close();
        }
 
        //添加本地淘宝账号
        public void AddTaoBaoAccount(TBAccountLogin account) {
            List<TBAccountLogin> list= GetByTBUid(account.TbUid);
            if (list != null && list.Count > 0)
                return;
            SQLiteConnection con = getConnection();
            con.Open();
            string sql = "insert into tb_account_info (`index`,accountName, tbUid,loginState) values ("+ account .Index+ ", '"+ account .NickName+ "',"+account.TbUid+", "+(account.Login?1:0)+")";
            SQLiteCommand command = new SQLiteCommand(sql, con);
            command.ExecuteNonQuery();
            con.Close();
        }
 
        //掉线
        public void TBOffLine(int index) {
            SQLiteConnection con= getConnection();
            con.Open();
            string sql = "update tb_account_info set loginState=0 where `index`=" + index;
            SQLiteCommand command = new SQLiteCommand(sql, con);
            command.ExecuteNonQuery();
            con.Close();
        }
 
 
        public void TBDelete(int index)
        {
            SQLiteConnection con = getConnection();
            con.Open();
            string sql = "delete from tb_account_info  where `index`=" + index;
            SQLiteCommand command = new SQLiteCommand(sql, con);
            command.ExecuteNonQuery();
            con.Close();
        }
 
        //上线
        public void TBOnLine(TBAccountLogin account) {
            TBDelete(account.Index);
            AddTaoBaoAccount(account);
        }
 
 
        //根据淘宝UID查询
        public List<TBAccountLogin> GetByTBUid(String tbUid) {
            List<TBAccountLogin> list = new List<TBAccountLogin>();
            SQLiteConnection con = getConnection();
            con.Open();
            string sql = "select * from tb_account_info where tbUid="+tbUid;
            if (tbUid == null) {
                sql = "select * from tb_account_info";
            }
            SQLiteCommand command = new SQLiteCommand(sql, con);
            SQLiteDataReader reader = command.ExecuteReader();
            while (reader.Read()) {
                TBAccountLogin account = new TBAccountLogin();
                account.NickName = reader["accountName"].ToString();
                account.Login = Convert.ToInt32(reader["loginState"]) == 1 ? true : false;
                account.TbUid = reader["tbUid"].ToString();
                account.Index =Convert.ToInt32(reader["index"].ToString());
                list.Add(account);
            }
            con.Close();
            return list;
        }
 
        /**
         * 
         * 登录账号管理
         * 
         */
 
 
        private void CreateLoginTable() {
            SQLiteConnection con = getConnection();
            con.Open();
            string sql = "CREATE TABLE  IF NOT EXISTS account_info (`account` varchar(256) PRIMARY KEY, `token` varchar(256), expireTime varchar(64),login_state int)";
            SQLiteCommand command = new SQLiteCommand(sql, con);
            command.ExecuteNonQuery();
            con.Close();
        }
 
 
        //添加账户
        public void AddAccount(UserInfo userInfo) {
            DeleteByAccount(userInfo.Account);
            SQLiteConnection con = getConnection();
            con.Open();
            string sql = "insert into  account_info(`account`,`token`,expireTime,login_state) values('" + userInfo.Account+ "','"+ userInfo.Token+ "','"+ TimeUtil.GetGeneralTime(userInfo.SdljShareExpireTime,"yyyyMMdd HH:mm:ss")+ "',1)";
            SQLiteCommand command = new SQLiteCommand(sql, con);
            command.ExecuteNonQuery();
            con.Close();
        }
 
        //删除账户
        public void DeleteByAccount(String account)
        {
            SQLiteConnection con = getConnection();
            con.Open();
            string sql = "delete from account_info where `account`='" + account + "'";
            SQLiteCommand command = new SQLiteCommand(sql, con);
            command.ExecuteNonQuery();
            con.Close();
        }
 
        //查询账户
        public UserInfo GetLoginAccount()
        {
            SQLiteConnection con = getConnection();
            con.Open();
            string sql = "select * from account_info where login_state=1 limit 1";
            SQLiteCommand command = new SQLiteCommand(sql, con);
            SQLiteDataReader reader = command.ExecuteReader();
            UserInfo user = new UserInfo();
            if (reader.Read())
            {
                user.Account = reader["account"].ToString();
                user.Token = reader["token"].ToString();
               // user.SdljShareExpireTime =new DateTime( reader["expireTime"].ToString());
            }
            else {
                user = null;
            }
            reader.Close();
            con.Close();
            return user;
        }
 
        /*
         *
         *系统配置参数
         * 
         */
 
 
        private void CreateConfigTable()
        {
            SQLiteConnection con = getConnection();
            con.Open();
            string sql = "CREATE TABLE  IF NOT EXISTS config (`key` varchar(256) PRIMARY KEY, `value` varchar(256))";
            SQLiteCommand command = new SQLiteCommand(sql, con);
            command.ExecuteNonQuery();
            con.Close();
        }
 
 
        //添加账户
        public void AddConfig(Config config)
        {
            DeleteConfig(config.Key);
            SQLiteConnection con = getConnection();
            con.Open();
            string sql = "insert into  config(`key`,`value`) values('" + config.Key + "','" + config.Value + "')";
            SQLiteCommand command = new SQLiteCommand(sql, con);
            command.ExecuteNonQuery();
            con.Close();
        }
 
        //删除账户
        public void DeleteConfig(String key)
        {
            SQLiteConnection con = getConnection();
            con.Open();
            string sql = "delete from config where `key`='" + key + "'";
            SQLiteCommand command = new SQLiteCommand(sql, con);
            command.ExecuteNonQuery();
            con.Close();
        }
 
        //查询账户
        public Config GetConfig(String key)
        {
            SQLiteConnection con = getConnection();
            con.Open();
            string sql = "select * from config where `key`='"+ key + "'";
            SQLiteCommand command = new SQLiteCommand(sql, con);
            SQLiteDataReader reader = command.ExecuteReader();
            Config config = new Config();
            if (reader.Read())
            {
                config.Key = reader["key"].ToString();
                config.Value = reader["value"].ToString();
            }
            else
            {
                config = null;
            }
            reader.Close();
            con.Close();
            return config;
        }
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
    }
}