package com.steering.pss.ydm.pipeleavefactory;

import CoreFS.SA06.CoreReturnObject;
import CoreFS.SA08.CoreXmlSqlParsersFactory;
import CoreFS.SC01.CoreConstantData;
import CoreFS.Util.StringUtils;
import com.steering.comm.converter.CoreConverter;
import com.steering.comm.data.ComBaseInfo;
import com.steering.comm.db.DBCommon;
import com.steering.comm.tools.EntityTool;
import com.steering.comm.tools.StringTool;
import com.steering.pss.sale.order.orderchange.CrmBillService;
import com.steering.pss.ydm.base.MeanValueCalculation;
import com.steering.pss.ydm.entity.YdmZcLoadvehicleMatEntity;

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

/**
 * 提单退货红冲
 * 
 * @author Administrator
 */
public class FrmReturnDashed extends DBCommon {

	/**
	 * 查询提单主表信息
	 * 
	 * @param list
	 *            传递参数
	 * @return obj
	 */
	public CoreReturnObject queryBillM(ArrayList<String> params, String[] salgs,String supp) {
		String sql = CoreXmlSqlParsersFactory
				.getSql("FrmReturnDashed.QueryBillM");
		sql += CoreConverter.toSqlConditionString("T.SALE_ORG", salgs);
		// if (status.equals("")) {
		// sql += "  AND M.BILL_STATUS IN ('4','5')";
		// }
		// else {
		// sql += "  AND M.BILL_STATUS ='" + status + "'";
		// }
		if("120504".equals(supp)){
			sql += "AND T.SUPPLY_UNIT=  '" + supp + "'";
		}
		sql += "  GROUP BY T.BILL_NO,T.BILL_STATUS,T.LOADVEHICLE_TYPE,T.ISPRECLOSE,"
				+ "B.BASENAME,T.BILL_TICKET_STATUS, T.OUTSTOCK_TICKET_STATUS, T.TRANSMNY_TICKET_STATUS   ORDER BY T.BILL_NO ASC";
		return this.getData(sql, params.toArray());
	}

	/**
	 * 通过提单号查询炉信息
	 * 
	 * @param billNo
	 *            提单号
	 * @return CoreReturnObject
	 */
	public CoreReturnObject queryBillC(ArrayList<String> list) {
		String sql = CoreXmlSqlParsersFactory
				.getSql("FrmReturnDashed.QueryBillC");
		return this.getData(sql, list.toArray());
	}

	public CoreReturnObject queryLoadMatNo(String billNo, String judgeStoveNo,
			String batchNo, String groupNo, String num) {
		String sql = CoreXmlSqlParsersFactory
				.getSql("FrmReturnDashed.queryLoadMatNo");
		return this.getData(sql, new Object[] { billNo, judgeStoveNo, batchNo,
				groupNo, billNo, judgeStoveNo, batchNo, groupNo, num });
	}

	/***
	 * 查询炉批是否结转
	 * 
	 * @param list
	 * @return
	 */
	public CoreReturnObject selectYearMoth(ArrayList<String> list) {
		String sql = CoreXmlSqlParsersFactory
				.getSql("FrmReturnDashed.selectYearMoth");
		return this.getData(sql, list.toArray());
	}

	/**
	 * 通过销售组织查询退货提单号
	 * 
	 * @param belongNo
	 *            销售组织
	 * @return CoreReturnObject
	 */
	public CoreReturnObject getQueryOutBillNo(String belongNo) {
		String sql = CoreXmlSqlParsersFactory
				.getSql("FrmReturnDashed.getQueryOutBillNo");
		return this.getData(sql, new Object[] { belongNo });
	}

	/**
	 * 通过品名查询是否坯子
	 * 
	 * @param billNo
	 *            提单号
	 * @return CoreReturnObject
	 */
	public CoreReturnObject getQueryProduct(String productNo) {
		String sql = CoreXmlSqlParsersFactory
				.getSql("FrmReturnDashed.getQueryProduct");
		return this.getData(sql, new Object[] { productNo });
	}

	/**
	 * 通过只数查重量
	 * 
	 * @param billNo
	 *            提单号
	 * @return CoreReturnObject
	 */
	public CoreReturnObject queryWeghit(ArrayList<String> list) {
		String sql = CoreXmlSqlParsersFactory
				.getSql("FrmReturnDashed.queryWeghit");
		return this.getData(sql, list.toArray());
	}

	/**
	 * 查询发货实绩表理货单号对应的材料信息
	 * 
	 * @param list
	 *            传递参数
	 * @return obj
	 */
	public CoreReturnObject queryLoadC(String billNo) {
		String sql = CoreXmlSqlParsersFactory
				.getSql("FrmReturnDashed.QueryLoadC");
		// sql += CoreConverter.toSqlConditionString("T.BELONG_CODE", salgs);

		sql += "   GROUP BY T.JUDGE_STOVE_NO, T.BATCH_NO, T.BATCH_GROUD_NO, T.ORDER_NO, "
				+ " T.ORDER_SEQ, T.DELIVERY_NO, T.M_BILL_NO, T.MATERIAL_NAME, T.RECEIV_NM,T.MEMO,"
				+ "T.PRODUCNAME, T.GRADENAME, T.STEELNAME, T.SPEC_NAME, T.MODEL_DESC,"
				+ "T.STORAGE_NO, B.STORAGE_NAME, T.STD_NAME, T.STD_STYLE_DESC, T.BELONG_CODE, "
				+ "T.BELONG_NAME, T.CREATE_NAME, T.ORD_LN_DLY_PK, T.ACT_LEN_MIN, T.ACT_LEN_MAX "
				+ "  ORDER BY CREATE_TIME DESC";
		return this.getData(sql, new Object[] { billNo });
	}

	/**
	 * 根据炉号、批号自动组号
	 * 
	 * @param conn
	 *            c
	 * @param strLcode
	 *            传递参数
	 * @return obj
	 * @throws Exception
	 *             异常处理
	 */
	public String queryGroupNo(Connection conn, String strLcode, String strBatch)
			throws Exception {
		int count = 0;
		PreparedStatement psta = null;
		String sqlLovsqe = CoreXmlSqlParsersFactory
				.getSql("CorePipeInTemp.queryGroupNo");
		psta = conn.prepareStatement(sqlLovsqe);
		psta.setString(1, strLcode);
		psta.setString(2, strBatch);
		psta.setString(3, strLcode);
		psta.setString(4, strBatch);
		ResultSet rs = psta.executeQuery();
		if (rs.next()) {
			String res = rs.getString(1);
			if (res == null) {
				res = "001";
			} else {
				count = Integer.parseInt(res.substring(res.length() - 3,
						res.length()));
			}
		}
		rs.close();
		psta.close();
		return StringTool.formatDecimal(++count, "000");
	}

	/**
	 * 查询发货实绩表理货单号对应的材料信息
	 * 
	 * @param list
	 *            传递参数
	 * @return obj
	 */
	public CoreReturnObject queryOutListBill(ArrayList<String> list,
			String[] salgs,String cusInfo,String returnCode,String returnType) {
		String sql = CoreXmlSqlParsersFactory
				.getSql("FrmReturnDashed.queryOutListBill");
		if (!StringUtils.isEmpty(returnCode)) {
			sql +=    "AND T.RETURN_CODE = '" + returnCode + "'" ;
		}
		if (!StringUtils.isEmpty(returnType)) {
			sql +=    "AND T.RETURN_TYPE = '" + returnType + "'" ;
		}
		sql +=" AND (T.SUPPLY_UNIT_CODE IS NULL OR T.SUPPLY_UNIT_CODE='" + cusInfo +"')";
		sql += CoreConverter.toSqlConditionString("T.BELONG_CODE", salgs);
		sql += "  GROUP BY T.M_BILL_NO,T.HAND_FLAG   ORDER BY CREATE_TIME DESC";
		return this.getData(sql, list.toArray());
	}

	/**
	 * 查询提单状态
	 * 
	 * @param matInfoEntity
	 * @return
	 */
	public CoreReturnObject qureyFlag(String billNo, String ordLnPkNo) {
		return this.getDataBySqlID("FrmReturnDashed.qureyFlag", new Object[] {
				billNo, ordLnPkNo });
	}

	/**
	 * 验证重量
	 * 
	 * @param matInfoEntity
	 * @return
	 */
	public CoreReturnObject checkMatWeight(ArrayList<String> list) {
		String sql = CoreXmlSqlParsersFactory
				.getSql("FrmReturnDashed.checkMatWeight");
		return this.getData(sql, list.toArray());
	}

	/**
	 * 每支材料对应的倒库倒库单号
	 * 
	 * @param conn
	 *            c
	 * @param strlist
	 *            传递参数
	 * @return obj
	 * @throws Exception
	 *             异常处理
	 */
	public static String querySendOut(Connection conn, String strlist)
			throws Exception {
		int seq = 0;
		PreparedStatement psta = null;
		Date dt = new Date();
		SimpleDateFormat matter1 = new SimpleDateFormat("yyMMdd");
		SimpleDateFormat matter = new SimpleDateFormat("yyyy-MM-dd");
		String sqlLovsqe = CoreXmlSqlParsersFactory
				.getSql("FrmReturnDashed.queryOutList");
		psta = conn.prepareStatement(sqlLovsqe);
		psta.setString(1, strlist);
		psta.setString(2, matter.format(dt));
		ResultSet rs = psta.executeQuery();
		if (rs.next()) {
			String res = rs.getString(1);
			if (res == null) {
				res = "HO" + matter1.format(dt) + "00001";
			} else {
				seq = Integer.parseInt(res.substring(res.length() - 5,
						res.length()));
			}
		}
		rs.close();
		psta.close();
		return "HO" + matter1.format(dt)
				+ StringTool.formatDecimal(++seq, "00000");
	}

	/**
	 * 查询提单号
	 * 
	 * @param conn
	 *            c
	 * @param strlist
	 *            传递参数
	 * @return obj
	 * @throws Exception
	 *             异常处理
	 */
	public static String querySendIn(Connection conn, String strlist)
			throws Exception {
		String seq = "";
		PreparedStatement psta = null;
		String sqlLovsqe = CoreXmlSqlParsersFactory.getSql(
				"FrmReturnDashed.queryBillMMax").replace("#BillNO#",
				strlist + "%");
		psta = conn.prepareStatement(sqlLovsqe);
		psta.setString(1, strlist);
		ResultSet rs = psta.executeQuery();
		if (rs.next()) {
			seq = rs.getString("BILL_NO");
		}
		rs.close();
		psta.close();
		return seq;
	}

	/**
	 * 查询提单主表序号
	 * 
	 * @param conn
	 *            c
	 * @param strlist
	 *            传递参数
	 * @return obj
	 * @throws Exception
	 *             异常处理
	 */
	public static String querySendInList(Connection conn, String strlist)
			throws Exception {
		String seq = "";
		PreparedStatement psta = null;
		String sqlLovsqe = CoreXmlSqlParsersFactory
				.getSql("FrmReturnDashed.queryBillMMaxseq");
		psta = conn.prepareStatement(sqlLovsqe);
		psta.setString(1, strlist);
		psta.setString(2, strlist);
		ResultSet rs = psta.executeQuery();
		if (rs.next()) {
			seq = rs.getString(1);
		}
		rs.close();
		psta.close();
		return seq;
	}

	/**
	 * 查询提单材料表序号
	 * 
	 * @param conn
	 *            c
	 * @param strlist
	 *            传递参数
	 * @return obj
	 * @throws Exception
	 *             异常处理
	 */
	public static String queryBillCSeq(Connection conn, String matNo)
			throws Exception {
		String seq = "";
		PreparedStatement psta = null;
		String sqlLovsqe = CoreXmlSqlParsersFactory
				.getSql("FrmReturnDashed.queryBillCseq");
		psta = conn.prepareStatement(sqlLovsqe);
		psta.setString(1, matNo);
		psta.setString(2, matNo);
		ResultSet rs = psta.executeQuery();
		if (rs.next()) {
			seq = rs.getString(1);
		}
		rs.close();
		psta.close();
		return seq;
	}

	/**
	 * 退货根据提单号生成装车单号
	 * 
	 * @param conn
	 *            c
	 * @param strlist
	 *            传递参数
	 * @return obj
	 * @throws Exception
	 *             异常处理
	 */
	public static String queryLoadNo(Connection conn, String strlist)
			throws Exception {
		String seq = "";
		PreparedStatement psta = null;
		String sqlLovsqe = CoreXmlSqlParsersFactory
				.getSql("FrmReturnDashed.queryLoadNo");
		psta = conn.prepareStatement(sqlLovsqe);
		psta.setString(1, strlist);
		psta.setString(2, strlist);
		ResultSet rs = psta.executeQuery();
		if (rs.next()) {
			seq = rs.getString("LOADVEHICLE_NO");
		}
		rs.close();
		psta.close();
		return seq;
	}

	/**
	 * 生成装车序号
	 * 
	 * @param conn
	 *            c
	 * @param strlist
	 *            传递参数
	 * @return obj
	 * @throws Exception
	 *             异常处理
	 */
	public static String queryLoadMatSeqNo(Connection conn, String strlist)
			throws Exception {
		String seq = "";
		PreparedStatement psta = null;
		String sqlLovsqe = CoreXmlSqlParsersFactory
				.getSql("FrmReturnDashed.queryLoadMatSeqNo");
		psta = conn.prepareStatement(sqlLovsqe);
		psta.setString(1, strlist);
		psta.setString(2, strlist);
		ResultSet rs = psta.executeQuery();
		if (rs.next()) {
			seq = rs.getString("LOADVEHICLE_SQE");
		}
		rs.close();
		psta.close();
		return seq;
	}

	/**
	 * 获取退货最大材料号
	 * 
	 * @param conn
	 *            c
	 * @param strlist
	 *            传递参数
	 * @return obj
	 * @throws Exception
	 *             异常处理
	 */
	public static String qureyMatNo(Connection conn, String jusNo, String bathNo)
			throws Exception {
		String seq = "";
		PreparedStatement psta = null;
		String sqlLovsqe = CoreXmlSqlParsersFactory
				.getSql("FrmReturnDashed.queryMatNoMax");
		psta = conn.prepareStatement(sqlLovsqe);
		psta.setString(1, jusNo);
		psta.setString(2, bathNo);
		psta.setString(3, jusNo);
		psta.setString(4, bathNo);
		psta.setString(5, jusNo);
		psta.setString(6, bathNo);
		psta.setString(7, jusNo);
		psta.setString(8, bathNo);
		ResultSet rs = psta.executeQuery();
		if (rs.next()) {
			seq = rs.getString("MAT_NO");
		}
		rs.close();
		psta.close();
		return seq;
	}

	/**
	 * 新增退货
	 * 
	 * @param pram
	 * @return
	 */
	@SuppressWarnings("unchecked")
	public CoreReturnObject addLoadReturn(ArrayList<Object> pram,
			String strbillNo, String management_no, String management_name,
			String departid, String departname,String storageNoChoose,String cusInfo,
			String returnType,String returnCode,String pipmemoText) {
		CoreReturnObject cro = new CoreReturnObject();
		Connection connection = this.getDao(CoreConstantData.sysbeanstr)
				.getConnection();
		String sqlquery = "";
		Date now = new Date();
		SimpleDateFormat dfT = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		String strNowTime = dfT.format(now);
		try {
			connection.setAutoCommit(false); // 取消自动提交事务
			List<YdmZcLoadvehicleMatEntity> loadCEntitys = null;
			String sqlinsertOut = CoreXmlSqlParsersFactory
					.getSql("FrmReturnDashed.addOutReturn"); // 新增到出库表
			PreparedStatement pstaOut = connection
					.prepareStatement(sqlinsertOut);
			
			String sqlinsertOutXs = CoreXmlSqlParsersFactory
					.getSql("matxs.addOutReturn"); // 新增到销售出库表
			PreparedStatement pstaOutXs = connection
					.prepareStatement(sqlinsertOutXs);
			
			String insetBillM = CoreXmlSqlParsersFactory
					.getSql("FrmReturnDashed.addBillMReturn"); // 新增到提单主表
			PreparedStatement psatBillM = connection
					.prepareStatement(insetBillM);
			String insetBillC = CoreXmlSqlParsersFactory
					.getSql("FrmReturnDashed.addBillCReturn"); // 新增到提单从表
			PreparedStatement pstaBillC = connection
					.prepareStatement(insetBillC);
			String insetLoad = CoreXmlSqlParsersFactory
					.getSql("FrmReturnDashed.addLoadReturn"); // 新增到装车主表
			PreparedStatement pstaLoad = connection.prepareStatement(insetLoad);
			String insetLoadMat = CoreXmlSqlParsersFactory
					.getSql("FrmReturnDashed.addLoadMatReturn"); // 新增到装车从表
			PreparedStatement pstaLoadMat = connection
					.prepareStatement(insetLoadMat);
			// String sqlinsertMatNo = CoreXmlSqlParsersFactory
			// .getSql("FrmReturnDashed.addMatNoReturn"); //
			// 新增材料表（删除材料表MAT_ZC_BACKUP）
			// PreparedStatement pstaMatNo = connection
			// .prepareStatement(sqlinsertMatNo);
			String addMatNo = CoreXmlSqlParsersFactory
					.getSql("FrmReturnDashed.AddMatNoZc");// 新增材料表（删除材料表MAT_ZC_BACKUP）
			PreparedStatement pstaMatNo = connection.prepareStatement(addMatNo);
			String sqlinsertbutter = CoreXmlSqlParsersFactory
					.getSql("FrmReturnDashed.addButterReturn"); // 新增位置表
			PreparedStatement pstabutter = connection
					.prepareStatement(sqlinsertbutter);
			String updateProOrd = CoreXmlSqlParsersFactory
					.getSql("FrmReturnDashed.updateProOrd"); // 回写进程表
			PreparedStatement pstaPro = connection
					.prepareStatement(updateProOrd);

			String insertCrmBillM = CoreXmlSqlParsersFactory
					.getSql("CrmBill.insertCrmBillM");//新增到crm提单m
			PreparedStatement pstaCrmBillM = connection.prepareStatement(insertCrmBillM);
			String insertCrmBillC = CoreXmlSqlParsersFactory
					.getSql("CrmBill.insertCrmBillC");//新增到crm提单c
			PreparedStatement pstaCrmBillC = connection.prepareStatement(insertCrmBillC);
			String insertCrmOut = CoreXmlSqlParsersFactory
					.getSql("CrmOutList.insertCrmOutList"); // 新增到crm出库表
			PreparedStatement pstaCrmOut = connection.prepareStatement(insertCrmOut);
			String addCrmMat = CoreXmlSqlParsersFactory
					.getSql("CrmMatZc.insertCrmMatZc_M");//新增crm材料表，取m
			PreparedStatement psAddCrmMat = connection.prepareStatement(addCrmMat);


			String cratname = "";
			String crattime = "";
			String billNo = "";
			String bill_no = querySendIn(connection, strbillNo);
			double loadCount = 0;
			double loadWeghit = 0;
			double loadlen = 0;
			for (int i = 0; i < pram.size(); i++) {
				String loadNo = queryLoadNo(connection, bill_no);
				ArrayList<String> list = (ArrayList<String>) pram.get(i);
				billNo = list.get(0);
				String ordLnDlyPk = list.get(4);
				String storageNo = list.get(5);
				String judgeNo = list.get(6);
				String batchNo = list.get(7);
				String groupNo = list.get(8);
				cratname = list.get(11);
				crattime = list.get(12);
				String manageNO = list.get(13);
				String manageName = list.get(14);
				String deparId = list.get(15);
				String deparNmae = list.get(16);
				String gbflag = list.get(17);
				String bc = list.get(18);
				String bz = list.get(19);
				String prdCls = list.get(21);
				String stdName = list.get(22);
				double sendnum = Double.parseDouble(list.get(20));
				int count = 0;// 支数
				double weghit = Double.parseDouble(list.get(10));// 退货重量
				double sjweghit = 0;// 实绩重量
				double gbweghit = 0;// 过磅重量

				// 判断是否0支非0吨
				if (list.get(9).equals("0")) {
					count = 1;
				} else {
					count = Integer.parseInt(list.get(9));
				}
				// 判断是否过磅
				if (gbflag.equals("是")) {
					gbweghit = weghit;
				} else {
					sjweghit = weghit;
				}

				double loadnum = 0;// 装车支数
				// double loadwt = 0;// 装车吨数
				// double loadpowt = 0;// 装车过磅吨数
				// 查询要退货的装车材料支数
				sqlquery = CoreXmlSqlParsersFactory
						.getSql("FrmReturnDashed.queryYdmLoadMat");
				PreparedStatement psta = connection.prepareStatement(sqlquery);
				psta.setObject(1, billNo);
				psta.setObject(2, judgeNo);
				psta.setObject(3, batchNo);
				psta.setObject(4, groupNo);
				psta.setObject(5, ordLnDlyPk);
				// psta.setObject(6, vehiNo);
				ResultSet rs = psta.executeQuery();
				if (rs.next()) {
					loadnum = Double.parseDouble(rs.getString("ACT_COUNT"));
					// loadwt = Double.parseDouble(rs.getString("ACT_WEIGHT"));
					// loadpowt = Double.parseDouble(rs
					// .getString("PONDER_GROSS_WT"));
				}
				rs.close();
				psta.close();

				double outnum = 0;// 已退货支数
				// double outdwt = 0;// 已退货吨数
				// double outpowt = 0;// 已退货车过磅吨数
				// 查询已退货的装车材料支数
				sqlquery = CoreXmlSqlParsersFactory
						.getSql("FrmReturnDashed.queryOutLoadMat");
				psta = connection.prepareStatement(sqlquery);
				psta.setObject(1, billNo);
				psta.setObject(2, judgeNo);
				psta.setObject(3, batchNo);
				psta.setObject(4, groupNo);
				psta.setObject(5, ordLnDlyPk);
				// psta.setObject(6, vehiNo);
				rs = psta.executeQuery();
				if (rs.next()) {
					outnum = Double.parseDouble(rs.getString("ACT_COUNT"));
					// outdwt = Double.parseDouble(rs.getString("ACT_WEIGHT"));
					// outpowt = Double.parseDouble(rs
					// .getString("PONDER_GROSS_WT"));
				}
				rs.close();
				psta.close();

				if (loadnum + outnum == 0) {
					cro.setV_errMsg("炉号为：" + judgeNo + ",批号为：" + batchNo
							+ ",组号为：" + groupNo + ",已退完。");
					return cro;
				}

				// 查询要退货的装车材料记录
				sqlquery = CoreXmlSqlParsersFactory.getSql(
						"FrmReturnDashed.queryLoadReturn").replace("#RUMCONT#",
						String.valueOf(Math.abs(count) + 1));
				psta = connection.prepareStatement(sqlquery);
				psta.setObject(1, judgeNo);
				psta.setObject(2, billNo);
				psta.setObject(3, batchNo);
				psta.setObject(4, groupNo);
				psta.setObject(5, billNo);
				psta.setObject(6, ordLnDlyPk);
				psta.setObject(7, storageNo);
				psta.setObject(8, judgeNo);
				psta.setObject(9, batchNo);
				psta.setObject(10, groupNo);
				psta.setObject(11, stdName);
				rs = psta.executeQuery();
				loadCEntitys = EntityTool.getEntitysByRs(rs,
						YdmZcLoadvehicleMatEntity.class);
				rs.close();
				psta.close();

				if (loadCEntitys == null || loadCEntitys.size() == 0) {
					cro.setV_errMsg("装车信息发生变化,请重新获取数据!");
					return cro;
				}
				// String zbsFlag = "";
				// String zbsId = "";
				// String jgZbsId = "";
				// // 查询材料历史表质保书来源，质保书ID
				// sqlquery =
				// "SELECT MAX(T.ZBS_ID) ZBS_ID,MAX(T.ZBS_FLAG) ZBS_FLAG,MAX(T.JG_ZBS_ID) JG_ZBS_ID FROM MAT_ZC_BACKUP T WHERE  T.JUDGE_STOVE_NO =? AND T.BATCH_NO=? AND T.BATCH_GROUD_NO=?";
				// psta = connection.prepareStatement(sqlquery);
				// psta.setObject(1, judgeNo);
				// psta.setObject(2, batchNo);
				// psta.setObject(3, groupNo);
				// rs = psta.executeQuery();
				// if (rs.next()) {
				// zbsFlag = rs.getString("ZBS_FLAG");
				// zbsId = rs.getString("ZBS_ID");
				// jgZbsId = rs.getString("JG_ZBS_ID");
				// }
				// rs.close();
				// psta.close();
				String sendOut = querySendOut(connection, "800806");
				// 获取出库流水号
				String butessMatNo = FrmPipeEntruck.querySendOutList(
						connection, sendOut, "800806");
				int outNo = Integer.parseInt(butessMatNo.substring(
						butessMatNo.length() - 4, butessMatNo.length()));
				// 获取材料号
				// String matNoMax = qureyMatNo(connection, judgeNo, batchNo);
				// int matNo = Integer.parseInt(matNoMax.substring(
				// matNoMax.length() - 4, matNoMax.length()));
				String loadSeq = queryLoadMatSeqNo(connection, loadNo);
				int loadSeqNo = Integer.parseInt(loadSeq.substring(
						loadSeq.length() - 5, loadSeq.length()));
				double billCount = 0;
				double billlen = 0;
				if (list.get(9).equals("0")) {// 0支非0吨
					for (YdmZcLoadvehicleMatEntity loadCEntity : loadCEntitys) {
						loadCount = 0;
						loadlen = 0;
						loadWeghit = weghit;
						// 新增出库表
						pstaOut.setString(
								1,
								sendOut
										+ StringTool.formatDecimal(outNo,
										"0000"));
						pstaOut.setString(2, sendOut);
						pstaOut.setString(3, "800806");
						pstaOut.setString(4, "出库红冲");
						pstaOut.setString(5, loadCEntity.getVehicleNo());
						pstaOut.setString(6, loadCEntity.getMatNo());
						pstaOut.setString(7, loadCEntity.getLotNo());
						pstaOut.setString(8, loadCEntity.getStoveNo());
						pstaOut.setString(9, loadCEntity.getJudgeStoveNo());
						pstaOut.setString(10, loadCEntity.getBatchNo());
						pstaOut.setString(11, loadCEntity.getGroupNo());
						pstaOut.setString(12, loadCEntity.getMaterialNo());
						pstaOut.setString(13, loadCEntity.getMaterialName());
						pstaOut.setString(14, loadCEntity.getProduccode());
						pstaOut.setString(15, loadCEntity.getProducname());
						pstaOut.setString(16, loadCEntity.getStdCode());
						pstaOut.setString(17, loadCEntity.getStdName());
						pstaOut.setString(18, loadCEntity.getGradecode());
						pstaOut.setString(19, loadCEntity.getGradename());
						pstaOut.setString(20, loadCEntity.getSteelcode());
						pstaOut.setString(21, loadCEntity.getSteelname());
						pstaOut.setString(22, loadCEntity.getStdStyle());
						pstaOut.setString(23, loadCEntity.getStdStyleDesc());
						pstaOut.setString(24, loadCEntity.getSpecCode());
						pstaOut.setString(25, loadCEntity.getSpecName());
						pstaOut.setString(26, loadCEntity.getModelCode());
						pstaOut.setString(27, loadCEntity.getModelDesc());
						pstaOut.setString(28, loadCEntity.getPlineCode());
						pstaOut.setString(29, loadCEntity.getPlineName());
						pstaOut.setString(30, loadCEntity.getActDimater());
						pstaOut.setString(31, loadCEntity.getActHeight());
						pstaOut.setString(32, "0");// 长度
						pstaOut.setString(33, loadCEntity.getActLenMin());
						pstaOut.setString(34, loadCEntity.getActLenMax());
						pstaOut.setString(35, "0");// 支数
						pstaOut.setObject(36, String.valueOf(weghit));// 重量
						pstaOut.setString(37, "0");// 理重
						if ("".equals(storageNoChoose)) {
							pstaOut.setString(38, loadCEntity.getStorageNo());
							pstaOut.setString(39, loadCEntity.getLocationNo());
						} else {
							pstaOut.setString(38, storageNoChoose);
							pstaOut.setString(39, storageNoChoose + "10101");
						}
						pstaOut.setString(40, loadCEntity.getLaryNo());
						pstaOut.setString(41, loadCEntity.getLocNo());
						pstaOut.setString(42, "");
						pstaOut.setString(43, "");
						pstaOut.setString(44, loadCEntity.getStorageTypeNo());
						pstaOut.setString(45, loadCEntity.getStorageTypeName());
						pstaOut.setString(46, manageNO);
						pstaOut.setString(47, manageName);
						if (loadCEntity.getRpoTime() == "") {
							pstaOut.setString(48, "");
						} else {
							pstaOut.setString(48, loadCEntity.getRpoTime()
									.substring(0, 19));
						}
						pstaOut.setString(49, loadCEntity.getProBc());
						pstaOut.setString(50, loadCEntity.getProBz());
						pstaOut.setString(51, crattime);
						pstaOut.setString(52, bc);
						pstaOut.setString(53, bz);
						pstaOut.setString(54, cratname);
						pstaOut.setString(55, crattime);
						pstaOut.setString(56, loadCEntity.getOrdPk());
						pstaOut.setString(57, loadCEntity.getOrdLnPk());
						pstaOut.setString(58, loadCEntity.getOrdLnDlyPk());
						pstaOut.setString(59, loadCEntity.getOrderNo());
						pstaOut.setString(60, loadCEntity.getOrderSeq());
						pstaOut.setString(61, loadCEntity.getOrderSeqNo());
						pstaOut.setString(62, deparId);
						pstaOut.setString(63, deparNmae);
						pstaOut.setString(64, loadCEntity.getSaleOrg());
						pstaOut.setString(65, loadCEntity.getSaleOrgDesc());
						pstaOut.setString(66, loadCEntity.getPonderNo());
						pstaOut.setObject(67, String.valueOf(weghit));
						pstaOut.setObject(68, bill_no);
						pstaOut.setObject(69, String.valueOf(sendnum));
						pstaOut.setObject(70, loadCEntity.getVstel());
						pstaOut.setObject(71, loadCEntity.getVstelDesc());
						pstaOut.setObject(72, "0");
						pstaOut.setObject(73, "801403");
						pstaOut.setObject(74, "80150301");
						pstaOut.setObject(75, loadCEntity.getReceivNo());
						pstaOut.setObject(76, loadCEntity.getReceivNm());
						pstaOut.setObject(77, "");
						pstaOut.setObject(78, loadCEntity.getFinishproMold());
						pstaOut.setObject(79, "801704");
						pstaOut.setObject(80, cusInfo);
						pstaOut.setObject(81, cusInfo);
						pstaOut.setObject(82, loadCEntity.getMatNo());
						pstaOut.setObject(83, loadCEntity.getMatNo());
						pstaOut.setObject(84, loadCEntity.getMatNo());
						pstaOut.setObject(85, "120504".equals(cusInfo)? ComBaseInfo.GetBalYear_2416(now): ComBaseInfo.GetBalYear(now));
						pstaOut.setObject(86, loadCEntity.getMatNo());
						pstaOut.setObject(87, returnType);
						pstaOut.setObject(88, returnCode);
						pstaOut.addBatch();

						pstaOutXs.setString(
								1,
								sendOut
										+ StringTool.formatDecimal(outNo,
										"0000"));
						pstaOutXs.setString(2, sendOut);
						pstaOutXs.setString(3, "800806");
						pstaOutXs.setString(4, "出库红冲");
						pstaOutXs.setString(5, loadCEntity.getVehicleNo());
						pstaOutXs.setString(6, loadCEntity.getMatNo());
						pstaOutXs.setString(7, loadCEntity.getLotNo());
						pstaOutXs.setString(8, loadCEntity.getStoveNo());
						pstaOutXs.setString(9, loadCEntity.getJudgeStoveNo());
						pstaOutXs.setString(10, loadCEntity.getBatchNo());
						pstaOutXs.setString(11, loadCEntity.getGroupNo());
						pstaOutXs.setString(12, loadCEntity.getMaterialNo());
						pstaOutXs.setString(13, loadCEntity.getMaterialName());
						pstaOutXs.setString(14, loadCEntity.getProduccode());
						pstaOutXs.setString(15, loadCEntity.getProducname());
						pstaOutXs.setString(16, loadCEntity.getStdCode());
						pstaOutXs.setString(17, loadCEntity.getStdName());
						pstaOutXs.setString(18, loadCEntity.getGradecode());
						pstaOutXs.setString(19, loadCEntity.getGradename());
						pstaOutXs.setString(20, loadCEntity.getSteelcode());
						pstaOutXs.setString(21, loadCEntity.getSteelname());
						pstaOutXs.setString(22, loadCEntity.getStdStyle());
						pstaOutXs.setString(23, loadCEntity.getStdStyleDesc());
						pstaOutXs.setString(24, loadCEntity.getSpecCode());
						pstaOutXs.setString(25, loadCEntity.getSpecName());
						pstaOutXs.setString(26, loadCEntity.getModelCode());
						pstaOutXs.setString(27, loadCEntity.getModelDesc());
						pstaOutXs.setString(28, loadCEntity.getPlineCode());
						pstaOutXs.setString(29, loadCEntity.getPlineName());
						pstaOutXs.setString(30, loadCEntity.getActDimater());
						pstaOutXs.setString(31, loadCEntity.getActHeight());
						pstaOutXs.setString(32, "0");// 长度
						pstaOutXs.setString(33, loadCEntity.getActLenMin());
						pstaOutXs.setString(34, loadCEntity.getActLenMax());
						pstaOutXs.setString(35, "0");// 支数
						pstaOutXs.setObject(36, String.valueOf(weghit));// 重量
						pstaOutXs.setString(37, "0");// 理重
						if ("".equals(storageNoChoose)) {
							pstaOutXs.setString(38, loadCEntity.getStorageNo());
							pstaOutXs.setString(39, loadCEntity.getLocationNo());
						} else {
							pstaOutXs.setString(38, storageNoChoose);
							pstaOutXs.setString(39, storageNoChoose + "10101");
						}
						pstaOutXs.setString(40, loadCEntity.getLaryNo());
						pstaOutXs.setString(41, loadCEntity.getLocNo());
						pstaOutXs.setString(42, "");
						pstaOutXs.setString(43, "");
						pstaOutXs.setString(44, loadCEntity.getStorageTypeNo());
						pstaOutXs.setString(45, loadCEntity.getStorageTypeName());
						pstaOutXs.setString(46, manageNO);
						pstaOutXs.setString(47, manageName);
						if (loadCEntity.getRpoTime() == "") {
							pstaOutXs.setString(48, "");
						} else {
							pstaOutXs.setString(48, loadCEntity.getRpoTime()
									.substring(0, 19));
						}
						pstaOutXs.setString(49, loadCEntity.getProBc());
						pstaOutXs.setString(50, loadCEntity.getProBz());
						pstaOutXs.setString(51, crattime);
						pstaOutXs.setString(52, bc);
						pstaOutXs.setString(53, bz);
						pstaOutXs.setString(54, cratname);
						pstaOutXs.setString(55, crattime);
						pstaOutXs.setString(56, loadCEntity.getOrdPk());
						pstaOutXs.setString(57, loadCEntity.getOrdLnPk());
						pstaOutXs.setString(58, loadCEntity.getOrdLnDlyPk());
						pstaOutXs.setString(59, loadCEntity.getOrderNo());
						pstaOutXs.setString(60, loadCEntity.getOrderSeq());
						pstaOutXs.setString(61, loadCEntity.getOrderSeqNo());
						pstaOutXs.setString(62, deparId);
						pstaOutXs.setString(63, deparNmae);
						pstaOutXs.setString(64, loadCEntity.getSaleOrg());
						pstaOutXs.setString(65, loadCEntity.getSaleOrgDesc());
						pstaOutXs.setString(66, loadCEntity.getPonderNo());
						pstaOutXs.setObject(67, String.valueOf(weghit));
						pstaOutXs.setObject(68, bill_no);
						pstaOutXs.setObject(69, String.valueOf(sendnum));
						pstaOutXs.setObject(70, loadCEntity.getVstel());
						pstaOutXs.setObject(71, loadCEntity.getVstelDesc());
						pstaOutXs.setObject(72, "0");
						pstaOutXs.setObject(73, "801403");
						pstaOutXs.setObject(74, "80150301");
						pstaOutXs.setObject(75, loadCEntity.getReceivNo());
						pstaOutXs.setObject(76, loadCEntity.getReceivNm());
						pstaOutXs.setObject(77, "");
						pstaOutXs.setObject(78, loadCEntity.getFinishproMold());
						pstaOutXs.setObject(79, "801704");
						pstaOutXs.setObject(80, cusInfo);
						pstaOutXs.setObject(81, cusInfo);
						pstaOutXs.setObject(82, loadCEntity.getMatNo());
						pstaOutXs.setObject(83, loadCEntity.getMatNo());
						pstaOutXs.setObject(84, loadCEntity.getMatNo());
						pstaOutXs.setObject(85, "120504".equals(cusInfo)? ComBaseInfo.GetBalYear_2416(now): ComBaseInfo.GetBalYear(now));
						pstaOutXs.setObject(86, loadCEntity.getMatNo());
						pstaOutXs.setObject(87, returnType);
						pstaOutXs.setObject(88, returnCode);
						pstaOutXs.addBatch();
						
						//crm 出库表迁移
						pstaCrmOut.setString(1, loadNo
								+ StringTool.formatDecimal(loadSeqNo,"00000"));
						pstaCrmOut.setString(2, bill_no);
						pstaCrmOut.setString(3, loadCEntity.getLoadvehicleNo());
						pstaCrmOut.setString(4, loadCEntity.getJudgeStoveNo());
						pstaCrmOut.setString(5, loadCEntity.getBatchNo());
						pstaCrmOut.setString(6, loadCEntity.getGroupNo());
						pstaCrmOut.setString(7, loadCEntity.getMaterialNo());
						pstaCrmOut.setString(8, loadCEntity.getMaterialName());
						pstaCrmOut.setString(9, loadCEntity.getProduccode());
						pstaCrmOut.setString(10, loadCEntity.getProducname());
						pstaCrmOut.setString(11, loadCEntity.getStdCode());
						pstaCrmOut.setString(12, loadCEntity.getStdName());
						pstaCrmOut.setString(13, loadCEntity.getGradecode());
						pstaCrmOut.setString(14, loadCEntity.getGradename());
						pstaCrmOut.setString(15, loadCEntity.getSteelcode());
						pstaCrmOut.setString(16, loadCEntity.getSteelname());
						pstaCrmOut.setString(17, loadCEntity.getStdStyle());
						pstaCrmOut.setString(18, loadCEntity.getStdStyleDesc());
						pstaCrmOut.setString(19, loadCEntity.getSpecCode());
						pstaCrmOut.setString(20, loadCEntity.getSpecName());
						pstaCrmOut.setString(21, loadCEntity.getModelCode());
						pstaCrmOut.setString(22, loadCEntity.getModelDesc());
						pstaCrmOut.setString(23, loadCEntity.getActLenMin());
						pstaCrmOut.setString(24, loadCEntity.getActLenMax());
						pstaCrmOut.setString(25, "0");
						pstaCrmOut.setString(26, String.valueOf(weghit));
						pstaCrmOut.setString(27, "0");
						pstaCrmOut.setString(28, loadCEntity.getStorageNo());
						pstaCrmOut.setString(29, loadCEntity.getStorageNo());
						pstaCrmOut.setString(30, loadCEntity.getSenTime());
						pstaCrmOut.setString(31, loadCEntity.getValidflag());
						pstaCrmOut.setString(32, loadCEntity.getOrderNo());
						pstaCrmOut.setString(33, loadCEntity.getOrderSeq());
						pstaCrmOut.setString(34, loadCEntity.getOrderSeqNo());
						pstaCrmOut.setString(35, loadCEntity.getOrdPk());
						pstaCrmOut.setString(36, loadCEntity.getOrdLnPk());
						pstaCrmOut.setString(37, loadCEntity.getOrdLnDlyPk());
						pstaCrmOut.setString(38, "0");
						pstaCrmOut.setString(39, strNowTime);
						pstaCrmOut.setString(40, billNo);
						pstaCrmOut.setString(41,billNo);
						pstaCrmOut.setString(42,billNo);
						pstaCrmOut.setString(43,"120504".equals(cusInfo)? ComBaseInfo.GetBalYear_2416(now): ComBaseInfo.GetBalYear(now));
						pstaCrmOut.setString(44,loadCEntity.getActDimater());
						pstaCrmOut.setString(45,loadCEntity.getActHeight());
						pstaCrmOut.setString(46,loadCEntity.getSaleOrg());
						pstaCrmOut.setString(47,loadCEntity.getSaleOrgDesc());
						pstaCrmOut.setString(48, "800806");
						pstaCrmOut.setString(49, "出库红冲");
						pstaCrmOut.addBatch();

						// 提单从表
						String billCseq = queryBillCSeq(connection,
								loadCEntity.getMatNo());
						pstaBillC.setObject(1, billCseq);
						pstaBillC.setObject(2, bill_no);
						pstaBillC.setObject(3, loadCEntity.getMatNo());
						pstaBillC.setObject(4, "0");
						pstaBillC.setObject(5, "0");
						pstaBillC.setObject(6, String.valueOf(weghit));
						pstaBillC.setObject(7, "0");
						pstaBillC.setObject(8, cratname);
						pstaBillC.setObject(9, crattime);
						pstaBillC.setObject(10, String.valueOf(sendnum));
						pstaBillC.setObject(11, "801704");
						pstaBillC.setObject(12, loadCEntity.getMatNo());
						pstaBillC.setObject(13, billNo);
						pstaBillC.addBatch();

						// 装车从表
						pstaLoadMat.setObject(
								1,
								loadNo
										+ StringTool.formatDecimal(loadSeqNo,
										"00000"));
						pstaLoadMat.setObject(2, loadNo);
						pstaLoadMat.setObject(3, bill_no);
						pstaLoadMat.setObject(4, loadCEntity.getMatNo());
						pstaLoadMat.setObject(5, "0");
						pstaLoadMat.setObject(6, "0");
						pstaLoadMat.setObject(7, String.valueOf(weghit));
						pstaLoadMat.setObject(8, "0");
						pstaLoadMat.setObject(9, crattime);
						pstaLoadMat.setObject(10, cratname);
						pstaLoadMat.setObject(11, crattime);
						pstaLoadMat.setObject(12, String.valueOf(weghit));
						pstaLoadMat.setObject(13, String.valueOf(sendnum));
						pstaLoadMat.setObject(14, "801704");
						pstaLoadMat.setObject(15, "120504".equals(cusInfo)? ComBaseInfo.GetBalYear_2416(now): ComBaseInfo.GetBalYear(now));
						pstaLoadMat.setObject(16, loadCEntity.getMatNo());
						pstaLoadMat.setObject(17, billNo);
						pstaLoadMat.addBatch();
						loadSeqNo++;
						// matNo++;
					}
					pstaPro.setObject(1, Math.abs(count));
					pstaPro.setObject(2, Math.abs(weghit));
					pstaPro.setObject(3, String.valueOf(Math.abs(sendnum)));
					pstaPro.setObject(4, ordLnDlyPk);
					pstaPro.addBatch();

					// pstaOut.executeBatch();
					// pstaPro.executeBatch();
					// 装车主表
					pstaLoad.setObject(1, loadNo);
					pstaLoad.setObject(2, bill_no);
					pstaLoad.setObject(3, String.valueOf(loadWeghit));
					pstaLoad.setObject(4, String.valueOf(-loadCount));
					pstaLoad.setObject(5, String.valueOf(-loadlen));
					pstaLoad.setObject(6, crattime);
					pstaLoad.setObject(7, management_no);
					pstaLoad.setObject(8, management_name);
					pstaLoad.setObject(9, cratname);
					pstaLoad.setObject(10, crattime);
					pstaLoad.setObject(11, departid);
					pstaLoad.setObject(12, departname);
					pstaLoad.setObject(13, "1");
					pstaLoad.setObject(14, billNo);
					pstaLoad.setObject(15, prdCls);
					pstaLoad.addBatch();
					pstaLoad.executeBatch();
				} else {// 非0支非0吨
					List<double[]> lenList = MeanValueCalculation
							.MeanCalculation(Math.abs(weghit), Math.abs(count));
					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]));
							}
						}
					}
					List<double[]> lensendnum = MeanValueCalculation
							.MeanCalculation(Math.abs(sendnum), Math.abs(count));
					List<String> len1 = new ArrayList<String>();// 计算每只退货实发量
					for (int k = 0; k < lensendnum.size(); k++) {
						double[] conLen1 = lensendnum.get(k);
						if ((int) conLen1[0] >= 0) {
							for (int h = 0; h < (int) conLen1[0]; h++) {
								len1.add(String.valueOf(conLen1[1]));
							}
						}
					}
					int w = 0;
					int s = 0;
					for (YdmZcLoadvehicleMatEntity loadCEntity : loadCEntitys) {
						billCount += Double.parseDouble(loadCEntity
								.getActCount());
						billlen += Double.parseDouble(loadCEntity.getActLen());
						double weghitgb = Double.parseDouble(loadCEntity
								.getPonderGrossWt());
						double weghitsj = Double.parseDouble(loadCEntity
								.getActWeight());
						if (gbflag.equals("是")) {
							weghitgb = Double.parseDouble(len.get(w));
						} else {
							weghitsj = Double.parseDouble(len.get(w));
						}
						loadCount += Double.parseDouble(loadCEntity
								.getActCount());
						loadlen += Double.parseDouble(loadCEntity.getActLen());
						loadWeghit += -weghitsj;

						// 新增材料表
						// pstaMatNo.setObject(1, loadCEntity.getMatNo());
						// pstaMatNo.setObject(2, "801403");
						// pstaMatNo.setObject(3, "80150301");
						// pstaMatNo.setObject(4, loadCEntity.getLotNo());
						// pstaMatNo.setObject(5, loadCEntity.getStoveNo());
						// pstaMatNo.setObject(6,
						// loadCEntity.getJudgeStoveNo());
						// pstaMatNo.setObject(7, loadCEntity.getBatchNo());
						// pstaMatNo.setObject(8, loadCEntity.getGroupNo());
						// pstaMatNo.setObject(9, loadCEntity.getMaterialNo());
						// pstaMatNo.setObject(10,
						// loadCEntity.getMaterialName());
						// pstaMatNo.setObject(11, loadCEntity.getProduccode());
						// pstaMatNo.setObject(12, loadCEntity.getProducname());
						// pstaMatNo.setObject(13, loadCEntity.getSteelcode());
						// pstaMatNo.setObject(14, loadCEntity.getSteelname());
						// pstaMatNo.setObject(15, loadCEntity.getGradecode());
						// pstaMatNo.setObject(16, loadCEntity.getGradename());
						// pstaMatNo.setObject(17, loadCEntity.getSpecCode());
						// pstaMatNo.setObject(18, loadCEntity.getSpecName());
						// pstaMatNo.setObject(19, loadCEntity.getModelCode());
						// pstaMatNo.setObject(20, loadCEntity.getModelDesc());
						// pstaMatNo.setObject(21, loadCEntity.getStdCode());
						// pstaMatNo.setObject(22, loadCEntity.getStdName());
						// pstaMatNo.setObject(23, loadCEntity.getStdStyle());
						// pstaMatNo.setObject(24,
						// loadCEntity.getStdStyleDesc());
						// pstaMatNo.setObject(25, loadCEntity.getProOrderNo());
						// pstaMatNo.setObject(26, loadCEntity.getOrderNo());
						// pstaMatNo.setObject(27, loadCEntity.getOrderSeq());
						// pstaMatNo.setObject(28, loadCEntity.getOrderSeqNo());
						// pstaMatNo.setObject(29,
						// loadCEntity.getProcessCdoe());
						// pstaMatNo.setObject(30,
						// loadCEntity.getProcessDesc());
						// pstaMatNo.setObject(31, loadCEntity.getPlineCode());
						// pstaMatNo.setObject(32, loadCEntity.getPlineName());
						// pstaMatNo.setObject(33, loadCEntity.getActDimater());
						// pstaMatNo.setObject(34, loadCEntity.getActHeight());
						// pstaMatNo.setObject(35,
						// String.valueOf(loadCEntity.getActLen()));
						// pstaMatNo.setObject(36, loadCEntity.getActLenMin());
						// pstaMatNo.setObject(37, loadCEntity.getActLenMax());
						// pstaMatNo.setObject(38, loadCEntity.getActCount());
						// pstaMatNo.setObject(39, String.valueOf(weghitsj));
						// pstaMatNo.setObject(40, String.valueOf(loadCEntity
						// .getActTheoryWeight()));
						// pstaMatNo.setObject(41, loadCEntity.getSaleOrg());
						// pstaMatNo.setObject(42,
						// loadCEntity.getSaleOrgDesc());
						// pstaMatNo.setObject(43, crattime);
						// pstaMatNo.setObject(44, "1");
						// pstaMatNo.setObject(45, loadCEntity.getProBc());
						// pstaMatNo.setObject(46, loadCEntity.getProBz());
						// if (loadCEntity.getRpoTime() == "") {
						// pstaMatNo.setObject(47, "");
						// } else {
						// pstaMatNo.setObject(47, loadCEntity.getRpoTime()
						// .substring(0, 19));
						// }
						// pstaMatNo.setObject(48, cratname);
						// pstaMatNo.setObject(49, crattime);
						// pstaMatNo.setObject(50, loadCEntity.getOrdPk());
						// pstaMatNo.setObject(51, loadCEntity.getOrdLnPk());
						// pstaMatNo.setObject(52, loadCEntity.getOrdLnDlyPk());
						// pstaMatNo.setObject(53,
						// loadCEntity.getJudgeStdName());
						// pstaMatNo.setObject(54,
						// loadCEntity.getJudgeStdCode());
						// pstaMatNo.setObject(55, loadCEntity.getPonderNo());
						// pstaMatNo.setObject(56, String.valueOf(weghitgb));
						// pstaMatNo.setObject(57, "");
						// pstaMatNo.setObject(58,
						// loadCEntity.getFinishproMold());
						// pstaMatNo.setObject(59, "801704");
						// pstaMatNo.setObject(60, zbsFlag);
						// pstaMatNo.setObject(61, zbsId);
						// pstaMatNo.setObject(62, jgZbsId);
						// pstaMatNo.addBatch();
						// 新增材料表MAT_ZC_m（新增材料表，删除mat_zc_backup）1
						pstaMatNo.setObject(1, weghitsj);
						pstaMatNo.setObject(2, pipmemoText);
						pstaMatNo.setObject(3, loadCEntity.getFinishproMold());
						pstaMatNo.setObject(4, "801704");
						pstaMatNo.setObject(5, loadCEntity.getMatNo());
						pstaMatNo.setObject(6, weghitsj);
						pstaMatNo.setObject(7, pipmemoText);
						pstaMatNo.setObject(8, loadCEntity.getFinishproMold());
						pstaMatNo.setObject(9, "801704");
						pstaMatNo.setObject(10, loadCEntity.getMatNo());
						pstaMatNo.setObject(11, loadCEntity.getMatNo());
						pstaMatNo.addBatch();

						//新增crm材料表
						psAddCrmMat.setObject(1, "0");
						psAddCrmMat.setObject(2, strNowTime);
						psAddCrmMat.setObject(3, loadCEntity.getMatNo());
						psAddCrmMat.addBatch();

						// 新增位置表
						pstabutter.setObject(1, loadCEntity.getMatNo());
						pstabutter.setObject(2, loadCEntity.getJudgeStoveNo());
						pstabutter.setObject(3, loadCEntity.getBatchNo());
						pstabutter.setObject(4, loadCEntity.getGroupNo());
						if ("".equals(storageNoChoose)) {
							pstabutter.setObject(5, loadCEntity.getStorageNo());
							pstabutter.setObject(6, loadCEntity.getLocationNo());
						} else {
							pstabutter.setObject(5, storageNoChoose);
							pstabutter.setObject(6, storageNoChoose + "10101");
						}
						pstabutter.setObject(7, loadCEntity.getLaryNo());
						pstabutter.setObject(8, loadCEntity.getLocNo());
						pstabutter.setObject(9, "");
						pstabutter.setObject(10, "");
						pstabutter
								.setObject(11, loadCEntity.getStorageTypeNo());
						pstabutter.setObject(12,
								loadCEntity.getStorageTypeName());
						pstabutter.setObject(13, manageNO);
						pstabutter.setObject(14, manageName);
						pstabutter.setObject(15, cratname);
						pstabutter.setObject(16, crattime);
						pstabutter.setObject(17, deparId);
						pstabutter.setObject(18, deparNmae);
						pstabutter.addBatch();

						// 新增出库表
						pstaOut.setString(
								1,
								sendOut
										+ StringTool.formatDecimal(outNo,
										"0000"));
						pstaOut.setString(2, sendOut);
						pstaOut.setString(3, "800806");
						pstaOut.setString(4, "出库红冲");
						pstaOut.setString(5, loadCEntity.getVehicleNo());
						pstaOut.setString(6, loadCEntity.getMatNo());
						pstaOut.setString(7, loadCEntity.getLotNo());
						pstaOut.setString(8, loadCEntity.getStoveNo());
						pstaOut.setString(9, loadCEntity.getJudgeStoveNo());
						pstaOut.setString(10, loadCEntity.getBatchNo());
						pstaOut.setString(11, loadCEntity.getGroupNo());
						pstaOut.setString(12, loadCEntity.getMaterialNo());
						pstaOut.setString(13, loadCEntity.getMaterialName());
						pstaOut.setString(14, loadCEntity.getProduccode());
						pstaOut.setString(15, loadCEntity.getProducname());
						pstaOut.setString(16, loadCEntity.getStdCode());
						pstaOut.setString(17, loadCEntity.getStdName());
						pstaOut.setString(18, loadCEntity.getGradecode());
						pstaOut.setString(19, loadCEntity.getGradename());
						pstaOut.setString(20, loadCEntity.getSteelcode());
						pstaOut.setString(21, loadCEntity.getSteelname());
						pstaOut.setString(22, loadCEntity.getStdStyle());
						pstaOut.setString(23, loadCEntity.getStdStyleDesc());
						pstaOut.setString(24, loadCEntity.getSpecCode());
						pstaOut.setString(25, loadCEntity.getSpecName());
						pstaOut.setString(26, loadCEntity.getModelCode());
						pstaOut.setString(27, loadCEntity.getModelDesc());
						pstaOut.setString(28, loadCEntity.getPlineCode());
						pstaOut.setString(29, loadCEntity.getPlineName());
						pstaOut.setString(30, loadCEntity.getActDimater());
						pstaOut.setString(31, loadCEntity.getActHeight());
						pstaOut.setString(32, String.valueOf(-Double
								.parseDouble(loadCEntity.getActLen())));// 长度
						pstaOut.setString(33, loadCEntity.getActLenMin());
						pstaOut.setString(34, loadCEntity.getActLenMax());
						pstaOut.setString(35, "-1");// 支数
						pstaOut.setObject(36, String.valueOf(-weghitsj));// 重量
						pstaOut.setString(37, String.valueOf(-Double
								.parseDouble(loadCEntity.getActTheoryWeight())));// 理重
						if ("".equals(storageNoChoose)) {
							pstaOut.setString(38, loadCEntity.getStorageNo());
							pstaOut.setString(39, loadCEntity.getLocationNo());
						} else {
							pstaOut.setString(38, storageNoChoose);
							pstaOut.setString(39, storageNoChoose + "10101");
						}
						pstaOut.setString(40, loadCEntity.getLaryNo());
						pstaOut.setString(41, loadCEntity.getLocNo());
						pstaOut.setString(42, "");
						pstaOut.setString(43, "");
						pstaOut.setString(44, loadCEntity.getStorageTypeNo());
						pstaOut.setString(45, loadCEntity.getStorageTypeName());
						pstaOut.setString(46, manageNO);
						pstaOut.setString(47, manageName);
						if (loadCEntity.getRpoTime() == "") {
							pstaOut.setString(48, "");
						} else {
							pstaOut.setString(48, loadCEntity.getRpoTime()
									.substring(0, 19));
						}
						pstaOut.setString(49, loadCEntity.getProBc());
						pstaOut.setString(50, loadCEntity.getProBz());
						pstaOut.setString(51, crattime);
						pstaOut.setString(52, bc);
						pstaOut.setString(53, bz);
						pstaOut.setString(54, cratname);
						pstaOut.setString(55, crattime);
						pstaOut.setString(56, loadCEntity.getOrdPk());
						pstaOut.setString(57, loadCEntity.getOrdLnPk());
						pstaOut.setString(58, loadCEntity.getOrdLnDlyPk());
						pstaOut.setString(59, loadCEntity.getOrderNo());
						pstaOut.setString(60, loadCEntity.getOrderSeq());
						pstaOut.setString(61, loadCEntity.getOrderSeqNo());
						pstaOut.setString(62, deparId);
						pstaOut.setString(63, deparNmae);
						pstaOut.setString(64, loadCEntity.getSaleOrg());
						pstaOut.setString(65, loadCEntity.getSaleOrgDesc());
						pstaOut.setString(66, loadCEntity.getPonderNo());
						pstaOut.setObject(67, String.valueOf(-weghitgb));
						pstaOut.setObject(68, bill_no);
						pstaOut.setObject(69, String.valueOf(-Double
								.parseDouble(len1.get(s))));
						pstaOut.setObject(70, loadCEntity.getVstel());
						pstaOut.setObject(71, loadCEntity.getVstelDesc());
						pstaOut.setObject(72, "0");
						pstaOut.setObject(73, "801403");
						pstaOut.setObject(74, "80150301");
						pstaOut.setObject(75, loadCEntity.getReceivNo());
						pstaOut.setObject(76, loadCEntity.getReceivNm());
						pstaOut.setObject(77, "");
						pstaOut.setObject(78, loadCEntity.getFinishproMold());
						pstaOut.setObject(79, "801704");
						pstaOut.setObject(80, cusInfo);
						pstaOut.setObject(81, cusInfo);
						pstaOut.setObject(82, loadCEntity.getMatNo());
						pstaOut.setObject(83, loadCEntity.getMatNo());
						pstaOut.setObject(84, loadCEntity.getMatNo());
						pstaOut.setObject(85, "120504".equals(cusInfo)? ComBaseInfo.GetBalYear_2416(now): ComBaseInfo.GetBalYear(now));
						pstaOut.setObject(86, loadCEntity.getMatNo());
						pstaOut.setObject(87, returnType);
						pstaOut.setObject(88, returnCode);
						pstaOut.addBatch();

						pstaOutXs.setString(
								1,
								sendOut
										+ StringTool.formatDecimal(outNo,
										"0000"));
						pstaOutXs.setString(2, sendOut);
						pstaOutXs.setString(3, "800806");
						pstaOutXs.setString(4, "出库红冲");
						pstaOutXs.setString(5, loadCEntity.getVehicleNo());
						pstaOutXs.setString(6, loadCEntity.getMatNo());
						pstaOutXs.setString(7, loadCEntity.getLotNo());
						pstaOutXs.setString(8, loadCEntity.getStoveNo());
						pstaOutXs.setString(9, loadCEntity.getJudgeStoveNo());
						pstaOutXs.setString(10, loadCEntity.getBatchNo());
						pstaOutXs.setString(11, loadCEntity.getGroupNo());
						pstaOutXs.setString(12, loadCEntity.getMaterialNo());
						pstaOutXs.setString(13, loadCEntity.getMaterialName());
						pstaOutXs.setString(14, loadCEntity.getProduccode());
						pstaOutXs.setString(15, loadCEntity.getProducname());
						pstaOutXs.setString(16, loadCEntity.getStdCode());
						pstaOutXs.setString(17, loadCEntity.getStdName());
						pstaOutXs.setString(18, loadCEntity.getGradecode());
						pstaOutXs.setString(19, loadCEntity.getGradename());
						pstaOutXs.setString(20, loadCEntity.getSteelcode());
						pstaOutXs.setString(21, loadCEntity.getSteelname());
						pstaOutXs.setString(22, loadCEntity.getStdStyle());
						pstaOutXs.setString(23, loadCEntity.getStdStyleDesc());
						pstaOutXs.setString(24, loadCEntity.getSpecCode());
						pstaOutXs.setString(25, loadCEntity.getSpecName());
						pstaOutXs.setString(26, loadCEntity.getModelCode());
						pstaOutXs.setString(27, loadCEntity.getModelDesc());
						pstaOutXs.setString(28, loadCEntity.getPlineCode());
						pstaOutXs.setString(29, loadCEntity.getPlineName());
						pstaOutXs.setString(30, loadCEntity.getActDimater());
						pstaOutXs.setString(31, loadCEntity.getActHeight());
						pstaOutXs.setString(32, String.valueOf(-Double
								.parseDouble(loadCEntity.getActLen())));// 长度
						pstaOutXs.setString(33, loadCEntity.getActLenMin());
						pstaOutXs.setString(34, loadCEntity.getActLenMax());
						pstaOutXs.setString(35, "-1");// 支数
						pstaOutXs.setObject(36, String.valueOf(-weghitsj));// 重量
						pstaOutXs.setString(37, String.valueOf(-Double
								.parseDouble(loadCEntity.getActTheoryWeight())));// 理重
						if ("".equals(storageNoChoose)) {
							pstaOutXs.setString(38, loadCEntity.getStorageNo());
							pstaOutXs.setString(39, loadCEntity.getLocationNo());
						} else {
							pstaOutXs.setString(38, storageNoChoose);
							pstaOutXs.setString(39, storageNoChoose + "10101");
						}
						pstaOutXs.setString(40, loadCEntity.getLaryNo());
						pstaOutXs.setString(41, loadCEntity.getLocNo());
						pstaOutXs.setString(42, "");
						pstaOutXs.setString(43, "");
						pstaOutXs.setString(44, loadCEntity.getStorageTypeNo());
						pstaOutXs.setString(45, loadCEntity.getStorageTypeName());
						pstaOutXs.setString(46, manageNO);
						pstaOutXs.setString(47, manageName);
						if (loadCEntity.getRpoTime() == "") {
							pstaOutXs.setString(48, "");
						} else {
							pstaOutXs.setString(48, loadCEntity.getRpoTime()
									.substring(0, 19));
						}
						pstaOutXs.setString(49, loadCEntity.getProBc());
						pstaOutXs.setString(50, loadCEntity.getProBz());
						pstaOutXs.setString(51, crattime);
						pstaOutXs.setString(52, bc);
						pstaOutXs.setString(53, bz);
						pstaOutXs.setString(54, cratname);
						pstaOutXs.setString(55, crattime);
						pstaOutXs.setString(56, loadCEntity.getOrdPk());
						pstaOutXs.setString(57, loadCEntity.getOrdLnPk());
						pstaOutXs.setString(58, loadCEntity.getOrdLnDlyPk());
						pstaOutXs.setString(59, loadCEntity.getOrderNo());
						pstaOutXs.setString(60, loadCEntity.getOrderSeq());
						pstaOutXs.setString(61, loadCEntity.getOrderSeqNo());
						pstaOutXs.setString(62, deparId);
						pstaOutXs.setString(63, deparNmae);
						pstaOutXs.setString(64, loadCEntity.getSaleOrg());
						pstaOutXs.setString(65, loadCEntity.getSaleOrgDesc());
						pstaOutXs.setString(66, loadCEntity.getPonderNo());
						pstaOutXs.setObject(67, String.valueOf(-weghitgb));
						pstaOutXs.setObject(68, bill_no);
						pstaOutXs.setObject(69, String.valueOf(-Double
								.parseDouble(len1.get(s))));
						pstaOutXs.setObject(70, loadCEntity.getVstel());
						pstaOutXs.setObject(71, loadCEntity.getVstelDesc());
						pstaOutXs.setObject(72, "0");
						pstaOutXs.setObject(73, "801403");
						pstaOutXs.setObject(74, "80150301");
						pstaOutXs.setObject(75, loadCEntity.getReceivNo());
						pstaOutXs.setObject(76, loadCEntity.getReceivNm());
						pstaOutXs.setObject(77, "");
						pstaOutXs.setObject(78, loadCEntity.getFinishproMold());
						pstaOutXs.setObject(79, "801704");
						pstaOutXs.setObject(80, cusInfo);
						pstaOutXs.setObject(81, cusInfo);
						pstaOutXs.setObject(82, loadCEntity.getMatNo());
						pstaOutXs.setObject(83, loadCEntity.getMatNo());
						pstaOutXs.setObject(84, loadCEntity.getMatNo());
						pstaOutXs.setObject(85, "120504".equals(cusInfo)? ComBaseInfo.GetBalYear_2416(now): ComBaseInfo.GetBalYear(now));
						pstaOutXs.setObject(86, loadCEntity.getMatNo());
						pstaOutXs.setObject(87, returnType);
						pstaOutXs.setObject(88, returnCode);
						pstaOutXs.addBatch();
						
						//crm 出库表迁移
						pstaCrmOut.setString(1, loadNo
								+ StringTool.formatDecimal(loadSeqNo,"00000"));
						pstaCrmOut.setString(2, bill_no);
						pstaCrmOut.setString(3, loadCEntity.getLoadvehicleNo());
						pstaCrmOut.setString(4, loadCEntity.getJudgeStoveNo());
						pstaCrmOut.setString(5, loadCEntity.getBatchNo());
						pstaCrmOut.setString(6, loadCEntity.getGroupNo());
						pstaCrmOut.setString(7, loadCEntity.getMaterialNo());
						pstaCrmOut.setString(8, loadCEntity.getMaterialName());
						pstaCrmOut.setString(9, loadCEntity.getProduccode());
						pstaCrmOut.setString(10, loadCEntity.getProducname());
						pstaCrmOut.setString(11, loadCEntity.getStdCode());
						pstaCrmOut.setString(12, loadCEntity.getStdName());
						pstaCrmOut.setString(13, loadCEntity.getGradecode());
						pstaCrmOut.setString(14, loadCEntity.getGradename());
						pstaCrmOut.setString(15, loadCEntity.getSteelcode());
						pstaCrmOut.setString(16, loadCEntity.getSteelname());
						pstaCrmOut.setString(17, loadCEntity.getStdStyle());
						pstaCrmOut.setString(18, loadCEntity.getStdStyleDesc());
						pstaCrmOut.setString(19, loadCEntity.getSpecCode());
						pstaCrmOut.setString(20, loadCEntity.getSpecName());
						pstaCrmOut.setString(21, loadCEntity.getModelCode());
						pstaCrmOut.setString(22, loadCEntity.getModelDesc());
						pstaCrmOut.setString(23, loadCEntity.getActLenMin());
						pstaCrmOut.setString(24, loadCEntity.getActLenMax());
						pstaCrmOut.setString(25, "-1");
						pstaCrmOut.setString(26, String.valueOf(-weghitsj));
						pstaCrmOut.setString(27, String.valueOf(-Double
								.parseDouble(loadCEntity.getActLen())));
						pstaCrmOut.setString(28, loadCEntity.getStorageNo());
						pstaCrmOut.setString(29, loadCEntity.getStorageNo());
						pstaCrmOut.setString(30, loadCEntity.getSenTime());
						pstaCrmOut.setString(31, loadCEntity.getValidflag());
						pstaCrmOut.setString(32, loadCEntity.getOrderNo());
						pstaCrmOut.setString(33, loadCEntity.getOrderSeq());
						pstaCrmOut.setString(34, loadCEntity.getOrderSeqNo());
						pstaCrmOut.setString(35, loadCEntity.getOrdPk());
						pstaCrmOut.setString(36, loadCEntity.getOrdLnPk());
						pstaCrmOut.setString(37, loadCEntity.getOrdLnDlyPk());
						pstaCrmOut.setString(38, "0");
						pstaCrmOut.setString(39, strNowTime);
						pstaCrmOut.setString(40, billNo);
						pstaCrmOut.setString(41,billNo);
						pstaCrmOut.setString(42,billNo);
						pstaCrmOut.setString(43,"120504".equals(cusInfo)? ComBaseInfo.GetBalYear_2416(now): ComBaseInfo.GetBalYear(now));
						pstaCrmOut.setString(44,loadCEntity.getActDimater());
						pstaCrmOut.setString(45,loadCEntity.getActHeight());
						pstaCrmOut.setString(46,loadCEntity.getSaleOrg());
						pstaCrmOut.setString(47,loadCEntity.getSaleOrgDesc());
						pstaCrmOut.setString(48, "800806");
						pstaCrmOut.setString(49, "出库红冲");
						pstaCrmOut.addBatch();

						// 提单从表
						String billCseq = queryBillCSeq(connection,
								loadCEntity.getMatNo());
						pstaBillC.setObject(1, billCseq);
						pstaBillC.setObject(2, bill_no);
						pstaBillC.setObject(3, loadCEntity.getMatNo());
						pstaBillC.setObject(4, String.valueOf(-Double
								.parseDouble(loadCEntity.getActLen())));
						pstaBillC.setObject(5, "-1");
						pstaBillC.setObject(6, String.valueOf(-weghitsj));
						pstaBillC
								.setObject(7, String.valueOf(-Double
										.parseDouble(loadCEntity
												.getActTheoryWeight())));
						pstaBillC.setObject(8, cratname);
						pstaBillC.setObject(9, crattime);
						pstaBillC.setObject(10, String.valueOf(-Double
								.parseDouble(len1.get(s))));
						pstaBillC.setObject(11, "801704");
						pstaBillC.setObject(12, loadCEntity.getMatNo());
						pstaBillC.setObject(13, billNo);
						pstaBillC.addBatch();

						// 装车从表
						pstaLoadMat.setObject(
								1,
								loadNo
										+ StringTool.formatDecimal(loadSeqNo,
										"00000"));
						pstaLoadMat.setObject(2, loadNo);
						pstaLoadMat.setObject(3, bill_no);
						pstaLoadMat.setObject(4, loadCEntity.getMatNo());
						pstaLoadMat.setObject(5, String.valueOf(-Double
								.parseDouble(loadCEntity.getActLen())));
						pstaLoadMat.setObject(6, "-1");
						pstaLoadMat.setObject(7, String.valueOf(-weghitsj));
						pstaLoadMat
								.setObject(8, String.valueOf(-Double
										.parseDouble(loadCEntity
												.getActTheoryWeight())));
						pstaLoadMat.setObject(9, crattime);
						pstaLoadMat.setObject(10, cratname);
						pstaLoadMat.setObject(11, crattime);
						pstaLoadMat.setObject(12, String.valueOf(-weghitgb));
						pstaLoadMat.setObject(13, String.valueOf(-Double
								.parseDouble(len1.get(s))));
						pstaLoadMat.setObject(14, "801704");
						pstaLoadMat.setObject(15, "120504".equals(cusInfo)? ComBaseInfo.GetBalYear_2416(now): ComBaseInfo.GetBalYear(now));
						pstaLoadMat.setObject(16, loadCEntity.getMatNo());
						pstaLoadMat.setObject(17, billNo);
						pstaLoadMat.addBatch();

						loadSeqNo++;
						outNo++;
						w++;
						s++;
						// matNo++;
					}

					pstaPro.setObject(1, String.valueOf(Math.abs(count)));
					pstaPro.setObject(2, String.valueOf(Math.abs(weghit)));
					pstaPro.setObject(3, String.valueOf(Math.abs(sendnum)));
					pstaPro.setObject(4, ordLnDlyPk);
					pstaPro.addBatch();

				}

				// 查询提单主表是否有记录
				String BillCOUNT = "";
				String billMatNo = "";// 对齐
				sqlquery = CoreXmlSqlParsersFactory
						.getSql("FrmReturnDashed.queryBillMReturn");
				psta = connection.prepareStatement(sqlquery);
				psta.setObject(1, bill_no);
				psta.setObject(2, ordLnDlyPk);
				rs = psta.executeQuery();
				if (rs.next()) {
					BillCOUNT = rs.getString("COUNT");
					billMatNo = rs.getString("BILL_NO_SEQ");
				}
				rs.close();
				psta.close();

				if (BillCOUNT.equals("0")) {
					String bill_seq = querySendInList(connection, bill_no);
					psatBillM.setObject(1, bill_seq);
					psatBillM.setObject(2, bill_no);
					psatBillM.setObject(3, String.valueOf(weghit));
					psatBillM.setObject(4, String.valueOf(-billCount));
					psatBillM.setObject(5, String.valueOf(-billlen));
					psatBillM.setObject(6, String.valueOf(weghit));
					psatBillM.setObject(7, String.valueOf(-billCount));
					psatBillM.setObject(8, String.valueOf(-billlen));
					psatBillM.setObject(9, cratname);
					psatBillM.setObject(10, crattime);
					psatBillM.setObject(11, crattime);
					psatBillM.setObject(12, billNo);
					psatBillM.setObject(13, ordLnDlyPk);
					psatBillM.addBatch();
					psatBillM.executeBatch();

					//crm提单主表
//					pstaCrmBillM.setObject(1,strNowTime);
					pstaCrmBillM.setObject(1,"0");
					pstaCrmBillM.setObject(2,"0");
					pstaCrmBillM.setObject(3,bill_no);
					pstaCrmBillM.addBatch();
				} else {
					String sqlupdate = "UPDATE YDM_ZC_BILL_M T"
							+ " SET T.PLAN_WT   = TO_NUMBER(T.PLAN_WT) + TO_NUMBER('"
							+ String.valueOf(weghit)
							+ "'),"
							+ "T.PLAN_NUM      = TO_NUMBER(T.PLAN_NUM) + TO_NUMBER('"
							+ String.valueOf(-billCount)
							+ "'),"
							+ "T.PLAN_TOTL_LEN = TO_NUMBER(T.PLAN_TOTL_LEN) + TO_NUMBER('"
							+ String.valueOf(-billlen)
							+ "'),"
							+ "T.ACT_WT        = TO_NUMBER(T.ACT_WT) + TO_NUMBER('"
							+ String.valueOf(weghit)
							+ "'),"
							+ "T.ACT_NUM       = TO_NUMBER(T.ACT_NUM) + TO_NUMBER('"
							+ String.valueOf(-billCount)
							+ "'),"
							+ "T.ACT_TOTL_LEN  = TO_NUMBER(T.ACT_TOTL_LEN) + TO_NUMBER('"
							+ String.valueOf(-billlen) + "')"
							+ " WHERE T.BILL_NO_SEQ = '" + billMatNo
							+ "'  AND T.RETURN_FLAG = '1'"; // 修改提单主表量
					Statement sta1 = connection.createStatement();
					sta1.executeUpdate(sqlupdate);
					sta1.close();

					//crm提单主表
					CrmBillService crmBillService = new CrmBillService();
					if(crmBillService.selectCrmBill(connection,billNo,"0")){
//						pstaCrmBillM.setObject(1,strNowTime);
						pstaCrmBillM.setObject(1,"1");
						pstaCrmBillM.setObject(2,"1");
						pstaCrmBillM.setObject(3,bill_no);
						pstaCrmBillM.addBatch();
					}
				}
				pstaMatNo.executeBatch();
				psAddCrmMat.executeBatch();
				pstabutter.executeBatch();
				pstaBillC.executeBatch();
				pstaLoadMat.executeBatch();
				pstaOut.executeBatch();
				pstaOutXs.executeBatch();
				pstaCrmOut.executeBatch();
				pstaPro.executeBatch();
				pstaCrmBillM.executeBatch();

				// 装车主表
				pstaLoad.setObject(1, loadNo);
				pstaLoad.setObject(2, bill_no);
				pstaLoad.setObject(3, String.valueOf(loadWeghit));
				pstaLoad.setObject(4, String.valueOf(-loadCount));
				pstaLoad.setObject(5, String.valueOf(-loadlen));
				pstaLoad.setObject(6, crattime);
				pstaLoad.setObject(7, management_no);
				pstaLoad.setObject(8, management_name);
				pstaLoad.setObject(9, cratname);
				pstaLoad.setObject(10, crattime);
				pstaLoad.setObject(11, departid);
				pstaLoad.setObject(12, departname);
				pstaLoad.setObject(13, "1");
				pstaLoad.setObject(14, billNo);
				pstaLoad.setObject(15, prdCls);
				pstaLoad.addBatch();
				pstaLoad.executeBatch();
			}

			//crm提单从表
//			pstaCrmBillC.setObject(1,strNowTime);
			pstaCrmBillC.setObject(1,"0");
			pstaCrmBillC.setObject(2,bill_no);
			pstaCrmBillC.addBatch();
			pstaCrmBillC.executeBatch();

			cro.setV_errMsg("新增退货成功！");

			connection.commit();
		} catch (Exception e) {
			cro.setV_errMsg(e.getMessage());
			cro.setV_errCode(-1);
			if (connection != null) {
				try {
					connection.rollback(); // 回滚事务
				} catch (Exception e2) {
				}
			}
		} finally {
			try {
				if (connection != null && !connection.isClosed()) {
					connection.close();
				}
			} catch (Exception e2) {
			}
		}
		return cro;
	}

	/**
	 * 撤销退货
	 * 
	 * @param pram
	 * @return
	 */
	@SuppressWarnings("unchecked")
	public CoreReturnObject deleteLoadReturn(ArrayList<Object> pram) {
		CoreReturnObject cro = new CoreReturnObject();
		Connection connection = this.getDao(CoreConstantData.sysbeanstr)
				.getConnection();
		Date now = new Date();
		SimpleDateFormat dfT = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		String strNowTime = dfT.format(now);
		try {
			connection.setAutoCommit(false); // 取消自动提交事务
			String sqldeleteOut = CoreXmlSqlParsersFactory
					.getSql("FrmReturnDashed.deleteOutReturn"); // 删除出库表
			PreparedStatement pstaOut = connection
					.prepareStatement(sqldeleteOut);
			
			String sqldeleteOutXs = CoreXmlSqlParsersFactory
					.getSql("FrmReturnDashedXs.deleteOutReturn"); // 删除销售出库表
			PreparedStatement pstaOutXs = connection
					.prepareStatement(sqldeleteOutXs);

			String sqldeleteFmisOut = CoreXmlSqlParsersFactory
					.getSql("FrmReturnDashed.delFmisZcOutlist"); // 删除财务中间表表
			PreparedStatement pstadelFmisOut = connection
					.prepareStatement(sqldeleteFmisOut);
			// String sqldeleteMatNo = CoreXmlSqlParsersFactory
			// .getSql("FrmReturnDashed.deleteMatNoReturn"); // 删除材料表
			// PreparedStatement pstaMatNo = connection
			// .prepareStatement(sqldeleteMatNo);
			String addMat = CoreXmlSqlParsersFactory
					.getSql("FrmReturnDashed.AddMatZcBackup");// 新增材料表MAT_ZC_BACKUP（删除位置表，材料表）
			PreparedStatement pstaMatNo = connection.prepareStatement(addMat);
			// String sqldeletebutter = CoreXmlSqlParsersFactory
			// .getSql("FrmReturnDashed.deleteButterReturn"); // 删除位置表
			// PreparedStatement pstabutter = connection
			// .prepareStatement(sqldeletebutter);
			String deleteBillM = CoreXmlSqlParsersFactory
					.getSql("FrmReturnDashed.deleteBillMReturn"); // 删除提单主表
			PreparedStatement pstabillM = connection
					.prepareStatement(deleteBillM);
			String deleteBillC = CoreXmlSqlParsersFactory
					.getSql("FrmReturnDashed.deleteBillCReturn"); // 删除提单从表
			PreparedStatement pstaBillC = connection
					.prepareStatement(deleteBillC);
			String deleteLoad = CoreXmlSqlParsersFactory
					.getSql("FrmReturnDashed.deleteLoadReturn"); // 删除装车主表
			PreparedStatement pstaLoad = connection
					.prepareStatement(deleteLoad);
			String deleteLoadMat = CoreXmlSqlParsersFactory
					.getSql("FrmReturnDashed.deleteLoadMatReturn"); // 删除装车从表
			PreparedStatement pstaLoadMat = connection
					.prepareStatement(deleteLoadMat);

			String insertCrmBillM = CoreXmlSqlParsersFactory
					.getSql("CrmBill.insertCrmBillM");//新增到crm提单m
			PreparedStatement pstaCrmBillM = connection.prepareStatement(insertCrmBillM);
			String insertCrmBillC = CoreXmlSqlParsersFactory
					.getSql("CrmBill.insertCrmBillC");//新增到crm提单c
			PreparedStatement pstaCrmBillC = connection.prepareStatement(insertCrmBillC);
			String insertCrmOut = CoreXmlSqlParsersFactory
					.getSql("CrmOutList.insertQueryInfo"); // 新增到crm出库表
			PreparedStatement pstaCrmOut = connection.prepareStatement(insertCrmOut);
			String addCrmMat = CoreXmlSqlParsersFactory
					.getSql("CrmMatZc.insertCrmMatZc_GetM");//新增crm材料表，取m
			PreparedStatement psAddCrmMat = connection.prepareStatement(addCrmMat);

			String updateProOrd = CoreXmlSqlParsersFactory
					.getSql("FrmReturnDashed.deleteProOrd"); // 回写进程表
			PreparedStatement pstaPro = connection
					.prepareStatement(updateProOrd);
			for (int i = 0; i < pram.size(); i++) {
				ArrayList<String> list = (ArrayList<String>) pram.get(i);
				String billNo = list.get(0);
				String judgeNo = list.get(1);
				String batchNo = list.get(2);
				String groupNo = list.get(3);
				String ordLnDlyPk = list.get(5);
				double NuM = Math.abs(Double.parseDouble(list.get(4)));
				String profalg = list.get(6);
				double countc = 0;
				double weghit = 0;
				double prowt = 0;
				double sendnum = 0;
				double len = 0;
				String strcou = "";
				// 查询要已退货量
				String sqlquery = CoreXmlSqlParsersFactory
						.getSql("FrmReturnDashed.queryOutLoadMat");
				PreparedStatement psta = connection.prepareStatement(sqlquery);
				psta.setObject(1, billNo);
				psta.setObject(2, judgeNo);
				psta.setObject(3, batchNo);
				psta.setObject(4, groupNo);
				psta.setObject(5, ordLnDlyPk);
				ResultSet rs = psta.executeQuery();
				if (rs.next()) {
					countc = Double.parseDouble(rs.getString("ACT_COUNT"));
					weghit = Double.parseDouble(rs.getString("ACT_WEIGHT"));
					prowt = Double.parseDouble(rs.getString("PONDER_GROSS_WT"));
					sendnum = Double.parseDouble(rs.getString("SEND_NUM"));
					len = Double.parseDouble(rs.getString("ACT_LEN"));
					strcou = rs.getString("COUNNUM");
				}
				rs.close();
				psta.close();
				if (strcou.equals("0")) {
					cro.setV_errMsg("该记录已撤销退货！");
					return cro;
				}
				// 查询提单主表是否可退货
				String BILL_TICKET_STATUS = "";
				sqlquery = CoreXmlSqlParsersFactory
						.getSql("FrmReturnDashed.queryBillMRtrun");
				psta = connection.prepareStatement(sqlquery);
				psta.setObject(1, billNo);
				rs = psta.executeQuery();
				if (rs.next()) {
					BILL_TICKET_STATUS = rs.getString("BILL_TICKET_STATUS");
				}
				rs.close();
				psta.close();
				if (!BILL_TICKET_STATUS.equals("0")) {
					cro.setV_errMsg("该退货记录提单已票据对齐！");
					return cro;
				}

				// 判断财务系统是否接收
				sqlquery = CoreXmlSqlParsersFactory
						.getSql("FrmReturnDashed.getFmisZcOutlist");
				psta = connection.prepareStatement(sqlquery);
				psta.setObject(1, billNo);
				psta.setObject(2, judgeNo);
				psta.setObject(3, batchNo);
				psta.setObject(4, groupNo);
				psta.setObject(5, ordLnDlyPk);
				rs = psta.executeQuery();
				if (rs.next()) {
					if (!StringUtils.isEmpty(rs.getString(1))) {
						cro.setV_errMsg("财务系统同步出库信息，请联系财务撤销同步信息!");
						return cro;
					}
				}
				rs.close();
				psta.close();

				// 查询材料是否可撤销退货
				String matNO = "";
				String LOCK_FLAG = "";
				String MNG_LOCK_FLAG = "";
				double count = 0;
				String sqlquery1 = CoreXmlSqlParsersFactory
						.getSql("FrmReturnDashed.queryMatZcNo");
				PreparedStatement psta1 = connection
						.prepareStatement(sqlquery1);
				psta1.setObject(1, billNo);
				psta1.setObject(2, judgeNo);
				psta1.setObject(3, batchNo);
				psta1.setObject(4, groupNo);
				psta1.setObject(5, ordLnDlyPk);
				ResultSet rs1 = psta1.executeQuery();
				if (rs1.next()) {
					matNO = rs1.getString("MAT_STATUS");
					LOCK_FLAG = rs1.getString("LOCK_FLAG");
					MNG_LOCK_FLAG = rs1.getString("MNG_LOCK_FLAG");
					count = Double.parseDouble(rs1.getString("COUNT"));
				}
				rs1.close();
				psta1.close();

				if (count < NuM) {
					cro.setV_errMsg("炉号:" + judgeNo + ",批号:" + batchNo + ",组号:"
							+ groupNo + "该退货记录已库存红冲,不能撤销！");
					return cro;
				}
				if (!matNO.equals("80150301")) {
					cro.setV_errMsg("炉号:" + judgeNo + ",批号:" + batchNo + ",组号:"
							+ groupNo + "该退货记录已发运或已脱单,不能撤销！");
					return cro;
				}
				if (!LOCK_FLAG.equals("0")) {
					cro.setV_errMsg("炉号:" + judgeNo + ",批号:" + batchNo + ",组号:"
							+ groupNo + "该退货记录已质量封锁,不能撤销！");
					return cro;
				}
				if (MNG_LOCK_FLAG.equals("1")) {
					cro.setV_errMsg("炉号:" + judgeNo + ",批号:" + batchNo + ",组号:"
							+ groupNo + "该退货记录已管理封锁,不能撤销！");
					return cro;
				}
				double wt = 0;
				if (profalg.equals("是")) {
					wt = prowt;
				} else {
					wt = weghit;
				}

				if (Math.abs(countc) == 0) {
					pstaPro.setObject(1, Math.abs(countc));
					pstaPro.setObject(2, Math.abs(wt));
					pstaPro.setObject(3, Math.abs(sendnum));
					pstaPro.setObject(4, ordLnDlyPk);
					pstaPro.addBatch();

					//同步crm出库
					pstaCrmOut.setObject(1, "2");
					pstaCrmOut.setString(2, strNowTime);
					pstaCrmOut.setObject(3, billNo);
					pstaCrmOut.setObject(4, judgeNo);
					pstaCrmOut.setObject(5, batchNo);
					pstaCrmOut.setObject(6, groupNo);
					pstaCrmOut.setObject(7, ordLnDlyPk);
					pstaCrmOut.addBatch();

					// 删除出库表
					pstaOut.setObject(1, billNo);
					pstaOut.setObject(2, judgeNo);
					pstaOut.setObject(3, batchNo);
					pstaOut.setObject(4, groupNo);
					pstaOut.setObject(5, ordLnDlyPk);
					pstaOut.addBatch();
					
					pstaOutXs.setObject(1, billNo);
					pstaOutXs.setObject(2, judgeNo);
					pstaOutXs.setObject(3, batchNo);
					pstaOutXs.setObject(4, groupNo);
					pstaOutXs.setObject(5, ordLnDlyPk);
					pstaOutXs.addBatch();

					// 财务中间表表
					pstadelFmisOut.setObject(1, billNo);
					pstadelFmisOut.setObject(2, judgeNo);
					pstadelFmisOut.setObject(3, batchNo);
					pstadelFmisOut.setObject(4, groupNo);
					pstadelFmisOut.setObject(5, ordLnDlyPk);
					pstadelFmisOut.addBatch();

					// 删除提单从表‘
					pstaBillC.setObject(1, billNo);
					pstaBillC.setObject(2, ordLnDlyPk);
					pstaBillC.setObject(3, judgeNo);
					pstaBillC.setObject(4, batchNo);
					pstaBillC.setObject(5, groupNo);
					pstaBillC.addBatch();

					// 删除装车从表
					pstaLoadMat.setObject(1, billNo);
					pstaLoadMat.setObject(2, ordLnDlyPk);
					pstaLoadMat.setObject(3, judgeNo);
					pstaLoadMat.setObject(4, batchNo);
					pstaLoadMat.setObject(5, groupNo);
					pstaLoadMat.addBatch();
				} else {
					// 删除材料表
					pstaMatNo.setObject(1, billNo);
					pstaMatNo.setObject(2, judgeNo);
					pstaMatNo.setObject(3, batchNo);
					pstaMatNo.setObject(4, groupNo);
					pstaMatNo.setObject(5, ordLnDlyPk);
					pstaMatNo.setObject(6, billNo);
					pstaMatNo.setObject(7, judgeNo);
					pstaMatNo.setObject(8, batchNo);
					pstaMatNo.setObject(9, groupNo);
					pstaMatNo.setObject(10, ordLnDlyPk);
					pstaMatNo.setObject(11, billNo);
					pstaMatNo.setObject(12, judgeNo);
					pstaMatNo.setObject(13, batchNo);
					pstaMatNo.setObject(14, groupNo);
					pstaMatNo.setObject(15, ordLnDlyPk);
					pstaMatNo.setObject(16, billNo);
					pstaMatNo.setObject(17, judgeNo);
					pstaMatNo.setObject(18, batchNo);
					pstaMatNo.setObject(19, groupNo);
					pstaMatNo.setObject(20, ordLnDlyPk);
					pstaMatNo.addBatch();

					//crm同步材料
					psAddCrmMat.setObject(1, "2");
					psAddCrmMat.setObject(2, strNowTime);
					psAddCrmMat.setObject(3, billNo);
					psAddCrmMat.setObject(4, judgeNo);
					psAddCrmMat.setObject(5, batchNo);
					psAddCrmMat.setObject(6, groupNo);
					psAddCrmMat.setObject(7, ordLnDlyPk);
					psAddCrmMat.addBatch();

					// 删除位置表
					// pstabutter.setObject(1, billNo);
					// pstabutter.setObject(2, judgeNo);
					// pstabutter.setObject(3, batchNo);
					// pstabutter.setObject(4, groupNo);
					// pstabutter.setObject(5, ordLnDlyPk);
					// pstabutter.addBatch();

					// 回写进程表
					pstaPro.setObject(1, Math.abs(countc));
					pstaPro.setObject(2, Math.abs(wt));
					pstaPro.setObject(3, Math.abs(sendnum));
					pstaPro.setObject(4, ordLnDlyPk);
					pstaPro.addBatch();

					// 删除提单从表‘
					pstaBillC.setObject(1, billNo);
					pstaBillC.setObject(2, ordLnDlyPk);
					pstaBillC.setObject(3, judgeNo);
					pstaBillC.setObject(4, batchNo);
					pstaBillC.setObject(5, groupNo);
					pstaBillC.addBatch();

					// 删除装车从表
					pstaLoadMat.setObject(1, billNo);
					pstaLoadMat.setObject(2, ordLnDlyPk);
					pstaLoadMat.setObject(3, judgeNo);
					pstaLoadMat.setObject(4, batchNo);
					pstaLoadMat.setObject(5, groupNo);
					pstaLoadMat.addBatch();

					//同步crm出库
					pstaCrmOut.setObject(1, "2");
					pstaCrmOut.setString(2, strNowTime);
					pstaCrmOut.setObject(3, billNo);
					pstaCrmOut.setObject(4, judgeNo);
					pstaCrmOut.setObject(5, batchNo);
					pstaCrmOut.setObject(6, groupNo);
					pstaCrmOut.setObject(7, ordLnDlyPk);
					pstaCrmOut.addBatch();

					// 删除出库表
					pstaOut.setObject(1, billNo);
					pstaOut.setObject(2, judgeNo);
					pstaOut.setObject(3, batchNo);
					pstaOut.setObject(4, groupNo);
					pstaOut.setObject(5, ordLnDlyPk);
					pstaOut.addBatch();

					pstaOutXs.setObject(1, billNo);
					pstaOutXs.setObject(2, judgeNo);
					pstaOutXs.setObject(3, batchNo);
					pstaOutXs.setObject(4, groupNo);
					pstaOutXs.setObject(5, ordLnDlyPk);
					pstaOutXs.addBatch();

					
					// 财务中间表表
					pstadelFmisOut.setObject(1, billNo);
					pstadelFmisOut.setObject(2, judgeNo);
					pstadelFmisOut.setObject(3, batchNo);
					pstadelFmisOut.setObject(4, groupNo);
					pstadelFmisOut.setObject(5, ordLnDlyPk);
					pstadelFmisOut.addBatch();
				}
				if(i==0){
					//同步crm提单从表
					CrmBillService crmBillService = new CrmBillService();
					if(crmBillService.selectCrmBill(connection,billNo,"0")){
//						pstaCrmBillC.setObject(1,strNowTime);
						pstaCrmBillC.setObject(1, "2");
						pstaCrmBillC.setObject(2, billNo);
						pstaCrmBillC.addBatch();
						pstaCrmBillC.executeBatch();
					}
				}
				pstaCrmOut.executeBatch();
				pstaBillC.executeBatch();
				pstaLoadMat.executeBatch();
				// 查询提单从表是否还存在提单
				sqlquery = CoreXmlSqlParsersFactory
						.getSql("FrmReturnDashed.queryBillCRtrun");
				PreparedStatement psta2 = connection.prepareStatement(sqlquery);
				psta2.setObject(1, billNo);
				psta2.setObject(2, judgeNo);
				psta2.setObject(3, batchNo);
				psta2.setObject(4, groupNo);
				psta2.setObject(5, ordLnDlyPk);
				ResultSet rs2 = psta2.executeQuery();
				if (rs2.next()) {
					String sqlupdate = "UPDATE YDM_ZC_BILL_M T"
							+ " SET T.PLAN_WT   = TO_NUMBER(T.PLAN_WT) + TO_NUMBER('"
							+ Math.abs(weghit)
							+ "'),"
							+ "T.PLAN_NUM      = TO_NUMBER(T.PLAN_NUM) + TO_NUMBER('"
							+ Math.abs(countc)
							+ "'),"
							+ "T.PLAN_TOTL_LEN = TO_NUMBER(T.PLAN_TOTL_LEN) + TO_NUMBER('"
							+ Math.abs(len)
							+ "'),"
							+ "T.ACT_WT        = TO_NUMBER(T.ACT_WT) + TO_NUMBER('"
							+ Math.abs(weghit)
							+ "'),"
							+ "T.ACT_NUM       = TO_NUMBER(T.ACT_NUM) + TO_NUMBER('"
							+ Math.abs(countc)
							+ "'),"
							+ "T.ACT_TOTL_LEN  = TO_NUMBER(T.ACT_TOTL_LEN) + TO_NUMBER('"
							+ Math.abs(len)
							+ "')"
							+ " WHERE T.BILL_NO = '"
							+ billNo
							+ "' AND T.ORD_LN_DLY_PK='"
							+ ordLnDlyPk
							+ "'  AND T.RETURN_FLAG = '1'"; // 修改提单主表量
					Statement sta1 = connection.createStatement();
					sta1.executeUpdate(sqlupdate);
					sta1.close();
				} else {
					if(i==0){
						//crm同步提单主表
						CrmBillService crmBillService = new CrmBillService();
						if(crmBillService.selectCrmBill(connection,billNo,"0")){
							// 判断crm系统是否结算
							sqlquery = CoreXmlSqlParsersFactory
									.getSql("CrmBill.selectCrmBillIsPay");
							psta = connection.prepareStatement(sqlquery);
							psta.setObject(1, billNo);
							rs = psta.executeQuery();
							if (rs.next()) {
								if (rs.getInt(1) > 0) {
									cro.setV_errMsg("crm系统已结算，请联系crm系统撤销结算信息!");
									return cro;
								}
							}
							rs.close();
							psta.close();

//							pstaCrmBillM.setObject(1,strNowTime);
							pstaCrmBillM.setObject(1, "2");
							pstaCrmBillM.setObject(2,"2");
							pstaCrmBillM.setObject(3, billNo);
							pstaCrmBillM.addBatch();
						}
					}
					// 删除提单主表
					pstabillM.setObject(1, billNo);
					pstabillM.setObject(2, ordLnDlyPk);
					pstabillM.addBatch();
				}
				rs2.close();
				psta2.close();

				// 查询装车从表是否还存在提单
				sqlquery = CoreXmlSqlParsersFactory
						.getSql("FrmReturnDashed.queryLoadMatRtrun");
				PreparedStatement psta3 = connection.prepareStatement(sqlquery);
				psta3.setObject(1, billNo);
				psta3.setObject(2, judgeNo);
				psta3.setObject(3, batchNo);
				psta3.setObject(4, groupNo);
				psta3.setObject(5, ordLnDlyPk);
				ResultSet rs3 = psta3.executeQuery();
				if (rs3.next()) {
					String sqlupdate = "UPDATE YDM_ZC_LOADVEHICLE T "
							+ " SET T.STACKING_WT       = TO_NUMBER(T.STACKING_WT) + TO_NUMBER('"
							+ Math.abs(weghit)
							+ "'),"
							+ "  T.STACKING_PIECE    = TO_NUMBER(T.STACKING_PIECE) + TO_NUMBER('"
							+ Math.abs(countc)
							+ "'),"
							+ " T.STACKING_TOTL_LEN = TO_NUMBER(T.STACKING_TOTL_LEN) + TO_NUMBER('"
							+ Math.abs(len) + "')" + "  WHERE T.BILL_NO = '"
							+ billNo + "'   AND T.RETURN_FLAG = '1'"; // 修改提单主表量
					Statement sta1 = connection.createStatement();
					sta1.executeUpdate(sqlupdate);
					sta1.close();
				} else {
					// 删除装车主表
					pstaLoad.setObject(1, billNo);
					pstaLoad.addBatch();
				}
				rs3.close();
				psta3.close();
			}

			pstaCrmBillM.executeBatch();
			pstabillM.executeBatch();
			psAddCrmMat.executeBatch();
			pstaLoad.executeBatch();
			pstaPro.executeBatch();
			pstaMatNo.executeBatch();
			// pstabutter.executeBatch();
			// pstaInto.executeBatch();
			pstaOut.executeBatch();
			pstaOutXs.executeBatch();
			pstadelFmisOut.executeBatch();
			cro.setV_errMsg("撤销退货成功！");
			connection.commit();
		} catch (Exception e) {
			cro.setV_errMsg(e.getMessage());
			cro.setV_errCode(-1);
			if (connection != null) {
				try {

					connection.rollback(); // 回滚事务
				} catch (Exception e2) {
				}
			}
		} finally {
			try {
				if (connection != null && !connection.isClosed()) {
					connection.close();
				}
			} catch (Exception e2) {
			}
		}
		return cro;
	}

	/**
	 * 插入提单号并返回提单号
	 * 
	 * @param con
	 * @param salg
	 * @return
	 * @throws SQLException
	 */
	public static String insertBillNo(Connection con, String salg)
			throws SQLException {
		String billNo = "";
		String flag = "";
		if (salg.equals("100101")) {
			flag = "X";
		} else if (salg.equals("100102")) {
			flag = "M";
		} else if (salg.equals("100103")) {
			flag = "G";
		} else {
			return "S";
		}
		String sql = CoreXmlSqlParsersFactory.getSql(
				"FrmReturnDashed.createBillNo").replace("#BILLNO#",
				"BTH" + flag);
		PreparedStatement ps = con.prepareStatement(sql);
		ps.setObject(1, flag);
		ps.setObject(2, flag);
		ResultSet rs = ps.executeQuery();
		if (rs.next()) {
			billNo = rs.getString("BILL_NO");
		}
		rs.close();
		ps.close();
		return billNo;
	}

	/**
	 * 手工退货
	 * 
	 * @param pram
	 * @return
	 */
	public CoreReturnObject addPipDataReturn(ArrayList<String> list,
			String billMNo,String cusInfo) {
		CoreReturnObject cro = new CoreReturnObject();
		Connection connection = this.getDao(CoreConstantData.sysbeanstr)
				.getConnection();
		Date now = new Date();
		try {
			connection.setAutoCommit(false); // 取消自动提交事务
			String sqlinsertOut = CoreXmlSqlParsersFactory
					.getSql("FrmReturnDashed.addOutReturn1"); // 新增到出库表
			PreparedStatement pstaOut = connection
					.prepareStatement(sqlinsertOut);
			
			String sqlinsertOutXs = CoreXmlSqlParsersFactory
					.getSql("matxs.addOutReturn1"); // 新增到销售出库表
			PreparedStatement pstaOutXs = connection
					.prepareStatement(sqlinsertOutXs);
			
			String sqlinsertMatNo1 = CoreXmlSqlParsersFactory
					.getSql("FrmReturnDashed.addMatNoReturn1"); // 新增材料表
			PreparedStatement pstaMatNo1 = connection
					.prepareStatement(sqlinsertMatNo1);
			
			String sqlinsertMatNo1Xs = CoreXmlSqlParsersFactory
					.getSql("matxs.addMatNoReturn1"); // 新增销售材料表
			PreparedStatement pstaMatNo1Xs = connection
					.prepareStatement(sqlinsertMatNo1Xs);
			
			String sqlinsertMatNo = CoreXmlSqlParsersFactory
					.getSql("FrmReturnDashed.addMatNoReturn"); // 新增材料表
			PreparedStatement pstaMatNo = connection
					.prepareStatement(sqlinsertMatNo);
			
			String sqlinsertMatNoXs = CoreXmlSqlParsersFactory
					.getSql("matxs.addMatNoReturn"); // 新增销售材料表
			PreparedStatement pstaMatNoXs = connection
					.prepareStatement(sqlinsertMatNoXs);
			
			String sqlinsertbutter = CoreXmlSqlParsersFactory
					.getSql("FrmReturnDashed.addButterReturn"); // 新增位置表
			PreparedStatement pstabutter = connection
					.prepareStatement(sqlinsertbutter);
			String updateProOrd = CoreXmlSqlParsersFactory
					.getSql("FrmReturnDashed.updateProOrd"); // 回写进程表
			PreparedStatement pstaPro = connection
					.prepareStatement(updateProOrd);
			String cratname = "";
			String crattime = "";

			String ordLnDlyPk = list.get(19);
			String judgeNo = list.get(24);
			String batchNo = list.get(25);
			String groupNo = queryGroupNo(connection, judgeNo, batchNo);
			cratname = list.get(42);
			crattime = list.get(43);
			String manageNO = list.get(40);
			String manageName = list.get(41);
			String deparId = list.get(38);
			String deparNmae = list.get(39);
			String matType= list.get(53);
			int count = Integer.parseInt(list.get(26));// 支数
			double weghit = Double.parseDouble(list.get(27));// 退货重量
			double actlen = Double.parseDouble(list.get(28));
			String sendOut = querySendOut(connection, "800806");
			// 获取出库流水号
			String butessMatNo = FrmPipeEntruck.querySendOutList(connection,
					sendOut, "800806");
			int outNo = Integer.parseInt(butessMatNo.substring(
					butessMatNo.length() - 4, butessMatNo.length()));
			// 获取材料号
			String matNoMax = qureyMatNo(connection, judgeNo, batchNo);
			int matNo = Integer.parseInt(matNoMax.substring(
					matNoMax.length() - 4, matNoMax.length()));
			String billNo = "";
			if (billMNo.equals("")) {
				billNo = insertBillNo(connection, list.get(36));
				String sqlInsertBill = "INSERT INTO YDM_ZC_BILL_NO (BILL_NO) values ('"
						+ billNo + "')";
				Statement sta1 = connection.createStatement();
				sta1.executeUpdate(sqlInsertBill);
			} else {
				billNo = billMNo;
			}

			if (count == 0) {// 0支非0吨
				// 新增出库表
				
				pstaOut.setString(1,
						sendOut + StringTool.formatDecimal(outNo, "0000"));
				pstaOut.setString(2, sendOut);
				pstaOut.setString(3, "800806");
				pstaOut.setString(4, "出库红冲");
				pstaOut.setString(5, "");
				pstaOut.setString(
						6,
						judgeNo + batchNo
								+ StringTool.formatDecimal(matNo, "0000"));
				pstaOut.setString(7, "");
				pstaOut.setString(8, "");
				pstaOut.setString(9, judgeNo);
				pstaOut.setString(10, batchNo);
				pstaOut.setString(11, groupNo);
				pstaOut.setString(12, "");
				pstaOut.setString(13, "");
				pstaOut.setString(14, list.get(0));
				pstaOut.setString(15, list.get(1));
				pstaOut.setString(16, list.get(10));
				pstaOut.setString(17, list.get(11));
				pstaOut.setString(18, list.get(4));
				pstaOut.setString(19, list.get(5));
				pstaOut.setString(20, list.get(2));
				pstaOut.setString(21, list.get(3));
				pstaOut.setString(22, list.get(12));
				pstaOut.setString(23, list.get(13));
				pstaOut.setString(24, list.get(6));
				pstaOut.setString(25, list.get(7));
				pstaOut.setString(26, list.get(8));
				pstaOut.setString(27, list.get(9));
				pstaOut.setString(28, list.get(31));
				pstaOut.setString(29, list.get(32));
				pstaOut.setString(30, list.get(20));
				pstaOut.setString(31, list.get(21));
				pstaOut.setString(32, String.valueOf(-actlen));// 长度
				pstaOut.setString(33, list.get(22));
				pstaOut.setString(34, list.get(23));
				pstaOut.setString(35, String.valueOf(-count));// 支数
				pstaOut.setObject(36, String.valueOf(-weghit));// 重量
				pstaOut.setString(37, "");// 理重
				pstaOut.setString(38, list.get(34));
				pstaOut.setString(39, list.get(35));
				pstaOut.setString(40, "");
				pstaOut.setString(41, "");
				pstaOut.setString(42, "");
				pstaOut.setString(43, "");
				pstaOut.setString(44, list.get(48));
				pstaOut.setString(45, list.get(49));
				pstaOut.setString(46, manageNO);
				pstaOut.setString(47, manageName);
				pstaOut.setString(48, "");
				pstaOut.setString(49, "");
				pstaOut.setString(50, "");
				pstaOut.setString(51, crattime);
				pstaOut.setString(52, list.get(44));
				pstaOut.setString(53, list.get(45));
				pstaOut.setString(54, cratname);
				pstaOut.setString(55, crattime);
				pstaOut.setString(56, list.get(17));
				pstaOut.setString(57, list.get(18));
				pstaOut.setString(58, ordLnDlyPk);// 交货行PK
				pstaOut.setString(59, list.get(14));
				pstaOut.setString(60, list.get(15));
				pstaOut.setString(61, list.get(16));
				pstaOut.setString(62, deparId);
				pstaOut.setString(63, deparNmae);
				pstaOut.setString(64, list.get(36));
				pstaOut.setString(65, list.get(37));
				pstaOut.setString(66, "");
				pstaOut.setObject(67, "");
				pstaOut.setObject(68, billNo);
				pstaOut.setObject(69, "");
				pstaOut.setObject(70, list.get(34));
				pstaOut.setObject(71, list.get(50));
				pstaOut.setObject(72, "1");
				pstaOut.setObject(73, list.get(46));
				pstaOut.setObject(74, list.get(47));
				pstaOut.setObject(75, list.get(51));
				pstaOut.setObject(76, list.get(52));
				pstaOut.setObject(77, list.get(33));
				pstaOut.setObject(78, "");
				pstaOut.setObject(79, "801704");
				pstaOut.setObject(80, cusInfo);
				pstaOut.setObject(81, cusInfo);
				pstaOut.setObject(82, matType);
				pstaOut.setObject(83, list.get(31));
				pstaOut.setObject(84, list.get(31));
				pstaOut.setObject(85, "120504".equals(cusInfo)? ComBaseInfo.GetBalYear_2416(now): ComBaseInfo.GetBalYear(now));
				pstaOut.addBatch();
				
				//新增到销售出库表
				pstaOutXs.setString(1,
						sendOut + StringTool.formatDecimal(outNo, "0000"));
				pstaOutXs.setString(2, sendOut);
				pstaOutXs.setString(3, "800806");
				pstaOutXs.setString(4, "出库红冲");
				pstaOutXs.setString(5, "");
				pstaOutXs.setString(
						6,
						judgeNo + batchNo
								+ StringTool.formatDecimal(matNo, "0000"));
				pstaOutXs.setString(7, "");
				pstaOutXs.setString(8, "");
				pstaOutXs.setString(9, judgeNo);
				pstaOutXs.setString(10, batchNo);
				pstaOutXs.setString(11, groupNo);
				pstaOutXs.setString(12, "");
				pstaOutXs.setString(13, "");
				pstaOutXs.setString(14, list.get(0));
				pstaOutXs.setString(15, list.get(1));
				pstaOutXs.setString(16, list.get(10));
				pstaOutXs.setString(17, list.get(11));
				pstaOutXs.setString(18, list.get(4));
				pstaOutXs.setString(19, list.get(5));
				pstaOutXs.setString(20, list.get(2));
				pstaOutXs.setString(21, list.get(3));
				pstaOutXs.setString(22, list.get(12));
				pstaOutXs.setString(23, list.get(13));
				pstaOutXs.setString(24, list.get(6));
				pstaOutXs.setString(25, list.get(7));
				pstaOutXs.setString(26, list.get(8));
				pstaOutXs.setString(27, list.get(9));
				pstaOutXs.setString(28, list.get(31));
				pstaOutXs.setString(29, list.get(32));
				pstaOutXs.setString(30, list.get(20));
				pstaOutXs.setString(31, list.get(21));
				pstaOutXs.setString(32, String.valueOf(-actlen));// 长度
				pstaOutXs.setString(33, list.get(22));
				pstaOutXs.setString(34, list.get(23));
				pstaOutXs.setString(35, String.valueOf(-count));// 支数
				pstaOutXs.setObject(36, String.valueOf(-weghit));// 重量
				pstaOutXs.setString(37, "");// 理重
				pstaOutXs.setString(38, list.get(34));
				pstaOutXs.setString(39, list.get(35));
				pstaOutXs.setString(40, "");
				pstaOutXs.setString(41, "");
				pstaOutXs.setString(42, "");
				pstaOutXs.setString(43, "");
				pstaOutXs.setString(44, list.get(48));
				pstaOutXs.setString(45, list.get(49));
				pstaOutXs.setString(46, manageNO);
				pstaOutXs.setString(47, manageName);
				pstaOutXs.setString(48, "");
				pstaOutXs.setString(49, "");
				pstaOutXs.setString(50, "");
				pstaOutXs.setString(51, crattime);
				pstaOutXs.setString(52, list.get(44));
				pstaOutXs.setString(53, list.get(45));
				pstaOutXs.setString(54, cratname);
				pstaOutXs.setString(55, crattime);
				pstaOutXs.setString(56, list.get(17));
				pstaOutXs.setString(57, list.get(18));
				pstaOutXs.setString(58, ordLnDlyPk);// 交货行PK
				pstaOutXs.setString(59, list.get(14));
				pstaOutXs.setString(60, list.get(15));
				pstaOutXs.setString(61, list.get(16));
				pstaOutXs.setString(62, deparId);
				pstaOutXs.setString(63, deparNmae);
				pstaOutXs.setString(64, list.get(36));
				pstaOutXs.setString(65, list.get(37));
				pstaOutXs.setString(66, "");
				pstaOutXs.setObject(67, "");
				pstaOutXs.setObject(68, billNo);
				pstaOutXs.setObject(69, "");
				pstaOutXs.setObject(70, list.get(34));
				pstaOutXs.setObject(71, list.get(50));
				pstaOutXs.setObject(72, "1");
				pstaOutXs.setObject(73, list.get(46));
				pstaOutXs.setObject(74, list.get(47));
				pstaOutXs.setObject(75, list.get(51));
				pstaOutXs.setObject(76, list.get(52));
				pstaOutXs.setObject(77, list.get(33));
				pstaOutXs.setObject(78, "");
				pstaOutXs.setObject(79, "801704");
				pstaOutXs.setObject(80, cusInfo);
				pstaOutXs.setObject(81, cusInfo);
				pstaOutXs.setObject(82, matType);
				pstaOutXs.setObject(83, list.get(31));
				pstaOutXs.setObject(84, list.get(31));
				pstaOutXs.setObject(85, "120504".equals(cusInfo)? ComBaseInfo.GetBalYear_2416(now): ComBaseInfo.GetBalYear(now));
				pstaOutXs.addBatch();


				pstaPro.setObject(1, String.valueOf(count));
				pstaPro.setObject(2, String.valueOf(weghit));
				pstaPro.setObject(3, String.valueOf(0));
				pstaPro.setObject(4, ordLnDlyPk);
				pstaPro.addBatch();

				pstaOut.executeBatch();
				pstaOutXs.executeBatch();
				pstaPro.executeBatch();
			} else {// 非0支非0吨
				List<double[]> lenList = MeanValueCalculation.MeanCalculation(
						Math.abs(weghit), Math.abs(count));
				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]));
						}
					}
				}
				List<double[]> lensendnum = MeanValueCalculation
						.MeanCalculation(Math.abs(actlen), Math.abs(count));
				List<String> len1 = new ArrayList<String>();// 计算每只退货实发量
				for (int k = 0; k < lensendnum.size(); k++) {
					double[] conLen1 = lensendnum.get(k);
					if ((int) conLen1[0] >= 0) {
						for (int h = 0; h < (int) conLen1[0]; h++) {
							len1.add(String.valueOf(conLen1[1]));
						}
					}
				}
				int w = 0;
				int s = 0;
				for (int c = 0; c < count; c++) {

					if (!list.get(19).equals("")) {
						// 新增材料表
						pstaMatNo.setObject(
								1,
								judgeNo
										+ batchNo
										+ StringTool.formatDecimal(matNo,
												"0000"));
						pstaMatNo.setObject(2, list.get(46));
						pstaMatNo.setObject(3, list.get(47));
						pstaMatNo.setObject(4, "");
						pstaMatNo.setObject(5, "");
						pstaMatNo.setObject(6, judgeNo);
						pstaMatNo.setObject(7, batchNo);
						pstaMatNo.setObject(8, groupNo);
						pstaMatNo.setObject(9, "");
						pstaMatNo.setObject(10, "");
						pstaMatNo.setObject(11, list.get(0));
						pstaMatNo.setObject(12, list.get(1));
						pstaMatNo.setObject(13, list.get(2));
						pstaMatNo.setObject(14, list.get(3));
						pstaMatNo.setObject(15, list.get(4));
						pstaMatNo.setObject(16, list.get(5));
						pstaMatNo.setObject(17, list.get(6));
						pstaMatNo.setObject(18, list.get(7));
						pstaMatNo.setObject(19, list.get(8));
						pstaMatNo.setObject(20, list.get(9));
						pstaMatNo.setObject(21, list.get(10));
						pstaMatNo.setObject(22, list.get(11));
						pstaMatNo.setObject(23, list.get(12));
						pstaMatNo.setObject(24, list.get(13));
						pstaMatNo.setObject(25, "");
						pstaMatNo.setObject(26, list.get(14));
						pstaMatNo.setObject(27, list.get(15));
						pstaMatNo.setObject(28, list.get(16));
						pstaMatNo.setObject(29, "");
						pstaMatNo.setObject(30, "");
						pstaMatNo.setObject(31, list.get(31));
						pstaMatNo.setObject(32, list.get(32));
						pstaMatNo.setObject(33, list.get(20));
						pstaMatNo.setObject(34, list.get(21));
						pstaMatNo.setObject(35, String.valueOf(len1.get(s)));
						pstaMatNo.setObject(36, list.get(22));
						pstaMatNo.setObject(37, list.get(23));
						pstaMatNo.setObject(38, "1");
						pstaMatNo.setObject(39, String.valueOf(len.get(w)));
						pstaMatNo.setObject(40, "");
						//设置所属权单位
						if(list.get(36).equals("100103")){
							pstaMatNo.setObject(41, "100101");
							pstaMatNo.setObject(42,"100101");
						} else {
							pstaMatNo.setObject(41, list.get(36));
							pstaMatNo.setObject(42, list.get(36));
						}
						pstaMatNo.setObject(43, crattime);
						pstaMatNo.setObject(44, "1");
						pstaMatNo.setObject(45, "");
						pstaMatNo.setObject(46, "");

						pstaMatNo.setObject(47, "");

						pstaMatNo.setObject(48, cratname);
						pstaMatNo.setObject(49, crattime);
						pstaMatNo.setObject(50, list.get(17));
						pstaMatNo.setObject(51, list.get(18));
						pstaMatNo.setObject(52, list.get(19));
						pstaMatNo.setObject(53, "");
						pstaMatNo.setObject(54, "");
						pstaMatNo.setObject(55, "");
						pstaMatNo.setObject(56, "");
						pstaMatNo.setObject(57, list.get(33));
						pstaMatNo.setObject(58, "");
						pstaMatNo.setObject(59, "801704");
						pstaMatNo.setObject(60, cusInfo);
						pstaMatNo.setObject(61, cusInfo);
						pstaMatNo.setObject(62, matType);
						pstaMatNo.addBatch();
						
						pstaMatNoXs.setObject(
								1,
								judgeNo
										+ batchNo
										+ StringTool.formatDecimal(matNo,
												"0000"));
						pstaMatNoXs.setObject(2, list.get(46));
						pstaMatNoXs.setObject(3, list.get(47));
						pstaMatNoXs.setObject(4, "");
						pstaMatNoXs.setObject(5, "");
						pstaMatNoXs.setObject(6, judgeNo);
						pstaMatNoXs.setObject(7, batchNo);
						pstaMatNoXs.setObject(8, groupNo);
						pstaMatNoXs.setObject(9, "");
						pstaMatNoXs.setObject(10, "");
						pstaMatNoXs.setObject(11, list.get(0));
						pstaMatNoXs.setObject(12, list.get(1));
						pstaMatNoXs.setObject(13, list.get(2));
						pstaMatNoXs.setObject(14, list.get(3));
						pstaMatNoXs.setObject(15, list.get(4));
						pstaMatNoXs.setObject(16, list.get(5));
						pstaMatNoXs.setObject(17, list.get(6));
						pstaMatNoXs.setObject(18, list.get(7));
						pstaMatNoXs.setObject(19, list.get(8));
						pstaMatNoXs.setObject(20, list.get(9));
						pstaMatNoXs.setObject(21, list.get(10));
						pstaMatNoXs.setObject(22, list.get(11));
						pstaMatNoXs.setObject(23, list.get(12));
						pstaMatNoXs.setObject(24, list.get(13));
						pstaMatNoXs.setObject(25, "");
						pstaMatNoXs.setObject(26, list.get(14));
						pstaMatNoXs.setObject(27, list.get(15));
						pstaMatNoXs.setObject(28, list.get(16));
						pstaMatNoXs.setObject(29, "");
						pstaMatNoXs.setObject(30, "");
						pstaMatNoXs.setObject(31, list.get(31));
						pstaMatNoXs.setObject(32, list.get(32));
						pstaMatNoXs.setObject(33, list.get(20));
						pstaMatNoXs.setObject(34, list.get(21));
						pstaMatNoXs.setObject(35, String.valueOf(len1.get(s)));
						pstaMatNoXs.setObject(36, list.get(22));
						pstaMatNoXs.setObject(37, list.get(23));
						pstaMatNoXs.setObject(38, "1");
						pstaMatNoXs.setObject(39, String.valueOf(len.get(w)));
						pstaMatNoXs.setObject(40, "");
						//设置所属权单位
						if(list.get(36).equals("100103")){
							pstaMatNoXs.setObject(41, "100101");
							pstaMatNoXs.setObject(42,"100101");
						} else {
							pstaMatNoXs.setObject(41, list.get(36));
							pstaMatNoXs.setObject(42, list.get(36));
						}
						pstaMatNoXs.setObject(43, crattime);
						pstaMatNoXs.setObject(44, "1");
						pstaMatNoXs.setObject(45, "");
						pstaMatNoXs.setObject(46, "");

						pstaMatNoXs.setObject(47, "");

						pstaMatNoXs.setObject(48, cratname);
						pstaMatNoXs.setObject(49, crattime);
						pstaMatNoXs.setObject(50, list.get(17));
						pstaMatNoXs.setObject(51, list.get(18));
						pstaMatNoXs.setObject(52, list.get(19));
						pstaMatNoXs.setObject(53, "");
						pstaMatNoXs.setObject(54, "");
						pstaMatNoXs.setObject(55, "");
						pstaMatNoXs.setObject(56, "");
						pstaMatNoXs.setObject(57, list.get(33));
						pstaMatNoXs.setObject(58, "");
						pstaMatNoXs.setObject(59, "801704");
						pstaMatNoXs.setObject(60, cusInfo);
						pstaMatNoXs.setObject(61, cusInfo);
						pstaMatNoXs.setObject(62, matType);
						pstaMatNoXs.addBatch();
						
					} else {// 新增材料表
						pstaMatNo1.setObject(
								1,
								judgeNo
										+ batchNo
										+ StringTool.formatDecimal(matNo,
												"0000"));
						pstaMatNo1.setObject(2, list.get(46));
						pstaMatNo1.setObject(3, list.get(47));
						pstaMatNo1.setObject(4, "");
						pstaMatNo1.setObject(5, "");
						pstaMatNo1.setObject(6, judgeNo);
						pstaMatNo1.setObject(7, batchNo);
						pstaMatNo1.setObject(8, groupNo);
						pstaMatNo1.setObject(9, "");
						pstaMatNo1.setObject(10, "");
						pstaMatNo1.setObject(11, list.get(0));
						pstaMatNo1.setObject(12, list.get(1));
						pstaMatNo1.setObject(13, list.get(2));
						pstaMatNo1.setObject(14, list.get(3));
						pstaMatNo1.setObject(15, list.get(4));
						pstaMatNo1.setObject(16, list.get(5));
						pstaMatNo1.setObject(17, list.get(6));
						pstaMatNo1.setObject(18, list.get(7));
						pstaMatNo1.setObject(19, list.get(8));
						pstaMatNo1.setObject(20, list.get(9));
						pstaMatNo1.setObject(21, list.get(10));
						pstaMatNo1.setObject(22, list.get(11));
						pstaMatNo1.setObject(23, list.get(12));
						pstaMatNo1.setObject(24, list.get(13));
						pstaMatNo1.setObject(25, "");
						pstaMatNo1.setObject(26, list.get(14));
						pstaMatNo1.setObject(27, list.get(15));
						pstaMatNo1.setObject(28, list.get(16));
						pstaMatNo1.setObject(29, "");
						pstaMatNo1.setObject(30, "");
						pstaMatNo1.setObject(31, list.get(31));
						pstaMatNo1.setObject(32, list.get(32));
						pstaMatNo1.setObject(33, list.get(20));
						pstaMatNo1.setObject(34, list.get(21));
						pstaMatNo1.setObject(35, String.valueOf(len1.get(s)));
						pstaMatNo1.setObject(36, list.get(22));
						pstaMatNo1.setObject(37, list.get(23));
						pstaMatNo1.setObject(38, "1");
						pstaMatNo1.setObject(39, String.valueOf(len.get(w)));
						pstaMatNo1.setObject(40, "");
						//设置所属权单位
						if(list.get(36).equals("100103")){
							pstaMatNo1.setObject(41, "100101");
							pstaMatNo1.setObject(42,"100101");
						} else {
							pstaMatNo1.setObject(41, list.get(36));
							pstaMatNo1.setObject(42, list.get(36));
						}
						pstaMatNo1.setObject(43, crattime);
						pstaMatNo1.setObject(44, "1");
						pstaMatNo1.setObject(45, "");
						pstaMatNo1.setObject(46, "");

						pstaMatNo1.setObject(47, "");

						pstaMatNo1.setObject(48, cratname);
						pstaMatNo1.setObject(49, crattime);
						pstaMatNo1.setObject(50, list.get(17));
						pstaMatNo1.setObject(51, list.get(18));
						pstaMatNo1.setObject(52, list.get(19));
						pstaMatNo1.setObject(53, "");
						pstaMatNo1.setObject(54, "");
						pstaMatNo1.setObject(55, "");
						pstaMatNo1.setObject(56, "");
						pstaMatNo1.setObject(57, list.get(33));
						pstaMatNo1.setObject(58, "");
						pstaMatNo1.setObject(59, "801704");
						pstaMatNo1.setObject(60, cusInfo);
						pstaMatNo1.setObject(61, cusInfo);
						pstaMatNo1.setObject(62, matType);
						pstaMatNo1.addBatch();
						
						//新增销售材料表
						pstaMatNo1Xs.setObject(
								1,
								judgeNo
										+ batchNo
										+ StringTool.formatDecimal(matNo,
												"0000"));
						pstaMatNo1Xs.setObject(2, list.get(46));
						pstaMatNo1Xs.setObject(3, list.get(47));
						pstaMatNo1Xs.setObject(4, "");
						pstaMatNo1Xs.setObject(5, "");
						pstaMatNo1Xs.setObject(6, judgeNo);
						pstaMatNo1Xs.setObject(7, batchNo);
						pstaMatNo1Xs.setObject(8, groupNo);
						pstaMatNo1Xs.setObject(9, "");
						pstaMatNo1Xs.setObject(10, "");
						pstaMatNo1Xs.setObject(11, list.get(0));
						pstaMatNo1Xs.setObject(12, list.get(1));
						pstaMatNo1Xs.setObject(13, list.get(2));
						pstaMatNo1Xs.setObject(14, list.get(3));
						pstaMatNo1Xs.setObject(15, list.get(4));
						pstaMatNo1Xs.setObject(16, list.get(5));
						pstaMatNo1Xs.setObject(17, list.get(6));
						pstaMatNo1Xs.setObject(18, list.get(7));
						pstaMatNo1Xs.setObject(19, list.get(8));
						pstaMatNo1Xs.setObject(20, list.get(9));
						pstaMatNo1Xs.setObject(21, list.get(10));
						pstaMatNo1Xs.setObject(22, list.get(11));
						pstaMatNo1Xs.setObject(23, list.get(12));
						pstaMatNo1Xs.setObject(24, list.get(13));
						pstaMatNo1Xs.setObject(25, "");
						pstaMatNo1Xs.setObject(26, list.get(14));
						pstaMatNo1Xs.setObject(27, list.get(15));
						pstaMatNo1Xs.setObject(28, list.get(16));
						pstaMatNo1Xs.setObject(29, "");
						pstaMatNo1Xs.setObject(30, "");
						pstaMatNo1Xs.setObject(31, list.get(31));
						pstaMatNo1Xs.setObject(32, list.get(32));
						pstaMatNo1Xs.setObject(33, list.get(20));
						pstaMatNo1Xs.setObject(34, list.get(21));
						pstaMatNo1Xs.setObject(35, String.valueOf(len1.get(s)));
						pstaMatNo1Xs.setObject(36, list.get(22));
						pstaMatNo1Xs.setObject(37, list.get(23));
						pstaMatNo1Xs.setObject(38, "1");
						pstaMatNo1Xs.setObject(39, String.valueOf(len.get(w)));
						pstaMatNo1Xs.setObject(40, "");
						//设置所属权单位
						if(list.get(36).equals("100103")){
							pstaMatNo1Xs.setObject(41, "100101");
							pstaMatNo1Xs.setObject(42,"100101");
						} else {
							pstaMatNo1Xs.setObject(41, list.get(36));
							pstaMatNo1Xs.setObject(42, list.get(36));
						}
						pstaMatNo1Xs.setObject(43, crattime);
						pstaMatNo1Xs.setObject(44, "1");
						pstaMatNo1Xs.setObject(45, "");
						pstaMatNo1Xs.setObject(46, "");

						pstaMatNo1Xs.setObject(47, "");

						pstaMatNo1Xs.setObject(48, cratname);
						pstaMatNo1Xs.setObject(49, crattime);
						pstaMatNo1Xs.setObject(50, list.get(17));
						pstaMatNo1Xs.setObject(51, list.get(18));
						pstaMatNo1Xs.setObject(52, list.get(19));
						pstaMatNo1Xs.setObject(53, "");
						pstaMatNo1Xs.setObject(54, "");
						pstaMatNo1Xs.setObject(55, "");
						pstaMatNo1Xs.setObject(56, "");
						pstaMatNo1Xs.setObject(57, list.get(33));
						pstaMatNo1Xs.setObject(58, "");
						pstaMatNo1Xs.setObject(59, "801704");
						pstaMatNo1Xs.setObject(60, cusInfo);
						pstaMatNo1Xs.setObject(61, cusInfo);
						pstaMatNo1Xs.setObject(62, matType);
						pstaMatNo1Xs.addBatch();
					}
					// 新增位置表
					pstabutter.setObject(
							1,
							judgeNo + batchNo
									+ StringTool.formatDecimal(matNo, "0000"));
					pstabutter.setObject(2, judgeNo);
					pstabutter.setObject(3, batchNo);
					pstabutter.setObject(4, groupNo);
					pstabutter.setObject(5, list.get(34));
					pstabutter.setObject(6, list.get(35));
					pstabutter.setObject(7, "");
					pstabutter.setObject(8, "");
					pstabutter.setObject(9, "");
					pstabutter.setObject(10, "");
					pstabutter.setObject(11, list.get(48));
					pstabutter.setObject(12, list.get(49));
					pstabutter.setObject(13, manageNO);
					pstabutter.setObject(14, manageName);
					pstabutter.setObject(15, cratname);
					pstabutter.setObject(16, crattime);
					pstabutter.setObject(17, deparId);
					pstabutter.setObject(18, deparNmae);
					pstabutter.addBatch();

					// 新增出库表
					pstaOut.setString(1,
							sendOut + StringTool.formatDecimal(outNo, "0000"));
					pstaOut.setString(2, sendOut);
					pstaOut.setString(3, "800806");
					pstaOut.setString(4, "出库红冲");
					pstaOut.setString(5, "");
					pstaOut.setString(
							6,
							judgeNo + batchNo
									+ StringTool.formatDecimal(matNo, "0000"));
					pstaOut.setString(7, "");
					pstaOut.setString(8, "");
					pstaOut.setString(9, judgeNo);
					pstaOut.setString(10, batchNo);
					pstaOut.setString(11, groupNo);
					pstaOut.setString(12, "");
					pstaOut.setString(13, "");
					pstaOut.setString(14, list.get(0));
					pstaOut.setString(15, list.get(1));
					pstaOut.setString(16, list.get(10));
					pstaOut.setString(17, list.get(11));
					pstaOut.setString(18, list.get(4));
					pstaOut.setString(19, list.get(5));
					pstaOut.setString(20, list.get(2));
					pstaOut.setString(21, list.get(3));
					pstaOut.setString(22, list.get(12));
					pstaOut.setString(23, list.get(13));
					pstaOut.setString(24, list.get(6));
					pstaOut.setString(25, list.get(7));
					pstaOut.setString(26, list.get(8));
					pstaOut.setString(27, list.get(9));
					pstaOut.setString(28, list.get(31));
					pstaOut.setString(29, list.get(32));
					pstaOut.setString(30, list.get(20));
					pstaOut.setString(31, list.get(21));
					pstaOut.setString(32, String.valueOf("-" + len1.get(s)));// 长度
					pstaOut.setString(33, list.get(22));
					pstaOut.setString(34, list.get(23));
					pstaOut.setString(35, "-1");// 支数
					pstaOut.setObject(36, String.valueOf("-" + len.get(w)));// 重量
					pstaOut.setString(37, "");// 理重
					pstaOut.setString(38, list.get(34));
					pstaOut.setString(39, list.get(35));
					pstaOut.setString(40, "");
					pstaOut.setString(41, "");
					pstaOut.setString(42, "");
					pstaOut.setString(43, "");
					pstaOut.setString(44, list.get(48));
					pstaOut.setString(45, list.get(49));
					pstaOut.setString(46, manageNO);
					pstaOut.setString(47, manageName);
					pstaOut.setString(48, "");
					pstaOut.setString(49, "");
					pstaOut.setString(50, "");
					pstaOut.setString(51, crattime);
					pstaOut.setString(52, list.get(44));
					pstaOut.setString(53, list.get(45));
					pstaOut.setString(54, cratname);
					pstaOut.setString(55, crattime);
					pstaOut.setString(56, list.get(17));
					pstaOut.setString(57, list.get(18));
					pstaOut.setString(58, ordLnDlyPk);// 交货行PK
					pstaOut.setString(59, list.get(14));
					pstaOut.setString(60, list.get(15));
					pstaOut.setString(61, list.get(16));
					pstaOut.setString(62, deparId);
					pstaOut.setString(63, deparNmae);
					pstaOut.setString(64, list.get(36));
					pstaOut.setString(65, list.get(37));
					pstaOut.setString(66, "");
					pstaOut.setObject(67, "");
					pstaOut.setObject(68, billNo);
					pstaOut.setObject(69, "");
					pstaOut.setObject(70, list.get(34));
					pstaOut.setObject(71, list.get(50));
					pstaOut.setObject(72, "1");
					pstaOut.setObject(73, list.get(46));
					pstaOut.setObject(74, list.get(47));
					pstaOut.setObject(75, list.get(51));
					pstaOut.setObject(76, list.get(52));
					pstaOut.setObject(77, list.get(33));
					pstaOut.setObject(78, "");
					pstaOut.setObject(79, "801704");
					pstaOut.setObject(80, cusInfo);
					pstaOut.setObject(81, cusInfo);
					pstaOut.setObject(82, matType);
					pstaOut.setObject(83, list.get(31));
					pstaOut.setObject(84, list.get(31));
					pstaOut.setObject(85, "120504".equals(cusInfo)? ComBaseInfo.GetBalYear_2416(now): ComBaseInfo.GetBalYear(now));
					pstaOut.addBatch();
					
					// 新增销售出库表
					pstaOutXs.setString(1,
							sendOut + StringTool.formatDecimal(outNo, "0000"));
					pstaOutXs.setString(2, sendOut);
					pstaOutXs.setString(3, "800806");
					pstaOutXs.setString(4, "出库红冲");
					pstaOutXs.setString(5, "");
					pstaOutXs.setString(
							6,
							judgeNo + batchNo
									+ StringTool.formatDecimal(matNo, "0000"));
					pstaOutXs.setString(7, "");
					pstaOutXs.setString(8, "");
					pstaOutXs.setString(9, judgeNo);
					pstaOutXs.setString(10, batchNo);
					pstaOutXs.setString(11, groupNo);
					pstaOutXs.setString(12, "");
					pstaOutXs.setString(13, "");
					pstaOutXs.setString(14, list.get(0));
					pstaOutXs.setString(15, list.get(1));
					pstaOutXs.setString(16, list.get(10));
					pstaOutXs.setString(17, list.get(11));
					pstaOutXs.setString(18, list.get(4));
					pstaOutXs.setString(19, list.get(5));
					pstaOutXs.setString(20, list.get(2));
					pstaOutXs.setString(21, list.get(3));
					pstaOutXs.setString(22, list.get(12));
					pstaOutXs.setString(23, list.get(13));
					pstaOutXs.setString(24, list.get(6));
					pstaOutXs.setString(25, list.get(7));
					pstaOutXs.setString(26, list.get(8));
					pstaOutXs.setString(27, list.get(9));
					pstaOutXs.setString(28, list.get(31));
					pstaOutXs.setString(29, list.get(32));
					pstaOutXs.setString(30, list.get(20));
					pstaOutXs.setString(31, list.get(21));
					pstaOutXs.setString(32, String.valueOf("-" + len1.get(s)));// 长度
					pstaOutXs.setString(33, list.get(22));
					pstaOutXs.setString(34, list.get(23));
					pstaOutXs.setString(35, "-1");// 支数
					pstaOutXs.setObject(36, String.valueOf("-" + len.get(w)));// 重量
					pstaOutXs.setString(37, "");// 理重
					pstaOutXs.setString(38, list.get(34));
					pstaOutXs.setString(39, list.get(35));
					pstaOutXs.setString(40, "");
					pstaOutXs.setString(41, "");
					pstaOutXs.setString(42, "");
					pstaOutXs.setString(43, "");
					pstaOutXs.setString(44, list.get(48));
					pstaOutXs.setString(45, list.get(49));
					pstaOutXs.setString(46, manageNO);
					pstaOutXs.setString(47, manageName);
					pstaOutXs.setString(48, "");
					pstaOutXs.setString(49, "");
					pstaOutXs.setString(50, "");
					pstaOutXs.setString(51, crattime);
					pstaOutXs.setString(52, list.get(44));
					pstaOutXs.setString(53, list.get(45));
					pstaOutXs.setString(54, cratname);
					pstaOutXs.setString(55, crattime);
					pstaOutXs.setString(56, list.get(17));
					pstaOutXs.setString(57, list.get(18));
					pstaOutXs.setString(58, ordLnDlyPk);// 交货行PK
					pstaOutXs.setString(59, list.get(14));
					pstaOutXs.setString(60, list.get(15));
					pstaOutXs.setString(61, list.get(16));
					pstaOutXs.setString(62, deparId);
					pstaOutXs.setString(63, deparNmae);
					pstaOutXs.setString(64, list.get(36));
					pstaOutXs.setString(65, list.get(37));
					pstaOutXs.setString(66, "");
					pstaOutXs.setObject(67, "");
					pstaOutXs.setObject(68, billNo);
					pstaOutXs.setObject(69, "");
					pstaOutXs.setObject(70, list.get(34));
					pstaOutXs.setObject(71, list.get(50));
					pstaOutXs.setObject(72, "1");
					pstaOutXs.setObject(73, list.get(46));
					pstaOutXs.setObject(74, list.get(47));
					pstaOutXs.setObject(75, list.get(51));
					pstaOutXs.setObject(76, list.get(52));
					pstaOutXs.setObject(77, list.get(33));
					pstaOutXs.setObject(78, "");
					pstaOutXs.setObject(79, "801704");
					pstaOutXs.setObject(80, cusInfo);
					pstaOutXs.setObject(81, cusInfo);
					pstaOutXs.setObject(82, matType);
					pstaOutXs.setObject(83, list.get(31));
					pstaOutXs.setObject(84, list.get(31));
					pstaOutXs.setObject(85, "120504".equals(cusInfo)? ComBaseInfo.GetBalYear_2416(now): ComBaseInfo.GetBalYear(now));
					pstaOutXs.addBatch();

					outNo++;
					w++;
					s++;
					matNo++;
				}

				pstaPro.setObject(1, String.valueOf(count));
				pstaPro.setObject(2, String.valueOf(weghit));
				pstaPro.setObject(3, String.valueOf(0));
				pstaPro.setObject(4, ordLnDlyPk);
				pstaPro.addBatch();

				pstaMatNo.executeBatch();
				pstaMatNoXs.executeBatch();
				pstaMatNo1.executeBatch();
				pstaMatNo1Xs.executeBatch();
				pstabutter.executeBatch();
				pstaOut.executeBatch();
				pstaOutXs.executeBatch();
				pstaPro.executeBatch();

				pstaMatNo.close();
				pstaMatNo1.close();
				pstabutter.close();
				pstaOut.close();
				pstaOutXs.close();
				pstaPro.close();
			}
			cro.setV_errMsg("手工退货成功！");

			connection.commit();
		} catch (Exception e) {
			cro.setV_errMsg(e.getMessage());
			cro.setV_errCode(-1);
			if (connection != null) {
				try {
					connection.rollback(); // 回滚事务
				} catch (Exception e2) {
				}
			}
		} finally {
			try {
				if (connection != null && !connection.isClosed()) {
					connection.close();
				}
			} catch (Exception e2) {
			}
		}
		return cro;
	}

	/**
	 * 手工退货撤销
	 * 
	 * @param pram
	 * @return
	 */
	@SuppressWarnings("unchecked")
	public CoreReturnObject deleteLoadReturn1(ArrayList<Object> pram) {
		CoreReturnObject cro = new CoreReturnObject();
		Connection connection = this.getDao(CoreConstantData.sysbeanstr)
				.getConnection();
		try {
			connection.setAutoCommit(false); // 取消自动提交事务
			String sqldeleteOut = CoreXmlSqlParsersFactory
					.getSql("FrmReturnDashed.deleteOutReturn"); // 删除出库表
			PreparedStatement pstaOut = connection
					.prepareStatement(sqldeleteOut);
			
			String sqldeleteOutXs = CoreXmlSqlParsersFactory
					.getSql("FrmReturnDashedXs.deleteOutReturn"); // 删除出库表
			PreparedStatement pstaOutXs = connection
					.prepareStatement(sqldeleteOutXs);

			String sqldeleteFmisOut = CoreXmlSqlParsersFactory
					.getSql("FrmReturnDashed.delFmisZcOutlist"); // 删除财务中间表表
			PreparedStatement pstadelFmisOut = connection
					.prepareStatement(sqldeleteFmisOut);


			String sqldeleteMatNo = CoreXmlSqlParsersFactory
					.getSql("FrmReturnDashed.deleteMatNoReturn"); // 删除材料表
			PreparedStatement pstaMatNo = connection
					.prepareStatement(sqldeleteMatNo);
			String sqldeleteMatNoXs = CoreXmlSqlParsersFactory
					.getSql("FrmReturnDashedXs.deleteMatNoReturn"); // 删除材料表
			PreparedStatement pstaMatNoXs = connection
					.prepareStatement(sqldeleteMatNoXs);
			String sqldeletebutter = CoreXmlSqlParsersFactory
					.getSql("FrmReturnDashed.deleteButterReturn"); // 删除位置表
			PreparedStatement pstabutter = connection
					.prepareStatement(sqldeletebutter);
			String updateProOrd = CoreXmlSqlParsersFactory
					.getSql("FrmReturnDashed.deleteProOrd"); // 回写进程表
			PreparedStatement pstaPro = connection
					.prepareStatement(updateProOrd);
			for (int i = 0; i < pram.size(); i++) {
				ArrayList<String> list = (ArrayList<String>) pram.get(i);
				String billNo = list.get(0);
				String judgeNo = list.get(1);
				String batchNo = list.get(2);
				String groupNo = list.get(3);
				String ordLnDlyPk = list.get(5);
				double countc = 0;
				double weghit = 0;
				double sendnum = 0;
				String strcou = "";
				// 查询要已退货量
				String sqlquery = CoreXmlSqlParsersFactory
						.getSql("FrmReturnDashed.queryOutLoadMat");
				PreparedStatement psta = connection.prepareStatement(sqlquery);
				psta.setObject(1, billNo);
				psta.setObject(2, judgeNo);
				psta.setObject(3, batchNo);
				psta.setObject(4, groupNo);
				psta.setObject(5, ordLnDlyPk);
				ResultSet rs = psta.executeQuery();
				if (rs.next()) {
					countc = Double.parseDouble(rs.getString("ACT_COUNT"));
					weghit = Double.parseDouble(rs.getString("ACT_WEIGHT"));
					sendnum = Double.parseDouble(rs.getString("SEND_NUM"));
					strcou = rs.getString("COUNNUM");
				}
				rs.close();
				psta.close();
				if (strcou.equals("0")) {
					cro.setV_errMsg("该记录已撤销退货！");
					return cro;
				}

				// 判断财务系统是否接收
				sqlquery = CoreXmlSqlParsersFactory
						.getSql("FrmReturnDashed.getFmisZcOutlist");
				psta = connection.prepareStatement(sqlquery);
				psta.setObject(1, billNo);
				psta.setObject(2, judgeNo);
				psta.setObject(3, batchNo);
				psta.setObject(4, groupNo);
				psta.setObject(5, ordLnDlyPk);
				rs = psta.executeQuery();
				if (rs.next()) {
					if (!StringUtils.isEmpty(rs.getString(1))) {
						cro.setV_errMsg("财务系统同步出库信息，请联系财务撤销同步信息!");
						return cro;
					}
				}
				rs.close();
				psta.close();

				if (Math.abs(countc) == 0) {
					pstaPro.setObject(1, countc);
					pstaPro.setObject(2, weghit);
					pstaPro.setObject(3, sendnum);
					pstaPro.setObject(4, ordLnDlyPk);
					pstaPro.addBatch();

					// 删除出库表
					pstaOut.setObject(1, billNo);
					pstaOut.setObject(2, judgeNo);
					pstaOut.setObject(3, batchNo);
					pstaOut.setObject(4, groupNo);
					pstaOut.setObject(5, ordLnDlyPk);
					pstaOut.addBatch();
					// 删除出库表
					pstaOutXs.setObject(1, billNo);
					pstaOutXs.setObject(2, judgeNo);
					pstaOutXs.setObject(3, batchNo);
					pstaOutXs.setObject(4, groupNo);
					pstaOutXs.setObject(5, ordLnDlyPk);
					pstaOutXs.addBatch();

					// 财务中间表表
					pstadelFmisOut.setObject(1, billNo);
					pstadelFmisOut.setObject(2, judgeNo);
					pstadelFmisOut.setObject(3, batchNo);
					pstadelFmisOut.setObject(4, groupNo);
					pstadelFmisOut.setObject(5, ordLnDlyPk);
					pstadelFmisOut.addBatch();

				} else {
					// 删除材料表
					pstaMatNo.setObject(1, billNo);
					pstaMatNo.setObject(2, judgeNo);
					pstaMatNo.setObject(3, batchNo);
					pstaMatNo.setObject(4, groupNo);
					pstaMatNo.setObject(5, ordLnDlyPk);
					pstaMatNo.addBatch();
					
					// 删除销售材料表
					pstaMatNoXs.setObject(1, billNo);
					pstaMatNoXs.setObject(2, judgeNo);
					pstaMatNoXs.setObject(3, batchNo);
					pstaMatNoXs.setObject(4, groupNo);
					pstaMatNoXs.setObject(5, ordLnDlyPk);
					pstaMatNoXs.addBatch();

					// 删除位置表
					pstabutter.setObject(1, billNo);
					pstabutter.setObject(2, judgeNo);
					pstabutter.setObject(3, batchNo);
					pstabutter.setObject(4, groupNo);
					pstabutter.setObject(5, ordLnDlyPk);
					pstabutter.addBatch();

					pstaPro.setObject(1, countc);
					pstaPro.setObject(2, weghit);
					pstaPro.setObject(3, sendnum);
					pstaPro.setObject(4, ordLnDlyPk);
					pstaPro.addBatch();

					// 删除出库表
					pstaOut.setObject(1, billNo);
					pstaOut.setObject(2, judgeNo);
					pstaOut.setObject(3, batchNo);
					pstaOut.setObject(4, groupNo);
					pstaOut.setObject(5, ordLnDlyPk);
					pstaOut.addBatch();
					
					pstaOutXs.setObject(1, billNo);
					pstaOutXs.setObject(2, judgeNo);
					pstaOutXs.setObject(3, batchNo);
					pstaOutXs.setObject(4, groupNo);
					pstaOutXs.setObject(5, ordLnDlyPk);
					pstaOutXs.addBatch();

					// 财务中间表表
					pstadelFmisOut.setObject(1, billNo);
					pstadelFmisOut.setObject(2, judgeNo);
					pstadelFmisOut.setObject(3, batchNo);
					pstadelFmisOut.setObject(4, groupNo);
					pstadelFmisOut.setObject(5, ordLnDlyPk);
					pstadelFmisOut.addBatch();
				}

				String sqldeleteBill = "DELETE YDM_ZC_BILL_NO WHERE BILL_NO = '"
						+ billNo + "'";
				Statement sta1 = connection.createStatement();
				sta1.executeUpdate(sqldeleteBill);
			}
			pstaPro.executeBatch();
			pstaMatNo.executeBatch();
			pstaMatNoXs.executeBatch();
			pstabutter.executeBatch();
			pstaOut.executeBatch();
			pstaOutXs.executeBatch();
			pstadelFmisOut.executeBatch();
			cro.setV_errMsg("撤销退货成功！");
			connection.commit();
		} catch (Exception e) {
			cro.setV_errMsg(e.getMessage());
			cro.setV_errCode(-1);
			if (connection != null) {
				try {
					connection.rollback(); // 回滚事务
				} catch (Exception e2) {
				}
			}
		} finally {
			try {
				if (connection != null && !connection.isClosed()) {
					connection.close();
				}
			} catch (Exception e2) {
			}
		}
		return cro;
	}

	/**
	 * 按支退货
	 * 
	 * @param pram1
	 * @param pram
	 * @param management_no
	 * @param management_name
	 * @param departid
	 * @param departname
	 * @return
	 */
	@SuppressWarnings("unchecked")
	public CoreReturnObject doAddCount(ArrayList<String> list,
			ArrayList<Object> pram, String management_no,
			String management_name, String departid, String departname,String storageNoChoose,String cusInfo,
			String returnType,String returnCode,String pipmemoText) {
		CoreReturnObject cro = new CoreReturnObject();
		Connection connection = this.getDao(CoreConstantData.sysbeanstr)
				.getConnection();
		String sqlquery = "";
		PreparedStatement ps = null;
		ResultSet rs = null;
		Date now = new Date();
		SimpleDateFormat dfT = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		String strNowTime = dfT.format(now);
		try {
			connection.setAutoCommit(false); // 取消自动提交事务
			List<YdmZcLoadvehicleMatEntity> loadCEntitys = null;
			String sqlinsertOut = CoreXmlSqlParsersFactory
					.getSql("FrmReturnDashed.addOutReturn"); // 新增到出库表
			PreparedStatement pstaOut = connection
					.prepareStatement(sqlinsertOut);
			String sqlinsertOutXs = CoreXmlSqlParsersFactory
					.getSql("matxs.addOutReturn"); // 新增到销售出库表
			PreparedStatement pstaOutXs = connection
					.prepareStatement(sqlinsertOutXs);
			String insetBillM = CoreXmlSqlParsersFactory
					.getSql("FrmReturnDashed.addBillMReturn"); // 新增到提单主表
			PreparedStatement psatBillM = connection
					.prepareStatement(insetBillM);
			String insetBillC = CoreXmlSqlParsersFactory
					.getSql("FrmReturnDashed.addBillCReturn"); // 新增到提单从表
			PreparedStatement pstaBillC = connection
					.prepareStatement(insetBillC);
			String insetLoad = CoreXmlSqlParsersFactory
					.getSql("FrmReturnDashed.addLoadReturn"); // 新增到装车主表
			PreparedStatement pstaLoad = connection.prepareStatement(insetLoad);
			String insetLoadMat = CoreXmlSqlParsersFactory
					.getSql("FrmReturnDashed.addLoadMatReturn"); // 新增到装车从表
			PreparedStatement pstaLoadMat = connection
					.prepareStatement(insetLoadMat);
			String addMatNo = CoreXmlSqlParsersFactory
					.getSql("FrmReturnDashed.AddMatNoZc");// 新增材料表（删除材料表MAT_ZC_BACKUP）
			PreparedStatement pstaMatNo = connection.prepareStatement(addMatNo);
			String sqlinsertbutter = CoreXmlSqlParsersFactory
					.getSql("FrmReturnDashed.addButterReturn"); // 新增位置表
			PreparedStatement pstabutter = connection
					.prepareStatement(sqlinsertbutter);
			String updateProOrd = CoreXmlSqlParsersFactory
					.getSql("FrmReturnDashed.updateProOrd"); // 回写进程表'

			String insertCrmBillM = CoreXmlSqlParsersFactory
					.getSql("CrmBill.insertCrmBillM");//新增到crm提单m
			PreparedStatement pstaCrmBillM = connection.prepareStatement(insertCrmBillM);
			String insertCrmBillC = CoreXmlSqlParsersFactory
					.getSql("CrmBill.insertCrmBillC");//新增到crm提单c
			PreparedStatement pstaCrmBillC = connection.prepareStatement(insertCrmBillC);
			String insertCrmOut = CoreXmlSqlParsersFactory
					.getSql("CrmOutList.insertCrmOutList"); // 新增到crm出库表
			PreparedStatement pstaCrmOut = connection.prepareStatement(insertCrmOut);
			String addCrmMat = CoreXmlSqlParsersFactory
					.getSql("CrmMatZc.insertCrmMatZc_M");//新增crm材料表，取M
			PreparedStatement psAddCrmMat = connection.prepareStatement(addCrmMat);

			PreparedStatement pstaPro = connection
					.prepareStatement(updateProOrd);
			String billNo = list.get(0);
			String ordLnDlyPk = list.get(4);
			// String storageNo = list.get(5);
			String judgeNo = list.get(6);
			String batchNo = list.get(7);
			String groupNo = list.get(8);
			String cratname = list.get(9);
			String crattime = list.get(10);
			String manageNO = list.get(11);
			String manageName = list.get(12);
			String deparId = list.get(13);
			String deparNmae = list.get(14);
			// String gbflag = list.get(15);
			String bc = list.get(16);
			String bz = list.get(17);
			String prdCls = list.get(18);
			double sendnum = 0;
			double weghit = 0;// 退货重量

			String bill_no = querySendIn(connection, billNo);// 退货提单号
			double loadCount = 0;
			double loadlen = 0;

			String loadNo = queryLoadNo(connection, bill_no);
			String sendOut = querySendOut(connection, "800806");
			// 获取出库流水号
			String butessMatNo = FrmPipeEntruck.querySendOutList(connection,
					sendOut, "800806");
			int outNo = Integer.parseInt(butessMatNo.substring(
					butessMatNo.length() - 4, butessMatNo.length()));
			String loadSeq = queryLoadMatSeqNo(connection, loadNo);

			int loadSeqNo = Integer.parseInt(loadSeq.substring(
					loadSeq.length() - 5, loadSeq.length()));
			for (int i = 0; i < pram.size(); i++) {

				ArrayList<String> list1 = (ArrayList<String>) pram.get(i);
				String matNo = list1.get(0);

				int count = 0;// 支数
				double loadnum = 0;// 装车支数
				// 查询要退货的装车材料支数
				sqlquery = CoreXmlSqlParsersFactory
						.getSql("FrmReturnDashed.queryYdmLoadMat");
				ps = connection.prepareStatement(sqlquery);
				ps.setObject(1, billNo);
				ps.setObject(2, judgeNo);
				ps.setObject(3, batchNo);
				ps.setObject(4, groupNo);
				ps.setObject(5, ordLnDlyPk);
				// ps.setObject(6, vehiNo);
				rs = ps.executeQuery();
				if (rs.next()) {
					loadnum = Double.parseDouble(rs.getString("ACT_COUNT"));
				}
				rs.close();
				ps.close();

				double outnum = 0;// 已退货支数
				// 查询已退货的装车材料支数
				sqlquery = CoreXmlSqlParsersFactory
						.getSql("FrmReturnDashed.queryOutLoadMat");
				ps = connection.prepareStatement(sqlquery);
				ps.setObject(1, billNo);
				ps.setObject(2, judgeNo);
				ps.setObject(3, batchNo);
				ps.setObject(4, groupNo);
				ps.setObject(5, ordLnDlyPk);
				rs = ps.executeQuery();
				if (rs.next()) {
					outnum = Double.parseDouble(rs.getString("ACT_COUNT"));
				}
				rs.close();
				ps.close();

				if (loadnum + outnum == 0) {
					cro.setV_errMsg("炉号为：" + judgeNo + ",批号为：" + batchNo
							+ ",组号为：" + groupNo + ",已退完。");
					return cro;
				}

				// 查询要退货的装车材料记录
				sqlquery = CoreXmlSqlParsersFactory
						.getSql("FrmReturnDashed.queryLoadReturn1");
				ps = connection.prepareStatement(sqlquery);
				ps.setObject(1, billNo);
				ps.setObject(2, matNo);
				rs = ps.executeQuery();
				loadCEntitys = EntityTool.getEntitysByRs(rs,
						YdmZcLoadvehicleMatEntity.class);
				rs.close();
				ps.close();

				if (loadCEntitys == null || loadCEntitys.size() == 0) {
					cro.setV_errMsg("装车信息发生变化,请重新获取数据!");
					return cro;
				}

				for (YdmZcLoadvehicleMatEntity loadCEntity : loadCEntitys) {
					double weghitsj = Double.parseDouble(loadCEntity
							.getActWeight());
					// if (gbflag.equals("是")) {
					// weghitsj = Double.parseDouble(loadCEntity
					// .getPonderGrossWt());
					// }
					loadCount += Double.parseDouble(loadCEntity.getActCount());
					loadlen += Double.parseDouble(loadCEntity.getActLen());
					sendnum += -Double.parseDouble(loadCEntity.getSendNum());
					weghit += -Double.parseDouble(loadCEntity.getActWeight());
					// 新增材料表MAT_ZC_m（新增材料表，删除mat_zc_backup）
					
					pstaMatNo.setObject(1, weghitsj);
					pstaMatNo.setObject(2, pipmemoText);
					pstaMatNo.setObject(3, loadCEntity.getFinishproMold());
					pstaMatNo.setObject(4, "801704");
					pstaMatNo.setObject(5, loadCEntity.getMatNo());
					pstaMatNo.setObject(6, loadCEntity.getMatNo());
					pstaMatNo.addBatch();

					//新增crm材料表
					psAddCrmMat.setObject(1,"0");
					psAddCrmMat.setObject(2, strNowTime);
					psAddCrmMat.setObject(3, loadCEntity.getMatNo());
					psAddCrmMat.addBatch();

					// 新增位置表
					pstabutter.setObject(1, loadCEntity.getMatNo());
					pstabutter.setObject(2, loadCEntity.getJudgeStoveNo());
					pstabutter.setObject(3, loadCEntity.getBatchNo());
					pstabutter.setObject(4, loadCEntity.getGroupNo());
					if("".equals(storageNoChoose)){
						pstabutter.setObject(5, loadCEntity.getStorageNo());
						pstabutter.setObject(6, loadCEntity.getLocationNo());
					}
					else{
						pstabutter.setObject(5, storageNoChoose);
						pstabutter.setObject(6, storageNoChoose+"10101");
					}
					pstabutter.setObject(7, loadCEntity.getLaryNo());
					pstabutter.setObject(8, loadCEntity.getLocNo());
					pstabutter.setObject(9, "");
					pstabutter.setObject(10, "");
					pstabutter.setObject(11, loadCEntity.getStorageTypeNo());
					pstabutter.setObject(12, loadCEntity.getStorageTypeName());
					pstabutter.setObject(13, manageNO);
					pstabutter.setObject(14, manageName);
					pstabutter.setObject(15, cratname);
					pstabutter.setObject(16, crattime);
					pstabutter.setObject(17, deparId);
					pstabutter.setObject(18, deparNmae);
					pstabutter.addBatch();

					// 新增出库表
					pstaOut.setString(1,
							sendOut + StringTool.formatDecimal(outNo, "0000"));
					pstaOut.setString(2, sendOut);
					pstaOut.setString(3, "800806");
					pstaOut.setString(4, "出库红冲");
					pstaOut.setString(5, loadCEntity.getVehicleNo());
					pstaOut.setString(6, loadCEntity.getMatNo());
					pstaOut.setString(7, loadCEntity.getLotNo());
					pstaOut.setString(8, loadCEntity.getStoveNo());
					pstaOut.setString(9, loadCEntity.getJudgeStoveNo());
					pstaOut.setString(10, loadCEntity.getBatchNo());
					pstaOut.setString(11, loadCEntity.getGroupNo());
					pstaOut.setString(12, loadCEntity.getMaterialNo());
					pstaOut.setString(13, loadCEntity.getMaterialName());
					pstaOut.setString(14, loadCEntity.getProduccode());
					pstaOut.setString(15, loadCEntity.getProducname());
					pstaOut.setString(16, loadCEntity.getStdCode());
					pstaOut.setString(17, loadCEntity.getStdName());
					pstaOut.setString(18, loadCEntity.getGradecode());
					pstaOut.setString(19, loadCEntity.getGradename());
					pstaOut.setString(20, loadCEntity.getSteelcode());
					pstaOut.setString(21, loadCEntity.getSteelname());
					pstaOut.setString(22, loadCEntity.getStdStyle());
					pstaOut.setString(23, loadCEntity.getStdStyleDesc());
					pstaOut.setString(24, loadCEntity.getSpecCode());
					pstaOut.setString(25, loadCEntity.getSpecName());
					pstaOut.setString(26, loadCEntity.getModelCode());
					pstaOut.setString(27, loadCEntity.getModelDesc());
					pstaOut.setString(28, loadCEntity.getPlineCode());
					pstaOut.setString(29, loadCEntity.getPlineName());
					pstaOut.setString(30, loadCEntity.getActDimater());
					pstaOut.setString(31, loadCEntity.getActHeight());
					pstaOut.setString(32, String.valueOf(-Double
							.parseDouble(loadCEntity.getActLen())));// 长度
					pstaOut.setString(33, loadCEntity.getActLenMin());
					pstaOut.setString(34, loadCEntity.getActLenMax());
					pstaOut.setString(35, "-1");// 支数
					pstaOut.setObject(36, "-" + loadCEntity.getActWeight());// 重量
					pstaOut.setString(37, String.valueOf(-Double
							.parseDouble(loadCEntity.getActTheoryWeight())));// 理重
					
					pstaOutXs.setString(1,
							sendOut + StringTool.formatDecimal(outNo, "0000"));
					pstaOutXs.setString(2, sendOut);
					pstaOutXs.setString(3, "800806");
					pstaOutXs.setString(4, "出库红冲");
					pstaOutXs.setString(5, loadCEntity.getVehicleNo());
					pstaOutXs.setString(6, loadCEntity.getMatNo());
					pstaOutXs.setString(7, loadCEntity.getLotNo());
					pstaOutXs.setString(8, loadCEntity.getStoveNo());
					pstaOutXs.setString(9, loadCEntity.getJudgeStoveNo());
					pstaOutXs.setString(10, loadCEntity.getBatchNo());
					pstaOutXs.setString(11, loadCEntity.getGroupNo());
					pstaOutXs.setString(12, loadCEntity.getMaterialNo());
					pstaOutXs.setString(13, loadCEntity.getMaterialName());
					pstaOutXs.setString(14, loadCEntity.getProduccode());
					pstaOutXs.setString(15, loadCEntity.getProducname());
					pstaOutXs.setString(16, loadCEntity.getStdCode());
					pstaOutXs.setString(17, loadCEntity.getStdName());
					pstaOutXs.setString(18, loadCEntity.getGradecode());
					pstaOutXs.setString(19, loadCEntity.getGradename());
					pstaOutXs.setString(20, loadCEntity.getSteelcode());
					pstaOutXs.setString(21, loadCEntity.getSteelname());
					pstaOutXs.setString(22, loadCEntity.getStdStyle());
					pstaOutXs.setString(23, loadCEntity.getStdStyleDesc());
					pstaOutXs.setString(24, loadCEntity.getSpecCode());
					pstaOutXs.setString(25, loadCEntity.getSpecName());
					pstaOutXs.setString(26, loadCEntity.getModelCode());
					pstaOutXs.setString(27, loadCEntity.getModelDesc());
					pstaOutXs.setString(28, loadCEntity.getPlineCode());
					pstaOutXs.setString(29, loadCEntity.getPlineName());
					pstaOutXs.setString(30, loadCEntity.getActDimater());
					pstaOutXs.setString(31, loadCEntity.getActHeight());
					pstaOutXs.setString(32, String.valueOf(-Double
							.parseDouble(loadCEntity.getActLen())));// 长度
					pstaOutXs.setString(33, loadCEntity.getActLenMin());
					pstaOutXs.setString(34, loadCEntity.getActLenMax());
					pstaOutXs.setString(35, "-1");// 支数
					pstaOutXs.setObject(36, "-" + loadCEntity.getActWeight());// 重量
					pstaOutXs.setString(37, String.valueOf(-Double
							.parseDouble(loadCEntity.getActTheoryWeight())));// 理重
					
					if("".equals(storageNoChoose)){
						pstaOut.setString(38, loadCEntity.getStorageNo());
						pstaOut.setString(39, loadCEntity.getLocationNo());
						pstaOutXs.setString(38, loadCEntity.getStorageNo());
						pstaOutXs.setString(39, loadCEntity.getLocationNo());
					}else{
						pstaOut.setString(38, storageNoChoose);
						pstaOut.setString(39, storageNoChoose+"10101");
						pstaOutXs.setString(38, storageNoChoose);
						pstaOutXs.setString(39, storageNoChoose+"10101");
					}
					pstaOut.setString(40, loadCEntity.getLaryNo());
					pstaOut.setString(41, loadCEntity.getLocNo());
					pstaOut.setString(42, "");
					pstaOut.setString(43, "");
					pstaOut.setString(44, loadCEntity.getStorageTypeNo());
					pstaOut.setString(45, loadCEntity.getStorageTypeName());
					pstaOut.setString(46, manageNO);
					pstaOut.setString(47, manageName);
					pstaOutXs.setString(40, loadCEntity.getLaryNo());
					pstaOutXs.setString(41, loadCEntity.getLocNo());
					pstaOutXs.setString(42, "");
					pstaOutXs.setString(43, "");
					pstaOutXs.setString(44, loadCEntity.getStorageTypeNo());
					pstaOutXs.setString(45, loadCEntity.getStorageTypeName());
					pstaOutXs.setString(46, manageNO);
					pstaOutXs.setString(47, manageName);
					if (loadCEntity.getRpoTime() == "") {
						pstaOut.setString(48, "");
						pstaOutXs.setString(48, "");

					} else {
						pstaOut.setString(48, loadCEntity.getRpoTime()
								.substring(0, 19));
						pstaOutXs.setString(48, loadCEntity.getRpoTime()
								.substring(0, 19));
					}
					pstaOut.setString(49, loadCEntity.getProBc());
					pstaOut.setString(50, loadCEntity.getProBz());
					pstaOut.setString(51, crattime);
					pstaOut.setString(52, bc);
					pstaOut.setString(53, bz);
					pstaOut.setString(54, cratname);
					pstaOut.setString(55, crattime);
					pstaOut.setString(56, loadCEntity.getOrdPk());
					pstaOut.setString(57, loadCEntity.getOrdLnPk());
					pstaOut.setString(58, loadCEntity.getOrdLnDlyPk());
					pstaOut.setString(59, loadCEntity.getOrderNo());
					pstaOut.setString(60, loadCEntity.getOrderSeq());
					pstaOut.setString(61, loadCEntity.getOrderSeqNo());
					pstaOut.setString(62, deparId);
					pstaOut.setString(63, deparNmae);
					pstaOut.setString(64, loadCEntity.getSaleOrg());
					pstaOut.setString(65, loadCEntity.getSaleOrgDesc());
					pstaOut.setString(66, loadCEntity.getPonderNo());
					pstaOut.setObject(67, "-" + loadCEntity.getPonderGrossWt());
					pstaOut.setObject(68, bill_no);
					pstaOut.setObject(69, "-" + loadCEntity.getSendNum());
					pstaOut.setObject(70, loadCEntity.getVstel());
					pstaOut.setObject(71, loadCEntity.getVstelDesc());
					pstaOut.setObject(72, "0");
					pstaOut.setObject(73, "801403");
					pstaOut.setObject(74, "80150301");
					pstaOut.setObject(75, loadCEntity.getReceivNo());
					pstaOut.setObject(76, loadCEntity.getReceivNm());
					pstaOut.setObject(77, "");
					pstaOut.setObject(78, loadCEntity.getFinishproMold());
					pstaOut.setObject(79, "801704");
					pstaOut.setObject(80, cusInfo);
					pstaOut.setObject(81, cusInfo);
					pstaOut.setObject(82, loadCEntity.getMatNo());
					pstaOut.setObject(83, loadCEntity.getMatNo());
					pstaOut.setObject(84, loadCEntity.getMatNo());
					pstaOut.setObject(85, "120504".equals(cusInfo)? ComBaseInfo.GetBalYear_2416(now): ComBaseInfo.GetBalYear(now));
					pstaOut.setObject(86, loadCEntity.getMatNo());
					pstaOut.setObject(87, returnType);
					pstaOut.setObject(88, returnCode);
					pstaOut.addBatch();
					
					pstaOutXs.setString(49, loadCEntity.getProBc());
					pstaOutXs.setString(50, loadCEntity.getProBz());
					pstaOutXs.setString(51, crattime);
					pstaOutXs.setString(52, bc);
					pstaOutXs.setString(53, bz);
					pstaOutXs.setString(54, cratname);
					pstaOutXs.setString(55, crattime);
					pstaOutXs.setString(56, loadCEntity.getOrdPk());
					pstaOutXs.setString(57, loadCEntity.getOrdLnPk());
					pstaOutXs.setString(58, loadCEntity.getOrdLnDlyPk());
					pstaOutXs.setString(59, loadCEntity.getOrderNo());
					pstaOutXs.setString(60, loadCEntity.getOrderSeq());
					pstaOutXs.setString(61, loadCEntity.getOrderSeqNo());
					pstaOutXs.setString(62, deparId);
					pstaOutXs.setString(63, deparNmae);
					pstaOutXs.setString(64, loadCEntity.getSaleOrg());
					pstaOutXs.setString(65, loadCEntity.getSaleOrgDesc());
					pstaOutXs.setString(66, loadCEntity.getPonderNo());
					pstaOutXs.setObject(67, "-" + loadCEntity.getPonderGrossWt());
					pstaOutXs.setObject(68, bill_no);
					pstaOutXs.setObject(69, "-" + loadCEntity.getSendNum());
					pstaOutXs.setObject(70, loadCEntity.getVstel());
					pstaOutXs.setObject(71, loadCEntity.getVstelDesc());
					pstaOutXs.setObject(72, "0");
					pstaOutXs.setObject(73, "801403");
					pstaOutXs.setObject(74, "80150301");
					pstaOutXs.setObject(75, loadCEntity.getReceivNo());
					pstaOutXs.setObject(76, loadCEntity.getReceivNm());
					pstaOutXs.setObject(77, "");
					pstaOutXs.setObject(78, loadCEntity.getFinishproMold());
					pstaOutXs.setObject(79, "801704");
					pstaOutXs.setObject(80, cusInfo);
					pstaOutXs.setObject(81, cusInfo);
					pstaOutXs.setObject(82, loadCEntity.getMatNo());
					pstaOutXs.setObject(83, loadCEntity.getMatNo());
					pstaOutXs.setObject(84, loadCEntity.getMatNo());
					pstaOutXs.setObject(85, "120504".equals(cusInfo)? ComBaseInfo.GetBalYear_2416(now): ComBaseInfo.GetBalYear(now));
					pstaOutXs.setObject(86, loadCEntity.getMatNo());
					pstaOutXs.setObject(87, returnType);
					pstaOutXs.setObject(88, returnCode);
					pstaOutXs.addBatch();

					//crm 出库表迁移
					pstaCrmOut.setObject(
							1,
							loadNo
									+ StringTool.formatDecimal(loadSeqNo,
									"00000"));
					pstaCrmOut.setString(2,bill_no);
					pstaCrmOut.setString(3,loadCEntity.getLoadvehicleNo());
					pstaCrmOut.setString(4,loadCEntity.getJudgeStoveNo());
					pstaCrmOut.setString(5, loadCEntity.getBatchNo());
					pstaCrmOut.setString(6, loadCEntity.getGroupNo());
					pstaCrmOut.setString(7, loadCEntity.getMaterialNo());
					pstaCrmOut.setString(8, loadCEntity.getMaterialName());
					pstaCrmOut.setString(9, loadCEntity.getProduccode());
					pstaCrmOut.setString(10, loadCEntity.getProducname());
					pstaCrmOut.setString(11, loadCEntity.getStdCode());
					pstaCrmOut.setString(12, loadCEntity.getStdName());
					pstaCrmOut.setString(13, loadCEntity.getGradecode());
					pstaCrmOut.setString(14, loadCEntity.getGradename());
					pstaCrmOut.setString(15, loadCEntity.getSteelcode());
					pstaCrmOut.setString(16, loadCEntity.getSteelname());
					pstaCrmOut.setString(17, loadCEntity.getStdStyle());
					pstaCrmOut.setString(18, loadCEntity.getStdStyleDesc());
					pstaCrmOut.setString(19, loadCEntity.getSpecCode());
					pstaCrmOut.setString(20, loadCEntity.getSpecName());
					pstaCrmOut.setString(21, loadCEntity.getModelCode());
					pstaCrmOut.setString(22, loadCEntity.getModelDesc());
					pstaCrmOut.setString(23, loadCEntity.getActLenMin());
					pstaCrmOut.setString(24, loadCEntity.getActLenMax());
					pstaCrmOut.setString(25, "-1");
					pstaCrmOut.setString(26, "-" + loadCEntity.getActWeight());
					pstaCrmOut.setString(27, String.valueOf(-Double
							.parseDouble(loadCEntity.getActLen())));
					pstaCrmOut.setString(28, loadCEntity.getStorageNo());
					pstaCrmOut.setString(29, loadCEntity.getStorageNo());
					pstaCrmOut.setString(30, loadCEntity.getSenTime());
					pstaCrmOut.setString(31, loadCEntity.getValidflag());
					pstaCrmOut.setString(32, loadCEntity.getOrderNo());
					pstaCrmOut.setString(33, loadCEntity.getOrderSeq());
					pstaCrmOut.setString(34, loadCEntity.getOrderSeqNo());
					pstaCrmOut.setString(35, loadCEntity.getOrdPk());
					pstaCrmOut.setString(36, loadCEntity.getOrdLnPk());
					pstaCrmOut.setString(37, loadCEntity.getOrdLnDlyPk());
					pstaCrmOut.setString(38,"0");
					pstaCrmOut.setString(39, strNowTime);
					pstaCrmOut.setString(40,billNo);
					pstaCrmOut.setString(41,billNo);
					pstaCrmOut.setString(42,billNo);
					pstaCrmOut.setString(43,"120504".equals(cusInfo)? ComBaseInfo.GetBalYear_2416(now): ComBaseInfo.GetBalYear(now));
					pstaCrmOut.setString(44,loadCEntity.getActDimater());
					pstaCrmOut.setString(45,loadCEntity.getActHeight());
					pstaCrmOut.setString(46,loadCEntity.getSaleOrg());
					pstaCrmOut.setString(47,loadCEntity.getSaleOrgDesc());
					pstaCrmOut.setString(48, "800806");
					pstaCrmOut.setString(49, "出库红冲");
					pstaCrmOut.addBatch();

					// 提单从表
					String billCseq = queryBillCSeq(connection,
							loadCEntity.getMatNo());
					pstaBillC.setObject(1, billCseq);
					pstaBillC.setObject(2, bill_no);
					pstaBillC.setObject(3, loadCEntity.getMatNo());
					pstaBillC.setObject(4, String.valueOf(-Double
							.parseDouble(loadCEntity.getActLen())));
					pstaBillC.setObject(5, "-1");
					pstaBillC.setObject(6, String.valueOf(-weghitsj));
					pstaBillC.setObject(7, String.valueOf(-Double
							.parseDouble(loadCEntity.getActTheoryWeight())));
					pstaBillC.setObject(8, cratname);
					pstaBillC.setObject(9, crattime);
					pstaBillC.setObject(10, "-" + loadCEntity.getSendNum());
					pstaBillC.setObject(11, "801704");
					pstaBillC.setObject(12, matNo);
					pstaBillC.setObject(13, billNo);
					pstaBillC.addBatch();

					// 装车从表
					pstaLoadMat.setObject(
							1,
							loadNo
									+ StringTool.formatDecimal(loadSeqNo,
											"00000"));
					pstaLoadMat.setObject(2, loadNo);
					pstaLoadMat.setObject(3, bill_no);
					pstaLoadMat.setObject(4, loadCEntity.getMatNo());
					pstaLoadMat.setObject(5, String.valueOf(-Double
							.parseDouble(loadCEntity.getActLen())));
					pstaLoadMat.setObject(6, "-1");
					pstaLoadMat.setObject(7, String.valueOf(-weghitsj));
					pstaLoadMat.setObject(8, String.valueOf(-Double
							.parseDouble(loadCEntity.getActTheoryWeight())));
					pstaLoadMat.setObject(9, crattime);
					pstaLoadMat.setObject(10, cratname);
					pstaLoadMat.setObject(11, crattime);
					pstaLoadMat.setObject(12,
							"-" + loadCEntity.getPonderGrossWt());
					pstaLoadMat.setObject(13, "-" + loadCEntity.getSendNum());
					pstaLoadMat.setObject(14, "801704");
					pstaLoadMat.setObject(15, "120504".equals(cusInfo)? ComBaseInfo.GetBalYear_2416(now): ComBaseInfo.GetBalYear(now));
					pstaLoadMat.setObject(16, loadCEntity.getMatNo());
					pstaLoadMat.setObject(17, billNo);
					pstaLoadMat.addBatch();

					loadSeqNo++;
					outNo++;
					// matNo++;
				}
				pstaMatNo.executeBatch();
				psAddCrmMat.executeBatch();
				pstabutter.executeBatch();
				pstaBillC.executeBatch();
				pstaLoadMat.executeBatch();
				pstaOut.executeBatch();
				pstaOutXs.executeBatch();
				pstaCrmOut.executeBatch();
			}
			pstaPro.setObject(1, String.valueOf(Math.abs(-loadCount)));
			pstaPro.setObject(2, String.valueOf(Math.abs(weghit)));
			pstaPro.setObject(3, String.valueOf(Math.abs(sendnum)));
			pstaPro.setObject(4, ordLnDlyPk);
			pstaPro.addBatch();
			pstaPro.executeBatch();

			// 查询提单主表是否有记录
			String BillCOUNT = "";
			String billMatNo = "";// 对齐
			sqlquery = CoreXmlSqlParsersFactory
					.getSql("FrmReturnDashed.queryBillMReturn");
			ps = connection.prepareStatement(sqlquery);
			ps.setObject(1, bill_no);
			ps.setObject(2, ordLnDlyPk);
			rs = ps.executeQuery();
			if (rs.next()) {
				BillCOUNT = rs.getString("COUNT");
				billMatNo = rs.getString("BILL_NO_SEQ");
			}
			rs.close();
			ps.close();

			if (BillCOUNT.equals("0")) {
				String bill_seq = querySendInList(connection, bill_no);
				psatBillM.setObject(1, bill_seq);
				psatBillM.setObject(2, bill_no);
				psatBillM.setObject(3, String.valueOf(weghit));
				psatBillM.setObject(4, String.valueOf(-loadCount));
				psatBillM.setObject(5, String.valueOf(-loadlen));
				psatBillM.setObject(6, String.valueOf(weghit));
				psatBillM.setObject(7, String.valueOf(-loadCount));
				psatBillM.setObject(8, String.valueOf(-loadlen));
				psatBillM.setObject(9, cratname);
				psatBillM.setObject(10, crattime);
				psatBillM.setObject(11, crattime);
				psatBillM.setObject(12, billNo);
				psatBillM.setObject(13, ordLnDlyPk);
				psatBillM.addBatch();
				psatBillM.executeBatch();

				//crm提单主表
//				pstaCrmBillM.setObject(1,strNowTime);
				pstaCrmBillM.setObject(1,"0");
				pstaCrmBillM.setObject(2,"0");
				pstaCrmBillM.setObject(3,bill_no);
				pstaCrmBillM.addBatch();
			} else {
				String sqlupdate = "UPDATE YDM_ZC_BILL_M T"
						+ " SET T.PLAN_WT   = TO_NUMBER(T.PLAN_WT) + TO_NUMBER('"
						+ String.valueOf(weghit)
						+ "'),"
						+ "T.PLAN_NUM      = TO_NUMBER(T.PLAN_NUM) + TO_NUMBER('"
						+ String.valueOf(-loadCount)
						+ "'),"
						+ "T.PLAN_TOTL_LEN = TO_NUMBER(T.PLAN_TOTL_LEN) + TO_NUMBER('"
						+ String.valueOf(-loadlen)
						+ "'),"
						+ "T.ACT_WT        = TO_NUMBER(T.ACT_WT) + TO_NUMBER('"
						+ String.valueOf(weghit)
						+ "'),"
						+ "T.ACT_NUM       = TO_NUMBER(T.ACT_NUM) + TO_NUMBER('"
						+ String.valueOf(-loadCount)
						+ "'),"
						+ "T.ACT_TOTL_LEN  = TO_NUMBER(T.ACT_TOTL_LEN) + TO_NUMBER('"
						+ String.valueOf(-loadlen) + "')"
						+ " WHERE T.BILL_NO_SEQ = '" + billMatNo
						+ "'  AND T.RETURN_FLAG = '1'"; // 修改提单主表量
				Statement sta1 = connection.createStatement();
				sta1.executeUpdate(sqlupdate);
				sta1.close();

				//crm提单主表
				CrmBillService crmBillService = new CrmBillService();
				if(crmBillService.selectCrmBill(connection,billNo,"0")){
//					pstaCrmBillM.setObject(1,strNowTime);
					pstaCrmBillM.setObject(1,"1");
					pstaCrmBillM.setObject(2,"1");
					pstaCrmBillM.setObject(3,bill_no);
					pstaCrmBillM.addBatch();
				}
			}

			// 装车主表
			pstaLoad.setObject(1, loadNo);
			pstaLoad.setObject(2, bill_no);
			pstaLoad.setObject(3, String.valueOf(weghit));
			pstaLoad.setObject(4, String.valueOf(-loadCount));
			pstaLoad.setObject(5, String.valueOf(-loadlen));
			pstaLoad.setObject(6, crattime);
			pstaLoad.setObject(7, management_no);
			pstaLoad.setObject(8, management_name);
			pstaLoad.setObject(9, cratname);
			pstaLoad.setObject(10, crattime);
			pstaLoad.setObject(11, departid);
			pstaLoad.setObject(12, departname);
			pstaLoad.setObject(13, "1");
			pstaLoad.setObject(14, billNo);
			pstaLoad.setObject(15, prdCls);
			pstaLoad.addBatch();
			pstaLoad.executeBatch();

			pstaCrmBillM.executeBatch();
			//crm提单从表
//			pstaCrmBillC.setObject(1,strNowTime);
			pstaCrmBillC.setObject(1,"0");
			pstaCrmBillC.setObject(2,bill_no);
			pstaCrmBillC.addBatch();
			pstaCrmBillC.executeBatch();

			cro.setV_errMsg("新增退货成功！");

			connection.commit();
		} catch (Exception e) {
			cro.setV_errMsg(e.getMessage());
			cro.setV_errCode(-1);
			if (connection != null) {
				try {
					connection.rollback(); // 回滚事务
				} catch (Exception e2) {
				}
			}
		} finally {
			try {
				if (connection != null && !connection.isClosed()) {
					connection.close();
				}
			} catch (Exception e2) {
			}
		}
		return cro;
	}
}
