package com.steering.pss.plnsaleord.stove;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.SQLException;
import java.text.DecimalFormat;
import java.util.ArrayList;
import CoreFS.SA06.CoreReturnObject;
import CoreFS.SA08.CoreXmlSqlParsersFactory;
import CoreFS.SC01.CoreConstantData;

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

/**
 * 炉计划通用方法
 * @author cheng
 *
 */
public class StoveBase extends DBCommon{
	public CoreReturnObject addNewRow(String jsonsString,String SqlID,String entityClasString) {
		CoreReturnObject returnObject =null;
		Class<?> entityObj = null;
		try {
			entityObj=Class.forName(entityClasString);
			Object object =EntityTool.getEntityByJSONStr(jsonsString, entityObj.newInstance().getClass());
			String sqlString =EntityTool.fillSqlByEntity(CoreXmlSqlParsersFactory.getSql(SqlID), object);
			returnObject=this.setData(sqlString);
		} catch (Exception e) {
			// TODO: handle exception
			returnObject.SetErrInfo(e.getMessage());
		}
		return returnObject;
	}
	
	public CoreReturnObject queryAll(String tablenameString) {
		CoreReturnObject returnObject =null;
		String sqlString = String.format("select * from %s ", tablenameString);
		returnObject=this.getData(sqlString);
		return returnObject;
	}
	
	
	public CoreReturnObject queryAll(String tablenameString,String PlineCode,String OrdNo,String ProPlanId,String[] szStatus) {
		CoreReturnObject returnObject =null;
		boolean isfirstparam=true;
		
		String sqlString = String.format("select * from %s ", tablenameString);
		if(!StringTool.isNullOrEmpty(PlineCode))
		{
			sqlString  += String.format(" where PLINE_CODE = '%s' ", PlineCode);
			isfirstparam=false;
		}
		if(!StringTool.isNullOrEmpty(OrdNo))
		{
			sqlString += isfirstparam ? String.format(" where ORDER_NO = '%s' ", OrdNo) : String.format(" and ORDER_NO = '%s' ", OrdNo);
			isfirstparam=false;
		}
		if(!StringTool.isNullOrEmpty(ProPlanId))
		{
			
			sqlString += isfirstparam ? String.format(" where PRO_PLAN_ID = '%s' ", ProPlanId) : String.format(" and PRO_PLAN_ID = '%s' ", ProPlanId);
			isfirstparam=false;
		}
		String statusString="";
		for (String status : szStatus) {
			statusString += String.format(" EXE_STATUS = '%s' Or ", status);
		}
		statusString = statusString.substring(0, statusString.lastIndexOf("Or"));
		sqlString += isfirstparam?String.format(" where %s", statusString):String.format(" and (%s)", statusString);
		returnObject=this.getData(sqlString);
		return returnObject;
	}
	
	
	public CoreReturnObject queryOrderS(String sqlId,String PlineCode,String OrdNo,String ProPlanId,String gxPlanNo,String[] szStatus) {
		CoreReturnObject returnObject = null;
		String sqlString = CoreXmlSqlParsersFactory.getSql(sqlId);
		boolean isfirstparam=true;
		if(!StringTool.isNullOrEmpty(PlineCode))
		{
			sqlString  += String.format(" where PLINE_CODE = '%s' ", PlineCode);
			isfirstparam=false;
		}
		if(!StringTool.isNullOrEmpty(OrdNo))
		{
			sqlString += isfirstparam ? String.format(" where ORDER_NO = '%s' ", OrdNo) : String.format(" and ORDER_NO = '%s' ", OrdNo);
			isfirstparam=false;
		}
		if(!StringTool.isNullOrEmpty(ProPlanId))
		{
			sqlString += isfirstparam ? String.format(" where PRO_PLAN_ID = '%s' ", ProPlanId) : String.format(" and PRO_PLAN_ID = '%s' ", ProPlanId);
			isfirstparam=false;
		}
		if(!StringTool.isNullOrEmpty(gxPlanNo))
		{
			sqlString +=isfirstparam ? String.format(" where GX_PLAN_NO = '%s' ", gxPlanNo) : String.format(" and GX_PLAN_NO = '%s' ", gxPlanNo);
		}
		String statusString="";
		for (String status : szStatus) {
			statusString += String.format(" EXE_STATUS = '%s' Or ", status);
		}
		statusString = statusString.substring(0, statusString.lastIndexOf("Or"));
		sqlString += isfirstparam?String.format(" where %s", statusString):String.format(" and (%s)", statusString);
		returnObject=this.getData(sqlString);
		return returnObject;
	}
	
	public CoreReturnObject queryZy(String sqlId) {
		CoreReturnObject returnObject = null;
		returnObject = this.getDataBySqlID(sqlId);
		return returnObject;
	}
	
	public CoreReturnObject queryZy(String sqlId,String Pro_PLan_ID,String Gx_Plan_No) {
		CoreReturnObject returnObject = null;
		String sqlString = CoreXmlSqlParsersFactory.getSql(sqlId);
		boolean isfirstparam=true;
		if(!StringTool.isNullOrEmpty(Pro_PLan_ID))
		{
			sqlString  += String.format(" where Pro_PLan_ID = '%s' ", Pro_PLan_ID);
			isfirstparam=false;
		}
		if(!StringTool.isNullOrEmpty(Gx_Plan_No))
		{
			sqlString += isfirstparam ? String.format(" where Gx_Plan_No = '%s' ", Gx_Plan_No) : String.format(" and Gx_Plan_No = '%s' ", Gx_Plan_No);
			isfirstparam=false;
		}
		sqlString += " ORDER BY CREATE_TIME desc";
		returnObject=this.getData(sqlString);
		return returnObject;
	}
	
	public CoreReturnObject queryZy(String sqlId,String StoveNo,String BatchNo,String BatchGroupNo,String ProPlanId,String GxPlanNo) {
		CoreReturnObject returnObject = null;
		String sqlString = CoreXmlSqlParsersFactory.getSql(sqlId);
		boolean isfirstparam=true;
		if(!StringTool.isNullOrEmpty(ProPlanId))
		{
			sqlString  += String.format(" where Pro_PLan_ID = '%s' ", ProPlanId);
			isfirstparam = false;
		}
		if(!StringTool.isNullOrEmpty(GxPlanNo))
		{
			sqlString += isfirstparam ? String.format(" where Gx_Plan_No = '%s' ", GxPlanNo) : String.format(" and Gx_Plan_No = '%s' ", GxPlanNo);
			isfirstparam = false;
		}
		if(!StringTool.isNullOrEmpty(StoveNo))
		{
			sqlString += isfirstparam ? String.format(" where HEATNO_LAST = '%s' ", StoveNo) : String.format(" and HEATNO_LAST = '%s' ", StoveNo);
			isfirstparam = false;
		}
		if(!StringTool.isNullOrEmpty(BatchNo) )
		{
			if(!sqlId.equals("PlanLuDBK.queryZYDBK"))
			{
				sqlString += isfirstparam ? String.format(" where LAST_BATCH_NO = '%s' ", BatchNo) : String.format(" and LAST_BATCH_NO = '%s' ", BatchNo);
			}
			else {
				sqlString += isfirstparam ? String.format(" where ZG_BATCH_NO = '%s' ", BatchNo) : String.format(" and ZG_BATCH_NO = '%s' ", BatchNo);
			}	
			isfirstparam = false;
		}
		if(!StringTool.isNullOrEmpty(BatchGroupNo))
		{
			sqlString += isfirstparam ? String.format(" where BATCH_GROUD_NO = '%s' ", BatchGroupNo) : String.format(" and BATCH_GROUD_NO = '%s' ", BatchGroupNo);
			isfirstparam = false;
		}
		sqlString += isfirstparam ? " where Planstatus = '0' Or Planstatus = '1' OR Planstatus = '2' " : " AND (Planstatus = '0' OR Planstatus = '1' OR Planstatus = '2')";
		sqlString += " ORDER BY CREATE_TIME desc";
		returnObject = this.getData(sqlString);
		return returnObject;
	}
	
	public CoreReturnObject queryZyC(String sqlId,String HeatPlanNo) {
		CoreReturnObject returnObject = null;
		String sqlsString = CoreXmlSqlParsersFactory.getSql(sqlId);
		if(!StringTool.isNullOrEmpty(HeatPlanNo))
		{
			sqlsString += String.format(" where Heat_Plan_No = '%s'",HeatPlanNo);
		}
		returnObject = this.getData(sqlsString);
		return returnObject;
	}
	
	public CoreReturnObject queryZy(String sqlId,String HeatPlanNo) {
		CoreReturnObject returnObject = null;
		String sqlsString = CoreXmlSqlParsersFactory.getSql(sqlId);
		if(!StringTool.isNullOrEmpty(HeatPlanNo))
		{
			sqlsString += String.format(" where Heat_Plan_No = '%s'",HeatPlanNo);
		}
		returnObject = this.getData(sqlsString);
		return returnObject;
	}
	
	
	public CoreReturnObject queryZy(String SQLID,String PLINE,String CREATETIMEFROM,String CREATETIMETO,String ORDERNO,String[] szStatus) {
		CoreReturnObject returnObject = null;
		String sqlString = CoreXmlSqlParsersFactory.getSql(SQLID);
		boolean isfirstparam=true;
		if(!StringTool.isNullOrEmpty(PLINE))
		{
			sqlString  += String.format(" where PLINE_CODE = '%s' ", PLINE);
			isfirstparam = false;
		}
		if(!StringTool.isNullOrEmpty(ORDERNO))
		{
			sqlString += isfirstparam ? String.format(" where ORDER_NO = '%s' ", ORDERNO) : String.format(" and ORDER_NO = '%s' ", ORDERNO);
			isfirstparam = false;
		}
		if(!StringTool.isNullOrEmpty(CREATETIMEFROM) &&!StringTool.isNullOrEmpty(CREATETIMETO))
		{
			sqlString += isfirstparam ? String.format(" where CREATE_TIME >= to_date('%s','yyyy-mm-dd hh24:mi:ss') ", CREATETIMEFROM) : String.format(" and CREATE_TIME >= to_date('%s','yyyy-mm-dd hh24:mi:ss') ", CREATETIMEFROM);
			isfirstparam=false;
		}
		if(!StringTool.isNullOrEmpty(CREATETIMETO) &&!StringTool.isNullOrEmpty(CREATETIMETO))
		{
			sqlString += isfirstparam ? String.format(" where CREATE_TIME <= to_date('%s','yyyy-mm-dd hh24:mi:ss') ", CREATETIMETO) : String.format(" and CREATE_TIME <= to_date('%s','yyyy-mm-dd hh24:mi:ss') ", CREATETIMETO);
			isfirstparam=false;
		}
		
		String statusString="";
		for (String status : szStatus) {
			statusString += String.format(" PLANSTATUS = '%s' Or ", status);
		}
		statusString = statusString.substring(0, statusString.lastIndexOf("Or"));
		sqlString += isfirstparam?String.format(" where %s", statusString):String.format(" and (%s)", statusString);
		sqlString += " ORDER BY CREATE_TIME desc";
		returnObject=this.getData(sqlString);
		return returnObject;
	}
	
	public CoreReturnObject getMaxHeatPlanNo(String tableNamesString)
	{
		CoreReturnObject returnObject=null;
		String sqlString=CoreXmlSqlParsersFactory.getSql("StoveBase.queryMaxHeat_Plan_No");
		sqlString=StringTool.ReplaceAll(sqlString, "wencheng", tableNamesString);
		returnObject=this.getData(sqlString);
		return returnObject;
	}
	
	public CoreReturnObject UpdateOrder_WT_NUM(String tablenamesString,ArrayList<Object> params) {
		CoreReturnObject returnObject=null;
		String sqlString=CoreXmlSqlParsersFactory.getSql("StoveBase.UpdateOrder_WT_NUM");
		sqlString=StringTool.ReplaceAll(sqlString, "wencheng", tablenamesString);
		returnObject=this.setData(sqlString, params.toArray());
		return returnObject;
	}
	
	public CoreReturnObject UpdateOrder_MATCHIN_WT_NUM(String tablenamesString,ArrayList<Object> params) {
		CoreReturnObject returnObject=null;
		String sqlString=CoreXmlSqlParsersFactory.getSql("StoveBase.UpdateOrder_MatchIn_WT_NUM");
		sqlString=StringTool.ReplaceAll(sqlString, "wencheng", tablenamesString);
		returnObject=this.setData(sqlString, params.toArray());
		return returnObject;
	}
	
	public CoreReturnObject UpdatePlnStatus(String tablenameString,ArrayList<Object> params)
	{
		CoreReturnObject returnObject = null;
		String sqlString=CoreXmlSqlParsersFactory.getSql("StoveBase.UpdatePlnStatus");
		sqlString=StringTool.ReplaceAll(sqlString, "wencheng", tablenameString);
		returnObject=this.setData(sqlString, params.toArray());
		return returnObject;
	}
	
	public CoreReturnObject ExcuteNonQuery(String jsonString,String sqlID,String entityClasString,ArrayList<Object> paramsList) {
		CoreReturnObject returnObject = null;
		Class<?> entityClass=null;
		try {
			entityClass=Class.forName(entityClasString);
			Object objEntity=EntityTool.getEntityByJSONStr(jsonString, entityClass.newInstance().getClass());
			String sqlString=EntityTool.fillSqlByEntity(CoreXmlSqlParsersFactory.getSql(sqlID), objEntity);
			if(paramsList.size() > 0)
			{
				returnObject=this.setData(sqlString, paramsList.toArray());
			}
			else {
				returnObject=this.setData(sqlString);
			}
			
		} catch (Exception e) {
			// TODO: handle exception
		}
		return returnObject;
	}
	
	public CoreReturnObject ExcuteNonQuery(String SqlId,ArrayList<Object> paramList) {
		CoreReturnObject returnObject = null;
		returnObject = this.setDataBySqlID(SqlId, paramList.toArray());
		return returnObject;
	}
	
	public CoreReturnObject queryMaxProdIndex(String tablenameString)
	{
		CoreReturnObject returnObject = null;
		String sqlString=CoreXmlSqlParsersFactory.getSql("StoveBase.queryMaxProdIndex");
		sqlString=StringTool.ReplaceAll(sqlString, "wencheng", tablenameString);
		returnObject=this.getData(sqlString);
		return returnObject;
	}
	
	public CoreReturnObject queryCHECKJVAL(String ORD_LN_DLY_PK)
	{
		CoreReturnObject returnObject =null;
		returnObject=this.getDataBySqlID("StoveBase.queryCHECKJVAL", new Object[]{ORD_LN_DLY_PK});
		return returnObject;
	}
	
	public CoreReturnObject ExeTransctionOwn(ArrayList<String> SqlIDList,ArrayList<ArrayList<Object>> paramList)  {
		CoreReturnObject returnObject = new CoreReturnObject();
		boolean isReady=true;
		Connection cnnConnection=this.getDao(CoreConstantData.sysbeanstr).getConnection();
		try {
			cnnConnection.setAutoCommit(false);
			for (int i = 0; i < SqlIDList.size(); i++) {
				PreparedStatement pS = cnnConnection.prepareStatement(SqlIDList.get(i));
				for (int j = 0; j < paramList.get(i).size(); j++) {
					pS.setObject(j+1, paramList.get(i).get(j));
				}
				int nResult = pS.executeUpdate();
				if(nResult < 1)
				{
					isReady = false;
					if(cnnConnection !=null)
					{
						cnnConnection.rollback();
						cnnConnection.setAutoCommit(true);
					}
					returnObject.SetErrInfo(String.format("语句执行无效，影响0行 %s \r\n %s", CoreXmlSqlParsersFactory.getSql(SqlIDList.get(i)),paramList.get(i).toString()));
					break;
				}
			}
			if(isReady && cnnConnection != null)
			{
				cnnConnection.commit();
				cnnConnection.setAutoCommit(true);
			}
		} catch (SQLException e) {
			try {
				cnnConnection.rollback();
				cnnConnection.setAutoCommit(true);
			} catch (SQLException e1) {
				// TODO Auto-generated catch block
				e1.printStackTrace();
			}
			returnObject.SetErrInfo(e.getMessage());
		} finally
		{
			try {
				if(cnnConnection != null && !cnnConnection.isClosed())
				{
					cnnConnection.close();
				}
			} catch (SQLException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
		
		return returnObject;
	}
	
	public CoreReturnObject ExeTransction(ArrayList<String> SqlIDList,ArrayList<ArrayList<Object>> paramList) {
		CoreReturnObject returnObject = null;
		ArrayList<Object[]> paramArrayList=new ArrayList<Object[]>();
		for (ArrayList<Object> objects : paramList) {
			paramArrayList.add(objects.toArray());
		}
		returnObject = this.setDataById(SqlIDList, paramArrayList);
		return returnObject;
	}
	
	public CoreReturnObject queryBatchMatch(String tableName,String ProPlanId,String GxPlanNo) {
		CoreReturnObject returnObject = null;
		String sqlString = CoreXmlSqlParsersFactory.getSql("StoveBase.queryBatchMatch");
		sqlString = StringTool.ReplaceAll(sqlString, "wencheng", tableName);
		returnObject = this.getData(sqlString, new Object[]{ProPlanId,GxPlanNo});
		return returnObject;
	}
	
	@SuppressWarnings("null")
	public CoreReturnObject ExeTransction(ArrayList<String> _entitySqlIds,ArrayList<String> _jsonStrings,ArrayList<String> _entityClassNameList,ArrayList<String> _SqlIdList,ArrayList<ArrayList<Object>> _paramList)
	{
		CoreReturnObject returnObject = null;
		ArrayList<String> sqlList =new ArrayList<String>();
		ArrayList<Object[]> paramArrayList = new ArrayList<Object[]>();
		
		for (int i = 0; i < _entitySqlIds.size(); i++) {
			Class<?> entityClass=null;
			try {
				entityClass=Class.forName(_entityClassNameList.get(i));
			} catch (ClassNotFoundException e) {
				returnObject.SetErrInfo(e.getMessage());
				// TODO Auto-generated catch block
				e.printStackTrace();
				
			}
			Object objEntity = null;
			try {
				objEntity = EntityTool.getEntityByJSONStr(_jsonStrings.get(i), entityClass.newInstance().getClass());
			} catch (InstantiationException e) {
				// TODO Auto-generated catch block
				returnObject.SetErrInfo(e.getMessage());
				e.printStackTrace();
			} catch (IllegalAccessException e) {
				// TODO Auto-generated catch block
				returnObject.SetErrInfo(e.getMessage());
				e.printStackTrace();
			}
			String sqlString=EntityTool.fillSqlByEntity(CoreXmlSqlParsersFactory.getSql(_entitySqlIds.get(i)), objEntity);
			sqlList.add(sqlString);
			paramArrayList.add(new Object[]{});
		}
		for (int a = 0; a < _SqlIdList.size(); a++) {
			String sqlString =CoreXmlSqlParsersFactory.getSql(_SqlIdList.get(a));
			sqlList.add(sqlString);
			paramArrayList.add(_paramList.get(a).toArray());
		}
		returnObject = this.setData(sqlList, paramArrayList);
		return returnObject;
		
	}
	
	public CoreReturnObject queryOrderStatus(String tableName,String proPlanId,String gxPlanNo) {
		CoreReturnObject returnObject = new CoreReturnObject();
		if(StringTool.isNullOrEmpty(tableName) || StringTool.isNullOrEmpty(proPlanId) || StringTool.isNullOrEmpty(gxPlanNo))
		{
			returnObject.setV_errMsg("参数不能为空");
			return returnObject;
		}
		String sqlsString = CoreXmlSqlParsersFactory.getSql("StoveBase.queryOrderStatus");
		sqlsString = StringTool.ReplaceAll(sqlsString, "wencheng", tableName);
		returnObject = this.getData(sqlsString, new Object[]{proPlanId,gxPlanNo});
		return returnObject;
	}
	
	public CoreReturnObject queryPlanStatus(String tableName,String HeatPlanNo) {
		CoreReturnObject returnObject = new CoreReturnObject();
		if(StringTool.isNullOrEmpty(tableName) || StringTool.isNullOrEmpty(HeatPlanNo))
		{
			returnObject.setV_errMsg("参数不能为空");
			return returnObject;
		}
		String sqlsString = CoreXmlSqlParsersFactory.getSql("StoveBase.queryPlanStatus");
		sqlsString = StringTool.ReplaceAll(sqlsString, "wencheng", tableName);
		returnObject = this.getData(sqlsString, new Object[]{HeatPlanNo});
		return returnObject;
	}
	
	public double getSpecDecimal(double src,int point) {
		String format = "#.";
		if(point == 0)
		{
			return Math.floor(src);
		}
		else {
			for (int i = 0; i <point; i++) {
				format += "#";
			}
			DecimalFormat df = new DecimalFormat(format);
			return Double.parseDouble(df.format(src));			
		}
	}
	
	
}
