package com.steering.ydm.bc;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
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.converter.EntityTool;
import com.steering.comm.db.DBCommon;
import com.steering.common.StringTool;
import com.steering.mes.mcp.entity.YdmBcButtressEntity;
import com.steering.mes.mcp.entity.YdmBcInlistEntity;
import com.steering.mes.mcp.entity.YdmBcOutlistEntity;
import com.steering.mes.mcp.entity.YdmBcScrapoutlistEntity;


public class FrmFilpInStorage extends DBCommon{

	/***
     * 根据数据权限绑定仓库
     * @return
     */
    public CoreReturnObject doQueryStorageArr(String[] arr)
    {
    	String sql = CoreXmlSqlParsersFactory.getSql("FrmFilpInStorage.doQueryStorageArr");
    	sql += CoreConverter.toSqlConditionString("T.STORAGE_NO", arr);
    	sql += " ORDER BY T.STORAGE_NO ASC";
    	return this.getData(sql);
    }
    /***
     * 查询出库信息(读卡)
     * @return
     */
    public CoreReturnObject doQueryOutList(String outDoc)
    {
    	return this.getDataBySqlID("FrmFilpInStorage.doQueryOutList", new Object[]{outDoc});
    }
    /***
     * 查询出库信息
     * @return
     */
    public CoreReturnObject doQueryOutList(ArrayList<String> parmList,String[] arr)
    {
    	String sql = CoreXmlSqlParsersFactory.getSql("FrmFilpInStorage.doQueryOutList1");
    	sql += CoreConverter.toSqlConditionString("T.TAR_STORAGE_NO", arr);
    	if(!parmList.get(0).equals(""))
    	{
    		sql += " AND OUTSTOCK_TIME >= TO_DATE('"
				+ parmList.get(0)
				+ "','YYYY-MM-DD HH24:MI:SS') AND OUTSTOCK_TIME <= TO_DATE('"
				+ parmList.get(1) + "','YYYY-MM-DD HH24:MI:SS')";
    	}
    	if(!parmList.get(2).equals(""))
    	{
			sql += CoreConverter.toSqlConditionString("T.JUDGE_STOVE_NO", parmList.get(2).replace("，",",").split(","));
    		//sql += " AND T.JUDGE_STOVE_NO = '"+parmList.get(2)+"'";
    	}
    	if(!parmList.get(3).equals(""))
    	{
    		sql += " AND T.OUTSTOCK_DOC = '"+parmList.get(3)+"'";
    	}
    	if(!parmList.get(4).equals(""))
		{
			sql += " AND T.FLAG_STOVE_NO_ZG = '"+parmList.get(4)+"'";
		}
    	sql += " GROUP BY T.OUTSTOCK_DOC,T.ICCAR_NO,T.VEHICLE_NO,T.STOVE_NO,T.JUDGE_STOVE_NO,T.BATCH_NO,T.BATCH_GROUD_NO,T.JUDGE_STD_NAME,T.ACT_LEN_MIN,T.ACT_LEN_MAX,T.PIPMEMO,T.MEMO,";
    	sql += " T.PRO_ORDER_NO,T.PLINE_CODE,T.PLINE_NAME,T.BELONG_CODE,T.BELONG_NAME,T.STORAGE_NO,T.TAR_STORAGE_NO,T.DATA_SUORCE,T.QCM_STAUS,T.TAR_REC_FLAG,T.MATERIAL_NO,T.MATERIAL_NAME";
    	return this.getData(sql);
    }
    
    /***
     * 查询收料信息
     * @return
     */
    public CoreReturnObject douQueryInList(ArrayList<String> parmList,String[] arr)
    {
    	String sql = CoreXmlSqlParsersFactory.getSql("FrmFilpInStorage.douQueryInList");
    	sql += CoreConverter.toSqlConditionString("T.STORAGE_NO", arr);
    	if(!parmList.get(0).equals(""))
    	{
    		sql += " AND T.INSTOCK_TIME >= TO_DATE('"
				+ parmList.get(0)
				+ "','YYYY-MM-DD HH24:MI:SS') AND T.INSTOCK_TIME <= TO_DATE('"
				+ parmList.get(1) + "','YYYY-MM-DD HH24:MI:SS')";
    	}
    	if(!parmList.get(4).equals(""))
    	{
    		sql += " AND T.OUTSTOCK_TIME >= TO_DATE('"
    				+ parmList.get(4)
    				+ "','YYYY-MM-DD HH24:MI:SS') AND T.OUTSTOCK_TIME <= TO_DATE('"
    				+ parmList.get(5) + "','YYYY-MM-DD HH24:MI:SS')";
    	}
    	if(!parmList.get(2).equals(""))
    	{
    		sql += " AND T.JUDGE_STOVE_NO = '"+parmList.get(2)+"'";
    	}
    	if(!parmList.get(3).equals(""))
    	{
    		sql += " AND T.INSTOCK_DOC = '"+parmList.get(3)+"'";
    	}
    	if(!parmList.get(6).equals(""))
		{
			sql += " AND T.FLAG_STOVE_NO_ZG = '"+parmList.get(6)+"'";
		}
    	sql += " GROUP BY T.INSTOCK_DOC,T.ICCAR_NO,T.VEHICLE_NO,T.STOVE_NO,T.JUDGE_STOVE_NO,T.BATCH_NO,T.BATCH_GROUD_NO,T.PRO_ORDER_NO,T.JUDGE_STD_NAME,T.ACT_LEN_MIN,T.ACT_LEN_MAX,";
    	sql += " T.PLINE_CODE,T.PLINE_NAME,T.BELONG_CODE,T.BELONG_NAME,T.STORAGE_NO,T.LOCATION_NO,T.LARY_NO,T.DATA_SUORCE,T.QCM_STAUS,T.M_BILL_NO,T.MATERIAL_NO,T.MATERIAL_NAME";
    	return this.getData(sql);
    }
    /***
     * 查询库位信息
     * @param storageNo
     * @return
     */
    public CoreReturnObject doQueryLocation(String storageNo)
    {
    	return this.getDataBySqlID("FrmFilpInStorage.doQueryLocation", new Object[]{storageNo});
    }
    /***
     * 获取过磅重量
     * @param parm
     * @return
     */
    public CoreReturnObject getPossWgt(ArrayList<String> parm)
    {
    	CoreReturnObject cro = new CoreReturnObject();
        Connection connection = this.getDao(CoreConstantData.sysbeanstr)
                .getConnection();
        Connection msCon = this.getDao("JLDATEDao").getConnection();
        PreparedStatement psta = null;
        String sql = "";
        ResultSet rs = null;
        String acount = "";
        Date now = new Date();
		SimpleDateFormat dateFormat = new SimpleDateFormat(
				"yyyy-MM-dd HH:mm:ss");
		String systime = dateFormat.format(now);
		String possWgt = "";//过磅重量
		int outCount = 0;
        try {
        	ArrayList<String> sqllist = new ArrayList<String>();
        	for(String str : parm)
        	{
        		YdmBcInlistEntity ydmBcEntity = EntityTool.getEntityByJSONStr(str, YdmBcInlistEntity.class);
        		
        		sql = CoreXmlSqlParsersFactory.getSql("FrmFilpInStorage.selectOutPossWgt");
    			sql = EntityTool.fillSqlByEntity(sql, ydmBcEntity);
    			psta = msCon.prepareStatement(sql);
    			rs = psta.executeQuery();
    			if(rs.next())
    			{
    				possWgt = rs.getString("SUTTLE");
    			}
    			rs.close();
    			psta.close();
    			if(possWgt.equals(""))
    			{
    				cro.setV_errMsg("物流老系统中未读到过磅重量,请核实!");
    				return cro;
    			}
    			ydmBcEntity.setPonderGrossWt(possWgt);
    			ArrayList<String> matList = new ArrayList<String>();
				sql = CoreXmlSqlParsersFactory.getSql("FrmFilpInStorage.selectOutMatNo");//查询所有材料号
				sql = EntityTool.fillSqlByEntity(sql, ydmBcEntity);
				psta = connection.prepareStatement(sql);
				rs = psta.executeQuery();
				while(rs.next())
				{
					matList.add(rs.getString("MAT_NO"));
				}
				rs.close();
				psta.close();
				
				sql = CoreXmlSqlParsersFactory.getSql("FrmFilpInStorage.selectOutCount");//查询出库单号下支数
				sql = EntityTool.fillSqlByEntity(sql, ydmBcEntity);
				psta = connection.prepareStatement(sql);
				rs = psta.executeQuery();
				if(rs.next())
				{
					outCount = Integer.parseInt(rs.getString("ACOUNT"));
				}
				rs.close();
				psta.close();
				
				
				List<double[]> list = MeanValueCalculation.MeanCalculation(
                        Double.parseDouble(possWgt), outCount);
				int s = 0;
				if(matList != null && matList.size() == outCount)
				{
					for (int j = 0; j < list.size(); j++) {
						double[] conW = list.get(j);
						if ((int) conW[0] >= 0) {
							for (int z = 0; z < (int) conW[0]; z++) {
								YdmBcInlistEntity ydmBcMatEntity = new YdmBcInlistEntity();
								ydmBcMatEntity.setMatNo(matList.get(s));
								ydmBcMatEntity.setOutstockDoc(ydmBcEntity.getOutstockDoc());
								ydmBcMatEntity.setPonderGrossWt(String.valueOf(conW[1]));
								//ydmBcMatEntity.setInstockDoc(ydmBcEntity.getInstockDoc());
								String updatePossWgtSql = CoreXmlSqlParsersFactory.getSql("FrmFilpInStorage.getPossWgt");
								updatePossWgtSql = EntityTool.fillSqlByEntity(updatePossWgtSql, ydmBcMatEntity);
								sqllist.add(updatePossWgtSql);
								s++;
							}
						}
					}
				}

				String updatePonWgtSql  = CoreXmlSqlParsersFactory.getSql("FrmFilpInStorage.UpdateScrapWgt");
				updatePonWgtSql = EntityTool.fillSqlByEntity(updatePonWgtSql, ydmBcEntity);
				sqllist.add(updatePonWgtSql);
        	}
        	cro = this.getDao(CoreConstantData.sysbeanstr).ExcuteTransction(sqllist);
        	if(cro.getV_errMsg().equals(""))
        	{
        		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;
    }
    /***
     * 收料作业
     * @return
     */
    public CoreReturnObject doAddInListData(ArrayList<String> parm,ArrayList<ArrayList<String>> locationList,String inSort,String userName,String departId,String departMent,String depart,String departM,String[] arr,String userOrder,String userGroup,String storageNo,ArrayList<String> possList)
    {
    	CoreReturnObject cro = new CoreReturnObject();
        Connection connection = this.getDao(CoreConstantData.sysbeanstr)
                .getConnection();
		Connection msCon = null;
        //Connection msCon = this.getDao("JLDATEDao").getConnection();
//		TZH 24.4.24 注销物流接口
//        Connection tmsCon = this.getDao("TmsDao").getConnection();
        PreparedStatement psta = null;
        String sql = "";
        ResultSet rs = null;
        String acount = "";
        Date now = new Date();
		SimpleDateFormat dateFormat = new SimpleDateFormat(
				"yyyy-MM-dd HH:mm:ss");
		String systime = dateFormat.format(now);
		String bathYear = StringTool.GetBalYear(now,StringTool.GetSupplyUnit(EntityTool
						.getEntityByJSONStr(possList.get(0), YdmBcOutlistEntity.class).getPlineCode())); //改25号结转
		String possWgt = "";//过磅重量
		int outCount = 0;
		String wayStaus = "";//运单状态
		String isCarUser = "";
		String wayNo = "";//运单号
		String isUnLoading = "";//卸点是否需要吊车
		int aCount = 0;
		String areaType = "";//根据出库仓库、收料仓库区分区域
		String belongCode = "";
		String belongName = "";
		String goWhereCode = "";//去向
		String goWhereName = "";
		String sourceCode = "";//来源
		String sourceName = "";
        try {
//    		TZH 24.4.24 注销物流接口
//        	tmsCon.setAutoCommit(false);
        	ArrayList<String> sqllist = new ArrayList<String>();
        	String inDoc = getInDoc(connection);//获取入库单号
			String inDocCq = getInDocCq(connection);//获取产权入库单号
			String outDocCq = getOutDocCq(connection);//获取产权出库单号

        	for(String str : possList){
				YdmBcOutlistEntity ydmPossEntity = EntityTool
						.getEntityByJSONStr(str, YdmBcOutlistEntity.class);
				areaType = queryStoreArea(connection,ydmPossEntity.getOutstockDoc(),storageNo);
				if(areaType.equals("1"))
				{
					msCon = this.getDao("JLDATEDao").getConnection();
					sql = CoreXmlSqlParsersFactory
							.getSql("FrmFilpInStorage.selectOutPossWgt");
					sql = EntityTool.fillSqlByEntity(sql, ydmPossEntity);
					psta = msCon.prepareStatement(sql);
					rs = psta.executeQuery();
					if (rs.next()) {
						possWgt = rs.getString("SUTTLE");
					}
					rs.close();
					psta.close();
					msCon.close();
				}else if(areaType.equals("2"))
				{
					belongCode = "002001001";
					belongName = "生产计划部";
					goWhereCode = "120501";
					goWhereName = "天津钢管制造有限公司";
					sourceCode = "120504";
					sourceName = "江苏天淮钢管有限公司";
				}else if(areaType.equals("3"))
				{
					belongCode = "002023005";
					belongName = "产品制造部";
					goWhereCode = "120504";
					goWhereName = "江苏天淮钢管有限公司";
					sourceCode = "120501";
					sourceName = "天津钢管制造有限公司";

				}

				sql = CoreXmlSqlParsersFactory.getSql("FrmFilpInStorage.selectOutCount");//查询出库单号下支数
				sql = EntityTool.fillSqlByEntity(sql, ydmPossEntity);
				psta = connection.prepareStatement(sql);
				rs = psta.executeQuery();
				if(rs.next())
				{
					outCount = Integer.parseInt(rs.getString("ACOUNT"));
				}
				rs.close();
				psta.close();

				if(!String.valueOf(outCount).equals(ydmPossEntity.getActCount()))
				{
					cro.setV_errMsg("同一出库单必须一次性收料!");
					return  cro;
				}

				if (possWgt != null && possWgt != "") {
					ArrayList<String> matList = new ArrayList<String>();
					sql = CoreXmlSqlParsersFactory
							.getSql("FrmFilpInStorage.selectOutMatNo");// 查询所有材料号
					sql = EntityTool.fillSqlByEntity(sql, ydmPossEntity);
					psta = connection.prepareStatement(sql);
					rs = psta.executeQuery();
					while (rs.next()) {
						matList.add(rs.getString("MAT_NO"));
					}
					rs.close();
					psta.close();


					
					List<double[]> list = MeanValueCalculation.MeanCalculation(
							Double.parseDouble(possWgt), outCount);
					int s = 0;
					if(matList != null && matList.size() == outCount)
					{
						for (int j = 0; j < list.size(); j++) {
							double[] conW = list.get(j);
							if ((int) conW[0] >= 0) {
								for (int z = 0; z < (int) conW[0]; z++) {
									YdmBcOutlistEntity ydmBcMatEntity = new YdmBcOutlistEntity();
									ydmBcMatEntity.setMatNo(matList.get(s));
									ydmBcMatEntity.setOutstockDoc(ydmPossEntity
											.getOutstockDoc());
									ydmBcMatEntity.setPonderGrossWt(String
											.valueOf(conW[1]));
									String updatePossWgtSql = CoreXmlSqlParsersFactory
											.getSql("FrmFilpInStorage.UpdatePossWgt");
									updatePossWgtSql = EntityTool.fillSqlByEntity(
											updatePossWgtSql, ydmBcMatEntity);
									sqllist.add(updatePossWgtSql);
									s++;
								}
							}
						}
					}

				}

				

			}
			for(int i = 0;i<parm.size();i++)
			{
				YdmBcOutlistEntity ymdOutlist = EntityTool.getEntityByJSONStr(parm.get(i), YdmBcOutlistEntity.class);
				if(ymdOutlist.getQcmStaus().equals("合格"))
				{
					ymdOutlist.setQcmStaus("00");
				}else if(ymdOutlist.getQcmStaus().equals("待处理"))
				{
					ymdOutlist.setQcmStaus("11");
				}else if(ymdOutlist.getQcmStaus().equals("判废"))
				{
					ymdOutlist.setQcmStaus("22");
				}
				
				
				sql = CoreXmlSqlParsersFactory.getSql("FrmFilpInStorage.selectOutListData");
				sql = EntityTool.fillSqlByEntity(sql, ymdOutlist);
				psta = connection.prepareStatement(sql);
				rs = psta.executeQuery();
				if(rs.next())
				{
					acount = rs.getString("ACOUNT");
				}
				rs.close();
				psta.close();
				if(Integer.parseInt(acount)>0)
				{
					cro.setV_errMsg("此炉"+ymdOutlist.getJudgeStoveNo()+"已进行过收料,不允许重复收料!");
					return cro;
				}
				
				sql = CoreXmlSqlParsersFactory.getSql("FrmFilpOutStorage.SelectStorageIsCarUser");
				psta = connection.prepareStatement(sql);
				psta.setString(1,ymdOutlist.getStorageNo());
				rs = psta.executeQuery();
				if(rs.next())
				{
					isCarUser = rs.getString("IS_CAR_TAKING");
				}
				rs.close();
				psta.close();

				if(ymdOutlist.getOutType().equals("写卡倒运") && isCarUser.equals("1") || ymdOutlist.getOutType().equals("票据倒运") && isCarUser.equals("1"))
				{
					if(!ymdOutlist.getQcmStaus().equals("22"))
					{
                        //判断运单状态
//						TZH 24.4.24 注销物流接口
//						sql = CoreXmlSqlParsersFactory.getSql("FrmFilpOutStorage.selectWayBillStaus");

//						psta = tmsCon.prepareStatement(sql);
//						psta.setString(1,ymdOutlist.getOutstockDoc());
//						rs = psta.executeQuery();
//						if(rs.next())
//						{
//							wayStaus = rs.getString("VALIDFLAG");
//							wayNo = rs.getString("WAYBILL_NO");
//						}
//						rs.close();
//						psta.close();
						if(!wayStaus.equals(""))
						{
							if(Integer.parseInt(wayStaus) < 50)
							{
								cro.setV_errMsg("司机运单未扫码到达，不允许进行打卡收料!");
								return cro;
							}
						}

						//判断收料点是否需要吊车
						sql = CoreXmlSqlParsersFactory.getSql("FrmFilpInStorage.selectUnLoadingFlag");
						psta = connection.prepareStatement(sql);
						if(storageNo.equals(""))
						{
							psta.setString(1,ymdOutlist.getTarStorageNo());
						}
						else
						{
							psta.setString(1,storageNo);
						}
						rs = psta.executeQuery();
						if(rs.next())
						{
							isUnLoading = rs.getString("IS_CRANE_UNLOADING");
						}
						rs.close();
						psta.close();

						if(!wayStaus.equals(""))
						{
							if(Integer.parseInt(wayStaus) < 50)
							{
								if(isUnLoading.equals("1"))
								{
//									TZH 24.4.24 注销物流接口
//									sql = CoreXmlSqlParsersFactory.getSql("FrmFilpOutStorage.selectTmsRelation");
//									psta = tmsCon.prepareStatement(sql);
//									psta.setString(1,wayNo);
//									if(storageNo.equals(""))
//									{
//										psta.setString(2,ymdOutlist.getTarStorageNo());
//									}else
//									{
//										psta.setString(2,storageNo);
//									}
//
//									rs = psta.executeQuery();
//									if(rs.next())
//									{
//										aCount = Integer.parseInt(rs.getString("ACOUNT"));
//									}
//									rs.close();
//									psta.close();
									if(aCount == 0)
									{
										cro.setV_errMsg("打卡收料前需吊车扫拖车司机运单二维码!");
										return  cro;
									}
								}
							}
						}

					}
				}
//				TZH 24.4.24 注销物流接口
//				String insertTmsOutSql = CoreXmlSqlParsersFactory.getSql("FrmFilpInStorage.insertTmsDoc");
//				psta = tmsCon.prepareStatement(insertTmsOutSql);
//				psta.setString(1,ymdOutlist.getOutstockDoc());
//				psta.setString(2,ymdOutlist.getOutstockDoc());
//				psta.setString(3,inDoc);
//				psta.setString(4,possWgt);
//
//				psta.executeUpdate();
//				psta.close();
//				
				/***
				 * 废品收料时 生成废品出库单
				 */

				if(ymdOutlist.getQcmStaus().equals("22"))
				{
					if(!storageNo.equals(""))
					{

						if(storageNo.equals("LYY1")||storageNo.equals("LFG1")||storageNo.equals("LFG2"))
						{
							YdmBcScrapoutlistEntity ydmScrapEntity = new YdmBcScrapoutlistEntity();
							ydmScrapEntity.setOutstockDoc(ymdOutlist.getOutstockDoc());
							ydmScrapEntity.setCrateName(userName);
							ydmScrapEntity.setCrateTime(systime);
							ydmScrapEntity.setBalYearMonth(bathYear);
							//ydmScrapEntity.setPonderGrossWt(possWgt);

							String insertScrapSql = CoreXmlSqlParsersFactory.getSql("FrmFilpInStorage.doAddBcScrap");
							insertScrapSql = EntityTool.fillSqlByEntity(insertScrapSql, ydmScrapEntity);
							sqllist.add(insertScrapSql);
						}
					}else
					{
						if(ymdOutlist.getTarStorageNo().equals("LYY1")||ymdOutlist.getTarStorageNo().equals("LFG1")||ymdOutlist.getTarStorageNo().equals("LFG2"))
						{
							YdmBcScrapoutlistEntity ydmScrapEntity = new YdmBcScrapoutlistEntity();
							ydmScrapEntity.setOutstockDoc(ymdOutlist.getOutstockDoc());
							ydmScrapEntity.setCrateName(userName);
							ydmScrapEntity.setCrateTime(systime);
							ydmScrapEntity.setBalYearMonth(bathYear);
							//ydmScrapEntity.setPonderGrossWt(possWgt);

							String insertScrapSql = CoreXmlSqlParsersFactory.getSql("FrmFilpInStorage.doAddBcScrap");
							insertScrapSql = EntityTool.fillSqlByEntity(insertScrapSql, ydmScrapEntity);
							sqllist.add(insertScrapSql);
						}
					}

				}
				if(locationList.size()>0)
				{
					for (int k = 0; k < locationList.get(i).size(); k++) {

						YdmBcButtressEntity ydmGpButtessEntity = EntityTool.getEntityByJSONStr(locationList.get(i).get(k), YdmBcButtressEntity.class);
						
						YdmBcInlistEntity ydmInTity = new YdmBcInlistEntity();
						ydmInTity.setInstockDoc(inDoc);
						ydmInTity.setInstockTypeCode(inSort);
						ydmInTity.setStoveNo(ymdOutlist.getStoveNo());
						ydmInTity.setJudgeStoveNo(ymdOutlist.getJudgeStoveNo());
						ydmInTity.setBatchNo(ymdOutlist.getBatchNo());
						ydmInTity.setBatchGroudNo(ymdOutlist.getBatchGroudNo());
						ydmInTity.setOutstockDoc(ymdOutlist.getOutstockDoc());
						ydmInTity.setDepartmentCode(departId);
						ydmInTity.setDepartmentDesc(departMent);
						ydmInTity.setManagementNo(depart);
						ydmInTity.setManagementName(departM);
						ydmInTity.setCreateName(userName);
						ydmInTity.setStorageNo(ymdOutlist.getStorageNo());
						ydmInTity.setTarStorageNo(ydmGpButtessEntity.getStorageNo());
						ydmInTity.setTarLocationNo(ydmGpButtessEntity.getLocationNo());
						ydmInTity.setTarLaryNo(ydmGpButtessEntity.getLaryNo());
						ydmInTity.setActCount(ydmGpButtessEntity.getActCount());
						ydmInTity.setInBc(userOrder);
						ydmInTity.setInBz(userGroup);
						ydmInTity.setCreateTime(systime);
						ydmInTity.setBelongCode(ymdOutlist.getBelongCode());
						
						ydmInTity.setMaterialNo(ymdOutlist.getMaterialNo());
						ydmInTity.setProduccode(ymdOutlist.getProduccode());
						ydmInTity.setSteelcode(ymdOutlist.getSteelcode());
						ydmInTity.setSpecCode(ymdOutlist.getSpecCode());
						ydmInTity.setModelCode(ymdOutlist.getModelCode());
						ydmInTity.setStdStyle(ymdOutlist.getStdStyle());
						ydmInTity.setStdCode(ymdOutlist.getStdCode());
						ydmInTity.setJudgeStdName(ymdOutlist.getJudgeStdName());
						ydmInTity.setActLenMin(ymdOutlist.getActLenMin());
						ydmInTity.setActLenMax(ymdOutlist.getActLenMax());
						ydmInTity.setInBc(userOrder);
						ydmInTity.setInBz(userGroup);
						ydmInTity.setBalYearMonth(bathYear);

						
						String insertInListSql = CoreXmlSqlParsersFactory.getSql("FrmFilpInStorage.doAddInListData");
						insertInListSql = EntityTool.fillSqlByEntity(insertInListSql, ydmInTity);
						sqllist.add(insertInListSql);


						if(areaType.equals("2")||areaType.equals("3"))
						{
							YdmBcOutlistEntity ydmBcOutEntity = new YdmBcOutlistEntity();
							ymdOutlist.setOutstockDoccq(outDocCq);
							ymdOutlist.setInstockDoc(inDocCq);
							ymdOutlist.setCreateName(userName);
							ymdOutlist.setCreateTime(systime);
							ymdOutlist.setTarStorageNo(ydmGpButtessEntity.getStorageNo());
							ymdOutlist.setTarLocationNo(ydmGpButtessEntity.getLocationNo());
							ymdOutlist.setBelongCode(belongCode);
							ymdOutlist.setBelongName(belongName);
							ymdOutlist.setMatGowhereCode(goWhereCode);
							ymdOutlist.setMatGowhereName(goWhereName);
							ymdOutlist.setSoureCode(sourceCode);
							ymdOutlist.setSoureName(sourceName);
							ymdOutlist.setBalYearMonth(bathYear);

							String insertCqSql = CoreXmlSqlParsersFactory.getSql("FrmFilpInStorage.doAddInListDataCq");
							insertCqSql = EntityTool.fillSqlByEntity(insertCqSql, ymdOutlist);
							sqllist.add(insertCqSql);
						}

					}
				}else
				{
					YdmBcInlistEntity ydmInTity = new YdmBcInlistEntity();
					ydmInTity.setInstockDoc(inDoc);
					ydmInTity.setInstockTypeCode(inSort);
					ydmInTity.setStoveNo(ymdOutlist.getStoveNo());
					ydmInTity.setJudgeStoveNo(ymdOutlist.getJudgeStoveNo());
					ydmInTity.setBatchNo(ymdOutlist.getBatchNo());
					ydmInTity.setBatchGroudNo(ymdOutlist.getBatchGroudNo());
					ydmInTity.setOutstockDoc(ymdOutlist.getOutstockDoc());
					ydmInTity.setDepartmentCode(departId);
					ydmInTity.setDepartmentDesc(departMent);
					ydmInTity.setManagementNo(depart);
					ydmInTity.setManagementName(departM);
					ydmInTity.setCreateName(userName);
					ydmInTity.setStorageNo(ymdOutlist.getStorageNo());
					if(!storageNo.equals(""))
					{
						ydmInTity.setTarStorageNo(storageNo);
						ydmInTity.setTarLocationNo(storageNo + "10101");
					}
					else
					{
						ydmInTity.setTarStorageNo(ymdOutlist.getTarStorageNo());
						ydmInTity.setTarLocationNo(ymdOutlist.getTarStorageNo() + "10101");
					}
					ydmInTity.setTarLaryNo("1");
					ydmInTity.setActCount(ymdOutlist.getActCount());
					ydmInTity.setInBc(userOrder);
					ydmInTity.setInBz(userGroup);
					ydmInTity.setCreateTime(systime);
					ydmInTity.setBelongCode(ymdOutlist.getBelongCode());
					ydmInTity.setMaterialNo(ymdOutlist.getMaterialNo());
					ydmInTity.setProduccode(ymdOutlist.getProduccode());
					ydmInTity.setSteelcode(ymdOutlist.getSteelcode());
					ydmInTity.setSpecCode(ymdOutlist.getSpecCode());
					ydmInTity.setModelCode(ymdOutlist.getModelCode());
					ydmInTity.setStdStyle(ymdOutlist.getStdStyle());
					ydmInTity.setStdCode(ymdOutlist.getStdCode());
					ydmInTity.setJudgeStdName(ymdOutlist.getJudgeStdName());
					ydmInTity.setActLenMin(ymdOutlist.getActLenMin());
					ydmInTity.setActLenMax(ymdOutlist.getActLenMax());
					ydmInTity.setInBc(userOrder);
					ydmInTity.setInBz(userGroup);
					ydmInTity.setBalYearMonth(bathYear);
					
					String insertInListSql = CoreXmlSqlParsersFactory.getSql("FrmFilpInStorage.doAddInListData");
					insertInListSql = EntityTool.fillSqlByEntity(insertInListSql, ydmInTity);
					sqllist.add(insertInListSql);


					if(areaType.equals("2")||areaType.equals("3"))
					{
						//YdmBcOutlistEntity ydmBcOutEntity = new YdmBcOutlistEntity();
						ymdOutlist.setOutstockDoccq(outDocCq);
						ymdOutlist.setInstockDoc(inDocCq);
						ymdOutlist.setCreateName(userName);
						ymdOutlist.setCreateTime(systime);
						if(!storageNo.equals(""))
						{
							ymdOutlist.setTarStorageNo(storageNo);
							ymdOutlist.setTarLocationNo(storageNo + "10101");
						}
						else
						{
							ymdOutlist.setTarStorageNo(ymdOutlist.getTarStorageNo());
							ymdOutlist.setTarLocationNo(ymdOutlist.getTarStorageNo() + "10101");
						}
						ymdOutlist.setBelongCode(belongCode);
						ymdOutlist.setBelongName(belongName);
						ymdOutlist.setMatGowhereCode(goWhereCode);
						ymdOutlist.setMatGowhereName(goWhereName);
						ymdOutlist.setSoureCode(sourceCode);
						ymdOutlist.setSoureName(sourceName);
						ymdOutlist.setBalYearMonth(bathYear);
						String insertCqSql = CoreXmlSqlParsersFactory.getSql("FrmFilpInStorage.doAddInListDataCq");
						insertCqSql = EntityTool.fillSqlByEntity(insertCqSql, ymdOutlist);
						sqllist.add(insertCqSql);
					}
				}
				
			}
			cro = this.getDao(CoreConstantData.sysbeanstr).ExcuteTransction(sqllist);
			if(cro.getV_errMsg().equals(""))
			{
//				TZH 24.4.24 注销物流接口
//				tmsCon.commit();
				cro.setV_errMsg("收料成功!");
			}
			
			
        }
	   	catch (Exception e) {
            System.out.println(e.getMessage());
//			TZH 24.4.24 注销物流接口
//            if (connection != null && tmsCon !=null) 
            	 if (connection != null)
            {
                try {
                	cro.setV_errMsg(e.getMessage());
                    connection.rollback(); // 回滚事务
//    				TZH 24.4.24 注销物流接口
//					tmsCon.rollback();
                }
                catch (Exception e2) {
                }
            }
        }
        finally {
            try {
                if (connection != null && !connection.isClosed()) {
                    connection.close();
                }
//				if (tmsCon != null && !tmsCon.isClosed()) {
//					tmsCon.close();
//				}
            }
            catch (Exception e2) {
            }
        }
        return cro;
    }

	/***
	 * 查询出库单的出库仓库、收料仓库区域
	 * @param connection
	 * @param outstockDoc
	 * @return
	 */
	private String queryStoreArea(Connection connection, String outstockDoc,String tarStorageNo) throws SQLException {
		String flag = "";
		PreparedStatement ps = null;
		ResultSet rs = null;
		String outTarStorageNo = "";
		String storageNoArea = "";//出库仓库所属区域
		String tarStorageArea = "";//收料仓库所属区域
		String sql = CoreXmlSqlParsersFactory.getSql("FrmFilpInStorage.queryCkStoreArea");
		ps = connection.prepareStatement(sql);
		ps.setString(1,outstockDoc);
		rs = ps.executeQuery();
		if(rs.next())
		{
			storageNoArea = rs.getString("SEAM_AREA");
			outTarStorageNo = rs.getString("TAR_STORAGE_NO");
		}
		rs.close();
		ps.close();


		String sql1 = CoreXmlSqlParsersFactory.getSql("FrmFilpOutStorage.queryStoreArea");
		ps = connection.prepareStatement(sql1);
		if(tarStorageNo.equals(""))
		{
			ps.setString(1,outTarStorageNo);
		}else
		{
			ps.setString(1,tarStorageNo);
		}

		rs = ps.executeQuery();
		if(rs.next())
		{
			tarStorageArea = rs.getString("SEAM_AREA");
		}
		rs.close();
		ps.close();
		if(storageNoArea.equals("0") && tarStorageArea.equals("0"))//本部库之间的倒运
		{
			flag = "1";
		}else if(storageNoArea.equals("1") && tarStorageArea.equals("0"))//天淮发往本部
		{
			flag = "2";
		}else if (storageNoArea.equals("0") && tarStorageArea.equals("1"))//本部发往天淮
		{
			flag = "3";
		}else
		{
			flag = "0";
		}
		return  flag;
	}

	/***
     * 撤销收料
     * @return
     */
    public CoreReturnObject deleteData(ArrayList<String> parmList,String userName)
    {
    	CoreReturnObject cro = new CoreReturnObject();
        Connection connection = this.getDao(CoreConstantData.sysbeanstr)
                .getConnection();
        PreparedStatement psta = null;
        String sql = "";
        ResultSet rs = null;
        String acount = "";
        Date now = new Date();
		SimpleDateFormat dateFormat = new SimpleDateFormat(
				"yyyy-MM-dd HH:mm:ss");
		String systime = dateFormat.format(now);
        try {
        	ArrayList<String> sqllist = new ArrayList<String>();
			for(String str:parmList)
			{
				YdmBcInlistEntity ymdInList = EntityTool.getEntityByJSONStr(str, YdmBcInlistEntity.class);
				if(ymdInList.getQcmStaus().equals("合格"))
				{
					ymdInList.setQcmStaus("00");
				}else if(ymdInList.getQcmStaus().equals("待处理"))
				{
					ymdInList.setQcmStaus("11");
				}else if(ymdInList.getQcmStaus().equals("判废"))
				{
					ymdInList.setQcmStaus("22");
				}
				if(!ymdInList.getQcmStaus().equals("22"))
				{
					if(ymdInList.getDataSuorce().equals("0"))
					{
						sql = CoreXmlSqlParsersFactory.getSql("FrmFilpInStorage.selectInListData");
					}else 
					{
						sql = CoreXmlSqlParsersFactory.getSql("FrmFilpInStorage.selectInListDataZc");
					}
					
					sql = EntityTool.fillSqlByEntity(sql, ymdInList);
					psta = connection.prepareStatement(sql);
					rs = psta.executeQuery();
					if(rs.next())
					{
						acount = rs.getString("ACOUNT");
					}
					rs.close();
					psta.close();
					if(!acount.equals(ymdInList.getActCount()))
					{
						cro.setV_errMsg("物料已上料或者已倒运出去或者库位已改变,不允许撤销!");
						return cro;
					}
				}
				
				ymdInList.setCreateTime(systime);
				ymdInList.setCreateName(userName);
				String deleteInListSql = CoreXmlSqlParsersFactory.getSql("FrmFilpInStorage.deleteInListData");
				deleteInListSql = EntityTool.fillSqlByEntity(deleteInListSql, ymdInList);
				sqllist.add(deleteInListSql);
			}
			cro = this.getDao(CoreConstantData.sysbeanstr).ExcuteTransction(sqllist);
			if(cro.getV_errMsg().equals(""))
			{
				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;
	
    }
    
    /***
	 * 生成入库单号
	 * @return
	 * @throws SQLException 
	 */
	public String getInDoc(Connection con)
	{
		String inDoc = "";
		String sql = CoreXmlSqlParsersFactory.getSql("FrmFilpInStorage.getInDoc");
		PreparedStatement psta = null;
		try {
			psta = con.prepareStatement(sql);
			ResultSet rs = psta.executeQuery();
			if(rs.next())
			{
				inDoc = rs.getString("INSTOCK_DOC");
			}
			rs.close();
			psta.close();
		} catch (SQLException e) {
			
			e.printStackTrace();
		}
		return inDoc;
	}

	/***
	 * 生成产权入库单号
	 * @return
	 * @throws SQLException
	 */
	public String getInDocCq(Connection con)
	{
		String inDoc = "";
		String sql = CoreXmlSqlParsersFactory.getSql("FrmFilpInStorage.getInDocCq");
		PreparedStatement psta = null;
		try {
			psta = con.prepareStatement(sql);
			ResultSet rs = psta.executeQuery();
			if(rs.next())
			{
				inDoc = rs.getString("INSTOCK_DOC");
			}
			rs.close();
			psta.close();
		} catch (SQLException e) {

			e.printStackTrace();
		}
		return inDoc;
	}

	/***
	 * 生成产权出库单号
	 * @return
	 * @throws SQLException
	 */
	public String getOutDocCq(Connection con)
	{
		String outDoc = "";
		String sql = CoreXmlSqlParsersFactory.getSql("FrmFilpInStorage.getOutDocCq");
		PreparedStatement psta = null;
		try {
			psta = con.prepareStatement(sql);
			ResultSet rs = psta.executeQuery();
			if(rs.next())
			{
				outDoc = rs.getString("OUTSTOCK_DOC");
			}
			rs.close();
			psta.close();
		} catch (SQLException e) {

			e.printStackTrace();
		}
		return outDoc;
	}
}
