/**
 * @(#)file   PromiseListSaveBiz.java
 * @(#)author  hoonseok choi
 * @(#)date    2022.11.8
 *
 * Copyright (c) www.udapsoft.co.kr, Inc.
 * All rights reserved.
 */

 package kr.co.udapsoft.ebid.buyer.cont.outside.biz;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.SQLException;

import kr.co.hsnc.common.base.WAFLogger;
import kr.co.hsnc.common.util.ValueObject;
import kr.co.udapsoft.ebid.buyer.common.file.FileManager;

import com.udapsoft.modular.web.ibsheet7.IBSheet7Factory;
import com.udapsoft.modular.web.ibsheet7.util.IBSheet7Utils;
import com.udapsoft.waf.system.Biz;
import com.udapsoft.waf.system.HandlerStorage;

public class PromiseListSaveBiz extends Biz {
	
	public PromiseListSaveBiz(HandlerStorage storage) {
		super(storage);
	}

	public void doBiz() throws Exception {
		if (storage.compareEvent("SAVE_PROMISE_LIST")) {
			
			boolean success = false;
			String[] status = super.gets("sheet3.STATUS");

			try {
				super.conn.begin();
				
				for (int i = 0; i < status.length; i++) {
					
					if (IBSheet7Utils.equalStatusInsert(status[i])) {
						success = saveData(super.conn.getConnection(), IBSheet7Utils.getIBSheet7SqlParams("SHEET3", params, i));
					}
					
					if (IBSheet7Utils.equalStatusUpdate(status[i])) {
						success = updateData(super.conn.getConnection(), IBSheet7Utils.getIBSheet7SqlParams("SHEET3", params, i));
					}
					
					if (IBSheet7Utils.equalStatusDelete(status[i])) {
						FileManager fileManager = new FileManager();
		            	fileManager.delFileMapProc(conn.getConnection(), "OUT_PROMISE_DOC", IBSheet7Utils.getIBSheet7SqlParams("SHEET3", params, i).get("PROMISE_DOC_ID"));
						success = deleteData(super.conn.getConnection(), IBSheet7Utils.getIBSheet7SqlParams("SHEET3", params, i));
					}
				}
				
				super.storage.setSuccess(success);
				storage.addObject("ibsheet7Data", IBSheet7Factory.createIBSheet7(params, storage.request));
			} catch (Exception e) {
				WAFLogger.error(e);
				super.conn.setRollback();
				throw new Exception(e);
				
			} finally {
				super.conn.close();
			}
		}
	}

	private boolean saveData(Connection connection, ValueObject sqlParams) throws Exception {
		PreparedStatement pstmt = null;
		
		try {
			StringBuffer sql = new StringBuffer();
			
			sql.append("   INSERT INTO PROMISE_DOC_TYPE(                             \n");
			sql.append("        PROMISE_DOC_ID                                       \n");
			sql.append("       ,PROMISE_DOC_TYPE                                     \n");
			sql.append("       ,PROMISE_DOC_TYPE_GB                                  \n");
			sql.append("       ,CONT_MOD_ID                                          \n");
			sql.append("       ,CORP_REG_NO                                          \n");
			sql.append("       ,CORP_NAME                                            \n");
			sql.append("       ,CONT_NO                                              \n");
			sql.append("       ,MOD_NO                                               \n");
			sql.append("       ,REMARK                                               \n");
			sql.append("   )VALUES(                                                  \n");
			sql.append("        PROMISE_DOC_ID_S.NEXTVAL                             \n");
			sql.append("       ,'10'                                                 \n");
			sql.append("       ,?                                                    \n");
			sql.append("       ,?                                                    \n");
			sql.append("       ,?                                                    \n");
			sql.append("       ,?                                                    \n");
			sql.append("       ,?                                                    \n");
			sql.append("       ,?                                                    \n");
			sql.append("       ,?                                                    \n");
			sql.append("   )                                                         \n");
			
			pstmt = connection.prepareStatement(sql.toString());
			int paramIdx = 1;
			
			pstmt.setString(paramIdx++, sqlParams.get("PROMISE_DOC_TYPE_GB"));
			pstmt.setString(paramIdx++, params.get("SUB_CONT_MOD_ID"));
			pstmt.setString(paramIdx++, params.get("C_VENDCD"));
			pstmt.setString(paramIdx++, params.get("C_MEMBER_NAME"));
			pstmt.setString(paramIdx++, params.get("CON_DOC_NO"));
			pstmt.setString(paramIdx++, params.get("MOD_NO"));
			pstmt.setString(paramIdx++, sqlParams.get("REMARK"));
			
			pstmt.executeUpdate();
			return true;
		} catch (Exception e) {
			WAFLogger.error(e);
			throw new Exception(e);
		} finally {
			try {
				if(pstmt != null) pstmt.close();
			} catch(SQLException se) {
				WAFLogger.error(se);
			}
		}
	}
	
	private boolean updateData(Connection connection, ValueObject sqlParams) throws Exception {
		PreparedStatement pstmt = null;
		
		try {
			StringBuffer sql = new StringBuffer();
			sql.append("  UPDATE PROMISE_DOC_TYPE SET                           	 \n");
			sql.append("          PROMISE_DOC_TYPE_GB= ?                    \n");
			sql.append("         ,CONT_MOD_ID        = ?                    \n");
			sql.append("         ,CORP_REG_NO        = ?                    \n");
			sql.append("         ,CORP_NAME          = ?                    \n");
			sql.append("         ,CONT_NO            = ?                    \n");
			sql.append("         ,MOD_NO             = ?                    \n");
			sql.append("         ,REMARK             = ?                    \n");
			sql.append("  WHERE PROMISE_DOC_ID = ?                          	 \n");
			
			pstmt = connection.prepareStatement(sql.toString());
			int paramIdx = 1;
			
			pstmt.setString(paramIdx++, sqlParams.get("PROMISE_DOC_TYPE_GB"));
			pstmt.setString(paramIdx++, params.get("SUB_CONT_MOD_ID"));
			pstmt.setString(paramIdx++, params.get("C_VENDCD"));
			pstmt.setString(paramIdx++, params.get("C_MEMBER_NAME"));
			pstmt.setString(paramIdx++, params.get("CON_DOC_NO"));
			pstmt.setString(paramIdx++, params.get("MOD_NO"));
			pstmt.setString(paramIdx++, sqlParams.get("REMARK"));
			pstmt.setString(paramIdx++, sqlParams.get("PROMISE_DOC_ID"));
			//pstmt.setInt(paramIdx++, sqlParams.getInt("PROMISE_DOC_ID"));
			
			pstmt.executeUpdate();
			return true;
		} catch (Exception e) {
			WAFLogger.error(e);
			throw new Exception(e);
		} finally {
			try {
				if(pstmt != null) pstmt.close();
			} catch(SQLException se) {
				WAFLogger.error(se);
			}
		}
	}
	
	private boolean deleteData(Connection connection, ValueObject sqlParams) throws Exception {
		PreparedStatement pstmt = null;
		
		try {
			StringBuffer sql = new StringBuffer();
			sql.append("	DELETE FROM PROMISE_DOC_TYPE WHERE PROMISE_DOC_ID = ?			\n ");
			
			pstmt = connection.prepareStatement(sql.toString());
			int paramIdx = 1;
			pstmt.setInt(paramIdx++, sqlParams.getInt("PROMISE_DOC_ID"));
			
			pstmt.executeUpdate();
			return true;
		} catch (Exception e) {
			WAFLogger.error(e);
			throw new Exception(e);
		} finally {
			try {
				if(pstmt != null) pstmt.close();
			} catch(SQLException se) {
				WAFLogger.error(se);
			}
		}
	}
}
