package nc.bs.pu.plugin.DataUtils;

import java.util.HashMap;
import java.util.Map;

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.jdbc.framework.processor.MapProcessor;
import nc.vo.bd.material.MaterialVO;
import nc.vo.ct.purdaily.entity.CtPuBVO;
import nc.vo.org.DeptVO;
import nc.vo.org.FinanceOrgVO;
import nc.vo.org.PurchaseOrgVO;
import nc.vo.pu.m25.entity.InvoiceItemVO;

public class QueryDataUtil {

	private static IUAPQueryBS iuap = (IUAPQueryBS) NCLocator.getInstance().lookup(IUAPQueryBS.class.getName());
	
	public static void getBillno(String billno)throws Exception {
		String sql = "select pk_order from po_order where vbillcode = '"+billno+"' and nvl(dr,0) = 0";
		String pk_order = (String) iuap.executeQuery(sql, new ColumnProcessor());
		if (pk_order != 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 PurchaseOrgVO getFinanceOrgVO(String code) throws Exception {
		String sql = "select * from org_purchaseorg where  code = '"+code+"'and nvl(dr,0) = 0";
		PurchaseOrgVO purchaseOrgVO = (PurchaseOrgVO) iuap.executeQuery(sql, new BeanProcessor(PurchaseOrgVO.class));
		if (purchaseOrgVO == null) {
			throw new Exception("获取组织_业务单元_采购组织信息失败,未找到与参数" + code + "有关的数据！");
		}
		return purchaseOrgVO;
	}
	//组织_集团
	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) throws Exception {
		if("".equals(code)) {
			return null;
		}
		String sql = "select pk_billtypeid from bd_billtype where  pk_billtypecode = '"+code+"' 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 MaterialVO getMaterialPK(String code) throws Exception {
		if("".equals(code)) {
			return null;
		}
		String sql = "select * from bd_material where  code  = '"+code+"' and enablestate = '2' and nvl(dr,0) = 0";
		MaterialVO materialVO = (MaterialVO) iuap.executeQuery(sql, new BeanProcessor(MaterialVO.class));
		if (materialVO == null) {
			throw new Exception("获取物料信息失败,未找到与参数" + code + "有关的数据！");
		}
		return materialVO;
	}
	//收支项目
	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,String pk_supplier) throws Exception {
		if("".equals(accnum) || "".equals(pk_supplier)) {
			return null;
		}
		String sql = "select pk_bankaccsub  from bd_bankaccsub where accnum = '"+accnum+"' and pk_bankaccbas in(select pk_bankaccbas from bd_custbank where pk_cust = '"+pk_supplier+"') and fronzenstate  = '0'";
		String pk_bankaccsub = (String) iuap.executeQuery(sql, new ColumnProcessor());
		if (pk_bankaccsub == null) {
			throw new Exception("获取银行账户信息失败,未找到与参数" + accnum + "有关的数据！");
		}
		return pk_bankaccsub;
	}
	
	//银行账户开户行
	public static String getBankdocName(String accnum,String pk_supplier) throws Exception {
		if("".equals(accnum) || "".equals(pk_supplier)) {
			return null;
		}
		String sql = "select name from bd_bankdoc where pk_bankdoc = (select pk_bankdoc  from bd_bankaccbas where accnum = '"+accnum+"' and pk_bankaccbas in(select pk_bankaccbas from bd_custbank where pk_cust = '"+pk_supplier+"') and accstate  = 0) and enablestate = 2";
		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 String getMeasdocPK(String code) throws Exception {
		String sql = "select pk_measdoc from bd_measdoc where code = '"+code+"'";
		String pk_measdoc = (String) iuap.executeQuery(sql, new ColumnProcessor());
		if (pk_measdoc == null) {
			throw new Exception("获取计量单位信息失败,未找到与参数" + code + "有关的数据！");
		}
		return pk_measdoc;
	}
		
	
	
	//采购合同子表数据
	public static CtPuBVO getContract(String vbillcode,String crowno)throws Exception {
		if("".equals(vbillcode)) {
			return null;
		}
		String sql = "select * from ct_pu_b where pk_ct_pu = (select pk_ct_pu from ct_pu where vbillcode = '"+vbillcode+"' and nvl(dr,0) = 0) and crowno = '"+crowno+"' and nvl(dr,0) = 0";
		CtPuBVO ctPuBVO = (CtPuBVO) iuap.executeQuery(sql, new BeanProcessor(CtPuBVO.class));
		if (ctPuBVO == null) {
			throw new Exception("获取采购合同信息失败,未找到与参数" + vbillcode + ","+crowno+"有关的数据！");
		}
		return ctPuBVO;
	}
	
	
	//查询源头和来源数据
	public static Map<String,String> getDataMap(String type,String vbillcode,String crowno)throws Exception {
		if("".equals(vbillcode)) {
			return null;
		}
		Map<String,String> map = new HashMap<String,String>();
		String sql = "";
		
		if("20".equals(type)) {
			sql = "select pk_praybill as pk_id,pk_praybill_b as pk_id_b from po_praybill_b where pk_praybill = (select pk_praybill from po_praybill where vbillcode = '"+vbillcode+"' and nvl(dr,0) = 0) and crowno = '"+crowno+"' and nvl(dr,0) = 0";
			map = (Map<String, String>) iuap.executeQuery(sql.toString(), new MapProcessor());
			if(map == null) {
				throw new Exception("获取请购单信息失败,未找到与参数" + vbillcode + ","+crowno+"有关的数据！");
			}
		}else if("Z2".equals(type)) {
			sql = "select pk_ct_pu as pk_id,pk_ct_pu_b as pk_id_b from ct_pu_b where pk_ct_pu = (select pk_ct_pu from ct_pu where vbillcode = '"+vbillcode+"' and nvl(dr,0) = 0) and crowno = '"+crowno+"' and nvl(dr,0) = 0";
			map = (Map<String, String>) iuap.executeQuery(sql.toString(), new MapProcessor());
			if(map == null) {
				throw new Exception("获取采购合同信息失败,未找到与参数" + vbillcode + ","+crowno+"有关的数据！");
			}
		}else if("21".equals(type)) {
			sql = "select pk_order as pk_id,pk_order_b as pk_id_b from po_order_b where pk_order = (select pk_order  from po_order where vbillcode = '"+vbillcode+"' and nvl(dr,0) = 0) and crowno = '"+crowno+"' and nvl(dr,0) = 0";
			map = (Map<String, String>) iuap.executeQuery(sql.toString(), new MapProcessor());
			if(map == null) {
				throw new Exception("获取采购订单信息失败,未找到与参数" + vbillcode + ","+crowno+"有关的数据！");
			}
			
		}else if("45".equals(type)) {
			sql = "select cgeneralhid as pk_id,cgeneralbid as pk_id_b from ic_purchasein_b where cgeneralhid = (select cgeneralhid from vbillcode = '"+vbillcode+"' and nvl(dr,0) = 0) and crowno = '"+crowno+"' and nvl(dr,0) = 0";
			map = (Map<String, String>) iuap.executeQuery(sql.toString(), new MapProcessor());
			if(map == null) {
				throw new Exception("获取采购入库单信息失败,未找到与参数" + vbillcode + ","+crowno+"有关的数据！");
			}
		}
		return map;
	}
	
	
}
