package com.steering.pss.sale.base;

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

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

import com.steering.comm.db.DBCommon;
import com.steering.comm.tools.EntityTool;
import com.steering.comm.tools.StringTool;
import com.steering.pss.qcm.model.GenericEntity;

public class CoreBuyerSend extends DBCommon {
	/**
	 * 查询客户收货单位
	 * 
	 * @param name
	 * @return
	 */
	public CoreReturnObject GetQueryBuyerSend(String customer_no,
			String sale_org, String custm_use) {
		Object[] params = new Object[] { customer_no, sale_org, custm_use };
		return this.getDataBySqlID("CoreBuyerSend.doQuery", params);
	}

	/**
	 * 新增客户用途类型单位
	 * 
	 * @return CoreReturnObject
	 */
	public CoreReturnObject GetInsertCustmUse(ArrayList<Object> parm) {
		CoreReturnObject cro = new CoreReturnObject();
		String strSql = CoreXmlSqlParsersFactory
				.getSql("CoreBuyerSend.insertCustmuse");
		try {
			cro = this.getDao(CoreConstantData.sysbeanstr).ExcuteNonQuery(
					strSql, parm.toArray());
		} catch (SQLException e) {
			cro = cro.SetErrInfo(e.getMessage());
		}
		return cro;
	}

	/**
	 * 新增客户收货单位
	 * 
	 * @return CoreReturnObject 。
	 */
	public CoreReturnObject SetInsertSend(ArrayList<Object> parm) {
		CoreReturnObject cro = new CoreReturnObject();
		String strSql = CoreXmlSqlParsersFactory
				.getSql("CoreBuyerSend.doInsert");
		try {
			cro = this.getDao(CoreConstantData.sysbeanstr).ExcuteNonQuery(
					strSql, parm.toArray());
		} catch (SQLException e) {
			cro = cro.SetErrInfo(e.getMessage());
		}
		return cro;
	}

	/**
	 * 修改客户收货单位
	 * 
	 * @return CoreReturnObject 。
	 */
	public CoreReturnObject SetUpdateSendCustmuse(ArrayList<Object> parm) {
		CoreReturnObject cro = new CoreReturnObject();
		String strSql = CoreXmlSqlParsersFactory
				.getSql("CoreBuyerSend.doUpdateCustmuse");
		try {
			cro = this.getDao(CoreConstantData.sysbeanstr).ExcuteNonQuery(
					strSql, parm.toArray());
		} catch (SQLException e) {
			cro = cro.SetErrInfo(e.getMessage());
		}
		return cro;
	}
	
	/**
	 * 保存收货信息
	 * @param listCustmUse
	 * @param listRcvUnit
	 * @param listDelFreight
	 * @param listCustmFreight
	 * @return
	 */
	public CoreReturnObject saveBuyerSend(ArrayList<String> listCustmUse, ArrayList<String> listRcvUnit,
			ArrayList<String> listDelFreight, ArrayList<ArrayList<String>> listCustmFreights){
		ArrayList<String> sqllist = new ArrayList<String>();
		ArrayList<Object[]> parms = new ArrayList<Object[]>();
		String sqlGetRcvUnitCnt = CoreXmlSqlParsersFactory.getSql("CoreBuyerSend.getRcvUnitCnt");
		sqlGetRcvUnitCnt = StringTool.ReplaceAll(sqlGetRcvUnitCnt, "#CUSTOMER_NO#", listCustmUse.get(0));
		sqlGetRcvUnitCnt = StringTool.ReplaceAll(sqlGetRcvUnitCnt, "#SALE_ORG#", listCustmUse.get(1));
		sqlGetRcvUnitCnt = StringTool.ReplaceAll(sqlGetRcvUnitCnt, "#CUSTM_USE#", listCustmUse.get(2));
		Connection con = this.getDao(CoreConstantData.sysbeanstr).getConnection();
		ArrayList<GenericEntity> listRcvUnitCnt = EntityTool.getEntitysBySql(sqlGetRcvUnitCnt, con, GenericEntity.class);
		String sqlRcvUnit = "";
		if(listRcvUnitCnt.get(0).getCount().equals("0")){
			//获取客户系统用途是否存在
			String sqlGetCustmuseCnt = CoreXmlSqlParsersFactory.getSql("CoreBuyerSend.getCustmCnt");
			sqlGetCustmuseCnt = StringTool.ReplaceAll(sqlGetCustmuseCnt, "#CUSTOMER_NO#", listCustmUse.get(0));
			sqlGetCustmuseCnt = StringTool.ReplaceAll(sqlGetCustmuseCnt, "#SALE_ORG#", listCustmUse.get(1));
			sqlGetCustmuseCnt = StringTool.ReplaceAll(sqlGetCustmuseCnt, "#CUSTM_USE#", listCustmUse.get(2));
			con = this.getDao(CoreConstantData.sysbeanstr).getConnection();
			ArrayList<GenericEntity> listCustmCnt = EntityTool.getEntitysBySql(sqlGetCustmuseCnt, con, GenericEntity.class);
			//判断客户系统用途是否存在，不存在增加
			String sqlCustmuse="";
			if(listCustmCnt.get(0).getCount().equals("0")){
			   sqlCustmuse = CoreXmlSqlParsersFactory.getSql("CoreBuyerSend.insertCustmuse");
			   sqllist.add(sqlCustmuse);
			   parms.add(listCustmUse.toArray());
			}	
			else{
				sqlCustmuse = CoreXmlSqlParsersFactory.getSql("CoreBuyerSend.UpdateCustmuse");
				ArrayList<String> listCustmUseUpdate = new ArrayList<String>();
				listCustmUseUpdate.add(listCustmUse.get(3));
				listCustmUseUpdate.add(listCustmUse.get(0));
				listCustmUseUpdate.add(listCustmUse.get(1));
				listCustmUseUpdate.add(listCustmUse.get(2));
				sqllist.add(sqlCustmuse);
			    parms.add(listCustmUseUpdate.toArray());
			}
			
			sqlRcvUnit = CoreXmlSqlParsersFactory.getSql("CoreBuyerSend.insertRcvUnit");			
		}else{
			sqlRcvUnit = CoreXmlSqlParsersFactory.getSql("CoreBuyerSend.updateRcvUnit");
		}
		sqllist.add(sqlRcvUnit);
		parms.add(listRcvUnit.toArray());
		
		for(String str : listDelFreight){
			String sqlDeleteFreight = CoreXmlSqlParsersFactory.getSql("CoreBuyerOrder.doDeleteFreight");
			sqllist.add(sqlDeleteFreight);
			parms.add(new Object[]{str});
		}
		
		for(ArrayList<String> listCustmFreight : listCustmFreights){
			String sqlInsertFreight = CoreXmlSqlParsersFactory.getSql("CoreBuyerOrder.doInsertCustmFreight");
			sqllist.add(sqlInsertFreight);
			parms.add(listCustmFreight.toArray());
		}
		return this.getDao(CoreConstantData.sysbeanstr).ExcuteTransction(sqllist, parms);
	}
}
