package nc.bs.arap.plugin;

import nc.bs.framework.common.NCLocator;
import nc.itf.uap.IUAPQueryBS;
import nc.jdbc.framework.processor.BeanProcessor;
import nc.jdbc.framework.processor.ColumnProcessor;
import nc.vo.org.DeptVO;
import nc.vo.org.FinanceOrgVO;
import nc.vo.pu.m25.entity.InvoiceItemVO;

public class GetDataUtil {

	private static IUAPQueryBS iuap = (IUAPQueryBS) NCLocator.getInstance().lookup(IUAPQueryBS.class.getName());
	
	public static void getBillno(String billno)throws Exception {
		String sql = "select pk_payablebill from ap_payablebill where billno = '"+billno+"' and nvl(dr,0) = 0";
		String pk_payablebill = (String) iuap.executeQuery(sql, new ColumnProcessor());
		if (pk_payablebill != null) {
			throw new Exception("应付单单据号已存在!");
		}
	}
	
	// 用户 
	public static String getUserPK(String code) throws Exception {
		if("".equals(code)) {
			return null;
		}
		String sql = "select cuserid from sm_user where  cuserid = '"+code+"' and enablestate = '2' and nvl(dr,0) = 0";
		String cuserid = (String) iuap.executeQuery(sql, new ColumnProcessor());
		if (cuserid == null) {
			throw new Exception("获取用户信息失败,未找到与参数" + code + "有关的数据！");
		}
		return cuserid;
	}
	
	// 部门 
	public static String getDeptPK(String code) throws Exception {
		if("".equals(code)) {
			return null;
		}
		String sql = "select pk_dept from org_dept where  code = '"+code+"' and enablestate = '2' and nvl(dr,0) = 0";
		String pk_dept = (String) iuap.executeQuery(sql, new ColumnProcessor());
		if (pk_dept == null) {
			throw new Exception("获取部门信息失败,未找到与参数" + code + "有关的数据！");
		}
		return pk_dept;
	}
	
	// 部门版本
	public static String getDeptVPK(String code) throws Exception {
		if("".equals(code)) {
			return null;
		}
		String sql = "select pk_vid from org_dept_v where  code = '"+code+"' and enablestate = '2' and nvl(dr,0) = 0";
		String pk_vid = (String) iuap.executeQuery(sql, new ColumnProcessor());
		if (pk_vid == null) {
			throw new Exception("获取部门信息失败,未找到与参数" + code + "有关的数据！");
		}
		return pk_vid;
	}	
	
	
	// 人员
	public static String getPsndocPK(String code) throws Exception {
		if("".equals(code)) {
			return null;
		}
		String sql = "select pk_psndoc from sm_user where  cuserid = '"+code+"' and enablestate = '2' and nvl(dr,0) = 0";
		String pk_psndoc = (String) iuap.executeQuery(sql, new ColumnProcessor());
		if (pk_psndoc == null) {
			throw new Exception("获取人员信息失败,未找到与参数" + code + "有关的数据！");
		}
		return pk_psndoc;
	}
	
	
	// 业务流程
	public static String getBusitypePK(String code) throws Exception {
		String sql = "select pk_busitype from bd_busitype where  busicode = '"+code+"' and validity = '1' and nvl(dr,0) = 0";
		String pk_busitype = (String) iuap.executeQuery(sql, new ColumnProcessor());
		if (pk_busitype == null) {
			throw new Exception("获取业务流程信息失败,未找到与参数" + code + "有关的数据！");
		}
		return pk_busitype;
	}
	
	// 币种
	public static String getCurrtypePK(String code) throws Exception {
		String sql = "select pk_currtype from bd_currtype where  code = '"+code+"'and nvl(dr,0) = 0";
		String pk_currtype = (String) iuap.executeQuery(sql, new ColumnProcessor());
		if (pk_currtype == null) {
			throw new Exception("获取币种信息失败,未找到与参数" + code + "有关的数据！");
		}
		return pk_currtype;
	}
	//组织_业务单元_财务组织
	public static FinanceOrgVO getFinanceOrgVO(String code) throws Exception {
		String sql = "select * from org_financeorg where  code = '"+code+"'and nvl(dr,0) = 0";
		FinanceOrgVO financeOrgVO = (FinanceOrgVO) iuap.executeQuery(sql, new BeanProcessor(FinanceOrgVO.class));
		if (financeOrgVO == null) {
			throw new Exception("获取组织_业务单元_财务组织信息失败,未找到与参数" + code + "有关的数据！");
		}
		return financeOrgVO;
	}
	//组织_集团
	public static String getGroupPK(String code) throws Exception {
		String sql = "select pk_group from org_group where  code = '"+code+"' and enablestate = '2' and nvl(dr,0) = 0";
		String pk_group = (String) iuap.executeQuery(sql, new ColumnProcessor());
		if (pk_group == null) {
			throw new Exception("获取组织_集团信息失败,未找到与参数" + code + "有关的数据！");
		}
		return pk_group;
	}
	//单据类型
	public static String getBilltypePK(String code,String pk_group) throws Exception {
		String sql = "select pk_billtypeid from bd_billtype where  pk_billtypecode = '"+code+"' and pk_group = '"+pk_group+"' and nvl(dr,0) = 0";
		String pk_billtypeid = (String) iuap.executeQuery(sql, new ColumnProcessor());
		if (pk_billtypeid == null) {
			throw new Exception("获取单据类型信息失败,未找到与参数" + code + "有关的数据！");
		}
		return pk_billtypeid;
	}
	
	
	//国家地区
	public static String getCountryzonePK(String code) throws Exception {
		String sql = "select pk_country from bd_countryzone where  code  = '"+code+"' and nvl(dr,0) = 0";
		String pk_country = (String) iuap.executeQuery(sql, new ColumnProcessor());
		if (pk_country == null) {
			throw new Exception("获取国家地区信息失败,未找到与参数" + code + "有关的数据！");
		}
		return pk_country;
	}
	//物料
	public static String getMaterialPK(String code) throws Exception {
		if("".equals(code)) {
			return null;
		}
		String sql = "select pk_material from bd_material where  code  = '"+code+"' and enablestate = '2' and nvl(dr,0) = 0";
		String pk_material = (String) iuap.executeQuery(sql, new ColumnProcessor());
		if (pk_material == null) {
			throw new Exception("获取物料信息失败,未找到与参数" + code + "有关的数据！");
		}
		return pk_material;
	}
	//收支项目
	public static String getInoutbusiclassPK(String code) throws Exception {
		if("".equals(code)) {
			return null;
		}
		String sql = "select pk_inoutbusiclass from bd_inoutbusiclass where  code  = '"+code+"' and enablestate = '2' and nvl(dr,0) = 0";
		String pk_inoutbusiclass = (String) iuap.executeQuery(sql, new ColumnProcessor());
		if (pk_inoutbusiclass == null) {
			throw new Exception("获取收支项目信息失败,未找到与参数" + code + "有关的数据！");
		}
		return pk_inoutbusiclass;
	}
	
	
	
	//自定义档案
	public static String getDefdocPK(String code,String defdoclistcode,String name) throws Exception {
		if("".equals(code)) {
			return null;
		}
		String sql = "select pk_defdoc from bd_defdoc where pk_defdoclist = (select pk_defdoclist from bd_defdoclist where code = '"+defdoclistcode+"' and nvl(dr,0) = 0) and code  = '"+code+"' and enablestate = '2' and nvl(dr,0) = 0";
		String pk_defdoc = (String) iuap.executeQuery(sql, new ColumnProcessor());
		if (pk_defdoc == null) {
			throw new Exception("获取"+name+"信息失败,未找到与参数" + code + "有关的数据！");
		}
		return pk_defdoc;
	}
	
	//供应商
	public static String getSupplierPK(String code) throws Exception {
		if("".equals(code)) {
			return null;
		}
		String sql = "select pk_supplier from bd_supplier where  code  = '"+code+"' and enablestate = '2' and nvl(dr,0) = 0";
		String pk_supplier = (String) iuap.executeQuery(sql, new ColumnProcessor());
		if (pk_supplier == null) {
			throw new Exception("获取供应商信息失败,未找到与参数" + code + "有关的数据！");
		}
		return pk_supplier;
	}
	
	//银行账户
	public static String getBankaccsubPK(String accnum) throws Exception {
		if("".equals(accnum)) {
			return null;
		}
		String sql = "select pk_bankaccsub from bd_bankaccsub where  accnum  = '"+accnum+"' and nvl(dr,0) = 0";
		String pk_bankaccsub = (String) iuap.executeQuery(sql, new ColumnProcessor());
		if (pk_bankaccsub == null) {
			throw new Exception("获取银行账户信息失败,未找到与参数" + accnum + "有关的数据！");
		}
		return pk_bankaccsub;
	}	
	
	//结算方式
	public static String getBalatypePK(String code) throws Exception {
		String sql = "select pk_balatype from bd_balatype where  code  = '"+code+"' and enablestate = '2' and nvl(dr,0) = 0";
		String pk_balatype = (String) iuap.executeQuery(sql, new ColumnProcessor());
		if (pk_balatype == null) {
			throw new Exception("获取结算方式信息失败,未找到与参数" + code + "有关的数据！");
		}
		return pk_balatype;
	}
	
	//增值税税码税率
	public static String getTaxcodePK(String code) throws Exception {
		String sql = "select pk_taxcode from bd_taxcode where  code  = '"+code+"' and enablestate = '2' and nvl(dr,0) = 0";
		String pk_taxcode = (String) iuap.executeQuery(sql, new ColumnProcessor());
		if (pk_taxcode == null) {
			throw new Exception("获取税码信息失败,未找到与参数" + code + "有关的数据！");
		}
		return pk_taxcode;
	}
	//客户
	public static String getCustomerPK(String code) throws Exception {
		if("".equals(code)) {
			return null;
		}
		String sql = "select pk_customer from bd_customer where  code  = '"+code+"' and enablestate = '2' and nvl(dr,0) = 0";
		String pk_customer = (String) iuap.executeQuery(sql, new ColumnProcessor());
		if (pk_customer == null) {
			throw new Exception("获取客户信息失败,未找到与参数" + code + "有关的数据！");
		}
		return pk_customer;
	}	
	
	//项目
	public static String getprojectPK(String code) throws Exception {
		if("".equals(code)) {
			return null;
		}
		String sql = "select pk_project from bd_project where  project_code  = '"+code+"' and enablestate = '2' and nvl(dr,0) = 0";
		String pk_project = (String) iuap.executeQuery(sql, new ColumnProcessor());
		if (pk_project == null) {
			throw new Exception("获取项目信息失败,未找到与参数" + code + "有关的数据！");
		}
		return pk_project;
	}	
		
	//采购发票子表数据
	public static InvoiceItemVO getInvoice(String vbillcode,String crowno)throws Exception {
		if("".equals(vbillcode)) {
			return null;
		}
		String sql = "select * from po_invoice_b where pk_invoice = (select pk_invoice from po_invoice where vbillcode = '"+vbillcode+"' and nvl(dr,0) = 0) and crowno = '"+crowno+"' and nvl(dr,0) = 0";
		InvoiceItemVO invoiceItemVO = (InvoiceItemVO) iuap.executeQuery(sql, new BeanProcessor(InvoiceItemVO.class));
		if (invoiceItemVO == null) {
			throw new Exception("获取采购发票信息失败,未找到与参数" + vbillcode + ","+crowno+"有关的数据！");
		}
		return invoiceItemVO;
	}
	
	
}
