package com.steering.pss.ydm.Report;


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 java.sql.Connection;
import java.sql.PreparedStatement;
import java.util.ArrayList;

public class FrmRunTimeRptCw extends DBCommon{
	
	
	public CoreReturnObject getQueryData(String yearMoth,String[] arr,String supplyUnit)
	{
	    String strSupplyUnit = "";
        if(supplyUnit.equals("120501"))
        {
            strSupplyUnit = " AND (T.SUPPLY_UNIT_CODE = '"+supplyUnit+"' OR T.SUPPLY_UNIT_CODE IS NULL)";
        }else if(supplyUnit.equals("120504")){
            strSupplyUnit = " AND T.SUPPLY_UNIT_CODE = '"+supplyUnit+"'";
        }
		String sql = CoreXmlSqlParsersFactory.getSql("FrmRunTimeRptCw.getQueryData");
		String str = CoreConverter.toSqlConditionString("t.BELONG_CODE", arr);
		sql = sql.replace("#CONDTIONS#", str + strSupplyUnit);
		sql = sql.replace("#BAL_YEAR_MONTH#", yearMoth);
		return this.getData(sql);
	}

    public CoreReturnObject getQueryData1(String yearMoth,String[] arr,String supplyUnit)
    {
        String sql = CoreXmlSqlParsersFactory.getSql("FrmRunTimeRptCw.getQueryData1");
        sql = sql.replace("#BAL_YEAR_MONTH#", yearMoth);
        return this.getData(sql);
    }
	
	
	/**
     * 获取期初重量
     * @param strWhere
     * @return
     */
    public CoreReturnObject getOriginalLast(String time,String strWhere,String[] arr)
    {
    	String sql = CoreXmlSqlParsersFactory.getSql("FrmRunTimeRptCw.getOriginalLast").replace("#condtions#", strWhere);
    	sql += CoreConverter.toSqlConditionString("t.BELONG_CODE", arr);
    	sql += " group by t.JUDGE_STOVE_NO,t.BATCH_NO,t.BATCH_GROUD_NO,T.PRODUCNAME,T.STD_STYLE_DESC,T.STEELNAME,T.SPEC_NAME,T.MODEL_DESC";
    	return this.getData(sql,new Object[]{time});
    }
    
    /***
     * 本期入库量
     * @param strOriginal
     * @param
     * @param
     * @param strWhere
     * @return
     */
    public CoreReturnObject getThisIn(String strOriginal,String strWhere,String[] arr)
    {
    	String sql = CoreXmlSqlParsersFactory.getSql("FrmRunTimeRptCw.getThisIn").replace("#condtions#", strWhere);
    	sql += CoreConverter.toSqlConditionString("t.BELONG_CODE", arr);
    	sql += " group by t.JUDGE_STOVE_NO,t.BATCH_NO,t.BATCH_GROUD_NO,T.PRODUCNAME,T.STD_STYLE_DESC,T.STEELNAME,T.SPEC_NAME,T.MODEL_DESC";
    	return this.getData(sql, new Object[]{strOriginal,strOriginal,strOriginal});
    }
    
    /***
     * 本期出库量
     * @param strOriginal
     * @param
     * @param
     * @param strWhere
     * @return
     */
    public CoreReturnObject getThisOut(String strOriginal,String strWhere,String[] arr)
    {
    	String sql = CoreXmlSqlParsersFactory.getSql("FrmRunTimeRptCw.getThisOut").replace("#condtions#", strWhere);
    	sql = sql.replace("#condtions1#", strWhere);
    	sql += CoreConverter.toSqlConditionString("t.BELONG_CODE", arr);
    	sql += " group by t.JUDGE_STOVE_NO,t.BATCH_NO,t.BATCH_GROUD_NO,T.PRODUCNAME,T.STD_STYLE_DESC,T.STEELNAME,T.SPEC_NAME,T.MODEL_DESC";
    	return this.getData(sql, new Object[]{strOriginal,strOriginal,strOriginal});
    }

    
    /***
     * 结转期初数据
     * @param list
     * @return
     */
    public CoreReturnObject insertInital(ArrayList<ArrayList<String>> list,String bathYear,String []arr,String custom)
    {
    	CoreReturnObject cro = new CoreReturnObject();
        Connection connection = this.getDao(CoreConstantData.sysbeanstr).getConnection();
         try {
             connection.setAutoCommit(false); // 取消自动提交事务
             String insertInitalsql = CoreXmlSqlParsersFactory.getSql("FrmRunTimeRptCw.insertInital1_");
             PreparedStatement insertInitalpsta = connection.prepareStatement(insertInitalsql);
             insertInitalpsta.setString(1, bathYear);
             insertInitalpsta.setString(2, bathYear);
             insertInitalpsta.addBatch();
             insertInitalpsta.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
     * @return
     */
    public CoreReturnObject getIntalJZ(String bathYear,String[] arr,String custom)
    {
    	String sql = CoreXmlSqlParsersFactory.getSql("FrmRunTimeRptCw.getIntalJZ");
    	sql += CoreConverter.toSqlConditionString("T.BELONG_CODE", arr);
    	return this.getData(sql, new Object[]{bathYear,custom});
    }
    
    public CoreReturnObject doQueryTail(String bathYear,String[] arr)
    {
    	String sql = CoreXmlSqlParsersFactory.getSql("FrmRunTimeRptCw.doQueryTail");
    	sql += CoreConverter.toSqlConditionString("T.BELONG_CODE", arr);
    	return this.getData(sql, new Object[]{bathYear});
    }
}
