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

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

import com.steering.pss.sale.order.craftDesign.dao.MscJGDao;
import com.steering.pss.sale.order.craftDesign.main.CraftDesignMain;
import com.steering.pss.sale.order.craftDesign.model.ComMscJgEntity;
import com.steering.pss.sale.order.craftDesign.model.ComMscRJgTorqueEntity;
import com.steering.pss.sale.order.craftDesign.model.CraftOrdDesignMscJgBlEntity;
import com.steering.pss.sale.order.craftDesign.model.CraftOrdDesignMscJgEntity;
import com.steering.pss.sale.order.qualityDesign.model.SlmOrdDesignMscJgEntity;

/**
 * 接箍
 * 
 * @author Administrator
 * 
 */
public class MscJG {
	/**
	 * 验证接箍码
	 * 
	 * @param con
	 *            连接字符串
	 * @param msc
	 *            冶金规范码
	 * @param dimater
	 *            外径
	 * @param height
	 *            壁厚
	 * @param steelCode
	 *            钢级代码
	 * @param steelName
	 *            钢级名称
	 * @param specCode
	 *            产品规格
	 * @param specName
	 *            产品规格名称
	 * @param specStyle
	 *            产品规格类型
	 * @param designKey
	 *            主键
	 * @return
	 * @throws SQLException
	 */
	public Object[] checkMscJG(Connection con, String msc, String dimater,
			String height, String steelCode, String steelName,
			String modelCode, String modelDesc, String specCode,
			String specName, String specStyle, String designKey,
			String orderLinePk, List<CraftOrdDesignMscJgEntity> codeJGs,
			String userName, String designKeyFlag, String flag,
			String operateType, String designStyle) throws SQLException {
		MscJGDao mscJGDaoImp = new MscJGDao();
		Object[] jgInfo = new Object[4];
		String errMessage = "";
		
		//设定接箍处理
		if(codeJGs != null)
		{
			boolean clearSetJg = false;
			for(CraftOrdDesignMscJgEntity codeJG : codeJGs){
				ComMscJgEntity mscJg = mscJGDaoImp.getJGBaseInfo(con, codeJG.getCodeJg());
				if(mscJg.getValidflag().equals("0")){
					errMessage = "接箍码" + mscJg.getCodeJg() + "已被作废，请重新设定其他接箍码！";
					jgInfo[0] = errMessage;
					jgInfo[1] = "0";
					return jgInfo;
				}
				if(mscJGDaoImp.getCountByKeyJg(con, codeJG.getKeyJg()).equals("0")){
					mscJGDaoImp.deleteJg(con, designKey);
					clearSetJg = true;
					break;
				}
				if (specStyle.equals("C")) {
					codeJGs = mscJGDaoImp.getJGBySpecCode(con, msc, steelCode, modelDesc, specCode, orderLinePk, codeJG.getKeyJg());
				}else{
					codeJGs = mscJGDaoImp.getJGByDAndH(con, msc, steelCode, modelDesc, dimater, height, orderLinePk, codeJG.getKeyJg());
				}
				if(codeJGs == null || codeJGs.size() == 0){
					errMessage = "设定的接箍码" + codeJG.getCodeJg() + "接箍KEY" + codeJG.getKeyJg() + "：不符合合同信息(产品规格:" +
							specName + "、钢级:" + steelName + "、扣型:" + modelDesc + ")，或者技术特殊要求与合同不一致！";
					jgInfo[0] = errMessage;
					jgInfo[1] = "0";
					return jgInfo;
				}
			}
			if(clearSetJg){
				codeJGs.clear();
			}
		}
		
		if (codeJGs == null || codeJGs.size() == 0) {
			// 根据产品类型确定如何查询接箍码
			if (specStyle.equals("C")) {
				codeJGs = mscJGDaoImp.getJGBySpecCode(con, msc, steelCode,
						modelDesc, specCode, orderLinePk, "");
				if (codeJGs == null || codeJGs.size() == 0) {
					errMessage = "没有维护符合( 产品规格:" + specName + "、 钢级:"
							+ steelName + "、扣型："+  modelDesc +")的接箍码, 或者技术特殊要求与合同不一致!";
					jgInfo[0] = errMessage;
					jgInfo[1] = "0";
					return jgInfo;
				}
				if (codeJGs.size() > 1) {
					errMessage = "维护了多个符合( 产品规格：" + specName + "、钢级："
							+ steelName + "、扣型："+  modelDesc +")的接箍码,请设定接箍!";
					jgInfo[0] = errMessage;
					jgInfo[1] = "1";
					return jgInfo;
				}
			} else {
				codeJGs = mscJGDaoImp.getJGByDAndH(con, msc, steelCode,
						modelDesc, dimater, height, orderLinePk, "");
				if (codeJGs == null || codeJGs.size() == 0) {
					errMessage = "没有维护符合( 外径:" + dimater + "、 壁厚:" + height
							+ "、钢级:" + steelName + "、扣型："+  modelDesc +")的接箍码, 或者技术特殊要求与合同不一致!";
					jgInfo[0] = errMessage;
					jgInfo[1] = "0";
					return jgInfo;
				}
				if (codeJGs == null || codeJGs.size() > 1) {
					errMessage = "维护了多个符合( 外径:" + dimater + "、 壁厚:" + height
							+ "、 钢级:" + steelName + "、扣型："+  modelDesc +")的接箍码,请设定接箍!";
					jgInfo[0] = errMessage;
					jgInfo[1] = "1";
					return jgInfo;
				}
			}
		}

		// 查询接箍码信息(通过接箍KEY)
		CraftOrdDesignMscJgEntity jgEntity = mscJGDaoImp.getJGInfo(con, codeJGs
				.get(0).getKeyJg());
		if (jgEntity == null) {
			errMessage = "找不到接箍主键(" + codeJGs.get(0).getKeyJg() + ")与冶金规范码("
					+ msc + ")的关联信息";
			jgInfo[0] = errMessage;
			jgInfo[1] = "0";
			return jgInfo;
		}

		//获取拧接信息
		if (!specStyle.equals("C")) {
			ArrayList<ComMscRJgTorqueEntity> torques = mscJGDaoImp.getTorque(con, jgEntity.getKeyJg(),
					dimater, height);
			if(torques.size() > 1){
				errMessage = "接箍码" + jgEntity.getCodeJg() + "接箍KEY" + jgEntity.getKeyJg() +
						"找到多行拧接记录，请检查规格范围";
				jgInfo[0] = errMessage;
				jgInfo[1] = "0";
				return jgInfo;
			}
			if(torques.size() > 0){
				ComMscRJgTorqueEntity torque = torques.get(0);
				jgEntity.setTorqueUnit(torque.getTorqueUnit());
				jgEntity.setTorqueMin(torque.getTorqueMin());
				jgEntity.setTorqueMax(torque.getTorqueMax());
				jgEntity.setTorqueBest(torque.getTorqueBest());
				jgEntity.setPointMin(torque.getPointMin());
				jgEntity.setPointMax(torque.getPointMax());
			}
		}

		// 将设定接箍字段赋值
		if (codeJGs != null && codeJGs.size() > 0) {
			jgEntity.setSetJg("1");
		}
		// 验证接箍基础信息
		ComMscJgEntity comMscJgEntity = mscJGDaoImp.getJGBaseInfo(con,
				jgEntity.getCodeJg());
		if (comMscJgEntity == null) {
			errMessage = "接箍码(" + jgEntity.getCodeJg() + ")在接箍基础表不存在";
			jgInfo[0] = errMessage;
			jgInfo[1] = "0";
			return jgInfo;
		}
		if (comMscJgEntity.getCheckJVal().equals("")
				|| Double.parseDouble(comMscJgEntity.getCheckJVal()) <= 0
				|| comMscJgEntity.getWeightJg().equals("")
				|| Double.parseDouble(comMscJgEntity.getWeightJg()) <= 0) {
			errMessage = "接箍码(" + jgEntity.getCodeJg() + ")的单重与测量J值都必须大于0!";
			jgInfo[0] = errMessage;
			jgInfo[1] = "0";
			return jgInfo;
		}
		if (comMscJgEntity.getHeightJg().equals("")) {
			errMessage = "接箍码(" + jgEntity.getCodeJg() + ")的临界壁厚不能为空!";
			jgInfo[0] = errMessage;
			jgInfo[1] = "0";
			return jgInfo;
		}
		jgEntity.setDesginKey(designKey);
		List<CraftOrdDesignMscJgBlEntity> jgblEntitys = new ArrayList<CraftOrdDesignMscJgBlEntity>();

		// 查询接箍码对应的备料码
		List<CraftOrdDesignMscJgBlEntity> codeBLs = mscJGDaoImp.getJGBL(con,
				jgEntity.getCodeJg(), jgEntity.getCustAlphaNo(),
				jgEntity.getAlphaCode());
		if (codeBLs == null || codeBLs.size() == 0) {
			errMessage = "接箍码(" + jgEntity.getCodeJg() + ") 接箍主键(" + jgEntity.getKeyJg() + "没有维护备料码!";
			jgInfo[0] = errMessage;
			jgInfo[1] = "0";
			return jgInfo;
		}
		// 建立接箍码与备料码关系
		for (CraftOrdDesignMscJgBlEntity codeBL : codeBLs) {
			String[] blInfo = mscJGDaoImp.getJGBLInfo(con, codeBL.getPscBl());
			if (blInfo == null || blInfo[2].equals("2")) {
				CraftDesignMain craftDesignMain = new CraftDesignMain();
				craftDesignMain.craftDesign(codeBL.getPscBl(), userName,
						designKeyFlag, "1", operateType, designStyle);
			}
			blInfo = mscJGDaoImp.getJGBLInfo(con, codeBL.getPscBl());
			if (!blInfo[2].equals("1")) {
				errMessage = "接箍码(" + jgEntity.getCodeJg() + ") 接箍主键(" + jgEntity.getKeyJg() + ")的备料码("
						+ codeBL.getPscBl() + ")未通过工艺评审!";
				jgInfo[0] = errMessage;
				jgInfo[1] = "0";
				return jgInfo;
			}
			String blDesignKey = blInfo[1];
			// 接箍备料关系
			CraftOrdDesignMscJgBlEntity jgblEntity = new CraftOrdDesignMscJgBlEntity();
			jgblEntity.setCodeJg(jgEntity.getKeyJg());
			jgblEntity.setDesginKey(designKey);
			jgblEntity.setPscBl(codeBL.getPscBl());
			jgblEntity.setDesginKeyBl(blDesignKey);
			jgblEntity.setCustAlphaNo(codeBL.getCustAlphaNo());
			jgblEntity.setOrderSpecialDesc(codeBL.getOrderSpecialDesc());
			jgblEntity.setAlphaCode(codeBL.getAlphaCode());
			jgblEntitys.add(jgblEntity);
		}
		jgInfo[0] = errMessage;
		jgInfo[1] = "0";
		jgInfo[2] = jgEntity;
		jgInfo[3] = jgblEntitys;
		return jgInfo;
	}
}
