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.pss.qcm.model.ComBaseWaterGroupEntity;
import com.steering.pss.qcm.model.GenericEntity;

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

public class ComBaseWaterGroupDAL extends DBCommon{
	/**
	 * 查询连浇群组
	 * @return
	 */
	public CoreReturnObject queryWaterGroup(){
		String sql = CoreXmlSqlParsersFactory.getSql("ComBaseWaterGroupDAL.queryWaterGroup");
		return this.getData(sql);
	}
	
	/**
	 * 查询连浇群组和钢种的对应关系
	 */
	public CoreReturnObject queryWaterGroupGrade(String waterGroupCode){
		String sql = CoreXmlSqlParsersFactory.getSql("ComBaseWaterGroupDAL.queryWaterGroupGrade");
		sql = StringTool.ReplaceAll(sql, "#WATER_GROUP_CODE#", waterGroupCode);
		return this.getData(sql);
	}
	
	/**
	 * 保存连浇群组和钢种的对应关系
	 * @param listJSON
	 * @return
	 */
	public CoreReturnObject saveWaretGroupGrade(ArrayList<String> listJSON){
		ArrayList<String> sqllist = new ArrayList<String>();
		String maxCode = "";
		if(listJSON.size() > 0){
			ComBaseWaterGroupEntity waterGroupEntity = EntityTool.getEntityByJSONStr(listJSON.get(0), ComBaseWaterGroupEntity.class);
			String sqlDel = CoreXmlSqlParsersFactory.getSql("ComBaseWaterGroupDAL.delWaterGroupGrade");
			sqlDel = StringTool.ReplaceAll(sqlDel, "#WATER_GROUP_CODE#", waterGroupEntity.getWaterGroupCode());
			sqllist.add(sqlDel);
			
			if(waterGroupEntity.getWaterGroupCode().equals("")){
				String sqlMaxCode = CoreXmlSqlParsersFactory.getSql("ComBaseWaterGroupDAL.getMaxWaterGroupCode");
				Connection con = this.getDao(CoreConstantData.sysbeanstr).getConnection();
				GenericEntity maxCodeEntity = EntityTool.getEntityBySql(sqlMaxCode, con, GenericEntity.class);
				maxCode = maxCodeEntity.getMaxValue();
				
			}else{
				maxCode = waterGroupEntity.getWaterGroupCode();
			}
		}
		
		for(String strJSON : listJSON){
			ComBaseWaterGroupEntity waterGroupEntity = EntityTool.getEntityByJSONStr(strJSON, ComBaseWaterGroupEntity.class);
			String sql = CoreXmlSqlParsersFactory.getSql("ComBaseWaterGroupDAL.setWaterGroupGrade");
			if(!waterGroupEntity.getWaterGroupCode().equals("")){
				sql = StringTool.ReplaceAll(sql, "#WATER_GROUP_CODE#", waterGroupEntity.getWaterGroupCode());
			}
			sql = StringTool.ReplaceAll(sql, "#WATER_GROUP_CODE#", maxCode);
			sql = EntityTool.fillSqlByEntity(sql, waterGroupEntity);
			sqllist.add(sql);
		}
		
		CoreReturnObject cro = this.getDao(CoreConstantData.sysbeanstr).ExcuteTransction(sqllist);
		cro.setResult(maxCode);
		return cro;
	}
	
	/**
	 * 查询连浇群组名称是否存在
	 * @param waterGroup
	 * @param waterGroupCode
	 * @return
	 */
	public CoreReturnObject getWaterGroupCnt(String waterGroup, String waterGroupCode){
		String sql = CoreXmlSqlParsersFactory.getSql("ComBaseWaterGroupDAL.getWaterGroupCnt");
		sql = StringTool.ReplaceAll(sql, "#WATER_GROUP#", waterGroup);
		sql = StringTool.ReplaceAll(sql, "#WATER_GROUP_CODE#", waterGroupCode);
		return this.getData(sql);
	}
	
	/**
	 * 查询连浇群组钢种组合是否存在
	 * @param waterGroup
	 * @param waterGroupCode
	 * @return
	 */
	public CoreReturnObject getWaterGroupGradeCnt(String gradeCodes, String waterGroupCode){
		String sql = CoreXmlSqlParsersFactory.getSql("ComBaseWaterGroupDAL.getWaterGroupGradeCnt");
		sql = StringTool.ReplaceAll(sql, "#GRADECODES#", gradeCodes);
		sql = StringTool.ReplaceAll(sql, "#WATER_GROUP_CODE#", waterGroupCode);
		return this.getData(sql);
	}
}
