package com.steering.pss.ydm.pipeleavefactory;

import CoreFS.SA06.CoreReturnObject;
import CoreFS.SA08.CoreXmlSqlParsersFactory;
import CoreFS.SC01.CoreConstantData;
import com.steering.comm.converter.CoreConverter;
import com.steering.comm.db.DBCommon;
import com.steering.comm.tools.EntityTool;
import com.steering.pss.ydm.base.MeanValueCalculation;
import com.steering.pss.ydm.entity.*;
import org.apache.commons.beanutils.BeanUtils;

import java.math.BigDecimal;
import java.sql.*;
import java.text.DecimalFormat;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * 铁运提单编制
 * 
 * @author Administrator
 */
public class FrmPipeTrainBill extends DBCommon {
	// 2015-08-27

	/**
	 * 查询发运计划信息
	 * 
	 * @param params
	 *            查询条件
	 * @param args
	 *            销售组织权限
	 * @return CoreReturnObject
	 */
	public CoreReturnObject querySendPlan(ArrayList<Object> params,
			String[] args) {
		String sql = CoreXmlSqlParsersFactory
				.getSql("FrmPipeTrainBill.querySendPlan");
		sql += CoreConverter.toSqlConditionString(" T.MANAGEMENT_NO", args);
		sql += " ORDER BY T.PLAN_YEAR_MONTH,T.SEND_PLAN_NO";
		return this.getData(sql, params.toArray());
	}

	/**
	 * 查询发运合同信息
	 * 
	 * @param sendPlanNo
	 *            发运计划号
	 * @param
	 *
	 * @return CoreReturnObject
	 */
	public CoreReturnObject queryOrderInfo(String sendPlanNo) {
		Object[] params = new Object[] { sendPlanNo };
		return this.getDataBySqlID("FrmPipeTrainBill.queryOrderInfo", params);
	}

	/**
	 * 查询仓库可编制信息
	 * 
	 * @param ordPk
	 *            合同主键
	 * @param ordLnPk
	 *            行主键
	 * @param ordLnDlyPK
	 *            交货行主键
	 * @return CoreReturnObject
	 */
	public CoreReturnObject querySourse(String ordPk, String ordLnPk,
			String ordLnDlyPK) {
		Object[] params = new Object[] { ordLnDlyPK };
		String sql = CoreXmlSqlParsersFactory
				.getSql("FrmPipeTrainBill.querySourse");

		return this.getData(sql, params);
	}

	// 2015/08/28
	/**
	 * 查询提单主表信息
	 * 
	 * @param
	 *
	 * @return obj
	 */
	public CoreReturnObject queryBillM(ArrayList<String> param, String[] salgs) {
		String sql = CoreXmlSqlParsersFactory
				.getSql("FrmPipeTrainBill.queryBillM");
		sql += CoreConverter.toSqlConditionString("T.MANAGEMENT_NO", salgs);
		sql += "  GROUP BY T.BILL_NO,T.BILL_STATUS,T.LOADVEHICLE_TYPE,T.PLAN_TRANS_TYPE,T.SALE_ORG_DESC,T.SEND_PLAN_NO,"
				+ "T.ISPRECLOSE,T.STATION_NO,T.STATION_NM,T.CUSTOMER_NO,T.CUSTOMER_NM,T.RECEIV_NO,T.RECEIV_NM,"
				+ "T.STLMT_UNIT_NO,T.STLMT_UNIT_NM,T.CREATE_NAME,T.SALE_ORG,"
				+ "T.DELVRY_ADDR,T.ZOUT_FLAG,T.BILL_TICKET_STATUS,T.OUTSTOCK_TICKET_STATUS,T.TRANSMNY_TICKET_STATUS "
				+ " ORDER BY BILL_NO,BILL_STATUS,LOADVEHICLE_TYPE";
		return this.getData(sql, param.toArray());
	}

	/**
	 * 查询提单材料表信息
	 * 
	 * @param
	 *
	 * @return obj
	 */
	public CoreReturnObject queryBillC(String billNo) {
		Object[] params = new Object[] { billNo };
		return this.getDataBySqlID("FrmPipeTrainBill.queryBillC", params);
	}

	/**
	 * 下发
	 * 
	 * @param
	 * @return
	 */
	public CoreReturnObject DoDown(ArrayList<String[]> billNoArrs,
			String userName) {
		CoreReturnObject core = new CoreReturnObject();
		Connection con = null;
		PreparedStatement ps = null;
		ResultSet rs = null;
		String sql = "";
		con = this.getDao("TgDao").getConnection();
		try {
			con.setAutoCommit(false);
			sql = CoreXmlSqlParsersFactory
					.getSql("FrmPipeTrainBill.checkDoDown");
			String sqlU = CoreXmlSqlParsersFactory
					.getSql("FrmPipeTrainBill.UpdateBillSeq");
			String sqlSend = CoreXmlSqlParsersFactory
					.getSql("FrmPipeTrainBill.UpdateSendPlanStatus");
			for (String[] billNos : billNoArrs) {
				String billNo = billNos[0];
				String sendPlanNo = billNos[1];
				String sendDayplanNo = billNos[2];
				ps = con.prepareStatement(sql);
				ps.setObject(1, billNo);
				String status = "";
				rs = ps.executeQuery();
				if (rs.next()) {
					status = rs.getString("BILL_STATUS");
				}
				rs.close();
				ps.close();
				if (status.equals("0")) {
					ps = con.prepareStatement(sqlU);
					ps.setObject(1, userName);
					ps.setObject(2, billNo);

					ps.executeUpdate();
					ps.close();
					ps = con.prepareStatement(sqlSend);
					ps.setObject(1, sendPlanNo);
					ps.setObject(2, sendDayplanNo);
					ps.executeUpdate();
					ps.close();
				} else {
					core.setV_errMsg("不是提单编制状态的提单,不能下发!");
					return core;
				}
			}
			core.setV_errMsg("下发成功!");
			con.commit();
		} catch (SQLException e) {
			try {
				con.rollback();
			} catch (SQLException e1) {
				e1.printStackTrace();
			}
			e.printStackTrace();
		} finally {
			try {
				if (con != null)
					con.close();
			} catch (SQLException e) {
				e.printStackTrace();
			}
		}

		return core;
	}

	/**
	 * 新增提单
	 * 
	 * @param param
	 *            参数集合
	 * @param
	 *
	 * @return CoreReturnObject
	 */
	public CoreReturnObject doAddBillInfo(ArrayList<String> param,
			ArrayList<MatInfoEntity> matInfoEntitys, String orderNo,
			String ordersSeqNo, String delvNo, String strPraFlag,String supplyUnit, String supplyUnitDesc) {
		CoreReturnObject core = new CoreReturnObject();
		String billNo = param.get(0);
		String sendPlanNo = param.get(1);
		String ordPk = param.get(2);
		String ordLnPk = param.get(3);
		String ordLnDlyPk = param.get(4);
		String userName = param.get(5);
		// String departId = param.get(6);
		// String department = param.get(7);
		String virtualFlag = param.get(8);
		String beginTime = param.get(9);
		String endTime = param.get(10);
		Date nowTime = new Date();
		SimpleDateFormat dfT = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		String strNowTime = dfT.format(nowTime);
		Connection con = null;
		PreparedStatement ps = null;
		try {
			con = this.getDao(CoreConstantData.sysbeanstr).getConnection();
			con.setAutoCommit(false);
			// 验证计划信息
			YdmZcSendplanMEntity sendPlanMEntity = CorePipeCarBill
					.getSendPlanInfo(con, sendPlanNo);
			if (sendPlanMEntity == null) {
				core.setV_errMsg("计划" + sendPlanNo + "信息已改变,请重新获取数据!");
				return core;
			}
			if (!(sendPlanMEntity.getSendPlanStatus().equals("1") || sendPlanMEntity
					.getSendPlanStatus().equals("2"))) {
				core.setV_errMsg("发运计划号:" + sendPlanNo + "不是编制/下发状态,不能生成提单!");
				return core;
			}
			// 生成修改或者新增提单号标识
			boolean flag = false;
			String zbsSource = "";
			if (!billNo.equals("")) {
				String[] billInfo = CorePipeCarBill
						.checkBillStatus(con, billNo);
				if (billInfo == null) {
					core.setV_errMsg("提单号发生变化,请重新获取数据!");
					return core;
				}
				String billStatus = billInfo[0];
				String beSendPlanNo = billInfo[1];
				zbsSource = billInfo[3];
				if (!(billStatus.equals("0"))) {
					core.setV_errMsg("提单号:" + billNo + "不是编制,不能追加提单信息!");
					return core;
				}
				if (!beSendPlanNo.equals(sendPlanNo)) {
					core.setV_errMsg("不能在计划号:" + beSendPlanNo + "的提单号:"
							+ billNo + "下追加计划号:" + sendPlanNo + "信息!");
					return core;
				}
				// 如果是同一个计划号同一个交货行号向下就修改提单
				flag = CorePipeCarBill.checkIsCommon(con, billNo, sendPlanNo,
						ordPk, ordLnPk, ordLnDlyPk);
			} else {
				// 生成提单号
				flag = true;
				billNo = CorePipeCarBill.insertBillNo(con,
						sendPlanMEntity.getSaleOrg());
				String sqlInsertBill = "INSERT INTO YDM_ZC_BILL_NO (BILL_NO) values ('"
						+ billNo + "')";
				Statement sta1 = con.createStatement();
				sta1.executeUpdate(sqlInsertBill);
				sta1.close();
			}

			// 获取合同信息(发运计划子信息)
			YdmZcSendplanCEntity planCEntity = CorePipeCarBill
					.getSendPlanCInfo(con, sendPlanNo, ordPk, ordLnPk,
							ordLnDlyPk);
			if (planCEntity == null) {
				core.setV_errMsg("合同信息已改变,请重新获取数据!");
				return core;
			}
			// // 合同计划量
			// double orderWgtPlan =
			// Double.parseDouble(planCEntity.getPlanWt());
			// // 合同已编制量
			// double sumVolumeWgt = CorePipeCarBill.getBillVolume(con,
			// sendPlanNo, ordPk, ordLnPk, ordLnDlyPk);

			DecimalFormat df = new DecimalFormat("0000");
			String sqlUpMat = CoreXmlSqlParsersFactory
					.getSql("CorePipeCarBill.updateMatZcMByMatNo");
			PreparedStatement psMat = con.prepareStatement(sqlUpMat);

			// String sqlUpMatLen = CoreXmlSqlParsersFactory
			// .getSql("CorePipeCarBill.updateMatZcMByMatNolen");
			// PreparedStatement psMatlen = con.prepareStatement(sqlUpMatLen);

			String sqlAddBillC = CoreXmlSqlParsersFactory
					.getSql("CorePipeCarBill.addBillCInfo");
			PreparedStatement ps1 = con.prepareStatement(sqlAddBillC);
			String sqlAddBillM = CoreXmlSqlParsersFactory
					.getSql("CorePipeCarBill.addBillMInfo");
			String sqlupdateBillM = CoreXmlSqlParsersFactory
					.getSql("CorePipeCarBill.updateBillByBillNo");
			// Statement st=con.createStatement();
			String sqlupdateSendM = CoreXmlSqlParsersFactory
					.getSql("CorePipeCarBill.updateSendM");
			PreparedStatement psSendM = con.prepareStatement(sqlupdateSendM);
			String sqlupdateSendC = CoreXmlSqlParsersFactory
					.getSql("CorePipeCarBill.updateSendC");
			PreparedStatement psSendC = con.prepareStatement(sqlupdateSendC);
			// 插入到运输表
			String sqlupdateDeryL = CoreXmlSqlParsersFactory
					.getSql("CorePipeCarBill.InsertDeryL");
			PreparedStatement psDeryL = con.prepareStatement(sqlupdateDeryL);
			String zbsFlag = "";
			String jgZbsId = "";
			for (MatInfoEntity matInfoEntity : matInfoEntitys) {
				// 分炉标识
				// String prtFlag = CorePipeCarBill.checkIsPrtByJustNo(con,
				// matInfoEntity.getJudgeStoveNo(), matInfoEntity
				// .getBatchNo(), matInfoEntity.getBatchGroudNo(),
				// matInfoEntity.getActCount(), "110502");
				String prtFlag = strPraFlag;
				// 可编辑信息
				List<MatZcMInfoEntity1> matZcMInfos = CorePipeCarBill
						.getInventoryCanInfo(con, matInfoEntity,"1");

				List<double[]> lenList = MeanValueCalculation.MeanCalculation(
						Double.parseDouble(matInfoEntity.getSendNum()), Integer
								.parseInt(String.valueOf(matInfoEntity
										.getActCount())));
				List<String> len = new ArrayList<String>();// 计算每只长度
				for (int j = 0; j < lenList.size(); j++) {
					double[] conLen = lenList.get(j);
					if ((int) conLen[0] >= 0) {
						for (int z = 0; z < (int) conLen[0]; z++) {
							len.add(String.valueOf(conLen[1]));
						}
					}
				}
				int w = 0;

				for (MatZcMInfoEntity1 matZcMInfoEntity : matZcMInfos) {

					if (matZcMInfoEntity.getZbsFlag() != null
							|| !matZcMInfoEntity.getZbsFlag().equals("")) {
						if (zbsFlag.equals("")) {
							zbsFlag = matZcMInfoEntity.getZbsFlag();
						}
					}
					if (!zbsFlag.equals("")
							&& !zbsFlag.equals(matZcMInfoEntity.getZbsFlag())) {
						core.setV_errMsg("选择炉号质保书来源不一致不能生成同一提单!");
						return core;
					}
					if (matZcMInfoEntity.getJgZbsId() != null
							|| !matZcMInfoEntity.getJgZbsId().equals("")) {
						if (jgZbsId.equals("")) {
							jgZbsId = matZcMInfoEntity.getJgZbsId();
						}
					}
//					if (!jgZbsId.equals("")
//							&& !jgZbsId.equals(matZcMInfoEntity.getJgZbsId())) {
//						core.setV_errMsg("选择炉号接箍质保书Id不一致不能生成同一提单!");
//						return core;
//					}
//					String matNo = "";
//					String strBillNo = "";
//					String sql = CoreXmlSqlParsersFactory
//							.getSql("CorePipeCarBill.queryBillMatNoC");
//					PreparedStatement ps2 = con.prepareStatement(sql);
//					ps2.setObject(1, matZcMInfoEntity.getMatNo());
//					ResultSet rs = ps2.executeQuery();
//					if (rs.next()) {
//						matNo = rs.getString("MAT_NO");
//						strBillNo = rs.getString("BILL_NO");
//					}
//					rs.close();
//					ps2.close();
//
//					if (!matNo.equals("")) {
//						core.setV_errMsg("该炉号："
//								+ matZcMInfoEntity.getJudgeStoveNo() + "、批号："
//								+ matZcMInfoEntity.getBatchNo() + "、组号："
//								+ matZcMInfoEntity.getBatchGroudNo()
//								+ ",已生成提单！提单号为:" + strBillNo);
//						return core;
//					}
					String gllockFlag = "";
					String zllockFlag = "";
					String sql = CoreXmlSqlParsersFactory
							.getSql("CorePipeCarBill.queryLockMatNo");
					PreparedStatement ps2 = con.prepareStatement(sql);
					ps2.setObject(1, matZcMInfoEntity.getMatNo());
					ResultSet rs = ps2.executeQuery();
					if (rs.next()) {
						gllockFlag = rs.getString("MNG_LOCK_FLAG");
						zllockFlag = rs.getString("LOCK_FLAG");
					}
					rs.close();
					ps2.close();

					if (gllockFlag.equals("1")) {
						core.setV_errMsg("已管理封锁,不能生成提单！");
						return core;
					}
					if (zllockFlag.equals("1")) {
						core.setV_errMsg("已质量封锁,不能生成提单！");
						return core;
					}
					// 修改库存状态
					// ps = con.prepareStatement(sqlUpMat);
					psMat.setObject(1, matZcMInfoEntity.getMatNo());
					psMat.addBatch();

					// psMatlen.setObject(1, String.valueOf(len.get(w)));
					// psMatlen.setObject(2, matZcMInfoEntity.getMatNo());
					// psMatlen.addBatch();
					// 新增提单从表信息
					// int billCIndex = CorePipeCarBill.getBillCMaxSeq(con,
					// matZcMInfoEntity.getMatNo());
					String billNoSeq = CorePipeCarBill.getBillCMaxSeq(con,
							matZcMInfoEntity.getMatNo());
					// matZcMInfoEntity.getMatNo()
					// + df.format(billCIndex + 1);
					ps1.setObject(1, prtFlag);
					ps1.setObject(2, matZcMInfoEntity.getOrdPk());
					ps1.setObject(3, matZcMInfoEntity.getOrdLnPk());
					ps1.setObject(4, matZcMInfoEntity.getOrdLnDlyPk());
					ps1.setObject(5, billNoSeq);
					ps1.setObject(6, billNo);
					ps1.setObject(7, orderNo);
					ps1.setObject(8, ordersSeqNo);
					ps1.setObject(9, matZcMInfoEntity.getMatNo());
					ps1.setObject(10, matZcMInfoEntity.getLotNo());
					ps1.setObject(11, matZcMInfoEntity.getJudgeStoveNo());
					ps1.setObject(12, matZcMInfoEntity.getBatchNo());
					ps1.setObject(13, matZcMInfoEntity.getBatchGroudNo());
					ps1.setObject(14, matZcMInfoEntity.getStoveNo());
					ps1.setObject(15, matZcMInfoEntity.getMaterialNo());
					ps1.setObject(16, matZcMInfoEntity.getMaterialName());
					ps1.setObject(17, matZcMInfoEntity.getProduccode());
					ps1.setObject(18, matZcMInfoEntity.getProducname());
					ps1.setObject(19, matZcMInfoEntity.getStdCode());
					ps1.setObject(20, matZcMInfoEntity.getStdName());
					ps1.setObject(21, matZcMInfoEntity.getGradecode());
					ps1.setObject(22, matZcMInfoEntity.getGradename());
					ps1.setObject(23, matZcMInfoEntity.getSteelcode());
					ps1.setObject(24, matZcMInfoEntity.getSteelname());
					ps1.setObject(25, matZcMInfoEntity.getStdStyle());
					ps1.setObject(26, matZcMInfoEntity.getStdStyleDesc());
					ps1.setObject(27, matZcMInfoEntity.getSpecCode());
					ps1.setObject(28, matZcMInfoEntity.getSpecName());
					ps1.setObject(29, matZcMInfoEntity.getModelCode());
					ps1.setObject(30, matZcMInfoEntity.getModelDesc());
					ps1.setObject(31, matZcMInfoEntity.getPlineCode());
					ps1.setObject(32, matZcMInfoEntity.getPlineName());
					ps1.setObject(33, matZcMInfoEntity.getActDimater());
					ps1.setObject(34, matZcMInfoEntity.getActHeight());
					ps1.setObject(35,
							String.valueOf(matZcMInfoEntity.getActLen()));
					ps1.setObject(36, matZcMInfoEntity.getActLenMin());
					ps1.setObject(37, matZcMInfoEntity.getActLenMax());
					ps1.setObject(38,
							String.valueOf(matZcMInfoEntity.getActCount()));
					ps1.setObject(39,
							String.valueOf(matZcMInfoEntity.getActWeight()));
					ps1.setObject(40, String.valueOf(matZcMInfoEntity
							.getActTheoryWeight()));
					ps1.setObject(41, matZcMInfoEntity.getJudgeResultCode());
					ps1.setObject(42, matZcMInfoEntity.getProBc());
					ps1.setObject(43, matZcMInfoEntity.getProBz());
					ps1.setObject(44, matZcMInfoEntity.getRpoTime());
					ps1.setObject(45, matZcMInfoEntity.getMatNo());
					ps1.setObject(46, matZcMInfoEntity.getMatNo());
					ps1.setObject(47, userName);
					ps1.setObject(48, strNowTime);
					ps1.setObject(49, delvNo);
					ps1.setObject(50, matInfoEntity.getVstel());
					ps1.setObject(51, matInfoEntity.getVstelDesc());
					if(matInfoEntity.getActWeight().equals(matInfoEntity.getSendNum())){
						ps1.setObject(52, matZcMInfoEntity.getActWeight());
					}else{
						ps1.setObject(52, String.valueOf(len.get(w)));	
					}
					// ps1.setObject(52, String.valueOf(len.get(w)));
					ps1.setObject(53, "");
					ps1.setObject(54, matZcMInfoEntity.getFinishproMold());
					ps1.setObject(55, matZcMInfoEntity.getInMold());
					ps1.setObject(56, matZcMInfoEntity.getZbsId());
					ps1.setObject(57, matZcMInfoEntity.getJgZbsId());
					ps1.setObject(58, "");
					ps1.setObject(59, matZcMInfoEntity.getInType());
					ps1.addBatch();
					w++;
				}

			}
			// psMatlen.executeBatch();

			// 查询库存可编制量
			double sumWgtCan = 0;
			int sumCount = 0;
			double sumLen = 0;
			for (MatInfoEntity matInfoEntity : matInfoEntitys) {
				double[] amount = CorePipeCarBill.getInventoryCanWgt(con,
						matInfoEntity);
				int amountCount = (int) amount[0];
				double amountWgt = amount[1];
				double amountLen = amount[2];
				if (amountCount != Integer
						.parseInt(matInfoEntity.getActCount())
						|| Math.abs(new BigDecimal(amountWgt).setScale(2,BigDecimal.ROUND_HALF_UP).subtract(new BigDecimal(matInfoEntity.getActWeight()).setScale(2,BigDecimal.ROUND_HALF_UP)).doubleValue()) > 1) {
					core.setV_errMsg("库存可编制量信息发生改变,请重新获取数据!");
					return core;
				}
				sumWgtCan += amountWgt;
				sumCount +=  amountCount;
				sumLen += amountLen;
			}
			// 新增提单主表信息
			if (flag) {

				YdmZcBillMEntity billMEntity = new YdmZcBillMEntity();
				BeanUtils.copyProperties(billMEntity, sendPlanMEntity);
				// 查询提单主表最大序号
				int indexMaxM = CorePipeCarBill.getBillMMaxSeq(con, billNo);
				String billNoSeq = billNo + df.format(indexMaxM);
				BillOrderInfoEntity orderLineEntity = CorePipeCarBill
						.getBillOrderInfo(con, ordLnDlyPk);
				if (orderLineEntity == null) {
					core.setV_errMsg("合同头或合同行或交货行已作废!");
					return core;
				}
				BeanUtils.copyProperties(billMEntity, planCEntity);
				billMEntity.setStdCode(orderLineEntity.getStdCode());
				billMEntity.setStdName(orderLineEntity.getStdName());
				billMEntity.setSteelcode(orderLineEntity.getSteelcode());
				billMEntity.setSteelname(orderLineEntity.getSteelname());
				billMEntity.setStlmtUnitNo(orderLineEntity.getStlmtUnitNo());
				billMEntity.setStlmtUnitNm(orderLineEntity.getStlmtUnitNm());
				billMEntity.setReceivAddr(orderLineEntity.getReceivAddr());
				billMEntity.setDelvryAddr(orderLineEntity.getDelvryAddr());
				billMEntity
						.setFstDraweeUnit(orderLineEntity.getFstDraweeUnit());
				billMEntity.setAgencyFl(orderLineEntity.getAgencyFl());
				billMEntity.setPickupFlag(orderLineEntity.getPickupFlag());
				billMEntity.setPlanTransType("110502");
				billMEntity.setBillNo(billNo);
				billMEntity.setBillNoSeq(billNoSeq);
				billMEntity.setPlanNum(String.valueOf(sumCount));
				billMEntity.setPlanWt(String.valueOf(sumWgtCan));
				billMEntity.setPlanTotlLen(String.valueOf(sumLen));
				billMEntity.setCreateName(userName);
				billMEntity.setBillStatus("0");
				billMEntity.setLoadvehicleType(virtualFlag);
				billMEntity.setActStartTime(beginTime);
				billMEntity.setActEndTime(endTime);
				billMEntity.setMemo("");
				billMEntity.setCreateTime(strNowTime);
				billMEntity.setCreditNo("");
				billMEntity.setExtraFlag(orderLineEntity.getExtraFlag());
				billMEntity.setItemNo("");
				billMEntity.setSingleNo("");
				billMEntity.setLicenseNo("");
				billMEntity.setSupplyUnit(supplyUnit);
				billMEntity.setSupplyUnitDesc(supplyUnitDesc);

				String sqlAddBillMStr = EntityTool.fillSqlByEntity(sqlAddBillM,
						billMEntity);
				ps = con.prepareStatement(sqlAddBillMStr);
				if("".equals(zbsFlag))
					zbsFlag="1";
				ps.setObject(1, zbsFlag);
				ps.setObject(2, "");
				ps.executeUpdate();
				ps.close();

				psDeryL.setObject(1, userName);
				psDeryL.setObject(2, strNowTime);
				psDeryL.setObject(3, billNo);
				psDeryL.setObject(4, ordLnDlyPk);
				psDeryL.addBatch();
			} else {
				if (!zbsFlag.equals(zbsSource)) {
					core.setV_errMsg("选择炉号与追加提单质保书来源不一致,不能追加!");
					return core;
				}
				// 修改提单主表信息
				ps = con.prepareStatement(sqlupdateBillM);
				ps.setObject(1, String.valueOf(sumCount));
				ps.setObject(2, String.valueOf(sumWgtCan));
				ps.setObject(3, String.valueOf(sumLen));
				ps.setObject(4, billNo);
				ps.setObject(5, sendPlanNo);
				// ps.setObject(6, ordPk);
				// ps.setObject(7, ordLnPk);
				ps.setObject(6, ordLnDlyPk);
				ps.executeUpdate();
				ps.close();

				String sqlquery = CoreXmlSqlParsersFactory
						.getSql("CorePipeCarBill.queryDeryL");
				PreparedStatement psd = con.prepareStatement(sqlquery);
				psd.setObject(1, ordLnDlyPk);
				psd.setObject(2, billNo);
				ResultSet rsd = psd.executeQuery();
				if (!rsd.next()) {
					psDeryL.setObject(1, userName);
					psDeryL.setObject(2, strNowTime);
					psDeryL.setObject(3, billNo);
					psDeryL.setObject(4, ordLnDlyPk);
					psDeryL.addBatch();
				}
			}
			psMat.executeBatch();
			psMat.close();
			ps1.executeBatch();
			ps1.close();

			psSendM.setObject(1, String.valueOf(sumWgtCan));
			psSendM.setObject(2, userName);
			psSendM.setObject(3, strNowTime);
			psSendM.setObject(4, sendPlanNo);
			psSendM.addBatch();
			psSendC.setObject(1, String.valueOf(sumWgtCan));
			psSendC.setObject(2, userName);
			psSendC.setObject(3, strNowTime);
			psSendC.setObject(4, sendPlanNo);
			psSendC.setObject(5, ordLnDlyPk);
			psSendC.addBatch();

			if (flag) {
				core.setV_errMsg("新增提货单成功!");
			} else {
				core.setV_errMsg("修改提货单成功!");
			}
			psSendM.executeBatch();
			psSendC.executeBatch();
			psDeryL.executeBatch();
			con.commit();
		} catch (Exception e) {
			core.setV_errMsg(e.getMessage());
			e.printStackTrace();
			try {
				con.rollback();
			} catch (SQLException e1) {
				e1.printStackTrace();
			}
		} finally {
			if (con != null) {
				try {
					con.close();
				} catch (SQLException e1) {
					e1.printStackTrace();
				}
			}
		}
		return core;
	}

	/**
	 * 修改出库地点
	 * 
	 * @param
	 * @return
	 */
	@SuppressWarnings("unchecked")
	public CoreReturnObject updateStoager(ArrayList<Object> pram) {
		CoreReturnObject core = new CoreReturnObject();
		Connection con = this.getDao(CoreConstantData.sysbeanstr)
				.getConnection();
		PreparedStatement ps = null;
		ResultSet rs = null;
		String sql = "";
		try {
			con.setAutoCommit(false);
			sql = CoreXmlSqlParsersFactory
					.getSql("CorePipeCarBill.QUERYBILLMATNO");
			String sqlU = CoreXmlSqlParsersFactory
					.getSql("FrmPipeTrainBill.UpdateBillCStorage");// 修改提单从表出库地点
			PreparedStatement psupdateBill = con.prepareStatement(sqlU);
			String sqlSend = CoreXmlSqlParsersFactory
					.getSql("FrmPipeTrainBill.Updatebuttress");// 修改位置表出库地点
			PreparedStatement psupdateButt = con.prepareStatement(sqlSend);
			for (int i = 0; i < pram.size(); i++) {
				ArrayList<String> list = (ArrayList<String>) pram.get(i);
				String billNo = list.get(0);
				String justNo = list.get(1);
				String batchNo = list.get(2);
				String groupNo = list.get(3);
				String storag = list.get(4);
				String matNo = "";
				ps = con.prepareStatement(sql);
				ps.setObject(1, billNo);
				ps.setObject(2, justNo);
				ps.setObject(3, batchNo);
				ps.setObject(4, groupNo);
				rs = ps.executeQuery();
				while (rs.next()) {
					matNo = rs.getString("MAT_NO");
					psupdateButt.setObject(1, storag);
					psupdateButt.setObject(2, storag + 10101);
					psupdateButt.setObject(3, matNo);
					psupdateButt.addBatch();

					psupdateBill.setObject(1, storag);
					psupdateBill.setObject(2, storag + 10101);
					psupdateBill.setObject(3, billNo);
					psupdateBill.setObject(4, matNo);
					psupdateBill.addBatch();

				}
				rs.close();
				ps.close();

			}
			psupdateButt.executeBatch();
			psupdateBill.executeBatch();
			core.setResult(1);
			con.commit();
		} catch (SQLException e) {
			try {
				con.rollback();
			} catch (SQLException e1) {
				e1.printStackTrace();
			}
			e.printStackTrace();
		} finally {
			try {
				if (con != null)
					con.close();
			} catch (SQLException e) {
				e.printStackTrace();
			}
		}

		return core;
	}

	/**
	 * 修改装车地点
	 * 
	 * @param billNos
	 * @return
	 */
	@SuppressWarnings("unchecked")
	public CoreReturnObject updateLoad(ArrayList<Object> pram) {
		CoreReturnObject core = new CoreReturnObject();
		Connection con = this.getDao(CoreConstantData.sysbeanstr)
				.getConnection();
		PreparedStatement ps = null;
		ResultSet rs = null;
		String sql = "";
		try {
			con.setAutoCommit(false);
			String sqlU = CoreXmlSqlParsersFactory
					.getSql("FrmPipeTrainBill.UpdateBillCLoad");// 修改提单从表出库地点
			PreparedStatement psupdateBill = con.prepareStatement(sqlU);
			for (int i = 0; i < pram.size(); i++) {
				ArrayList<String> list = (ArrayList<String>) pram.get(i);
				String billNo = list.get(0);
				String justNo = list.get(1);
				String batchNo = list.get(2);
				String groupNo = list.get(3);
				String storag = list.get(4);
				String storagName = list.get(5);
				String matNo = "";
				sql = CoreXmlSqlParsersFactory
						.getSql("CorePipeCarBill.QUERYBILLMATNO");
				ps = con.prepareStatement(sql);
				ps.setObject(1, billNo);
				ps.setObject(2, justNo);
				ps.setObject(3, batchNo);
				ps.setObject(4, groupNo);
				rs = ps.executeQuery();
				while (rs.next()) {
					matNo = rs.getString("MAT_NO");

					psupdateBill.setObject(1, storag);
					psupdateBill.setObject(2, storagName);
					psupdateBill.setObject(3, billNo);
					psupdateBill.setObject(4, matNo);
					psupdateBill.addBatch();

				}
				rs.close();
				ps.close();

			}
			psupdateBill.executeBatch();
			core.setResult(1);
			con.commit();
		} catch (SQLException e) {
			try {
				con.rollback();
			} catch (SQLException e1) {
				e1.printStackTrace();
			}
			e.printStackTrace();
		} finally {
			try {
				if (con != null)
					con.close();
			} catch (SQLException e) {
				e.printStackTrace();
			}
		}

		return core;
	}
}
