package com.steering.pss.qcm;

import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.text.DecimalFormat;
import java.util.ArrayList;
import java.util.HashMap;

import CoreFS.Util.StringUtils;
import net.sf.json.JSONObject;

import com.steering.comm.db.DBCommon;
import com.steering.comm.tools.EntityTool;
import com.steering.comm.tools.SqlBuilder;
import com.steering.comm.tools.StringTool;
import com.steering.pss.qcm.model.BaseAdditionalObject;
import com.steering.pss.qcm.model.BaseStdAlphaEntity;
import com.steering.pss.qcm.model.BaseStdObject;
import com.steering.pss.qcm.model.ComBaseStdAlphaEntity;
import com.steering.pss.qcm.model.GenericEntity;
import com.steering.pss.qcm.model.MscEntity;
import com.steering.pss.qcm.model.PscEntity;
import com.steering.pss.qcm.model.PscObject;
import com.steering.pss.qcm.model.PscRMscEntity;
import com.steering.pss.qcm.model.StdCusterObject;

import CoreFS.SA06.CoreReturnObject;
import CoreFS.SA08.CoreXmlSqlParsersFactory;
import CoreFS.SC01.CoreConstantData;

/**
 * 产品规范管理
 * 
 * @author Administrator PDY 2015/01/23
 */
public class CoreProductManage extends DBCommon {

	/**
	 * 标准基础(如果customInfo等于1，就加数据权限)
	 * 
	 * @param std
	 *            标准名称
	 * @param valid
	 *            是否包含无效
	 * @return 查询结果集
	 */
	public CoreReturnObject queryBaseStd(String std, String valid,
			String stdCode, String customInfo, String[] validDataPurviewIds) {
		String sql = CoreXmlSqlParsersFactory
				.getSql("CoreProductManage.queryStd");
//		if (customInfo.equals("1")) {
//			sql += CoreConverter.toSqlConditionString("S.UNIT_CODE",
//					validDataPurviewIds);
//		}
		sql += "  ORDER BY S.STD_CODE	";
		Object[] params = new Object[] { std, valid, stdCode, stdCode, stdCode };
		return this.getData(sql, params);
	}

	/**
	 * 产品规范码是否关联冶金规范码
	 * 
	 * @param psc
	 *            产品规范码
	 * @return 平台统一对象
	 */
	public CoreReturnObject isPscLinkMsc(String psc) {
		Object[] params = new Object[] { psc };
		return this.getDataBySqlID("CoreProductManage.IsPSCLinkMSC", params);
	}

	/**
	 * 产品规范
	 * 
	 * @param stdCode
	 *            标准代码
	 * @param valid
	 *            是否包含无效
	 * @return 查询结果集
	 */
	public CoreReturnObject queryPsc(String stdCode, String valid) {
		Object[] params = new Object[] { stdCode, valid };
		return this.getDataBySqlID("CoreProductManage.queryPsc", params);
	}

	/**
	 * 标准附加项
	 * 
	 * @param stdCode
	 *            标准代码
	 * @param valid
	 *            是否包含无效
	 * @return 查询结果集
	 */
	public CoreReturnObject queryAdd(String stdCode, String valid) {
		String sql = CoreXmlSqlParsersFactory
				.getSql("CoreProductManage.queryAdd");
		sql = StringTool.ReplaceAll(sql, "#STD_CODE#", stdCode);
		sql = StringTool.ReplaceAll(sql, "#VALIDFLAG#", valid);
		return this.getData(sql);
	}

	/**
	 * 查询ALpha
	 * 
	 * @param stdCode
	 *            标准代码
	 * @param valid
	 *            是否有效
	 * @return 平台统一返回对象
	 */
	public CoreReturnObject queryAlpha(String stdCode, String psc, String useCode,
			String valid, String isPopup) {
		Object[] params = new Object[] { stdCode, valid, isPopup, psc, psc, psc, useCode, useCode };
		return this.getDataBySqlID("CoreProductManage.queryAlpha", params);
	}

	/**
	 * 产品规范 保存
	 * 
	 * @param stdData
	 *            标准基础
	 * @param pscData
	 *            产品规范
	 * @param addData
	 *            附加要求
	 * @param alphaData
	 *            alpha
	 * @param custData
	 *            客户
	 * @return 平台统一返回对象
	 */
	public CoreReturnObject saveProductManage(ArrayList<Object> stdData,
			ArrayList<Object> pscData, ArrayList<Object> addData,
			ArrayList<Object> alphaData, ArrayList<Object> custData,
			ArrayList<String> specList) {
		ArrayList<String> sqlIdList = new ArrayList<String>();
		ArrayList<Object[]> params = new ArrayList<Object[]>();
		HashMap<String, String> stdCodes = new HashMap<String, String>();
		try {
			String sql = CoreXmlSqlParsersFactory
					.getSql("CoreProductManage.queryStdMax");
			ResultSet rs = this.getDao("TgDao").ExceuteQueryForResultSet(sql);
			int stdCodePart = 0;
			if (rs.next()) {
				stdCodePart = Integer
						.parseInt(rs.getString(1).substring(3, 10));
			}
			rs.close();
			for (int i = 0; i < stdData.size(); i++) {
				JSONObject json = JSONObject.fromObject(stdData.get(i)
						.toString());
				BaseStdObject com = (BaseStdObject) JSONObject.toBean(json,
						BaseStdObject.class);
				Object[] param = null;
				if (com.getValidflag().equals("")) {
					DecimalFormat df = new DecimalFormat("0000000");
					String stdCode = "STD" + df.format(stdCodePart + 1);
					param = new Object[] { stdCode, com.getStdName(),
							com.getStdStyle(), com.getStdNote(),
							com.getStdStyleCode(), com.getStdStyleName(),
							com.getStdVersion(), com.getProcessCode(),
							com.getProcessDesc(), com.getDepartmentCode(),
							com.getDepartmentDesc(), com.getUnitCode(),
							com.getUnitDesc(), com.getCreateName(),
							com.getMemo() };
					stdCodePart += 1;
					stdCodes.put(com.getStdCode(), stdCode);
					com.setStdCode(stdCode);
					String sqlInsertStdAlpha = getInsertStdAlphaSql(com);// 新增标准时，默认新增用途描述为/的用途码。
					sqlIdList.add(CoreXmlSqlParsersFactory
							.getSql("CoreProductManage.addStd"));
					params.add(param);
					sqlIdList.add(sqlInsertStdAlpha);
					params.add(null);
				} else {
					param = new Object[] { com.getStdCode(), com.getStdName(),
							com.getStdStyle(), com.getStdNote(),
							com.getStdStyleCode(), com.getStdStyleName(),
							com.getStdVersion(), com.getProcessCode(),
							com.getProcessDesc(), com.getDepartmentCode(),
							com.getDepartmentDesc(), com.getUnitCode(),
							com.getUnitDesc(), com.getUpdateName(),
							com.getMemo(), com.getStdCode() };
					sqlIdList.add(CoreXmlSqlParsersFactory
							.getSql("CoreProductManage.updateStd"));
					params.add(param);
				}
			}
			int maxPsc = 0;
			String sqlGetMaxPsc = CoreXmlSqlParsersFactory
					.getSql("CoreProductManage.getMaxPsc");
			Connection con = this.getDao(CoreConstantData.sysbeanstr)
					.getConnection();
			ArrayList<GenericEntity> listMaxPsc = EntityTool.getEntitysBySql(
					sqlGetMaxPsc, con, GenericEntity.class);
			maxPsc = Integer.parseInt(listMaxPsc.get(0).getMaxValue());
			for (int i = 0; i < pscData.size(); i++) {
				JSONObject json = JSONObject.fromObject(pscData.get(i)
						.toString());
				PscEntity com = (PscEntity) JSONObject.toBean(json,
						PscEntity.class);
				Object[] param = null;
				if (com.getValidflag().equals("")) {
					if (stdCodes.containsKey(com.getStdCode())) {
						com.setStdCode(stdCodes.get(com.getStdCode()));
					}
					maxPsc++;
					String psc = "PSC" + String.format("%07d", maxPsc);
					com.setPsc(psc);
					String sqlInserPsc = CoreXmlSqlParsersFactory
							.getSql("CoreProductManage.addPsc");
					sqlInserPsc = EntityTool.fillSqlByEntity(sqlInserPsc, com);
					sqlIdList.add(sqlInserPsc);
					String useCode = com.getStdCode() + "000";
					String useDesc = "/";
					MscEntity mscEntity = new MscEntity();
					mscEntity.setUseCode(useCode);
					mscEntity.setUseDesc(useDesc);
					String sqlInsertMsc = getInsertMscSql(com, mscEntity, "PSC");
					// String sqlInsertPscRMsc = getInsertPscRMscSql(com);
					sqlIdList.add(sqlInsertMsc);
					// sqlIdList.add(sqlInsertPscRMsc);
					params.add(new Object[] {});
					params.add(new Object[] {});
					// params.add(new Object[]{});
				} else {
					param = new Object[] { com.getPscDesc(),
							com.getProduccode(), com.getProducname(),
							com.getStdCode(), com.getStdName(),
							com.getSteelcode(), com.getSteelname(),
							com.getStdStyle(), com.getStdStyleDesc(),
							com.getNewProcCode(), com.getNewProcDesc(),
							com.getDepartmentCode(), com.getDepartmentDesc(),
							com.getUnitCode(), com.getAutojudge(),
							com.getUnitDesc(), com.getCreateName(),
							com.getMemo(), com.getProducHead(),
							com.getProducType(), com.getProducStyle(),
							com.getProducDesc(), com.getCompressionRatio(),
							com.getPsc() };
					sqlIdList.add(CoreXmlSqlParsersFactory
							.getSql("CoreProductManage.updatePsc"));
					params.add(param);
				}
			}
			for (int i = 0; i < addData.size(); i++) {
				JSONObject json = JSONObject.fromObject(addData.get(i)
						.toString());
				BaseAdditionalObject com = (BaseAdditionalObject) JSONObject
						.toBean(json, BaseAdditionalObject.class);
				Object[] param = null;
				if (com.getValidflag().equals("")) {
					if (stdCodes.containsKey(com.getStdCode())) {
						com.setStdCode(stdCodes.get(com.getStdCode()));
					}
					param = new Object[] { com.getStdCode(), com.getStdName(),
							com.getAskItme(), com.getAskItmeDesc(),
							com.getAskSubItme(), com.getAskSubItmeDesc(),
							com.getAskSubItmeVal(), com.getAskSubItmeValDesc(),
							com.getPic(), com.getCreateName(), com.getMemo() };
					sqlIdList.add(CoreXmlSqlParsersFactory
							.getSql("CoreProductManage.addAdd"));
					params.add(param);
				} else {
					param = new Object[] { com.getStdCode(), com.getStdName(),
							com.getAskItme(), com.getAskItmeDesc(),
							com.getAskSubItme(), com.getAskSubItmeDesc(),
							com.getAskSubItmeVal(), com.getAskSubItmeValDesc(),
							com.getPic(), com.getCreateName(), com.getMemo(),
							com.getPk() };
					sqlIdList.add(CoreXmlSqlParsersFactory
							.getSql("CoreProductManage.updateAdd"));
					params.add(param);
				}
			}

			for (int i = 0; i < alphaData.size(); i++) {
				JSONObject json = JSONObject.fromObject(alphaData.get(i)
						.toString());
				ComBaseStdAlphaEntity com = (ComBaseStdAlphaEntity) JSONObject
						.toBean(json, ComBaseStdAlphaEntity.class);
				Object[] param = null;
				if (com.getValidflag().equals("")) {
					if (stdCodes.containsKey(com.getStdCode())) {
						com.setStdCode(stdCodes.get(com.getStdCode()));
					}
					String sql1 = CoreXmlSqlParsersFactory
							.getSql("CoreProductManage.queryStdMax1");
					Object[] obj = new Object[] { com.getStdCode(),com.getStdCode() };
					ResultSet rs1 = this.getDao("TgDao").ExceuteQueryForResultSet(sql1,obj);
					String stdSeq="";
					if (rs1.next()) {
						stdSeq = rs1.getString(1);
					}
					rs.close();
					int stdSeqN=0;
					stdSeqN=Integer.parseInt(stdSeq)+1;
					DecimalFormat df = new DecimalFormat("000");
					String alphaCode = com.getStdCode() + df.format(stdSeqN);
					if("999".equals(stdSeq)){
						sql1 = CoreXmlSqlParsersFactory
								.getSql("CoreProductManage.queryStdMax2");
						rs1 = this.getDao("TgDao").ExceuteQueryForResultSet(sql1,obj);
						stdSeq="";
						if (rs1.next()) {
							stdSeq = rs1.getString(1);
						}
						rs1.close();
						stdSeqN=Integer.parseInt(stdSeq)+1;
						df = new DecimalFormat("0000");
						alphaCode = com.getStdCode() + df.format(stdSeqN);
					}
					
					/*param = new Object[] { com.getStdCode(), com.getStdCode(),
							com.getStdCode(), com.getAlphaName(),
							com.getStdCode(), com.getStdNote(),
							com.getStdStyle(), com.getStdStyleName(),
							com.getStdVersion(), com.getDepartmentCode(),
							com.getDepartmentDesc(), com.getUnitCode(),
							com.getUnitDesc(), com.getCreateName(),
							com.getMemo() };
					sqlIdList.add(CoreXmlSqlParsersFactory
							.getSql("CoreProductManage.addAlpha"));*/
					param = new Object[] { alphaCode, com.getAlphaName(),
							com.getStdCode(), com.getStdNote(),
							com.getStdStyle(), com.getStdStyleName(),
							com.getStdVersion(), com.getDepartmentCode(),
							com.getDepartmentDesc(), com.getUnitCode(),
							com.getUnitDesc(), com.getCreateName(),
							com.getMemo() };
					sqlIdList.add(CoreXmlSqlParsersFactory
							.getSql("CoreProductManage.addAlphaNew"));
					params.add(param);
				} else {
					param = new Object[] { com.getAlphaName(),
							com.getStdCode(), com.getStdNote(),
							com.getStdStyle(), com.getStdStyleName(),
							com.getStdVersion(), com.getDepartmentCode(),
							com.getDepartmentDesc(), com.getUnitCode(),
							com.getUnitDesc(), com.getUpdateName(),
							com.getMemo(), com.getAlphaCode() };
					sqlIdList.add(CoreXmlSqlParsersFactory
							.getSql("CoreProductManage.updateAlpha"));
					params.add(param);
					
					param = new Object[]{com.getAlphaName(), com.getAlphaCode()};
					sqlIdList.add(CoreXmlSqlParsersFactory
							.getSql("CoreProductManage.updateMscAlphaDesc"));
					params.add(param);
				}
			}

			for (int i = 0; i < custData.size(); i++) {
				JSONObject json = JSONObject.fromObject(custData.get(i)
						.toString());
				StdCusterObject com = (StdCusterObject) JSONObject.toBean(json,
						StdCusterObject.class);
				Object[] param = null;
				if (com.getValidflag().equals("")
						&& com.getCustCode().equals(""))
					continue;
				if (com.getValidflag().equals("")
						&& !com.getCustName().equals("/")) {
					param = new Object[] { com.getStdCode(), com.getStdName(),
							com.getCustCode(), com.getCustName(),
							com.getCreateName(), com.getMemo() };
					sqlIdList.add(CoreXmlSqlParsersFactory
							.getSql("CoreStandard.addCust"));
				} else if (!com.getCustCode().equals("")
						&& !com.getCustName().equals("/")) {
					param = new Object[] { com.getStdCode(), com.getStdName(),
							com.getCustCode(), com.getCustName(),
							com.getCreateName(), com.getMemo(),
							com.getStdCode(), com.getOldCustCode() };
					sqlIdList.add(CoreXmlSqlParsersFactory
							.getSql("CoreStandard.updateCust"));
				} else if (!com.getCustName().equals("/")) {
					param = new Object[] { com.getStdCode(),
							com.getOldCustCode() };
					sqlIdList.add(CoreXmlSqlParsersFactory
							.getSql("CoreStandard.UpdatedeleteCust"));
				}
				Object[] parm = null;
				Object[] parm1 = null;
				if (com.getCustName().equals("/")) {
					parm = new Object[] { com.getStdCode() };
					sqlIdList.add(CoreXmlSqlParsersFactory
							.getSql("CoreStandard.DeleteCust1"));
					parm1 = new Object[] { com.getStdCode(), com.getStdName(),
							com.getCustCode(), com.getCustName(),
							com.getCreateName(), com.getMemo() };
					sqlIdList.add(CoreXmlSqlParsersFactory
							.getSql("CoreStandard.addCust"));
				}
				if (param != null) {
					params.add(param);
				}
				if (parm != null) {
					params.add(parm);
				}
				if (parm1 != null) {
					params.add(parm1);
				}
			}

			// 规格
			BaseStdRSpec baseStdRSpec = new BaseStdRSpec();
			ArrayList<String> specSqlList = baseStdRSpec.getNoQuerySqlList(
					specList, "Save");
			for (int i = 0; i < specSqlList.size(); i++) {
				sqlIdList.add(specSqlList.get(i));
				params.add(new Object[] {});
			}
			
		} catch (SQLException e) {
		}

		return this.setData(sqlIdList, params);
	}

	private String getInsertStdAlphaSql(BaseStdObject stdObj) {
		BaseStdAlphaEntity stdAlphaEntity = new BaseStdAlphaEntity();
		stdAlphaEntity.setAlphaCode(stdObj.getStdCode() + "000");
		stdAlphaEntity.setAlphaName("/");
		stdAlphaEntity.setCreateName(stdObj.getCreateName());
		stdAlphaEntity.setDepartmentCode(stdObj.getDepartmentCode());
		stdAlphaEntity.setDepartmentDesc(stdObj.getDepartmentDesc());
		stdAlphaEntity.setStdCode(stdObj.getStdCode());
		stdAlphaEntity.setUnitCode(stdObj.getUnitCode());
		stdAlphaEntity.setUnitDesc(stdObj.getUnitDesc());
		String sql = CoreXmlSqlParsersFactory
				.getSql("CoreProductManage.setBaseStdAlpha");
		sql = EntityTool.fillSqlByEntity(sql, stdAlphaEntity);

		return sql;
	}

	public String getInsertMscSql(PscEntity pscObj, MscEntity mscEntity, String flag) {
		mscEntity.setAllowNk("1");
		mscEntity.setCreateName(pscObj.getCreateName());
		mscEntity.setDepartmentCode(pscObj.getDepartmentCode());
		mscEntity.setDepartmentDesc(pscObj.getDepartmentDesc());
		mscEntity.setMsc(pscObj.getPsc().replaceAll("PSC", "MSC") + "000");
		mscEntity.setMscDesc(pscObj.getPscDesc());
		mscEntity.setMscStyle(pscObj.getProducType());
		mscEntity.setNewProcCode(pscObj.getNewProcCode());
		mscEntity.setNewProcDesc(pscObj.getNewProcDesc());
		mscEntity.setProduccode(pscObj.getProduccode());
		mscEntity.setProducHead(pscObj.getProducHead());
		mscEntity.setProducname(pscObj.getProducname());
		mscEntity.setStdCode(pscObj.getStdCode());
		mscEntity.setStdName(pscObj.getStdName());
		mscEntity.setStdStyle(pscObj.getStdStyle());
		mscEntity.setStdStyleDesc(pscObj.getStdStyleDesc());
		mscEntity.setSteelcode(pscObj.getSteelcode());
		mscEntity.setSteelname(pscObj.getSteelname());
		mscEntity.setPsc(pscObj.getPsc());
		mscEntity.setPscDesc(pscObj.getPscDesc());
		mscEntity.setUnitCode(pscObj.getUnitCode());
		mscEntity.setUnitCodeProcess(pscObj.getUnitCode());
		mscEntity.setUnitDesc(pscObj.getUnitDesc());
		mscEntity.setUnitDescProcess(pscObj.getUnitDesc());
		mscEntity.setUserCount("1");
		// String sql =
		// CoreXmlSqlParsersFactory.getSql("CoreProductManage.setMsc");
		String sql = CoreXmlSqlParsersFactory.getSql("MscManage.insertMsc");
		sql = EntityTool.fillSqlByEntity(sql, mscEntity);

		return sql;
	}

	private String getInsertPscRMscSql(PscObject pscObj) {
		PscRMscEntity pscRMscEntity = new PscRMscEntity();
		pscRMscEntity.setCreateName(pscObj.getCreateName());
		pscRMscEntity.setMsc(pscObj.getPsc().replaceAll("PSC", "MSC") + "000");
		pscRMscEntity.setMscDesc(pscObj.getPscDesc());
		pscRMscEntity.setPsc(pscObj.getPsc());
		pscRMscEntity.setPscDesc(pscObj.getPscDesc());
		pscRMscEntity.setUseCode(pscObj.getStdCode() + "000");
		pscRMscEntity.setUseDesc("/");
		String sql = CoreXmlSqlParsersFactory
				.getSql("CoreProductManage.setPscRMsc");
		sql = EntityTool.fillSqlByEntity(sql, pscRMscEntity);

		return sql;
	}

	/**
	 * 产品规范 作废或者恢复
	 * 
	 * @param stdData
	 *            标准基础
	 * @param pscData
	 *            产品规范
	 * @param addData
	 *            附加要求
	 * @param flag
	 *            作废或者恢复标识
	 * @return 执行条数
	 */
	public CoreReturnObject deleteOrResume(ArrayList<Object> stdData,
			ArrayList<Object> pscData, ArrayList<Object> addData,
			ArrayList<Object> alphaData, ArrayList<Object> custData,
			ArrayList<String> specList, Boolean flag) {
		ArrayList<String> sqlIdList = new ArrayList<String>();
		ArrayList<Object[]> params = new ArrayList<Object[]>();
		for (int i = 0; i < stdData.size(); i++) {
			JSONObject json = JSONObject.fromObject(stdData.get(i).toString());
			BaseStdObject com = (BaseStdObject) JSONObject.toBean(json,
					BaseStdObject.class);
			Object[] param = null;
			if (flag) {
				param = new Object[] { com.getDeleteName(), com.getStdCode() };
				sqlIdList.add(CoreXmlSqlParsersFactory
						.getSql("CoreProductManage.deleteStd"));
			} else {
				param = new Object[] { "", com.getStdCode() };
				sqlIdList.add(CoreXmlSqlParsersFactory
						.getSql("CoreProductManage.resumeStd"));
			}
			params.add(param);
		}
		for (int i = 0; i < pscData.size(); i++) {
			JSONObject json = JSONObject.fromObject(pscData.get(i).toString());
			PscObject com = (PscObject) JSONObject
					.toBean(json, PscObject.class);
			Object[] param = null;
			if (flag) {
				param = new Object[] { com.getDeleteName(), com.getPsc() };
				sqlIdList.add(CoreXmlSqlParsersFactory
						.getSql("CoreProductManage.deletePsc"));
			} else {
				param = new Object[] { "", com.getPsc() };
				sqlIdList.add(CoreXmlSqlParsersFactory
						.getSql("CoreProductManage.resumePsc"));
			}
			params.add(param);
		}
		for (int i = 0; i < addData.size(); i++) {
			JSONObject json = JSONObject.fromObject(addData.get(i).toString());
			BaseAdditionalObject com = (BaseAdditionalObject) JSONObject
					.toBean(json, BaseAdditionalObject.class);
			Object[] param = null;
			if (flag) {
				param = new Object[] { com.getDeleteName(), com.getStdCode(),
						com.getAskSubItmeVal() };
				sqlIdList.add(CoreXmlSqlParsersFactory
						.getSql("CoreProductManage.deleteAdd"));
			} else {
				param = new Object[] { "", com.getStdCode(),
						com.getAskSubItmeVal() };
				sqlIdList.add(CoreXmlSqlParsersFactory
						.getSql("CoreProductManage.resumeAdd"));
			}
			params.add(param);
		}

		for (int i = 0; i < alphaData.size(); i++) {
			JSONObject json = JSONObject
					.fromObject(alphaData.get(i).toString());
			ComBaseStdAlphaEntity com = (ComBaseStdAlphaEntity) JSONObject
					.toBean(json, ComBaseStdAlphaEntity.class);
			Object[] param = null;
			if (flag) {
				param = new Object[] { com.getDeleteName(), com.getAlphaCode() };
				sqlIdList.add(CoreXmlSqlParsersFactory
						.getSql("CoreProductManage.deleteAlpha"));
			} else {
				param = new Object[] { com.getAlphaCode() };
				sqlIdList.add(CoreXmlSqlParsersFactory
						.getSql("CoreProductManage.resumeAlpha"));
			}
			params.add(param);
		}

		for (int i = 0; i < custData.size(); i++) {
			JSONObject json = JSONObject.fromObject(custData.get(i).toString());
			StdCusterObject com = (StdCusterObject) JSONObject.toBean(json,
					StdCusterObject.class);
			Object[] param = null;
			if (flag) {
				param = new Object[] { com.getDeleteName(), com.getStdCode(),
						com.getCustCode() };
				sqlIdList.add(CoreXmlSqlParsersFactory
						.getSql("CoreStandard.deleteCust"));
			} else {
				param = new Object[] { "", com.getStdCode(), com.getCustCode() };
				sqlIdList.add(CoreXmlSqlParsersFactory
						.getSql("CoreStandard.resumeCust"));
			}
			params.add(param);
		}

		// 规格
		BaseStdRSpec baseStdRSpec = new BaseStdRSpec();
		ArrayList<String> specSqlList = baseStdRSpec.getNoQuerySqlList(
				specList, flag ? "Delete" : "Resume");
		for (int i = 0; i < specSqlList.size(); i++) {
			sqlIdList.add(specSqlList.get(i));
			params.add(new Object[] {});
		}

		return this.setData(sqlIdList, params);
	}

	/**
	 * 附加要求项检验是否重复
	 * 
	 * @param stdCode
	 *            附加要求项代码
	 * @param baseCode
	 *            标准代码
	 * @return 重复则返回行，不重复返回NULL
	 */
	public CoreReturnObject checkAdditional(String stdCode, String baseCode) {
		Object[] params = new Object[] { stdCode, baseCode };
		return this.getDataBySqlID("CoreProductManage.checkAdd", params);
	}

	/**
	 * 产品规范描述 新增时检验是否重复
	 * 
	 * @param pscDesc
	 *            产品规范描述
	 * @return 重复则返回1，不重复返回NULL
	 */
	public CoreReturnObject checkPsc(String produceCode, String stdStyle,
			String steelCode, String stdCode) {
		String sql = CoreXmlSqlParsersFactory
				.getSql("CoreProductManage.checkPsc");
		sql = StringTool.ReplaceAll(sql, "#PRODUCCODE#", produceCode);
		sql = StringTool.ReplaceAll(sql, "#STD_STYLE#", stdStyle);
		sql = StringTool.ReplaceAll(sql, "#STEELCODE#", steelCode);
		sql = StringTool.ReplaceAll(sql, "#STD_CODE#", stdCode);

		return this.getData(sql);
	}

	/**
	 * 产品规范描述 新增时检验是否重复
	 * 
	 * @param alphaName
	 *            alpha
	 * @return 重复则返回1，不重复返回NULL
	 */
	public CoreReturnObject checkAlpha(String alphaName, String stdCode) {
		Object[] params = new Object[] { alphaName, stdCode };
		return this.getDataBySqlID("CoreProductManage.checkAlpha", params);
	}

	/**
	 * 产品规范描述 修改时检验是否重复
	 * 
	 * @param pscDesc
	 *            产品规范描述
	 * @param psc
	 *            产品规范码
	 * @return 重复则返回1，不重复返回NULL
	 */
	public CoreReturnObject checkPscUpdate(String produceCode, String stdStyle,
			String steelCode, String stdCode, String psc) {
		String sql = CoreXmlSqlParsersFactory
				.getSql("CoreProductManage.checkPscUpdate");
		sql = StringTool.ReplaceAll(sql, "#PRODUCCODE#", produceCode);
		sql = StringTool.ReplaceAll(sql, "#STD_STYLE#", stdStyle);
		sql = StringTool.ReplaceAll(sql, "#STEELCODE#", steelCode);
		sql = StringTool.ReplaceAll(sql, "#STD_CODE#", stdCode);
		sql = StringTool.ReplaceAll(sql, "#PSC#", psc);

		return this.getData(sql);
	}

	/**
	 * 产品规范描述 修改时检验是否重复
	 * 
	 * @param pscDesc
	 *            产品规范描述
	 * @param psc
	 *            产品规范码
	 * @return 重复则返回1，不重复返回NULL
	 */
	public CoreReturnObject checkAlphaUpdate(String alphaCode, String alphaName, String stdCode) {
		Object[] params = new Object[] { alphaCode, alphaName, stdCode};
		return this
				.getDataBySqlID("CoreProductManage.checkAlphaUpdate", params);
	}

	/**
	 * 标准名称 新增时检验是否重复
	 * 
	 * @param stdName
	 *            标准名称
	 * @return 重复返回1，不重复返回NULL
	 */
	public CoreReturnObject checkStd(String stdName, String stdType) {
		String sql = CoreXmlSqlParsersFactory
				.getSql("CoreProductManage.checkStd");
		sql = StringTool.ReplaceAll(sql, "#STD_NAME#", stdName);
		sql = StringTool.ReplaceAll(sql, "#STDSTYLE#", stdType);
		return this.getData(sql);
	}

	/**
	 * 标准名称 修改时检验是否重复
	 * 
	 * @param stdName
	 *            标准名称
	 * @param stdCode
	 *            标准代码
	 * @return 重复返回1，不重复返回NULL
	 */
	public CoreReturnObject checkStdUpdate(String stdName, String stdCode,
			String stdType) {
		String sql = CoreXmlSqlParsersFactory
				.getSql("CoreProductManage.checkStdUpdate");
		sql = StringTool.ReplaceAll(sql, "#STD_CODE#", stdCode);
		sql = StringTool.ReplaceAll(sql, "#STD_NAME#", stdName);
		sql = StringTool.ReplaceAll(sql, "#STDSTYLE#", stdType);
		return this.getData(sql);
	}

	/**
	 * 作废标准记录 检验引用项是否已经作废
	 * 
	 * @param stdCode
	 *            标准代码
	 * @return 已经全部作废返回值等于0，否则返回值等于1
	 */
	public CoreReturnObject checkStdDelete(String stdCode) {
		Object[] params = new Object[] { stdCode, stdCode, stdCode };
		return this.getDataBySqlID("CoreProductManage.checkStdDelete", params);
	}

	/**
	 * 作废标准记录 检验引用项是否已经作废
	 * 
	 * @param stdCode
	 *            标准代码
	 * @return 已经全部作废返回值等于0，否则返回值等于1
	 */
	public CoreReturnObject checkAlphaDelete(String alphaCode) {
		Object[] params = new Object[] { alphaCode };
		return this
				.getDataBySqlID("CoreProductManage.checkAlphaDelete", params);
	}

	/**
	 * 恢复附加项记录或者产品规范码 检验标准代码是否已经恢复
	 * 
	 * @param stdCode
	 *            标准代码
	 * @return 已经恢复则返回NULL，否则返回1
	 */
	public CoreReturnObject checkFromResume(String stdCode) {
		Object[] params = new Object[] { stdCode };
		return this.getDataBySqlID("CoreProductManage.checkFromResume", params);
	}

	/**
	 * 恢复附加项记录或者产品规范码 检验标准代码是否已经恢复
	 * 
	 * @param stdCode
	 *            标准代码
	 * @return 已经恢复则返回NULL，否则返回1
	 */
	public CoreReturnObject checkFromResumeAlpha(String stdCode) {
		Object[] params = new Object[] { stdCode };
		return this.getDataBySqlID("CoreProductManage.checkFromResume", params);
	}

	/**
	 * 附加要求
	 * 
	 * @param itemCode
	 *            代码
	 * @return 数据集
	 */
	public CoreReturnObject getSubItem(String itemCode, String stdFlag) {
		Object[] params = new Object[] { itemCode, stdFlag, stdFlag, stdFlag };
		return this.getDataBySqlID("CoreProductManage.getSubItem", params);
	}
	
	/**
	 * 附加要求
	 * 
	 * @param itemCode
	 *            代码
	 * @return 数据集
	 */
	public CoreReturnObject getSubItem2(String itemCode, String stdFlag) {
		Object[] params = new Object[] { itemCode, stdFlag, stdFlag, stdFlag };
		return this.getDataBySqlID("CoreProductManage.getSubItem2", params);
	}

	public CoreReturnObject getStdDesc(String itemCode) {
		Object[] params = new Object[] { itemCode };
		return this.getDataBySqlID("CoreProductManage.getStdDesc", params);
	}

	public CoreReturnObject getProductHead() {

		return this.getDataBySqlID("CoreProductManage.GetProductHead");
	}

	/**
	 * 作废或者恢复产品规范向下所有数据
	 * 
	 * @return
	 */
	public CoreReturnObject deleteOrResumeStd(String deleteFlag,
			String userName, String stdCode) {
		String sql = CoreXmlSqlParsersFactory
				.getSql("CoreProductManage.deleteOrResumeStd");
		sql = StringTool.ReplaceAll(sql, "#DELETE_FLAG#", deleteFlag);
		sql = StringTool.ReplaceAll(sql, "#DELETE_NAME#", userName);
		sql = StringTool.ReplaceAll(sql, "#STD_CODE#", stdCode);
		return this.setData(sql);
	}
	
	/**
	 * 通过主键查询标准
	 * @param stdCode
	 * @return
	 */
	public CoreReturnObject getBaseStdByPk(String stdCode){
		String sql = CoreXmlSqlParsersFactory.getSql("CoreProductManage.getBaseStdByPk");
		sql = StringTool.ReplaceAll(sql, "#STD_CODE#", stdCode);
		return this.getData(sql);
	}
	
	/**
	 * 通过主键查询产品
	 * @return
	 */
	public CoreReturnObject getBasePscByPk(String pscCode){
		String sql = CoreXmlSqlParsersFactory.getSql("CoreProductManage.getBasePscByPk");
		sql = StringTool.ReplaceAll(sql, "#PSC#", pscCode);
		return this.getData(sql);
	}
	
	/**
	 * 通过主键查询用途码
	 * @return
	 */
	public CoreReturnObject getBaseStdRAlphaByPk(String alphaCode){
		String sql = CoreXmlSqlParsersFactory.getSql("CoreProductManage.getBaseStdRAlphaByPk");
		sql = StringTool.ReplaceAll(sql, "#ALPHA_CODE#", alphaCode);
		return this.getData(sql);
	}
	
	
	/**
	 * 通过标准类型查询标准基础信息
	 */
	public CoreReturnObject GetComBaseStdByStyle(String[] styles,String stdName ){
		String sql = CoreXmlSqlParsersFactory.getSql("CoreProductManage.getComBaseStdByStyle");
		String sqlStyles = "'1'";
		for(String style : styles){
			sqlStyles += ", '" + style + "'";
		}
		if(!StringUtils.isEmpty(stdName)){
			sql+="  AND T.STD_NAME LIKE'"+stdName+"%' ";
		}
		sql = sql.replaceAll("#SQL_STYLES#", sqlStyles);
		return this.getData(sql);
	}
	
	/**
	 * 复制标准
	 * @param stdCode
	 * @param stdName
	 * @return
	 */
	public CoreReturnObject CopyStd(String stdCode, String stdName){
		SqlBuilder b = new SqlBuilder("CoreProductManage.CopyStd");
		b.Fill(new String[]{ stdCode, stdName });
		return this.setData(b.toString());
	}
	
	/**
	 * 是否存在标准名称
	 * @param stdName
	 * @return
	 */
	public CoreReturnObject IsExistStdName(String stdName){
		SqlBuilder b = new SqlBuilder("CoreProductManage.IsExistStdName");
		b.Fill(new String[]{stdName});
		return this.getData(b.toString());
	}
	
	/**
	 * 禁用合同
	 * @param stdCode
	 * @param flag
	 * @param name
	 * @return
	 */
	public CoreReturnObject ForbiddenOrder(String stdCode, String flag, String name){
		SqlBuilder b = new SqlBuilder("CoreProductManage.ForbiddenOrder");
		b.Fill(new String[]{stdCode, flag, name});
		return this.setData(b.toString());
	}
	
	public CoreReturnObject OrderLineUseCnt(String stdCode){
		SqlBuilder b = new SqlBuilder("CoreProductManage.OrderLineUseCnt");
		b.Fill(new String[]{ stdCode });
		return this.getData(b.toString());
		
	}
}
