Blame view

src/main/java/com/batch/controller/JobController.java 13 KB
8dc487b1   함상기   Init Version - 20...
1
2
  package com.batch.controller;
  
30bbef42   함상기   ai sub Matching 추가
3
  import java.math.BigDecimal;
8dc487b1   함상기   Init Version - 20...
4
5
6
7
  import java.util.ArrayList;
  import java.util.HashMap;
  import java.util.List;
  import java.util.Map;
78928007   함상기   20240305
8
  import java.util.UUID;
8dc487b1   함상기   Init Version - 20...
9
10
  
  import org.springframework.beans.factory.annotation.Autowired;
82994579   함상기   2024.04.26
11
  import org.springframework.beans.factory.annotation.Value;
8dc487b1   함상기   Init Version - 20...
12
  import org.springframework.web.bind.annotation.GetMapping;
78928007   함상기   20240305
13
  import org.springframework.web.bind.annotation.PathVariable;
8dc487b1   함상기   Init Version - 20...
14
15
16
17
18
19
20
21
  import org.springframework.web.bind.annotation.PostMapping;
  import org.springframework.web.bind.annotation.RequestBody;
  import org.springframework.web.bind.annotation.RequestMapping;
  import org.springframework.web.bind.annotation.RestController;
  
  import com.batch.config.MatchingSetup;
  import com.batch.config.MatchingSetup.Matching;
  import com.batch.mapper.primary.MatchingInnerDelingMapper;
8dc487b1   함상기   Init Version - 20...
22
23
  import com.batch.service.JobService;
  import com.batch.util.FileUtil;
82994579   함상기   2024.04.26
24
  import com.batch.util.JsonUtil;
8dc487b1   함상기   Init Version - 20...
25
26
  import com.google.gson.JsonObject;
  
8dc487b1   함상기   Init Version - 20...
27
28
29
30
31
32
33
34
35
36
37
  import lombok.extern.slf4j.Slf4j;
  
  @RestController
  @RequestMapping("/api/job")
  @Slf4j
  public class JobController {
  
      @Autowired
      private JobService jobService;
      @Autowired
      private MatchingInnerDelingMapper matchingInnerDelingMapper;
82994579   함상기   2024.04.26
38
39
40
41
42
43
  	@Value("${thread.ai.count.rowmax}")
  	BigDecimal bdAiAsyncMaxRowCount;
  	@Value("${thread.ai.processing.wait}")
  	BigDecimal bdAiAsyncWaitTime;
  	@Value("${thread.ai.processing.wait.total}")
  	BigDecimal bdAiAsyncTotalWaitTime;
8dc487b1   함상기   Init Version - 20...
44
      
31cbf4da   함상기   2024-04-02
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
      
      
      
      /**
       * TODO : AI 서버에 특정 경로에 생성되는 파일을 읽는(끝에 50 정도) API 
       * */
      @PostMapping("/readlogs")
      public StringBuffer readLogs( @RequestBody Map<String, String> params) throws Exception {
  		StringBuffer sb = new StringBuffer();
  		
  		String filePath  = params.get("filePath");
  		int readCnt  = Integer.parseInt(params.get("readCnt"));
  		
      	sb = FileUtil.readFileLastLines(filePath, readCnt, "ISO-8859-1", "UTF-8");
  
      	log.info("sb ===");
      	log.info(sb.toString());
  		return sb;
      }
8dc487b1   함상기   Init Version - 20...
64
65
  
      @PostMapping("/create")
f97db154   함상기   .
66
      public Map<String, String> createJob( @RequestBody Map<String, Object> params) throws Exception {
8dc487b1   함상기   Init Version - 20...
67
68
69
70
71
72
      	
      	/*
      	 * {                                
      	 *   "sysSe": "LS_ALL",             
      	 *   "accnutYm": "202306",          
      	 * }  
78928007   함상기   20240305
73
74
75
76
77
      	 */            
      	//Job Create Log
      	UUID uuid = UUID.randomUUID();
      	String sJobGroup = uuid.toString();
      	
82994579   함상기   2024.04.26
78
      	log.info("Start Create Job");
78928007   함상기   20240305
79
      	jobService.createData(sJobGroup, params);
82994579   함상기   2024.04.26
80
      	log.info("End Create Job");
8dc487b1   함상기   Init Version - 20...
81
      	
78928007   함상기   20240305
82
83
84
      	Map<String, String> rtnMap = new HashMap<String, String>();
      	rtnMap.put("jobGroupId", sJobGroup);
      	rtnMap.put("jobMessage", "신규 작업데이타를 생성합니다. 작업이 끝난후 작업결과는 별도로 확인 바랍니다.");
8dc487b1   함상기   Init Version - 20...
85
      	
78928007   함상기   20240305
86
          return rtnMap;
8dc487b1   함상기   Init Version - 20...
87
88
89
90
      }
      
      
      @PostMapping("/matching")
78928007   함상기   20240305
91
      public Map<String, String> matchingJob( @RequestBody Map<String, String> params) throws Exception {
8dc487b1   함상기   Init Version - 20...
92
93
94
95
96
      	
      	/*
      	 * {                                
      	 *   "sysSe": "LS_ALL",             
      	 *   "accnutYm": "202306",          
30bbef42   함상기   ai sub Matching 추가
97
98
      	 *   "cpr_code": "xxxxx",          
      	 *   "partn_cpr": "xxxxx",          
8dc487b1   함상기   Init Version - 20...
99
      	 * }  
78928007   함상기   20240305
100
101
102
103
104
      	 */                           
      	//Job Create Log
      	UUID uuid = UUID.randomUUID();
      	String sJobGroup = uuid.toString();
      	
82994579   함상기   2024.04.26
105
      	log.info("Start Matching Job");
78928007   함상기   20240305
106
      	jobService.matchingJob(sJobGroup, params);
82994579   함상기   2024.04.26
107
      	log.info("End Matching Job");
8dc487b1   함상기   Init Version - 20...
108
      	
78928007   함상기   20240305
109
110
111
112
113
      	Map<String, String> rtnMap = new HashMap<String, String>();
      	rtnMap.put("jobGroupId", sJobGroup);
      	rtnMap.put("jobMessage", "매칭작업을 시작합니다. 작업이 끝난후 작업결과는 별도로 확인 바랍니다.");
      	    	
          return rtnMap;
8dc487b1   함상기   Init Version - 20...
114
115
116
      }
      
      @PostMapping("/extramatching")
78928007   함상기   20240305
117
      public Map<String, String> extraMatchingJob( @RequestBody Map<String, String> params) throws Exception {
8dc487b1   함상기   Init Version - 20...
118
119
120
121
      	
      	/*
      	 * {                                
      	 *   "sysSe": "LS_ALL",             
78928007   함상기   20240305
122
      	 *   "accnutYm": "202306",    
30bbef42   함상기   ai sub Matching 추가
123
124
      	 *   "cpr_code": "xxxxx",          
      	 *   "partn_cpr": "xxxxx",          
78928007   함상기   20240305
125
126
      	 *   "searchOne": "dta_ty in ('11','21','31','33','35','37','41')",
      	 *   "searchTwo": "dta_ty in ('11','21','31','33','35','37','41')"      
8dc487b1   함상기   Init Version - 20...
127
      	 * }  
78928007   함상기   20240305
128
      	 */                       
78928007   함상기   20240305
129
130
131
132
      	//Job Create Log
      	UUID uuid = UUID.randomUUID();
      	String sJobGroup = uuid.toString();
      	
82994579   함상기   2024.04.26
133
134
      	log.info("Start Extra Matching Job");
      	params.put("asCurrency", "Y");
d3e8a8a9   함상기   2024-04-01
135
      	List<Map> retData = matchingInnerDelingMapper.getCustomItemReadData(params);
a45ecedc   ggun12   * EX 매칭은 순차 실행으로 처리
136
  
d3e8a8a9   함상기   2024-04-01
137
138
139
140
141
142
143
144
145
146
147
148
149
150
      	// 수익/비용
      	for(Map curMap : retData) {
      		Map<String, String> cParams = new HashMap<String, String>();
      		cParams.putAll(curMap);
      		cParams.put("conds", "T");
      		jobService.extraJobSub(sJobGroup, cParams);
      	}
      	//채권/채무
      	for(Map curMap : retData) {
      		Map<String, String> cParams = new HashMap<String, String>();
      		cParams.putAll(curMap);
      		cParams.put("conds", "B");
      		jobService.extraJobSub(sJobGroup, cParams);
      	}
a45ecedc   ggun12   * EX 매칭은 순차 실행으로 처리
151
  
82994579   함상기   2024.04.26
152
      	log.info("End Extra Matching Job");
8dc487b1   함상기   Init Version - 20...
153
      	
78928007   함상기   20240305
154
155
156
      	Map<String, String> rtnMap = new HashMap<String, String>();
      	rtnMap.put("jobGroupId", sJobGroup);
      	rtnMap.put("jobMessage", "Extra 매칭작업을 시작합니다. 작업이 끝난후 작업결과는 별도로 확인 바랍니다.");
8dc487b1   함상기   Init Version - 20...
157
      	
78928007   함상기   20240305
158
          return rtnMap;
8dc487b1   함상기   Init Version - 20...
159
160
161
      }      
      
      
30bbef42   함상기   ai sub Matching 추가
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
      @PostMapping("/aisubmatching")
      public Map<String, String> aiSubMatchingJob( @RequestBody Map<String, String> params) throws Exception {
      	
      	/*
      	 * {                                
      	 *   "sysSe": "LS_ALL",
      	 *   "accnutYm": "202306",    
      	 *   "mtch_ty": "AI_SLT"          
      	 *   "cpr_code": "xxxxx",          
      	 *   "partn_cpr": "xxxxx"          
      	 * }  
      	 */                       
      	//Job Create Log
      	UUID uuid = UUID.randomUUID();
      	String sJobGroup = uuid.toString();
      	
82994579   함상기   2024.04.26
178
      	log.info("Start AI Sub Matching Job");
30bbef42   함상기   ai sub Matching 추가
179
180
181
182
183
184
185
186
187
188
189
      	List<Map> retData = matchingInnerDelingMapper.getAiSubReadData(params);
  
      	//AI의 경우 수익비용만 매칭하고 있음
      	for(Map curMap : retData) {
      		Map<String, String> cParams = new HashMap<String, String>();
      		cParams.putAll(params);
      		cParams.putAll(curMap);
      		cParams.put("conds", "T");
      		jobService.aiSubJobSub(sJobGroup, cParams);
      	}
  
82994579   함상기   2024.04.26
190
      	log.info("End Extra Matching Job");
30bbef42   함상기   ai sub Matching 추가
191
192
193
194
195
196
197
198
199
      	
      	Map<String, String> rtnMap = new HashMap<String, String>();
      	rtnMap.put("jobGroupId", sJobGroup);
      	rtnMap.put("jobMessage", "Extra 매칭작업을 시작합니다. 작업이 끝난후 작업결과는 별도로 확인 바랍니다.");
      	
          return rtnMap;
      }      
  
      
8dc487b1   함상기   Init Version - 20...
200
      @PostMapping("/aimatching")
78928007   함상기   20240305
201
      public Map<String, String> aiMatchingJob( @RequestBody Map<String, String> params) throws Exception {
8dc487b1   함상기   Init Version - 20...
202
203
204
205
206
      	
      	/*
      	 * {                                
      	 *   "sysSe": "LS_ALL",             
      	 *   "accnutYm": "202306",          
30bbef42   함상기   ai sub Matching 추가
207
208
209
      	 *   "cpr_code": "xxxxx",          
      	 *   "partn_cpr": "xxxxx",          
      	 *   "error_range": "0"          
8dc487b1   함상기   Init Version - 20...
210
      	 * }  
78928007   함상기   20240305
211
      	 */             
82994579   함상기   2024.04.26
212
      	//Job Create LogmatchingInnerDelingMapper
78928007   함상기   20240305
213
214
215
      	UUID uuid = UUID.randomUUID();
      	String sJobGroup = uuid.toString();
      	
82994579   함상기   2024.04.26
216
      	log.info("Start AI Matching Job");
8dc487b1   함상기   Init Version - 20...
217
      	List<Map> retData = matchingInnerDelingMapper.getAiReadData(params);
82994579   함상기   2024.04.26
218
      	BigDecimal bdCurrentRowCount = BigDecimal.ZERO; 
8dc487b1   함상기   Init Version - 20...
219
      	for(Map curMap : retData) {
30bbef42   함상기   ai sub Matching 추가
220
221
222
      		Map<String, Object> curMParams = new HashMap<String, Object>();
      		curMParams.putAll(curMap);
      		curMParams.put("error_range", params.get("error_range"));
82994579   함상기   2024.04.26
223
224
225
226
      		
      		BigDecimal bdCntAll = new BigDecimal(String.valueOf(curMap.get("cnt_all")));
      		bdCurrentRowCount = bdCurrentRowCount.add(bdCntAll);
      		log.info("Call Matching Job (" + JsonUtil.objectToString(curMap) + ")");
30bbef42   함상기   ai sub Matching 추가
227
      		jobService.aiJobSub(sJobGroup, curMParams);
82994579   함상기   2024.04.26
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
      		
      		//전체카운트가 처리가능카운트를 넘어설때
      		if (bdCurrentRowCount.compareTo(bdAiAsyncMaxRowCount) > -1) {
      			bdCurrentRowCount = BigDecimal.ZERO;
      			
      			//현재 작업그룹이 종료가 되었는지 체크해서 종료시 카운트 초기화 하고 다음작업을 한다.
      	    	boolean blnStatus = true;
      	    	Integer iTotalWait = 0;
      	    	Integer iWaitTime = bdAiAsyncWaitTime.intValue() * 1000; //밀리초
      	    	do {
  	    	    	Map<String, Object> mParam = new HashMap<String, Object>();
  	    	    	mParam.put("userJobGroup", sJobGroup);
  	    	    	List<Map> lmJob = matchingInnerDelingMapper.getUserJobStatus(mParam);
  	    	    	for (Map curJob : lmJob) {
  	    	    		String ScurStatus = (String) curJob.get("status"); 
  	    	    		if (!"Finished".equalsIgnoreCase(ScurStatus)) {
  	    	    			blnStatus = false;	    	    			
  	    	    			Thread.sleep(iWaitTime); 
  	    	    			iTotalWait = iTotalWait + iWaitTime;
  	    	    			break;
  	    	    		}
  	    	    	}
      	    	} while (!blnStatus && iTotalWait < bdAiAsyncTotalWaitTime.intValue());  //전체 기다리는 시간까지 체크  
      	    	log.info("Next Thread Group Processing");
      		}
8dc487b1   함상기   Init Version - 20...
253
      	}
82994579   함상기   2024.04.26
254
      	log.info("End AI Matching Job");
8dc487b1   함상기   Init Version - 20...
255
      	
78928007   함상기   20240305
256
257
258
      	Map<String, String> rtnMap = new HashMap<String, String>();
      	rtnMap.put("jobGroupId", sJobGroup);
      	rtnMap.put("jobMessage", "AI 매칭작업을 시작합니다. 작업이 끝난후 작업결과는 별도로 확인 바랍니다.");
8dc487b1   함상기   Init Version - 20...
259
      	
78928007   함상기   20240305
260
          return rtnMap;
8dc487b1   함상기   Init Version - 20...
261
262
263
      }    
  
      @PostMapping("/return")
78928007   함상기   20240305
264
      public Map<String, String> returnJob( @RequestBody Map<String, String> params) throws Exception {
8dc487b1   함상기   Init Version - 20...
265
266
267
268
269
270
      	
      	/*
      	 * {                                
      	 *   "sysSe": "LS_ALL",             
      	 *   "accnutYm": "202306",          
      	 * }  
78928007   함상기   20240305
271
272
273
274
      	 */        
      	UUID uuid = UUID.randomUUID();
      	String sJobGroup = uuid.toString();
  
82994579   함상기   2024.04.26
275
      	log.info("Start Return Job");
78928007   함상기   20240305
276
      	jobService.returnRwsultData(sJobGroup, params);
82994579   함상기   2024.04.26
277
      	log.info("End Return Job");
8dc487b1   함상기   Init Version - 20...
278
      	
78928007   함상기   20240305
279
280
281
      	Map<String, String> rtnMap = new HashMap<String, String>();
      	rtnMap.put("jobGroupId", sJobGroup);
      	rtnMap.put("jobMessage", "매칭결과 반영을 시작합니다. 작업이 끝난후 작업결과는 별도로 확인 바랍니다.");
8dc487b1   함상기   Init Version - 20...
282
      	
78928007   함상기   20240305
283
          return rtnMap;
8dc487b1   함상기   Init Version - 20...
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
      }
      
      
      @GetMapping("/configJobStr")
      public String configJobStr() throws Exception {
      	
      	StringBuffer sb = FileUtil.readFileToString("matchingSetup.json");
  
      	return sb.toString();
      }
  
      @GetMapping("/configJobJsonObj")
      public JsonObject configJobJsonObj() throws Exception {
      	
      	StringBuffer sb = FileUtil.readFileToString("matchingSetup.json");
      	JsonObject jobj = FileUtil.strToJsonObj(sb.toString());
  
      	return jobj;
      }
  
      @GetMapping("/configJobObj")
      public Object configJobObj() throws Exception {
      	
      	StringBuffer sb = FileUtil.readFileToString("matchingSetup.json");
      	MatchingSetup matchingSetup = (MatchingSetup) FileUtil.strToObj(sb.toString(), MatchingSetup.class);
  
      	return matchingSetup;
      }
  
      @GetMapping("/config")
      public Object config() throws Exception {
      	return configJobObj();
      }
  
      @GetMapping("/config/jobList")
      public Object configJobList() throws Exception {
      	
      	StringBuffer sb = FileUtil.readFileToString("matchingSetup.json");
      	MatchingSetup matchingSetup = (MatchingSetup) FileUtil.strToObj(sb.toString(), MatchingSetup.class);
  
      	List<String> lJobList = new ArrayList<String>();
      	for (Matching curMatching : matchingSetup.getMatchingSetup()) {
      		lJobList.add(curMatching.getType() + ':' + curMatching.getTypeName());
      	}
      	
      	return lJobList;
      }
  
78928007   함상기   20240305
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
      @GetMapping("/userJobInfo/{userJobGroup}")
      public Object statusBatchJob(@PathVariable(name = "userJobGroup") String sUserJobGroup) throws Exception {
      	Map<String, Object> rtnVal = new HashMap<String, Object>();
      	
      	Map<String, Object> mParam = new HashMap<String, Object>();
      	mParam.put("userJobGroup", sUserJobGroup);
      	List<Map> lmJob = matchingInnerDelingMapper.getUserJobStatus(mParam);
      	rtnVal.put("JobList", lmJob);
      	boolean blnStatus = true;
      	for (Map curMap : lmJob) {
      		String ScurStatus = (String) curMap.get("status"); 
      		if (!"Finished".equalsIgnoreCase(ScurStatus)) {
      			blnStatus = false;
      			break;
      		}
      	}
      	rtnVal.put("JobStatus", blnStatus);
      	
      	
      	return rtnVal;
      }
82994579   함상기   2024.04.26
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
    
      
      @PostMapping("/callAsyncJob")
      public Map<String, String> callAsyncJob( @RequestBody Map<String, String> params) throws Exception {
      	
      	/*
      	 * {                                
      	 *   "PrgName": "cmd",
      	 *   "PrgParams": "/C dir /w",    
      	 * }  
      	 */                       
      	//Job Create Log
      	UUID uuid = UUID.randomUUID();
      	String sJobGroup = uuid.toString();
      	
      	log.info("Start AI Sub Matching Job");
  
      	//OS Command Line 수행
  		jobService.callAsyncJobSub(sJobGroup, params);
  
      	log.info("End Extra Matching Job");
      	
      	Map<String, String> rtnMap = new HashMap<String, String>();
      	rtnMap.put("jobGroupId", sJobGroup);
      	rtnMap.put("jobMessage", "OS Command 작업을 시작합니다. 작업이 끝난후 작업결과는 별도로 확인 바랍니다.");
      	
          return rtnMap;
      }      
78928007   함상기   20240305
381
      
8dc487b1   함상기   Init Version - 20...
382
383
      
  }