pg_app_db_func.py
11.2 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
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
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
#! /usr/bin/env python
# -*- coding: utf-8 -*-
# Date: 2020/7/12 16:27
# @Author:bxh
# @file: pg_db_func.py
import psycopg2
from datetime import date, timedelta
import datetime,time
def get_today():
now_today = datetime.datetime.now()
str_time = now_today.strftime("%Y-%m-%d")
return str_time
def get_tomorrow():
tomorrow = (date.today() + timedelta(days=1)).strftime("%Y-%m-%d")
return tomorrow
def getMonth():
year = str(datetime.date.today().year)
d = datetime.date.today()
month = '%02d' % d.month
if month in ['01', '03', '05', '07', '08', '10', '12']:
s = str("-01 00:00:00")
e = str("-31 23:59:59")
startTime = year + "-" + str(month) + s
endTime = year + "-" + str(month) + e
list = [startTime, endTime]
return list
elif month == '02':
s = str("-01 00:00:00")
e = str("-28 23:59:59")
startTime = year + "-" + str(month) + s
endTime = year + "-" + str(month) + e
list = [startTime, endTime]
return list
else:
s = str("-01 00:00:00")
e = str("-30 23:59:59")
startTime = year + "-" + str(month) + s
endTime = year + "-" + str(month) + e
list = [startTime, endTime]
return list
list_time = getMonth()
def get_starTimestamp():
return list_time[0]
def get_endTimestamp():
return list_time[1]
# 获得连接
def GetConnect():
conn = False
try:
conn = psycopg2.connect(database="partner_test",
user="bianxinhua_user",
password="Bianxinhua_user#123",
host="101.37.68.23",
port="15432")
except Exception as err:
print("连接数据库失败,%s" % err)
return conn
#获取超级管理员id
def get_partner_id_from_database():
db = GetConnect()
cursor = db.cursor()
# sql= "select id from " + '''"''' + tbl + '''"'''+ ' ' + 'order by id desc '
sql = '''select id from partner_info where account = '13459147023' '''
# print(sql)
try:
cursor.execute(sql)
result = cursor.fetchone()
return result[0]
except Exception as err:
# 发生错误时回滚
print(err)
db.rollback()
db.close()
# print(get_partner_id_from_database())
def get_partner_name_from_database():
db = GetConnect()
cursor = db.cursor()
# sql= "select id from " + '''"''' + tbl + '''"'''+ ' ' + 'order by id desc '
sql = '''select partner_name from partner_info where account = '13459147023' '''
print(sql)
try:
cursor.execute(sql)
result = cursor.fetchone()
return result[0]
except Exception as err:
# 发生错误时回滚
print(err)
db.rollback()
db.close()
# print(get_partner_name_from_database())
#获取超级管理员账号
def get_partner_account_from_database():
db = GetConnect()
cursor = db.cursor()
# sql= "select id from " + '''"''' + tbl + '''"'''+ ' ' + 'order by id desc '
sql = '''select account from partner_info where account = '13459147023' '''
# print(sql)
try:
cursor.execute(sql)
result = cursor.fetchone()
return result[0]
except Exception as err:
# 发生错误时回滚
print(err)
db.rollback()
db.close()
# print(get_partner_account_from_database())
#获取超级管理员密码
def get_partner_password_from_database(tbl):
db = GetConnect()
cursor = db.cursor()
# sql= "select id from " + '''"''' + tbl + '''"'''+ ' ' + 'order by id desc '
sql = '''select "password" from \"{}\" where account = '13459147023' '''.format(tbl)
# print(sql)
try:
cursor.execute(sql)
result = cursor.fetchone()
return result[0]
except Exception as err:
# 发生错误时回滚
print(err)
db.rollback()
db.close()
#累计实发订单
def get_cumulativeQuantity_from_database():
db = GetConnect()
cursor = db.cursor()
# sql= "select id from " + '''"''' + tbl + '''"'''+ ' ' + 'order by id desc '
sql = '''select COUNT(id) from order_base where partner_id = {} and order_type in (1) '''.format(get_partner_id_from_database())
# print(sql)
try:
cursor.execute(sql)
result = cursor.fetchone()
return result[0]
except Exception as err:
# 发生错误时回滚
print(err)
db.rollback()
db.close()
#累计实发订单金额
def get_cumulativeMoney_from_database():
db = GetConnect()
cursor = db.cursor()
# sql= "select id from " + '''"''' + tbl + '''"'''+ ' ' + 'order by id desc '
sql = '''select sum(plan_order_amount) from order_base where partner_id = {} and order_type in (1) '''.format(get_partner_id_from_database())
# print(sql)
try:
cursor.execute(sql)
result = cursor.fetchone()
if round(result[0], 2) == 0:
return 0
else:
return round(result[0], 2)
except Exception as err:
# 发生错误时回滚
print(err)
db.rollback()
db.close()
# print(get_cumulativeMoney_from_database())
#今日新增实发订单金额
def get_todayRealMoney_from_database():
db = GetConnect()
cursor = db.cursor()
# sql= "select id from " + '''"''' + tbl + '''"'''+ ' ' + 'order by id desc '
sql = '''select COALESCE(sum(plan_order_amount),0) from order_base where create_time between \'{}\' and \'{}\' and order_type in (1) '''.format(get_today(),get_tomorrow())
# print(sql)
try:
cursor.execute(sql)
result = cursor.fetchone()
if round(result[0], 2) == 0:
return 0
else:
return round(result[0], 2)
except Exception as err:
# 发生错误时回滚
print(err)
db.rollback()
db.close()
#今日新增实发订单
def get_todayRealQuantity_from_database():
db = GetConnect()
cursor = db.cursor()
# sql= "select id from " + '''"''' + tbl + '''"'''+ ' ' + 'order by id desc '
sql = '''select COUNT(id) from order_base where create_time between \'{}\' and \'{}\' and order_type in (1) '''.format(get_today(),get_tomorrow())
# print(sql)
try:
cursor.execute(sql)
result = cursor.fetchone()
return int(result[0])
except Exception as err:
# 发生错误时回滚
print(err)
db.rollback()
db.close()
#累计所有年份应收分红
def get_receivable_from_database():
db = GetConnect()
cursor = db.cursor()
# sql= "select id from " + '''"''' + tbl + '''"'''+ ' ' + 'order by id desc '
sql = '''select sum(COALESCE(partner_bonus_has,0)+COALESCE(partner_bonus_not,0)-COALESCE(partner_bonus_expense,0)) from order_base where create_time between \'{}\' and \'{}\' and partner_id = {} and order_type in (1) '''.format('2020-04-1','2021-03-31 23:59:59',get_partner_id_from_database())
# print(sql)
try:
cursor.execute(sql)
result = cursor.fetchone()
if round(result[0], 2) == 0:
return 0
else:
return round(result[0], 2)
except Exception as err:
# 发生错误时回滚
print(err)
db.rollback()
db.close()
#实发订单总数
def get_total_from_database():
db = GetConnect()
cursor = db.cursor()
# sql= "select id from " + '''"''' + tbl + '''"'''+ ' ' + 'order by id desc '
sql = '''select count(id) from order_base where create_time between \'{}\' and \'{}\' and partner_id = {} and order_type in (1) '''.format('2020-04-1','2021-03-31 23:59:59',get_partner_id_from_database())
# print(sql)
try:
cursor.execute(sql)
result = cursor.fetchone()
return result[0]
except Exception as err:
# 发生错误时回滚
print(err)
db.rollback()
db.close()
#累计所有年份未收分红
def get_outstanding_from_database():
db = GetConnect()
cursor = db.cursor()
# sql= "select id from " + '''"''' + tbl + '''"'''+ ' ' + 'order by id desc '
sql = '''select sum(partner_bonus_not) from order_base where partner_id = {} and order_type=1 and create_time between \'{}\' and \'{}\' '''.format(get_partner_id_from_database(),'2020-04-01 00:00:00','2021-03-31 23:59:59')
# print(sql)
try:
cursor.execute(sql)
result = cursor.fetchone()
if round(result[0], 2) == 0:
return 0
else:
return round(result[0],2)
except Exception as err:
# 发生错误时回滚
print(err)
db.rollback()
db.close()
# print(get_outstanding_from_database())
#累计所有年份已收分红
def get_received_from_database():
db = GetConnect()
cursor = db.cursor()
# sql= "select id from " + '''"''' + tbl + '''"'''+ ' ' + 'order by id desc '
sql = '''select sum(partner_bonus_has) from order_base where partner_id = {} and order_type=1 and create_time between \'{}\' and \'{}\' '''.format(get_partner_id_from_database(),'2020-04-01 00:00:00','2021-03-31 23:59:59')
# print(sql)
try:
cursor.execute(sql)
result = cursor.fetchone()
if round(result[0],2) == 0:
return 0
else:
return round(result[0],2)
except Exception as err:
# 发生错误时回滚
print(err)
db.rollback()
db.close()
#已收/未收明细列表--发货单号
def get_detailAction_0_order_code_from_database():
db = GetConnect()
cursor = db.cursor()
# sql= "select id from " + '''"''' + tbl + '''"'''+ ' ' + 'order by id desc '
sql = '''select order_code from order_base where partner_id = {} and order_type=1 and partner_bonus_has > 0 order by id desc '''.format(get_partner_id_from_database())
# print(sql)
try:
cursor.execute(sql)
result = cursor.fetchone()
return result[0]
except Exception as err:
# 发生错误时回滚
print(err)
db.rollback()
db.close()
#已收/未收明细列表--订单金额
def get_detailAction_0_plan_order_amount_from_database():
db = GetConnect()
cursor = db.cursor()
# sql= "select id from " + '''"''' + tbl + '''"'''+ ' ' + 'order by id desc '
sql = '''select plan_order_amount from order_base where partner_id = {} and order_type=1 and partner_bonus_has > 0 order by id desc '''.format(get_partner_id_from_database())
# print(sql)
try:
cursor.execute(sql)
result = cursor.fetchone()
return result[0]
except Exception as err:
# 发生错误时回滚
print(err)
db.rollback()
db.close()
#已收/未收明细列表--应收分红
def get_detailAction_0_use_partner_bonus_amount_from_database():
db = GetConnect()
cursor = db.cursor()
# sql= "select id from " + '''"''' + tbl + '''"'''+ ' ' + 'order by id desc '
sql = '''select use_partner_bonus from order_base where partner_id = {} and order_type=1 and partner_bonus_has > 0 order by id desc '''.format(get_partner_id_from_database())
# print(sql)
try:
cursor.execute(sql)
result = cursor.fetchone()
return result[0]
except Exception as err:
# 发生错误时回滚
print(err)
db.rollback()
db.close()
# print(get_detailAction_0_use_partner_bonus_amount_from_database())