/**
 * @(#)file    OwnListSaveBiz.java
 * @(#)author  gyeongmo an
 * @(#)date    2014.7.2
 * Copyright (c) www.udapsoft.co.kr, Inc.
 */
package kr.co.udapsoft.ebid.buyer.sycs.own.biz;

import kr.co.hsnc.common.base.WAFLogger;
import kr.co.hsnc.common.sql.RowSet;
import kr.co.hsnc.common.sql.RowSetImpl;
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 com.udapsoft.waf.system.Biz;
import com.udapsoft.waf.system.HandlerStorage;

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

	public void doBiz() throws Exception {
        boolean success = false;
        
        System.out.println("=================wefsdfdsfsdfdsfsdfs====================");
        
        try 
        {
            conn.begin();
            
            ValueObject check_id = new ValueObject();
            
            System.out.println("------------------->" + params.get("p_my_user_id"));
            
            check_id  = getSRegId(params);
            if(check_id.get("cnt").equals("0")) //2015.12.11
            {
            	success = save_info(params); 
            }
            else
            {
                success = false;
            }
            
            //success = save_info(params); 
                
            if(success == true)
            {
                String message = "등록성공";
                ValueObject errMsg = new ValueObject();
                errMsg.set("errMsg", message);
                storage.addValueObject("VALUEOBJECT", errMsg);
            }
            else
		    {
		        String message = "동일한 담당자명이 존재합니다.";
                ValueObject errMsg = new ValueObject();
                errMsg.set("errMsg", message);
                storage.addValueObject("VALUEOBJECT", errMsg);
		    }
            
            super.storage.setSuccess(success);
            
        } catch (Exception e) {
            System.out.println("e======>" + e);
            e.printStackTrace();
            conn.setRollback();
        } finally {
            conn.end();
        }
	}
	
	
	
	/**
     * 
     * 담당자 수정
     */
    private boolean save_info(ValueObject params) throws Exception
    {
        
        Persistent persistent = null;
        StringBuffer sqlstr = new StringBuffer();
        
        
        try{
            
                persistent = new PersistentImpl(super.conn.getConnection());
            
                sqlstr.append("             UPDATE SUPP_CHARGE SET                  \n")
                        .append("                   USER_NAME  = ?              \n")
                        .append("                 , PASSWD   = F_SHA256(?)         \n") //SHA256
                        .append("                 , DIVISION   = ?         \n")
                        .append("                 , POSITION  = ?       \n")
                        .append("                 , TEL_NUM   = ?      \n")
                        .append("                 , FAX_NUM  = ?       \n")
                        .append("                 , HP1    = ?                      \n")
                        .append("                 , HP2    = ?                      \n")
                        .append("                 , HP3    = ?                      \n")
                        .append("                 , EMAIL   = ?                                     \n")
                        .append("                 , UPD_BY   = ?                                     \n")
                        .append("                 , UPD_DATE   = SYSDATE                                     \n")
                        .append("                 WHERE USER_ID= ?                                                                                                    \n");
                        
            
                        persistent.setStatement(sqlstr.toString());
                        persistent.addParameter(params.get("p_user_name"));
                        persistent.addParameter(params.get("p_passwd"));
                        persistent.addParameter(params.get("p_division"));
                        persistent.addParameter(params.get("p_position"));
                        persistent.addParameter(params.get("p_tel_num"));
                        persistent.addParameter(params.get("p_fax_num"));
                        persistent.addParameter(params.get("p_hp1"));
                        persistent.addParameter(params.get("p_hp2"));
                        persistent.addParameter(params.get("p_hp3"));
                        persistent.addParameter(params.get("p_email"));
                        persistent.addParameter(params.get("user_id"));
                        persistent.addParameter(params.get("user_id"));
                                                
                        
                        System.out.println(persistent.getParameters());
                        System.out.println(sqlstr.toString());
                        persistent.execute();
                        persistent.clearParameters();
                        
                        return true;
            
        } catch(Exception e) {
            System.out.println("e===>" +e);
            e.printStackTrace();
            return false;            
        }
    }
    
    public ValueObject getSRegId(ValueObject params){
        Search search = new SearchImpl();
        ValueObject vo = null;
        RowSet rowSet = new RowSetImpl();
        
        StringBuffer sqlstr = new StringBuffer();
        
        
        try{
            
            sqlstr.append("      SELECT                                        \n")
                  .append("              COUNT(*) CNT                          \n")
                  .append("            FROM SUPP_CHARGE                        \n")
            	  .append("         WHERE (USER_ID IS NULL OR USER_ID  <> ?)   \n")
                  .append("           AND (CORP_ID = ? AND USER_NAME  = ? )    \n");
             
                   
            search.setStatement(sqlstr.toString());
            search.addParameter(params.get("user_id").trim());
            search.addParameter(params.get("p_corp_id").trim());
            search.addParameter(params.get("p_user_name").trim());
            
            rowSet = search.execute();
            
//            System.out.println(params);
//            System.out.println(sqlstr.toString());
//            System.out.println(search.getParameters());
            
            if(rowSet.size()>0){
                vo = RowSetUtility.getValueObject(rowSet.getRow(0));
            }
            
        }catch(Exception e){
        	 System.out.println(e.getMessage());
             e.printStackTrace();
            throw e;
        }finally{
            if( vo == null) vo = new ValueObject();
            return vo;
        }
    }

}
