/**
 * @(#)file    NotiDetailBiz.java
 * @(#)author  gyeongmo an
 * @(#)date    2014.7.14
 * @(#)Description: 공지사항 상세
 *
 * Copyright (c) www.udapsoft.co.kr, Inc.
 * All rights reserved.
 */
package kr.co.udapsoft.ebid.buyer.center.center.biz;

import java.sql.Connection;

import kr.co.hsnc.common.base.WAFLogger;
import kr.co.hsnc.common.sql.Row;
import kr.co.hsnc.common.sql.RowSet;
import kr.co.hsnc.common.sql.RowSetImpl;
import kr.co.hsnc.common.sql.WAFSQLException;
import kr.co.hsnc.common.sql.persistent.Persistent;
import kr.co.hsnc.common.sql.persistent.PersistentImpl;
import kr.co.hsnc.common.sql.search.Search;
import kr.co.hsnc.common.sql.search.SearchImpl;
import kr.co.hsnc.common.sql.util.RowSetUtility;
import kr.co.hsnc.common.util.ValueObject;
import kr.co.udapsoft.ebid.buyer.common.file.FileManager;

import com.udapsoft.waf.system.Biz;
import com.udapsoft.waf.system.HandlerStorage;

public class NotiDetailBiz extends Biz {

	public NotiDetailBiz(HandlerStorage storage) {
		super(storage);
	}
	
	public void doBiz() throws Exception {
		ValueObject vo = new ValueObject();
    	boolean success =  false;
		if(storage.compareEvent("GET_NOTI_DETAIL")){
			String seq = params.get("seq");
			vo = getNotiDetail();
	        storage.addValueObject("notiDetailRowSet", vo);

	        RowSet rowSet = FileManager.getFileList("NOTI_DETAIL", seq);
	        
	        String str="";
	        for(int i=0;i<rowSet.size();i++){
	        	Row row=rowSet.getRow(i);
	        	str += (i+1)+" : "+row.get("FILE_NAME")+" ("+ row.get("FILE_SIZE")+")";
	        	if(i+1!=rowSet.size()){
	        		str +="\r\n";
	        	}
	        }
	        
	        vo.set("NOTI_DETAIL_FILE", str);
		} else if(storage.compareEvent("NEW_NOTI_DETAIL")){
			storage.addValueObject("notiDetailRowSet", vo);
		} else if(storage.compareEvent("SAVE_NOTI_DETAIL")){
			success = saveNoti(params);
			storage.setSuccess(success);
		} else if(storage.compareEvent("UPDATE_NOTI_DETAIL")){
			success = updateNoti(params);
			storage.setSuccess(success);
		} else if ( storage.compareEvent("DELETE_NOTI_DETAIL") ) {
			success = deleteNoti(params);
			storage.setSuccess(success);
		}
	}

	
	private ValueObject getNotiDetail() {
        Search search = new SearchImpl();
        RowSet rowSet = new RowSetImpl();
        StringBuffer sql = new StringBuffer();
        ValueObject vo = new ValueObject();
        try {
        	sql.append("	SELECT          		\n");
        	sql.append("		SEQ           		\n");
        	sql.append("		, NOTI_DIV    		\n");
        	sql.append("		, NOTI_DY    		\n");
        	sql.append("		, SUPP_OPEN_YN		\n");
        	sql.append("		, WRITE_NM    		\n");
        	sql.append("		, WRITE_ID    		\n");
        	sql.append("		, FILE_MAP_ID 		\n");
        	sql.append("		, TITLE       		\n");
        	sql.append("		, CONTENT     		\n");
        	sql.append("		, CRE_BY      		\n");
        	sql.append("		, CRE_DATE    		\n");
        	sql.append("		, UPD_BY      		\n");
        	sql.append("		, UPD_DATE    		\n");
        	sql.append("	FROM NOTI       		\n");
        	sql.append("	WHERE SEQ = '" + params.get("seq") + "'   		\n");
        	
        	search.setStatement(sql);
        	WAFLogger.debug(sql.toString());
            rowSet = search.execute();
            if(rowSet.size()>0){
                vo = RowSetUtility.getValueObject(rowSet.getRow(0));
            }
            return vo;
        }
        catch (WAFSQLException se) {
            super.doException(se);
            this.storage.setSuccess(false);
            this.storage.setMessage(se.getMessage());
            storage.setDetailMessage(se);
        }
        finally{
            if( vo == null) vo = new ValueObject();
            return vo;
        }
	}
	
	/**                                                                                                                                              
	 * 저장                                                                                                  
	 */ 
   public boolean saveNoti(ValueObject params) {
        String supp_open_yn_value 			= params.get("supp_open_yn_value");		// 업체공개여부
        String write_nm 				= params.get("write_nm");			// 작성자명
        String write_id 		= params.get("write_id");			// 작성자id
        String file_map_id 			= params.get("file_map_id");		// 첨부파일맵 id
        String title 	= params.get("noti_title");				// 제목
        String content 		= params.get("content");			// 내용
        String noti_dy 		= params.get("noti_dy");			// 공지일자
      
        Persistent persistent_i = null;
        StringBuffer sql   = new StringBuffer();
        
        sql.append("	INSERT INTO NOTI		\n");
        sql.append("	(               		\n");
        sql.append("		SEQ           		\n");
        sql.append("		, NOTI_DIV    		\n");
        sql.append("		, SUPP_OPEN_YN		\n");
        sql.append("		, WRITE_NM    		\n");
        sql.append("		, WRITE_ID    		\n");
        sql.append("		, FILE_MAP_ID 		\n");
        sql.append("		, TITLE       		\n");
        sql.append("		, CONTENT     		\n");
        sql.append("		, CRE_BY      		\n");
        sql.append("		, CRE_DATE    		\n");
        sql.append("		, NOTI_DY    		\n");
        sql.append("	) VALUES (      		\n");
        sql.append("		?		\n");
        sql.append("		,'1'            		\n");
        sql.append("		,?            		\n");
        sql.append("		,?            		\n");
        sql.append("		,?            		\n");
        sql.append("		,?            		\n");
        sql.append("		,?            		\n");
        sql.append("		,?            		\n");
        sql.append("		,?            		\n");
        sql.append("		,SYSDATE      		\n");
        sql.append("		,?      		\n");
        sql.append("	)               		\n");

        try {
            conn.begin();

            ValueObject voseq = getSeq(super.conn.getConnection());
            storage.addValueObject("VALUEOBJECT", voseq);
            
            persistent_i = new PersistentImpl(super.conn.getConnection());
            persistent_i.setStatement(sql.toString());
            
            persistent_i.addParameter(voseq.get("SEQ"));
            persistent_i.addParameter(supp_open_yn_value);
            persistent_i.addParameter(write_nm);
            persistent_i.addParameter(write_id);
            persistent_i.addParameter(file_map_id);
            persistent_i.addParameter(title);
            persistent_i.addParameter(content);
            persistent_i.addParameter(write_id);
            persistent_i.addParameter(noti_dy.replaceAll("-",""));
                    
            WAFLogger.debug(sql.toString());
            persistent_i.execute();
            persistent_i.clearParameters();
                
            return true;
        }
        catch(Exception e) {
            super.doException(e);
            super.storage.setMessage(e.getMessage());
            super.storage.setDetailMessage(e);
            super.conn.setRollback();
            return false;
        }
        finally {
            super.conn.end();
        }
    }
	
   /**                                                                                                                                              
	 * 수정                                                                                                  
	 */ 
  public boolean updateNoti(ValueObject params) {
      String supp_open_yn_value 			= params.get("supp_open_yn_value");		// 업체공개여부
      String write_nm 				= params.get("write_nm");			// 작성자명
      String write_id 		= params.get("write_id");			// 작성자id
      String file_map_id 			= params.get("file_map_id");		// 첨부파일맵 id
      String title 	= params.get("noti_title");				// 제목
      String content 		= params.get("content");			// 내용
      String seq 		= params.get("seq");				// 번호
      String noti_dy = params.get("noti_dy");	// 공지일자
      
     
       Persistent persistent_u = null;
       StringBuffer sql   = new StringBuffer();
       
       sql.append("	UPDATE NOTI SET       		\n");
       sql.append("		SUPP_OPEN_YN = ?  		\n");
       sql.append("		, WRITE_NM = ?      		\n");
       sql.append("		, WRITE_ID = ?      		\n");
       sql.append("		, FILE_MAP_ID = ?   		\n");
       sql.append("		, TITLE = ?         		\n");
       sql.append("		, CONTENT = ?       		\n");
       sql.append("		, UPD_BY = ?        		\n");
       sql.append("		, UPD_DATE = SYSDATE		\n");
       sql.append("		, NOTI_DY = ?		\n");
       sql.append("	WHERE SEQ = ?         		\n");
       
       try {
           conn.begin();

           persistent_u = new PersistentImpl(super.conn.getConnection());
           persistent_u.setStatement(sql.toString());
           
           persistent_u.addParameter(supp_open_yn_value);
           persistent_u.addParameter(write_nm);
           persistent_u.addParameter(write_id);
           persistent_u.addParameter(file_map_id);
           persistent_u.addParameter(title);
           persistent_u.addParameter(content);
           persistent_u.addParameter(write_id);
           persistent_u.addParameter(noti_dy.replaceAll("-",  ""));
           persistent_u.addParameter(seq);
           
           WAFLogger.debug(sql.toString());
           persistent_u.execute();
           persistent_u.clearParameters();
               
           return true;
       }
       catch(Exception e) {
           super.doException(e);
           super.storage.setMessage(e.getMessage());
           super.storage.setDetailMessage(e);
           super.conn.setRollback();
           return false;
       }
       finally {
           super.conn.end();
       }
   }
	
  /**                                                                                                                                              
   * 삭제                                                                                                  
   */ 
  public boolean deleteNoti(ValueObject params) {
	  String seq 		= params.get("seq");
	  
	  Persistent persistent_d = null;
	  StringBuffer sql   = new StringBuffer();
	  
	  sql.append("	DELETE FROM NOTI WHERE SEQ = ?	\n");

	  try {
		  conn.begin();
		  
		  persistent_d = new PersistentImpl(super.conn.getConnection());
		  persistent_d.setStatement(sql.toString());
		  
		  persistent_d.addParameter(seq);
		  
		  WAFLogger.debug(sql.toString());
		  persistent_d.execute();
		  persistent_d.clearParameters();
		  
		  return true;
	  }
	  catch(Exception e) {
		  super.doException(e);
		  super.storage.setMessage(e.getMessage());
		  super.storage.setDetailMessage(e);
		  super.conn.setRollback();
		  return false;
	  }
	  finally {
		  super.conn.end();
	  }
  }
	
	
  /**
	 * Seq 채번하기 (Persisitent 이용)
	 * @param 	
	 * @return	ValueObject
	 */
	public ValueObject getSeq(Connection conn) throws Exception {
	
		Persistent persistent_q = null;
		
		ValueObject vo = new ValueObject();
		ValueObject row = null;
		String sqlstr = "";
		
		try{
			persistent_q = new PersistentImpl(conn);
			sqlstr = "SELECT NOTI_S.NEXTVAL SEQ FROM DUAL";
			persistent_q.setStatement(sqlstr); 
			row = RowSetUtility.getValueObject(persistent_q.query().getRow(0));
			vo.set("seq", row.get("seq"));
			
		}catch(Exception e){
			WAFLogger.error(this.getClass().getName() + " --> getSeq() : " + e.getMessage());
			WAFLogger.error(e);
			throw e;
          
		}finally{
			if(vo == null) vo = new ValueObject();
		}
		
		return vo;
	}
	
	
	
}