package com.steering.pss.ydm.pipeleavefactory;

import CoreFS.SA06.CoreReturnObject;
import CoreFS.SA08.CoreXmlSqlParsersFactory;
import CoreFS.SC01.CoreConstantData;
import net.sf.json.JSONObject;

import com.google.gson.Gson;
import com.steering.comm.DefaultWebServiceRequest;
import com.steering.comm.ServiceToCx;
import com.steering.comm.converter.CoreConverter;
import com.steering.comm.db.DBCommon;
import com.steering.comm.tools.EntityTool;
import com.steering.pss.sale.order.orderchange.CrmBillService;
import com.steering.pss.sale.order.qualityDesign.QualityDesignTool;
import com.steering.pss.ydm.base.MeanValueCalculation;
import com.steering.pss.ydm.entity.*;
import org.apache.commons.beanutils.BeanUtils;

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

/**
 * 汽运提单编制
 * 
 * @author Administrator
 */
public class CorePipeCarBill extends DBCommon {

	/**
	 * 查询挂提单资源量
	 * 
	 * @param con
	 * @param matInfoEntity
	 * @return
	 * @throws SQLException
	 */
	public static double[] getInventoryCanWgt(Connection con,
			MatInfoEntity matInfoEntity) throws SQLException {
		double[] arr = new double[3];
		String sql = CoreXmlSqlParsersFactory
				.getSql("CorePipeCarBill.getInventoryCanWgt");
		sql = EntityTool.fillSqlByEntity(sql, matInfoEntity);
		PreparedStatement ps = con.prepareStatement(sql);
		ps.setObject(1, Integer.parseInt(matInfoEntity.getActCount()) + 1);
		ResultSet rs = ps.executeQuery();
		if (rs.next()) {
			arr[0] = rs.getDouble("SUM_COUNT");
			arr[1] = rs.getDouble("SUM_WGT");
			arr[2] = rs.getDouble("SUM_LEN");
		}
		rs.close();
		ps.close();
		return arr;
	}

	/***
	 * 按管号查询
	 * @return
	 */
	public CoreReturnObject doQueryDataMat(String matZcTity)
	{

		MatZcMEntity matZcEntity = EntityTool.getEntityByJSONStr(matZcTity, MatZcMEntity.class);
		String sql = CoreXmlSqlParsersFactory.getSql("CorePipeCarBill.doQueryDataMat");
		sql = EntityTool.fillSqlByEntity(sql,matZcEntity);
		return  this.getData(sql);
	}
    /**
     * 查询收貨單位
     * @return 平台统一返回对象
     */
    public CoreReturnObject getReceivNo() {
        return this.getDataBySqlID("CorePipeCarBill.getReceivNo");
    }

	/**
	 * 许可证号
	 */
	public CoreReturnObject queryLicenseNo(String licesinNo) {
		String sql = "SELECT T.BASECODE,T.BASENAME FROM COM_BASE_INFO T WHERE T.SORTCODE='"
				+ licesinNo + "' AND T.VALIDFLAG='1'";
		return this.getData(sql);
	}

	/**
	 * 发运计划查询
	 * 
	 * @param
	 *
	 * @return obj
	 */
	public CoreReturnObject querySendPlan(ArrayList<Object> params,
			String[] args,String supplyUnit) {
		String sql = CoreXmlSqlParsersFactory
				.getSql("CorePipeCarBill.querySendPlan");
		if(supplyUnit.equals("120501"))
		{
			//sql += " AND (T.SUPPLY_UNIT = '"+supplyUnit+"' OR T.SUPPLY_UNIT IS NULL)";
		}else if(supplyUnit.equals("120504")){
			sql += " AND T.SUPPLY_UNIT = '"+supplyUnit+"'";
		}
		sql += CoreConverter.toSqlConditionString("T.MANAGEMENT_NO", args);
		sql += " ORDER BY T.PLAN_YEAR_MONTH, T.SEND_PLAN_NO";
		return this.getData(sql, params.toArray());
	}

	/**
	 * 根据产线产线管理部门
	 */
	public CoreReturnObject queryPlineDepart(String plineCode) {
		String sql = CoreXmlSqlParsersFactory
				.getSql("CorePipeCarBill.queryPlineDepart");
		return this.getData(sql, new Object[] { plineCode });
	}

	/**
	 * 导出数据
	 * 
	 * @param
	 *
	 * @return obj
	 */
	public CoreReturnObject queryBillExtie(String[] billNo) {
		String sql = CoreXmlSqlParsersFactory
				.getSql("CorePipeCarBill.queryBillExtie");
		sql += " AND (" + CoreConverter.toSqlConditionString1000("T.BILL_NO", billNo) + ")";
		sql += "  GROUP BY T.BILL_NO, T.ORDER_NO, T.ORDER_SEQ, H.SALE_AREA_DESC, T.DELIVERY_NO, "
				+ "T.PLAN_TRANS_TYPE, C.JUDGE_STOVE_NO, C.BATCH_NO, C.GROUP_NO, C.STORAGE_NO, C.LOCATION_NO, "
				+ "C.VSTEL_DESC, T.CREATE_NAME, T.CREATE_TIME, C.PRODUCNAME, C.STEELNAME, "
				+ "C.SPEC_NAME,T.CUSTOMER_NM, T.UPDATE_TIME, T.BILL_STATUS, T.LOADVEHICLE_TYPE, T.STATION_NM, "
				+ "T.SPCL_LN_NM, C.STD_NAME, C.GRADENAME, T.RECEIV_NM, C.STD_STYLE_DESC, "
				+ "C.MODEL_DESC, T.ISPRECLOSE, T.SALE_ORG_DESC, T.PICKUP_FLAG, T.ZOUT_FLAG, "
				+ "T.BILL_TICKET_STATUS, T.OUTSTOCK_TICKET_STATUS, T.TRANSMNY_TICKET_STATUS, "
				+ "C.EDIT_FLAG, C.LOADVEHICLE_FLAG,C.ACT_LEN_MIN,C.ACT_LEN_MAX  ORDER BY T.BILL_NO";
		return this.getData(sql);
	}

	/***
	 * 回写打印次数，打印时间
	 * 
	 * @param
	 * @return
	 */
	public CoreReturnObject updatePintFlag(String time, String billNo) {
		return this.setDataBySqlID("CorePipeCarBill.updatePintFlag",
				new Object[] { time, billNo });
	}

	/***
	 * 查询是否理货
	 * 
	 * @param
	 * @return
	 */
	public CoreReturnObject getBillCedit(String billNo, String judNo,
			String batcNo, String groupNo, String endstart) {
		return this.getDataBySqlID("CorePipeCarBill.getBillCedit",
				new Object[] { billNo, judNo, batcNo, groupNo, endstart });
	}

	/**
	 * 修改过磅
	 * 
	 * @param list
	 *            传递参数
	 * @return obj
	 */
	public CoreReturnObject updatePrtFalg(ArrayList<ArrayList<String>> list) {
		ArrayList<String> sqllist = new ArrayList<String>();
		ArrayList<Object[]> parmlist = new ArrayList<Object[]>();
		for (int i = 0; i < list.size(); i++) {
			sqllist.add(CoreXmlSqlParsersFactory
					.getSql("CorePipeCarBill.UpdatePrtFalg"));
			parmlist.add(list.get(i).toArray());
		}
		return this.getDao(CoreConstantData.sysbeanstr).ExcuteTransction(
				sqllist, parmlist); // 执行事务处理（多个非查询， 自动提交、回滚）
	}

	/**
	 * 发运合同信息
	 * 
	 * @param strPlanNo
	 *            参数
	 * @return obj
	 */
	public CoreReturnObject queryOrderInfo(String strPlanNo) {
		Object[] obj = new Object[] { strPlanNo };
		return this.getDataBySqlID("CorePipeCarBill.QueryOrderInfo", obj);
	}

	/**
	 * 查询目标港
	 * 
	 * @param
	 *
	 * @return obj
	 */
	public CoreReturnObject getPortDesc(String portNo) {

		return this.getDataBySqlID("CorePipeCarBill.getPortDesc",
				new Object[] { portNo });
	}

	/**
	 * 查询合同信息对应材料表信息
	 * 
	 * @param list
	 *            合同数据
	 * @return obj
	 */
	public CoreReturnObject queryMatInfo(ArrayList<String> list,String custom) {
		String sql = "";
//		if(custom.equals("1_120504"))
//		{
			sql  = CoreXmlSqlParsersFactory.getSql("CorePipeCarBill.QueryMatInfoTH");
//		}else
//		{
//			sql = CoreXmlSqlParsersFactory.getSql("CorePipeCarBill.QueryMatInfo");
//		}
		return this.getData(sql, list.toArray());
	}


	/**
	 * 查询提单主表信息
	 * 
	 * @param
	 *
	 * @return obj
	 */
	public CoreReturnObject queryBillM(ArrayList<String> params,
									   String[] salgs, String prdCls, String supplyUnit) {
		return queryBillM(params,salgs,prdCls,supplyUnit,"");
	}
	public CoreReturnObject queryBillM(ArrayList<String> params,
			String[] salgs, String prdCls, String supplyUnit,String cust) {
		String sql = CoreXmlSqlParsersFactory
				.getSql("CorePipeCarBill.QueryBillM");
		// if (prdCls.equals("405005")) {
		// sql += " AND T.PRD_CLS='405005'";
		// }
		// else {
		// sql += " AND (T.PRD_CLS!='405005' OR T.PRD_CLS IS NULL)";
		// }
		if(supplyUnit.equals("120501"))
		{
			//sql += " AND (T.SUPPLY_UNIT = '"+supplyUnit+"' OR T.SUPPLY_UNIT IS NULL)";
		}else if(supplyUnit.equals("120504")){
			sql += " AND T.SUPPLY_UNIT = '"+supplyUnit+"'";
		}
		// 天淮关联交易
		if (cust.contains("GLJY")) {
			sql += " AND EXISTS (SELECT 1 FROM PLN_ORDER_ZG_S WHERE TH_ORDER_NO_SEQ = T.ORDER_NO || '/'||T.ORDER_SEQ)";
		}
//		sql += CoreConverter.toSqlConditionString("T.MANAGEMENT_NO", salgs);
		sql += " AND (" + CoreConverter.toSqlConditionString1("T.MANAGEMENT_NO", salgs);
		sql += " OR " + CoreConverter.toSqlConditionString1("H.DOCUMENTARY_DEPT", salgs) + ")" ;
		sql += "  GROUP BY T.BILL_NO,T.BILL_STATUS,T.LOADVEHICLE_TYPE,T.PLAN_TRANS_TYPE,T.SEND_PLAN_NO, T.DELVRY_ADDR,"
				+ "T.ISPRECLOSE, T.SALE_ORG,T.SALE_ORG_DESC,T.CUSTOMER_NO,T.CUSTOMER_NM,T.RECEIV_NO,T.RECEIV_NM,T."
				+ "STLMT_UNIT_NO,T.STLMT_UNIT_NM, T.CREATE_NAME,T.ZOUT_FLAG,"
				+ "T.BILL_TICKET_STATUS,T.OUTSTOCK_TICKET_STATUS,T.TRANSMNY_TICKET_STATUS,T.SUPPLY_UNIT,T.SUPPLY_UNIT_DESC,T.MEMO"
				+ "  ORDER BY T.BILL_NO," + "T.BILL_STATUS,T.LOADVEHICLE_TYPE";
		return this.getData(sql, params.toArray());
	}
	
	/**
	 * 查询提单关联车号信息
	 * @param billNo
	 * @return
	 */
	public CoreReturnObject queryCarNoInfo(String billNo) {
		String sql = CoreXmlSqlParsersFactory
				.getSql("CorePipeCarBill.queryCarNoInfo");
		Object[] params = new Object[] { billNo };
		return this.getData(sql, params);
	}
	
	/**
	 * 查询提单关联车号信息
	 * @param billNo
	 * @return
	 */
	public CoreReturnObject queryCarNoWeightInfo(String billNo) {
		String sql = CoreXmlSqlParsersFactory
				.getSql("CorePipeCarBill.queryCarNoWeightInfo");
		Object[] params = new Object[] { billNo };
		return this.getData(sql, params);
	}
	
	/**
	 * 查询提单关联车号信息
	 * @param billNo
	 * @return
	 */
	public CoreReturnObject queryCarNoWeightInfoNew(String billNo) {
		String sql = CoreXmlSqlParsersFactory
				.getSql("CorePipeCarBill.queryCarNoWeightInfoNew");
		return this.getData(sql);
	}


	public CoreReturnObject queryCarNoWeightInfoNewBySteel() {
		String sql = CoreXmlSqlParsersFactory
				.getSql("CorePipeCarBill.queryCarNoWeightInfoNewBySteel");
		return this.getData(sql);
	}

	public CoreReturnObject queryCarNoWeightInfoNewBySteel1() {
		String sql = CoreXmlSqlParsersFactory
				.getSql("CorePipeCarBill.queryCarNoWeightInfoNewBySteel1");
		return this.getData(sql);
	}

	/**
	 * 当日装车信息
	 * @return
	 */
	public CoreReturnObject queryCarNoLoadvehicleBySteel() {
		String sql = CoreXmlSqlParsersFactory
				.getSql("CorePipeCarBill.queryCarNoLoadvehicleBySteel");
		return this.getData(sql);
	}
	
	/**
	 * 保存车号
	 * @param parms
	 * @param billNo
	 * @return
	 */
	@SuppressWarnings("unchecked")
	public CoreReturnObject carNoAdd(ArrayList<Object> parms,
			String billNo) {
		CoreReturnObject core = new CoreReturnObject();
		Connection con = null;
		PreparedStatement ps = null;
		con = this.getDao(CoreConstantData.sysbeanstr).getConnection();
		try {
			con.setAutoCommit(false);
			String sql = CoreXmlSqlParsersFactory
                    .getSql("CorePipeCarBill.clearInfo");
			ps = con.prepareStatement(sql);
			ps.setObject(1, billNo);
            ps.executeUpdate();
            ps.close();
			for (int i = 0; i < parms.size(); i++) {
                ArrayList<Object> list = (ArrayList<Object>) parms.get(i);
                sql = CoreXmlSqlParsersFactory
                        .getSql("CorePipeCarBill.insertCarNo");
    			ps = con.prepareStatement(sql);
    			ps.setObject(1, list.get(0));
    			ps.setObject(2, list.get(1));
                ps.executeUpdate();
                ps.close();
            } 
			core.setV_errMsg("保存成功!");
			con.commit();
		} catch (SQLException e) {
			try {
				con.rollback();
			} catch (SQLException e1) {
				e1.printStackTrace();
			}
			e.printStackTrace();
		} finally {
			try {
				if (con != null)
					con.close();
			} catch (SQLException e) {
				e.printStackTrace();
			}
		}

		return core;
	}
	
	/**
	 * 根据id删除
	 * @param id
	 * @return
	 */
	public CoreReturnObject doDelete(String id) {
		CoreReturnObject core = new CoreReturnObject();
		Connection con = null;
		PreparedStatement ps = null;
		con = this.getDao(CoreConstantData.sysbeanstr).getConnection();
		try {
			con.setAutoCommit(false);
			String sql = CoreXmlSqlParsersFactory
                    .getSql("CorePipeCarBill.deleteById");
			ps = con.prepareStatement(sql);
			ps.setObject(1, id);
            ps.executeUpdate();
            ps.close();
			core.setV_errMsg("删除成功!");
			con.commit();
		} catch (SQLException e) {
			try {
				con.rollback();
			} catch (SQLException e1) {
				e1.printStackTrace();
			}
			e.printStackTrace();
		} finally {
			try {
				if (con != null)
					con.close();
			} catch (SQLException e) {
				e.printStackTrace();
			}
		}

		return core;
	}

	/**
	 * 查询提单材料表信息
	 * 
	 * @param
	 *
	 * @return obj
	 */
	public CoreReturnObject queryBillC(String billNo,String custom) {
		Object[] params = new Object[] { billNo };
		String sql = "";
//		if(custom.equals("1_120504"))
//		{
			sql = CoreXmlSqlParsersFactory.getSql("CorePipeCarBill.QueryBillCTH");
//		}else
//		{
//			sql  = CoreXmlSqlParsersFactory.getSql("CorePipeCarBill.QueryBillC");
//		}
		return  this.getData(sql,params);
	}

	/**
	 * 查询提单主表提单号
	 * 
	 * @param
	 *
	 * @return obj
	 */
	public CoreReturnObject queryBillNo(String salgAb) {
		Object[] params = new Object[] { salgAb, salgAb };
		return this.getDataBySqlID("CorePipeCarBill.QueryBillNo", params);
	}

	/**
	 * 查询提单主表主键
	 * 
	 * @param list
	 *            参数
	 * @return obj
	 */
	public CoreReturnObject getBillNoSeq(ArrayList<String> list) {
		return this.getDataBySqlID("CorePipeCarBill.GetBillNoSeq",
				list.toArray());
	}

	/**
	 * 查询改提货单是否已存在对应的计划号下
	 * 
	 * @param list
	 *            参数
	 * @return obj
	 */
	public CoreReturnObject queryBillNoSeq(String list) {
		return this.getDataBySqlID("CorePipeCarBill.QueryBillNoSeq",
				new Object[] { list });
	}

	/**
	 * 查询提单材料表主键
	 * 
	 * @param list
	 *            参数
	 * @return obj
	 */
	public CoreReturnObject getBillSeq(ArrayList<String> list) {
		return this
				.getDataBySqlID("CorePipeCarBill.GetBillSeq", list.toArray());
	}

	/**
	 * 查询提单材料表主键
	 * 
	 * @param list
	 *            参数
	 * @return obj
	 */
	public CoreReturnObject queryBillMat(ArrayList<String> list) {
		String sql = CoreXmlSqlParsersFactory
				.getSql("CorePipeCarBill.QueryBillMat");
		return this.getData(sql, list.toArray());
	}

	/**
	 * 废除提单信息
	 * 
	 * @param
	 *
	 * @param
	 *
	 * @param
	 *
	 * @return obj
	 */
	public CoreReturnObject updateBill(ArrayList<String> billNos,
			String userName, String deptId, String deptName) {
		CoreReturnObject core = new CoreReturnObject();
		Connection con = null;
		//Connection wlCon = null;
		PreparedStatement ps = null;
		ResultSet rs = null;
		String sql = "";
		try {
			con = this.getDao("TgDao").getConnection();
			con.setAutoCommit(false);
			//wlCon = new DBCommon().getDao("TmsDao").getConnection();注释生产物流  2024/4/24 肖沅累
			//wlCon.setAutoCommit(false);
			// 验证提单号
			sql = CoreXmlSqlParsersFactory
					.getSql("CorePipeCarBill.getBillInfoByBillNoSeq");
			for (String billNo : billNos) {
				ps = con.prepareStatement(sql);
				ps.setObject(1, billNo);
				rs = ps.executeQuery();
				List<YdmZcBillMEntity> billMEntitys = EntityTool
						.getEntitysByRs(rs, YdmZcBillMEntity.class);
				rs.close();
				ps.close();
				if (billMEntitys == null || billMEntitys.size() == 0) {
					core.setV_errMsg("提单信息发生变化,请重新获取数据!");
					return core;
				}
				if("1".equals(billMEntitys.get(0).getLockBillFlag())){
					core.setV_errMsg("提单" + billNo + "已锁定，不能撤销，请联系质保部业务员解锁后再进行操作!");
					return core;
				}
				for (YdmZcBillMEntity billMEntity : billMEntitys) {
					QualityDesignTool.nullConverNullString(billMEntity);
					String sqlBillC = CoreXmlSqlParsersFactory
							.getSql("CorePipeCarBill.DeleteBillC");
					String sqlBillM = CoreXmlSqlParsersFactory
							.getSql("CorePipeCarBill.DeleteBillM");
					String sqlMat = CoreXmlSqlParsersFactory
							.getSql("CorePipeCarBill.DeleteMat");
					String sqlSendM = CoreXmlSqlParsersFactory
							.getSql("CorePipeCarBill.DeleteSendM");
					String sqlSendC = CoreXmlSqlParsersFactory
							.getSql("CorePipeCarBill.DeleteSendC");
					String sqlBillNo = CoreXmlSqlParsersFactory
							.getSql("CorePipeCarBill.DeleteBillNo");
					String sqlDele = CoreXmlSqlParsersFactory
							.getSql("CorePipeCarBill.DeleteTRANSIT1");
					String sqlDeleWl = CoreXmlSqlParsersFactory
							.getSql("CorePipeCarBill.deleteWlInfoByBillNo");
					if (billMEntity.getBillStatus().equals("0")) {
						ps = con.prepareStatement(sqlBillM);
						ps.setObject(1, billNo);
						ps.executeUpdate();
						ps.close();

						ps = con.prepareStatement(sqlMat);
						ps.setObject(1, userName);
						ps.setObject(2, billMEntity.getBillNo());
						ps.setObject(3, billMEntity.getOrdLnPk());
						ps.setObject(4, billMEntity.getOrdLnDlyPk());
						ps.executeUpdate();
						ps.close();

						ps = con.prepareStatement(sqlBillC);
						ps.setObject(1, billMEntity.getBillNo());
						ps.setObject(2, billMEntity.getOrdLnPk());
						ps.setObject(3, billMEntity.getOrdLnDlyPk());
						ps.executeUpdate();
						ps.close();

						ps = con.prepareStatement(sqlSendM);
						ps.setObject(1, billMEntity.getPlanWt());
						ps.setObject(2, billMEntity.getSendPlanNo());
						ps.executeUpdate();
						ps.close();

						ps = con.prepareStatement(sqlSendC);
						ps.setObject(1, billMEntity.getPlanWt());
						ps.setObject(2, billMEntity.getSendPlanNo());
						ps.setObject(3, billMEntity.getOrdLnDlyPk());
						ps.executeUpdate();
						ps.close();

						ps = con.prepareStatement(sqlBillNo);
						ps.setObject(1, billMEntity.getBillNo());
						ps.executeUpdate();
						ps.close();
						// 删除提单运输方式
						ps = con.prepareStatement(sqlDele);
						ps.setObject(1, billMEntity.getBillNo());
						ps.executeUpdate();
						ps.close();
						// 删除物流表信息
						/*ps = wlCon.prepareStatement(sqlDeleWl);
						ps.setObject(1, billMEntity.getBillNo());
						ps.executeUpdate();
						ps.close();*/

					} else {
						core.setV_errMsg("提单号:" + billMEntity.getBillNo()
								+ "不是提单编制状态,不能撤销提单!");
						return core;
					}

				}

			}
			core.setV_errMsg("提单撤销成功!");
			con.commit();
			//wlCon.commit();

		} catch (SQLException e) {
			try {
				con.rollback();
				//wlCon.rollback();
			} catch (SQLException e1) {
				e1.printStackTrace();
			}
			e.printStackTrace();
		} finally {
			if (con != null) {
				try {
					con.close();
				} catch (SQLException e) {
					e.printStackTrace();
				}
			}
			/*if (wlCon != null) {
				try {
					wlCon.close();
				} catch (SQLException e) {
					e.printStackTrace();
				}
			}*/
		}
		return core;
	}

	/**
	 * 初始化提单
	 * 
	 * @param sendPlanNo
	 * @return
	 */
	public CoreReturnObject getBillNoBySendPlanNo(String sendPlanNo, String type) {
		Object[] params = new Object[] { sendPlanNo, type };
		return this.getDataBySqlID("CorePipeCarBill.getBillNoBySendPlanNo",
				params);
	}

	/**
	 * 根据提单号验证计划号
	 * 
	 * @param billNo
	 *            提单号
	 * @return CoreReturnObject
	 */
	public CoreReturnObject getSendPlanNoByBillNo(String billNo) {
		Object[] params = new Object[] { billNo };
		return this.getDataBySqlID("CorePipeCarBill.getSendPlanNoByBillNo",
				params);
	}

	/**
	 * 查询交货行量
	 * 
	 * @param
	 *
	 * @return CoreReturnObject
	 */
	public CoreReturnObject queryOrdLnDlyPkWT(String ordLnDlyPk) {
		Object[] params = new Object[] { ordLnDlyPk };
		return this.getDataBySqlID("CorePipeCarBill.queryOrdLnDlyPkWT", params);
	}

	/**
	 * 查询该合同提单量
	 * 
	 * @param
	 *
	 * @return CoreReturnObject
	 */
	public CoreReturnObject queryBillWeight(String ordLnDlyPk) {
		Object[] params = new Object[] { ordLnDlyPk };
		return this.getDataBySqlID("CorePipeCarBill.queryBillWeight", params);
	}

	/**
	 * 查询计划信息
	 * 
	 * @param con
	 * @param sendPlanNo
	 * @return
	 * @throws SQLException
	 */
	public static YdmZcSendplanMEntity getSendPlanInfo(Connection con,
			String sendPlanNo) throws SQLException {
		String sql = CoreXmlSqlParsersFactory
				.getSql("CorePipeCarBill.getsendPlanInfoBySendPlanNo");
		PreparedStatement ps = con.prepareStatement(sql);
		ps.setObject(1, sendPlanNo);
		ResultSet rs = ps.executeQuery();
		List<YdmZcSendplanMEntity> sendPlanMEntitys = EntityTool
				.getEntitysByRs(rs, YdmZcSendplanMEntity.class);
		rs.close();
		ps.close();
		if (sendPlanMEntitys == null || sendPlanMEntitys.size() == 0) {
			return null;
		} else {
			for (YdmZcSendplanMEntity ydmZcSendplanMEntity : sendPlanMEntitys) {
				QualityDesignTool.nullConverNullString(ydmZcSendplanMEntity);
			}
			return sendPlanMEntitys.get(0);
		}
	}

	/**
	 * 查询计划从表信息
	 * 
	 * @param con
	 * @param sendPlanNo
	 * @return
	 * @throws SQLException
	 */
	public static YdmZcSendplanCEntity getSendPlanCInfo(Connection con,
			String sendPlanNo, String ordPk, String ordLnPk, String ordLnDlyPk)
			throws SQLException {
		String sql = CoreXmlSqlParsersFactory
				.getSql("CorePipeCarBill.getPlanCInfoByordLnPkAndSPNo");
		PreparedStatement ps = con.prepareStatement(sql);
		ps.setObject(1, sendPlanNo);
		ps.setObject(2, ordPk);
		ps.setObject(3, ordLnPk);
		ps.setObject(4, ordLnDlyPk);
		ResultSet rs = ps.executeQuery();
		List<YdmZcSendplanCEntity> sendPlanCEntitys = EntityTool
				.getEntitysByRs(rs, YdmZcSendplanCEntity.class);
		rs.close();
		ps.close();
		if (sendPlanCEntitys == null || sendPlanCEntitys.size() == 0) {
			return null;
		} else {
			for (YdmZcSendplanCEntity sendPlanCEntity : sendPlanCEntitys) {
				QualityDesignTool.nullConverNullString(sendPlanCEntity);
			}
			return sendPlanCEntitys.get(0);
		}
	}

	/**
	 * 生成提单号
	 * 
	 * @param con
	 * @param salg
	 * @return
	 * @throws SQLException
	 */
	public static String createBillNo(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("CorePipeCarBill.createBillNo");
		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 con
	 * @param salg
	 * @return
	 * @throws SQLException
	 */
	public static String insertBillNo(Connection con, String salg)
			throws SQLException {
		String billNo = "";
		String flag = "";
		if (salg.equals("100101") || salg.equals("100105")) {
			flag = "X";
		} else if (salg.equals("100102")) {
			flag = "M";
		} else if (salg.equals("100103")) {
			flag = "G";
		} else {
			return "S";
		}
		String sql = CoreXmlSqlParsersFactory.getSql(
				"CorePipeCarBill.createBillNo")
				.replace("#BILLNO#", "TH" + 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();

		// String sqlInsertBill =
		// "INSERT INTO YDM_ZC_BILL_NO (BILL_NO) values ('"+billNo+"')";
		// Statement sta1 = con.createStatement();
		// sta1.executeUpdate(sqlInsertBill);
		// sta1.close();
		// con.commit();
		return billNo;
	}

	/**
	 * 验证提单编号
	 * 
	 * @param con
	 *            连接字符串
	 * @param billNo
	 *            提单编号
	 * @return String
	 * @throws SQLException
	 */
	public static String[] checkBillStatus(Connection con, String billNo)
			throws SQLException {
		String[] arrStr = null;
		String sql = CoreXmlSqlParsersFactory
				.getSql("CorePipeCarBill.checkBillStatus");
		PreparedStatement ps = con.prepareStatement(sql);
		ps.setObject(1, billNo);
		ResultSet rs = ps.executeQuery();
		if (rs.next()) {
			arrStr = new String[6];
			arrStr[0] = rs.getString("BILL_STATUS");
			arrStr[1] = rs.getString("SEND_PLAN_NO");
			arrStr[2] = rs.getString("VSTEL_CODE");
			arrStr[3] = rs.getString("ZBS_SOURCE");
			arrStr[4] = rs.getString("FLAG_LEN");
			arrStr[5] = rs.getString("CREATE_NAME");
		}
		rs.close();
		ps.close();
		return arrStr;
	}

	/**
	 * 验证新增或者修改提单主表
	 * 
	 * @param con
	 * @param billNo
	 * @param sendPlanNo
	 * @param
	 * @param ordPk
	 * @param ordLnPk
	 * @param ordLnDlyPk
	 * @return
	 * @throws SQLException
	 */
	public static boolean checkIsCommon(Connection con, String billNo,
			String sendPlanNo, String ordPk, String ordLnPk, String ordLnDlyPk)
			throws SQLException {
		boolean flag = true;
		String sql = CoreXmlSqlParsersFactory
				.getSql("CorePipeCarBill.checkIsCommon");
		PreparedStatement ps = con.prepareStatement(sql);
		ps.setObject(1, billNo);
		ps.setObject(2, sendPlanNo);
		ps.setObject(3, ordPk);
		ps.setObject(4, ordLnPk);
		ps.setObject(5, ordLnDlyPk);
		ResultSet rs = ps.executeQuery();
		int count = 0;
		if (rs.next()) {
			count = rs.getInt("COUNT");
		}
		rs.close();
		ps.close();
		if (count != 0) {
			flag = false;
		}
		return flag;
	}

	/**
	 * 查询合同已编制量
	 * 
	 * @param con
	 *            连接字符串
	 * @param sendPlanNo
	 *            计划号
	 * @param ordPk
	 *            合同主键
	 * @param ordLnPk
	 *            合同行主键
	 * @param ordLnDlyPk
	 *            交货行主键
	 * @return double
	 * @throws SQLException
	 */
	public static double getBillVolume(Connection con, String sendPlanNo,
			String ordPk, String ordLnPk, String ordLnDlyPk)
			throws SQLException {
		double volumeWgt = 0;
		String sql = CoreXmlSqlParsersFactory
				.getSql("CorePipeCarBill.getBillWgtBySendNo");
		PreparedStatement ps = con.prepareStatement(sql);
		ps.setObject(1, sendPlanNo);
		ps.setObject(2, ordPk);
		ps.setObject(3, ordLnPk);
		ps.setObject(4, ordLnDlyPk);
		ResultSet rs = ps.executeQuery();
		if (rs.next()) {
			volumeWgt = rs.getDouble("SUM_PLAN_WT");
		}
		rs.close();
		ps.close();
		return volumeWgt;
	}

	/**
	 * 新增提单
	 * 
	 * @param param
	 *            参数集合
	 * @param
	 *
	 * @return CoreReturnObject
	 */
	public CoreReturnObject DoAdd(ArrayList<String> param,
			ArrayList<MatInfoEntity> matInfoEntitys, String strPraFlag,
			String orderNo, String ordersSeqNo, String delvNo,String supplyUnit, String supplyUnitDesc,String memo,String custom) {
		CoreReturnObject core = new CoreReturnObject();
		String billNo = param.get(0);
		String sendPlanNo = param.get(1);
		String ordPk = param.get(2);
		String ordLnPk = param.get(3);
		String ordLnDlyPk = param.get(4);
		String virtualFlag = param.get(5);
		String userName = param.get(6);
		// String department = param.get(7);
		// String deptid = param.get(8);
		String beginTime = param.get(9);
		String endTime = param.get(10);
		String credNo = param.get(11);
		String pordNo = param.get(12);
		String pordName = param.get(13);
		String singleNo = param.get(14);//订单号
		String itemNo = param.get(15);//项目号
		String licenseNo = "";// param.get(16);
		String flagLen = param.get(17);
		String overstock = param.get(18); // 是否压库
		Date nowTime = new Date();
		Gson gson =  new Gson();
		SimpleDateFormat dfT = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		String strNowTime = dfT.format(nowTime);
		Connection con = null;
		PreparedStatement ps = null;
		//Statement wlStmt = null;
		//Connection wlCon = null;
		try {
			con = this.getDao(CoreConstantData.sysbeanstr).getConnection();
			con.setAutoCommit(false);
			//wlCon = new DBCommon().getDao("TmsDao").getConnection();注释生产物流  2024/4/24 肖沅累
			//wlCon.setAutoCommit(false);
			//wlStmt = wlCon.createStatement();
			// 验证计划信息
			YdmZcSendplanMEntity sendPlanMEntity = CorePipeCarBill
					.getSendPlanInfo(con, sendPlanNo);
			if (sendPlanMEntity == null) {
				core.setV_errMsg("计划" + sendPlanNo + "信息已改变,请重新获取数据!");
				return core;
			}
			if (!(sendPlanMEntity.getSendPlanStatus().equals("1") || sendPlanMEntity
					.getSendPlanStatus().equals("2"))) {
				core.setV_errMsg("发运计划号:" + sendPlanNo + "不是执行/下发状态,不能生成提单!");
				return core;
			}
			// 生成修改或者新增提单号标识
			boolean flag = false;
			String zbsSource = "";
			String flagLen1= "";
			if (!billNo.equals("")) {
				String[] billInfo = CorePipeCarBill
						.checkBillStatus(con, billNo);
				if (billInfo == null) {
					core.setV_errMsg("提单号发生变化,请重新获取数据!");
					return core;
				}
				String billStatus = billInfo[0];
				String beSendPlanNo = billInfo[1];
				zbsSource = billInfo[3];
				flagLen1=billInfo[4];
				userName=billInfo[5];
				// String storage = billInfo[2];
				if (!(billStatus.equals("0"))) {
					core.setV_errMsg("提单号:" + billNo + "不是编制,不能追加提单信息!");
					return core;
				}
				if (!beSendPlanNo.equals(sendPlanNo)) {
					core.setV_errMsg("不能在计划号:" + beSendPlanNo + "的提单号:"
							+ billNo + "下追加计划号:" + sendPlanNo + "信息!");
					return core;
				}
				// 如果是同一个计划号同一个交货行号向下就修改提单
				flag = CorePipeCarBill.checkIsCommon(con, billNo, sendPlanNo,
						ordPk, ordLnPk, ordLnDlyPk);
			} else {
				// 生成提单号
				flag = true;
				billNo = CorePipeCarBill.insertBillNo(con,
						sendPlanMEntity.getSaleOrg());
				String sqlInsertBill = "INSERT INTO YDM_ZC_BILL_NO (BILL_NO) values ('"
						+ billNo + "')";
				Statement sta1 = con.createStatement();
				sta1.executeUpdate(sqlInsertBill);
				sta1.close();
				// con.commit();
			}

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

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

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

			String sqlAddBillM = CoreXmlSqlParsersFactory
					.getSql("CorePipeCarBill.addBillMInfo");
			String sqlupdateBillM = CoreXmlSqlParsersFactory
					.getSql("CorePipeCarBill.updateBillByBillNo");
			String sqlupdateSendM = CoreXmlSqlParsersFactory
					.getSql("CorePipeCarBill.updateSendM");
			PreparedStatement psSendM = con.prepareStatement(sqlupdateSendM);
			String sqlupdateSendC = CoreXmlSqlParsersFactory
					.getSql("CorePipeCarBill.updateSendC");
			PreparedStatement psSendC = con.prepareStatement(sqlupdateSendC);
			// 插入到运输表
			String sqlupdateDeryL = CoreXmlSqlParsersFactory
					.getSql("CorePipeCarBill.InsertDeryL");
			PreparedStatement psDeryL = con.prepareStatement(sqlupdateDeryL);
			// Statement st=con.createStatement();
			String zbsFlag = "";
			String jgZbsId = "";
			double sumWgtCan = 0;
			int sumCount = 0;
			double sumLen = 0;
			
			for (MatInfoEntity matInfoEntity : matInfoEntitys) {
                System.out.println("库存信息===》"+ gson.toJson(matInfoEntity));
				List<MatZcMInfoEntity1> matZcMInfos = null;
				String storageMaseg = queryOrderLinDer(con, ordLnDlyPk,
						matInfoEntity.getStorageNo(),
						matInfoEntity.getStorageName());
				if (!storageMaseg.equals("")) {
					core.setV_errMsg(storageMaseg);
					return core;
				}
				// 分炉标识
				String prtFlag = strPraFlag;

				if(matInfoEntity.getMatNo() !=null)
				{
					if(!matInfoEntity.getMatNo().equals(""))
					{
						System.out.println("matno====>"+ gson.toJson(matInfoEntity.getMatNo()));
						String[] matNoList = matInfoEntity.getMatNo().split(",");
						matZcMInfos = CorePipeCarBill.getMatZc(con, matInfoEntity, matNoList,custom);
					}else
					{
						// 可编辑信息
						matZcMInfos = CorePipeCarBill
								.getInventoryCanInfo(con, matInfoEntity,custom);
					}
				}
				else
				{
					// 可编辑信息
					matZcMInfos = CorePipeCarBill
							.getInventoryCanInfo(con, matInfoEntity,custom);
				}


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

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

					if (gllockFlag.equals("1")) {
						core.setV_errMsg("已管理封锁,不能生成提单！");
						return core;
					}
					if (zllockFlag.equals("1")) {
						core.setV_errMsg("已质量封锁,不能生成提单！");
						return core;
					}

                        // 修改库存状态
						psMat.setObject(1, matZcMInfoEntity.getMatNo());
						psMat.addBatch();
						// // 修改长度
						// psMatlen.setObject(1, String.valueOf(len.get(w)));
						// psMatlen.setObject(2, matZcMInfoEntity.getMatNo());
						// psMatlen.addBatch();

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

						ps1.setObject(53, matInfoEntity.getBoundNum());
						ps1.setObject(54, matZcMInfoEntity.getFinishproMold());
						ps1.setObject(55, matZcMInfoEntity.getInMold());
						ps1.setObject(56, matZcMInfoEntity.getZbsId());
						ps1.setObject(57, matZcMInfoEntity.getJgZbsId());
						ps1.setObject(58, memo);
						ps1.setObject(59, matZcMInfoEntity.getInType());
//						ps1.addBatch();
						ps1.executeUpdate();
						sumWgtCan +=Double.parseDouble(matZcMInfoEntity.getActWeight());
						sumCount +=Integer.parseInt(matZcMInfoEntity.getActCount());
						if(!"".equals(matZcMInfoEntity.getActLen()))
							sumLen +=Double.parseDouble(matZcMInfoEntity.getActLen());
						w++;
                    }
			}
			// psMatlen.executeBatch();
			// 查询库存可编制量
/*			double sumWgtCan = 0;
			int sumCount = 0;
			double sumLen = 0;
			for (MatInfoEntity matInfoEntity : matInfoEntitys) {
				double[] amount = CorePipeCarBill.getInventoryCanWgt(con,
						matInfoEntity);
				int amountCount = (int) amount[0];
				double amountWgt = amount[1];
				double amountLen = amount[2];
				if (amountCount != Integer
						.parseInt(matInfoEntity.getActCount())
						) {
					core.setV_errMsg("库存炉号:" + matInfoEntity.getJudgeStoveNo()
							+ ",批号:" + matInfoEntity.getBatchNo() + ",组号:"
							+ matInfoEntity.getBatchGroudNo()
							+ ",可编制量信息发生改变,请重新获取数据!");
					return core;
				}
				sumWgtCan += amountWgt;
				sumCount += amountCount;
				sumLen += amountLen;
			}*/
			// 新增提单主表信息
			if (flag) {

				YdmZcBillMEntity billMEntity = new YdmZcBillMEntity();
				BeanUtils.copyProperties(billMEntity, sendPlanMEntity);
				// 查询提单主表最大序号
				int indexMaxM = CorePipeCarBill.getBillMMaxSeq(con, billNo);
				String billNoSeq = billNo + df.format(indexMaxM);
				BillOrderInfoEntity orderLineEntity = CorePipeCarBill
						.getBillOrderInfo(con, ordLnDlyPk);
				BeanUtils.copyProperties(billMEntity, planCEntity);
				billMEntity.setStdCode(orderLineEntity.getStdCode());
				billMEntity.setStdName(orderLineEntity.getStdName());
				billMEntity.setSteelcode(orderLineEntity.getSteelcode());
				billMEntity.setSteelname(orderLineEntity.getSteelname());
				billMEntity.setStlmtUnitNo(orderLineEntity.getStlmtUnitNo());
				billMEntity.setStlmtUnitNm(orderLineEntity.getStlmtUnitNm());
				billMEntity.setReceivAddr(orderLineEntity.getReceivAddr());
				billMEntity.setDelvryAddr(orderLineEntity.getDelvryAddr());
				billMEntity
						.setFstDraweeUnit(orderLineEntity.getFstDraweeUnit());
				billMEntity.setAgencyFl(orderLineEntity.getAgencyFl());
				billMEntity.setPickupFlag(orderLineEntity.getPickupFlag());
				billMEntity.setPlanTransType("110501");
				billMEntity.setBillNo(billNo);
				billMEntity.setBillNoSeq(billNoSeq);
				billMEntity.setPlanNum(String.valueOf(sumCount));
				billMEntity.setPlanWt(String.valueOf(sumWgtCan));
				billMEntity.setPlanTotlLen(String.valueOf(sumLen));
				billMEntity.setCreateName(userName);
				billMEntity.setBillStatus("0");
				billMEntity.setLoadvehicleType(virtualFlag);
				billMEntity.setActStartTime(beginTime);
				billMEntity.setActEndTime(endTime);
				//billMEntity.setMemo(memo);
				billMEntity.setCreateTime(strNowTime);
				billMEntity.setPortNo(pordNo);
				billMEntity.setPortDesc(pordName);
				billMEntity.setCreditNo(credNo);
				billMEntity.setExtraFlag(orderLineEntity.getExtraFlag());
				billMEntity.setItemNo(itemNo);
				billMEntity.setSingleNo(singleNo);
				billMEntity.setLicenseNo(licenseNo);
				billMEntity.setSupplyUnit(supplyUnit);
				billMEntity.setSupplyUnitDesc(supplyUnitDesc);
				billMEntity.setOverstock(overstock);
				String sqlAddBillMStr = EntityTool.fillSqlByEntity(sqlAddBillM,
						billMEntity);
				ps = con.prepareStatement(sqlAddBillMStr);
				ps.setObject(1, zbsFlag);
				ps.setObject(2, flagLen);
				ps.executeUpdate();
				ps.close();

				psDeryL.setObject(1, userName);
				psDeryL.setObject(2, strNowTime);
				psDeryL.setObject(3, billNo);
				psDeryL.setObject(4, ordLnDlyPk);
				psDeryL.addBatch();
				System.out.println("提单主信息===》"+ gson.toJson(billMEntity));
		        
			} else {
				if (!zbsFlag.equals(zbsSource)) {
					core.setV_errMsg("选择炉号与追加提单质保书来源不一致,不能追加!");
					return core;
				}
//				if (!flagLen.equals(flagLen1)) {
//					core.setV_errMsg("选择炉号与追加提单质保书是否取长度不一致,不能追加!");
//					return core;
//				}
//				if (!flagLen.equals(flagLen1)) {
//					core.setV_errMsg("选择炉号与追加提单接箍质保书Id不一致,不能追加!");
//					return core;
//				}
				// 修改提单主表信息
				ps = con.prepareStatement(sqlupdateBillM);
				ps.setObject(1, String.valueOf(sumCount));
				ps.setObject(2, String.valueOf(sumWgtCan));
				ps.setObject(3, String.valueOf(sumLen));
				ps.setObject(4, billNo);
				ps.setObject(5, sendPlanNo);
				ps.setObject(6, ordLnDlyPk);
				ps.executeUpdate();
				ps.close();

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

			}
//			psMat.executeBatch();
//			psMat.close();
			// 2021/6/24 因提单材料序号可能重复，修改不走批量
//			ps1.executeBatch();
			ps1.close();
			if(flag){
				//删除物流表数据
				/*String sqlDelete = CoreXmlSqlParsersFactory
						.getSql("CorePipeCarBill.deleteWlInfoByBillNo");//
				PreparedStatement patDelete = wlCon
						.prepareStatement(sqlDelete);
				patDelete.setObject(1, billNo);
				patDelete.addBatch();
				patDelete.executeBatch();*/
				
				//新增物流表信息
				
		        
				String sql2 = CoreXmlSqlParsersFactory
						.getSql("CorePipeCarBill.QueryBillCByBillNo");
				PreparedStatement ps2 = con.prepareStatement(sql2);
				ps2.setObject(1, billNo);
				ResultSet rs2 = ps2.executeQuery();
				List<YdmZcBillCEntity> billCEntitys = EntityTool
							.getEntitysByRs(rs2, YdmZcBillCEntity.class);
		        
		        for(YdmZcBillCEntity billC:billCEntitys){
		        	/*		        	//获取对应物料编码
			        String outWlCode="";
			        String outWlName="";
			        sql2 = CoreXmlSqlParsersFactory
			                .getSql("CorePipeCarBill.getWlInfo");
					ps2 = con.prepareStatement(sql2);
			        ps2.setObject(1,billC.getMatNo());
			        rs2 = ps2.executeQuery();
			        if (rs2.next()) {
			        	outWlCode=rs2.getString("wlCode");
			        	outWlName=rs2.getString("wlName");
			        }
			        else{
			        	sql2 = CoreXmlSqlParsersFactory
				                .getSql("CorePipeCarBill.getWlInfoFromBackup");
						ps2 = con.prepareStatement(sql2);
				        ps2.setObject(1,billC.getMatNo());
				        rs2 = ps2.executeQuery();
				        if (rs2.next()) {
				        	outWlCode=rs2.getString("wlCode");
				        	outWlName=rs2.getString("wlName");
				        }
			        }
			        rs2.close();
			        ps2.close();*/
		        	//获取最大流水号顺序号
					String demandNo="";
					/*sql2 = CoreXmlSqlParsersFactory
			                .getSql("CorePipeCarBill.getMaxSeq");
			        ps2 = wlCon.prepareStatement(sql2);
			        SimpleDateFormat dateFormat = new SimpleDateFormat("yyyyMMdd");
			        String a="XS"+dateFormat.format(new Date());
			        ps2.setObject(1, "XS"+dateFormat.format(new Date()));
			        rs2 = ps2.executeQuery();
			        if (rs2.next()) {
			        	demandNo=rs2.getString("DEMAND_NO");
			            if(demandNo==null)
			            	demandNo="XS"+dateFormat.format(new Date())+"00001";
			            else
			            	demandNo="XS"+dateFormat.format(new Date())+String.format("%05d",(Integer.parseInt(demandNo.substring(10))+1));
			        }else{
			        	demandNo="XS"+dateFormat.format(new Date())+"00001";
			        }
			        rs2.close();
			        ps2.close();*/
		        
		        	/*String sqlAddBcOutList = CoreXmlSqlParsersFactory
							.getSql("CorePipeCarBill.InsertWlInfo");
					PreparedStatement patAddBcOutList = wlCon
							.prepareStatement(sqlAddBcOutList);
					patAddBcOutList.setObject(1, demandNo);
					patAddBcOutList.setObject(2, billC.getPlineName()+"汽运需求");
					patAddBcOutList.setObject(3, "L1");
					patAddBcOutList.setObject(4, "*");
					patAddBcOutList.setObject(5, "XS");
					patAddBcOutList.setObject(6, "销售运输");
					patAddBcOutList.setObject(7, billC.getBillNo()+"/"+billC.getOrdLnDlyPk());
					patAddBcOutList.setObject(8, billC.getVstel());
					patAddBcOutList.setObject(9, billC.getVstelDesc());
					patAddBcOutList.setObject(10, billC.getVstel());
					patAddBcOutList.setObject(11, billC.getVstelDesc());
					patAddBcOutList.setObject(12, "SC");
					patAddBcOutList.setObject(13, "生产用车");
					patAddBcOutList.setObject(14, "CAR01");
					patAddBcOutList.setObject(15, "拖车");
					patAddBcOutList.setObject(16, "75.000");
					patAddBcOutList.setObject(17, "0");
					patAddBcOutList.setObject(18, billC.getJudgeStoveNo());
					patAddBcOutList.setObject(19, billC.getBatchNo());
					patAddBcOutList.setObject(20, "");
					patAddBcOutList.setObject(21, billC.getMaterialNo());
					patAddBcOutList.setObject(22, billC.getMaterialName());
					patAddBcOutList.setObject(23, billC.getSpecName());
					patAddBcOutList.setObject(24, billC.getActDimater());
					patAddBcOutList.setObject(25, billC.getActHeight());
					patAddBcOutList.setObject(26, billC.getActLen());
					patAddBcOutList.setObject(27, billC.getActLenMin());
					patAddBcOutList.setObject(28, billC.getActLenMax());
					patAddBcOutList.setObject(29, billC.getActCount());
					patAddBcOutList.setObject(30, billC.getActWeight());
					patAddBcOutList.setObject(31, "");
					patAddBcOutList.setObject(32, "");
					patAddBcOutList.setObject(33, "");
					patAddBcOutList.setObject(34, "");
					patAddBcOutList.setObject(35, "");
					patAddBcOutList.setObject(36, "");
					patAddBcOutList.setObject(37, "1");
					patAddBcOutList.setObject(38, "20");
					patAddBcOutList.setObject(39, "20");
					patAddBcOutList.setObject(40, "0");
					patAddBcOutList.setObject(41, userName);
					patAddBcOutList.addBatch();
					patAddBcOutList.executeBatch();*/
				}
			}
			psSendM.setObject(1, String.valueOf(sumWgtCan));
			psSendM.setObject(2, userName);
			psSendM.setObject(3, strNowTime);
			psSendM.setObject(4, sendPlanNo);
			psSendM.addBatch();
			psSendC.setObject(1, String.valueOf(sumWgtCan));
			psSendC.setObject(2, userName);
			psSendC.setObject(3, strNowTime);
			psSendC.setObject(4, sendPlanNo);
			psSendC.setObject(5, ordLnDlyPk);
			psSendC.addBatch();
			
			//验证
			String	sql = CoreXmlSqlParsersFactory
					.getSql("CorePipeCarBill.verifiNum");
			PreparedStatement ps3 = con.prepareStatement(sql);
			ps3.setObject(1, billNo);
			ResultSet rs = ps3.executeQuery();
			String count="";
			if (rs.next()) {
				count = rs.getString("num");
			}
			rs.close();
			ps3.close();

			if (Integer.parseInt(count)>10) {
				core.setV_errMsg("同一提单同一合同行下对应炉批号超过10个,不能生成提单！");
				return core;
			}
			
			
			if (flag) {
				core.setV_errMsg("新增提单成功!");
			} else {
				core.setV_errMsg("修改提单成功!");
			}
			psSendM.executeBatch();
			psSendC.executeBatch();
			psDeryL.executeBatch();
			con.commit();
			//wlCon.commit();
		} catch (Exception e) {
			core.setV_errMsg(e.getMessage());
			e.printStackTrace();
			try {
				con.rollback();
				//wlCon.rollback();
			} catch (SQLException e1) {
				e1.printStackTrace();
			}
		} finally {
			if (con != null) {
				try {
					con.close();
				} catch (SQLException e1) {
					e1.printStackTrace();
				}
			}
			/*if (wlCon != null) {
				try {
					wlCon.close();
				} catch (SQLException e1) {
					e1.printStackTrace();
				}
			}*/
		}
		return core;
	}

	/**
	 * 验证是否分炉
	 * 
	 * @param con
	 * @param justStoveNo
	 * @param batchNo
	 * @param actCount
	 * @return
	 * @throws SQLException
	 */
	public static String checkIsPrtByJustNo(Connection con, String justStoveNo,
			String batchNo, String groupNo, String actCount, String prtFlag)
			throws SQLException {
		String prt = "0";
		String sql = CoreXmlSqlParsersFactory
				.getSql("CorePipeCarBill.checkIsPrtByJustNo");
		PreparedStatement ps = con.prepareStatement(sql);
		ps.setObject(1, justStoveNo);
		ps.setObject(2, batchNo);
		ps.setObject(3, groupNo);
		ResultSet rs = ps.executeQuery();
		int count = 0;
		if (rs.next()) {
			count = rs.getInt("COUNT");
		}
		rs.close();
		ps.close();
		if (prtFlag.equals("110501")) {
			if (Integer.parseInt(actCount) != count) {
				prt = "1";
			}
		}
		return prt;
	}

	/**
	 * 获取交货行交货地点
	 * 
	 * @param con
	 * @param ordLnDvPk
	 * @return
	 * @throws SQLException
	 */
	public static String queryOrderLinDer(Connection con, String ordLnDvPk,
			String storage, String storageName) throws SQLException {
		String prt = "";
		String sql = CoreXmlSqlParsersFactory
				.getSql("CorePipeCarBill.queryOrderLinDer");
		PreparedStatement ps = con.prepareStatement(sql);
		ps.setObject(1, ordLnDvPk);
		ResultSet rs = ps.executeQuery();
		String addr = "";
		if (rs.next()) {
			addr = rs.getString("DELVRY_ADDR");
		}
		rs.close();
		ps.close();
		// if (addr.equals("客户库-腾飞") && !storage.equals("LTF1")) {
		// prt = "您选择的该合同交货地点为：" + addr + "、与选择的炉所在出库地点：" + storageName
		// + "，不一致,不能进行此操作！";
		// }
		// if (addr.equals("客户库-宝仓"))
		// if (!storage.equals("LBC2") && !storage.equals("LBC1")) {
		// prt = "您选择的该合同交货地点为：" + addr + "、与选择的炉所在出库地点：" + storageName
		// + "，不一致,不能进行此操作！";
		// }
		// if (addr.equals("客户库-安利"))
		// if (!storage.equals("LAL1") && !storage.equals("LAL2")
		// && !storage.equals("LAL3")) {
		// prt = "您选择的该合同交货地点为：" + addr + "、与选择的炉所在出库地点：" + storageName
		// + "，不一致,不能进行此操作！";
		// }
		// if (addr.equals("客户库-润德")) {
		// if (!storage.equals("LZD1") && !storage.equals("LZT1")) {
		// prt = "您选择的该合同交货地点为：" + addr + "、与选择的炉所在出库地点：" + storageName
		// + "，不一致,不能进行此操作！";
		// }
		// }
		return prt;
	}

	/**
	 * 查询库存可编制资源信息
	 * 
	 * @param con
	 *            连接字符串
	 * @param matInfoEntity
	 *            资源类
	 * @return List<MatZcMInfoEntity>
	 * @throws SQLException
	 */
	public static List<MatZcMInfoEntity1> getInventoryCanInfo(Connection con,
			MatInfoEntity matInfoEntity,String custom) throws SQLException {
		String sql = "";
//		if(custom.equals("1_120504"))
//		{
			sql  = CoreXmlSqlParsersFactory.getSql("CorePipeCarbill.getInventoryCanInfoTH");
//		}else
//		{
//			sql = CoreXmlSqlParsersFactory.getSql("CorePipeCarbill.getInventoryCanInfo");
//		}
		sql = EntityTool.fillSqlByEntity(sql, matInfoEntity);
		PreparedStatement ps = con.prepareStatement(sql);
		ps.setObject(1, Integer.parseInt(matInfoEntity.getActCount()) + 1);
		ResultSet rs = ps.executeQuery();
		List<MatZcMInfoEntity1> matZcMInfoEntitys = EntityTool.getEntitysByRs(
				rs, MatZcMInfoEntity1.class);
		ps.close();
		rs.close();
		if (matZcMInfoEntitys == null || matZcMInfoEntitys.size() == 0) {
			return null;
		} else {
			for (MatZcMInfoEntity1 matZcMInfoEntity : matZcMInfoEntitys) {
				QualityDesignTool.nullConverNullString(matZcMInfoEntity);
			}
			return matZcMInfoEntitys;
		}

	}

	/**
	 * 查询库存可编制资源信息
	 *
	 * @param con
	 *            连接字符串
	 * @param matInfoEntity
	 *            资源类
	 * @return List<MatZcMInfoEntity>
	 * @throws SQLException
	 */
	public static List<MatZcMInfoEntity1> getMatZc(Connection con, MatInfoEntity matInfoEntity,String[] matNoList,String custom) throws SQLException {
		String sql = "";
//		if(!custom.equals("1_120504"))
//		{
//			sql = CoreXmlSqlParsersFactory
//					.getSql("CorePipeCarbill.getMatZc");
//		}else
//		{
			sql = CoreXmlSqlParsersFactory
					.getSql("CorePipeCarbill.getMatZcTH");
		//}
		sql = EntityTool.fillSqlByEntity(sql, matInfoEntity);
		sql +=CoreConverter.toSqlConditionString("T.MAT_NO",matNoList);
		PreparedStatement ps = con.prepareStatement(sql);
		ResultSet rs = ps.executeQuery();
		List<MatZcMInfoEntity1> matZcMInfoEntitys = EntityTool.getEntitysByRs(
				rs, MatZcMInfoEntity1.class);
		ps.close();
		rs.close();
		if (matZcMInfoEntitys == null || matZcMInfoEntitys.size() == 0) {
			return null;
		} else {
			for (MatZcMInfoEntity1 matZcMInfoEntity : matZcMInfoEntitys) {
				QualityDesignTool.nullConverNullString(matZcMInfoEntity);
			}
			return matZcMInfoEntitys;
		}

	}
	/**
	 * 查询提单从表最大序号
	 * 
	 * @param con
	 * @param matNo
	 * @return
	 * @throws SQLException
	 */
	public static String getBillCMaxSeq(Connection con, String matNo)
			throws SQLException {
		String index = "";
		String sql = CoreXmlSqlParsersFactory
				.getSql("CorePipeCarbill.getBillCMaxSeq");
		PreparedStatement ps = con.prepareStatement(sql);
		ps.setObject(1, matNo);
		ps.setObject(2, matNo);
		ResultSet rs = ps.executeQuery();
		if (rs.next()) {
			index = rs.getString("BILL_SQE");
		}
		rs.close();
		ps.close();
		return index;
	}

	/**
	 * 查询提单主表最大序号
	 * 
	 * @param con
	 * @param
	 * @return
	 * @throws SQLException
	 */
	public static int getBillMMaxSeq(Connection con, String billNo)
			throws SQLException {
		int index = 0;
		String sql = CoreXmlSqlParsersFactory
				.getSql("CorePipeCarbill.getBillMMaxSeq");
		PreparedStatement ps = con.prepareStatement(sql);
		ps.setObject(1, billNo);
		ResultSet rs = ps.executeQuery();
		if (rs.next()) {
			index = rs.getInt("INDEX_SEQ");
		}
		rs.close();
		ps.close();
		return index;
	}

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

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

		return core;
	}

	/**
	 * 验证重量
	 * 
	 * @param matInfoEntity
	 * @return
	 */
	public CoreReturnObject checkMatWeight(MatInfoEntity matInfoEntity) {
		String sql = CoreXmlSqlParsersFactory
				.getSql("CorePipeCarBill.checkMatWeight");
		sql = EntityTool.fillSqlByEntity(sql, matInfoEntity);
		Object[] param = new Object[] { Integer.parseInt(matInfoEntity
				.getActCount()) + 1 };
		return this.getData(sql, param);
	}

	/**
	 * 查询合同信息
	 * 
	 * @param con
	 * @param
	 * @return
	 * @throws SQLException
	 */
	public static BillOrderInfoEntity getBillOrderInfo(Connection con,
			String ordLnDlyPk) throws SQLException {
		// 查询合同信息
		String sql = CoreXmlSqlParsersFactory
				.getSql("CorePipeCarBill.getOrdNoInfoByOrdLnPk");
		PreparedStatement ps = con.prepareStatement(sql);
		ps.setObject(1, ordLnDlyPk);
		ResultSet rs = ps.executeQuery();
		List<BillOrderInfoEntity> orderLineEntitys = EntityTool.getEntitysByRs(
				rs, BillOrderInfoEntity.class);
		rs.close();
		ps.close();
		if (orderLineEntitys == null || orderLineEntitys.size() == 0) {
			return null;
		} else {
			for (BillOrderInfoEntity billOrderInfoEntity : orderLineEntitys) {
				QualityDesignTool.nullConverNullString(billOrderInfoEntity);
			}
			return orderLineEntitys.get(0);
		}
	}

	/**
	 * 撤销下发
	 * 
	 * @return
	 */
	public CoreReturnObject cancelDown(ArrayList<String[]> billNoArrs,
			String userName) {
		CoreReturnObject core = new CoreReturnObject();
		Connection con = null;
		PreparedStatement ps = null;
		ResultSet rs = null;
		String sql = "";
		con = this.getDao(CoreConstantData.sysbeanstr).getConnection();
		try {
			con.setAutoCommit(false);
			sql = CoreXmlSqlParsersFactory
					.getSql("CorePipeCarBill.checkDoDown");
			String sqlU = CoreXmlSqlParsersFactory
					.getSql("CorePipeCarbill.cancelDown");
			String sqlCount = CoreXmlSqlParsersFactory
					.getSql("CorePipeCarbill.getCountBySendPlanNo");
			String sqlUpdateSendM = CoreXmlSqlParsersFactory
					.getSql("CorePipeCarbill.updatePlanMBySendPlanNo");
			for (String[] billNos : billNoArrs) {
				String billNo = billNos[0];
				String sendPlanNo = billNos[1];
				ps = con.prepareStatement(sql);
				ps.setObject(1, billNo);
				String status = "";// 提单状态
				String loadFal = "";// 提单类型
				String isFalg = "";// 虚单关闭
				rs = ps.executeQuery();
				if (rs.next()) {
					status = rs.getString("BILL_STATUS");
					loadFal = rs.getString("LOADVEHICLE_TYPE");
					isFalg = rs.getString("ISPRECLOSE");
				}
				rs.close();
				ps.close();
				int count = 0;
				if (loadFal.equals("1")) {
					if (!status.equals("1")) {
						core.setV_errMsg("提单号：" + billNo + ":实单不是下发状态的提单,不能撤销!");
						return core;
					}
				} else {
					if (!isFalg.equals("0") || !status.equals("1")) {
						core.setV_errMsg("提单号：" + billNo
								+ ":虚单不是未关闭且提单下发状态,不能撤销!");
						return core;

					}
				}
				// 修改提单状态
				ps = con.prepareStatement(sqlU);
				ps.setObject(1, billNo);
				ps.setObject(2, userName);
				ps.setObject(3, billNo);
				ps.executeUpdate();
				ps.close();
				// 验证计划号的提单数量(如果除开这个提单没有别的提单,则将计划号改为下发状态)
				ps = con.prepareStatement(sqlCount);
				ps.setObject(1, billNo);
				ps.setObject(2, sendPlanNo);
				rs = ps.executeQuery();
				if (rs.next()) {
					count = rs.getInt("COUNT");
				}
				rs.close();
				ps.close();
				if (count == 0) {
					ps = con.prepareStatement(sqlUpdateSendM);
					ps.setObject(1, sendPlanNo);
					ps.executeUpdate();
					ps.close();
				}

			}

			core.setV_errMsg("撤销下发成功!");
			con.commit();
		} catch (SQLException e) {
			try {
				con.rollback();
			} catch (SQLException e1) {
				e1.printStackTrace();
			}
			e.printStackTrace();
		} finally {
			try {
				if (con != null)
					con.close();
			} catch (SQLException e) {
				e.printStackTrace();
			}
		}

		return core;
	}

	/**
	 * 撤销划断
	 * 
	 * @param billNos
	 * @param userName
	 * @return
	 */
	public CoreReturnObject cancelBillDrawOff(ArrayList<String> billNos,
			String userName) {
		CoreReturnObject core = new CoreReturnObject();
		Connection con = null;
		PreparedStatement ps = null;
		ResultSet rs = null;
		try {
			con = this.getDao(CoreConstantData.sysbeanstr).getConnection();
			con.setAutoCommit(false);
			String sqlBillInfo = CoreXmlSqlParsersFactory
					.getSql("CorePipeCarbill.getBillInfoByBillNo");
			String sqlTal = CoreXmlSqlParsersFactory
					.getSql("CorePipeCarbill.getTallysheetNum");
			String sqlcancel = CoreXmlSqlParsersFactory
					.getSql("CorePipeCarbill.cancelBillDrawOff");
			for (String billNo : billNos) {
				ps = con.prepareStatement(sqlBillInfo);
				ps.setObject(1, billNo);
				rs = ps.executeQuery();
				String status = "";// 提单状态
				String vehicle = "";// 提单类型
				String isPreclose = "";// 虚单关闭状态
				String biTicStat = "";// 提单齐集状态
				if (rs.next()) {
					status = rs.getString("BILL_STATUS");
					vehicle = rs.getString("LOADVEHICLE_TYPE");
					isPreclose = rs.getString("ISPRECLOSE");
					biTicStat = rs.getString("BILL_TICKET_STATUS");
				}
				rs.close();
				ps.close();
				if (!biTicStat.equals("0")) {
					core.setV_errMsg("提单号:" + billNo + "已发送结算,不能撤销一次关闭!");
					return core;
				}
				if (!isPreclose.equals("1")) {
					core.setV_errMsg("提单号:" + billNo + "不是一次关闭,不能撤销一次关闭!");
					return core;
				}
				if (!status.equals("1")) {
					core.setV_errMsg("提单号:" + billNo + "不是下发状态的提单,不能撤销一次关闭!");
					return core;
				}

				ps = con.prepareStatement(sqlTal);
				ps.setObject(1, billNo);
				rs = ps.executeQuery();
				int count = 0;
				if (rs.next()) {
					count = rs.getInt("COUNT");
				}
				rs.close();
				ps.close();
				if (count != 0) {
					core.setV_errMsg("提单号:" + billNo + "存在理货数据,不能撤销!");
					return core;
				}
				ps = con.prepareStatement(sqlcancel);
				ps.setObject(1, billNo);
				ps.executeUpdate();
				ps.close();

				//crm提单同步
				CrmBillService crmBillService = new CrmBillService();
				// 判断crm系统是否结算
				if(crmBillService.selectCrmBillIsPay(con, billNo)){
					core.setV_errMsg("crm系统已结算，请联系crm系统撤销结算信息!");
					return core;
				}
				crmBillService.insertCrmBill(con,billNo,"2");
			}
			core.setV_errMsg("撤销一次关闭成功!");
			con.commit();
		} catch (SQLException e) {
			try {
				con.rollback();
			} catch (SQLException e1) {
				e1.printStackTrace();
			}
			e.printStackTrace();
		} finally {
			try {
				if (con != null)
					con.close();
			} catch (SQLException e) {
				e.printStackTrace();
			}
		}

		return core;
	}

	/**
	 * 提单划断
	 * 
	 * @param billNos
	 * @param userName
	 * @return
	 * @throws Exception 
	 */
	public CoreReturnObject billDrawOff(ArrayList<String> billNos,
			String userName) throws Exception {
		CoreReturnObject core = new CoreReturnObject();
		Connection con = null;
		PreparedStatement ps = null;
		ResultSet rs = null;
		try {
			con = this.getDao(CoreConstantData.sysbeanstr).getConnection();
			con.setAutoCommit(false);
			String sqlBillInfo = CoreXmlSqlParsersFactory
					.getSql("CorePipeCarbill.getBillInfoByBillNo");
			String sqlcancel = CoreXmlSqlParsersFactory
					.getSql("CorePipeCarbill.billDrawOff");
			List<JSONObject> jsonObjects  = new ArrayList<>();
			   JSONObject jsonObject =  new JSONObject();
			for (String billNo : billNos) {
				ps = con.prepareStatement(sqlBillInfo);
				ps.setObject(1, billNo);
				rs = ps.executeQuery();
				String status = "";
				String vehicle = "";
				String isPreclose = "";
				if (rs.next()) {
					status = rs.getString("BILL_STATUS");
					vehicle = rs.getString("LOADVEHICLE_TYPE");
					isPreclose = rs.getString("ISPRECLOSE");
					   jsonObject.put("form_code", "MESTHSO10");
					   jsonObject.put("form_name", "虚单关闭");
					   jsonObject.put("form_time", rs.getString("CREATE_TIME"));
					   jsonObject.put("op_flag","N");
					   jsonObject.put("outbill_no",rs.getString("BILL_NO"));
					   jsonObject.put("deliver_plan_num",rs.getString("BILL_NO_SEQ"));
					   jsonObject.put("order_no",rs.getString("ORDER_NO"));
					   jsonObject.put("order_no_main",rs.getString("ORDER_SEQ"));
					   jsonObject.put("receive_customer",rs.getString("RECEIV_NM"));
					   jsonObject.put("car_no","");
					   jsonObject.put("load_quantity",rs.getString("PLAN_NUM"));
					   jsonObject.put("load_weight",rs.getString("PLAN_WT"));
					   jsonObject.put("load_storage",rs.getString("MANAGEMENT_NAME"));
					   jsonObject.put("load_date","");
					   jsonObject.put("load_name","");
					   jsonObjects.add(jsonObject);
				}
				rs.close();
				ps.close();
				// if (!(status.equals("1") && vehicle.equals("2") &&
				// !isPreclose
				// .equals("0"))) {
				// core.setV_errMsg("提单号:" + billNo
				// + "不是虚拟下发且未一次关闭的提单,不能再次关闭!");
				// return core;
				// }
				if (status.equals("0")) {
					core.setV_errMsg("提单号:" + billNo + "未下发,不能一次关闭!");
					return core;
				}
				if (vehicle.equals("2") && !isPreclose.equals("0")) {
					core.setV_errMsg("提单号:" + billNo + "已一次关闭,不能再次关闭!");
					return core;
				}
				ps = con.prepareStatement(sqlcancel);
				ps.setObject(1, billNo);
				ps.executeUpdate();
				ps.close();

				//crm提单同步
				CrmBillService crmBillService = new CrmBillService();
				crmBillService.insertCrmBill(con,billNo,"0");
			}
			 DefaultWebServiceRequest request = new DefaultWebServiceRequest();
			   request.setMsgBody(jsonObjects.toString());
			   request.setServiceId("MESTHSO10");
			   String result = ServiceToCx.callWebserviceASMX("http://172.54.10.24:8191/ReceiverService.asmx",request,
						"receiveMessage",jsonObjects.toString());	
			   if(!result.equals("200")) {
				   core.setV_errMsg("接口信息上传失败！");
				   core.setResult(1);
				    return core;
			   }
			con.commit();
			core.setV_errMsg("提单一次关闭成功!");

		} catch (SQLException e) {
			try {
				con.rollback();
			} catch (SQLException e1) {
				e1.printStackTrace();
			}
			e.printStackTrace();
		} finally {
			try {
				if (con != null)
					con.close();
			} catch (SQLException e) {
				e.printStackTrace();
			}
		}

		return core;
	}

	/**
	 * 合同撤销
	 * 
	 * @param param
	 * @return
	 */
	public CoreReturnObject cancelDownOrderNo(ArrayList<String> param) {
		CoreReturnObject core = new CoreReturnObject();
		PreparedStatement ps = null;
		ResultSet rs = null;
		Connection con = null;
		//Connection wlCon = null;
		try {
			con = this.getDao(CoreConstantData.sysbeanstr).getConnection();
			con.setAutoCommit(false);
			//wlCon = new DBCommon().getDao("TmsDao").getConnection();注释生产物流  2024/4/24 肖沅累
			//wlCon.setAutoCommit(false);
			String sql = CoreXmlSqlParsersFactory
					.getSql("CorePipeCarbill.getBillInfoByBillNo");
			String billNo = param.get(0);
			String ordPk = param.get(1);
			String ordLnPk = param.get(2);
			String ordLnDlyPk = param.get(3);
			String judgeStoveNo = param.get(4);
			String batchNo = param.get(5);
			String userName = param.get(6);
			String groupNo = param.get(7);
			String locationNo = param.get(8);
			ps = con.prepareStatement(sql);
			ps.setObject(1, billNo);
			rs = ps.executeQuery();
			String status = "";
			String flag = "";
			if (rs.next()) {
				status = rs.getString("BILL_STATUS");
				flag = rs.getString("LOCK_BILL_FLAG");
			}
			rs.close();
			ps.close();
			if (!status.equals("0")) {
				core.setV_errMsg("提单号:" + billNo + "发生变化,请重新获取数据!");
				return core;
			}
			if (flag.equals("1")) {
				core.setV_errMsg("提单号:" + billNo + "已锁定,请联系质保部业务员解锁后再进行操作!");
				return core;
			}
			// 修改物料表里数据(状态改为 80150301)
			sql = CoreXmlSqlParsersFactory
					.getSql("CorePipeCarBill.deleteMatByOrdPkAndJust");
//			if(custom.equals("1_120504"))
//			{
			if(locationNo!=null && !"".equals(locationNo))
				sql = sql.replace("#LOCATION_NO#"," AND T1.LOCATION_NO = '"+locationNo+"'");
//			}else
//			{
//				sql  = sql.replace("#LOCATION_NO#","");
//			}
			ps = con.prepareStatement(sql);
			ps.setObject(1, userName);
			ps.setObject(2, billNo);
			ps.setObject(3, ordPk);
			ps.setObject(4, ordLnPk);
			ps.setObject(5, ordLnDlyPk);
			ps.setObject(6, judgeStoveNo);
			ps.setObject(7, batchNo);
			ps.setObject(8, groupNo);
			ps.executeUpdate();
			ps.close();
			// 判断量(主表量)
			sql = CoreXmlSqlParsersFactory
					.getSql("CorePipeCarBill.queryBillMByOrdPkAndJust");
			ps = con.prepareStatement(sql);
			ps.setObject(1, billNo);
			ps.setObject(2, ordLnDlyPk);
			rs = ps.executeQuery();
			double planWgt = 0;
			int planNum = 0;
			double planLen = 0;
			String sendPlanNo = "";
			if (rs.next()) {
				planWgt = rs.getDouble("PLAN_WT");
				planNum = rs.getInt("PLAN_NUM");
				sendPlanNo = rs.getString("SEND_PLAN_NO");
				planLen = rs.getDouble("PLAN_TOTL_LEN");
			}
			rs.close();
			ps.close();
			// 从表量
			sql = CoreXmlSqlParsersFactory
					.getSql("CorePipeCarBill.queryBillCByOrdPkAndJust");
//			if(custom.equals("1_120504"))
//			{
				sql = sql.replace("#LOCATION_NO#"," AND T.LOCATION_NO = '"+locationNo+"'");
//			}else
//			{
//				sql  = sql.replace("#LOCATION_NO#","");
//			}
			ps = con.prepareStatement(sql);
			ps.setObject(1, billNo);
			ps.setObject(2, ordLnDlyPk);
			ps.setObject(3, judgeStoveNo);
			ps.setObject(4, batchNo);
			ps.setObject(5, groupNo);
			rs = ps.executeQuery();
			double actWgt = 0;
			int actNum = 0;
			double actLen = 0;
			if (rs.next()) {
				actWgt = rs.getDouble("ACT_WEIGHT");
				actNum = rs.getInt("ACT_COUNT");
				actLen = rs.getDouble("ACT_LEN");
			}
			rs.close();
			ps.close();
			// 提单主表处理
			if (planNum > actNum) {
				sql = CoreXmlSqlParsersFactory
						.getSql("CorePipeCarBill.updateBillMByOrdPkAndJust");
				ps = con.prepareStatement(sql);
				ps.setObject(1, planWgt - actWgt);
				ps.setObject(2, planNum - actNum);
				ps.setObject(3, planLen - actLen);
				ps.setObject(4, billNo);
				ps.setObject(5, ordLnDlyPk);
				ps.executeUpdate();
				ps.close();
			} else {
				sql = CoreXmlSqlParsersFactory
						.getSql("CorePipeCarBill.deleteBillMByOrdPkAndJust");
				ps = con.prepareStatement(sql);
				ps.setObject(1, billNo);
				ps.setObject(2, ordLnDlyPk);
				ps.executeUpdate();
				ps.close();

				sql = CoreXmlSqlParsersFactory
						.getSql("CorePipeCarBill.DeleteTRANSIT");
				ps = con.prepareStatement(sql);
				ps.setObject(1, billNo);
				ps.setObject(2, ordLnDlyPk);
				ps.executeUpdate();
				ps.close();
			}

			// 删除提单从表对应的数据
			sql = CoreXmlSqlParsersFactory
					.getSql("CorePipeCarBill.deleteBillCByOrdPkAndJust");

//			if(custom.equals("1_120504"))
//			{
			if(locationNo!=null && !"".equals(locationNo))
				sql = sql.replace("#LOCATION_NO#"," AND T.LOCATION_NO = '"+locationNo+"'");
//			}else
//			{
//				sql  = sql.replace("#LOCATION_NO#","");
//			}
			ps = con.prepareStatement(sql);
			ps.setObject(1, billNo);
			ps.setObject(2, ordLnDlyPk);
			ps.setObject(3, judgeStoveNo);
			ps.setObject(4, batchNo);
			ps.setObject(5, groupNo);
			ps.executeUpdate();
			ps.close();

			String sqlSendM = CoreXmlSqlParsersFactory
					.getSql("CorePipeCarBill.DeleteSendM");
			String sqlSendC = CoreXmlSqlParsersFactory
					.getSql("CorePipeCarBill.DeleteSendC");

			ps = con.prepareStatement(sqlSendM);
			ps.setObject(1, actWgt);
			ps.setObject(2, sendPlanNo);
			ps.executeUpdate();
			ps.close();

			ps = con.prepareStatement(sqlSendC);
			ps.setObject(1, actWgt);
			ps.setObject(2, sendPlanNo);
			ps.setObject(3, ordLnDlyPk);
			ps.executeUpdate();
			ps.close();
			
			//删除物流表数据
			/*String sqlDeteteWl = CoreXmlSqlParsersFactory
					.getSql("CorePipeCarBill.deleteWlInfoByBillNo");
			ps = wlCon.prepareStatement(sqlDeteteWl);
			ps.setObject(1, billNo+"/"+ordLnDlyPk);
			ps.executeUpdate();
			ps.close();*/
			
			core.setV_errMsg("撤销成功!");
			con.commit();
			//wlCon.commit();
		} catch (SQLException e) {
			e.printStackTrace();
		} finally {
			if (con != null) {
				try {
					con.close();
				} catch (SQLException e) {
					e.printStackTrace();
				}
			}
			/*if (wlCon != null) {
				try {
					wlCon.close();
				} catch (SQLException e) {
					e.printStackTrace();
				}
			}*/
		}
		return core;
	}

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

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

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

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

		return core;
	}
	/**
	 * 修改出库地点
	 * 
	 * @param
	 * @return
	 */
	@SuppressWarnings("unchecked")
	public CoreReturnObject updateReceivNo(ArrayList<Object> pram) {
		CoreReturnObject core = new CoreReturnObject();
		Connection con = this.getDao(CoreConstantData.sysbeanstr)
				.getConnection();
		PreparedStatement ps = null;
		ResultSet rs = null;
		String sql = "";
		try {
			con.setAutoCommit(false);
			for (int i = 0; i < pram.size(); i++) {
				ArrayList<String> list = (ArrayList<String>) pram.get(i);
				String billNo = list.get(0);
				String receivNo = list.get(1);
				String receivNm = list.get(2);
				
				sql = CoreXmlSqlParsersFactory
						.getSql("CorePipeCarBill.checkBillStatus");
				ps = con.prepareStatement(sql);
				ps.setObject(1, billNo);
				rs = ps.executeQuery();
				if (rs.next()) {
				if(!rs.getString("BILL_STATUS").equals("0")){
					core.setV_errMsg("你选择的提单不是提单编制状态,不能修改收货单位！");
					return core;
				}
				}
				rs.close();
				ps.close();
				//修改收货单位
				String update = " UPDATE YDM_ZC_BILL_M T SET T.RECEIV_NO=?,T.RECEIV_NM=? WHERE T.BILL_NO=?";
				ps=con.prepareStatement(update);
				ps.setObject(1, receivNo);
				ps.setObject(2, receivNm);
				ps.setObject(3, billNo);
				ps.executeUpdate();
				ps.close();
			}
			core.setV_errMsg("修改成功！");
			con.commit();
		} catch (SQLException e) {
			try {
				con.rollback();
			} catch (SQLException e1) {
				e1.printStackTrace();
			}
			e.printStackTrace();
		} finally {
			try {
				if (con != null)
					con.close();
			} catch (SQLException e) {
				e.printStackTrace();
			}
		}

		return core;
	}

	//修改项目号/订单号
	public CoreReturnObject UpdateItemNo(String billNo, String singleNo, String itemNo) {
		CoreReturnObject core = new CoreReturnObject();
		Connection con = this.getDao(CoreConstantData.sysbeanstr)
				.getConnection();
		PreparedStatement ps = null;
		try {
			con.setAutoCommit(false);

			String update = " UPDATE YDM_ZC_BILL_M T SET T.SINGLE_NO=?,T.ITEM_NO=? WHERE T.BILL_NO=?";
			ps=con.prepareStatement(update);
			ps.setObject(1, singleNo);
			ps.setObject(2, itemNo);
			ps.setObject(3, billNo);
			ps.executeUpdate();
			ps.close();
			core.setV_errMsg("修改成功！");
			con.commit();
		} catch (SQLException e) {
			try {
				con.rollback();
			} catch (SQLException e1) {
				e1.printStackTrace();
			}
			e.printStackTrace();
		} finally {
			try {
				if (con != null)
					con.close();
			} catch (SQLException e) {
				e.printStackTrace();
			}
		}

		return core;
	}

	//修改发货去向
	public CoreReturnObject UpdateDelvryWhereabouts(ArrayList<Object> pram) {
		CoreReturnObject core = new CoreReturnObject();
		Connection con = this.getDao(CoreConstantData.sysbeanstr)
				.getConnection();
		PreparedStatement ps = null;
		try {
			con.setAutoCommit(false);
			for (int i = 0; i < pram.size(); i++) {
				ArrayList<String> list = (ArrayList<String>) pram.get(i);
				String billNo = list.get(0);
				String delvryWhereabouts = list.get(1);
				String update = " UPDATE YDM_ZC_BILL_M T SET T.DELVRY_WHEREABOUTS=? WHERE T.BILL_NO=?";
				ps = con.prepareStatement(update);
				ps.setObject(1, delvryWhereabouts);
				ps.setObject(2, billNo);
				ps.executeUpdate();
				ps.close();
			}
			core.setV_errMsg("修改成功！");
			con.commit();
		} catch (SQLException e) {
			try {
				con.rollback();
			} catch (SQLException e1) {
				e1.printStackTrace();
			}
			e.printStackTrace();
		} finally {
			try {
				if (con != null)
					con.close();
			} catch (SQLException e) {
				e.printStackTrace();
			}
		}

		return core;
	}

	//修改结算状态
	public CoreReturnObject UpdateRealinvoiceno1(ArrayList<Object> pram) {
		CoreReturnObject core = new CoreReturnObject();
		Connection con = this.getDao(CoreConstantData.sysbeanstr)
				.getConnection();
		PreparedStatement ps = null;
		try {
			con.setAutoCommit(false);
			for (int i = 0; i < pram.size(); i++) {
				ArrayList<String> list = (ArrayList<String>) pram.get(i);
				String billNo = list.get(0);
				String delvryWhereabouts = list.get(1);
				String update = " UPDATE YDM_ZC_BILL_M T SET T.REALINVOICENO1=DECODE(?,'已结算','1','') WHERE T.BILL_NO=?";
				ps = con.prepareStatement(update);
				ps.setObject(1, delvryWhereabouts);
				ps.setObject(2, billNo);
				ps.executeUpdate();
				ps.close();
			}
			core.setV_errMsg("修改成功！");
			con.commit();
		} catch (SQLException e) {
			try {
				con.rollback();
			} catch (SQLException e1) {
				e1.printStackTrace();
			}
			e.printStackTrace();
		} finally {
			try {
				if (con != null)
					con.close();
			} catch (SQLException e) {
				e.printStackTrace();
			}
		}

		return core;
	}

	//修改关联交易状态
	public CoreReturnObject UpdateLetDeliveryFlag(ArrayList<Object> pram) {
		CoreReturnObject core = new CoreReturnObject();
		Connection con = this.getDao(CoreConstantData.sysbeanstr)
				.getConnection();
		PreparedStatement ps = null;
		try {
			con.setAutoCommit(false);
			for (int i = 0; i < pram.size(); i++) {
				ArrayList<String> list = (ArrayList<String>) pram.get(i);
				String billNo = list.get(0);
				String delvryWhereabouts = list.get(1);
				String update = " UPDATE YDM_ZC_BILL_M T SET T.LET_DELIVERY_FLAG=DECODE(?,'未确认','1','一次确认','2','二次确认','') WHERE T.BILL_NO=?";
				ps = con.prepareStatement(update);
				ps.setObject(1, delvryWhereabouts);
				ps.setObject(2, billNo);
				ps.executeUpdate();
				ps.close();
			}
			core.setV_errMsg("修改成功！");
			con.commit();
		} catch (SQLException e) {
			try {
				con.rollback();
			} catch (SQLException e1) {
				e1.printStackTrace();
			}
			e.printStackTrace();
		} finally {
			try {
				if (con != null)
					con.close();
			} catch (SQLException e) {
				e.printStackTrace();
			}
		}

		return core;
	}

	//修改是否齐炉状态
	public CoreReturnObject UpdateStoveTicketFlag(ArrayList<Object> pram) {
		CoreReturnObject core = new CoreReturnObject();
		Connection con = this.getDao(CoreConstantData.sysbeanstr)
				.getConnection();
		PreparedStatement ps = null;
		try {
			con.setAutoCommit(false);
			for (int i = 0; i < pram.size(); i++) {
				ArrayList<String> list = (ArrayList<String>) pram.get(i);
				String billNo = list.get(0);
				String delvryWhereabouts = list.get(1);
				String update = " UPDATE YDM_ZC_BILL_M T SET T.STOVE_TICKET_FLAG=DECODE(?,'','1','已齐炉','0','1') WHERE T.BILL_NO=?";
				ps = con.prepareStatement(update);
				ps.setObject(1, delvryWhereabouts);
				ps.setObject(2, billNo);
				ps.executeUpdate();
				ps.close();
			}
			core.setV_errMsg("修改成功！");
			con.commit();
		} catch (SQLException e) {
			try {
				con.rollback();
			} catch (SQLException e1) {
				e1.printStackTrace();
			}
			e.printStackTrace();
		} finally {
			try {
				if (con != null)
					con.close();
			} catch (SQLException e) {
				e.printStackTrace();
			}
		}

		return core;
	}


	public CoreReturnObject queryLimsData(String[] billNos){
		if(billNos.length == 0 || billNos == null){
			return null;
		}
		String sql = CoreXmlSqlParsersFactory.getSql("CorePipeCarBill.queryLimsData");
		sql = sql.replaceAll("#code#",CoreConverter.toSqlConditionString("M.BILL_NO", billNos));
		return this.getData(sql);
	}

	public CoreReturnObject getMoneyByBuyercode(String buyercode) {
		Object[] params = new Object[] { buyercode};
		return this.getDataBySqlID("CorePipeCarBill.getMoneyByBuyercode", params);
	}

	public CoreReturnObject getBillOrder(String billNo) {
		Object[] params = new Object[] { billNo};
		return this.getDataBySqlID("CorePipeCarBill.getBillOrder", params);
	}

	public CoreReturnObject DoDownLog(ArrayList<ArrayList<String>> list) {
		CoreReturnObject cro = new CoreReturnObject();
		ArrayList<String> sqllist = new ArrayList<String>();
		ArrayList<Object[]> parmlist = new ArrayList<Object[]>();
		for (int i = 0; i < list.size(); i++) {
			String sql = " INSERT INTO YDM_ZC_BILL_DOWN(\n" +
					"        BILL_NO,\n" +
					"        STLMT_UNIT_NO,\n" +
					"        STLMT_UNIT_NM,\n" +
					"        STLMT_UNIT_MONEY,\n" +
					"        ORDER_MONEY,\n" +
					"        CREATE_NAME,\n" +
					"        CREATE_TIME)\n" +
					" VALUES( \n" +
					"        ?,\n" +
					"        ?,\n" +
					"        ?,\n" +
					"        ?,\n" +
					"        ?,\n" +
					"        ?,\n" +
					"        SYSDATE)";
			sqllist.add(sql);
			parmlist.add(list.get(i).toArray());

		}
		cro = this.setData(sqllist, parmlist);
		if(cro.getV_errCode()>=0){
			cro.setV_errMsg("修改成功!");
		}
		return cro;
	}

}
