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.SqlBuilder;
import com.steering.comm.tools.StringTool;
import com.steering.comm.tools.TransctionTool;
import com.steering.pss.qcm.model.ComBaseSplineCEntity;
import com.steering.pss.qcm.model.ComBaseSplineCRModelEntity;
import com.steering.pss.qcm.model.ComBaseSplineCRProductEntity;
import com.steering.pss.qcm.model.ComBaseSplineCRStdEntity;
import com.steering.pss.qcm.model.ComBaseSplineCRSteelEntity;
import com.steering.pss.qcm.model.ComBaseSplineEntity;
import com.steering.pss.qcm.model.ComBaseSplineRStdEntity;
import com.steering.pss.qcm.model.GenericEntity;

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

public class ComBaseSplineDAL extends DBCommon{
	
	public CoreReturnObject Query(String phyNameMin, String sampleStyle, String validflag,String seamArea){
		String sql = CoreXmlSqlParsersFactory.getSql("ComBaseSpline.Query");
		sql = StringTool.ReplaceAll(sql, "#VALIDFLAG#", validflag);
		sql = StringTool.ReplaceAll(sql, "#PHY_NAME_MIN#", phyNameMin);
		sql = StringTool.ReplaceAll(sql, "#SAMPLE_STYLE#", sampleStyle);
		sql =StringTool.ReplaceAll(sql, "#SEAM_AREA#", seamArea);
		return this.getData(sql);
	}
	
	public CoreReturnObject Query2(String phyCodeMin, String itemCodeF,
			String itemCodeC, String ordLnPk, String itemCodeW){
		SqlBuilder b = new SqlBuilder("ComBaseSpline.Query2");
		b.Fill(new String[]{phyCodeMin, itemCodeF, itemCodeC});
		return this.getData(b.toString());
	}
	
	public CoreReturnObject QueryNk(String phyCodeMin, String stdCode, 
			String stdCodeTest, String itemCodeF){
		String sql = CoreXmlSqlParsersFactory.getSql("ComBaseSpline.QueryNk");
		sql = StringTool.ReplaceAll(sql, "#PHY_CODE_MIN#", phyCodeMin);
		sql = StringTool.ReplaceAll(sql, "#STD_CODE#", stdCode);
		sql = StringTool.ReplaceAll(sql, "#STD_CODE_TEST#", stdCodeTest);
		if(itemCodeF.equals("I0001")){
			sql = StringTool.ReplaceAll(sql, "#PHY_DIR#", "纵向");
		}else if(itemCodeF.equals("I0004")){
			sql = StringTool.ReplaceAll(sql, "#PHY_DIR#", "横向");
		}else{
			sql = StringTool.ReplaceAll(sql, "#PHY_DIR#", "");
		}
		
		return this.getData(sql);
	}
	
	/**
	 * 保存试样码基础数据。
	 * @param jsons
	 * @return
	 */
	public CoreReturnObject Save(ArrayList<String> jsons, ArrayList<String> jsonCs){
		TransctionTool transction = new TransctionTool();
		try{
			Connection con = transction.getCon();
			GenericEntity maxEic = null;
			for(String json : jsons){
				ComBaseSplineEntity splineEntity = EntityTool.getEntityByGson(json, ComBaseSplineEntity.class);
				String sql = "";
				
				if(splineEntity.getValidflag().equals("")){
					
					sql = CoreXmlSqlParsersFactory.getSql("ComBaseSpline.Insert");
					String sqlGetMaxEic = CoreXmlSqlParsersFactory.getSql("ComBaseSpline.GetMaxEic");
					maxEic = EntityTool.getEntityBySqlNoClsCon(sqlGetMaxEic, con, GenericEntity.class);
					splineEntity.setEic(maxEic.getMaxValue());
				}
				else{
					sql = CoreXmlSqlParsersFactory.getSql("ComBaseSpline.Update");
					
					//修改从表试样组
					String sqlUpdateSplineC = CoreXmlSqlParsersFactory.getSql("ComBaseSpline.UpdateSplineC");
					sqlUpdateSplineC = EntityTool.fillSqlByEntity(sqlUpdateSplineC, splineEntity);
					if(!transction.ExcuteTransction(sqlUpdateSplineC)){
						return transction.rollBack();
					}
				}
				
				sql = EntityTool.fillSqlByEntity(sql, splineEntity);
				if(!transction.ExcuteTransction(sql)){
					return transction.rollBack();
				}
				
				//删除试验标准，再新增试验标准
				String sqlDeleteSplineRStd = CoreXmlSqlParsersFactory.getSql("ComBaseSpline.DeleteSplineRStd");
				sqlDeleteSplineRStd = StringTool.ReplaceAll(sqlDeleteSplineRStd, "#EIC#", splineEntity.getEic());
				if(!transction.ExcuteTransction(sqlDeleteSplineRStd)){
					return transction.rollBack();
				}
				
				for(ComBaseSplineRStdEntity splineRStdEntity : splineEntity.getSplineRStds()){
					String sqlInsertSplineRStd = CoreXmlSqlParsersFactory.getSql("ComBaseSpline.InsertSplineRStd");
					splineRStdEntity.setEic(splineEntity.getEic());
					sqlInsertSplineRStd = EntityTool.fillSqlByEntity(sqlInsertSplineRStd, splineRStdEntity);
					if(!transction.ExcuteTransction(sqlInsertSplineRStd)){
						return transction.rollBack();
					}
				}
			}
			
			for(String json : jsonCs){
				ComBaseSplineCEntity splineCEntity = EntityTool.getEntityByGson(json, ComBaseSplineCEntity.class);
				String sql = "";
				if(splineCEntity.getValidflag().equals("")){
					sql = CoreXmlSqlParsersFactory.getSql("ComBaseSplineC.Insert");
					String sqlGetMaxTic = CoreXmlSqlParsersFactory.getSql("ComBaseSplineC.GetMaxTic");
					GenericEntity maxTic = EntityTool.getEntityBySqlNoClsCon(sqlGetMaxTic, con, GenericEntity.class);
					splineCEntity.setTic(maxTic.getMaxValue());
				}
				else{
					sql = CoreXmlSqlParsersFactory.getSql("ComBaseSplineC.Update");
				}
				
				if(maxEic != null){
					splineCEntity.setEic(maxEic.getMaxValue());
				}
				sql = EntityTool.fillSqlByEntity(sql, splineCEntity);
				if(!transction.ExcuteTransction(sql)){
					return transction.rollBack();
				}
				
				//删除并新增集合
				String sqlDelSplineCRSteel = CoreXmlSqlParsersFactory.getSql("ComBaseSplineC.DeleteSplineCRSteel");
				sqlDelSplineCRSteel = StringTool.ReplaceAll(sqlDelSplineCRSteel, "#TIC#", splineCEntity.getTic());
				if(!transction.ExcuteTransction(sqlDelSplineCRSteel)){
					return transction.rollBack();
				}
				
				for(ComBaseSplineCRSteelEntity splineCRSteel : splineCEntity.getSplineCRSteel()){
					String sqlIstSplineCRSteel = CoreXmlSqlParsersFactory.getSql("ComBaseSplineC.InsertSplineCRSteel");
					splineCRSteel.setTic(splineCEntity.getTic());
					sqlIstSplineCRSteel = EntityTool.fillSqlByEntity(sqlIstSplineCRSteel, splineCRSteel);
					if(!transction.ExcuteTransction(sqlIstSplineCRSteel)){
						return transction.rollBack();
					}
				}
				
				String sqlDelSplineCRProduct = CoreXmlSqlParsersFactory.getSql("ComBaseSplineC.DeleteSplineCRProduct");
				sqlDelSplineCRProduct = EntityTool.fillSqlByEntity(sqlDelSplineCRProduct, splineCEntity);
				if(!transction.ExcuteTransction(sqlDelSplineCRProduct)){
					return transction.rollBack();
				}
				
				for(ComBaseSplineCRProductEntity splineCRProduct : splineCEntity.getSplineCRProduct()){
					String sqlIstSplineCRProduct = CoreXmlSqlParsersFactory.getSql("ComBaseSplineC.InsertSplineCRProduct");
					splineCRProduct.setTic(splineCEntity.getTic());
					sqlIstSplineCRProduct = EntityTool.fillSqlByEntity(sqlIstSplineCRProduct, splineCRProduct);
					if(!transction.ExcuteTransction(sqlIstSplineCRProduct)){
						return transction.rollBack();
					}
				}
				
				SqlBuilder b = new SqlBuilder("ComBaseSplineC.DeleteSplineCRStd");
				b.Fill(new String[]{splineCEntity.getTic()});
				if(!transction.ExcuteTransction(b.toString())){
					return transction.rollBack();
				}
				for(ComBaseSplineCRStdEntity splineCRStd : splineCEntity.getSplineCRStd()){
					String sqlIstSplineCRStd = CoreXmlSqlParsersFactory.getSql("ComBaseSplineC.InsertSplineCRStd");
					splineCRStd.setTic(splineCEntity.getTic());
					sqlIstSplineCRStd = EntityTool.fillSqlByEntity(sqlIstSplineCRStd, splineCRStd);
					if(!transction.ExcuteTransction(sqlIstSplineCRStd)){
						return transction.rollBack();
					}
				}
				
				//扣型
				b = new SqlBuilder("ComBaseSplineC.DeleteSplineCRModel");
				b.Fill(new String[]{splineCEntity.getTic()});
				if(!transction.ExcuteTransction(b.toString())){
					return transction.rollBack();
				}
				for(ComBaseSplineCRModelEntity splineCRModel : splineCEntity.getSplineCRModel()){
					String sqlIstSplineCRModel = CoreXmlSqlParsersFactory.getSql("ComBaseSplineC.InsertSplineCRModel");
					splineCRModel.setTic(splineCEntity.getTic());
					sqlIstSplineCRModel = EntityTool.fillSqlByEntity(sqlIstSplineCRModel, splineCRModel);
					if(!transction.ExcuteTransction(sqlIstSplineCRModel)){
						return transction.rollBack();
					}
				}
			}
			
		}catch(Exception ex){
			CoreReturnObject cro = transction.rollBack();
			cro.setV_errMsg(ex.getMessage());
			return cro;
		}
		
		return transction.commit();
	}
	
	/**
	 * 修改有效标志
	 * @param jsons
	 * @return
	 */
	public CoreReturnObject UpdateValidflag(ArrayList<String> jsons, String flag){
		ArrayList<String> sqllist = new ArrayList<String>();
		for(String json : jsons){
			ComBaseSplineEntity splineEntity = EntityTool.getEntityByJSONStr(json, ComBaseSplineEntity.class);
			String sql = "";
			if(flag.equals("0")){
				sql = CoreXmlSqlParsersFactory.getSql("ComBaseSpline.Delete");
				String sqlSplineC = CoreXmlSqlParsersFactory.getSql("ComBaseSplineC.DeleteByEic");
				sql = EntityTool.fillSqlByEntity(sql, splineEntity);
				sqlSplineC = EntityTool.fillSqlByEntity(sqlSplineC, splineEntity);
				sqllist.add(sql);
				sqllist.add(sqlSplineC);
			}else{
				sql = CoreXmlSqlParsersFactory.getSql("ComBaseSpline.Resume");
				sql = EntityTool.fillSqlByEntity(sql, splineEntity);
				sqllist.add(sql);
			}
		}
		
		return this.getDao(CoreConstantData.sysbeanstr).ExcuteTransction(sqllist);
	}
	
	public CoreReturnObject GetComBasePhyC(){
		String sql = CoreXmlSqlParsersFactory.getSql("ComBaseSpline.GetComBasePhyC");
		return this.getData(sql);
	}
	
	public CoreReturnObject UpdateFilePosition(String filePath, String eic){
		String sql = CoreXmlSqlParsersFactory.getSql("ComBaseSpline.UpdateFilePosition");
		sql = StringTool.ReplaceAll(sql, "#FILE_POSITION#", filePath);
		sql = StringTool.ReplaceAll(sql, "#EIC#", eic);
		return this.setData(sql);
	}
	
	
	public CoreReturnObject GetComBaseStdS(String eic){
		String sql = CoreXmlSqlParsersFactory.getSql("ComBaseSpline.GetComBaseStdS");
		return this.getData(sql);
	}
	
	/**
	 * 是否重复
	 * @param strJSON
	 * @return
	 */
	public CoreReturnObject IsRepeat(String strJSON) {
		ComBaseSplineEntity splineEntity = EntityTool.getEntityByJSONStr(strJSON, ComBaseSplineEntity.class);
		SqlBuilder b = new SqlBuilder("ComBaseSpline.IsRepeat");
		b.Fill(new String[]{splineEntity.getPhyCodeMin(), splineEntity.getItemCodeF(), splineEntity.getItemCodeS(),
				splineEntity.getSizeDesc(), splineEntity.getEic()});
		return this.getData(b.toString());
	}
}
