package com.steering.pss.sale.order.craftDesign.dao;

import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.List;

import com.steering.comm.tools.EntityTool;
import com.steering.comm.tools.SqlBuilder;
import com.steering.pss.qcm.CorePhySample;
import com.steering.pss.qcm.model.ComBaseSplineEntity;
import com.steering.pss.qcm.model.GenericEntity;
import com.steering.pss.sale.order.craftDesign.model.ComBaseSpecEntity;
import com.steering.pss.sale.order.craftDesign.model.CraftEntity;
import com.steering.pss.sale.order.craftDesign.model.CraftOrdDesignMscBiltCEntity;
import com.steering.pss.sale.order.craftDesign.model.CraftOrdDesignMscBiltEntity;
import com.steering.pss.sale.order.craftDesign.model.CraftOrdDesignMscGzGgEntity;
import com.steering.pss.sale.order.craftDesign.model.CraftOrdDesignMscPlEntity;
import com.steering.pss.sale.order.craftDesign.model.CraftOrdDesignMscPlPrcEntity;
import com.steering.pss.sale.order.craftDesign.model.CraftOrdDesignStdPicEntity;
import com.steering.pss.sale.order.craftDesign.model.CraftOrdDesignStdPicSmpEntity;
import com.steering.pss.sale.order.craftDesign.model.SpecEntity;

import CoreFS.SA08.CoreXmlSqlParsersFactory;

public class MscSplineDao {
	public static String[] CheckSpline(Connection con, CraftEntity design, String speclFlg,
			CraftOrdDesignMscPlEntity mscPlEntity, SpecEntity specEntity) throws SQLException{
		OperationDao operationDao = new OperationDao();
		MscSplineDao mscSplineDao = new MscSplineDao();
		String[] msc0SplineResults = new String[]{"", "", "", "", "", "", ""};
		msc0SplineResults[2] = design.getDimater();
		msc0SplineResults[3] = design.getHeight();
		String[] mscnSplineResults = new String[]{"", "", "", "", "", "", ""};
		mscnSplineResults[2] = design.getDimater();
		mscnSplineResults[3] = design.getHeight();
		//内控试样码
		String[] nkSplineResults = new String[7];
		List<CraftOrdDesignMscPlPrcEntity> processEntitys = operationDao.getAllOperation(
				con, design.getMscOr(), mscPlEntity.getMscPline());
		for(CraftOrdDesignMscPlPrcEntity processEntity : processEntitys){
			int count = 0;
			if(processEntity.getProcessCode().equals("B")){
				ComBaseSpecEntity spec = new ComBaseSpecEntity();
				for(CraftOrdDesignMscBiltCEntity blitCSpec : specEntity.getBillSpecCs()){
					spec = mscSplineDao.getSpecInfoBySpecCode(blitCSpec.getCSpecCode(), con);
					nkSplineResults = mscSplineDao.CheckSpline(con, design,
							"2", spec,
							mscPlEntity.getMscPline(), processEntity.getProcessCode());
					if(nkSplineResults[0].equals("")){
						count++;
						break;
					}
				}
				if(count == 0){
					nkSplineResults[0] = "制程:" + mscPlEntity.getMscPline() + " 工序:" + processEntity.getProcessDesc() +
							" 铸造坯外径:" + spec.getDimater() + " " + nkSplineResults[0];
					nkSplineResults[2] = spec.getDimater();
					nkSplineResults[4] = mscPlEntity.getMscPline();
					nkSplineResults[5] = processEntity.getProcessCode();
					break;
				}
			}else if(processEntity.getProcessCode().equals("C")){
				ComBaseSpecEntity spec = new ComBaseSpecEntity();
				for(CraftOrdDesignMscBiltEntity blitSpec : specEntity.getBillSpecs()){
					spec = mscSplineDao.getSpecInfoBySpecCode(blitSpec.getBSpecCode(), con);
					nkSplineResults = mscSplineDao.CheckSpline(con, design,
							"2", spec,
							mscPlEntity.getMscPline(), processEntity.getProcessCode());
					if(nkSplineResults[0].equals("")){
						count++;
						break;
					}
				}
				if(count == 0){
					nkSplineResults[0] = "制程:" + mscPlEntity.getMscPline() + " 工序:" + processEntity.getProcessDesc() +
							" 锻造坯外径:" + spec.getDimater() + " " + nkSplineResults[0];
					nkSplineResults[2] = spec.getDimater();
					nkSplineResults[4] = mscPlEntity.getMscPline();
					nkSplineResults[5] = processEntity.getProcessCode();
					break;
				}
			}else if(processEntity.getProcessCode().equals("D")){
				ComBaseSpecEntity spec = new ComBaseSpecEntity();
				for(CraftOrdDesignMscGzGgEntity rollSpec : specEntity.getRollSpecs()){
					spec = mscSplineDao.getSpecInfoBySpecCode(rollSpec.getSpecCode(), con);
					nkSplineResults = mscSplineDao.CheckSpline(con, design,
							"2", spec, 
							mscPlEntity.getMscPline(), processEntity.getProcessCode());
					if(nkSplineResults[0].equals("")){
						count++;
						break;
					}
				}
				if(count == 0){
					nkSplineResults[0] = "制程:" + mscPlEntity.getMscPline() + " 工序:" + processEntity.getProcessDesc() +
							" 轧管规格:" + spec.getSpecName() + " " + nkSplineResults[0];
					nkSplineResults[2] = spec.getDimater();
					nkSplineResults[3] = spec.getHeight();
					nkSplineResults[4] = mscPlEntity.getMscPline();
					nkSplineResults[5] = processEntity.getProcessCode();
					break;
				}
			}else{
				ComBaseSpecEntity spec = mscSplineDao.getSpecInfoBySpecCode(design.getSpecCode(), con);
				nkSplineResults = mscSplineDao.CheckSpline(con, design,
						"2", spec,
						mscPlEntity.getMscPline(), processEntity.getProcessCode());
				nkSplineResults[2] = design.getDimater();
				nkSplineResults[3] = design.getHeight();
				nkSplineResults[4] = mscPlEntity.getMscPline();
				nkSplineResults[5] = processEntity.getProcessCode();
				if(!nkSplineResults[0].equals("")){
					break;
				}
			}
		}
	
		if (msc0SplineResults[0].equals("")
				&& mscnSplineResults[0].equals("")
				&& nkSplineResults[0].equals("")) {
			return msc0SplineResults;
		} else {
			if (!msc0SplineResults[0].equals("")) {
				String objKey = msc0SplineResults[1] + "," + "0" + "," + design.getDimater() + "," + design.getHeight()
						+ ",,";
				msc0SplineResults[4] = objKey;
				return msc0SplineResults; 
			} else if (!mscnSplineResults[0].equals("")) {
				String objKey = mscnSplineResults[1] + "," + "1" + "," + design.getDimater() + "," + design.getHeight()
						+ ",,";
				mscnSplineResults[4] = objKey;
				return mscnSplineResults;
			} else {
				String objKey = nkSplineResults[1] + "," + "2" + "," + nkSplineResults[2] + "," + nkSplineResults[3]
						+ "," + nkSplineResults[4] + "," + nkSplineResults[5];
				nkSplineResults[4] = objKey;
				return nkSplineResults;
			}
		}
	}
	
	
	
	public String[] CheckSpline(Connection con, CraftEntity design, String speclFlg,
			ComBaseSpecEntity spec, String mscPline, String process) throws SQLException {
		SqlBuilder b;
		String dimater = spec.getDimater();
		String height = spec.getHeight();
		if(speclFlg.equals("2")){
			b = new SqlBuilder("MscSplineDao.GetNkPics");
		}else{
			b = new SqlBuilder("MscSplineDao.GetJfPics");
		}
		b.Fill(new String[]{design.getDesignKey(), speclFlg, mscPline, process});

		ArrayList<CraftOrdDesignStdPicEntity> pics = EntityTool.getEntitysBySqlNoClsCon(b.toString(), con,
				CraftOrdDesignStdPicEntity.class);
		MscPhyDao phyDaoImp = new MscPhyDao();
		String[] results = new String[] { "", "", "", "", "", ""};
		for (CraftOrdDesignStdPicEntity pic : pics) {
			GenericEntity stdCodeTest = EntityTool.getEntityBySqlNoClsCon(new SqlBuilder(
					"MscSplineDao.getStdCodeTest").Fill(new String[]{
							design.getDesignKey(), pic.getPic(), 
							pic.getPhyCodeMin(), pic.getItemCodeF(),
							pic.getItemCodeS(), pic.getItemCodeW()
					}).toString(), con, GenericEntity.class);
			
			String splineSql = new CorePhySample().QuerySplineSql(pic.getPhyCodeMin(), pic.getItemCodeF(),
					pic.getItemCodeS(), stdCodeTest.getValue(), design.getOrdLnPk(), dimater, height);
			List<ComBaseSplineEntity> baseSplines = EntityTool.getEntitysBySqlNoClsCon(splineSql, con, ComBaseSplineEntity.class);
			Object[] objs = CorePhySample.FilterSplineBySpec(baseSplines, dimater, height);
			List<ComBaseSplineEntity> baseSpline = (List<ComBaseSplineEntity>)objs[1];
			for(int i = baseSpline.size() - 1; i >= 0; i--){
				if(!baseSpline.get(i).getStdCode().equals("") && !baseSpline.get(i).getStdCode().equals(stdCodeTest.getValue())){
					baseSpline.remove(i);
				}
			}
			//移除重复的EIC
			for(int i = baseSpline.size() - 1; i >= 0; i--){
				int existCnt = 0;
				for(ComBaseSplineEntity pline : baseSpline){
					if(pline.getEic().equals(baseSpline.get(i).getEic())){
						existCnt++;
					}
				}
				if(existCnt > 1){
					baseSpline.remove(i);
				}
			}
			
			String mscStr = "";
			if (speclFlg.equals("0")) {
				mscStr = design.getMsc0();
			} else if (speclFlg.equals("1")) {
				mscStr = design.getMscN();
			} else {
				mscStr = "内控";
			}
			
			if(baseSpline.size() > 1){
				//获取设定试样码
				b = new SqlBuilder("MscSplineDao.GetSetEicSpline");
				b.Fill(new String[]{design.getOrdLnPk(), pic.getPhyCodeMin(), pic.getItemCodeF(),
						pic.getItemCodeW(), pic.getItemCodeS()});
				ComBaseSplineEntity setEicSpline = EntityTool.getEntityBySqlNoClsCon(b.toString(), con, ComBaseSplineEntity.class);
				if(setEicSpline != null){
					boolean existsSetEic = false;
					for(ComBaseSplineEntity baseSplineC : baseSpline){
						if(baseSplineC.getEic().equals(setEicSpline.getEic())){
							CraftOrdDesignStdPicSmpEntity smp = new CraftOrdDesignStdPicSmpEntity();
							smp.setLength(setEicSpline.getLength());
							smp.setWidth(setEicSpline.getWidth());
							smp.setSampleStyle(setEicSpline.getSampleStyle());
							smp.setEic(setEicSpline.getEic());
							smp.setPhyDir(setEicSpline.getPhyDir());
							smp.setSizeDesc(setEicSpline.getSizeDesc());
							smp.setSplineItemCodeF(setEicSpline.getItemCodeF());
							smp.setSplineItemNameF(setEicSpline.getItemNameF());
							smp.setSplineItemCodeS(setEicSpline.getItemCodeS());
							smp.setSplineItemNameS(setEicSpline.getItemNameS());
							smp.setDesginKey(design.getDesignKey());
							smp.setPic(pic.getPic());
							smp.setPhyCodeMin(pic.getPhyCodeMin());
							smp.setItemCodeF(pic.getItemCodeF());
							smp.setItemCodeC(pic.getItemCodeS());
							smp.setItemCodeW(pic.getItemCodeW());
							smp.setMscPline(mscPline);
							smp.setProcessCode(process);
							phyDaoImp.savePicSpline(con, smp);
							existsSetEic = true;
							break;
						}
					}
					if(existsSetEic){
						continue;
					}
				}
			}
			
			if(baseSpline.size() == 0){
				results[0] = mscStr + "  PIC:" + pic.getPic() + " 试样组：" + pic.getPhyNameMin() + " 试验方向：" + pic.getItemNameF()
				+ " 试验尺寸：" + pic.getItemNameS() + "(产品标准:" + design.getStdName() 
				+ "，外径：" + design.getDimater() + "，壁厚：" + design.getHeight() + "，品名：" + design.getProducDesc() + "，钢级："
				+ design.getSteelName() + ")" + " 找不到试样要求！";
				results[1] = pic.getPic();
				break;
			}else if(baseSpline.size() > 1){
				results[0] = mscStr + "  PIC:" + pic.getPic() + " 试样组：" + pic.getPhyNameMin() + " 试验方向：" + pic.getItemNameF()
				+ " 试验尺寸：" + pic.getItemNameS() + "(产品标准:" + design.getStdName()
			    + "，外径："+ design.getDimater() + "，壁厚：" + design.getHeight() + "，品名：" + design.getProducDesc() + "，钢级："
				+ design.getSteelName() +")" + " 找到多条试样要求！";
				results[1] = pic.getPic();
				break;
			}else{
				CraftOrdDesignStdPicSmpEntity smp = new CraftOrdDesignStdPicSmpEntity();
				smp.setLength(baseSpline.get(0).getLength());
				smp.setWidth(baseSpline.get(0).getWidth());
				smp.setSampleStyle(baseSpline.get(0).getSampleStyle());
				smp.setEic(baseSpline.get(0).getEic());
				smp.setPhyDir(baseSpline.get(0).getPhyDir());
				smp.setSizeDesc(baseSpline.get(0).getSizeDesc());
				smp.setSplineItemCodeF(baseSpline.get(0).getItemCodeF());
				smp.setSplineItemNameF(baseSpline.get(0).getItemNameF());
				smp.setSplineItemCodeS(baseSpline.get(0).getItemCodeS());
				smp.setSplineItemNameS(baseSpline.get(0).getItemNameS());
				smp.setDesginKey(design.getDesignKey());
				smp.setPic(pic.getPic());
				smp.setPhyCodeMin(pic.getPhyCodeMin());
				smp.setItemCodeF(pic.getItemCodeF());
				smp.setItemCodeC(pic.getItemCodeS());
				smp.setItemCodeW(pic.getItemCodeW());
				smp.setProcessCode(process);
				smp.setMscPline(mscPline);
				phyDaoImp.savePicSpline(con, smp);
			}
		}

		return results;
	}
	
	public String[] getUnit(Connection con){
		String[] results = new String[2];
		String sql = CoreXmlSqlParsersFactory.getSql("CraftSplineUnit.queryUnit");
		try{
			Statement st = con.createStatement();
			ResultSet rs = st.executeQuery(sql);
			if(rs.next()){
				results[0] = rs.getString("UNIT_CODE");
				results[1] = rs.getString("UNIT_DESC");
			}
			rs.close();
			st.close();
		}catch(Exception ex){
			
		}
		return results;
	}
	
//	public GenericEntity getDimaterHeightBySpecCode(String specCode, Connection con){
//		SqlBuilder b = new SqlBuilder("MscSplineDao.getDimaterHeightBySpecCode");
//		b.Fill(new String[]{specCode});
//		GenericEntity spec = EntityTool.getEntityBySqlNoClsCon(b.toString(), con, GenericEntity.class);
//		return spec;
//	}
	
	public ComBaseSpecEntity getSpecInfoBySpecCode(String specCode, Connection con){
		SqlBuilder b = new SqlBuilder("MscSplineDao.getSpecInfoBySpecCode");
		b.Fill(new String[]{specCode});
		ComBaseSpecEntity spec = EntityTool.getEntityBySqlNoClsCon(b.toString(), con, ComBaseSpecEntity.class);
		
		return spec;
	}
	
	public List<CraftOrdDesignMscPlEntity> getCraftMscPl(String designKey, Connection con){
		SqlBuilder b = new SqlBuilder("MscSplineDao.getCraftMscPl");
		b.Fill(new String[]{designKey});
		return EntityTool.getEntitysBySqlNoClsCon(b.toString(), con, CraftOrdDesignMscPlEntity.class);
	}

	// public String[] CheckSplineMscn(Connection con, CraftEntity design,
	// String designKey) throws SQLException{
	// MscPhyDao phyDaoImp = new MscPhyDao();
	//
	// String sqlGetMscnPics = CoreXmlSqlParsersFactory.getSql("");
	// sqlGetMscnPics = StringTool.ReplaceAll(sqlGetMscnPics, "##", designKey);
	// ArrayList<CraftOrdDesignStdPicEntity> mscnPics =
	// EntityTool.getEntitysBySqlNoClsCon(
	// sqlGetMscnPics, con, CraftOrdDesignStdPicEntity.class);
	// String[] results = new String[2];
	// for(CraftOrdDesignStdPicEntity mscnPic : mscnPics){
	// List<CraftOrdDesignStdPicSmpEntity> picSplineEntity =
	// phyDaoImp.getPicSpline(
	// con, mscnPic.getPic(), mscnPic.getPhyCodeMin(), design.getStdCode(),
	// design.getProducCode(), design.getSteelCode(), "1",
	// mscnPic.getItemCodeF(),
	// mscnPic.getItemCodeS(), mscnPic.getItemCodeW());
	//
	// CraftOrdDesignStdPicSmpEntity smp = MscPhy.checkSpline(picSplineEntity,
	// design.getSteelCode(), design.getDimater(), design.getHeight());
	//
	// if(smp.getEic().equals("")){
	// results[0] = design.getMsc0() + " 试样组：" + mscnPic.getPhyNameMin()
	// + " 试验方向：" + mscnPic.getItemNameF() + " 试验尺寸：" + mscnPic.getItemNameS()
	// + " 试验温度：" + mscnPic.getItemNameW() + "(外径："+ design.getDimater()
	// +",壁厚："+ design.getHeight() + ")"
	// + " 找不到试验要求！";
	// results[1] = mscnPic.getPic();
	// break;
	// }else if(smp.getSplineCount() >= 2){
	// results[0] = design.getMsc0() + " 试样组：" + mscnPic.getPhyNameMin()
	// + " 试验方向：" + mscnPic.getItemNameF() + " 试验尺寸：" + mscnPic.getItemNameS()
	// + " 试验温度：" + mscnPic.getItemNameW() + "(外径："+ design.getDimater()
	// +",壁厚："+ design.getHeight() + ")"
	// + " 找到多条试验要求！";
	// results[1] = mscnPic.getPic();
	// break;
	// }
	// }
	//
	// return results;
	// }
	//
	// public String[] CheckSplineNk(Connection con, CraftEntity design, String
	// designKey) throws SQLException{
	// String sqlGetNkPics = CoreXmlSqlParsersFactory.getSql("");
	// sqlGetNkPics = StringTool.ReplaceAll(sqlGetNkPics, "##", designKey);
	// ArrayList<CraftOrdDesignStdPicEntity> nkPics =
	// EntityTool.getEntitysBySqlNoClsCon(
	// sqlGetNkPics, con, CraftOrdDesignStdPicEntity.class);
	// MscPhyDao phyDaoImp = new MscPhyDao();
	// String[] results = new String[2];
	// for(CraftOrdDesignStdPicEntity nkPic : nkPics){
	// List<CraftOrdDesignStdPicSmpEntity> picSplineEntity =
	// phyDaoImp.getPicSpline(
	// con, nkPic.getPic(), nkPic.getPhyCodeMin(), design.getStdCode(),
	// design.getProducCode(), design.getSteelCode(), "0", nkPic.getItemCodeF(),
	// nkPic.getItemCodeS(), nkPic.getItemCodeW());
	// CraftOrdDesignStdPicSmpEntity smp = MscPhy.checkSpline(picSplineEntity,
	// design.getSteelCode(), design.getDimater(), design.getHeight());
	//
	//
	// if(smp.getEic().equals("")){
	// results[0] = design.getMsc0() + " 试样组：" + nkPic.getPhyNameMin()
	// + " 试验方向：" + nkPic.getItemNameF() + " 试验尺寸：" + nkPic.getItemNameS()
	// + " 试验温度：" + nkPic.getItemNameW() + "(外径："+ design.getDimater() +",壁厚："+
	// design.getHeight() + ")"
	// + " 找不到试验要求！";
	// results[1] = nkPic.getPic();
	// break;
	// }else if(smp.getSplineCount() >= 2){
	// results[0] = design.getMsc0() + " 试样组：" + nkPic.getPhyNameMin()
	// + " 试验方向：" + nkPic.getItemNameF() + " 试验尺寸：" + nkPic.getItemNameS()
	// + " 试验温度：" + nkPic.getItemNameW() + "(外径："+ design.getDimater() +",壁厚："+
	// design.getHeight() + ")"
	// + " 找到多条试验要求！";
	// results[1] = nkPic.getPic();
	// break;
	// }
	// }
	//
	// return results;
	// }
}
