package kr.co.kihyun.beans.batch;

import java.sql.*;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.io.PrintWriter;
import java.sql.PreparedStatement;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import kr.co.kihyun.lang.MString;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import kr.co.kihyun.db.DBManager;

public class UpdateNote  extends DBManager {

    private static final Logger LOG = LoggerFactory.getLogger(UpdateNote.class);

    private ResultSet rs = null;    
    private int count = 0;
    PreparedStatement pstmt = null;
    Statement stmt = null;
    PrintWriter out=null;
    private String cntId = null;
    private int tCount = 0;


    /******** tmpName 구해오기 ********/
    //46.메소드이름 생성규칙 위반 Update by YOUNGJUN,CHO  
    //public void ConfirmDeadLine(String uId, String docID,String docNum) {
    public void confirmDeadLine(String uId, String docID,String docNum) {
        StringBuilder sql = new StringBuilder(250);

        try {
            // res.setContentType("text/html;charset=UTF-8");
            // out = res.getWriter();

            String userID = uId;
            String ID = docID;
            String docIdNum = docNum;

            sql.append("SELECT ID FROM MOUMI_TOT_DOC WHERE END_DATE < SYSDATE AND ID ='"+docIdNum+"'");
            rs = execQuery(sql);

            if (rs != null) {
                rs.last();
                count = rs.getRow();
                rs.beforeFirst();
            } else {
               return;
            }

            tCount = count;
            cntId  = new String();

            for (int i = 0; rs.next(); i++) {
                cntId = rs.getString(1);
            }

            //System.out.println(sql);

        } catch (SQLException sqlEx) {
            LOG.error("\nSQLState - {}\nMySQL Error Code - {}\nmessage - {}\nsql - {}",
            new Object[] { sqlEx.getSQLState(), sqlEx.getErrorCode(), sqlEx.getMessage()});
            throw new RuntimeException(sqlEx);
        } catch (Exception ex) {
            throw new RuntimeException(ex);
        } finally {
            try {
                if (rs != null)
                    rs.close();
            } catch (SQLException ex) {
                //31.오류 상황 대응 부재_CWE-390 Add by YOUNGJUN,CHO
                ex.printStackTrace();
                //++++++++++++++++++++++++++++++++++++++++++++++++
            }
            try {
                if (stmt != null)
                    stmt.close();
            } catch (SQLException ex) {
                //31.오류 상황 대응 부재_CWE-390 Add by YOUNGJUN,CHO
                ex.printStackTrace();
                //++++++++++++++++++++++++++++++++++++++++++++++++
            }
            try {
                if (con != null)
                    con.close();
            } catch (SQLException ex) {
                //31.오류 상황 대응 부재_CWE-390 Add by YOUNGJUN,CHO
                ex.printStackTrace();
                //++++++++++++++++++++++++++++++++++++++++++++++++
            }
        }
    }
    //================================================
        
    //46.메소드이름 생성규칙 위반 Update by YOUNGJUN,CHO  
    //public void UpdateNote(HttpServletRequest req, HttpServletResponse res) {
    public void updateNote(HttpServletRequest req, HttpServletResponse res) {
        StringBuilder sql = new StringBuilder(250);           

        try {

            res.setContentType("text/html;charset=UTF-8");
            out = res.getWriter();
            
            String comment = MString.checkNull(req.getParameter("comment"),"null");
            String userID = req.getParameter("userID");
            String ID = req.getParameter("docID");
            //PRSS값 14로 업데이트 추가
            sql.append("UPDATE MOUMI_TOT_REPORT SET NOTE = '"+comment+"',PRSS=14 WHERE USER_ID = '"+userID+"' and ID = '"+ID+"'");
            //rs = execQuery(sql);
            execUpdate(sql.toString());

            //System.out.println(":::수정 쿼리::"+sql.toString());
         
        } catch (SQLException sqlEx) {
            LOG.error("\nSQLState - {}\nMySQL Error Code - {}\nmessage - {}\nsql - {}",
                    new Object[] { sqlEx.getSQLState(), sqlEx.getErrorCode(), sqlEx.getMessage(), sql });
            throw new RuntimeException(sqlEx);
        } catch (Exception ex) {
            throw new RuntimeException(ex);
        } finally {
            //close(rs);
            //execClose();
            try {
                if (rs != null)
                    rs.close();
            } catch (SQLException ex) {
                //31.오류 상황 대응 부재_CWE-390 Add by YOUNGJUN,CHO
                ex.printStackTrace();
                //++++++++++++++++++++++++++++++++++++++++++++++++
            }
            try {
                if (stmt != null)
                    stmt.close();
            } catch (SQLException ex) {
                //31.오류 상황 대응 부재_CWE-390 Add by YOUNGJUN,CHO
                ex.printStackTrace();
                //++++++++++++++++++++++++++++++++++++++++++++++++
            }
            try {
                if (con != null)
                    con.close();
            } catch (SQLException ex) {
                //31.오류 상황 대응 부재_CWE-390 Add by YOUNGJUN,CHO
                ex.printStackTrace();
                //++++++++++++++++++++++++++++++++++++++++++++++++
            }
        }
    }
    //================================================

    public int  getTcount(){
        return tCount;
    }

}
