package com.steering.pss.ydm.Stuffmanage;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;

import CoreFS.SA06.CoreReturnObject;
import CoreFS.SA08.CoreXmlSqlParsersFactory;
import CoreFS.SC01.CoreConstantData;

import com.steering.comm.converter.CoreConverter;
import com.steering.comm.db.DBCommon;

public class FrmStuffStow extends DBCommon{

	/***
	 * 查询倒垛实绩
	 * @param parm
	 * @param startTim
	 * @param endTim
	 * @param arr
	 * @return
	 */
	public CoreReturnObject doQueryStow(ArrayList<String> parm,String startTim,String endTim)
	{
		String sql = CoreXmlSqlParsersFactory.getSql("FrmStuffStow.doQueryStow");
		if(!startTim.equals(""))
		{
			sql += " AND STOW_TIME >= to_date('"+startTim+"','YYYY-MM-DD HH24:MI:SS') AND STOW_TIME <= to_date('"+endTim+"','YYYY-MM-DD HH24:MI:SS')";
		}
		
		sql += " group by GROUD_NO,PRO_ORDER_NO,PRO_ORDER_ID,PLINE_CODE,PLINE_NAME,BELONG_CODE,BELONG_NAME, LOCATION_NO,MEMO,PRODUCT_FLAG,MAT_STATUS,STOVE_NO,FLAG_STOVE_NO,";
		sql += " LOT_NO,JUDGE_STOVE_NO,BATCH_NO,CAST_NO,CCMID,SAMPLE_FLAG,CUTTING_FLAG,CUTTING_NUM,MATERIAL_NO, MATERIAL_NAME,PRODUCCODE,PRODUCNAME,JUDGE_GRADECODE,JUDGE_GRADENAME,";
		sql += " JUDGE_STD_CODE,JUDGE_STD_NAME,ACT_DIMATER,ACT_LEN_MIN,ACT_LEN_MAX,ACT_FIX_NUM,ACT_FIX_LEN";
		return this.getData(sql, parm.toArray());
	}
	
	/**
     * 查询材料位置信息
     * @param list 传递参数
     * @return 平台统一返回对象
     */
    public CoreReturnObject queryStow(ArrayList<String> list) {
        return this.getDataBySqlID("FrmStuffStow.queryStow", list.toArray());
    }
    
    /***
     * 获取所有料位号
     * @param stroeNo
     * @return
     */
    public CoreReturnObject getLocationNo(String stroeNo)
    {
    	return this.getDataBySqlID("FrmStuffStow.getLocationNo", new Object[]{stroeNo});
    }
    /***
     * 倒垛
     * @param list
     * @return
     */
    public CoreReturnObject doAddLocationNo(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 = "";
        String location = "";
        String laryNo = "";//层号
        String locNo = "";//位置
        String storeNo = "";//仓库号
        try {
			connection.setAutoCommit(false); // 取消自动提交事务
			String insertStowSql = CoreXmlSqlParsersFactory.getSql("FrmStuffStow.doAddLocationNo");
			PreparedStatement insertStowPsta = connection.prepareStatement(insertStowSql);
			ArrayList<String> list = new ArrayList<String>();
			for(int i= 0;i<parm.size();i++)
			{
				list = (ArrayList<String>)parm.get(i);
				location = list.get(7);//新料位号
				storeNo = list.get(0);
				laryNo = location.substring(storeNo.length(), 2);
				locNo = location.substring(laryNo.length(), 2);
				insertStowPsta.setString(1, list.get(1));
				insertStowPsta.setString(2, list.get(2));
				insertStowPsta.setString(3, list.get(3));
				insertStowPsta.setString(4, list.get(4));
				insertStowPsta.setString(5, list.get(0));
				insertStowPsta.setString(6, list.get(5));
				insertStowPsta.setInt(7, Integer.parseInt(list.get(6))+1);
				insertStowPsta.setString(8, list.get(8));
				insertStowPsta.setString(9, list.get(9));
				insertStowPsta.setString(10, list.get(7));
				insertStowPsta.setString(11, laryNo);
				insertStowPsta.setString(12, locNo);
				
				insertStowPsta.setString(13, list.get(13));
				insertStowPsta.setString(14, list.get(14));
				insertStowPsta.setString(15, list.get(11));
				insertStowPsta.setString(16, list.get(12));
				
				insertStowPsta.setString(17, list.get(10));
				insertStowPsta.setString(18, list.get(7));
				insertStowPsta.setString(19, laryNo);
				insertStowPsta.setString(20, locNo);
				insertStowPsta.addBatch();
			}
			insertStowPsta.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;
    }
}
