package com.steering.pss.ydm.pipemanage;

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

public class FrmFirmInList extends DBCommon{
	
	/***
	 * 获取产线代码
	 * @param custom
	 * @return
	 */
	public CoreReturnObject getPline(String custom)
	{
		return this.getDataBySqlID("FrmFirmInList.getPline",new Object[]{custom});
	}

	/***
	 * 查询入库实绩
	 * @param list
	 * @param arr
	 * @param startTim
	 * @param endTim
	 * @return
	 */
	public CoreReturnObject queryInListData(ArrayList<String> list,String[] arr,String startTim,String endTim)
	{
		String sql = CoreXmlSqlParsersFactory
				.getSql("FrmFirmInList.queryInListData");
		sql += CoreConverter.toSqlConditionString("t.PLINE_CODE", arr);
		if (!startTim.equals("")) {
			sql += " AND INSTOCK_TIME>=TO_DATE('" + startTim
					+ "','YYYY-MM-DD HH24:MI:SS') AND INSTOCK_TIME<= TO_DATE('"
					+ endTim + "','YYYY-MM-DD HH24:MI:SS')";
		}
		sql += " group by t.JUDGE_STOVE_NO,t.BATCH_NO,t.INSTOCK_DOC,t.PRODUCCODE,t.PRODUCNAME,t.STD_CODE,t.STD_NAME,t.STEELCODE,t.STEELNAME,t.STD_STYLE,t.STD_STYLE_DESC,t.SPEC_CODE,t.SPEC_NAME,t.JUDGE_STD_CODE,t.JUDGE_STD_NAME,t.GRADECODE,t.GRADENAME,t.BAL_YEAR_MONTH,t.PIPMEMO,t.BATCH_GROUD_NO,t.INSTOCK_TYPE_NAME,T.HAND_FLAG,";
		sql += " t.MODEL_DESC,t.ACT_DIMATER,t.ACT_HEIGHT,t.ACT_LEN_MIN,t.ACT_LEN_MAX,t.BELONG_CODE,t.BELONG_NAME,s.STORAGE_NAME,t.MEMO,t.CREATE_NAME,t.ORDER_NO,t.ORDER_SEQ,t.DELIVERY_NO,t.MODEL_CODE,t.PRODUCT_FLAG,t.ZB_RCDID,t.PLINE_CODE,t.PLINE_NAME,d.SALE_AREA_DESC,d.create_name,t.AFFIRM_LOG,t.SUPPLY_UNIT,t.IS_ALL_SCRAP";
		sql += " order by t.instock_doc desc";

		return this.getData(sql, list.toArray());
	}
	
	/***
	 * 审核缴库实绩
	 * @param parm
	 * @return
	 */
	public CoreReturnObject updateAffimLog(ArrayList<ArrayList<String>> parm)
	{
		CoreReturnObject cro = new CoreReturnObject();
        Connection connection = this.getDao(CoreConstantData.sysbeanstr)
                .getConnection();
        PreparedStatement psta = null;
        String sql = "";
        try {
        	 connection.setAutoCommit(false); // 取消自动提交事务
        	 sql  = CoreXmlSqlParsersFactory.getSql("FrmFirmInList.updateAffimLog");
        	 psta = connection.prepareStatement(sql);
        	for(int i = 0;i< parm.size();i++)
        	{
        		ArrayList<String> list = parm.get(i);
        		psta.setString(1, list.get(0));
        		psta.addBatch();
        	}
        	psta.executeBatch();
        	connection.commit();
        	cro.setV_errMsg("审核成功!");
        } catch (SQLException 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 parm
	 * @return
	 */
	public CoreReturnObject cancelUpdateAffimLog(ArrayList<ArrayList<String>> parm)
	{
		CoreReturnObject cro = new CoreReturnObject();
        Connection connection = this.getDao(CoreConstantData.sysbeanstr)
                .getConnection();
        PreparedStatement psta = null;
        String sql = "";
        try {
        	 connection.setAutoCommit(false); // 取消自动提交事务
        	 sql  = CoreXmlSqlParsersFactory.getSql("FrmFirmInList.cancelUpdateAffimLog");
        	 psta = connection.prepareStatement(sql);
        	for(int i = 0;i< parm.size();i++)
        	{
        		ArrayList<String> list = parm.get(i);
        		psta.setString(1, list.get(0));
        		psta.addBatch();
        	}
        	psta.executeBatch();
        	connection.commit();
        	cro.setV_errMsg("反审成功!");
        } catch (SQLException 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;
	}
}
