-
Notifications
You must be signed in to change notification settings - Fork 0
/
Dao.py
210 lines (185 loc) · 8.91 KB
/
Dao.py
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
# -*-coding: utf-8-*-
import os
import cx_Oracle as Oci
import pandas as pd
import warnings
from typing import List, Dict
from SingletonInstance import SingletonInstance
warnings.filterwarnings('ignore')
os.environ["NLS_LANG"] = ".AL32UTF8"
os.putenv('NLS_LANG', 'KOREAN_KOREA.KO16MSWIN949')
class Dao(SingletonInstance):
# D/B 접속 정보
HOST = '123.45.678.901'
SID = 'orcl'
ID = 'abcd'
PASSWORD = 'abcd'
# MARKET_CODE = '1005601' # 가락
MARKET_CODE = '1041401' # 대구
TRAIN_START_DATE = '20190501' # Train Data: Start Date
TRAIN_END_DATE = '20200228' # Test Data: End Date
DATE = '20200622' # 임시처리
def __init__(self):
self.conn: Oci.Connection = Oci.connect(self.ID, self.PASSWORD, self.HOST + '/' + self.SID)
# self.market_code = self.get_user_select_market()
# self.market_code = '1005601' # 가락동농수산물시장 Fixme: 임시처리
self.market_code = self.MARKET_CODE # 가락동농수산물시장 Fixme: 임시처리
self.train_start_date = self.TRAIN_START_DATE
self.train_end_date = self.TRAIN_END_DATE
self.date = self.DATE
def get_user_select_market(self):
query = """SELECT WHSAL_MRKT_NEW_CODE
FROM TBL_MRKT_DIST_INFO"""
return pd.read_sql(query, con=self.conn)
# User가 선택한 시장의 데이터를 받아오는 query
def get_data(self) -> pd.DataFrame:
query = """ SELECT DELNG_DE, AUC_SE_NM, INSTT_NEW_NM, STD_PRDLST_NEW_NM, STD_SPCIES_NEW_NM, DELNG_PRUT,
STD_QLITY_NEW_NM, SHIPMNT_SE_NM,STD_MTC_NEW_NM, DELNG_QY, SBID_PRIC
FROM TBL_DATA_MART
WHERE 1=1
AND WHSAL_MRKT_NEW_CODE = '{}' -- Market
AND STD_PRDLST_NEW_CODE = '0908' -- Tomato
AND DELNG_PRUT IN (4,5,10) -- Weight
AND (DELNG_DE >= '{}' AND DELNG_DE <= '{}') -- Date
UNION ALL
SELECT DELNG_DE, AUC_SE_NM, INSTT_NEW_NM, STD_PRDLST_NEW_NM, STD_SPCIES_NEW_NM, DELNG_PRUT,
STD_QLITY_NEW_NM, SHIPMNT_SE_NM,STD_MTC_NEW_NM, DELNG_QY, SBID_PRIC
FROM TBL_DATA_MART
WHERE 1=1
AND WHSAL_MRKT_NEW_CODE = '{}' -- Market
AND STD_PRDLST_NEW_CODE = '911' -- Tomato
AND STD_SPCIES_NEW_CODE <> '091103'
AND DELNG_PRUT IN (2,5) -- Weight
AND (DELNG_DE >= '{}' AND DELNG_DE <= '{}') -- Date
UNION ALL
SELECT DELNG_DE, AUC_SE_NM, INSTT_NEW_NM, STD_PRDLST_NEW_NM, STD_SPCIES_NEW_NM, DELNG_PRUT,
STD_QLITY_NEW_NM, SHIPMNT_SE_NM,STD_MTC_NEW_NM, DELNG_QY, SBID_PRIC
FROM TBL_DATA_MART
WHERE 1=1
AND WHSAL_MRKT_NEW_CODE = '{}' -- Market
AND STD_PRDLST_NEW_CODE = '911' -- Tomato
AND STD_SPCIES_NEW_CODE = '091103'
AND DELNG_PRUT IN (2,3) -- Weight
AND (DELNG_DE >= '{}' AND DELNG_DE <= '{}') -- Date""".format(self.market_code, self.train_start_date, self.train_end_date,
self.market_code, self.train_start_date, self.train_end_date,
self.market_code, self.train_start_date, self.train_end_date)
return pd.read_sql(query, con=self.conn)
def get_prdt_data(self) -> pd.DataFrame:
query = """SELECT DATE_INFO
, STD_MTC_NEW_CODE --산지코드
, STD_PRDLST_NEW_CODE -- 품목
, STD_SPCIES_NEW_CODE -- 품종
, DELNG_PRUT --단량
, STD_QLITY_NEW_CODE -- 등급
, SHIPMNT_SE_CODE -- 출하구분
, SUM(ORDER_QUANTITY) AS ORDER_QUANTITY -- 입고량
FROM TBL_PRDT_INFO
WHERE 1=1
AND STD_PRDLST_NEW_CODE = '803'
AND DELNG_PRUT IN (4,5,10)
AND SHIPMNT_SE_CODE IS NOT NULL
GROUP BY DATE_INFO
, STD_MTC_NEW_CODE
, STD_PRDLST_NEW_CODE
, STD_SPCIES_NEW_CODE
, DELNG_PRUT
, STD_QLITY_NEW_CODE
, SHIPMNT_SE_CODE
UNION ALL
SELECT DATE_INFO
, STD_MTC_NEW_CODE --산지코드
, STD_PRDLST_NEW_CODE -- 품목
, STD_SPCIES_NEW_CODE -- 품종
, DELNG_PRUT --단량
, STD_QLITY_NEW_CODE -- 등급
, SHIPMNT_SE_CODE -- 출하구분
, SUM(ORDER_QUANTITY) AS ORDER_QUANTITY -- 입고량
FROM TBL_PRDT_INFO
WHERE 1=1
AND STD_PRDLST_NEW_CODE = '806'
AND STD_SPCIES_NEW_CODE <> '080603'
AND DELNG_PRUT IN (2, 5)
AND SHIPMNT_SE_CODE IS NOT NULL
GROUP BY DATE_INFO
, STD_MTC_NEW_CODE
, STD_PRDLST_NEW_CODE
, STD_SPCIES_NEW_CODE
, DELNG_PRUT
, STD_QLITY_NEW_CODE
, SHIPMNT_SE_CODE
UNION ALL
SELECT DATE_INFO
, STD_MTC_NEW_CODE --산지코드
, STD_PRDLST_NEW_CODE -- 품목
, STD_SPCIES_NEW_CODE -- 품종
, DELNG_PRUT --단량
, STD_QLITY_NEW_CODE -- 등급
, SHIPMNT_SE_CODE -- 출하구분
, SUM(ORDER_QUANTITY) AS ORDER_QUANTITY -- 입고량
FROM TBL_PRDT_INFO
WHERE 1=1
AND STD_PRDLST_NEW_CODE = '806'
AND STD_SPCIES_NEW_CODE = '080603'
AND DELNG_PRUT IN (2, 3)
AND SHIPMNT_SE_CODE IS NOT NULL
GROUP BY DATE_INFO
, STD_MTC_NEW_CODE
, STD_PRDLST_NEW_CODE
, STD_SPCIES_NEW_CODE
, DELNG_PRUT
, STD_QLITY_NEW_CODE
, SHIPMNT_SE_CODE"""
return pd.read_sql(query, con=self.conn)
# 시장 이름별 시장 code mapping dictionary
def get_market_map(self):
query = """SELECT UNIQUE WHSAL_MRKT_NEW_NM
, WHSAL_MRKT_NEW_CODE
FROM TBL_NH_DATA_MART
ORDER BY WHSAL_MRKT_NEW_CODE"""
market = pd.read_sql(query, con=self.conn)
market_map = {row[0]: row[1] for i, row in market.iterrows()}
return market_map
# 법인 List를 받아오는 query
def get_corp_to_num_map(self) -> dict:
query = """SELECT UNIQUE INSTT_NEW_NM
FROM TBL_NH_DATA_MART
WHERE WHSAL_MRKT_NEW_CODE = '{}'
ORDER BY INSTT_NEW_NM
""".format(self.market_code)
corp_list = pd.read_sql(query, con=self.conn)
corp_map = {corp: i + 1 for i, corp in enumerate(corp_list['INSTT_NEW_NM'].values)}
return corp_map
def insert(self, sql: str, data_list: list) -> None:
try:
cursor = self.conn.cursor()
cursor.executemany(sql, data_list)
self.conn.commit()
except Oci.DatabaseError as e:
error, _ = e.args
error_code = error.code
raise e
def insert_whsd_info(self, data_list: list) -> None:
sql = """
INSERT INTO TBL_WHSD_INFO(
WHSAL_MRKT_NEW_CODE, DATE_INFO, NUM, STD_PRDLST_NEW_CODE, STD_MTC_NEW_CODE, PRO_NAME, SEQ,
EXP_DIST, EXP_PROFIT_PRICE, INSTT_NEW_NM, BIGO, REG_DTTM, REG_ID, REG_IP, UPD_DTTM, UPD_ID, UPD_IP
) values (
{}
)
""".format(', '.join(list(map(lambda x: f':{x}', range(1, 1 + 17)))))
try:
self.insert(sql, data_list)
except Oci.DatabaseError as e:
raise e
def get_custom_date(self) -> pd.DataFrame:
query = """쿼리 입력"""
return pd.read_sql(query, con=self.conn)
def update_data(self, data_list: list) -> None:
sql = """INSERT INTO TABLE(
col_1, col_2, .... col_n
)values(:1, :2, ...., :n)
"""
try:
self.insert(sql, data_list)
except Oci.DatabaseError as e:
raise e