package com.steering.pss.sale.order;

import CoreFS.SA06.CoreReturnObject;
import CoreFS.SA08.CoreXmlSqlParsersFactory;

import CoreFS.Util.StringUtils;
import com.steering.comm.db.DBCommon;

public class CoreOrderBuyer extends DBCommon {
	/**
	 * 用途类型(订货单位/结算单位/最终用户/收货单位)
	 * 
	 * @param customerNo
	 *            客户ID
	 * @param saleOrg
	 *            销售组织
	 * @return CoreReturnObject 必须返回CoreReturnObject
	 */
	public CoreReturnObject doQuery(String customerNo, String saleOrg) {
		Object[] params = new Object[] { customerNo, saleOrg, customerNo,
				saleOrg };
		return this.getDataBySqlID("CoreOrderBuyer.QUERYTYPE", params);
	}

	/**
	 * 查询(订货单位/结算单位/最终用户/收货单位)
	 * 
	 * @param customerNo
	 *            客户ID
	 * @param saleOrg
	 *            销售组织
	 * @return CoreReturnObject 必须返回CoreReturnObject
	 */
	public CoreReturnObject GetQueryCustomer(String customerNo, String saleOrg) {
		Object[] params = new Object[] { customerNo, saleOrg, customerNo,
				saleOrg };
		return this.getDataBySqlID("CoreOrderBuyer.doQueryCustomer", params);
	}

	/**
	 * 查询(最终用户/收货单位)
	 * 
	 * @param customerNo
	 *            客户ID
	 * @param saleOrg
	 *            销售组织
	 * @param custmuse
	 *            用途类型
	 * @return CoreReturnObject 必须返回CoreReturnObject
	 */
	public CoreReturnObject doQueryBuyer(String saleOrg, String custmuse) {
		Object[] obj = new Object[] { custmuse};
        String sql = CoreXmlSqlParsersFactory
                .getSql("CoreOrderBuyer.doQueryBuyer");
        String str="";
        if (saleOrg.equals("100102")) { // 国贸
        	str = "  AND B.SALE_ORG IN ('100102')";
        }
        else {//销售钢贸天淮
            str = "  AND B.SALE_ORG IN ('100101', '100103', '100105')";   
        }
        sql += str;
        return this.getData(sql, obj);
	}

	/**
	 * 查询(最终用户/收货单位)
	 * 
	 * @param customerNo
	 *            客户ID
	 * @param saleOrg
	 *            销售组织
	 * @param custmuse
	 *            用途类型
	 * @return CoreReturnObject 必须返回CoreReturnObject
	 */
	public CoreReturnObject doQueryBuyer100204(String saleOrg, String custmuse) {
		Object[] obj = new Object[] { custmuse};
        String sql = CoreXmlSqlParsersFactory
                .getSql("CoreOrderBuyer.doQueryBuyer100204");
        String str="";
        if (saleOrg.equals("100102")) { // 国贸
        	str = "  AND C.SALE_ORG IN ('100102')";
        }
        else {//销售钢贸天淮
            str = "  AND C.SALE_ORG IN ('100101', '100103', '100105')";   
        }
        sql += str;
        return this.getData(sql, obj);
	}

	public CoreReturnObject doQueryBuyer100105(String saleOrg, String custmuse) {
		Object[] obj = new Object[] { custmuse};
		String sql = CoreXmlSqlParsersFactory
				.getSql("CoreOrderBuyer.doQueryBuyer100105");
		String str="";
		if (saleOrg.equals("100102")) { // 国贸
			str = "  AND C.SALE_ORG IN ('100102')";
		}
		else {//销售钢贸天淮
			str = "  AND C.SALE_ORG IN ('100101', '100102','100103', '100105')";
		}
		sql += str;
		return this.getData(sql);
	}

	/**
	 * 查询(订货单位/结算单位)
	 * 
	 * @param customerNo
	 *            客户ID
	 * @param saleOrg
	 *            销售组织
	 * @param custmuse
	 *            用途类型
	 * @return CoreReturnObject 必须返回CoreReturnObject
	 */
	public CoreReturnObject doQueryBuyers(String saleOrg, String custmuse) {
		Object[] obj = new Object[] { custmuse};
        String sql = CoreXmlSqlParsersFactory
                .getSql("CoreOrderBuyer.doQueryBuyers");
        String str="";
        if (saleOrg.equals("100102")) { // 国贸
        	str = "  AND B.SALE_ORG IN ('100102')";
        }
        else {//销售钢贸天淮
            str = "  AND B.SALE_ORG IN ('100101', '100103', '100105')";   
        }
        sql += str;
        return this.getData(sql, obj);
	}

	public CoreReturnObject doQueryUserInfo(String userName, String userId) {
		String sql = CoreXmlSqlParsersFactory
				.getSql("CoreOrderBuyer.doQueryUserInfo");
		if(!StringUtils.isEmpty(userName)){
			sql += " and t.USERNAME = '" + userName + "'";
		}
		if(!StringUtils.isEmpty(userId)){
			sql += " and t.USERID = '" + userId + "'";
		}
		return this.getData(sql);
	}


	/**
	 * 查询函数 根据客户名称或者简称
	 * 
	 * @return CoreReturnObject
	 */
	public CoreReturnObject QUERYROW(String customerName, String abrvition,
			String custm) {
		String sql = "SELECT c.customer_no, c.sale_org, c.custm_use"
				+ " FROM slm_base_customer a, slm_base_custm_right b, slm_base_custm_use c"
				+ " WHERE a.customer_no IN (SELECT customer_no"
				+ "                          FROM slm_base_customer"
				+ "                         WHERE 1 = 1 " + customerName + ")"
				+ "  AND a.customer_no = b.customer_no" + "  AND b.sale_org = "
				+ abrvition + "" + "  AND a.customer_no = c.customer_no"
				+ "  AND c.sale_org = b.sale_org" + "	AND c.custm_use = '"
				+ custm + "'";
		return this.getData(sql);
	}
}
