package com.steering.pss.sale.order.qualityDesign.Dao.idao;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.SQLException;
import java.util.List;
import com.steering.comm.tools.EntityTool;
import com.steering.comm.tools.StringTool;
import com.steering.pss.qcm.model.QcmZbsTemplateRMscEntity;
import com.steering.pss.qcm.model.QcmZbsTemplateRMscSpecEntity;
import com.steering.pss.sale.order.craftDesign.tool.CraftTool;
import CoreFS.SA08.CoreXmlSqlParsersFactory;

public class ZbsTemplateDao {
	
	public boolean CheckTemplate(Connection con, String msc, String dimater, String height, String model, String spec, String designKey) throws SQLException{
		String sql = CoreXmlSqlParsersFactory.getSql("ZbsTemplateDao.GetZbsTemplateRMscSpec");
		sql = StringTool.ReplaceAll(sql, "#MSC#", msc);
		List<QcmZbsTemplateRMscSpecEntity> zbsTemplateRMscSpecs = EntityTool.getEntitysBySqlNoClsCon(sql, con, QcmZbsTemplateRMscSpecEntity.class);
		if(zbsTemplateRMscSpecs.size() == 0){
			String sqlGetZbsTemplateRMsc = CoreXmlSqlParsersFactory.getSql("ZbsTemplateDao.GetZbsTemplateRMsc");
			sqlGetZbsTemplateRMsc = StringTool.ReplaceAll(sqlGetZbsTemplateRMsc, "#MSC#", msc);
			List<QcmZbsTemplateRMscEntity> zbsTemplateRMscs = EntityTool.getEntitysBySqlNoClsCon(sqlGetZbsTemplateRMsc, con, QcmZbsTemplateRMscEntity.class);
			if(zbsTemplateRMscs.size() == 0){
				return false;
			}else{
				SaveZbsTemplate(zbsTemplateRMscs.get(0), designKey, con);
				return true;
			}
		}else{
			for(QcmZbsTemplateRMscSpecEntity zbsTemplateRMscSpec : zbsTemplateRMscSpecs){
				if(CraftTool.checkStr(dimater, zbsTemplateRMscSpec.getMinDSign(), zbsTemplateRMscSpec.getMinD())
						&& CraftTool.checkStr(dimater, zbsTemplateRMscSpec.getMaxDSign(), zbsTemplateRMscSpec.getMaxD())
						&& CraftTool.checkStr(height, zbsTemplateRMscSpec.getMinHSign(), zbsTemplateRMscSpec.getMinH())
						&& CraftTool.checkStr(height, zbsTemplateRMscSpec.getMaxHSign(), zbsTemplateRMscSpec.getMaxH())
						&& (IsContain(zbsTemplateRMscSpec.getModelCode().split("\\|"), model) || zbsTemplateRMscSpec.getModelCode().equals(""))
						&& (IsContain(zbsTemplateRMscSpec.getSpecJg().split(";"), spec) || zbsTemplateRMscSpec.getSpecJg().equals(""))){
					
					String sqlGetZbsTemplateRMsc = CoreXmlSqlParsersFactory.getSql("ZbsTemplateDao.GetZbsTemplateRMscByPk");
					sqlGetZbsTemplateRMsc = StringTool.ReplaceAll(sqlGetZbsTemplateRMsc, "#PRIMARY_KEY#", zbsTemplateRMscSpec.getPrimaryKey());
					QcmZbsTemplateRMscEntity zbsTemplateRMsc = EntityTool.getEntityBySqlNoClsCon(sqlGetZbsTemplateRMsc, con, QcmZbsTemplateRMscEntity.class);
					if(zbsTemplateRMsc == null) continue;
					else{
						SaveZbsTemplate(zbsTemplateRMsc, designKey, con);
						return true;
					}
				}
			}
		}
		
		return false;
	}
	
	private void SaveZbsTemplate(QcmZbsTemplateRMscEntity zbsTemplateRMsc, String designKey, Connection con) throws SQLException{
		String sql = CoreXmlSqlParsersFactory.getSql("ZbsTemplateDao.Insert");
		sql = StringTool.ReplaceAll(sql, "#DESGIN_KEY#", designKey);
		sql = EntityTool.fillSqlByEntity(sql, zbsTemplateRMsc);
		PreparedStatement ps = con.prepareStatement(sql);
		ps.executeUpdate();
		ps.close();
	}
	
	private boolean IsContain(String[] array, String value){
		for(String str : array){
			if(str.equals(value)){
				return true;
			}
		}
		
		return false;
	}
}
