-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathauto_export_mysql_report.py
More file actions
5357 lines (5201 loc) · 113 KB
/
Copy pathauto_export_mysql_report.py
File metadata and controls
5357 lines (5201 loc) · 113 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
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#!/usr/bin/env python
# !-*-coding:utf-8 -*-
# !@Date : 2018/12/17 0017 上午 10:12
# !@Author : Damon.guo
# !@File : export_mysql_report.py
import sys
# sys.setdefaultencoding('utf8')
# sys.path.append("D:\Python27\Lib\site-packages")
# sys.path.append("d:\python27\lib")
# import pymysql
# import PyMySQL
import MySQLdb
import pandas as pd
import datetime
import time
import os
import smtplib
from email.mime.multipart import MIMEMultipart
from email.mime.text import MIMEText
from email.mime.application import MIMEApplication
import logging
import calendar
import xlsxwriter
host = '10.0.0.100'
# user = 'KILIMALL'
user = "root"
dbpass = "123456"
port = 3306
"""通过传入的任务类型 ,自动执行指定sql语句,将结果保存为execl表格自动发送邮件到邮件列表的人"""
# 检查文件是否存在
def fileExists(filePath):
if not os.path.exists(filePath):
print "文件:%s 不存在,请检查" % filePath
return False
return True
def datedeltatime(days):
now = datetime.datetime.now()
daydelta = datetime.timedelta(days=days)
enddaydelta = datetime.timedelta(days=1)
print "daydelat", daydelta
days_ago = now - daydelta
onedays_ago = now - enddaydelta
print "daysago", days_ago
startdate = days_ago.strftime('%Y-%m-%d')
enddate = onedays_ago.strftime('%Y-%m-%d')
print startdate, enddate
return startdate, enddate
# print time.strftime("%Y-%m-%d", days_ago)
#
# if datetime.datetime.fromtimestamp(mtime_s) <= days_ago:
# print "sss"
# return True
# else:
# return False
def getlastweek():
sevenday = datetime.timedelta(days=7)
oneday = datetime.timedelta(days=1)
today = datetime.datetime.now()
lastweektoday = today - sevenday
print "lastweektoday", lastweektoday.strftime('%Y-%m-%d')
lastMonday = lastweektoday
lastSunday = lastweektoday
# 取上上周星期一时间
while lastMonday.weekday() != calendar.MONDAY:
lastMonday -= oneday
lastMonday = lastMonday.strftime('%Y-%m-%d')
# 取上上周星期天时间
while lastSunday.weekday() != calendar.SUNDAY:
lastSunday += oneday
lastSunday = lastSunday.strftime('%Y-%m-%d')
print "lastweekdate:%s %s" % (lastMonday, lastSunday)
# 返回 上个星期一和星期天的具体日期
return lastMonday, lastSunday
def getLastDayOfLastMonth():
from datetime import datetime
d = datetime.now()
c = calendar.Calendar()
year = d.year
month = d.month
if month == 1:
month = 12
year -= 1
else:
month -= 1
days = calendar.monthrange(year, month)[1]
"""返回两个整数组成的元组,第一个是该月的第一天是星期几,第二个是该月的天数。(calendar.monthrange(year, month):
Returns weekday of first day of the month and number of days in month, for the specified year and month.——Python文档)
ps:此处计算星期几是按照星期一为0计算。"""
starlastMonth = (datetime(year, month, 1)).strftime('%Y-%m-%d')
endlastMonth = (datetime(year, month, days)).strftime('%Y-%m-%d')
print "lastMonth-startdata:%s" % starlastMonth
print "lastMonth-enddate:%s" % endlastMonth
return starlastMonth, endlastMonth
def getLastDayOfLasttwoMonth():
# 罚金 要退后两个月
from datetime import datetime
d = datetime.now()
c = calendar.Calendar()
year = d.year
month = d.month
# month = 2
if month == 1:
month = 11
year -= 1
elif month == 2:
month = 12
year -= 1
else:
# month = 12
month -= 2
days = calendar.monthrange(year, month)[1]
# print calendar.monthrange(year, month)
"""返回两个整数组成的元组,第一个是该月的第一天是星期几,第二个是该月的天数。(calendar.monthrange(year, month):
Returns weekday of first day of the month and number of days in month, for the specified year and month.——Python文档)
ps:此处计算星期几是按照星期一为0计算。"""
starlastMonth = (datetime(year, month, 1)).strftime('%Y-%m-%d')
endlastMonth = (datetime(year, month, days)).strftime('%Y-%m-%d')
print "lasttwoMonth-startdata:%s" % starlastMonth
print "lasttwoMonth-enddate:%s" % endlastMonth
return starlastMonth, endlastMonth
def TimeStampToTime(timestamp):
# 时间戳转换为时间
timeStruct = time.localtime(timestamp)
return time.strftime('%Y-%m-%d %H:%M:%S', timeStruct)
# 返回时间戳
def getTimeStamp(filePath):
filePath = unicode(filePath, 'utf8')
t = os.path.getmtime(filePath)
# return t
return TimeStampToTime(t)
def connMysql(dbname):
# 建立数据库连接
try:
conn = MySQLdb.connect(host=host, user=user, passwd=dbpass, db=dbname, port=port, charset='utf8')
except Exception, e:
print e
sys.exit()
cur = conn.cursor()
return conn, cur
def execMysql(cursor, mysqlstr):
# 获取游数据库标.
cursor.execute('SET time_zone = "+3:00"')
cursor.execute(mysqlstr)
res = cursor.fetchall()
return res
def getYesterday():
today = datetime.date.today()
oneday = datetime.timedelta(days=1)
yesterday = today - oneday
return yesterday
def sendMail(fileName, receiverlist):
username = 'data_send@kilimall.com'
password = '8Y9keikOOmWaaR9LA38d'
sender = username
# today = str(getYesterday())
# today = str(datetime.date.today())
today = datetime.datetime.now()
today = today.strftime('%Y-%m-%d')
# 如名字所示: Multipart就是多个部分
msg = MIMEMultipart()
msg['Subject'] = "%s-DATA FOR EXECL" % today
msg['From'] = sender
# msg['To'] = receivers
# 下面是文字部分,也就是纯文本
strtext = """Hi,All, this mail from program auto to send!attachment file , kindly check it, thanks!
this mail is right for month data
"""
puretext = MIMEText(strtext)
msg.attach(puretext)
print("email content already")
# 下面是附件部分 ,这里分为了好几个类型
# 首先是xlsx类型的附件,全部销售数据
for file in fileName:
# 附件名称处理去掉全路径
# filename = file.split("\\")[-1]
filename = file.split("/")[-1]
xlsxpart = MIMEApplication(open(file, 'rb').read())
# xlsxpart.add_header('Content-Disposition', 'attachment', filename=today + "stock.csv")
xlsxpart.add_header('Content-Disposition', 'attachment', filename=filename)
msg.attach(xlsxpart)
print("first attachment file already")
try:
# print("开始连接邮件服务器了")
client = smtplib.SMTP()
client.connect('imap.exmail.qq.com')
client.login(username, password)
# client.sendmail(sender, ['garcia.li@kilimall.com'], msg.as_string())
# client.sendmail(sender, ['danni.wang@kilimall.com', 'vina.tang@kilimall.com', 'eason.yi@kilimall.com',
# 'garcia.li@kilimall.com', 'daisy.zeng@kilimall.com', 'jimmyscm@kilimall.com',
# 'lixia@kilimall.com', 'victor.ma@kilimall.com', 'sophia.li@kilimall.com'],
# msg.as_string())
#
# client.sendmail(sender, ['516605659@qq.com', "damon.guo@kilimall.com"], msg.as_string())
client.sendmail(sender, receiverlist, msg.as_string())
client.quit()
print('today data already send.')
except smtplib.SMTPRecipientsRefused:
print('Recipient refused')
except smtplib.SMTPAuthenticationError:
print('Auth error')
except smtplib.SMTPSenderRefused:
print('Sender refused')
except Exception as e:
print(e)
def readMysql_fromFile(fileNAME):
with open(fileNAME, "rb") as fd:
sql = fd.read()
# sql = sql.replace("\r\n", "").replace("\n", "")
sql = sql.replace("\r", " ").replace("\n", " ")
return sql
def listdir(path):
listdir = os.listdir(path)
sqllist = []
sqlDict = {}
for i in listdir:
if i.startswith("shuoming") or i.endswith(".csv") or i.endswith(".xls"):
continue
filname = i.split(".")[0]
file = os.path.join(path, i)
sqlDict[filname] = readMysql_fromFile(file)
# sqllist.append(readMysql_fromFile(file))
return sqlDict
def sub_main(dbname, filename, sql):
# 执行sql 导出导文件
today = str(datetime.date.today())
conn, cur = connMysql(dbname)
res = execMysql(cur, sql)
result_list = list(res)
# 设设置 数据格式str防止在execl 长的int 为乱码
result = pd.DataFrame(result_list, dtype=str)
# print cur.description
# print result
if not result.empty:
result.columns = [filed[0] for filed in cur.description] # 列表生成式,所有字段 表头
# export_path_file = os.path.join(exportpath, "%s-%s.xls") % (today, filename)
# result.to_csv(export_tpath_file, index=False, encoding="utf-8", float_format="%.2f")
# result.to_excel(filename, index=False, encoding="utf-8")
result.to_excel(filename, index=False, engine="xlsxwriter", encoding="utf-8")
# result.to_csv(filename, index=False, encoding="utf-8")
# result.to_xlss
conn.close()
return filename
def main(path, jobtype):
"""还可以继续优化,根据jobtype 对传入的sql语句中的时间赋值 ,不需要一个语句根据day week 定义多个sql"""
# if jobtype == "week":
# start = startweekday
# end = endweekday
# elif jobtype =="day":
# start = Yesterday
# end = Yesterday
#
startweekday, endweekday = getlastweek()
stardmonthday, endmonthday = getLastDayOfLastMonth()
stardtwomonthday, endtwomonthday = getLastDayOfLasttwoMonth()
today = datetime.datetime.now()
monthstarday = today.strftime("%Y-%m-01")
today = today.strftime("%Y-%m-%d")
Yesterday = getYesterday()
Yesterday = Yesterday.strftime("%Y-%m-%d")
print "销量日期区间,%s - %s" % (monthstarday, Yesterday)
# print Yesterday
# sys.exit()
# 定义的周执行的sql脚本
dingdanwangcheng_week = """SELECT
CONCAT(',',o.order_sn),
IFNULL(
oc.voucher_price / o.goods_amount * g.goods_liquidate_amount,
0
) AS voucher,
oc.`voucher_type`,
o.`order_amount`,
o.`store_id`,
o.`store_name`,
FROM_UNIXTIME(o.`finnshed_time`) AS finnshed_time,
FROM_UNIXTIME(o.`add_time`) AS add_time,
o.`logistics_type`,
g.`goods_id`,
g.`goods_name`,
(g.`goods_liquidate_amount`/g.`goods_num`)AS goods_price,
g.`goods_num`,
g.`goods_type`,
g.`gc_id`,
FROM_UNIXTIME(o.`payment_time`) AS payment_time,
IFNULL(
g.goods_liquidate_amount / o.goods_amount * o.`shipping_fee`,
0
) AS shipping_fee,
oc.cash_rewards,
( so.`commission_amount` + so.`tech_fee` ) AS goods_commission
FROM
nc_order_goods g
LEFT JOIN nc_order o
ON g.`order_id` = o.`order_id`
LEFT JOIN nc_order_common oc
ON o.`order_id` = oc.`order_id`
LEFT JOIN nc_shop_orders so
ON (g.`order_id` = so.`order_id` and g.`goods_id` = so.`goods_id` )
WHERE o.finnshed_time >= UNIX_TIMESTAMP('%s 00:00:00')
AND o.finnshed_time <= UNIX_TIMESTAMP('%s 23:59:59');""" % (startweekday, endweekday)
tuihuanhuo_week = """SELECT
CONCAT(',',o.order_sn),
IFNULL(
oc.voucher_price / o.goods_amount * g.goods_liquidate_amount,
0
) AS voucher,
oc.`voucher_type`,
o.`order_amount`,
o.`store_id`,
o.`store_name`,
FROM_UNIXTIME(o.`finnshed_time`) AS finnshed_time,
FROM_UNIXTIME(o.`add_time`) AS add_time,
o.`logistics_type`,
g.`goods_id`,
g.`goods_name`,
(rc.`refund_amount`/g.`goods_num`)AS goods_price,
g.`goods_num`,
g.`goods_type`,
g.`gc_id`,
FROM_UNIXTIME(o.`payment_time`) AS payment_time,
IFNULL(
g.goods_liquidate_amount / o.goods_amount * o.`shipping_fee`,
0
) AS shipping_fee,
rc.type,
rf.`cash_rewards`
FROM
nc_order_goods g
LEFT JOIN nc_order o
ON g.`order_id` = o.`order_id`
LEFT JOIN nc_order_common oc
ON o.`order_id` = oc.`order_id`
LEFT JOIN nc_returns rc
ON rc.`order_id` = o.`order_id`
LEFT JOIN nc_return_refunds rf
ON rc.`id` = rf.return_id
WHERE rc.id IN (SELECT id FROM nc_returns WHERE `status` = 4) AND rc.`type` IN (1,2) AND rc.updated_at >= UNIX_TIMESTAMP('%s 00:00:00') AND rc.updated_at <= UNIX_TIMESTAMP('%s 23:59:59')
AND g.goods_id = rc.`goods_id` AND g.`order_id` = rc.`order_id`;""" % (startweekday, endweekday)
fajing_week = """SELECT
CONCAT(',',order_sn),
o.store_id,store_name,order_amount, FROM_UNIXTIME(payment_time) AS payment_time,order_state,
IF(bad_order_type = 1,FROM_UNIXTIME(o.`bi_updated_time`),0) AS cancel_time,
IF(bad_order_type = 2 AND c.`transfer_warehouse_time` != 0,FROM_UNIXTIME(c.`transfer_warehouse_time`),0) AS transfer_warehouse_time,
IF(bad_order_type = 2 AND c.`transfer_warehouse_time` != 0,(c.`transfer_warehouse_time` - o.`payment_time`)/(3600*24),0) AS spend_time,
(
IF(
bad_order_type = 2,
3,
IF(bad_order_type = 1, 2, 0)
)
) AS Fine
FROM
nc_order o LEFT JOIN nc_order_common c ON o.`order_id` = c.`order_id`
WHERE bad_order_type IN (1, 2)
AND payment_time > UNIX_TIMESTAMP('%s 00:00:00')
AND payment_time <= UNIX_TIMESTAMP('%s 23:59:59') ;""" % (startweekday, endweekday)
# 定义的月执行脚本
dingdanwangcheng_month = """
SELECT
CONCAT(',',o.order_sn),
IFNULL(
oc.voucher_price / o.goods_amount * g.goods_liquidate_amount,
0
) AS voucher,
oc.`voucher_type`,
o.`order_amount`,
o.`store_id`,
o.`store_name`,
FROM_UNIXTIME(o.`finnshed_time`) AS finnshed_time,
FROM_UNIXTIME(o.`add_time`) AS add_time,
o.`logistics_type`,
g.`goods_id`,
g.`goods_name`,
(g.`goods_liquidate_amount`/g.`goods_num`)AS goods_price,
g.`goods_num`,
g.`goods_type`,
g.`gc_id`,
FROM_UNIXTIME(o.`payment_time`) AS payment_time,
IFNULL(
g.goods_liquidate_amount / o.goods_amount * o.`shipping_fee`,
0
) AS shipping_fee,
oc.cash_rewards,
( so.`commission_amount` + so.`tech_fee` ) AS goods_commission
FROM
nc_order_goods g
LEFT JOIN nc_order o
ON g.`order_id` = o.`order_id`
LEFT JOIN nc_order_common oc
ON o.`order_id` = oc.`order_id`
LEFT JOIN nc_shop_orders so
ON (g.`order_id` = so.`order_id` and g.`goods_id` = so.`goods_id` )
WHERE o.finnshed_time >= UNIX_TIMESTAMP('%s 00:00:00')
AND o.finnshed_time <= UNIX_TIMESTAMP('%s 23:59:59'); """ % (stardmonthday, endmonthday)
tuihuanhuo_month = """SELECT
CONCAT(',',o.order_sn),
IFNULL(
oc.voucher_price / o.goods_amount * g.goods_liquidate_amount,
0
) AS voucher,
oc.`voucher_type`,
o.`order_amount`,
o.`store_id`,
o.`store_name`,
FROM_UNIXTIME(o.`finnshed_time`) AS finnshed_time,
FROM_UNIXTIME(o.`add_time`) AS add_time,
o.`logistics_type`,
g.`goods_id`,
g.`goods_name`,
(rc.`refund_amount`/g.`goods_num`)AS goods_price,
g.`goods_num`,
g.`goods_type`,
g.`gc_id`,
FROM_UNIXTIME(o.`payment_time`) AS payment_time,
IFNULL(
g.goods_liquidate_amount / o.goods_amount * o.`shipping_fee`,
0
) AS shipping_fee,
rc.type,
rf.`cash_rewards`
FROM
nc_order_goods g
LEFT JOIN nc_order o
ON g.`order_id` = o.`order_id`
LEFT JOIN nc_order_common oc
ON o.`order_id` = oc.`order_id`
LEFT JOIN nc_returns rc
ON rc.`order_id` = o.`order_id`
LEFT JOIN nc_return_refunds rf
ON rc.`id` = rf.return_id
WHERE rc.id IN (SELECT id FROM nc_returns WHERE `status` = 4) AND rc.`type` IN (1,2) AND rc.updated_at >= UNIX_TIMESTAMP('%s 00:00:00') AND rc.updated_at <= UNIX_TIMESTAMP('%s 23:59:59')
AND g.goods_id = rc.`goods_id` AND g.`order_id` = rc.`order_id`;""" % (stardmonthday, endmonthday)
fajing_month = """SELECT
CONCAT(',',order_sn),o.store_id,store_name,order_amount, FROM_UNIXTIME(payment_time) AS payment_time,order_state,
IF(bad_order_type = 1,FROM_UNIXTIME(o.`bi_updated_time`),0) AS cancel_time,
IF(bad_order_type = 2 AND c.`transfer_warehouse_time` != 0,FROM_UNIXTIME(c.`transfer_warehouse_time`),0) AS transfer_warehouse_time,
IF(bad_order_type = 2 AND c.`transfer_warehouse_time` != 0,(c.`transfer_warehouse_time` - o.`payment_time`)/(3600*24),0) AS spend_time,
(
IF(
bad_order_type = 2,
3,
IF(bad_order_type = 1, 2, 0)
)
) AS Fine
FROM
nc_order o LEFT JOIN nc_order_common c ON o.`order_id` = c.`order_id`
WHERE bad_order_type IN (1, 2)
AND payment_time > UNIX_TIMESTAMP('%s 00:00:00')
AND payment_time <= UNIX_TIMESTAMP('%s 23:59:59') ;""" % (stardtwomonthday, endtwomonthday)
xiaoliang1 = """SELECT
ooo.goods_commonid,
ooo.goods_id,
ooo.goods_name,
ooo.goods_storage,
ooo.solditem,
ooo.store_id,
ooo.store_name,
ooo.oaddtime,
CASE ooo.is_global WHEN 1 THEN 'global'
ELSE 'local' END AS storetype,
ooo.timess,
gc.gc_name,
gc1.gc_name,
gc2.gc_name,
ooo.paymenttime,
ooo.leixing,
ooo.goods_price
FROM
(
SELECT
gs.goods_commonid,
og.goods_id,
og.store_id,
o.store_name,
gs.gc_id_1,
gs.gc_id_2,
gs.gc_id_3,
og.goods_name,
og.goods_price,
store.is_global,
gs.goods_storage,
FROM_UNIXTIME(gs.goods_addtime) AS timess,
FROM_UNIXTIME(o.add_time) AS oaddtime,
CASE o.payment_time
WHEN 0 THEN
0
ELSE
FROM_UNIXTIME(o.payment_time, '%%Y%%m%%d')
END AS paymenttime,
CASE o.logistics_type WHEN 1 THEN 'FBK'
WHEN 2 THEN 'GS'
WHEN 0 THEN 'DS'
END AS leixing,
SUM(og.goods_num) AS solditem
FROM
nc_order_goods og
INNER JOIN nc_order o ON og.order_id = o.order_id
INNER JOIN nc_goods gs ON og.goods_id = gs.goods_id
INNER JOIN nc_store store ON og.store_id=store.store_id
WHERE
FROM_UNIXTIME(o.add_time, '%%Y%%m%%d') =%s
OR FROM_UNIXTIME(o.payment_time, '%%Y%%m%%d') =%s
GROUP BY
gs.goods_commonid,
og.goods_id,
og.store_id,
o.store_name,
gs.gc_id_1,
gs.gc_id_2,
gs.gc_id_3,
og.goods_name,
og.goods_price, store.is_global,
gs.goods_storage,timess,paymenttime,leixing
) AS ooo
INNER JOIN nc_goods_class gc ON ooo.gc_id_1 = gc.gc_id
INNER JOIN nc_goods_class gc1 ON ooo.gc_id_2 = gc1.gc_id
INNER JOIN nc_goods_class gc2 ON ooo.gc_id_3 = gc2.gc_id""" % (monthstarday, Yesterday)
xiaoliang_day = """SELECT
ooo.goods_commonid,
ooo.goods_id,
ooo.goods_name,
ooo.goods_storage,
ooo.solditem,
ooo.store_id,
ooo.store_name,
CASE ooo.is_global WHEN 1 THEN 'global'
ELSE 'local' END AS storetype,
ooo.timess,
gc.gc_name,
gc1.gc_name,
gc2.gc_name,
ooo.oaddtime,
ooo.paymenttime,
ooo.leixing,
ooo.goods_price
FROM
(
SELECT
gs.goods_commonid,
og.goods_id,
og.store_id,
o.store_name,
gs.gc_id_1,
gs.gc_id_2,
gs.gc_id_3,
og.goods_name,
og.goods_price,
store.is_global,
gs.goods_storage,
FROM_UNIXTIME(gs.goods_addtime) AS timess,
FROM_UNIXTIME(o.add_time, '%%Y%%m%%d') AS oaddtime,
CASE o.payment_time
WHEN 0 THEN
0
ELSE
FROM_UNIXTIME(o.payment_time, '%%Y%%m%%d')
END AS paymenttime,
CASE o.logistics_type WHEN 1 THEN 'FBK'
WHEN 2 THEN 'GS'
WHEN 0 THEN 'DS'
END AS leixing,
SUM(og.goods_num) AS solditem
FROM
nc_order_goods og
INNER JOIN nc_order o ON og.order_id = o.order_id
INNER JOIN nc_goods gs ON og.goods_id = gs.goods_id
INNER JOIN nc_store store ON og.store_id=store.store_id
WHERE
o.add_time BETWEEN UNIX_TIMESTAMP('%s 00:00:00') AND UNIX_TIMESTAMP('%s 23:59:59')
OR (o.payment_time BETWEEN UNIX_TIMESTAMP('%s 00:00:00') AND UNIX_TIMESTAMP('%s 23:59:59'))
GROUP BY
gs.goods_commonid,
og.goods_id,
og.store_id,
o.store_name,
gs.gc_id_1,
gs.gc_id_2,
gs.gc_id_3,
og.goods_name,
og.goods_price, store.is_global,
gs.goods_storage,timess,paymenttime,leixing
) AS ooo
INNER JOIN nc_goods_class gc ON ooo.gc_id_1 = gc.gc_id
INNER JOIN nc_goods_class gc1 ON ooo.gc_id_2 = gc1.gc_id
INNER JOIN nc_goods_class gc2 ON ooo.gc_id_3 = gc2.gc_id""" % (Yesterday, Yesterday, Yesterday, Yesterday)
xiaoliang_week = """SELECT
ooo.goods_commonid,
ooo.goods_id,
ooo.goods_name,
ooo.goods_storage,
ooo.solditem,
ooo.store_id,
ooo.store_name,
CASE ooo.is_global WHEN 1 THEN 'global'
ELSE 'local' END AS storetype,
ooo.timess,
gc.gc_name,
gc1.gc_name,
gc2.gc_name,
ooo.oaddtime,
ooo.paymenttime,
ooo.leixing,
ooo.goods_price
FROM
(
SELECT
gs.goods_commonid,
og.goods_id,
og.store_id,
o.store_name,
gs.gc_id_1,
gs.gc_id_2,
gs.gc_id_3,
og.goods_name,
og.goods_price,
store.is_global,
gs.goods_storage,
FROM_UNIXTIME(gs.goods_addtime) AS timess,
FROM_UNIXTIME(o.add_time, '%%Y%%m%%d') AS oaddtime,
CASE o.payment_time
WHEN 0 THEN
0
ELSE
FROM_UNIXTIME(o.payment_time, '%%Y%%m%%d')
END AS paymenttime,
CASE o.logistics_type WHEN 1 THEN 'FBK'
WHEN 2 THEN 'GS'
WHEN 0 THEN 'DS'
END AS leixing,
SUM(og.goods_num) AS solditem
FROM
nc_order_goods og
INNER JOIN nc_order o ON og.order_id = o.order_id
INNER JOIN nc_goods gs ON og.goods_id = gs.goods_id
INNER JOIN nc_store store ON og.store_id=store.store_id
WHERE
o.add_time BETWEEN UNIX_TIMESTAMP('%s 00:00:00') AND UNIX_TIMESTAMP('%s 23:59:59')
OR (o.payment_time BETWEEN UNIX_TIMESTAMP('%s 00:00:00') AND UNIX_TIMESTAMP('%s 23:59:59'))
GROUP BY
gs.goods_commonid,
og.goods_id,
og.store_id,
o.store_name,
gs.gc_id_1,
gs.gc_id_2,
gs.gc_id_3,
og.goods_name,
og.goods_price, store.is_global,
gs.goods_storage,timess,paymenttime,leixing
) AS ooo
INNER JOIN nc_goods_class gc ON ooo.gc_id_1 = gc.gc_id
INNER JOIN nc_goods_class gc1 ON ooo.gc_id_2 = gc1.gc_id
INNER JOIN nc_goods_class gc2 ON ooo.gc_id_3 = gc2.gc_id;""" % (startweekday, endweekday, startweekday, endweekday)
xiaoliang_month = """SELECT
ooo.goods_commonid,
ooo.goods_id,
ooo.goods_name,
ooo.goods_storage,
ooo.solditem,
ooo.store_id,
ooo.store_name,
CASE ooo.is_global WHEN 1 THEN 'global'
ELSE 'local' END AS storetype,
ooo.timess,
gc.gc_name,
gc1.gc_name,
gc2.gc_name,
ooo.oaddtime,
ooo.paymenttime,
ooo.leixing,
ooo.goods_price
FROM
(
SELECT
gs.goods_commonid,
og.goods_id,
og.store_id,
o.store_name,
gs.gc_id_1,
gs.gc_id_2,
gs.gc_id_3,
og.goods_name,
og.goods_price,
store.is_global,
gs.goods_storage,
FROM_UNIXTIME(gs.goods_addtime) AS timess,
FROM_UNIXTIME(o.add_time, '%%Y%%m%%d') AS oaddtime,
CASE o.payment_time
WHEN 0 THEN
0
ELSE
FROM_UNIXTIME(o.payment_time, '%%Y%%m%%d')
END AS paymenttime,
CASE o.logistics_type WHEN 1 THEN 'FBK'
WHEN 2 THEN 'GS'
WHEN 0 THEN 'DS'
END AS leixing,
SUM(og.goods_num) AS solditem
FROM
nc_order_goods og
INNER JOIN nc_order o ON og.order_id = o.order_id
INNER JOIN nc_goods gs ON og.goods_id = gs.goods_id
INNER JOIN nc_store store ON og.store_id=store.store_id
WHERE
o.add_time BETWEEN UNIX_TIMESTAMP('%s 00:00:00') AND UNIX_TIMESTAMP('%s 23:59:59')
OR (o.payment_time BETWEEN UNIX_TIMESTAMP('%s 00:00:00') AND UNIX_TIMESTAMP('%s 23:59:59'))
GROUP BY
gs.goods_commonid,
og.goods_id,
og.store_id,
o.store_name,
gs.gc_id_1,
gs.gc_id_2,
gs.gc_id_3,
og.goods_name,
og.goods_price, store.is_global,
gs.goods_storage,timess,paymenttime,leixing
) AS ooo
INNER JOIN nc_goods_class gc ON ooo.gc_id_1 = gc.gc_id
INNER JOIN nc_goods_class gc1 ON ooo.gc_id_2 = gc1.gc_id
INNER JOIN nc_goods_class gc2 ON ooo.gc_id_3 = gc2.gc_id;""" % (
stardmonthday, endmonthday, stardmonthday, endmonthday)
# 自营销售数据,所有的店铺和单独个人管理数据,按每天,每周,每月定时一起发送到日 周 月邮件列表
ziyingxiaoshou_day = """SELECT
CONCAT(',',t.order_sn),
t.voucher,
t.voucher_type,
t.order_amount,
t.manjian,
t.store_id,
t.store_name,
t.finnshed_time,
t.add_time,
t.logistics_type,
t.goods_id,
t.goods_name,
t.is_global,
IF (
t.voucher_type = 0,
t.goods_price + t.manjian - t.voucher,
t.goods_price + t.manjian
) goods_price,
t.commis_rate,
t.goods_num,
t.goods_type,
t.gc_id,
t.refund_id,
t.goods_storage,
t.payment_time,
t.goods_liquidate_price,
t.goods_liquidate_amount,
t.dmember_mobile,
t.dmember_name,
t.goods_serial,
t.order_amount
FROM
(
SELECT
a.order_sn,
IFNULL(
common.voucher_price / (a.goods_amount) * goods.goods_price,
0
) AS voucher,
common.voucher_type,
a.order_amount,
IFNULL(
(
a.order_amount - a.goods_amount - a.shipping_fee
) / (a.goods_amount) * goods.goods_price,
0
) AS manjian,
a.store_id,
s.store_name,
s.is_global,
FROM_UNIXTIME(a.finnshed_time) AS finnshed_time,
FROM_UNIXTIME(a.add_time) AS add_time,
a.logistics_type,
goods.goods_id,
goods.goods_name,
goods.goods_price,
goods.commis_rate,
goods.goods_num,
goods.goods_type,
goods.gc_id,
gs.goods_storage,
n.refund_id,
FROM_UNIXTIME(a.payment_time) payment_time,
goods.goods_liquidate_amount / goods_num AS goods_liquidate_price,
goods.goods_liquidate_amount,
kili.dmember_name,
kili.dmember_mobile,
commons.goods_serial
FROM
nc_order a
LEFT JOIN nc_order_goods goods ON goods.order_id = a.order_id
LEFT JOIN nc_order_common common ON common.order_id = a.order_id
LEFT JOIN nc_refund_return_new n ON n.order_id = a.order_id
LEFT JOIN nc_store s ON s.store_id = a.store_id
LEFT JOIN nc_delivery_kilimall kili ON a.order_id=kili.order_id
LEFT JOIN nc_goods gs ON gs.goods_id=goods.goods_id
LEFT JOIN nc_goods_common commons ON gs.goods_commonid=commons.goods_commonid
WHERE
a.add_time BETWEEN UNIX_TIMESTAMP('%s 00:00:00') AND UNIX_TIMESTAMP('%s 23:59:59')
AND a.store_id IN (2706,
2606,
2189,
829,
2195,
2475,
2499,
2476,
2498,
2905,
3087,
653,
3036,
3271,
3272,
2272,
3406,
3906,
2757,
4429,
2518,
1,
2391,
2977,
3007,
3248,
3257,
3243,
3479,
3972,
3258,
3251,
2628,
2610,
2609,
2607,
2617,
3192,
3191,
3187,
3190,
3186,
3182,
3189,
3185,
3181,
3184,
3255,
3478,
3485,
3490,
3499,
3511,
3554,
3507,
1312,
3730,
4436,
2336,
95,
2877,
2415,
2526,
2393,
3482,
3487,
3729,
3492,
3601,
3515,
3571,
3541,
3578,
2096,
2819,
2546,
2547,
3090,
2482,
2886,
3250,
3260,
3256,
3254,
3242,
3249,
3246,
3244,
3259,
3252,
3408,
3253,
2622,
2620,
2619,
2642,
2618,
2629,
2655,
2648,
2632,
2633,
2649,
2654,
2646,
2611,
2613,
2612,
2621,
2608,
2631,
2651,
2616,
2650,
2615,
2624,
2638,
2652,
2645,
2653,
2625,
2639,
2635,