package com.seeyon.apps.src_financing.dao;


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

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

public class FinancingContractDaoImpl implements IFinancingContractDao {

//	本地
	private String daleteFZFinancing = "delete from formmain_0097 WHERE field0002 = ?";
	private String daleteFZFinancingSub = "delete from formson_0098 WHERE FORMMAIN_ID in (SELECT ID FROM formmain_0097 WHERE field0002 = ? )";
	private String getLXFinancingSub = "select id from formson_0091 WHERE FORMMAIN_ID in (SELECT ID FROM formmain_0087 WHERE field0001 = ? )";
//	测试
//	private String daleteFZFinancing = "delete from formmain_0019 WHERE field0002 = ?";
//	private String daleteFZFinancingSub = "delete from formson_0020 WHERE FORMMAIN_ID in (SELECT ID FROM formmain_0019 WHERE field0002 = ? )";
//	正式
//	private String daleteFZFinancing = "delete from formmain_0760 WHERE field0002 = ?";
//	private String daleteFZFinancingSub = "delete from formson_0761 WHERE FORMMAIN_ID in (SELECT ID FROM formmain_0760 WHERE field0002 = ? )";



	
	@Override
	public int daleteFZFinancing(String contractCode) throws BusinessException, SQLException {
		int i = 0 ;
		JDBCAgent agent = new JDBCAgent();
		try {
			StringBuilder sql = new StringBuilder(daleteFZFinancing);
			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 daleteFZFinancingSub(String contractCode) throws BusinessException, SQLException {
		int i = 0 ;
		JDBCAgent agent = new JDBCAgent();
		try {
			StringBuilder sql = new StringBuilder(daleteFZFinancingSub);
//			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<String> getLXFinancingSubId(String contractCode) throws BusinessException, SQLException {
		List<String> lists = new ArrayList<String>();
		JDBCAgent agent = new JDBCAgent();
		try {
			StringBuilder sql = new StringBuilder(getLXFinancingSub);
			List<Object> p = new ArrayList<Object>();
			p.add(contractCode);
			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 map = list.get(i);
					Object object = map.get("id");
					lists.add(object.toString());
				}
			}
		}finally {
			if (agent != null) {
				agent.close();
			}
		}
		return lists;
	}


}