package com.seeyon.apps.src_ydctleasebill.dao.impl;

import com.seeyon.apps.src_ydctleasebill.dao.ILeaseBillDao;
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 LeaseBillDaoImpl implements ILeaseBillDao {
//本地
    private String leaseBillByCode = "SELECT id FROM formmain_0129 WHERE field0001 = ? ";
    private String deleteBillById = "delete from formmain_0129 where id=?";
    private String deleteBillsByBillId = "delete from formson_0130 where formmain_id=?";
//    正式
//    private String leaseBillByCode = "SELECT id FROM formmain_1062 WHERE field0004 = ? ";
//    private String deleteBillById = "delete from formmain_1062 where id=?";
//    private String deleteBillsByBillId = "delete from formson_1182 where formmain_id=?";


    @Override
    public List<String> leaseBillByCode(String code) throws BusinessException, SQLException {
        List<String> lists = new ArrayList<String>();
        JDBCAgent agent = new JDBCAgent();
            try {
            StringBuilder sql = new StringBuilder(leaseBillByCode);
//			StringBuilder sql = new StringBuilder("SELECT id FROM formmain_0536 WHERE field0013 = ? ");
            List<Object> p = new ArrayList<Object>();
            p.add(code);
            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;
    }

    @Override
    public int deleteBillById(String id) throws BusinessException, SQLException {
        int i = 0 ;
        JDBCAgent agent = new JDBCAgent();
        try {
            StringBuilder sql = new StringBuilder(deleteBillById);
//			StringBuilder sql = new StringBuilder("delete from formmain_0536 where id=?");
            List<Object> p = new ArrayList<Object>();
            p.add(id);
            i=agent.execute(sql.toString(), p);
        }finally {
            if (agent != null) {
                agent.close();
            }
        }
        return i;
    }

    @Override
    public int deleteBillsByBillId(String formmainId) throws BusinessException, SQLException {
        int i = 0 ;
        JDBCAgent agent = new JDBCAgent();
        try {
            StringBuilder sql = new StringBuilder(deleteBillsByBillId);
//			StringBuilder sql = new StringBuilder("delete from formson_0538 where formmain_id=?");
            List<Object> p = new ArrayList<Object>();
            p.add(formmainId);
            i=agent.execute(sql.toString(), p);
        }finally {
            if (agent != null) {
                agent.close();
            }
        }
        return i;
    }
}
