package com.steering.ydm.bc;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.ArrayList;

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.common.EntityTool;
import com.steering.mes.mcp.entity.MatBcMEntity;
import com.steering.mes.mcp.entity.YdmBcStowlistEntity;

public class FrmBcStow extends DBCommon{
	/***
	 * 查询库存信息
	 * @return
	 */
	public CoreReturnObject doQueryMat(ArrayList<String> parmList,String[] storageArr)
	{
		String sql = CoreXmlSqlParsersFactory.getSql("FrmBcStow.doQueryData");
		if(!parmList.get(0).equals(""))
		{
			String strIn = " 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') ";
			sql = sql.replace("#INSTOCK_TIME#", strIn);
		}else
		{
			sql = sql.replace("#INSTOCK_TIME#", "");
		}
		if(!parmList.get(2).equals(""))
		{
			String strJudgeNo = " AND T.JUDGE_STOVE_NO = '"+parmList.get(2)+"'";
			sql = sql.replace("#JUDGE_STOVE_NO#", strJudgeNo);
		}else
		{
			sql = sql.replace("#JUDGE_STOVE_NO#", "");
		}
		String storageNo = CoreConverter.toSqlConditionString("S.STORAGE_NO", storageArr);
		sql = sql.replace("#STORAGE_NO#", storageNo);
		return this.getData(sql);
	}

	/***
	 * 查询倒垛实绩
	 * @return
	 */
	public CoreReturnObject doQueryStow(ArrayList<String> parmList,String[] storageArr)
	{ 
		String sql = CoreXmlSqlParsersFactory.getSql("FrmBcStow.doQueryStow");
		sql += CoreConverter.toSqlConditionString("T.STORAGE_NO", storageArr);
		if(!parmList.get(0).equals(""))
		{
			sql += " AND T.STOW_TIME >=TO_DATE('" + parmList.get(0)
			+ "', 'YYYY-MM-DD HH24:MI:SS')AND T.STOW_TIME <=TO_DATE('"
			+ parmList.get(1) + "', 'YYYY-MM-DD HH24:MI:SS') ";
		}
		if(!parmList.get(2).equals(""))
		{
			sql += " AND T.JUDGE_STOVE_NO = '"+parmList.get(2)+"'";
		}
		sql += " GROUP BY T.LOT_NO,T.STOVE_NO,T.JUDGE_STOVE_NO,T.BATCH_NO,T.BATCH_GROUD_NO,T.PLINE_CODE,T.PLINE_NAME,T.ACT_LEN_MIN,T.ACT_LEN_MAX,";
		sql += " T.BELONG_CODE,T.BELONG_NAME,T.LOCATION_NO,T.LARY_NO,T.OLD_LOCATION_NO,T.OLD_LARY_NO,T.STORAGE_NO,T.STOW_TIME,T.STOW_REASON,t.FLAG_STOVE_NO_ZG";
		return this.getData(sql);
	}
	/***
	 * 查询该炉库位信息
	 * @return
	 */
	public CoreReturnObject queryStow(ArrayList<String> parmList,String barCode)
	{
		String sql = "";
		if(barCode.equals("0"))
		{
			sql = CoreXmlSqlParsersFactory.getSql("FrmBcStow.queryBcStow");
		}else
		{
			sql = CoreXmlSqlParsersFactory.getSql("FrmBcStow.queryZcStow");
		}
		return this.getData(sql, parmList.toArray());
	}
	
	/***
     * 获取所有料位号
     * @param stroeNo
     * @return
     */
    public CoreReturnObject getLocationNo(String stroeNo)
    {
    	return this.getDataBySqlID("FrmBcStow.getLocationNo", new Object[]{stroeNo});
    }
    public CoreReturnObject doQueryStowReson()
    {
    	return this.getDataBySqlID("FrmBcStow.doQueryStowReson");
    }
    /***
     * 倒垛
     * @param list
     * @return
     */
    public CoreReturnObject doAddLocationNo(String matGpTity,String locationNo,String laryNo,ArrayList<ArrayList<String>> parm)
    {
    	CoreReturnObject cro = new CoreReturnObject();
        Connection connection = this.getDao(CoreConstantData.sysbeanstr)
                .getConnection();
        PreparedStatement psta = null;
        String sql = "";
        ResultSet rs = null;
        String acount = "";
        try {
        	ArrayList<String> sqllist = new ArrayList<String>();
			MatBcMEntity matBcEnTity = EntityTool.getEntityByJSONStr(matGpTity, MatBcMEntity.class);
			ArrayList<String> list = new ArrayList<String>();
			for(int i= 0;i<parm.size();i++)
			{
				list = parm.get(i);
				YdmBcStowlistEntity ydmStowEntity = new YdmBcStowlistEntity();
				ydmStowEntity.setJudgeStoveNo(matBcEnTity.getJudgeStoveNo());
				ydmStowEntity.setBatchNo(matBcEnTity.getBatchNo());
				ydmStowEntity.setBatchGroudNo(matBcEnTity.getBatchGroudNo());
				ydmStowEntity.setPlineCode(matBcEnTity.getPlineCode());
				ydmStowEntity.setStorageNo(matBcEnTity.getStorageNo());
				ydmStowEntity.setOldLocationNo(locationNo);
				ydmStowEntity.setOldLaryNo(laryNo);
				ydmStowEntity.setActCount(list.get(0));
				ydmStowEntity.setStowBc(list.get(3));
				ydmStowEntity.setStowBz(list.get(4));
				ydmStowEntity.setLocationNo(list.get(1));
				ydmStowEntity.setLaryNo(list.get(2));
				ydmStowEntity.setLocNo("");
				ydmStowEntity.setCreateName(list.get(5));
				ydmStowEntity.setManagementNo(list.get(6));
				ydmStowEntity.setManagementName(list.get(7));
				ydmStowEntity.setDepartmentCode(list.get(8));
				ydmStowEntity.setDepartmentDesc(list.get(9));
				ydmStowEntity.setBarCode(matBcEnTity.getBarCode());
				ydmStowEntity.setStowReason(list.get(11));
				ydmStowEntity.setProductFlag(list.get(12));
				ydmStowEntity.setFlagStoveNoZg(list.get(13));
				String insertStowSql = CoreXmlSqlParsersFactory.getSql("FrmBcStow.doAddLocationNo");
				insertStowSql = EntityTool.fillSqlByEntity(insertStowSql, ydmStowEntity);
				sqllist.add(insertStowSql);
			}
			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;
    }
}
