package com.steering.pss.qcm;

 
import java.sql.Connection;
import java.util.ArrayList;
import CoreFS.SA06.CoreReturnObject;
import CoreFS.SA08.CoreXmlSqlParsersFactory;
import CoreFS.SC01.CoreConstantData;

import com.steering.comm.db.DBCommon;
import com.steering.comm.tools.EntityTool;
import com.steering.comm.tools.StringTool;
import com.steering.pss.qcm.model.GenericEntity;

public class CoreSpecificationsGrade extends DBCommon {

	public CoreReturnObject doSave(ArrayList<ArrayList<Object>> spbAddParams,
			ArrayList<ArrayList<Object>> spbUpdateParams,
			ArrayList<ArrayList<Object>> spbSupplementAddParams,
			ArrayList<ArrayList<Object>> spbSupplementUpdateParams) {

		CoreReturnObject spbCro = new CoreReturnObject();

		String spbAddSql = CoreXmlSqlParsersFactory
				.getSql("CoreSpecificationsGrade.ADD");

		String spbUpdateSql = CoreXmlSqlParsersFactory
				.getSql("CoreSpecificationsGrade.MODIFY");

		String spbSupplementAddSql = CoreXmlSqlParsersFactory
				.getSql("CoreSpecificationsBasicsSupplement.ADD");

		String spbSupplementUpdateSql = CoreXmlSqlParsersFactory
				.getSql("CoreSpecificationsBasicsSupplement.MODIFY");

		ArrayList<Object> spbSqlList = new ArrayList<Object>();
		ArrayList<Object[]> spbParmList = new ArrayList<Object[]>();

		// 构建主表addsql语句和参数。

		if (spbAddParams.size() > 0) {
			for (int i = 0; i < spbAddParams.size(); i++) {
				spbSqlList.add(spbAddSql);
				spbParmList.add(spbAddParams.get(i).toArray());
			}
		}

		// 构建主表修改sql语句和参数。

		if (spbUpdateParams.size() > 0) {
			for (int j = 0; j < spbUpdateParams.size(); j++) {
				spbSqlList.add(spbUpdateSql);
				spbParmList.add(spbUpdateParams.get(j).toArray());
			}
		}
		// 构建从表新增sql语句和参数。

		if (spbSupplementAddParams.size() > 0) {
			for (int k = 0; k < spbSupplementAddParams.size(); k++) {
				spbSqlList.add(spbSupplementAddSql);
				spbParmList.add(spbSupplementAddParams.get(k).toArray());
			}
		}

		// 构建从表修改sql语句和参数。
		if (spbSupplementAddParams.size() > 0) {
			for (int g = 0; g < spbSupplementUpdateParams.size(); g++) {
				spbSqlList.add(spbSupplementUpdateSql);
				spbParmList.add(spbSupplementUpdateParams.get(g).toArray());
			}
		}

		try {
			spbCro = this.getDao("TgDao").ExcuteTransction(spbSqlList,
					spbParmList);
		} catch (Exception e) {
			spbCro = spbCro.SetErrInfo(e.getMessage());
		}

		return spbCro;

	}

	/**
	 * 产品规格基础信息查询
	 * 
	 * @param includeDeleted
	 *            是否包含已删除数据
	 * @param name
	 *            产品规格描述
	 * @return CoreReturnObject 必须返回CoreReturnObject
	 */
	public CoreReturnObject getLineList(Boolean includeDeleted, String name, String gradeName) {
		String deletedFlag = includeDeleted ? "0" : "";
		Object[] params = new Object[] { deletedFlag, name, gradeName, gradeName };
		return this.getDataBySqlID("CoreSpecificationsGrade.QUERY", params);
	}

	@SuppressWarnings("unchecked")
	public CoreReturnObject save(ArrayList<ArrayList<Object>> listGrds) {
		ArrayList<String> sqllist = new ArrayList<String>();
		String gradeCode = "";
		String sqlGeMaxGradeCode = CoreXmlSqlParsersFactory.getSql("CoreSpecificationsGrade.getMaxGradeCode");
		Connection con = this.getDao(CoreConstantData.sysbeanstr).getConnection();
		ArrayList<GenericEntity> listMaxGradeCode = EntityTool.getEntitysBySql(sqlGeMaxGradeCode, con, GenericEntity.class);
		gradeCode = listMaxGradeCode.get(0).getMaxValue();
		
		boolean pInsert = false;
		for(ArrayList<Object> listGrd : listGrds){
			pInsert = false;
			if(listGrd.size() > 2){
				if(listGrd.get(8).toString().equals("")){
					pInsert = true;
					gradeCode = "GZ" + String.format("%04d", Integer.parseInt(gradeCode.substring(2)) + 1); 
					String sqlInsertGrd = CoreXmlSqlParsersFactory.getSql("CoreSpecificationsGrade.ADD");
					sqlInsertGrd = StringTool.ReplaceAll(sqlInsertGrd, "#GRADECODE#", gradeCode);
					sqlInsertGrd = StringTool.ReplaceAll(sqlInsertGrd, "#GRADENAME#", listGrd.get(0).toString());
					sqlInsertGrd = StringTool.ReplaceAll(sqlInsertGrd, "#ASSORTMENT_CODE#", listGrd.get(1).toString());
					sqlInsertGrd = StringTool.ReplaceAll(sqlInsertGrd, "#ASSORTMENT_NAME#", listGrd.get(2).toString());
					sqlInsertGrd = StringTool.ReplaceAll(sqlInsertGrd, "#COEFFICIENT#", listGrd.get(3).toString());
					sqlInsertGrd = StringTool.ReplaceAll(sqlInsertGrd, "#CREATE_NAME#", listGrd.get(4).toString());
					sqlInsertGrd = StringTool.ReplaceAll(sqlInsertGrd, "#MEMO#", listGrd.get(5).toString());
					sqlInsertGrd = StringTool.ReplaceAll(sqlInsertGrd, "#ALLOW_BUY#", listGrd.get(6).toString());
					sqllist.add(sqlInsertGrd);
				}else{
					gradeCode = listGrd.get(7).toString();
					String sqlUpdateGrd = CoreXmlSqlParsersFactory.getSql("CoreSpecificationsGrade.UPDATE");
					sqlUpdateGrd = StringTool.ReplaceAll(sqlUpdateGrd, "#GRADENAME#", listGrd.get(0).toString());
					sqlUpdateGrd = StringTool.ReplaceAll(sqlUpdateGrd, "#ASSORTMENT_CODE#", listGrd.get(1).toString());
					sqlUpdateGrd = StringTool.ReplaceAll(sqlUpdateGrd, "#ASSORTMENT_NAME#", listGrd.get(2).toString());
					sqlUpdateGrd = StringTool.ReplaceAll(sqlUpdateGrd, "#COEFFICIENT#", listGrd.get(3).toString());
					sqlUpdateGrd = StringTool.ReplaceAll(sqlUpdateGrd, "#UPDATE_NAME#", listGrd.get(4).toString());
					sqlUpdateGrd = StringTool.ReplaceAll(sqlUpdateGrd, "#MEMO#", listGrd.get(5).toString());
					sqlUpdateGrd = StringTool.ReplaceAll(sqlUpdateGrd, "#ALLOW_BUY#", listGrd.get(6).toString());
					sqlUpdateGrd = StringTool.ReplaceAll(sqlUpdateGrd, "#GRADECODE#", listGrd.get(7).toString());
					sqllist.add(sqlUpdateGrd);
				}
			}
			
			Object obj = listGrd.get(listGrd.size() - 1);
			if(obj instanceof ArrayList){
				for(ArrayList<String> listGrdChm : (ArrayList<ArrayList<String>>)listGrd.get(listGrd.size() - 1)){
					String sgradeCode = "";
					if(!pInsert){
						sgradeCode = listGrdChm.get(11);
					}else{
						sgradeCode = gradeCode;
					}
					if(listGrdChm.get(13).toString().equals("")){
						String sqlInertGrdChm = CoreXmlSqlParsersFactory.getSql("CoreSpecificationsGradeChem.ADD");
						sqlInertGrdChm = StringTool.ReplaceAll(sqlInertGrdChm, "#ASSORTMENT_CODE#", listGrdChm.get(0));
						sqlInertGrdChm = StringTool.ReplaceAll(sqlInertGrdChm, "#ASSORTMENT_NAME#", listGrdChm.get(1));
						sqlInertGrdChm = StringTool.ReplaceAll(sqlInertGrdChm, "#COEFFICIENT#", listGrdChm.get(2));
						sqlInertGrdChm = StringTool.ReplaceAll(sqlInertGrdChm, "#STDMIN_SIGN#", listGrdChm.get(3));
						sqlInertGrdChm = StringTool.ReplaceAll(sqlInertGrdChm, "#STDMIN#", listGrdChm.get(4));
						sqlInertGrdChm = StringTool.ReplaceAll(sqlInertGrdChm, "#STDMAX_SIGN#", listGrdChm.get(5));
						sqlInertGrdChm = StringTool.ReplaceAll(sqlInertGrdChm, "#STDMAX#", listGrdChm.get(6));
						sqlInertGrdChm = StringTool.ReplaceAll(sqlInertGrdChm, "#CREATE_NAME#", listGrdChm.get(7));
						sqlInertGrdChm = StringTool.ReplaceAll(sqlInertGrdChm, "#MEMO#", listGrdChm.get(8));
						sqlInertGrdChm = StringTool.ReplaceAll(sqlInertGrdChm, "#GRADENAME#", listGrdChm.get(9));
						sqlInertGrdChm = StringTool.ReplaceAll(sqlInertGrdChm, "#CHEM_NAME#", listGrdChm.get(10));
						sqlInertGrdChm = StringTool.ReplaceAll(sqlInertGrdChm, "#GRADECODE#", sgradeCode);
						sqlInertGrdChm = StringTool.ReplaceAll(sqlInertGrdChm, "#CHEM_CODE#", listGrdChm.get(12));
						sqllist.add(sqlInertGrdChm);
					}else{
						String sqlInertGrdChm = CoreXmlSqlParsersFactory.getSql("CoreSpecificationsGradeChem.UPDATE");
						sqlInertGrdChm = StringTool.ReplaceAll(sqlInertGrdChm, "#ASSORTMENT_CODE#", listGrdChm.get(0));
						sqlInertGrdChm = StringTool.ReplaceAll(sqlInertGrdChm, "#ASSORTMENT_NAME#", listGrdChm.get(1));
						sqlInertGrdChm = StringTool.ReplaceAll(sqlInertGrdChm, "#COEFFICIENT#", listGrdChm.get(2));
						sqlInertGrdChm = StringTool.ReplaceAll(sqlInertGrdChm, "#STDMIN_SIGN#", listGrdChm.get(3));
						sqlInertGrdChm = StringTool.ReplaceAll(sqlInertGrdChm, "#STDMIN#", listGrdChm.get(4));
						sqlInertGrdChm = StringTool.ReplaceAll(sqlInertGrdChm, "#STDMAX_SIGN#", listGrdChm.get(5));
						sqlInertGrdChm = StringTool.ReplaceAll(sqlInertGrdChm, "#STDMAX#", listGrdChm.get(6));
						sqlInertGrdChm = StringTool.ReplaceAll(sqlInertGrdChm, "#UPDATE_NAME#", listGrdChm.get(7));
						sqlInertGrdChm = StringTool.ReplaceAll(sqlInertGrdChm, "#MEMO#", listGrdChm.get(8));
						sqlInertGrdChm = StringTool.ReplaceAll(sqlInertGrdChm, "#GRADENAME#", listGrdChm.get(9));
						sqlInertGrdChm = StringTool.ReplaceAll(sqlInertGrdChm, "#CHEM_NAME#", listGrdChm.get(10));
						sqlInertGrdChm = StringTool.ReplaceAll(sqlInertGrdChm, "#GRADECODE#", sgradeCode);
						sqlInertGrdChm = StringTool.ReplaceAll(sqlInertGrdChm, "#CHEM_CODE#", listGrdChm.get(12));
						
						sqllist.add(sqlInertGrdChm);
					}
				}
			}
		}
		
		CoreReturnObject cro = this.getDao(CoreConstantData.sysbeanstr).ExcuteTransction(sqllist);
		cro.setResult(gradeCode);
		return cro;
	}
	
//	public CoreReturnObject doUpdate(ArrayList<String> listGrd, ArrayList<String> listGrdChm) {
//        return this.setDataBySqlID("CoreSpecificationsGrade.UPDATE", parms.toArray());
//    }

	public CoreReturnObject doModify(ArrayList<ArrayList> parms) {

		CoreReturnObject cro = new CoreReturnObject();

		String sql = CoreXmlSqlParsersFactory
				.getSql("CoreSpecificationsGrade.MODIFY");

		ArrayList<Object> sqllist = new ArrayList<Object>();
		ArrayList<Object[]> parmlist = new ArrayList<Object[]>();

		for (int i = 0; i < parms.size(); i++) {
			sqllist.add(sql);
			parmlist.add(parms.get(i).toArray());
		}

		try {
			cro = this.getDao("TgDao").ExcuteTransction(sqllist, parmlist);
		} catch (Exception e) {
			cro = cro.SetErrInfo(e.getMessage());
		}

		return cro;

	}

	/**
	 * 产品规格基础信息删除或恢复
	 * 
	 * @param plineCodes
	 *            产线代码集合
	 * @param operator
	 *            操作人
	 * @param isDelete
	 *            true删除 false恢复
	 * @return CoreReturnObject 必须返回CoreReturnObject
	 */
	public CoreReturnObject deleteLineInfo(ArrayList<Object> parms,
	        Boolean isDelete) {
			if (isDelete) {
                return this.setDataBySqlID("CoreSpecificationsGrade.DELETE", parms.toArray());
			} else {
		         return this.setDataBySqlID("CoreSpecificationsGrade.RESUME", parms.toArray());
			}
	}
	
	/**
	 * 是否存在钢种名称
	 * @param gradeName
	 * @return
	 */
	public CoreReturnObject isExistGradeName(String gradeName){
		String sql = CoreXmlSqlParsersFactory.getSql("CoreSpecificationsGrade.isExistGradeName");
		sql = StringTool.ReplaceAll(sql, "#GRADENAME#", gradeName);
		
		return this.getData(sql);
	}
	
	/**
	 * 获取钢种基础数据
	 * @return
	 */
	public CoreReturnObject GetComBaseGrade(){
		String sql = CoreXmlSqlParsersFactory.getSql("CoreSpecificationsGrade.GetComBaseGrade");
		
		return this.getData(sql);
	}
}
