package com.steering.pss.sale.order.qualityDesign.server.iserver;

import java.sql.Connection;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
import com.steering.pss.sale.order.qualityDesign.Dao.factory.FactoryDao;
import com.steering.pss.sale.order.qualityDesign.Dao.imp.MscJGDaoImp;
import com.steering.pss.sale.order.qualityDesign.model.ComMscJgEntity;
import com.steering.pss.sale.order.qualityDesign.model.SlmOrdDesignMscJgBlEntity;
import com.steering.pss.sale.order.qualityDesign.model.SlmOrdDesignMscJgEntity;
import com.steering.pss.sale.order.qualityDesign.server.imp.MscJGImp;

public class IMscJG implements MscJGImp {

	/**
	 * 验证接箍码
	 * 
	 * @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<SlmOrdDesignMscJgEntity> codeJGs) throws SQLException {
		MscJGDaoImp mscJGDaoImp = FactoryDao.getMscJGDaoImp();
		Object[] jgInfo = new Object[4];
		String errMessage = "";
		
		//设定接箍处理
		if(codeJGs != null){
			boolean clearSetJg = false;
			for(SlmOrdDesignMscJgEntity 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)
		SlmOrdDesignMscJgEntity jgEntity = mscJGDaoImp.getJGInfo(con, codeJGs.get(0).getKeyJg());
		if (jgEntity == null) {
			errMessage = "找不到 接箍码(" + codeJGs.get(0).getCodeJg() + ")/接箍主键(" + codeJGs.get(0).getKeyJg() + ")与冶金规范码("
					+ msc + ")的关联信息!";
			jgInfo[0] = errMessage;
			jgInfo[1] = "0";
			return jgInfo;
		}

		// 查询接箍码基础信息(通过接箍码)
		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;
		}
		if (codeJGs != null && codeJGs.size() > 0) {
			jgEntity.setSetJg("1");
		}
		jgEntity.setDesginKey(designKey);
		List<SlmOrdDesignMscJgBlEntity> jgblEntitys = new ArrayList<SlmOrdDesignMscJgBlEntity>();

		// 查询接箍码对应的备料码
		List<SlmOrdDesignMscJgBlEntity> codeBLs = mscJGDaoImp.getJGBL(con, jgEntity.getCodeJg(), jgEntity.getCustAlphaNo(), jgEntity.getAlphaCode());
		if (codeBLs == null || codeBLs.size() == 0) {
			errMessage = "接箍码(" + codeJGs.get(0).getCodeJg() + ")/ 接箍主键(" + jgEntity.getKeyJg() + ")没有维护备料码!";
			jgInfo[0] = errMessage;
			jgInfo[1] = "0";
			return jgInfo;
		}
		// 建立接箍码与备料码关系
		for (SlmOrdDesignMscJgBlEntity codeBL : codeBLs) {
			String[] blInfo = mscJGDaoImp.getJGBLInfo(con, codeBL.getPscBl());
			if (blInfo == null) {
				errMessage = "接箍码(" + codeJGs.get(0).getCodeJg() + ")/接箍主键(" + jgEntity.getKeyJg() + ")的备料码(" + codeBL.getPscBl()
						+ ")在基础备料信息里不存在!";
				jgInfo[0] = errMessage;
				jgInfo[1] = "0";
				return jgInfo;
			}
			String blDesignKey = blInfo[0];
			String blStatus = blInfo[1];
			if (!blStatus.equals("2")) {
				errMessage = "接箍码(" + codeJGs.get(0).getCodeJg() + ")/接箍主键(" + jgEntity.getKeyJg() + ")的备料码(" + codeBL.getPscBl()
						+ ")未通过技术评审,请先评审备料码!";
				jgInfo[0] = errMessage;
				jgInfo[1] = "0";
				return jgInfo;
			}
			// 接箍备料关系
			SlmOrdDesignMscJgBlEntity jgblEntity = new SlmOrdDesignMscJgBlEntity();
			jgblEntity.setCodeJg(jgEntity.getKeyJg());
			jgblEntity.setDesginKey(designKey);
			jgblEntity.setPscBl(codeBL.getPscBl());
			jgblEntity.setCustAlphaNo(codeBL.getCustAlphaNo());
			jgblEntity.setOrderSpecialDesc(codeBL.getOrderSpecialDesc());
			jgblEntity.setAlphaCode(codeBL.getAlphaCode());
			jgblEntity.setDesginKeyBl(blDesignKey);
			jgblEntitys.add(jgblEntity);
		}

		jgInfo[0] = errMessage;
		jgInfo[1] = "0";
		jgInfo[2] = jgEntity;
		jgInfo[3] = jgblEntitys;
		return jgInfo;
	}
}
