package com.steering.pss.qcm.DAL;

import java.sql.Connection;
import java.util.ArrayList;
import com.steering.comm.db.DBCommon;
import com.steering.comm.tools.EntityTool;
import com.steering.comm.tools.StringTool;
import com.steering.comm.tools.TransctionTool;
import com.steering.pss.qcm.model.ComBaseStdRTestEntity;
import com.steering.pss.qcm.model.GenericEntity;
import CoreFS.SA06.CoreReturnObject;
import CoreFS.SA08.CoreXmlSqlParsersFactory;
import CoreFS.SC01.CoreConstantData;

public class ComBaseStdRTestDAL extends DBCommon{
	
	public CoreReturnObject Query(String phyNameMin, String validflag){
		String sql = CoreXmlSqlParsersFactory.getSql("ComBaseStdRTestDAL.Query");
		sql = StringTool.ReplaceAll(sql, "#VALIDFLAG#", validflag);
		sql = StringTool.ReplaceAll(sql, "#PHY_NAME_MIN#", phyNameMin);
		return this.getData(sql);
	}
	
	public CoreReturnObject Save(ArrayList<String> jsons){
		TransctionTool transction = new TransctionTool();
		try{
			Connection con = transction.getCon();
			for(String json : jsons){
				ComBaseStdRTestEntity stdRTest = EntityTool.getEntityByJSONStr(json, ComBaseStdRTestEntity.class);
				String sql = "";
				if(stdRTest.getValidflag().equals("")){
					sql = CoreXmlSqlParsersFactory.getSql("ComBaseStdRTestDAL.Insert");
					String sqlMaxPk = CoreXmlSqlParsersFactory.getSql("ComBaseStdRTestDAL.GetMaxPk");
					GenericEntity maxPk = EntityTool.getEntityBySqlNoClsCon(sqlMaxPk, con, GenericEntity.class);
					sql = StringTool.ReplaceAll(sql, "#PK#", maxPk.getMaxValue());
				}else{
					sql = CoreXmlSqlParsersFactory.getSql("ComBaseStdRTestDAL.Update");
				}
				
				sql = EntityTool.fillSqlByEntity(sql, stdRTest);
				if(!transction.ExcuteTransction(sql)){
					return transction.rollBack();
				}
			}
		}catch(Exception ex){
			CoreReturnObject cro = transction.rollBack();
			cro.setV_errMsg(ex.getMessage());
			return cro;
		}
		
		return transction.commit();
	}
	
	public CoreReturnObject UpdateValidflag(ArrayList<String> jsons, String validflag){
		ArrayList<String> sqllist = new ArrayList<String>();
		for(String json : jsons){
			ComBaseStdRTestEntity stdRTest = EntityTool.getEntityByJSONStr(json, ComBaseStdRTestEntity.class);
			String sql = "";
			if(validflag.equals("0")){
				sql = CoreXmlSqlParsersFactory.getSql("ComBaseStdRTestDAL.Delete");
			}else{
				sql = CoreXmlSqlParsersFactory.getSql("ComBaseStdRTestDAL.Resume");
			}
			sql = EntityTool.fillSqlByEntity(sql, stdRTest);
			sqllist.add(sql);
		}
		
		return this.getDao(CoreConstantData.sysbeanstr).ExcuteTransction(sqllist);
	}
	
	//获取试验标准
	public CoreReturnObject GetComBaseStdS(){
		String sql = CoreXmlSqlParsersFactory.getSql("ComBaseStdRTestDAL.GetComBaseStdS");
		return this.getData(sql);
	}
	
	//获取取样码基础信息
	public CoreReturnObject GetComBasePhyC(){
		String sql = CoreXmlSqlParsersFactory.getSql("ComBaseStdRTestDAL.GetComBasePhyC");
		return this.getData(sql);
	}
	
	//获取重复项目数
	public CoreReturnObject GetRepateCnt(String stdCodeTest, String phyCodeMin, String pk){
		String sql = CoreXmlSqlParsersFactory.getSql("ComBaseStdRTestDAL.GetRepateCnt");
		sql = StringTool.ReplaceAll(sql, "#STD_CODE_TEST#", stdCodeTest);
		sql = StringTool.ReplaceAll(sql, "#PHY_CODE_MIN#", phyCodeMin);
		if(pk.equals("")){
			pk = "1";
		}
		sql = StringTool.ReplaceAll(sql, "#PK#", pk);
		return this.getData(sql);
	}
}
