package com.steering.pss.ydm.pipemanage;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.text.DecimalFormat;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import CoreFS.SA06.CoreReturnObject;
import CoreFS.SA08.CoreXmlSqlParsersFactory;
import CoreFS.SC01.CoreConstantData;
import com.steering.comm.converter.CoreConverter;
import com.steering.comm.db.DBCommon;
import com.steering.comm.tools.EntityTool;
import com.steering.comm.tools.StringTool;
import com.steering.pss.judge.model.MatBcMEntity;
import com.steering.pss.ydm.base.MeanValueCalculation;
import com.steering.pss.ydm.entity.MatZcMEntity1;
import com.steering.pss.ydm.entity.QcmZgJugdeApplyEntity;
import com.steering.pss.ydm.entity.YdmZcInlistEntity;

public class FrmPipStoreRed extends DBCommon{

	
	/***
	 * 查询库存实绩
	 * @param pram
	 * @param arr
	 * @param startTim
	 * @param endTim
	 * @return
	 */
	public CoreReturnObject getMat(ArrayList<String> pram,String[] arr,String startTim,String endTim)
	{
		String sql = CoreXmlSqlParsersFactory.getSql("FrmPipStoreRed.getMat");
		sql += CoreConverter.toSqlConditionString("T1.BELONG_CODE", arr);
		if (!startTim.equals("")) {
			sql += " AND T1.INSTOCK_TIME >=TO_DATE('"
					+ startTim
					+ "', 'YYYY-MM-DD HH24:MI:SS')AND T1.INSTOCK_TIME <=TO_DATE('"
					+ endTim + "', 'YYYY-MM-DD HH24:MI:SS')";
		}
		if(!pram.get(0).equals(""))
		{
		    sql += " AND T1.JUDGE_STOVE_NO  LIKE '"+pram.get(0)+"%'";	
		}
		if(!pram.get(1).equals(""))
		{
			sql += " AND T1.ORDER_NO LIKE '%"+pram.get(1)+"'";
		}
		if(!pram.get(2).equals(""))
		{
			sql += " AND S.STORAGE_NO = '"+pram.get(2)+"'";
		}
		sql += " GROUP BY T1.JUDGE_STOVE_NO, T1.BATCH_NO, T1.BATCH_GROUD_NO,T1.PRODUCCODE,T1.PRODUCNAME,T1.STEELCODE,T1.STEELNAME,T1.SPEC_CODE,T1.SPEC_NAME,T1.MODEL_CODE,T1.GRADECODE,T1.GRADENAME,T1.STD_STYLE,T1.STD_STYLE_DESC,T1.ORD_LN_PK,T1.ORD_PK,T1.ORD_LN_DLY_PK,T1.MAT_STATUS,T1.MATERIAL_NO,T1.MATERIAL_NAME,";
		sql += " T1.MODEL_DESC,T1.STD_CODE,T1.STD_NAME,T1.ORDER_NO,T1.ORDER_SEQ,T1.ORDER_SEQ_NO,T1.ACT_DIMATER,T1.ACT_HEIGHT,T1.BELONG_CODE,T1.BELONG_NAME,T1.PLINE_CODE,T1.PLINE_NAME,";
		sql += " T2.STORAGE_NO,S.STORAGE_NAME,T1.MEMO,T1.PRODUCT_FLAG,T1.JUDGE_STD_CODE,T1.JUDGE_STD_NAME";
//T1.CREATE_NAME,T1.ACT_LEN_MIN,T1.ACT_LEN_MAX,
        return this.getData(sql);
	}
	/***
	 * 查询入库实绩
	 * @param parm
	 * @return
	 */
	public CoreReturnObject getInList(ArrayList<String> list,String[] arr,String startTim,String endTim)
	{
		String sql = CoreXmlSqlParsersFactory
				.getSql("FrmPipStoreRed.getInList");
		sql += CoreConverter.toSqlConditionString("t.BELONG_CODE", arr);
		if (!startTim.equals("")) {
			sql += " AND INSTOCK_TIME>=TO_DATE('" + startTim
					+ "','YYYY-MM-DD HH24:MI:SS') AND INSTOCK_TIME<= TO_DATE('"
					+ endTim + "','YYYY-MM-DD HH24:MI:SS')";
		}
		sql += " group by t.JUDGE_STOVE_NO,t.BATCH_NO,t.BATCH_GROUD_NO,t.INSTOCK_DOC,t.PRODUCCODE,t.PRODUCNAME,t.STD_CODE,t.STD_NAME,t.STEELCODE,t.STEELNAME,t.STD_STYLE,t.STD_STYLE_DESC,t.SPEC_CODE,t.SPEC_NAME,t.JUDGE_STD_CODE,t.JUDGE_STD_NAME,t.GRADECODE,t.GRADENAME,t.BAL_YEAR_MONTH,t.ORD_LN_PK,t.ORD_LN_DLY_PK,";
		sql += " t.MODEL_DESC,t.ACT_DIMATER,t.ACT_HEIGHT,t.ACT_LEN_MIN,t.ACT_LEN_MAX,t.BELONG_CODE,t.BELONG_NAME,s.STORAGE_NAME,t.MEMO,t.CREATE_NAME,t.ORDER_NO,t.ORDER_SEQ,t.DELIVERY_NO,t.MODEL_CODE,t.PRODUCT_FLAG,t.ZB_RCDID,t.PLINE_CODE,t.PLINE_NAME,d.SALE_AREA_DESC,d.create_name";
		sql += " order by t.instock_doc desc";

		return this.getData(sql, list.toArray());
		
	}
	
	/***
	 * 库存红冲
	 * @param parm1
	 * @param matZcEntity
	 * @return
	 */
	@SuppressWarnings("unchecked")
    public CoreReturnObject pipRedStore(ArrayList<Object> parm1,ArrayList<Object> parm2)
	{
		CoreReturnObject cro = new CoreReturnObject();
        Connection connection = this.getDao(CoreConstantData.sysbeanstr)
                .getConnection();
        PreparedStatement psta = null;
        ResultSet rs = null;
        Date now = new Date();
        SimpleDateFormat dateFormat = new SimpleDateFormat(
                "yyyy-MM-dd HH:mm:ss");
        String systime = dateFormat.format(now);
        SimpleDateFormat df = new SimpleDateFormat(
        "yyyyMM");
        String bathYear = df.format(now);
        String sql = "";
        String inListDoc = "";
        String inListDocCx = "";
        try {
            connection.setAutoCommit(false); // 取消自动提交事务
           
            String insertMatSql = CoreXmlSqlParsersFactory.getSql("pipRedStore.insertMat");//插入材料表
            PreparedStatement insertMatpsta = connection.prepareStatement(insertMatSql);
            
            String insertMatSqlXs = CoreXmlSqlParsersFactory.getSql("matxs.insertMat");//插入销售材料表
            PreparedStatement insertMatpstaXs = connection.prepareStatement(insertMatSqlXs);
            
            String insertButressSql = CoreXmlSqlParsersFactory.getSql("pipRedStore.insertButress");//插入位置表
            PreparedStatement insertButresspsta = connection.prepareStatement(insertButressSql);
            
            String deleteMatSql = CoreXmlSqlParsersFactory.getSql("pipRedStore.deleteMat");//删除材料表
            PreparedStatement deleteMatpsta = connection.prepareStatement(deleteMatSql);
            
            String deleteMatSqlXs = CoreXmlSqlParsersFactory.getSql("matxs.deleteMat");//删除材料表
            PreparedStatement deleteMatpstaXs = connection.prepareStatement(deleteMatSqlXs);
            
            String deleteButessSql = CoreXmlSqlParsersFactory.getSql("pipRedStore.deleteButess");//删除位置表
            PreparedStatement deleteButesspsta = connection.prepareStatement(deleteButessSql);
            
            String insertInListSql = CoreXmlSqlParsersFactory.getSql("pipRedStore.insertInList");//插入入库表
            PreparedStatement insertInpsta = connection.prepareStatement(insertInListSql);
            
            String insertInListSqlXs = CoreXmlSqlParsersFactory.getSql("matxs.insertInList");//插入入库表
            PreparedStatement insertInpstaXs = connection.prepareStatement(insertInListSqlXs);
            
            String updatePlanSql = CoreXmlSqlParsersFactory.getSql("pipRedStore.updatePlan");//修改计划
            PreparedStatement updatepsta = connection.prepareStatement(updatePlanSql);
            String updateMatSql = CoreXmlSqlParsersFactory.getSql("pipRedStore.updateMatWgtLen");
            PreparedStatement updateMatpsta = connection.prepareStatement(updateMatSql);
            String updateProcessSql = CoreXmlSqlParsersFactory.getSql("pipRedStore.updateProcessSql");
            PreparedStatement updateProcess = connection.prepareStatement(updateProcessSql);//修改进程表
            
            
            
            
            
            for(int i = 0;i<parm1.size();i++)
            {
            	ArrayList<String> list = (ArrayList<String>) parm1.get(i);
            	inListDoc = querySendSqlDoc(connection,list.get(9),list.get(8)); //查询入库单号
            	inListDocCx = getInListDoc(connection, "800709", systime);
            	int storeNum = Integer.parseInt(list.get(3));//库存支数
            	int redNum = Math.abs(Integer.parseInt(list.get(4)));//红冲支数
            	double storeWgt = Double.parseDouble(list.get(5));//库存重量
            	double redWgt = Double.parseDouble(list.get(6));//红冲重量
            	double storeLen = 0;//库存长度
            	double redLen = 0;//红冲长度
            	String lcode = list.get(0);//炉号
            	String bathNo = list.get(1);//批号
            	if(!list.get(10).equals("") && !list.get(10).equals("0"))
            	{
            		storeLen = Double.parseDouble(list.get(10));
            		redLen = Double.parseDouble(list.get(11));
            	}
            	ArrayList<String> storeWgtList = null;
				ArrayList<String> storeLengthList = null;
				ArrayList<String> redWgtList = null;
				ArrayList<String> redLengthList = null;
				if(Integer.parseInt(list.get(4))<0)
				{
					if (storeNum > redNum) {
						storeWgtList = new ArrayList<String>();
						List<double[]> listwgt = MeanValueCalculation
								.MeanCalculation(storeWgt - redWgt, storeNum
										- redNum);// 重量
						for (int k = 0; k < listwgt.size(); k++) {
							double[] conLen = listwgt.get(k);
							if ((int) conLen[0] > 0) {
								for (int x = 0; x < (int) conLen[0]; x++) {
									DecimalFormat dt = new DecimalFormat(
											"0.000");
									storeWgtList.add(dt.format(conLen[1])
											.toString());
								}
							}
						}
						if (storeLen > 0) {
							storeLengthList = new ArrayList<String>();
							List<double[]> lenList = MeanValueCalculation
									.MeanCalculation(storeLen - redLen,
											storeNum - redNum);
							for (int k = 0; k < lenList.size(); k++) {
								double[] conLen = lenList.get(k);
								if ((int) conLen[0] > 0) {
									for (int x = 0; x < (int) conLen[0]; x++) {
										DecimalFormat dt = new DecimalFormat(
												"0.000");
										storeLengthList.add(dt
												.format(conLen[1]).toString());
									}
								}
							}
						}
						redWgtList = new ArrayList<String>();
						List<double[]> redListwgt = MeanValueCalculation
								.MeanCalculation(redWgt, redNum);// 重量
						for (int k = 0; k < redListwgt.size(); k++) {
							double[] conLen = redListwgt.get(k);
							if ((int) conLen[0] > 0) {
								for (int x = 0; x < (int) conLen[0]; x++) {
									DecimalFormat dt = new DecimalFormat(
											"0.000");
									redWgtList.add(dt.format(conLen[1])
											.toString());
								}
							}
						}
						if (redLen > 0) {
							redLengthList = new ArrayList<String>();
							List<double[]> redLenList = MeanValueCalculation
									.MeanCalculation(redLen, redNum);
							for (int k = 0; k < redLenList.size(); k++) {
								double[] conLen = redLenList.get(k);
								if ((int) conLen[0] > 0) {
									for (int x = 0; x < (int) conLen[0]; x++) {
										DecimalFormat dt = new DecimalFormat(
												"0.000");
										redLengthList.add(dt.format(conLen[1])
												.toString());
									}
								}
							}
						}
					}
					ArrayList<String> matNo = new ArrayList<String>();// 材料号(红冲)
					sql = CoreXmlSqlParsersFactory
							.getSql("pipRedStore.selectMatNo");
					psta = connection.prepareStatement(sql);
					psta.setString(1, list.get(0));
					psta.setString(2, list.get(1));
					psta.setString(3, list.get(2));
					psta.setString(4, list.get(46));
					psta.setString(5, list.get(18));
					psta.setString(6, list.get(59));
					psta.setString(7, list.get(60));
					psta.setInt(8, redNum + 1);
					rs = psta.executeQuery();
					while (rs.next()) {
						matNo.add(rs.getString("MAT_NO"));
					}
					rs.close();
					psta.close();
					ArrayList<String> matNoStore = new ArrayList<String>();// 材料号(红冲剩余)
					sql = CoreXmlSqlParsersFactory
							.getSql("pipRedStore.selectMatNoStore");
					psta = connection.prepareStatement(sql);
					psta.setString(1, list.get(0));
					psta.setString(2, list.get(1));
					psta.setString(3, list.get(2));
					psta.setString(4, list.get(60));
					psta.setInt(5, (storeNum - redNum) + 1);
					rs = psta.executeQuery();
					while (rs.next()) {
						matNoStore.add(rs.getString("MAT_NO"));
					}
					rs.close();
					psta.close();

					String inListSeq = querySendSql(connection, inListDoc); // 入库流水号
					int n = Integer.parseInt(inListSeq.substring(inListSeq
							.length() - 4, inListSeq.length()));
					int m = 0;
					if(storeNum == redNum)
					{
						MatBcMEntity matBcEntity = new MatBcMEntity();
						matBcEntity.setJudgeStoveNo(list.get(0));// 判定炉号
						matBcEntity.setBatchNo(list.get(1));// 批号
						matBcEntity.setBatchGroudNo(list.get(2));// 组号
						matBcEntity.setMatStatus(list.get(46));// 材料状态
						matBcEntity.setStorageNo(list.get(18));// 仓库
						matBcEntity.setBalYearMonth(list.get(59));
						matBcEntity.setActCount(String.valueOf(redNum));
						matBcEntity.setInstockDoc(inListDocCx);
						matBcEntity.setInstockTime(systime);
						matBcEntity.setCreateName(list.get(7));
						matBcEntity.setBalYearMonth(bathYear);
						matBcEntity.setBelongCode(list.get(60));

						String insertBcMSql = CoreXmlSqlParsersFactory
								.getSql("pipRedStore.insertBcM");// 退制造部半成品
						insertBcMSql = EntityTool.fillSqlByEntity(insertBcMSql,
								matBcEntity);
						Statement st = connection.createStatement();
						st.executeUpdate(insertBcMSql);
						st.close();
					}
					String inListSeqCx = querySendSqlCx(connection, inListDocCx); // 入库流水号
					int b = Integer.parseInt(inListSeqCx.substring(inListSeqCx
							.length() - 4, inListSeqCx.length()));
					for (int j = 0; j < matNo.size(); j++) {
						sql = CoreXmlSqlParsersFactory
								.getSql("pipRedStore.selectMatData");// 查询材料号所有信息
						psta = connection.prepareStatement(sql);
						psta.setString(1, matNo.get(j));
						rs = psta.executeQuery();
						List<MatZcMEntity1> matZcEntity = EntityTool
								.getEntitysByRs(rs, MatZcMEntity1.class);
						rs.close();
						psta.close();
						if (storeNum == redNum) {
							for (MatZcMEntity1 matzctity : matZcEntity) {
								insertInpsta.setObject(1, matzctity
										.getManagementNo());
								insertInpsta.setObject(2, matzctity
										.getManagementName());
								insertInpsta.setObject(3, inListDoc
										+ StringTool.formatDecimal(n, "0000"));
								insertInpsta.setObject(4, inListDoc);
								insertInpsta.setObject(5, "800704");
								insertInpsta.setObject(6, "产出红冲");
								insertInpsta.setObject(7, matNo.get(j));
								insertInpsta.setObject(8, matzctity.getLotNo());
								insertInpsta.setObject(9, matzctity
										.getStoveNo());
								insertInpsta.setObject(10, matzctity
										.getJudgeStoveNo());
								insertInpsta.setObject(11, matzctity
										.getBatchNo());
								insertInpsta.setObject(12, matzctity
										.getBatchGroudNo());
								insertInpsta.setObject(13, matzctity
										.getProduccode());
								insertInpsta.setObject(14, matzctity
										.getProducname());
								insertInpsta.setObject(15, matzctity
										.getStdCode());
								insertInpsta.setObject(16, matzctity
										.getStdName());
								insertInpsta.setObject(17, matzctity
										.getSteelcode());
								insertInpsta.setObject(18, matzctity
										.getSteelname());
								insertInpsta.setObject(19, matzctity
										.getStdStyle());
								insertInpsta.setObject(20, matzctity
										.getStdStyleDesc());
								insertInpsta.setObject(21, matzctity
										.getSpecCode());
								insertInpsta.setObject(22, matzctity
										.getSpecName());
								insertInpsta.setObject(23, matzctity
										.getModelCode());
								insertInpsta.setObject(24, matzctity
										.getModelDesc());
								insertInpsta.setObject(25, matzctity
										.getActDimater());
								insertInpsta.setObject(26, matzctity
										.getActHeight());
								if (!matzctity.getActLenMin().equals("")) {
									insertInpsta
											.setObject(27, new DecimalFormat(
													"0.00").format(Double
													.parseDouble(matzctity
															.getActLenMin())));
								} else {
									insertInpsta.setObject(27, "0.00");
								}
								if (!matzctity.getActLenMax().equals("")) {
									insertInpsta
											.setObject(28, new DecimalFormat(
													"0.00").format(Double
													.parseDouble(matzctity
															.getActLenMax())));
								} else {
									insertInpsta.setObject(28, "0.00");
								}
								insertInpsta.setObject(29, "-"
										+ matzctity.getActCount());// 支数
								insertInpsta.setObject(30, "-"
										+ matzctity.getActWeight());// 重量
								insertInpsta.setObject(31, matzctity
										.getActTheoryWeight());
								insertInpsta.setObject(32, matzctity
										.getBelongCode());
								insertInpsta.setObject(33, matzctity
										.getBelongName());
								insertInpsta.setObject(34, matzctity
										.getStorageNo());
								insertInpsta.setObject(35, matzctity.getMemo());
								insertInpsta.setObject(36, "1");
								insertInpsta.setObject(37, list.get(7));// 创建人
								insertInpsta.setObject(38, systime);// 创建时间
								insertInpsta.setObject(39, matzctity
										.getOrderNo());
								insertInpsta.setObject(40, matzctity
										.getOrderSeq());
								insertInpsta.setObject(41, matzctity
										.getOrderSeqNo());
								insertInpsta
										.setObject(42, matzctity.getOrdPk());
								insertInpsta.setObject(43, matzctity
										.getOrdLnPk());
								insertInpsta.setObject(44, matzctity
										.getOrdLnDlyPk());
								insertInpsta.setObject(45, matzctity
										.getProductFlag());
								insertInpsta.setObject(46, matzctity
										.getMatStatus());
								insertInpsta.setObject(47, systime);// 交易时间
								insertInpsta.setObject(48, "");// 质保ID
								if(matzctity.getActLen() !="")
								{
									insertInpsta.setObject(49, "-"+matzctity
											.getActLen());
								}
								else
								{
									insertInpsta.setObject(49, "0");
								}
								insertInpsta.setObject(50, matzctity
										.getDepartmentCode());
								insertInpsta.setObject(51, matzctity
										.getDepartmentDesc());
								insertInpsta.setObject(52, matzctity
										.getJudgeStdName());
//								insertInpsta.setObject(53, matzctity
//										.getPlineCode());
//								insertInpsta.setObject(54, matzctity
//										.getPlineName());
								insertInpsta.setObject(53, list.get(48));
								insertInpsta.setObject(54, list.get(49));
								// insertInpsta
								// .setObject(53, list.get(13));
								// insertInpsta
								// .setObject(54, list.get(14));
								insertInpsta.setObject(55, matzctity
										.getGradecode());
								insertInpsta.setObject(56, matzctity
										.getGradename());
//								insertInpsta.setObject(57, matzctity
//										.getBalYearMonth());
								insertInpsta.setObject(57,bathYear);
								insertInpsta.setObject(58, matzctity.getInstockTime());//首次入库时间
								insertInpsta
										.setObject(59, matzctity.getLenNo());
								insertInpsta.setObject(60, matzctity
										.getLenDesc());
								insertInpsta.setObject(61, matzctity
										.getLenUnit());
								insertInpsta.setObject(62, matzctity
										.getPipmemo());
								insertInpsta.setObject(63, list.get(53));
								insertInpsta.setObject(64, list.get(54));
								insertInpsta.setObject(65, list.get(55));
								insertInpsta.setObject(66, list.get(56));
								insertInpsta.setObject(67, matzctity.getMaterialNo());
								insertInpsta.setObject(68, matzctity.getMaterialName());
								insertInpsta.addBatch();
								
								insertInpstaXs.setObject(1, matzctity
										.getManagementNo());
								insertInpstaXs.setObject(2, matzctity
										.getManagementName());
								insertInpstaXs.setObject(3, inListDoc
										+ StringTool.formatDecimal(n, "0000"));
								insertInpstaXs.setObject(4, inListDoc);
								insertInpstaXs.setObject(5, "800704");
								insertInpstaXs.setObject(6, "产出红冲");
								insertInpstaXs.setObject(7, matNo.get(j));
								insertInpstaXs.setObject(8, matzctity.getLotNo());
								insertInpstaXs.setObject(9, matzctity
										.getStoveNo());
								insertInpstaXs.setObject(10, matzctity
										.getJudgeStoveNo());
								insertInpstaXs.setObject(11, matzctity
										.getBatchNo());
								insertInpstaXs.setObject(12, matzctity
										.getBatchGroudNo());
								insertInpstaXs.setObject(13, matzctity
										.getProduccode());
								insertInpstaXs.setObject(14, matzctity
										.getProducname());
								insertInpstaXs.setObject(15, matzctity
										.getStdCode());
								insertInpstaXs.setObject(16, matzctity
										.getStdName());
								insertInpstaXs.setObject(17, matzctity
										.getSteelcode());
								insertInpstaXs.setObject(18, matzctity
										.getSteelname());
								insertInpstaXs.setObject(19, matzctity
										.getStdStyle());
								insertInpstaXs.setObject(20, matzctity
										.getStdStyleDesc());
								insertInpstaXs.setObject(21, matzctity
										.getSpecCode());
								insertInpstaXs.setObject(22, matzctity
										.getSpecName());
								insertInpstaXs.setObject(23, matzctity
										.getModelCode());
								insertInpstaXs.setObject(24, matzctity
										.getModelDesc());
								insertInpstaXs.setObject(25, matzctity
										.getActDimater());
								insertInpstaXs.setObject(26, matzctity
										.getActHeight());
								if (!matzctity.getActLenMin().equals("")) {
									insertInpstaXs
											.setObject(27, new DecimalFormat(
													"0.00").format(Double
													.parseDouble(matzctity
															.getActLenMin())));
								} else {
									insertInpstaXs.setObject(27, "0.00");
								}
								if (!matzctity.getActLenMax().equals("")) {
									insertInpstaXs
											.setObject(28, new DecimalFormat(
													"0.00").format(Double
													.parseDouble(matzctity
															.getActLenMax())));
								} else {
									insertInpstaXs.setObject(28, "0.00");
								}
								insertInpstaXs.setObject(29, "-"
										+ matzctity.getActCount());// 支数
								insertInpstaXs.setObject(30, "-"
										+ matzctity.getActWeight());// 重量
								insertInpstaXs.setObject(31, matzctity
										.getActTheoryWeight());
								insertInpstaXs.setObject(32, matzctity
										.getBelongCode());
								insertInpstaXs.setObject(33, matzctity
										.getBelongName());
								insertInpstaXs.setObject(34, matzctity
										.getStorageNo());
								insertInpstaXs.setObject(35, matzctity.getMemo());
								insertInpstaXs.setObject(36, "1");
								insertInpstaXs.setObject(37, list.get(7));// 创建人
								insertInpstaXs.setObject(38, systime);// 创建时间
								insertInpstaXs.setObject(39, matzctity
										.getOrderNo());
								insertInpstaXs.setObject(40, matzctity
										.getOrderSeq());
								insertInpstaXs.setObject(41, matzctity
										.getOrderSeqNo());
								insertInpstaXs
										.setObject(42, matzctity.getOrdPk());
								insertInpstaXs.setObject(43, matzctity
										.getOrdLnPk());
								insertInpstaXs.setObject(44, matzctity
										.getOrdLnDlyPk());
								insertInpstaXs.setObject(45, matzctity
										.getProductFlag());
								insertInpstaXs.setObject(46, matzctity
										.getMatStatus());
								insertInpstaXs.setObject(47, systime);// 交易时间
								insertInpstaXs.setObject(48, "");// 质保ID
								if(matzctity.getActLen() !="")
								{
									insertInpstaXs.setObject(49, "-"+matzctity
											.getActLen());
								}
								else
								{
									insertInpstaXs.setObject(49, "0");
								}
								insertInpstaXs.setObject(50, matzctity
										.getDepartmentCode());
								insertInpstaXs.setObject(51, matzctity
										.getDepartmentDesc());
								insertInpstaXs.setObject(52, matzctity
										.getJudgeStdName());
//								insertInpstaXs.setObject(53, matzctity
//										.getPlineCode());
//								insertInpstaXs.setObject(54, matzctity
//										.getPlineName());
								insertInpstaXs.setObject(53, list.get(48));
								insertInpstaXs.setObject(54, list.get(49));
								// insertInpstaXs
								// .setObject(53, list.get(13));
								// insertInpstaXs
								// .setObject(54, list.get(14));
								insertInpstaXs.setObject(55, matzctity
										.getGradecode());
								insertInpstaXs.setObject(56, matzctity
										.getGradename());
//								insertInpstaXs.setObject(57, matzctity
//										.getBalYearMonth());
								insertInpstaXs.setObject(57,bathYear);
								insertInpstaXs.setObject(58, matzctity.getInstockTime());//首次入库时间
								insertInpstaXs
										.setObject(59, matzctity.getLenNo());
								insertInpstaXs.setObject(60, matzctity
										.getLenDesc());
								insertInpstaXs.setObject(61, matzctity
										.getLenUnit());
								insertInpstaXs.setObject(62, matzctity
										.getPipmemo());
								insertInpstaXs.setObject(63, list.get(53));
								insertInpstaXs.setObject(64, list.get(54));
								insertInpstaXs.setObject(65, list.get(55));
								insertInpstaXs.setObject(66, list.get(56));
								insertInpstaXs.setObject(67, matzctity.getMaterialNo());
								insertInpstaXs.setObject(68, matzctity.getMaterialName());
								insertInpstaXs.addBatch();

							}

						}
						if (storeNum > redNum) {
							for (MatZcMEntity1 matzctity : matZcEntity) {
								insertInpsta.setObject(1, matzctity
										.getManagementNo());
								insertInpsta.setObject(2, matzctity
										.getManagementName());
								insertInpsta.setObject(3, inListDoc
										+ StringTool.formatDecimal(n, "0000"));
								insertInpsta.setObject(4, inListDoc);
								insertInpsta.setObject(5, "800704");
								insertInpsta.setObject(6, "产出红冲");
								insertInpsta.setObject(7, matNo.get(j));
								insertInpsta.setObject(8, matzctity.getLotNo());
								insertInpsta.setObject(9, matzctity
										.getStoveNo());
								insertInpsta.setObject(10, matzctity
										.getJudgeStoveNo());
								insertInpsta.setObject(11, matzctity
										.getBatchNo());
								insertInpsta.setObject(12, matzctity
										.getBatchGroudNo());
								insertInpsta.setObject(13, matzctity
										.getProduccode());
								insertInpsta.setObject(14, matzctity
										.getProducname());
								insertInpsta.setObject(15, matzctity
										.getStdCode());
								insertInpsta.setObject(16, matzctity
										.getStdName());
								insertInpsta.setObject(17, matzctity
										.getSteelcode());
								insertInpsta.setObject(18, matzctity
										.getSteelname());
								insertInpsta.setObject(19, matzctity
										.getStdStyle());
								insertInpsta.setObject(20, matzctity
										.getStdStyleDesc());
								insertInpsta.setObject(21, matzctity
										.getSpecCode());
								insertInpsta.setObject(22, matzctity
										.getSpecName());
								insertInpsta.setObject(23, matzctity
										.getModelCode());
								insertInpsta.setObject(24, matzctity
										.getModelDesc());
								insertInpsta.setObject(25, matzctity
										.getActDimater());
								insertInpsta.setObject(26, matzctity
										.getActHeight());
								insertInpsta
										.setObject(
												27,
												new DecimalFormat("0.00")
														.format(Double
																.parseDouble(matzctity
																		.getActLenMin())));
								insertInpsta
										.setObject(
												28,
												new DecimalFormat("0.00")
														.format(Double
																.parseDouble(matzctity
																		.getActLenMax())));
								insertInpsta.setObject(29, "-"
										+ matzctity.getActCount());// 支数
								insertInpsta.setObject(30, "-"
										+ redWgtList.get(m));// 重量
								insertInpsta.setObject(31, matzctity
										.getActTheoryWeight());
								insertInpsta.setObject(32, matzctity
										.getBelongCode());
								insertInpsta.setObject(33, matzctity
										.getBelongName());
								insertInpsta.setObject(34, matzctity
										.getStorageNo());
								insertInpsta.setObject(35, matzctity.getMemo());
								insertInpsta.setObject(36, "1");
								insertInpsta.setObject(37, list.get(7));// 创建人
								insertInpsta.setObject(38, systime);// 创建时间
								insertInpsta.setObject(39, matzctity
										.getOrderNo());
								insertInpsta.setObject(40, matzctity
										.getOrderSeq());
								insertInpsta.setObject(41, matzctity
										.getOrderSeqNo());
								insertInpsta
										.setObject(42, matzctity.getOrdPk());
								insertInpsta.setObject(43, matzctity
										.getOrdLnPk());
								insertInpsta.setObject(44, matzctity
										.getOrdLnDlyPk());
								insertInpsta.setObject(45, matzctity
										.getProductFlag());
								insertInpsta.setObject(46, matzctity
										.getMatStatus());
								insertInpsta.setObject(47, systime);// 交易时间
								insertInpsta.setObject(48, "");// 质保ID
								if (!list.get(10).equals("")
										&& !list.get(10).equals("0")) {
									insertInpsta.setObject(49, redLengthList
											.get(m));// 长度
								} else {
									insertInpsta.setObject(49, matzctity
											.getActLen());// 长度
								}
								insertInpsta.setObject(50, matzctity
										.getDepartmentCode());
								insertInpsta.setObject(51, matzctity
										.getDepartmentDesc());
								insertInpsta.setObject(52, matzctity
										.getJudgeStdName());
								insertInpsta.setObject(53, list.get(48));
								insertInpsta.setObject(54, list.get(49));
								
								insertInpsta.setObject(55, matzctity
										.getGradecode());
								insertInpsta.setObject(56, matzctity
										.getGradename());
								insertInpsta.setObject(57, bathYear);
								insertInpsta.setObject(58, matzctity.getInstockTime());
								insertInpsta
										.setObject(59, matzctity.getLenNo());
								insertInpsta.setObject(60, matzctity
										.getLenDesc());
								insertInpsta.setObject(61, matzctity
										.getLenUnit());
								insertInpsta.setObject(62, matzctity
										.getPipmemo());
								insertInpsta.setObject(63, matzctity.getMatOriginCode());
								insertInpsta.setObject(64, matzctity.getMatOriginName());
								insertInpsta.setObject(65, matzctity.getSoureCode());
								insertInpsta.setObject(66, matzctity.getSoureName());
								insertInpsta.setObject(67, matzctity.getMaterialNo());
								insertInpsta.setObject(68, matzctity.getMaterialName());
								insertInpsta.addBatch();
								insertInpstaXs.setObject(1, matzctity
										.getManagementNo());
								insertInpstaXs.setObject(2, matzctity
										.getManagementName());
								insertInpstaXs.setObject(3, inListDoc
										+ StringTool.formatDecimal(n, "0000"));
								insertInpstaXs.setObject(4, inListDoc);
								insertInpstaXs.setObject(5, "800704");
								insertInpstaXs.setObject(6, "产出红冲");
								insertInpstaXs.setObject(7, matNo.get(j));
								insertInpstaXs.setObject(8, matzctity.getLotNo());
								insertInpstaXs.setObject(9, matzctity
										.getStoveNo());
								insertInpstaXs.setObject(10, matzctity
										.getJudgeStoveNo());
								insertInpstaXs.setObject(11, matzctity
										.getBatchNo());
								insertInpstaXs.setObject(12, matzctity
										.getBatchGroudNo());
								insertInpstaXs.setObject(13, matzctity
										.getProduccode());
								insertInpstaXs.setObject(14, matzctity
										.getProducname());
								insertInpstaXs.setObject(15, matzctity
										.getStdCode());
								insertInpstaXs.setObject(16, matzctity
										.getStdName());
								insertInpstaXs.setObject(17, matzctity
										.getSteelcode());
								insertInpstaXs.setObject(18, matzctity
										.getSteelname());
								insertInpstaXs.setObject(19, matzctity
										.getStdStyle());
								insertInpstaXs.setObject(20, matzctity
										.getStdStyleDesc());
								insertInpstaXs.setObject(21, matzctity
										.getSpecCode());
								insertInpstaXs.setObject(22, matzctity
										.getSpecName());
								insertInpstaXs.setObject(23, matzctity
										.getModelCode());
								insertInpstaXs.setObject(24, matzctity
										.getModelDesc());
								insertInpstaXs.setObject(25, matzctity
										.getActDimater());
								insertInpstaXs.setObject(26, matzctity
										.getActHeight());
								insertInpstaXs
										.setObject(
												27,
												new DecimalFormat("0.00")
														.format(Double
																.parseDouble(matzctity
																		.getActLenMin())));
								insertInpstaXs
										.setObject(
												28,
												new DecimalFormat("0.00")
														.format(Double
																.parseDouble(matzctity
																		.getActLenMax())));
								insertInpstaXs.setObject(29, "-"
										+ matzctity.getActCount());// 支数
								insertInpstaXs.setObject(30, "-"
										+ redWgtList.get(m));// 重量
								insertInpstaXs.setObject(31, matzctity
										.getActTheoryWeight());
								insertInpstaXs.setObject(32, matzctity
										.getBelongCode());
								insertInpstaXs.setObject(33, matzctity
										.getBelongName());
								insertInpstaXs.setObject(34, matzctity
										.getStorageNo());
								insertInpstaXs.setObject(35, matzctity.getMemo());
								insertInpstaXs.setObject(36, "1");
								insertInpstaXs.setObject(37, list.get(7));// 创建人
								insertInpstaXs.setObject(38, systime);// 创建时间
								insertInpstaXs.setObject(39, matzctity
										.getOrderNo());
								insertInpstaXs.setObject(40, matzctity
										.getOrderSeq());
								insertInpstaXs.setObject(41, matzctity
										.getOrderSeqNo());
								insertInpstaXs
										.setObject(42, matzctity.getOrdPk());
								insertInpstaXs.setObject(43, matzctity
										.getOrdLnPk());
								insertInpstaXs.setObject(44, matzctity
										.getOrdLnDlyPk());
								insertInpstaXs.setObject(45, matzctity
										.getProductFlag());
								insertInpstaXs.setObject(46, matzctity
										.getMatStatus());
								insertInpstaXs.setObject(47, systime);// 交易时间
								insertInpstaXs.setObject(48, "");// 质保ID
								if (!list.get(10).equals("")
										&& !list.get(10).equals("0")) {
									insertInpstaXs.setObject(49, redLengthList
											.get(m));// 长度
								} else {
									insertInpstaXs.setObject(49, matzctity
											.getActLen());// 长度
								}
								insertInpstaXs.setObject(50, matzctity
										.getDepartmentCode());
								insertInpstaXs.setObject(51, matzctity
										.getDepartmentDesc());
								insertInpstaXs.setObject(52, matzctity
										.getJudgeStdName());
								insertInpstaXs.setObject(53, list.get(48));
								insertInpstaXs.setObject(54, list.get(49));
								
								insertInpstaXs.setObject(55, matzctity
										.getGradecode());
								insertInpstaXs.setObject(56, matzctity
										.getGradename());
								insertInpstaXs.setObject(57, bathYear);
								insertInpstaXs.setObject(58, matzctity.getInstockTime());
								insertInpstaXs
										.setObject(59, matzctity.getLenNo());
								insertInpstaXs.setObject(60, matzctity
										.getLenDesc());
								insertInpstaXs.setObject(61, matzctity
										.getLenUnit());
								insertInpstaXs.setObject(62, matzctity
										.getPipmemo());
								insertInpstaXs.setObject(63, matzctity.getMatOriginCode());
								insertInpstaXs.setObject(64, matzctity.getMatOriginName());
								insertInpstaXs.setObject(65, matzctity.getSoureCode());
								insertInpstaXs.setObject(66, matzctity.getSoureName());
								insertInpstaXs.setObject(67, matzctity.getMaterialNo());
								insertInpstaXs.setObject(68, matzctity.getMaterialName());
								insertInpstaXs.addBatch();

								
								MatBcMEntity matBcEntity = new MatBcMEntity();
								matBcEntity.setJudgeStoveNo(list.get(0));// 判定炉号
								matBcEntity.setBatchNo(list.get(1));// 批号
								matBcEntity.setBatchGroudNo(list.get(2));// 组号
								matBcEntity.setMatStatus(list.get(46));// 材料状态
								matBcEntity.setStorageNo(list.get(18));// 仓库
								matBcEntity.setBalYearMonth(list.get(59));
								matBcEntity.setInstockDoc(inListDocCx);
								matBcEntity.setInstockTime(systime);
								matBcEntity.setCreateName(list.get(7));
								matBcEntity.setBalYearMonth(bathYear);
								if (!list.get(10).equals("")
										&& !list.get(10).equals("0")) {
									matBcEntity.setActLen(String.valueOf(redLengthList
											.get(m)));// 长度
								}else
								{
									matBcEntity.setActLen(matzctity
											.getActLen());
								}
								matBcEntity.setActWeight(redWgtList.get(m));
								matBcEntity.setActTheoryWeight(redWgtList.get(m));
								matBcEntity.setInstockSqe(inListDocCx
										+ StringTool.formatDecimal(b, "0000"));
								matBcEntity.setBelongCode(list.get(60));
								matBcEntity.setMatNo(matzctity.getMatNo());
								String insertMatBcSql1 = CoreXmlSqlParsersFactory.getSql("pipRedStore.insertBcM1");//逐支退制造部
								insertMatBcSql1 = EntityTool.fillSqlByEntity(insertMatBcSql1, matBcEntity);
								Statement st = connection.createStatement();
								st.executeUpdate(insertMatBcSql1);
								st.close();
							}
							QcmZgJugdeApplyEntity qcmZgEntity = new QcmZgJugdeApplyEntity();
							qcmZgEntity.setJudgeStoveNo(list.get(0));// 判定炉号
							qcmZgEntity.setBatchNo(list.get(1));// 批号
							qcmZgEntity.setBatchGroudNo(list.get(2));// 组号
							qcmZgEntity.setMatStatus(list.get(46));// 材料状态
							qcmZgEntity.setStorageNo(list.get(18));// 仓库
							qcmZgEntity.setBalYearMonth(list.get(59));
							qcmZgEntity.setInstockDoc(inListDocCx);
							qcmZgEntity.setApplyTime(systime);
							qcmZgEntity.setApplyName(list.get(7));
							qcmZgEntity.setBalYearMonth(bathYear);
							qcmZgEntity.setActCount("-" + String.valueOf(redNum));
							qcmZgEntity.setActWeight("-"+String.valueOf(redWgt));
							qcmZgEntity.setActTheoryWeight("-"+String.valueOf(redWgt));
							qcmZgEntity.setActLen("-" + String.valueOf(redLen));
							qcmZgEntity.setBelongNameJugde(list.get(60));
							String insertQcmSql = CoreXmlSqlParsersFactory.getSql("FrmPipStoreRed.insertQcm");
							insertQcmSql = EntityTool.fillSqlByEntity(insertQcmSql, qcmZgEntity);
							Statement st = connection.createStatement();
							st.executeUpdate(insertQcmSql);
							st.close();
						}
						deleteMatpsta.setString(1, matNo.get(j));
						deleteMatpstaXs.setString(1, matNo.get(j));
						deleteMatpsta.addBatch();
						deleteMatpstaXs.addBatch();

						deleteButesspsta.setString(1, matNo.get(j));
						deleteButesspsta.addBatch();
						n++;
						m++;
						b++;
					}
					if (storeNum > redNum) {
						for (int h = 0; h < matNoStore.size(); h++) {
							updateMatpsta.setString(1, storeWgtList.get(h));
							if (storeLengthList == null) {
								updateMatpsta.setString(2, "0");
							} else {
								updateMatpsta.setString(2, storeLengthList
										.get(h));
							}
							updateMatpsta.setString(3, matNoStore.get(h));
							updateMatpsta.addBatch();
						}
					}
				}
				if(Integer.parseInt(list.get(4))>0)
	            {
					updateProcess.setString(1, String.valueOf(list.get(4)));
					updateProcess.setString(2, String.valueOf(redWgt));
					updateProcess.setString(3, String.valueOf(redLen));
					updateProcess.setString(4, list.get(14));
	            }else
	            {
	            	updateProcess.setString(1, String.valueOf(list.get(4)));
					updateProcess.setString(2, "-"+String.valueOf(redWgt));
					updateProcess.setString(3, "-"+String.valueOf(redLen));
					updateProcess.setString(4, list.get(14));
	            }
				updateProcess.addBatch();
            }
            //修改计划表已缴库支数、重量
            for (int k = 0; k < parm2.size(); k++) {
                ArrayList<Object> list4 = (ArrayList<Object>) parm2.get(k);
                if(Integer.parseInt(list4.get(1).toString())>0)
                {
					updatepsta.setString(1, list4.get(3).toString());
					updatepsta.setString(2, list4.get(4).toString());
					updatepsta.setString(3, list4.get(5).toString());
					updatepsta.setString(4, list4.get(1).toString());
					updatepsta.setString(5, list4.get(3).toString());
					updatepsta.setString(6, list4.get(4).toString());
					updatepsta.setString(7, list4.get(5).toString());
					updatepsta.setString(8,
							new DecimalFormat("0.000").format(Double
									.parseDouble(list4.get(0).toString())));
					//updatepsta.setString(9, list4.get(2).toString());
					updatepsta.setString(9, list4.get(3).toString());
					updatepsta.setString(10, list4.get(4).toString());
					updatepsta.setString(11, list4.get(5).toString());
                }else
                {
                	updatepsta.setString(1, list4.get(3).toString());
					updatepsta.setString(2, list4.get(4).toString());
					updatepsta.setString(3, list4.get(5).toString());
					updatepsta.setString(4, list4.get(1).toString());
					updatepsta.setString(5, list4.get(3).toString());
					updatepsta.setString(6, list4.get(4).toString());
					updatepsta.setString(7, list4.get(5).toString());
					updatepsta.setString(8,
							new DecimalFormat("0.000").format(Double
									.parseDouble("-"+list4.get(0).toString())));
					//updatepsta.setString(9, list4.get(2).toString());
					updatepsta.setString(9, list4.get(3).toString());
					updatepsta.setString(10, list4.get(4).toString());
					updatepsta.setString(11, list4.get(5).toString());
                }
                updatepsta.addBatch();
            }
            
            insertButresspsta.executeBatch();
            insertMatpsta.executeBatch();
            insertMatpstaXs.executeBatch();
            insertInpsta.executeBatch();
            insertInpstaXs.executeBatch();
            deleteMatpsta.executeBatch();
            deleteButesspsta.executeBatch();
            updateMatpsta.executeBatch();
            updatepsta.executeBatch();
            updateProcess.executeBatch();
            connection.commit();
            cro.setResult(1);
            cro.setV_errMsg("库存红冲成功!");
        }
        catch (Exception e) {
            System.out.println(e.getMessage());
            if (connection != null) {
                try {
                	cro.setV_errMsg(e.getMessage());
                    connection.rollback(); // 回滚事务
                }
                catch (Exception e2) {
                }
            }
        }
        finally {
            try {
                if (connection != null && !connection.isClosed()) {
                    connection.close();
                }
            }
            catch (Exception e2) {
            }
        }
        return cro;
	}
	
	 /**
     * 根据炉号、批号自动组号
     * @param conn c
     * @param strLcode 传递参数
     * @return obj
     * @throws Exception 异常处理
     */
    public String queryGroupNo(Connection conn, String strLcode, String strBatch)
            throws Exception {
//        int count = 0;
        String groupNo = "";
        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()));
//            }
        	groupNo = rs.getString("BATCH_GROUD_NO");
        }
        rs.close();
        psta.close();
        //return StringTool.formatDecimal(++count, "000");
        return groupNo;
    }
	
	/**
     * 根据炉号、批号自动生成材料号
     * @param conn c
     * @param strLcode 传递参数
     * @return obj
     * @throws Exception 异常处理
     */
    public String queryMatNoM(Connection conn, String strLcode, String strBatch)
            throws Exception {
//        int count = 0;
        String matNo = "";
        PreparedStatement psta = null;
        String sqlLovsqe = CoreXmlSqlParsersFactory
                .getSql("CorePipeInTemp.QueryMatNoM1");
        psta = conn.prepareStatement(sqlLovsqe);
        psta.setString(1, strLcode);
        psta.setString(2, strBatch);
        psta.setString(3, strLcode);
        psta.setString(4, strBatch);
        psta.setString(5, strLcode);
        psta.setString(6, strBatch);
        psta.setString(7, strLcode);
        psta.setString(8, strBatch);
        ResultSet rs = psta.executeQuery();
        if (rs.next()) {
//            String res = rs.getString(1);
//            if (res == null) {
//                res = strLcode + strBatch + "0001";
//            }
//            else {
//                count = Integer.parseInt(res.substring(res.length() - 4, res
//                        .length()));
//            }
        	matNo = rs.getString("MAT_NO");
        }
        rs.close();
        psta.close();
//        return (strLcode + strBatch)
//                + StringTool.formatDecimal(++count, "0000");
        return matNo;
    }
	
	/***
	 * 撤销库存红冲
	 * @param list
	 * @return
	 */
	public CoreReturnObject pipCancelRedStore(ArrayList<String> list)
	{
		CoreReturnObject cro = new CoreReturnObject();
        Connection connection = this.getDao(CoreConstantData.sysbeanstr)
                .getConnection();
        PreparedStatement psta = null;
        ResultSet rs = null;
        Date now = new Date();
        SimpleDateFormat dateFormat = new SimpleDateFormat(
                "yyyy-MM-dd HH:mm:ss");
        SimpleDateFormat dateYear = new SimpleDateFormat(
        "yyyyMM");
        String systime = dateFormat.format(now);
        String bathYear = dateYear.format(now);
        String sql = "";
        String inListDoc = "";
        int acount = 0;
        try {
            connection.setAutoCommit(false); // 取消自动提交事务
            inListDoc = list.get(9);
//            String insertMatSql = CoreXmlSqlParsersFactory.getSql("pipCancelRedStore.insertMat");//插入材料表
//            PreparedStatement insertMatpsta = connection.prepareStatement(insertMatSql);
//            String insertButressSql = CoreXmlSqlParsersFactory.getSql("pipCancelRedStore.insertButress");//插入位置表
//            PreparedStatement insertButresspsta = connection.prepareStatement(insertButressSql);
//            String deleteInListSql = CoreXmlSqlParsersFactory.getSql("pipCancelRedStore.deleteInList");//删除入库表
//            PreparedStatement deleteInListpsta = connection.prepareStatement(deleteInListSql);
            String updatePlanSql = CoreXmlSqlParsersFactory.getSql("pipCancelRedStore.updatePlan");//修改计划表
            PreparedStatement updatePlanpsta = connection.prepareStatement(updatePlanSql);
            String updateCancelProcessSql = CoreXmlSqlParsersFactory.getSql("pipCancelRedStore.updateProcess");
            PreparedStatement cancelProcess = connection.prepareStatement(updateCancelProcessSql);//修改进程表
//            String deleteMatSql = CoreXmlSqlParsersFactory.getSql("pipCancelRedStore.deleteMat");//删除物料表
//            PreparedStatement deleteMatpsta = connection.prepareStatement(deleteMatSql);
//            String deleteButessSql = CoreXmlSqlParsersFactory.getSql("pipCancelRedStore.deleteButess");//删除位置表
//            PreparedStatement deleteButesspsta = connection.prepareStatement(deleteButessSql);
            YdmZcInlistEntity ydmInEntity = new YdmZcInlistEntity();
            ydmInEntity.setInstockDoc(inListDoc);
            ydmInEntity.setJudgeStoveNo(list.get(6));
            ydmInEntity.setBatchNo(list.get(7));
            ydmInEntity.setBatchGroudNo(list.get(8));
            ydmInEntity.setBalYearMonth(bathYear);
            ydmInEntity.setBelongCode(list.get(13));
            ydmInEntity.setBelongName(list.get(14));
			sql = CoreXmlSqlParsersFactory.getSql("pipCancelRedStore.selectMatBcPro");//查询半成品表是否已利库
			sql = EntityTool.fillSqlByEntity(sql, ydmInEntity);
			psta = connection.prepareStatement(sql);
			rs = psta.executeQuery();
			if(rs.next())
			{
				acount = Integer.parseInt(rs.getString("ACOUNT"));
			}
			rs.close();
			psta.close();
			if(acount > 0)
			{
				cro.setV_errMsg("红冲的半成品已进行利库，不允许撤销红冲!");
				return cro;
			}
            if(Integer.parseInt(list.get(1))<0)
            {
				String cancelRedSql = CoreXmlSqlParsersFactory.getSql("pipCancelRedStore.cancelRed");
				cancelRedSql = EntityTool.fillSqlByEntity(cancelRedSql, ydmInEntity);
				Statement st = connection.createStatement();
				st.executeUpdate(cancelRedSql);
				st.close();
            }
            
			if(Integer.parseInt(list.get(1))<0)
			{
				updatePlanpsta.setString(1, list.get(3).toString());
				updatePlanpsta.setString(2, list.get(4).toString());
				updatePlanpsta.setString(3, list.get(5).toString());
				updatePlanpsta.setString(4, String.valueOf(Math.abs(Integer
						.parseInt(list.get(1).toString()))));
				updatePlanpsta.setString(5, list.get(3).toString());
				updatePlanpsta.setString(6, list.get(4).toString());
				updatePlanpsta.setString(7, list.get(5).toString());
				updatePlanpsta.setString(8, new DecimalFormat("0.000")
						.format(Math.abs(Double.parseDouble(list.get(0)
								.toString()))));
				//updatePlanpsta.setString(9, list.get(2).toString());
				updatePlanpsta.setString(9, list.get(3).toString());
				updatePlanpsta.setString(10, list.get(4).toString());
				updatePlanpsta.setString(11, list.get(5).toString());
				updatePlanpsta.addBatch();
				
				cancelProcess.setString(1, String.valueOf(Math.abs(Integer.parseInt((list.get(1))))));
	            cancelProcess.setString(2, String.valueOf(Math.abs(Double.parseDouble(list.get(0)))));
	            if(list.get(10).equals("") || list.get(10).equals("0"))
	            {
	            	cancelProcess.setString(3, "0");
	            }else
	            {
	              cancelProcess.setString(3, String.valueOf(Math.abs(Double.parseDouble(list.get(10)))));
	            }
	            cancelProcess.setString(4, list.get(12));
	            cancelProcess.addBatch();
			}
			else
			{
				updatePlanpsta.setString(1, list.get(3).toString());
				updatePlanpsta.setString(2, list.get(4).toString());
				updatePlanpsta.setString(3, list.get(5).toString());
				updatePlanpsta.setString(4, "-" + list.get(1).toString());
				updatePlanpsta.setString(5, list.get(3).toString());
				updatePlanpsta.setString(6, list.get(4).toString());
				updatePlanpsta.setString(7, list.get(5).toString());
				updatePlanpsta.setString(8, "-" + new DecimalFormat("0.000")
						.format(Double.parseDouble(list.get(0)
								.toString())));
				//updatePlanpsta.setString(9, list.get(2).toString());
				updatePlanpsta.setString(9, list.get(3).toString());
				updatePlanpsta.setString(10, list.get(4).toString());
				updatePlanpsta.setString(11, list.get(5).toString());
				updatePlanpsta.addBatch();
				
				cancelProcess.setString(1, "-"+String.valueOf(Math.abs(Integer.parseInt((list.get(1))))));
	            cancelProcess.setString(2, "-"+String.valueOf(Math.abs(Double.parseDouble(list.get(0)))));
	            if(list.get(10).equals("") || list.get(10).equals("0"))
	            {
	            	cancelProcess.setString(3, "0");
	            }else
	            {
	              cancelProcess.setString(3, "-"+String.valueOf(Math.abs(Double.parseDouble(list.get(10)))));
	            }
	            cancelProcess.setString(4, list.get(12));
	            cancelProcess.addBatch();
			}
            updatePlanpsta.executeBatch();
            cancelProcess.executeBatch();
            connection.commit();
            cro.setV_errMsg("库存红冲撤销成功!");
        } catch (Exception e) {
                System.out.println(e.getMessage());
                if (connection != null) {
                    try {
                    	cro.setV_errMsg(e.getMessage());
                        connection.rollback(); // 回滚事务
                    }
                    catch (Exception e2) {
                    }
                }
            }
            finally {
                try {
                    if (connection != null && !connection.isClosed()) {
                        connection.close();
                    }
                }
                catch (Exception e2) {
                }
            }
            return cro;
	}
	
	
	/**
     * 每支材料对应的入库单号
     * @param conn c
     * @param strlist 传递参数
     * @return obj
     * @throws Exception 异常处理
     */
    public String querySendSqlDoc(Connection conn, String strlist,String memo)
            throws Exception {
//        int seq = 0;
        String inListDoc = "";
         PreparedStatement psta = null;
//         Date dt = new Date();
//         SimpleDateFormat matter1 = new SimpleDateFormat("yyyy-MM-dd");
//         SimpleDateFormat matter2 = new SimpleDateFormat("yyMMdd");
        String sqlLovsqe = CoreXmlSqlParsersFactory
                .getSql("CorePipeInTemp.getInListDocNew");
        psta = conn.prepareStatement(sqlLovsqe);
        psta.setString(1, memo);
        psta.setString(2, strlist);
        ResultSet rs = psta.executeQuery();
        if (rs.next()) {
//            String res = rs.getString(1);
//            if (res == null) {
//                res = memo + matter2.format(dt) + "00001";
//            }
//            else {
//                seq = Integer.parseInt(res.substring(8, 13));
//            }
        	inListDoc = rs.getString("INSTOCK_DOC");
        }
        rs.close();
        psta.close();
        //return  memo + matter2.format(dt) + StringTool.formatDecimal(++seq, "00000");
        return inListDoc;
    }
    
    
    /***
	 * 获取入库单号
	 * 
	 * @param conn
	 * @param strInbound
	 * @return
	 * @throws SQLException
	 */
	public String getInListDoc(Connection conn, String strInbound,String systime)
			throws SQLException {
		String inListDoc = "";
		String sql = CoreXmlSqlParsersFactory
				.getSql("FrmBcIqInList.getInListDoc");
		PreparedStatement psta = conn.prepareStatement(sql);
		psta.setString(1, strInbound);
		psta.setString(2, systime);
		ResultSet rs = psta.executeQuery();
		if (rs.next()) {
			inListDoc = rs.getString("INSTOCK_DOC");
		}
		rs.close();
		psta.close();
		return inListDoc;
	}
    
    /**
     * 每支材料对应的入库流水号
     * @param conn c
     * @param strlist 传递参数
     * @return obj
     * @throws Exception 异常处理
     */
    public String querySendSql(Connection conn, String strlist)
            throws Exception {
        String inListDocSeq = "";
         PreparedStatement psta = null;
        String sqlLovsqe = CoreXmlSqlParsersFactory
                .getSql("CorePipeInTemp.getInListDoc1");
        psta = conn.prepareStatement(sqlLovsqe);
        psta.setString(1, strlist);
        psta.setString(2, strlist);
        ResultSet rs = psta.executeQuery();
        if (rs.next()) {
        	inListDocSeq = rs.getString("INSTOCK_SQE");
        }
        rs.close();
        psta.close();
        return inListDocSeq;
    }
    
    /**
     * 每支材料对应的入库流水号
     * @param conn c
     * @param strlist 传递参数
     * @return obj
     * @throws Exception 异常处理
     */
    public String querySendSqlCx(Connection conn, String strlist)
            throws Exception {
        String inListDocSeq = "";
         PreparedStatement psta = null;
        String sqlLovsqe = CoreXmlSqlParsersFactory
                .getSql("FrmPipStoreRed.querySendSqlCx");
        psta = conn.prepareStatement(sqlLovsqe);
        psta.setString(1, strlist);
        psta.setString(2, strlist);
        ResultSet rs = psta.executeQuery();
        if (rs.next()) {
        	inListDocSeq = rs.getString("INSTOCK_SQE");
        }
        rs.close();
        psta.close();
        return inListDocSeq;
    }
    
    /***
     * 查询状态支数
     * @param list
     * @return
     */
    public CoreReturnObject getStaueCount(ArrayList<String> list)
    {
    	 return this.getDataBySqlID("FrmPipStoreRed.getStaueCount", list.toArray());
    }
}
