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.StringTool;
import com.steering.pss.qcm.model.ComBaseSpecEntity;
import com.steering.pss.qcm.model.PscEntity;
import com.steering.pss.qcm.model.SlmOrdDesignMscJgEntity;
import com.steering.pss.qcm.model.SlmOrderLineEntity;
import CoreFS.SA06.CoreReturnObject;
import CoreFS.SA08.CoreXmlSqlParsersFactory;
import CoreFS.SC01.CoreConstantData;

public class ComMscRJgDAL extends DBCommon{
	
	public CoreReturnObject getMscRJgBySpec(String ordLnPk){
		Connection con = this.getDao(CoreConstantData.sysbeanstr).getConnection();
		String sqlOrderLine = CoreXmlSqlParsersFactory.getSql("ComMscRJgDAL.getSLmOrderLineByPk");
		sqlOrderLine = StringTool.ReplaceAll(sqlOrderLine, "#ORD_LN_PK#", ordLnPk);
		SlmOrderLineEntity orderLineEntity = EntityTool.getEntityBySql(sqlOrderLine, con, SlmOrderLineEntity.class);
		String sqlBaseSpec = CoreXmlSqlParsersFactory.getSql("ComMscRJgDAL.getComBaseSpecByPk");
		sqlBaseSpec = StringTool.ReplaceAll(sqlBaseSpec, "#SPEC_CODE#", orderLineEntity.getSpecCode());
		con = this.getDao(CoreConstantData.sysbeanstr).getConnection();
		ComBaseSpecEntity baseSpecEntity = EntityTool.getEntityBySql(sqlBaseSpec, con, ComBaseSpecEntity.class);
		String sqlPsc = CoreXmlSqlParsersFactory.getSql("ComMscRJgDAL.getComPscByPk");
		sqlPsc = StringTool.ReplaceAll(sqlPsc, "#PSC#", orderLineEntity.getPsc());
		con = this.getDao(CoreConstantData.sysbeanstr).getConnection();
		PscEntity pscEntity = EntityTool.getEntityBySql(sqlPsc, con, PscEntity.class);
		if(baseSpecEntity.getMscStyle().equals("C")){
			String sqlGetMscRJgBySpec2 = CoreXmlSqlParsersFactory.getSql("ComMscRJgDAL.getMscRJgBySpec2");
			sqlGetMscRJgBySpec2 = StringTool.ReplaceAll(sqlGetMscRJgBySpec2, "#MSC#", orderLineEntity.getMsc());
			sqlGetMscRJgBySpec2 = StringTool.ReplaceAll(sqlGetMscRJgBySpec2, "#SPEC_CODE#", baseSpecEntity.getSpecCode());
			sqlGetMscRJgBySpec2 = StringTool.ReplaceAll(sqlGetMscRJgBySpec2, "#STEEELCODE#", pscEntity.getSteelcode());
			sqlGetMscRJgBySpec2 = StringTool.ReplaceAll(sqlGetMscRJgBySpec2, "#CUST_ALPHA_NO#", orderLineEntity.getCustAlphaNo2());
			return this.getData(sqlGetMscRJgBySpec2);
		}else{
			String sqlGetMscRJgBySpec = CoreXmlSqlParsersFactory.getSql("ComMscRJgDAL.getMscRJgBySpec");
			sqlGetMscRJgBySpec = StringTool.ReplaceAll(sqlGetMscRJgBySpec, "#MSC#", orderLineEntity.getMsc());
			sqlGetMscRJgBySpec = StringTool.ReplaceAll(sqlGetMscRJgBySpec, "#HEIGHT#", baseSpecEntity.getHeight());
			sqlGetMscRJgBySpec = StringTool.ReplaceAll(sqlGetMscRJgBySpec, "#DIMATER#", baseSpecEntity.getDimater());
			sqlGetMscRJgBySpec = StringTool.ReplaceAll(sqlGetMscRJgBySpec, "#STEELCODE#", pscEntity.getSteelcode());
			sqlGetMscRJgBySpec = StringTool.ReplaceAll(sqlGetMscRJgBySpec, "#CUST_ALPHA_NO#", orderLineEntity.getCustAlphaNo2());
			return this.getData(sqlGetMscRJgBySpec);
		}
	}
	
	/**
	 * 插入到质量设计接箍和冶金关系表
	 * @return
	 */
	public CoreReturnObject insertDesignMscRJg(ArrayList<String> listJSON, String ordLnPk, String isCraft){
		ArrayList<String> sqllist = new ArrayList<String>();
		String sqlDelete = "";
		if(isCraft.equals("True")){
			sqlDelete = CoreXmlSqlParsersFactory.getSql("ComMscRJgDAL.delCraftMscRJg");
		}else{
			sqlDelete = CoreXmlSqlParsersFactory.getSql("ComMscRJgDAL.delDesignMscRJg");
		}
		
		sqlDelete = StringTool.ReplaceAll(sqlDelete, "#ORD_LN_PK#", ordLnPk);
		sqllist.add(sqlDelete);
		for(String strJSON : listJSON){
			SlmOrdDesignMscJgEntity mscRJgEntity = EntityTool.getEntityByJSONStr(strJSON, SlmOrdDesignMscJgEntity.class);
			String sql = "";
			if(isCraft.equals("True")){
				sql = CoreXmlSqlParsersFactory.getSql("ComMscRJgDAL.insertCraftMscRJg");
			}else{
				sql = CoreXmlSqlParsersFactory.getSql("ComMscRJgDAL.insertDesignMscRJg");
			}
			sql = EntityTool.fillSqlByEntity(sql, mscRJgEntity);
			sql = StringTool.ReplaceAll(sql, "#ORD_LN_PK#", ordLnPk);
			sqllist.add(sql);
		}
		return this.getDao(CoreConstantData.sysbeanstr).ExcuteTransction(sqllist);
	}
	
	/**
	 * 查询设定接箍
	 * @return
	 */
	public CoreReturnObject getKeyJgByOrdLnPk(String ordLnPk, String isCraft){
		String sql = "";
		if(isCraft.equals("True")){
			sql = CoreXmlSqlParsersFactory.getSql("ComMscRJgDAL.getCraftKeyJgByOrdLnPk");
		}else
		{
			sql = CoreXmlSqlParsersFactory.getSql("ComMscRJgDAL.getKeyJgByOrdLnPk");
		}
		sql = StringTool.ReplaceAll(sql, "#ORD_LN_PK#", ordLnPk);
		return this.getData(sql);
	}
}
