/**
 * @(#)file   PricesysListSaveBiz.java
 * @(#)author  TC40335
 * @(#)date    2024.10.16
 *
 * Copyright (c) www.udapsoft.co.kr, Inc.
 * All rights reserved.
 */

 package kr.co.udapsoft.ebid.buyer.cont.purchase.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 PricesysListSaveBiz extends Biz {
	
	public PricesysListSaveBiz(HandlerStorage storage) {
		super(storage);
	}

	public void doBiz() throws Exception {
		if (storage.compareEvent("SAVE_PRICESYS_LIST")) {
			
			boolean success = false;
			String[] status = super.gets("sheet4.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("SHEET4", params, i));
					}
					
					if (IBSheet7Utils.equalStatusUpdate(status[i])) {
						success = updateData(super.conn.getConnection(), IBSheet7Utils.getIBSheet7SqlParams("SHEET4", params, i));
					}
					
					if (IBSheet7Utils.equalStatusDelete(status[i])) {
						FileManager fileManager = new FileManager();
		            	fileManager.delFileMapProc(conn.getConnection(), "PUR_PRICESYS_DOC", IBSheet7Utils.getIBSheet7SqlParams("SHEET4", params, i).get("PRICE_SYS_ID"));
						success = deleteData(super.conn.getConnection(), IBSheet7Utils.getIBSheet7SqlParams("SHEET4", 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;
		System.out.println(sqlParams);
		System.out.println(params);
		
		try {
			StringBuffer sql = new StringBuffer();
			
			sql.append("   INSERT INTO PRICE_SYS(                             		 \n");
			sql.append("        PRICE_SYS_ID                                       	 \n");
			sql.append("       ,PRICE_SYS_YN                                     	 \n");
			sql.append("       ,OBJECT_NM	                                     	 \n");
			sql.append("       ,ITEM_NM		                                     	 \n");
			sql.append("       ,SYS_REASON	                                     	 \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("   )VALUES(                                                  \n");
			sql.append("        PRICE_SYS_ID_S.NEXTVAL                             	 \n");
			sql.append("       ,?                                                 	 \n");
			sql.append("       ,?                                                    \n");
			sql.append("       ,?                                                    \n");
			sql.append("       ,?                                                    \n");
			sql.append("       ,?                                                    \n");
			sql.append("       ,?                                                    \n");
			sql.append("       ,(SELECT MEMBER_NAME FROM CONT_PUR_SUPP WHERE PUR_CONT_MOD_ID = ? AND SIGN_SEQ = 2)  \n");
			sql.append("       ,(SELECT ORD_DOC_NO FROM PUR2000T WHERE PUR_CONT_MOD_ID = ?)               \n");
			sql.append("       ,?                                                    \n");
			sql.append("   )                                                         \n");
			
			pstmt = connection.prepareStatement(sql.toString());
			int paramIdx = 1;
			
			pstmt.setString(paramIdx++, sqlParams.get("PRICE_SYS_YN"));
			pstmt.setString(paramIdx++, sqlParams.get("OBJECT_NM"));
			pstmt.setString(paramIdx++, sqlParams.get("ITEM_NM"));
			pstmt.setString(paramIdx++, sqlParams.get("SYS_REASON"));
			pstmt.setString(paramIdx++, params.get("PUR_CONT_MOD_ID"));
			pstmt.setString(paramIdx++, params.get("C_VENDCD"));
			pstmt.setString(paramIdx++, params.get("PUR_CONT_MOD_ID"));
			pstmt.setString(paramIdx++, params.get("PUR_CONT_MOD_ID"));
			pstmt.setString(paramIdx++, params.get("MOD_NO"));
			
			
			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 PRICE_SYS SET                           	\n");
			sql.append("         ,PRICE_SYS_YN= ?                    		\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("  WHERE PRICE_SYS_ID = ?                          	\n");
			
			pstmt = connection.prepareStatement(sql.toString());
			int paramIdx = 1;
			
			pstmt.setString(paramIdx++, sqlParams.get("PRICE_SYS_GB"));
			pstmt.setString(paramIdx++, params.get("PUR_CONT_MOD_ID"));
			pstmt.setString(paramIdx++, params.get("C_VENDCD"));
			pstmt.setString(paramIdx++, params.get("C_MEMBER_NAME"));
			pstmt.setString(paramIdx++, params.get("ORD_DOC_NO"));
			pstmt.setString(paramIdx++, params.get("MOD_NO"));
			pstmt.setString(paramIdx++, sqlParams.get("PRICE_SYS_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 PRICE_SYS WHERE PRICE_SYS_ID = ?			\n ");
			
			pstmt = connection.prepareStatement(sql.toString());
			int paramIdx = 1;
			pstmt.setInt(paramIdx++, sqlParams.getInt("PRICE_SYS_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);
			}
		}
	}
}
