package com.seeyon.apps.src_contract.lease.dao;

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

import com.seeyon.apps.src_contract.lease.util.BaseUtil;
import com.seeyon.ctp.common.exceptions.BusinessException;
import com.seeyon.ctp.util.JDBCAgent;

public class LeaseContractDaoImpl extends BaseUtil  implements ILeaseContractDao {

//	private String historyPayment = "select * from bipZS01.FCT_RECEIVEPLAN_VIEW where CONTRACTCODE = '${CONTRACTCODE1}' and ENDDATE<=(select max(ENDDATE) as ENDDATE from bipZS01.FCT_RECEIVEPLAN_VIEW where CONTRACTCODE = '${CONTRACTCODE2}' and RECEIVETOTALCOST != 0)";
//
//	private String daleteFZLease = "delete from FORMMAIN_3618 WHERE FIELD0001 = ?";
//	private String daleteFZLeaseSub = "delete from FORMSON_3619 WHERE FORMMAIN_ID in (SELECT ID FROM FORMMAIN_3618 WHERE FIELD0001 = ? )";
//	private String daleteFZLease = "delete from formmain_0046 WHERE FIELD0001 = ?";
//	private String daleteFZLeaseSub = "delete from FORMSON_0047 WHERE FORMMAIN_ID in (SELECT ID FROM formmain_0046 WHERE FIELD0001 = ? )";
	
	private String historyPayment = "select * from NCCDATA.FCT_RECEIVEPLAN_VIEW where CONTRACTCODE = '${CONTRACTCODE1}' and ENDDATE<=(select max(ENDDATE) as ENDDATE from NCCDATA.FCT_RECEIVEPLAN_VIEW where CONTRACTCODE = '${CONTRACTCODE2}' and RECEIVETOTALCOST != 0)";

	private String daleteFZLease = "delete from FORMMAIN_3710 WHERE FIELD0001 = ?";
	private String daleteFZLeaseSub = "delete from FORMSON_3711 WHERE FORMMAIN_ID in (SELECT ID FROM FORMMAIN_3710 WHERE FIELD0001 = ? )";
	
	
	@Override
	public int daleteFZLease(String contractCode) throws BusinessException, SQLException {
		int i = 0 ;
		JDBCAgent agent = new JDBCAgent();
		try {
			StringBuilder sql = new StringBuilder(daleteFZLease);
			List<Object> p = new ArrayList<Object>();
			p.add(contractCode);
			i=agent.execute(sql.toString(), p);
		}finally {
			if (agent != null) {
				agent.close();
			}
		}
		return i;
	}

	@Override
	public int daleteFZLeaseSub(String contractCode) throws BusinessException, SQLException {
		int i = 0 ;
		JDBCAgent agent = new JDBCAgent();
		try {
			StringBuilder sql = new StringBuilder(daleteFZLeaseSub);
//			StringBuilder sql = new StringBuilder("delete from formmain_0536 where id=?");
			List<Object> p = new ArrayList<Object>();
			p.add(contractCode);
			i=agent.execute(sql.toString(), p);
		}finally {
			if (agent != null) {
				agent.close();
			}
		}
		return i;
	}
	@Override
		public List<Map<String, Object>> historyPayment(String contractCode) {
		List<Map<String ,Object>> lists = new ArrayList<Map<String,Object>>();
		String sql = historyPayment;
		sql = sql.replace("${CONTRACTCODE1}", contractCode);
		sql = sql.replace("${CONTRACTCODE2}", contractCode);
		System.out.println(sql);
		Object[] params = new Object[] {};
		ResultSet rs = super.executeQuery(sql, params);
		try {
			while (rs.next()) {
				Map<String ,Object> map = new HashMap<String,Object>();
//				map.put("CONTRACTCODE", rs.getString("CONTRACTCODE"));
				map.put("STARTDATE", rs.getString("STARTDATE"));
				map.put("ENDDATE", rs.getString("ENDDATE"));
				
				map.put("PLANENDDATE", rs.getString("PLANENDDATE"));
				map.put("PLANTOTALCOST", rs.getString("PLANTOTALCOST"));
				map.put("PLANRENTFEE", rs.getString("PLANRENTFEE"));
				map.put("PLANMANAGEFEE", rs.getString("PLANMANAGEFEE"));
				
				map.put("DETAILTOTALCOST", rs.getString("DETAILTOTALCOST"));
				map.put("DETAILRENT", rs.getString("DETAILRENT"));
				map.put("DETAILMANAGESERVICEFEE", rs.getString("DETAILMANAGESERVICEFEE"));
				map.put("DETAILMARKETFEE", rs.getString("DETAILMARKETFEE"));
				
				map.put("DERATETOTALCOST", rs.getString("DERATETOTALCOST"));
				map.put("DERATERENT", rs.getString("DERATERENT"));
				map.put("DERATEMANAGESERVICEFEE", rs.getString("DERATEMANAGESERVICEFEE"));
				map.put("DERATEMARKETFEE", rs.getString("DERATEMARKETFEE"));
				
				map.put("INCREASETOTALCOST", rs.getString("INCREASETOTALCOST"));
				map.put("INCREASERENT", rs.getString("INCREASERENT"));
				map.put("INCREASEMANAGESERVICEFEE", rs.getString("INCREASEMANAGESERVICEFEE"));
				map.put("INCREASEMARKETFEE", rs.getString("INCREASEMARKETFEE"));
				
				map.put("RECEIVETOTALCOST", rs.getString("RECEIVETOTALCOST"));
				map.put("REMAINTOTALCOST", rs.getString("REMAINTOTALCOST"));
				System.out.println(map);
				lists.add(map);
			}
		} catch (SQLException e) {
			e.printStackTrace();
		}finally {
			super.close();
		}
		return lists;
	}
}