Blame view

src/main/java/com/batch/config/MatchingExtraProcessorAuto.java 9.12 KB
8dc487b1   함상기   Init Version - 20...
1
2
3
4
5
6
7
8
9
10
11
  package com.batch.config;

  

  import java.math.BigDecimal;

  import java.text.SimpleDateFormat;

  import java.util.ArrayList;

  import java.util.Date;

  import java.util.HashMap;

  import java.util.List;

  import java.util.Map;

  import java.util.UUID;

  

a45ecedc   ggun12   * EX 매칭은 순차 실행으로 처리
12
13
  import org.springframework.batch.core.configuration.annotation.StepScope;

  

8dc487b1   함상기   Init Version - 20...
14
15
16
17
18
19
20
21
22
23
24
25
26
  import com.batch.mapper.primary.MatchingInnerDelingMapper;

  import com.batch.util.StatisticsUtil;

  

  import lombok.RequiredArgsConstructor;

  import lombok.extern.slf4j.Slf4j;

  

  @Slf4j

  @RequiredArgsConstructor

  public class MatchingExtraProcessorAuto {

  

  	private final MatchingInnerDelingMapper matchingInnerDelingMapper;

  

      @SuppressWarnings("unchecked")

a45ecedc   ggun12   * EX 매칭은 순차 실행으로 처리
27
  	public void process(Map paramRec, int iCmbnOwnCnt, int iCmbnTranCnt, int iStartOwn, int iStartTran, int mtchNumber) throws Exception {

8dc487b1   함상기   Init Version - 20...
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
  

      	//Job Create Log

      	UUID uuid = UUID.randomUUID();

      	HashMap<String, String> mt = new HashMap<String, String>();

          SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd-hh-mm-ss");

          String sDate = dateFormat.format(new Date()) + ":" + uuid.toString();

      	

      	Map<String, Object> paramLog = new HashMap<String, Object>();

      	paramLog.put("user_job_id", sDate);

      	paramLog.put("user_job_name", "자동조합매칭(" + paramRec.toString() + ",[" + iCmbnOwnCnt + "," + iStartOwn + "],[" + iCmbnTranCnt + "," + iStartTran + "])");

      	matchingInnerDelingMapper.createUserJob(paramLog);

  

      	

      	int iUpdated = 0;

      	

      	String sSysSe = (String) paramRec.get("sys_se");

      	String sAccnutYm = (String) paramRec.get("accnut_ym");

      	String sCprCode = (String) paramRec.get("cpr_code");

      	String sPartCpr = (String) paramRec.get("partn_cpr");

406c670d   ggun12   - 대상목록, 매칭목록 에 거래...
47
      	String sDelngCrncy = (String) paramRec.get("delng_crncy");

78928007   함상기   20240305
48
      	String sConds = (String) paramRec.get("conds");

406c670d   ggun12   - 대상목록, 매칭목록 에 거래...
49
      	

8dc487b1   함상기   Init Version - 20...
50
51
52
53
      	//작업시작

      	Map<String, Object> mParam = new HashMap<String, Object>();

      	mParam.put("sysSe", sSysSe);

      	mParam.put("accnutYm", sAccnutYm);

78928007   함상기   20240305
54
      	mParam.put("conds", sConds);

406c670d   ggun12   - 대상목록, 매칭목록 에 거래...
55
      	mParam.put("delngCrncy", sDelngCrncy);

8dc487b1   함상기   Init Version - 20...
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
      	

      	//----------------------------------------------------------------------------

      	//자기법인 데이타 가져오기

      	mParam.put("cprCode", sCprCode);

      	mParam.put("partnCpr", sPartCpr);

      	List<Map> lMatchingDataOne = matchingInnerDelingMapper.getMatchingExtraDataOne(mParam);

  

      	//상대법인 데이타 가져오기

      	mParam.put("cprCode", sPartCpr);

      	mParam.put("partnCpr", sCprCode);

      	List<Map> lMatchingDataTwo = matchingInnerDelingMapper.getMatchingExtraDataTwo(mParam);

      	

      	//Combination 데이타 만들기

  		List<Map> compResult = new ArrayList<Map>();

  		List<List<Map>> llMatchingDataOne = new ArrayList<List<Map>>();

  		List<List<Map>> llMatchingDataTwo = new ArrayList<List<Map>>();

      			

  		int iEndOwn = lMatchingDataOne.size();

  		if (iCmbnOwnCnt == 2) {

  			iEndOwn = iStartOwn + 1000;

  			if (iEndOwn > lMatchingDataOne.size()) iEndOwn = lMatchingDataOne.size();

  		}

81d4839a   함상기   20240228
78
79
  		if (iCmbnOwnCnt == 3) {

  			iEndOwn = iStartOwn + 50;

8dc487b1   함상기   Init Version - 20...
80
81
  			if (iEndOwn > lMatchingDataOne.size()) iEndOwn = lMatchingDataOne.size();

  		}

81d4839a   함상기   20240228
82
83
84
85
86
  		if (iCmbnOwnCnt > 3) {

  			iEndOwn = iStartOwn + 25;

  			if (iEndOwn > lMatchingDataOne.size()) iEndOwn = lMatchingDataOne.size();

  		}

  

8dc487b1   함상기   Init Version - 20...
87
88
89
90
91
  		int iEndTran = lMatchingDataTwo.size();

  		if (iCmbnTranCnt == 2) {

  			iEndTran = iStartTran + 1000;

  			if (iEndTran > lMatchingDataTwo.size()) iEndTran = lMatchingDataTwo.size();

  		}

81d4839a   함상기   20240228
92
93
94
95
96
97
  		if (iCmbnTranCnt == 3) {

  			iEndTran = iStartTran + 50;

  			if (iEndTran > lMatchingDataTwo.size()) iEndTran = lMatchingDataTwo.size();

  		}

  		if (iCmbnTranCnt > 3) {

  			iEndTran = iStartTran + 25;

8dc487b1   함상기   Init Version - 20...
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
  			if (iEndTran > lMatchingDataTwo.size()) iEndTran = lMatchingDataTwo.size();

  		}

  		

      	StatisticsUtil.reculsion(lMatchingDataOne, compResult, iStartOwn, iEndOwn, iCmbnOwnCnt, llMatchingDataOne);

      	StatisticsUtil.reculsion(lMatchingDataTwo, compResult, iStartTran, iEndTran, iCmbnTranCnt, llMatchingDataTwo);

      	

      	//----------------------------------------------------------------------------

      	//자기법인 데이타를 맵으로 처리한다.

      	Map<BigDecimal, List<List<Map>>> mMatchingDataOne = new HashMap<BigDecimal, List<List<Map>>>();

      	for (List<Map> curlMap : llMatchingDataOne) {

      		BigDecimal bdKey = BigDecimal.ZERO;

      		for(Map curMap : curlMap) {

      			bdKey = bdKey.add((BigDecimal) curMap.get("delng_amt"));

      		}

      		List<List<Map>> curllMap = new ArrayList<List<Map>>();

      		if (mMatchingDataOne.containsKey(bdKey)) curllMap = mMatchingDataOne.get(bdKey);

      		curllMap.add(curlMap);

      		mMatchingDataOne.put(bdKey, curllMap);

      	}

  

      	//상대법인 데이타를 맵으로 처리한다.

      	Map<BigDecimal, List<List<Map>>> mMatchingDataTwo = new HashMap<BigDecimal, List<List<Map>>>();

      	for (List<Map> curlMap : llMatchingDataTwo) {

      		BigDecimal bdKey = BigDecimal.ZERO;

      		for(Map curMap : curlMap) {

      			bdKey = bdKey.add((BigDecimal) curMap.get("delng_amt"));

      		}

      		List<List<Map>> curllMap = new ArrayList<List<Map>>();

      		if (mMatchingDataTwo.containsKey(bdKey)) curllMap = mMatchingDataTwo.get(bdKey);

      		curllMap.add(curlMap);

      		mMatchingDataTwo.put(bdKey, curllMap);

      	}

      	

      	//여기서 매칭 비교

      	//비교 lMatchingDataOne vs mMatchingDataTwo

      	List<Map> lMatchingResultUpdate = new ArrayList<Map>(); //업데이트할 대상

  

8dc487b1   함상기   Init Version - 20...
135
136
      	String mtchSys = "AUTO";

      	String mtchType = "EX_" + iCmbnOwnCnt + "_" + iCmbnTranCnt;

406c670d   ggun12   - 대상목록, 매칭목록 에 거래...
137
      	String mtchTypeName = "자기(" + iCmbnOwnCnt + "건Sum), 상대(" + iCmbnTranCnt + "건Sum), ("+sDelngCrncy+"),  비교(금액)";

8dc487b1   함상기   Init Version - 20...
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
      	for (BigDecimal curKey : mMatchingDataOne.keySet()) {

      		if (mMatchingDataTwo.containsKey(curKey)) {

      			List<List<Map>> llMapOne = mMatchingDataOne.get(curKey);

      			List<List<Map>> llMapTwo = mMatchingDataTwo.get(curKey);

      			

  				//위와 관련된 동일 레코드가 있는 리스트 삭제

  				for (int i=llMapOne.size()-1; i>=0;i--) {

  					List<Map> curlMap = llMapOne.get(i);    					

  					for (Map curMap : lMatchingResultUpdate) {

  						int curIdx = curlMap.indexOf(curMap);

  						if (curIdx != -1) {

  							llMapOne.remove(i);

  							break;

  						}    						

  					}

  				}

  				for (int i=llMapTwo.size()-1; i>=0;i--) {

  					List<Map> curlMap = llMapTwo.get(i);    					

  					for (Map curMap : lMatchingResultUpdate) {

  						int curIdx = curlMap.indexOf(curMap);

  						if (curIdx != -1) {

  							llMapTwo.remove(i);

  							break;

  						}    						

  					}

  				}

      			

      			

      			int iMin = Math.min(llMapOne.size(), llMapTwo.size());

      			if (iMin > 0) mtchNumber++;

      			while (iMin > 0) {

      				List<Map> curlMapOne = llMapOne.get(0);

      				for (Map curMap : curlMapOne) {

      					curMap.put("mtch_sys", mtchSys);

      					curMap.put("mtch_ty", mtchType);

      					curMap.put("mtch_ty_nm", mtchTypeName);

      					curMap.put("mtch_ky", mtchNumber);

      					lMatchingResultUpdate.add(curMap);

      				}

  					llMapOne.remove(0);

  

  					List<Map> curlMapTwo = llMapTwo.get(0);

      				for (Map curMap : curlMapTwo) {

      					curMap.put("mtch_sys", mtchSys);

      					curMap.put("mtch_ty", mtchType);

      					curMap.put("mtch_ty_nm", mtchTypeName);

      					curMap.put("mtch_ky", mtchNumber);

      					lMatchingResultUpdate.add(curMap);

      				}

  					llMapTwo.remove(0);

  

  					//위와 관련된 동일 레코드가 있는 리스트 삭제(추가 업데이트 목록)

      				for (int i=llMapOne.size()-1; i>=0;i--) {

      					List<Map> curlMap = llMapOne.get(i);    					

      					for (Map curMap : curlMapOne) {

      						int curIdx = curlMap.indexOf(curMap);

      						if (curIdx != -1) {

      							llMapOne.remove(i);

      							break;

      						}    						

      					}

      				}

      				for (int i=llMapTwo.size()-1; i>=0;i--) {

      					List<Map> curlMap = llMapTwo.get(i);    					

      					for (Map curMap : curlMapTwo) {

      						int curIdx = curlMap.indexOf(curMap);

      						if (curIdx != -1) {

      							llMapTwo.remove(i);

      							break;

      						}    						

      					}

      				}

          			iMin = Math.min(llMapOne.size(), llMapTwo.size());

      			}

      		}

      	}

      	

      	//----------------------------------------------------------------------------

      	//여기서 결과 업데이트

      	iUpdated = 0;

      	int limit = 1000; //1000건씩 batch

      	List<Map> lUpdated = new ArrayList<Map>();

      	for (Map curMap : lMatchingResultUpdate) {

      		lUpdated.add(curMap);

      		if (lUpdated.size() == limit) {

      			matchingInnerDelingMapper.setExtraResult(Map.of("itemList", lUpdated));

      			iUpdated = iUpdated + lUpdated.size();

      			lUpdated.clear();

      		}

      	}

      	if (lUpdated.size() > 0) {

      		matchingInnerDelingMapper.setExtraResult(Map.of("itemList", lUpdated));

      		iUpdated = iUpdated + lUpdated.size();    		

      	}

      	log.debug("Updated OrgData : " + iUpdated + "건");

      	

a45ecedc   ggun12   * EX 매칭은 순차 실행으로 처리
234
      	

8dc487b1   함상기   Init Version - 20...
235
236
237
238
239
240
241
242
      	//작업종료에 대한 로그 업데이트

      	paramLog.put("exit_code", "0");

      	paramLog.put("exit_message", "");    	

      	matchingInnerDelingMapper.finishUserJob(paramLog);

      	

      }

      

  }