package com.seeyon.apps.src_automatic.dao;

import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import org.apache.commons.collections.map.HashedMap;

import com.seeyon.ctp.common.exceptions.BusinessException;
import com.seeyon.ctp.util.JDBCAgent;

public class SendNoteMsgDaoImpl implements ISendNoteMsgDao {

	@Override
	public List<Map<String,Object>> findNoteByDate(String date) throws BusinessException, SQLException {
		List<Map<String,Object>> lists = new ArrayList<Map<String,Object>>();
		JDBCAgent agent = new JDBCAgent();
		try {
			StringBuilder sql = new StringBuilder(findNoteByDate);
			List<Object> p = new ArrayList<Object>();
			p.add(date);
			agent.execute(sql.toString(), p);
			List<Map> list = agent.resultSetToList();
			if (list != null && list.size() > 0) {
				for(int i = 0 ; i < list.size() ; i ++) {
					lists.add(list.get(i));
				}
			}
		}finally {
			if (agent != null) {
				agent.close();
			}
		}
		return lists;
	}
	
	@Override
	public List<Map<String,Object>> findNotesByFormId(String id) throws BusinessException, SQLException {
		List<Map<String,Object>> lists = new ArrayList<Map<String,Object>>();
		JDBCAgent agent = new JDBCAgent();
		try {
			StringBuilder sql = new StringBuilder(findNotesByFormId);
			List<Object> p = new ArrayList<Object>();
			p.add(id);
			agent.execute(sql.toString(), p);
			List<Map> list = agent.resultSetToList();
			if (list != null && list.size() > 0) {
				for(int i = 0 ; i < list.size() ; i ++) {
					Map<String, Object> map = new HashedMap();
					map.put("content", list.get(i).get("field0027"));
					map.put("recipient", list.get(i).get("field0028"));
					map.put("phone", list.get(i).get("field0029"));
					map.put("id", list.get(i).get("id"));
					lists.add(map);
				}
			}
		}finally {
			if (agent != null) {
				agent.close();
			}
		}
		return lists;
	}
	
	
	@Override
	public int updateNotesbySucceed(String result,String id) throws BusinessException, SQLException {
		int i = 0 ;
		JDBCAgent agent = new JDBCAgent();
		try {
			StringBuilder sql = new StringBuilder(updateNotesbySucceed);
//			StringBuilder sql = new StringBuilder("update formmain_0536 set field0013 = ? where id=?");
			List<Object> p = new ArrayList<Object>();
			p.add(result);
			p.add(id);
			i=agent.execute(sql.toString(), p);
		}finally {
			if (agent != null) {
				agent.close();
			}
		}
		return i;
	}
	
	@Override
	public int updateNotesbyDefeated(String result,String desc,String id) throws BusinessException, SQLException {
		int i = 0 ;
		JDBCAgent agent = new JDBCAgent();
		try {
			StringBuilder sql = new StringBuilder(updateNotesbyDefeated);
//			StringBuilder sql = new StringBuilder("update formmain_0536 set field0013 = ? where id=?");
			List<Object> p = new ArrayList<Object>();
			p.add(result);
			p.add(desc);
			p.add(id);
			i=agent.execute(sql.toString(), p);
		}finally {
			if (agent != null) {
				agent.close();
			}
		}
		return i;
	}

//private String findNoteByDate = "select id,field0002 from formmain_0183 where start_date like ? and field0014 = '-7272450028438992115'";
//private String findNotesByFormId = "select id,field0027,field0028,field0029 from formson_0184 where formmain_id = ? ";
//private String updateNotesbySucceed = "update formson_0184 set field0034 = ? where id=? ";
//private String updateNotesbyDefeated = "update formson_0184 set field0034 = ?,field0036 = ? where id=? ";

private String findNoteByDate = "select id,field0002 from formmain_3045 where field0032 = to_date(?,'yyyy-mm-dd') and field0014 = '-658637500406938218'";
private String findNotesByFormId = "select id,field0027,field0028,field0029 from formson_3046 where formmain_id = ?";
private String updateNotesbySucceed = "update formson_3046 set field0034 = ? where id=? ";
private String updateNotesbyDefeated = "update formson_3046 set field0034 = ?,field0036 = ? where id=? ";



	
}
