package com.steering.pss.qcm;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;

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

import com.steering.comm.db.DBCommon;
import com.steering.comm.tools.StringTool;

public class WaterRatioRStd extends DBCommon {

    public CoreReturnObject getStd() {
        return this.getDataBySqlID("CoreWaterRatioRStd.GetStd");
    }

    public CoreReturnObject getStdReplace() {
        return this.getDataBySqlID("CoreWaterRatioRStd.GetStdReplace");
    }

    public CoreReturnObject query(Boolean isValid) {
        Object [] obj = new Object[]{ isValid ? "0":""};
        return this.getDataBySqlID("CoreWaterRatioRStd.QUERY",obj);
    }

    @SuppressWarnings("unchecked")
    public CoreReturnObject save(ArrayList<Object> parm, ArrayList<Object> parmU) {

        CoreReturnObject cro = new CoreReturnObject();
        ArrayList<String> sqlIDlist = new ArrayList<String>();
        ArrayList<Object[]> params = new ArrayList<Object[]>();

        for (int i = 0; i < parm.size(); i++) {

            ArrayList<Object> list = (ArrayList<Object>) parm.get(i);
            String strsql = CoreXmlSqlParsersFactory
                    .getSql("CoreWaterRatioRStd.IsExistStdAdd");
            Object[] obj = new Object[] { list.get(1).toString(),
                    list.get(3).toString(), list.get(1).toString() };
            try {
                ResultSet rs = this.getDao(CoreConstantData.sysbeanstr).ExceuteQueryForResultSet(
                        strsql, obj);
                if (rs.next()) {
                    cro.setV_errMsg("FALSE###");
                    return cro;
                }
            }
            catch (SQLException e) {
                CoreIComponent.logAdapter.filelogger.logError(e.getMessage());// 将服务端的错误信息写入日志文件
            }
            list.set(0, list.get(1).toString() + list.get(3).toString());
            sqlIDlist.add(CoreXmlSqlParsersFactory
                    .getSql("CoreWaterRatioRStd.AddStd"));
            params.add(list.toArray());
        }

        for (int i = 0; i < parmU.size(); i++) {
            ArrayList<Object> list = new ArrayList<Object>();
            list = (ArrayList<Object>) parmU.get(i);

            // 一个标准只能引用一个水压标准,一个水压标准可被多个标准引用
            String strsql = CoreXmlSqlParsersFactory
                    .getSql("CoreWaterRatioRStd.IsExistStdUpdate");
            Object[] obj = new Object[] { list.get(1).toString(),list.get(11),
                    list.get(3).toString(), list.get(1).toString(),list.get(11)};
            try {
                ResultSet rs = this.getDao(CoreConstantData.sysbeanstr).ExceuteQueryForResultSet(
                        strsql, obj);
                if (rs.next()) {
                    cro.setV_errMsg("FALSE###");
                    return cro;
                }
            }
            catch (SQLException e) {
                CoreIComponent.logAdapter.filelogger.logError(e.getMessage());// 将服务端的错误信息写入日志文件
            }
            list.remove(list.size()-1);
            list.add(list.get(0).toString());
            list.set(0, list.get(1).toString() + list.get(3).toString());
            params.add(list.toArray());
            sqlIDlist.add(CoreXmlSqlParsersFactory
                    .getSql("CoreWaterRatioRStd.UpdateStd"));
        }

        cro = this.getDao(CoreConstantData.sysbeanstr).ExcuteTransction(sqlIDlist, params);
        return cro;
    }

    /**
     * 作废
     * @param id 水压系数
     * @return 平台统一返回对象
     */
    public CoreReturnObject delete(ArrayList<String> listId) {
    	ArrayList<String> sqllist = new ArrayList<String>();
    	for(String id : listId){
	    	String sql = CoreXmlSqlParsersFactory.getSql("CoreWaterRatioRStd.DeleteStd");
	    	sql = StringTool.ReplaceAll(sql, "#ID_#", id);
	    	sqllist.add(sql);
    	}
    	
    	return this.getDao(CoreConstantData.sysbeanstr).ExcuteTransction(sqllist);
    }
}
