package com.steering.pss.ydm.pipemanage;

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;

public class FrmOutList extends DBCommon{

	/***
	 * 查询销售出库
	 * @param parm
	 * @param arr
	 * @return
	 */
	public CoreReturnObject getOutList(String startTim,String endTim,ArrayList<String> parm,String[] arr,String outType,String supplyUnit)
	{
		String sql = CoreXmlSqlParsersFactory.getSql("FrmOutList.getOutList");
		if(!startTim.equals(""))
		{
			sql += " AND OUTSTOCK_TIME>=TO_DATE('" + startTim
			+ "','YYYY-MM-DD HH24:MI:SS') AND OUTSTOCK_TIME<= TO_DATE('"
			+ endTim + "','YYYY-MM-DD HH24:MI:SS')";
		}
		if(!outType.equals(""))
		{
		    sql += "AND T.OUTSTOCK_TYPE_CODE = '"+outType+"'";	
		}else
		{
			sql += "AND T.OUTSTOCK_TYPE_CODE in ('800804','800805','800806')";
		}
		if(supplyUnit.equals("120501"))
		{
			sql += " AND (T.SUPPLY_UNIT_CODE = '"+supplyUnit+"' OR T.SUPPLY_UNIT_CODE IS NULL)";
		}else if(supplyUnit.equals("120504")){
			sql += " AND T.SUPPLY_UNIT_CODE = '"+supplyUnit+"'";
		}
		sql += CoreConverter.toSqlConditionString("T.BELONG_CODE", arr);
		sql += " group by T.BAL_YEAR_MONTH,T.JUDGE_STOVE_NO,T.BATCH_NO,T.BATCH_GROUD_NO,T.MATERIAL_NO,T.MATERIAL_NAME,T.PRODUCCODE,T.PRODUCNAME,";
        sql += "T.STD_CODE,T.STD_NAME,T.GRADECODE,T.GRADENAME,T.STEELCODE,T.STEELNAME,T.STD_STYLE,T.STD_STYLE_DESC,T.SPEC_CODE,T.SPEC_NAME,T.MODEL_CODE,T.MODEL_DESC,T.PLINE_CODE,";
        sql += "T.PLINE_NAME, T.ACT_DIMATER,T.ACT_HEIGHT,T.ACT_LEN_MIN,T.ACT_LEN_MAX,T.BELONG_CODE,T.BELONG_NAME,T.STORAGE_NO,T.PRO_ORDER_NO,";
        
        sql += " T.ORD_PK,T.ORD_LN_PK,T.ORD_LN_DLY_PK,";
        sql += "T.ORDER_NO,T.ORDER_SEQ,T.DELIVERY_NO,T.JUDGE_STD_NAME,T.JUDGE_STD_CODE,T.PONDER_NO,T.OUTSTOCK_PLAN_NO,T.OUTSTOCK_DOC,T.OUTSTOCK_TYPE_CODE,T.OUTSTOCK_TYPE_NAME,T.VEHICLE_NO,s.storage_name";
		return this.getData(sql, parm.toArray());
	}
	
	/***
	 * 修改结转年月
	 * @param list
	 * @return
	 */
	public CoreReturnObject updateYear(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 {
			connection.setAutoCommit(false); // 取消自动提交事务
			String updateYearSql = CoreXmlSqlParsersFactory.getSql("FrmOutList.updateYear");
			PreparedStatement updateYearPsta = connection.prepareStatement(updateYearSql);
			for(int i = 0;i<parm.size();i++)
			{
				ArrayList<String> list = parm.get(i);
				updateYearPsta.setString(1, list.get(2));
				updateYearPsta.setString(2, list.get(3));
				updateYearPsta.setString(3, list.get(4));
				updateYearPsta.setString(4, list.get(5));
				updateYearPsta.setString(5, list.get(6));
				updateYearPsta.setString(6, list.get(7));
				updateYearPsta.setString(7, list.get(0));
				updateYearPsta.setString(8, list.get(1));
				updateYearPsta.setString(9, list.get(0));
				updateYearPsta.addBatch();
			}
			updateYearPsta.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;
	}
}
