package com.steering.jobface.qcm.task;

import java.lang.reflect.Method;
import java.sql.Connection;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import com.steering.comm.tools.EntityTool;
import com.steering.model.qcm.BaseChemStdsteelSelectBakEntity;
import com.steering.model.qcm.BaseChemStdsteelSelectEntity;

import CoreFS.SA08.CoreXmlSqlParsersFactory;

public class Jhy {
	public Jhy(Connection jhyCon, Connection qcmCon) {
		try {
			jhyCon.setAutoCommit(false);
			qcmCon.setAutoCommit(false);
			ArrayList<BaseChemStdsteelSelectBakEntity> testData2s = getTestData2(jhyCon);
			Statement jhyStmt = jhyCon.createStatement();
			Statement qcmStmt = qcmCon.createStatement();
			for (BaseChemStdsteelSelectBakEntity testData2 : testData2s) {
				ArrayList<BaseChemStdsteelSelectBakEntity> testDatas = getTestData(jhyCon, testData2);
				BaseChemStdsteelSelectEntity steelInfo = new BaseChemStdsteelSelectEntity();
				CopyData(testData2, testDatas, steelInfo);
				String sqlInsertQcmJhyElements = CoreXmlSqlParsersFactory
						.getSql("jhy.insert");
				sqlInsertQcmJhyElements = EntityTool.fillSqlByEntity(sqlInsertQcmJhyElements, steelInfo);
				qcmStmt.addBatch(sqlInsertQcmJhyElements);

				String sqlUpdateFlag = CoreXmlSqlParsersFactory.getSql("jhy.updateFlag");
				sqlUpdateFlag = EntityTool.fillSqlByEntity(sqlUpdateFlag, testData2);
				jhyStmt.addBatch(sqlUpdateFlag);
			}
			qcmStmt.executeBatch();
			qcmStmt.close();
			jhyStmt.executeBatch();
			jhyStmt.close();

			jhyCon.commit();
			qcmCon.commit();
		} catch (Exception ex) {
			ex.printStackTrace();
			try {
				jhyCon.rollback();
			} catch (SQLException e) {
				e.printStackTrace();
			}
			try {
				qcmCon.rollback();
			} catch (SQLException e) {
				e.printStackTrace();
			}
		}
	}

	private ArrayList<BaseChemStdsteelSelectBakEntity> getTestData(Connection con, BaseChemStdsteelSelectBakEntity testData2) {
		String sql = CoreXmlSqlParsersFactory.getSql("jhy.getTestData");
		sql = EntityTool.fillSqlByEntity(sql, testData2);
		ArrayList<BaseChemStdsteelSelectBakEntity> list = EntityTool.getEntitysBySqlNoClsCon(sql, con,
				BaseChemStdsteelSelectBakEntity.class);
		return list;
	}
	
	private ArrayList<BaseChemStdsteelSelectBakEntity> getTestData2(Connection con) {
		String sql = CoreXmlSqlParsersFactory.getSql("jhy.getTestData2");
		ArrayList<BaseChemStdsteelSelectBakEntity> list = EntityTool.getEntitysBySqlNoClsCon(sql, con,
				BaseChemStdsteelSelectBakEntity.class);
		return list;
	}

	private void CopyData(BaseChemStdsteelSelectBakEntity testData2,
			ArrayList<BaseChemStdsteelSelectBakEntity> testDatas, BaseChemStdsteelSelectEntity steelSelect) {
		Class<BaseChemStdsteelSelectEntity> steelSelectClass = BaseChemStdsteelSelectEntity.class;
		for (BaseChemStdsteelSelectBakEntity testData : testDatas) {
			try {
				Method method = steelSelectClass.getMethod("setAna" + testData.getItemName(), String.class);
				method.invoke(steelSelect, "1");
			} catch (Exception ex) {
				
			}
		}

		steelSelect.setSampleNo(testData2.getSampleNo());
		steelSelect.setSteelNo(testData2.getSteelNo());
		steelSelect.setSelectSteelNo(testData2.getSelectSteelNo());
	}
}
