package com.steering.pss.sale.base;

import java.sql.CallableStatement;
import java.sql.SQLException;
import java.util.ArrayList;

import CoreFS.SA01.CoreException;
import CoreFS.SA06.CoreReturnObject;
import CoreFS.SA08.CoreXmlSqlParsersFactory;
import CoreFS.SC01.CoreConstantData;

import com.steering.comm.db.DBCommon;
import com.steering.comm.tools.StringTool;

public class CoreBuyerBase extends DBCommon {
	/**
	 * 获取区域属性
	 * 
	 * @param name
	 * @return
	 */
	public CoreReturnObject GetQueryCustmright(String org, String customerName,
			String flg) {
		String sql = "SELECT mer.customer_no,PY,USER_GRP,mer.MARKET_SYS, customer_nm, (select BASENAME from com_base_info where BASECODE = mer.BIGAREA) BIGAREA,(SELECT REGION_NM from sel_admin_region where REGION_NO = mer.region_no) region_Name,region_no, abrvition, org,"
				+ " decode(validflag,'1','True','False') validflag, "
                + " create_name,to_char(create_time,'yyyy-MM-dd HH24:mi:ss') create_time, "
                + " update_name,to_char(update_time,'yyyy-MM-dd HH24:mi:ss') update_time, "
                + " delete_name,to_char(delete_time,'yyyy-MM-dd HH24:mi:ss') delete_time, "
				+ " (SELECT distinct custm_use"
				+ "  FROM slm_base_custm_use" + " WHERE sale_org IN("
				+ org
				+ ")  AND customer_no = mer.customer_no"
				+ "  "
				+ flg
				+ "  AND custm_use IN ('100202')) dingjie27,"
				+ " (SELECT distinct custm_use"
				+ "  FROM slm_base_custm_use"
				+ " WHERE sale_org IN ("
				+ org
				+ ")"
				+ "   AND customer_no = mer.customer_no"
				+ "   "
				+ flg
				+ "  AND custm_use IN ('100203')) sh27,"
				+ " (SELECT decode(max(custm_use),'','false','true')"
				+ "  FROM slm_base_custm_use" + " WHERE sale_org IN ("
				+ org
				+ ")"
				+ "  AND customer_no = mer.customer_no"
				+ "  "
				+ flg
				+ "  AND custm_use IN ('100202')) dingjie,"
				+ " (SELECT decode(max(custm_use),'','false','true')"
				+ "  FROM slm_base_custm_use"
				+ " WHERE sale_org in("
				+ org
				+ ")"
				+ "   AND customer_no = mer.customer_no"
				+ "   "
				+ flg
				+ "  AND custm_use IN ('100203')) sh,"
				+ " (SELECT decode(max(custm_use),'','false','true')"
				+ "  FROM slm_base_custm_use"
				+ " WHERE sale_org in("
				+ org
				+ ")"
				+ "   AND customer_no = mer.customer_no"
				+ "   "
				+ flg
				+ "  AND custm_use IN ('100204')) USERBOS"
				+ " FROM slm_base_customer mer"
				+ " WHERE customer_no IN (SELECT customer_no"
				+ "  FROM slm_base_custm_right"
				+ "  WHERE sale_org in("
				+ org
				+ ") ) " + customerName + " order by mer.customer_no ";
		return this.getData(sql);
	}

	/**
	 * 查询收货单位
	 * 
	 * @return CoreReturnObject
	 */
	public CoreReturnObject getBaseReceiveUnit(String customerName, String strValidFlag,
			String sale_org) {
		String sql = CoreXmlSqlParsersFactory.getSql("CoreBuyerOrder.getBaseReceiveUnit");
		sql = StringTool.ReplaceAll(sql, "#CUSTOMER_NM#", customerName);
		sql = StringTool.ReplaceAll(sql, "#VALIDFLAG#", strValidFlag);
		sql = sql.replaceAll("#CUSTOMER_NM#", customerName);
		sql = sql.replaceAll("#SALE_ORG#", sale_org);
		return this.getData(sql);
	}

	/**
	 * 查询订货单位
	 * 
	 * @return CoreReturnObject
	 */
	public CoreReturnObject getBaseOrderUnit(String customerName, String strValidFlag,
			String sale_org) {
		String sql = CoreXmlSqlParsersFactory.getSql("CoreBuyerOrder.getBaseOrderUnit");
		sql = StringTool.ReplaceAll(sql, "#CUSTOMER_NM#", customerName);
		sql = StringTool.ReplaceAll(sql, "#VALIDFLAG#", strValidFlag);
		sql = sql.replaceAll("#CUSTOMER_NM#", customerName);
		sql = sql.replaceAll("#SALE_ORG#", sale_org);
		return this.getData(sql);
	}

	/**
	 * 根据客户名称查询客户编码是否存在
	 * 
	 * @return CoreReturnObject
	 */
	public CoreReturnObject GetQueryCustomer(String name) {
		Object[] params = new Object[] { name };
		return this.getDataBySqlID("CoreBuyerBase.doQueryCustomer", params);
	}
	/**
	 * 根据客户名称查询无效客户
	 * 
	 * @return CoreReturnObject
	 */
	public CoreReturnObject GetQueryCTMR(String name) {
		Object[] params = new Object[] { name };
		return this.getDataBySqlID("CoreBuyerBase.doQueryCTMR", params);
	}
	/**
	 * 根据客户代码查询公共属性
	 * 
	 * @return CoreReturnObject
	 */
	public CoreReturnObject GetQueyPubuleCustomer(String name,String ordsqd) {
		String sql = CoreXmlSqlParsersFactory.getSql("CoreBuyerBase.doQueyPubuleCustomer");
		sql = StringTool.ReplaceAll(sql, "#CUSTOMER_NO#", name);
		sql = StringTool.ReplaceAll(sql, "#SALE_ORG#", ordsqd);
		return this.getData(sql);
	}
	/**
	 * 修改无效客户编码改为有效客户代码
	 * 
	 * @return CoreReturnObject 。
	 */
	public CoreReturnObject GetUpdateDeleteCTMR(ArrayList<Object> parm) {
		CoreReturnObject cro = new CoreReturnObject();
		String strSql = CoreXmlSqlParsersFactory
				.getSql("CoreBuyerBase.doUpdateDeleteCTMR");
		try {
			cro = this.getDao(CoreConstantData.sysbeanstr).ExcuteNonQuery(
					strSql, parm.toArray());
		} catch (SQLException e) {
			cro = cro.SetErrInfo(e.getMessage());
		}
		return cro;
	}
	
	/**
	 * 根据客户用途关系表ID是否存在
	 * 
	 * @return CoreReturnObject
	 */
	public CoreReturnObject GetQueryCustm_Use(String id,String sig,String orderSqd) {
		Object[] params = new Object[] { id, sig,orderSqd};
		return this.getDataBySqlID("CoreBuyerBase.doQueryCustm_Use", params);
	}
	
	/**
	 * 修改游戏标准
	 * 
	 * @return CoreReturnObject 。
	 */
	public CoreReturnObject SetUpCustm_Use(ArrayList<Object> parm) {
		CoreReturnObject cro = new CoreReturnObject();
		String strSql = CoreXmlSqlParsersFactory
				.getSql("CoreBuyerBase.SetUpCustm_Use");
		try {
			cro = this.getDao(CoreConstantData.sysbeanstr).ExcuteNonQuery(
					strSql, parm.toArray());
		} catch (SQLException e) {
			cro = cro.SetErrInfo(e.getMessage());
		}
		return cro;
	}
	
	/**
	 * 根据客户简称查询客户简称是否存在
	 * 
	 * @return CoreReturnObject
	 */
	public CoreReturnObject GetQueryAbrviTion(String name) {
		Object[] params = new Object[] { name };
		return this.getDataBySqlID("CoreBuyerBase.doQueryAbrviTion", params);
	}

	/**
	 * 根据客户ID查询收货单位表是否存在
	 * 
	 * @return CoreReturnObject
	 */
	public CoreReturnObject GetQueryReceivUnit(String name,String ordsed) {
		Object[] params = new Object[] { name,ordsed };
		return this.getDataBySqlID("CoreBuyerBase.doQueryReceivUnit", params);
	}

	/**
	 * 根据客户ID查询订货单位表是否存在
	 * 
	 * @return CoreReturnObject
	 */
	public CoreReturnObject GetQueryOrderUnit(String name,String ordsed) {
		Object[] params = new Object[] { name,ordsed };
		return this.getDataBySqlID("CoreBuyerBase.doQueryOrderUnit", params);
	}

	/**
	 * 新增客户编码
	 * 
	 * @return CoreReturnObject 。
	 */
	public CoreReturnObject GetInsertCustomer(ArrayList<Object> parm) {
		CoreReturnObject cro = new CoreReturnObject();
		String strSql = CoreXmlSqlParsersFactory
				.getSql("CoreBuyerBase.doInsertCustomer");
		try {
			cro = this.getDao(CoreConstantData.sysbeanstr).ExcuteNonQuery(
					strSql, parm.toArray());
		} catch (SQLException e) {
			cro = cro.SetErrInfo(e.getMessage());
		}
		return cro;
	}

	/**
	 * 新增客户授权
	 * 
	 * @return CoreReturnObject 。
	 */
	public CoreReturnObject GetInsertCustmRight(ArrayList<String> parm) {
		String strSql = CoreXmlSqlParsersFactory
				.getSql("CoreBuyerBase.doInsertCustmRight");
		ArrayList<String> sqllist = new ArrayList<String>();
		ArrayList<Object[]> parms = new ArrayList<Object[]>(); 
		if(parm.get(1).equals("100101") || parm.get(1).equals("100103")){
			sqllist.add(strSql);
			parm.set(1, "100101");
			parms.add(parm.toArray());
			sqllist.add(strSql);
			parm.set(1, "100103");
			parms.add(parm.toArray());
		}
		else{
			sqllist.add(strSql);
			parms.add(parm.toArray());
		}
		return this.getDao(CoreConstantData.sysbeanstr).ExcuteTransction(sqllist, parms);
	}

	/**
	 * 新增客户用途类型
	 * 
	 * @return CoreReturnObject 。
	 */
	public CoreReturnObject GetInsertCustmUse(ArrayList<String> parm) {
		String strSql = CoreXmlSqlParsersFactory
				.getSql("CoreBuyerBase.doInsertCustmUse");
		ArrayList<String> sqllist = new ArrayList<String>();
		ArrayList<Object[]> parms = new ArrayList<Object[]>();
		sqllist.add(strSql);
		parms.add(parm.toArray());
		return this.getDao(CoreConstantData.sysbeanstr).ExcuteTransction(sqllist, parms);
	}

	/**
	 * 更新客户编码信息
	 * 
	 * @return CoreReturnObject 。
	 */
	public CoreReturnObject GetUpdateCustomer(ArrayList<Object> parm) {
		CoreReturnObject cro = new CoreReturnObject();
		String strSql = CoreXmlSqlParsersFactory
				.getSql("CoreBuyerBase.doUpdateCustomer");
		try {
			cro = this.getDao(CoreConstantData.sysbeanstr).ExcuteNonQuery(
					strSql, parm.toArray());
		} catch (SQLException e) {
			cro = cro.SetErrInfo(e.getMessage());
		}
		return cro;
	}

	/*
	 * 删除客户编码信息
	 * 
	 * @return CoreReturnObject 。
	 */
	public CoreReturnObject GetUpdateCustomerALLS(ArrayList<Object> parm) {
		CoreReturnObject cro = new CoreReturnObject();
		String strSql = CoreXmlSqlParsersFactory
				.getSql("CoreBuyerBase.doUpdateCustomerALLS");
		try {
			cro = this.getDao(CoreConstantData.sysbeanstr).ExcuteNonQuery(
					strSql, parm.toArray());
		} catch (SQLException e) {
			cro = cro.SetErrInfo(e.getMessage());
		}
		return cro;
	}
	/*
	 * 删除客户用途关系表
	 * 
	 * @return CoreReturnObject 。
	 */
	public CoreReturnObject SetDeleteUseALL(ArrayList<Object> parm) {
		CoreReturnObject cro = new CoreReturnObject();
		String strSql = CoreXmlSqlParsersFactory
				.getSql("CoreBuyerBase.doDeleteUseALL");
		try {
			cro = this.getDao(CoreConstantData.sysbeanstr).ExcuteNonQuery(
					strSql, parm.toArray());
		} catch (SQLException e) {
			cro = cro.SetErrInfo(e.getMessage());
		}
		return cro;
	}
	
	public CoreReturnObject GetUpdateRightALL(ArrayList<Object> parm) {
		CoreReturnObject cro = new CoreReturnObject();
		String strSql = "";
		try {
			strSql = CoreXmlSqlParsersFactory
					.getSql("CoreBuyerBase.doUpdateRightALL");
			cro = this.getDao(CoreConstantData.sysbeanstr).ExcuteNonQuery(
					strSql, parm.toArray());
		} catch (SQLException e) {
			cro = cro.SetErrInfo(e.getMessage());
		}
		return cro;
	}

	public CoreReturnObject GetUpdateUseALL(ArrayList<Object> parm) {
		CoreReturnObject cro = new CoreReturnObject();
		String strSql = "";
		try {
			strSql = CoreXmlSqlParsersFactory
					.getSql("CoreBuyerBase.doUpdateUseALL");
			cro = this.getDao(CoreConstantData.sysbeanstr).ExcuteNonQuery(
					strSql, parm.toArray());
		} catch (SQLException e) {
			cro = cro.SetErrInfo(e.getMessage());
		}
		return cro;
	}

	public CoreReturnObject GetUpdateReceivUnitALL(ArrayList<Object> parm) {
		CoreReturnObject cro = new CoreReturnObject();
		String strSql = "";
		try {
			strSql = CoreXmlSqlParsersFactory
					.getSql("CoreBuyerBase.doUpdateReceivUnitALL");
			cro = this.getDao(CoreConstantData.sysbeanstr).ExcuteNonQuery(
					strSql, parm.toArray());
		} catch (SQLException e) {
			cro = cro.SetErrInfo(e.getMessage());
		}
		return cro;
	}

	public CoreReturnObject GetUpdateOrderUnitALL(ArrayList<Object> parm) {
		CoreReturnObject cro = new CoreReturnObject();
		String strSql = "";
		try {
			strSql = CoreXmlSqlParsersFactory
					.getSql("CoreBuyerBase.doUpdateOrderUnitALL");
			cro = this.getDao(CoreConstantData.sysbeanstr).ExcuteNonQuery(
					strSql, parm.toArray());
		} catch (SQLException e) {
			cro = cro.SetErrInfo(e.getMessage());
		}
		return cro;
	}

	public CoreReturnObject GetUpdateFreightALL(ArrayList<Object> parm) {
		CoreReturnObject cro = new CoreReturnObject();
		String strSql = "";
		try {
			strSql = CoreXmlSqlParsersFactory
					.getSql("CoreBuyerBase.doUpdateFreightALL");
			cro = this.getDao(CoreConstantData.sysbeanstr).ExcuteNonQuery(
					strSql, parm.toArray());
		} catch (SQLException e) {
			cro = cro.SetErrInfo(e.getMessage());
		}
		return cro;
	}

	/**
	 * 修改收货单位有效标志
	 * */
	public CoreReturnObject GetUpdateReceivUnit(ArrayList<Object> parm) {
		CoreReturnObject cro = new CoreReturnObject();
		String strSql = "";
		try {
			strSql = CoreXmlSqlParsersFactory
					.getSql("CoreBuyerBase.doUpdateReceivUnit");
			cro = this.getDao(CoreConstantData.sysbeanstr).ExcuteNonQuery(
					strSql, parm.toArray());
		} catch (SQLException e) {
			cro = cro.SetErrInfo(e.getMessage());
		}
		return cro;
	}

	/**
	 * 修改订结单位有效标志
	 * */
	public CoreReturnObject GetUpdateOrderUnit(ArrayList<Object> parm) {
		CoreReturnObject cro = new CoreReturnObject();
		String strSql = "";
		try {
			strSql = CoreXmlSqlParsersFactory
					.getSql("CoreBuyerBase.doUpdateOrderUnit");
			cro = this.getDao(CoreConstantData.sysbeanstr).ExcuteNonQuery(
					strSql, parm.toArray());
		} catch (SQLException e) {
			cro = cro.SetErrInfo(e.getMessage());
		}
		return cro;
	}

	/**
	 * 修改客户用途类型有效标志
	 * */
	public CoreReturnObject GetUpdateUse(ArrayList<Object> parm) {
		CoreReturnObject cro = new CoreReturnObject();
		String strSql = "";
		try {
			strSql = CoreXmlSqlParsersFactory
					.getSql("CoreBuyerBase.doUpdateUse");
			cro = this.getDao(CoreConstantData.sysbeanstr).ExcuteNonQuery(
					strSql, parm.toArray());
		} catch (SQLException e) {
			cro = cro.SetErrInfo(e.getMessage());
		}
		return cro;
	}
}
