-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathprepare_revision.py
107 lines (97 loc) · 4.2 KB
/
prepare_revision.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
from adj_serializer import AdjSerializer
from deal_serializer import DealSerializer
from file_save_helper import FileSaveHelper
from option_serializer import OptionSerializer
from platform_consumer_serializer import PlatformConsumerSerializer
from src.model.prepared_data import PreparedData
from src.model.raw_csv import RawCsvDeal, RawCsvGoodsOption, RawCsvPlatformConsumer, RawCsvAdj
class PrepareRevisionService:
@classmethod
def prepare(
cls,
goods_sno_list: list[int],
deal_filepath: str,
option_filepath: str,
consumer_filepath: str,
adj_filepath: str,
) -> PreparedData:
return PreparedData(
deal_map=cls._fetch_deals(
filepath=deal_filepath,
goods_sno_list=goods_sno_list,
),
option_map=cls._fetch_options(
filepath=option_filepath,
goods_sno_list=goods_sno_list,
),
platform_consumer_map=cls._fetch_platform_consumers(
filepath=consumer_filepath,
goods_sno_list=goods_sno_list,
),
adj_map=cls._fetch_adjs(
filepath=adj_filepath,
goods_sno_list=goods_sno_list,
),
)
@classmethod
def _fetch_options(cls, filepath: str, goods_sno_list: list[int]) -> dict[int, list[RawCsvGoodsOption]]:
serialized_data: bytes = FileSaveHelper.read(filepath=filepath)
option_map: dict[int, list[RawCsvGoodsOption]] = OptionSerializer.deserialize(data=serialized_data)
if goods_sno_list:
selected_option_map: dict[int, list[RawCsvGoodsOption]] = dict()
for goods_sno in goods_sno_list:
if goods_sno not in option_map:
print('option skipped goods_sno: ', goods_sno)
continue
selected_option_map[goods_sno] = option_map[goods_sno]
return selected_option_map
else:
return option_map
@classmethod
def _fetch_deals(cls, filepath: str, goods_sno_list: list[int]) -> dict[int, list[RawCsvDeal]]:
serialized_data: bytes = FileSaveHelper.read(filepath=filepath)
deal_map: dict[int, list[RawCsvDeal]] = DealSerializer.deserialize(data=serialized_data)
skipped_cnt: int = 0
if goods_sno_list:
selected_deal_map: dict[int, list[RawCsvDeal]] = dict()
for goods_sno in goods_sno_list:
if goods_sno not in deal_map:
skipped_cnt += 1
continue
selected_deal_map[goods_sno] = deal_map[goods_sno]
print('deal skipped goods_sno: ', skipped_cnt)
return selected_deal_map
else:
return deal_map
@classmethod
def _fetch_platform_consumers(
cls,
filepath: str,
goods_sno_list: list[int],
) -> dict[int, list[RawCsvPlatformConsumer]]:
data: bytes = FileSaveHelper.read(filepath=filepath)
consumer_map: dict[int, list[RawCsvPlatformConsumer]] = PlatformConsumerSerializer.deserialize(data=data)
if goods_sno_list:
selected_map: dict[int, list[RawCsvPlatformConsumer]] = dict()
for goods_sno in goods_sno_list:
if goods_sno not in consumer_map:
print('consumer skipped goods_sno: ', goods_sno)
continue
selected_map[goods_sno] = consumer_map[goods_sno]
return selected_map
else:
return consumer_map
@classmethod
def _fetch_adjs(cls, filepath: str, goods_sno_list: list[int]) -> dict[int, list[RawCsvAdj]]:
data: bytes = FileSaveHelper.read(filepath=filepath)
adj_map: dict[int, list[RawCsvAdj]] = AdjSerializer.deserialize(data=data)
if goods_sno_list:
selected_map: dict[int, list[RawCsvAdj]] = dict()
for goods_sno in goods_sno_list:
if goods_sno not in adj_map:
print('adj skipped goods_sno: ', goods_sno)
continue
selected_map[goods_sno] = adj_map[goods_sno]
return selected_map
else:
return adj_map