package com.steering.pss.sale.order.DAL;

import CoreFS.SA06.CoreReturnObject;
import CoreFS.SA08.CoreXmlSqlParsersFactory;
import CoreFS.SC01.CoreConstantData;
import com.steering.comm.converter.CoreConverter;
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.comm.tools.TransctionTool;
import com.steering.pss.qcm.model.GenericEntity;
import com.steering.pss.sale.order.model.*;
import com.steering.pss.sale.order.qualityDesign.server.iserver.CoreCastWell;
import net.sf.json.JSONObject;

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

public class CraftOrdDesignDAL extends DBCommon{
	
	/**
	 * 查询工艺评审主表
	 * @param strJSON
	 * @return
	 */
	public CoreReturnObject query(String strJSON, String[] dataPurviewIds, String userId){
		CraftOrdDesignEntity craftOrdDesign = EntityTool.getEntityByJSONStr(strJSON, CraftOrdDesignEntity.class);
		String sql = CoreXmlSqlParsersFactory.getSql("CraftOrdDesignDAL.query");
		String[] createTimes = craftOrdDesign.getCreateTime().split(",");
		String createTime1 = "";
		String createTime2 = "";
		if(createTimes.length == 2){
			createTime1 = createTimes[0];
			createTime2 = createTimes[1];
		}
		sql = StringTool.ReplaceAll(sql, "#CREATE_TIME1#", createTime1);
		sql = StringTool.ReplaceAll(sql, "#CREATE_TIME2#", createTime2);
		String orderNo = "";
        String orderSeq = "";
        String[] manageOrderNos = craftOrdDesign.getOrderNo().split("/");
        if(manageOrderNos.length > 1){
        	for(int i = 0; i < manageOrderNos.length - 1; i++){
            	orderNo += manageOrderNos[i];
            }
        	orderSeq = manageOrderNos[manageOrderNos.length - 1];
        }else{
        	orderNo = manageOrderNos[0];
        }
		sql = StringTool.ReplaceAll(sql, "#ORDER_NO#", orderNo);
		sql = StringTool.ReplaceAll(sql, "#ORDER_SEQ#", orderSeq);
		sql = StringTool.ReplaceAll(sql, "#USER_ID#", userId);
		sql = EntityTool.fillSqlByEntity(sql, craftOrdDesign);
		return this.getData(sql);
	}
	
	/**
	 * 查询工艺评审主表
	 * @param strJSON
	 * @return
	 */
	public CoreReturnObject queryAudit(String strJSON, String[] dataPurviewIds, String userId){
		CraftOrdDesignEntity craftOrdDesign = EntityTool.getEntityByJSONStr(strJSON, CraftOrdDesignEntity.class);
		String sql = CoreXmlSqlParsersFactory.getSql("CraftOrdDesignDAL.queryAudit");
		String[] createTimes = craftOrdDesign.getCreateTime().split(",");
		String createTime1 = "";
		String createTime2 = "";
		if(createTimes.length == 2){
			createTime1 = createTimes[0];
			createTime2 = createTimes[1];
		}
		sql = StringTool.ReplaceAll(sql, "#CREATE_TIME1#", createTime1);
		sql = StringTool.ReplaceAll(sql, "#CREATE_TIME2#", createTime2);
		String orderNo = "";
        String orderSeq = "";
        String[] manageOrderNos = craftOrdDesign.getOrderNo().split("/");
        if(manageOrderNos.length > 1){
        	for(int i = 0; i < manageOrderNos.length - 1; i++){
            	orderNo += manageOrderNos[i];
            }
        	orderSeq = manageOrderNos[manageOrderNos.length - 1];
        }else{
        	orderNo = manageOrderNos[0];
        }
		sql = StringTool.ReplaceAll(sql, "#ORDER_NO#", orderNo);
		sql = StringTool.ReplaceAll(sql, "#ORDER_SEQ#", orderSeq);
		sql = StringTool.ReplaceAll(sql, "#USER_ID#", userId);
		sql = EntityTool.fillSqlByEntity(sql, craftOrdDesign);
		return this.getData(sql);
	}
	
	/**
	 * 二级工艺文件查询
	 * @param strJSON
	 * @return
	 */
	public CoreReturnObject queryCraftLv2(String craftNo, String orderNo, 
			String planStatus, String maintenanceStatus, String customInfo){
		SqlBuilder b = new SqlBuilder("CraftOrdDesignDAL.queryCraftLv2");
		b.BuildLike("PSC_BL", orderNo, SqlBuilder.Empty);
		String orderNoField = "A.ORDER_NO || '/' || B.ORDER_SEQ";
		b.BuildLike("ORDER_NO", orderNoField, orderNo, SqlBuilder.Empty);
		b.BuildLike("CRAFT_NO", "T.CRAFT_NO", craftNo, SqlBuilder.Empty);
		String fieldDesc = "T.CRAFT_FILE_LV2_" + customInfo;
		if(maintenanceStatus.equals("1")){
			b.Replace("maintenanceStatus", fieldDesc + " IS NULL");
		}else{
			b.Replace("maintenanceStatus", " 1=1");
		}
		b.Fill("PLAN_STATUS", planStatus);
		b.Fill("PROCESS_CODE", customInfo);
		return this.getData(b.GetSql());
	}

	/**
	 * 二级工艺文件查询 (新)
	 * @param craftNo
	 * @param orderManageNo
	 * @param planStatus
	 * @param maintenanceStatus
	 * @param customInfo
	 * @param exeStatus
	 * @return
	 */
	public CoreReturnObject queryCraftLv2New(String craftNo, String orderManageNo,
											 String planStatus, String maintenanceStatus, String customInfo,
											 String exeStatus){
		SqlBuilder b = new SqlBuilder("CraftOrdDesignDAL.queryCraftLv2New");
		String orderNo = orderManageNo.split("/")[0];
		String orderSeq = "";
		if(orderManageNo.split("/").length > 1){
			orderSeq = orderManageNo.split("/")[1];
		}
		b.Fill(new String[]{ exeStatus, planStatus, orderNo, orderSeq,
				craftNo, maintenanceStatus, customInfo});
		return this.getData(b.toString().replace("#PLINE_CODE#",""));
	}

	/**
	 * 二级工艺文件查询 (新)
	 * @param craftNo
	 * @param orderManageNo
	 * @param planStatus
	 * @param maintenanceStatus
	 * @param customInfo
	 * @param exeStatus
	 * @return
	 */
	public CoreReturnObject queryCraftLv2New(String craftNo, String orderManageNo,
											 String planStatus, String maintenanceStatus, String customInfo,
											 String exeStatus,ArrayList<String> Pline) {
		 return queryCraftLv2New( craftNo,  orderManageNo,
				  planStatus,  maintenanceStatus,  customInfo,
				 new String[]{exeStatus},  Pline);
	}


	/**
	 * 二级工艺文件查询 (新)
	 * @param craftNo
	 * @param orderManageNo
	 * @param planStatus
	 * @param maintenanceStatus
	 * @param customInfo
	 * @param exeStatus
	 * @return
	 */
	public CoreReturnObject queryCraftLv2New(String craftNo, String orderManageNo,
											 String planStatus, String maintenanceStatus, String customInfo,
											 String[] exeStatus,ArrayList<String> Pline){
		SqlBuilder b ;
		String PlineColumn = "";
		String ExeStatusColumn = "";
		if(customInfo.equals("E")){
			b = new SqlBuilder("CraftOrdDesignDAL.queryCraftLv2New_DBK");
			PlineColumn = "DBKS.PLINE_CODE";
			ExeStatusColumn = "DBKS.EXE_STATUS";
		} else if(customInfo.equals("F")){
			b = new SqlBuilder("CraftOrdDesignDAL.queryCraftLv2New_RCL");
			PlineColumn = "RCLS.PLINE_CODE";
			ExeStatusColumn = "RCLS.EXE_STATUS";
		}else if(customInfo.equals("G")){
			b = new SqlBuilder("CraftOrdDesignDAL.queryCraftLv2New_JGX");
			PlineColumn = "JGXS.PLINE_CODE";
			ExeStatusColumn = "JGXS.EXE_STATUS";
		}

		else {
		    b =new SqlBuilder("CraftOrdDesignDAL.queryCraftLv2New");
			PlineColumn = "ZGS.PLINE_CODE";
			ExeStatusColumn = "ZGS.EXE_STATUS";
		}
		String orderNo = orderManageNo.split("/")[0];
		String orderSeq = "";
		if(orderManageNo.split("/").length > 1){
			orderSeq = orderManageNo.split("/")[1];
		}
		String PlineCode = CoreConverter.toSqlConditionString(PlineColumn,
				Pline.toArray(new String[Pline.size()]));

		PlineCode += " "  +  CoreConverter.toSqlConditionString(ExeStatusColumn,
				exeStatus);

		b.Fill(new String[]{ "1", planStatus, orderNo, orderSeq,
				craftNo, maintenanceStatus, customInfo});
		return this.getData(b.toString().replace("#PLINE_CODE#",PlineCode));
	}
	/**
	 * 获取轧管未完成时间
	 * @return
	 */
	public CoreReturnObject queryUnfinishTimeZg(){
		SqlBuilder b = new SqlBuilder("CraftOrdDesignDAL.GetUnfinishTimeZg");
		return this.getData("MSSQL2Dao", b.toString());
	}
	
	/**
	 * 通过主键查询工艺评审主表
	 * @param strJSON
	 * @return
	 */
	public CoreReturnObject queryByPk(String strJSON){
		CraftOrdDesignEntity craftOrdDesign = EntityTool.getEntityByJSONStr(strJSON, CraftOrdDesignEntity.class);
		String sql = CoreXmlSqlParsersFactory.getSql("CraftOrdDesignDAL.queryByPk");
		sql = EntityTool.fillSqlByEntity(sql, craftOrdDesign);
		return this.getData(sql);
	}
	
	/**
	 * 通过合同行主键查询工艺评审记录
	 * @param strJSON
	 * @return
	 */
	public CoreReturnObject queryCraftByOrLnPk(String strJSON){
		CraftOrdDesignEntity craftOrdDesign = EntityTool.getEntityByJSONStr(strJSON, CraftOrdDesignEntity.class);
		String sql = CoreXmlSqlParsersFactory.getSql("CraftOrdDesignDAL.queryCraftByOrLnPk");
		sql = EntityTool.fillSqlByEntity(sql, craftOrdDesign);
		return this.getData(sql);
	}
	
	/**
	 * 科室审核
	 * @param listJSON
	 * @return
	 */
	public CoreReturnObject audit(ArrayList<String> listJSON){
		ArrayList<String> sqllist = new ArrayList<String>();
		for(String strJSON : listJSON){
			CraftOrdDesignEntity craftOrdDesign = EntityTool.getEntityByJSONStr(strJSON, CraftOrdDesignEntity.class);
			String sql = CoreXmlSqlParsersFactory.getSql("CraftOrdDesignDAL.audit");
			sql = EntityTool.fillSqlByEntity(sql, craftOrdDesign);
			sqllist.add(sql);
			if("5".equals(craftOrdDesign.getValidflag())){
				SqlBuilder b = new SqlBuilder("CraftOrdDesignDAL.deleteMessage");
				b.Fill(new String[]{ "00", craftOrdDesign.getOrdLnPk() });
		    	sqllist.add(b.toString());
			}else{
				SqlBuilder b = new SqlBuilder("CraftOrdDesignDAL.updateMessage");
				b.Fill(new String[]{ "1020711","1020711", craftOrdDesign.getOrdLnPk() });
		    	sqllist.add(b.toString());
			}
			
		}
		return this.getDao(CoreConstantData.sysbeanstr).ExcuteTransction(sqllist);
	}
	
	/**
	 * 相关科室审核
	 * @param listJSON
	 * @return
	 */
	public CoreReturnObject audit1(ArrayList<String> listJSON){
		ArrayList<String> sqllist = new ArrayList<String>();
		for(String strJSON : listJSON){
			CraftOrdDesignEntity craftOrdDesign = EntityTool.getEntityByJSONStr(strJSON, CraftOrdDesignEntity.class);
			String sql = CoreXmlSqlParsersFactory.getSql("CraftOrdDesignDAL.audit1");
			sql = EntityTool.fillSqlByEntity(sql, craftOrdDesign);
			sqllist.add(sql);
			if("5".equals(craftOrdDesign.getValidflag())){
				SqlBuilder b = new SqlBuilder("CraftOrdDesignDAL.deleteMessage");
				b.Fill(new String[]{ "00", craftOrdDesign.getOrdLnPk() });
		    	sqllist.add(b.toString());
			}else{
				SqlBuilder b = new SqlBuilder("CraftOrdDesignDAL.updateMessage");
				b.Fill(new String[]{ "1020710","1020710", craftOrdDesign.getOrdLnPk() });
		    	sqllist.add(b.toString());
			}
		}
		
		return this.getDao(CoreConstantData.sysbeanstr).ExcuteTransction(sqllist);
	}
	
	/**
	 * 审批
	 * @param strJSON
	 * @return
	 */
	public CoreReturnObject approval(ArrayList<String> listJSON){
		ArrayList<String> sqllist = new ArrayList<String>();
		for(String strJSON : listJSON){
			CraftOrdDesignEntity craftOrdDesign = EntityTool.getEntityByJSONStr(strJSON, CraftOrdDesignEntity.class);
			String sql = CoreXmlSqlParsersFactory.getSql("CraftOrdDesignDAL.approval");
			sql = EntityTool.fillSqlByEntity(sql, craftOrdDesign);
			sqllist.add(sql);
			if("5".equals(craftOrdDesign.getValidflag())){
				SqlBuilder b = new SqlBuilder("CraftOrdDesignDAL.deleteMessage");
				b.Fill(new String[]{ "00", craftOrdDesign.getOrdLnPk() });
		    	sqllist.add(b.toString());
			}else{
				SqlBuilder b = new SqlBuilder("CraftOrdDesignDAL.updateMessage");
				b.Fill(new String[]{ "1020712","1020712", craftOrdDesign.getOrdLnPk() });
		    	sqllist.add(b.toString());
			}
		}
		return this.getDao(CoreConstantData.sysbeanstr).ExcuteTransction(sqllist);
	}
	
	/**
	 * 修改审批人
	 * @param listJSON
	 * @return
	 */
	public CoreReturnObject updateApproveName(ArrayList<String> listJSON){
		ArrayList<String> sqllist = new ArrayList<String>();
		for(String strJSON : listJSON){
			CraftOrdDesignEntity craftOrdDesign = EntityTool.getEntityByJSONStr(strJSON, CraftOrdDesignEntity.class);
			String sql = CoreXmlSqlParsersFactory.getSql("CraftOrdDesignDAL.UpdateApproveName");
			sql = EntityTool.fillSqlByEntity(sql, craftOrdDesign);
			sqllist.add(sql);
		}
		return this.getDao(CoreConstantData.sysbeanstr).ExcuteTransction(sqllist);
	}
	
	/**
	 * 修改批准人
	 * @param listJSON
	 * @return
	 */
	public CoreReturnObject updateApprove1Name(ArrayList<String> listJSON){
		ArrayList<String> sqllist = new ArrayList<String>();
		for(String strJSON : listJSON){
			CraftOrdDesignEntity craftOrdDesign = EntityTool.getEntityByJSONStr(strJSON, CraftOrdDesignEntity.class);
			String sql = CoreXmlSqlParsersFactory.getSql("CraftOrdDesignDAL.UpdateApprove1Name");
			sql = EntityTool.fillSqlByEntity(sql, craftOrdDesign);
			sqllist.add(sql);
		}
		return this.getDao(CoreConstantData.sysbeanstr).ExcuteTransction(sqllist);
	}
	
	/**
	 * 批准
	 * @param strJSON
	 * @return
	 */
	public CoreReturnObject approval1(ArrayList<String> listJSON){
		ArrayList<String> sqllist = new ArrayList<String>();
		for(String strJSON : listJSON){
			CraftOrdDesignEntity craftOrdDesign = EntityTool.getEntityByJSONStr(strJSON, CraftOrdDesignEntity.class);
			String sql = CoreXmlSqlParsersFactory.getSql("CraftOrdDesignDAL.approval1");
			sql = EntityTool.fillSqlByEntity(sql, craftOrdDesign);
			sqllist.add(sql);
			if("5".equals(craftOrdDesign.getValidflag())){
				SqlBuilder b = new SqlBuilder("CraftOrdDesignDAL.deleteMessage");
				b.Fill(new String[]{ "00", craftOrdDesign.getOrdLnPk() });
		    	sqllist.add(b.toString());
			}else{
				SqlBuilder b = new SqlBuilder("CraftOrdDesignDAL.updateMessage");
				b.Fill(new String[]{ "","", craftOrdDesign.getOrdLnPk() });
		    	sqllist.add(b.toString());
			}
		}
		return this.getDao(CoreConstantData.sysbeanstr).ExcuteTransction(sqllist);
	}
	
	/**
	 * 人工创建工艺文件
	 * @param strJSON
	 * @return
	 */
	public CoreReturnObject createCraft(ArrayList<String> listJSON,
			Boolean autoBacklog, Boolean autoMaterial, String userId){
		TransctionTool transction = new TransctionTool();
		Connection con = transction.getCon();
		try{
			for(String strJSON : listJSON){
				CraftOrdDesignEntity craftOrdDesign = EntityTool.getEntityByJSONStr(strJSON, CraftOrdDesignEntity.class);
				
				String sqlCraftNo = CoreXmlSqlParsersFactory.getSql("CraftOrdDesignDAL.createCraftNo");
				sqlCraftNo = EntityTool.fillSqlByEntity(sqlCraftNo, craftOrdDesign);
				GenericEntity craftNo = EntityTool.getEntityBySqlNoClsCon(sqlCraftNo, con, GenericEntity.class);
				if(craftOrdDesign.getCraftNo().equals("")){
					craftOrdDesign.setCraftNo(craftNo.getValue());
				}
				
				String getCraftNoV = CoreXmlSqlParsersFactory.getSql("CraftOrdDesignDAL.GetCraftNoV");
				getCraftNoV = EntityTool.fillSqlByEntity(getCraftNoV, craftOrdDesign);
				GenericEntity craftNoV = EntityTool.getEntityBySqlNoClsCon(getCraftNoV, con, GenericEntity.class);
				craftOrdDesign.setCraftNoV(craftNoV.getMaxValue());
				
				String sql = CoreXmlSqlParsersFactory.getSql("CraftOrdDesignDAL.createCraft");
				sql = EntityTool.fillSqlByEntity(sql, craftOrdDesign);
				if(!transction.ExcuteTransction(sql)){
					return transction.getReturnObject();
				}
				
				//修改工艺负责人
				SqlBuilder b = new SqlBuilder("CraftOrdDesignDAL.getCraftDesignAppointCnt");
				b.Fill(new String[]{ craftOrdDesign.getOrdLnPk() });
				GenericEntity cnt = EntityTool.getEntityBySqlNoClsCon(
						b.toString(), con, GenericEntity.class);
				if(cnt.getValue().equals("0")){
					b = new SqlBuilder("CraftOrdDesignDAL.insertCraftDesignAppoint");
					b.Fill(new String[]{ craftOrdDesign.getOrdLnPk(), craftOrdDesign.getDeleteName(), userId });
					if(!transction.ExcuteTransction(b.toString())){
						return transction.getReturnObject();
					}
				}
				
				
				//修改合同行中的工艺文件号和路径。
				String sqlUpdateOrdLnCraft = CoreXmlSqlParsersFactory.getSql("CraftOrdDesignDAL.updateOrdLnCraft");
				sqlUpdateOrdLnCraft = EntityTool.fillSqlByEntity(sqlUpdateOrdLnCraft, craftOrdDesign);
				if(!transction.ExcuteTransction(sqlUpdateOrdLnCraft)){
					return transction.getReturnObject();
				}
				
				//自动把旧的工艺文件置为无效
				String sqlSetCraftValid = CoreXmlSqlParsersFactory.getSql("CraftOrdDesignDAL.setCraftValidByOrdLnPk");
				sqlSetCraftValid = EntityTool.fillSqlByEntity(sqlSetCraftValid, craftOrdDesign);
				if(!transction.ExcuteTransction(sqlSetCraftValid)){
					return transction.getReturnObject();
				}
				
				//修改物料和制程的自动生成工艺文件
				if(autoBacklog){
					b = new SqlBuilder("CraftOrdDesignDAL.SetBacklogAutoCraft");
					b.Fill("ORD_LN_PK", craftOrdDesign.getOrdLnPk());
					if(!transction.ExcuteTransction(b.toString())){
						return transction.getReturnObject();
					}
				}
				if(autoMaterial){
					b = new SqlBuilder("CraftOrdDesignDAL.SetMaterialAutoCraft");
					b.Fill("ORD_LN_PK", craftOrdDesign.getOrdLnPk());
					if(!transction.ExcuteTransction(b.toString())){
						return transction.getReturnObject();
					}
				}
			}
			return transction.commit();
		}catch(Exception ex){
			CoreReturnObject cro = transction.rollBack();
			cro.setV_errMsg(ex.getMessage());
			return cro;
		}
	}
	
	/**
	 * 解锁
	 * @param strJSON
	 * @return
	 */
	public CoreReturnObject unLock(ArrayList<String> listJSON){
		ArrayList<String> sqllist = new ArrayList<String>();
		for(String strJSON : listJSON){
			CraftOrdDesignEntity craftOrdDesign = EntityTool.getEntityByJSONStr(strJSON, CraftOrdDesignEntity.class);
			String sql = CoreXmlSqlParsersFactory.getSql("CraftOrdDesignDAL.unLock");
			sql = EntityTool.fillSqlByEntity(sql, craftOrdDesign);
			sqllist.add(sql);
		}
		
		return this.getDao(CoreConstantData.sysbeanstr).ExcuteTransction(sqllist);
	}
	
	/**
	 * 封锁
	 * @param strJSON
	 * @return
	 */
	public CoreReturnObject lock(ArrayList<String> listJSON){
		ArrayList<String> sqllist = new ArrayList<String>();
		for(String strJSON : listJSON){
			CraftOrdDesignEntity craftOrdDesign = EntityTool.getEntityByJSONStr(strJSON, CraftOrdDesignEntity.class);
			String sql = CoreXmlSqlParsersFactory.getSql("CraftOrdDesignDAL.lock");
			sql = EntityTool.fillSqlByEntity(sql, craftOrdDesign);
			sqllist.add(sql);
		}
		
		return this.getDao(CoreConstantData.sysbeanstr).ExcuteTransction(sqllist);
	}
	
	/**
	 * 修改备注
	 * @param strJSON
	 * @return
	 */
	public CoreReturnObject setMemo(ArrayList<String> listJSON){
		ArrayList<String> sqllist = new ArrayList<String>();
		for(String strJSON : listJSON){
			CraftOrdDesignEntity craftOrdDesign = EntityTool.getEntityByJSONStr(strJSON, CraftOrdDesignEntity.class);
			String sql = CoreXmlSqlParsersFactory.getSql("CraftOrdDesignDAL.setMemo");
			sql = EntityTool.fillSqlByEntity(sql, craftOrdDesign);
			sqllist.add(sql);
		}
		
		return this.getDao(CoreConstantData.sysbeanstr).ExcuteTransction(sqllist);
	}
	
	/**
	 * 修改二级工艺文件
	 * @param strJSON
	 * @return
	 */
	public CoreReturnObject SaveCraftLv2(ArrayList<String> listJSON){
		ArrayList<String> sqllist = new ArrayList<String>();
		for(String strJSON : listJSON){
			CraftOrdDesignEntity craftOrdDesign = EntityTool.getEntityByJSONStr(strJSON, CraftOrdDesignEntity.class);
			String sql = CoreXmlSqlParsersFactory.getSql("CraftOrdDesignDAL.SaveCraftLv2");
			sql = EntityTool.fillSqlByEntity(sql, craftOrdDesign);
			sqllist.add(sql);
		}
		
		return this.getDao(CoreConstantData.sysbeanstr).ExcuteTransction(sqllist);
	}
	
	/**
	 * 提交审核
	 * @param strJSON
	 * @return
	 */
	public CoreReturnObject updateCraftNo(ArrayList<String> listJSON, ArrayList<ArrayList<String>> lists){
		TransctionTool transction = new TransctionTool();
		try{
			for(int i = 0; i < listJSON.size(); i++){
				String strJSON = listJSON.get(i);
				CraftOrdDesignEntity craftOrdDesign = EntityTool.getEntityByJSONStr(strJSON, CraftOrdDesignEntity.class);
				SqlBuilder b = new SqlBuilder("CraftOrdDesignDAL.SetValidflagByPk");
				b.Fill(new String[]{ craftOrdDesign.getValidflag(), craftOrdDesign.getOrdLnPk(),
						craftOrdDesign.getCraftSeq() });
				if(!transction.ExcuteTransction(b.toString())){
					return transction.getReturnObject();
				}
//				String sqlCraftNo = CoreXmlSqlParsersFactory.getSql("CraftOrdDesignDAL.createCraftNo");
//				sqlCraftNo = EntityTool.fillSqlByEntity(sqlCraftNo, craftOrdDesign);
//				GenericEntity craftNo = EntityTool.getEntityBySqlNoClsCon(sqlCraftNo, transction.getCon(), GenericEntity.class);
//				if(craftOrdDesign.getCraftNo().equals("")){
//					craftOrdDesign.setCraftNo(craftNo.getValue());
//				}
//				
//				Connection con = transction.getCon();
//				String getCraftNoV = CoreXmlSqlParsersFactory.getSql("CraftOrdDesignDAL.GetCraftNoV");
//				getCraftNoV = EntityTool.fillSqlByEntity(getCraftNoV, craftOrdDesign);
//				GenericEntity craftNoV = EntityTool.getEntityBySqlNoClsCon(getCraftNoV, con, GenericEntity.class);
//				String sql = CoreXmlSqlParsersFactory.getSql("CraftOrdDesignDAL.updateCraftNo");
//				craftOrdDesign.setCraftNoV(craftNoV.getMaxValue());
//				sql = EntityTool.fillSqlByEntity(sql, craftOrdDesign);
//				if(!transction.ExcuteTransction(sql)){
//					return transction.getReturnObject();
//				}
			}
		}catch(Exception ex){
			CoreReturnObject cro = transction.rollBack();
			cro.setV_errMsg(ex.getMessage());
			return cro;
		}
		return transction.commit();
	}
	
	/**
	 * 修改工艺文件路径
	 * @param strJSON
	 * @return
	 */
	public CoreReturnObject updateCraftPath(String strJSON, String isMutiUse, String isCraftPath){
		TransctionTool transction = new TransctionTool();
		try{
			Connection con = transction.getCon();
			CraftOrdDesignEntity craftOrdDesign = EntityTool.getEntityByJSONStr(strJSON, CraftOrdDesignEntity.class);
			String sqlMaxCraftNoV = CoreXmlSqlParsersFactory.getSql("CraftOrdDesignDAL.getMaxCraftNoV");
			sqlMaxCraftNoV = EntityTool.fillSqlByEntity(sqlMaxCraftNoV, craftOrdDesign);
			//GenericEntity maxCraftNoV = EntityTool.getEntityBySqlNoClsCon(sqlMaxCraftNoV, con, GenericEntity.class);
			if(craftOrdDesign.getCraftNoV().equals(""))
			{
				craftOrdDesign.setCraftNoV("00");
//				if(maxCraftNoV.getValue().equals("")){
//					craftOrdDesign.setCraftNoV("00");
//				}else{
//					craftOrdDesign.setCraftNoV(maxCraftNoV.getValue());
//				}
			}
			String sqlCraftOrdDesign = CoreXmlSqlParsersFactory.getSql("CraftOrdDesignDAL.getCraftOrdDesignByPk");
			sqlCraftOrdDesign = EntityTool.fillSqlByEntity(sqlCraftOrdDesign, craftOrdDesign);
			CraftOrdDesignEntity craftOrdDesingRow = EntityTool.getEntityBySqlNoClsCon(sqlCraftOrdDesign, con, CraftOrdDesignEntity.class);
			if(isCraftPath.equals("1")){
				craftOrdDesign.setCraftNo(craftOrdDesingRow.getCraftNo());
			}
			String sql = CoreXmlSqlParsersFactory.getSql("CraftOrdDesignDAL.updateCraftPath");
			String sqlUpdateOrdLnCraft = CoreXmlSqlParsersFactory.getSql("CraftOrdDesignDAL.updateOrdLnCraft");
			sqlUpdateOrdLnCraft = EntityTool.fillSqlByEntity(sqlUpdateOrdLnCraft, craftOrdDesign);
			if(craftOrdDesign.getCraftNo().equals("") || craftOrdDesign.getCraftPath().equals("")){
				craftOrdDesign.setValidflag("2");
				craftOrdDesign.setLockFlag("1");
			}else{
				if(craftOrdDesign.getCraftSeq().equals("1")){
					craftOrdDesign.setValidflag("1");
					craftOrdDesign.setLockFlag("1");
				}else{
					craftOrdDesign.setValidflag("8");
					craftOrdDesign.setLockFlag("1");
				}
			}
			sql = EntityTool.fillSqlByEntity(sql, craftOrdDesign);
			if(transction.ExcuteTransction(sql) == false){
				return transction.getReturnObject();
			}
			if(transction.ExcuteTransction(sqlUpdateOrdLnCraft) == false){
				return transction.getReturnObject();
			}
			
			//自动把旧的工艺文件置为无效
			String sqlSetCraftValid = CoreXmlSqlParsersFactory.getSql("CraftOrdDesignDAL.setCraftValidByOrdLnPk");
			sqlSetCraftValid = EntityTool.fillSqlByEntity(sqlSetCraftValid, craftOrdDesign);
			if(transction.ExcuteTransction(sqlSetCraftValid) == false){
				return transction.getReturnObject();
			}
//			if(isMutiUse.equals("1")){
//				String sqlInsertCraftFile = CoreXmlSqlParsersFactory.getSql("CraftOrdDesignDAL.insertCraftOrdDesignFile");
//				sqlInsertCraftFile = EntityTool.fillSqlByEntity(sqlInsertCraftFile, craftOrdDesign);
//				if(transction.ExcuteTransction(sqlInsertCraftFile) == false){
//					return transction.getReturnObject();
//				}
//			}
		}catch(Exception ex){
			CoreReturnObject cro = transction.rollBack();
			cro.setV_errMsg(ex.getMessage());
			return cro;
		}
		return transction.commit();
	}
	
	/**
	 * 新增工艺文件
	 * @param list
	 * @return
	 */
	public CoreReturnObject insertCraftOrdDesignFile(ArrayList<String> list){
		CraftOrdDesignFileEntity craftOrdDesign = new CraftOrdDesignFileEntity();
		craftOrdDesign.setCraftNo(list.get(0));
		craftOrdDesign.setCraftNoV(list.get(1));
		craftOrdDesign.setCraftPath(list.get(2));
		craftOrdDesign.setCreateName(list.get(3));
		craftOrdDesign.setCraftKey(list.get(4));
		String sqlInsertCraftFile = CoreXmlSqlParsersFactory.getSql("CraftOrdDesignDAL.insertCraftOrdDesignFile");
		sqlInsertCraftFile = EntityTool.fillSqlByEntity(sqlInsertCraftFile, craftOrdDesign);
		return this.setData(sqlInsertCraftFile);
	}
	
	/**
	 * 获取工艺文件表中的工艺文件号数目
	 * @param craftNo
	 * @return
	 */
	public CoreReturnObject getCraftFileCraftNoCnt(String craftNo){
		String sql = CoreXmlSqlParsersFactory.getSql("CraftOrdDesignDAL.getCraftFileCraftNoCnt");
		sql = StringTool.ReplaceAll(sql, "#CRAFT_NO#", craftNo);
		return this.getData(sql);
	}
	
	/**
	 * 通过主键查询工艺文件
	 * @param craftNo
	 * @param craftNoV
	 * @return
	 */
	public CoreReturnObject getCraftFileByPk(String craftNo, String craftNoV){
		String sql = CoreXmlSqlParsersFactory.getSql("CraftOrdDesignDAL.getCraftFileByPk");
	sql = StringTool.ReplaceAll(sql, "#CRAFT_NO#", craftNo);
	sql = StringTool.ReplaceAll(sql, "#CRAFT_NO_V#", craftNoV);
		return this.getData(sql);
}
	
	/**
	 * 查询最大的工艺文件版本号
	 * @param craftNo
	 * @return
	 */
	public CoreReturnObject getCraftFileMaxCraftNoV(String craftNo){
		String sql = CoreXmlSqlParsersFactory.getSql("CraftOrdDesignDAL.getCraftFileMaxCraftNoV");
		sql = StringTool.ReplaceAll(sql, "#CRAFT_NO#", craftNo);
		return this.getData(sql);
	}
	
	/**
	 * 获取工艺文件号的数目
	 * @param craftNo
	 * @return
	 */
	public CoreReturnObject getCraftNoCnt(String craftNo){
		String sql = CoreXmlSqlParsersFactory.getSql("CraftOrdDesignDAL.getCraftNoCnt");
		sql = StringTool.ReplaceAll(sql, "#CRAFT_NO#", craftNo);
		return this.getData(sql);
	}
	
	/**
	 * 获取排除合同行的工艺文件数目
	 * @param craftNo
	 * @param ordLnPk
	 * @return
	 */
	public CoreReturnObject getCraftNoCntOutLn(String craftNo, String craftNoV, String ordLnPk){
		String sql = CoreXmlSqlParsersFactory.getSql("CraftOrdDesignDAL.getCraftNoCntOutLn");
		sql = StringTool.ReplaceAll(sql, "#CRAFT_NO#", craftNo);
		sql = StringTool.ReplaceAll(sql, "#CRAFT_NO_V#", craftNoV);
		sql = StringTool.ReplaceAll(sql, "#ORD_LN_PK#", ordLnPk);
		return this.getData(sql);
	}
	
	/**
	 * 获取引用工艺文件号的数目
	 * @param craftNo
	 * @return
	 */
	public CoreReturnObject getRefCraftNoCnt(String craftNo, String ordLnPk, String craftSeq){
		String sql = CoreXmlSqlParsersFactory.getSql("CraftOrdDesignDAL.getRefCraftNoCnt");
		sql = StringTool.ReplaceAll(sql, "#CRAFT_NO#", craftNo);
		sql = StringTool.ReplaceAll(sql, "#ORD_LN_PK#", ordLnPk);
		sql = StringTool.ReplaceAll(sql, "#CRAFT_ID#", craftSeq);
		return this.getData(sql);
	}
	
	/**
	 * 查询最大的工艺文件版本号
	 * @param craftNo
	 * @return
	 */
	public CoreReturnObject getCraftMaxCraftNoV(String craftNo){
		String sql = CoreXmlSqlParsersFactory.getSql("CraftOrdDesignDAL.getCraftMaxCraftNoV");
		sql = StringTool.ReplaceAll(sql, "#CRAFT_NO#", craftNo);
		return this.getData(sql);
	}
	
	/**
	 * 通过合同行主键查询有效数目
	 * @return
	 */
	public CoreReturnObject getCraftValidCntByOrLnPk(String ordLnPk){
		String sql = CoreXmlSqlParsersFactory.getSql("CraftOrdDesignDAL.getCraftValidCntByOrLnPk");
		sql = StringTool.ReplaceAll(sql, "#ORD_LN_PK#", ordLnPk);
		return this.getData(sql);
	}
	
	/**
	 * 匹配工艺文件
	 * @param strJSON
	 * @return
	 */
	public CoreReturnObject matchCraftFile(ArrayList<String> listJSON){
		ArrayList<String> sqllist = new ArrayList<String>();
		for(String strJSON : listJSON){
			CraftOrdDesignEntity craftOrdDesign = EntityTool.getEntityByJSONStr(strJSON, CraftOrdDesignEntity.class);
			String sql = CoreXmlSqlParsersFactory.getSql("CraftOrdDesignDAL.matchCraftFile");
			sql = EntityTool.fillSqlByEntity(sql, craftOrdDesign);
			sqllist.add(sql);
		}
		
		return this.getDao(CoreConstantData.sysbeanstr).ExcuteTransction(sqllist);
	}
	
	/**
	 * 查询最大的工艺号并且有效的。
	 * @param ordLnPk
	 * @return
	 */
	public CoreReturnObject getNewValidCraft(String ordLnPk){
		String sql = CoreXmlSqlParsersFactory.getSql("CraftOrdDesignDAL.getNewValidCraft");
		sql = StringTool.ReplaceAll(sql, "#ORD_LN_PK#", ordLnPk);
		return this.getData(sql);
	}
	
	/**
	 * 设置最大的工艺序号生效
	 * @param ordLnPk
	 * @return
	 */
	public CoreReturnObject setCraftOrDesignValid(String ordLnPk){
		CoreReturnObject cro = new CoreReturnObject();
		Connection con = this.getDao(CoreConstantData.sysbeanstr).getConnection();
		try{
			setCraftOrDesignValid(ordLnPk, con);
		}catch(Exception ex){
			cro.setV_errMsg(ex.getMessage());
			return cro;
		}
		
		return cro;
	}
	
	/**
	 * 设置最大的工艺序号生效
	 * @param ordLnPk
	 * @param con
	 * @throws SQLException
	 */
	public void setCraftOrDesignValid(String ordLnPk, Connection con) throws SQLException{
		String sqlSetCraftOrdDesignValid = CoreXmlSqlParsersFactory.getSql("CraftOrdDesignDAL.setCraftOrDesignValid");
		sqlSetCraftOrdDesignValid = StringTool.ReplaceAll(sqlSetCraftOrdDesignValid, "#ORD_LN_PK#", ordLnPk);
		PreparedStatement ps = con.prepareStatement(sqlSetCraftOrdDesignValid);
		ps.executeUpdate();
		ps.close();
	}
	
	/**
	 * 获取DESIGN_KEY数目
	 * @param designKey
	 * @return
	 */
	public CoreReturnObject getDesignKeyCnt(String designKey){
		String sql = CoreXmlSqlParsersFactory.getSql("CraftOrdDesignDAL.getDesignKeyCnt");
		sql = StringTool.ReplaceAll(sql, "#DESGIN_KEY#", designKey);
		return this.getData(sql);
	}
	
	public CoreReturnObject getCntByDesignKeyProcess(String designKey, String processCode)
	{
		String sql = CoreXmlSqlParsersFactory.getSql("CraftOrdDesignDAL.getCntByDesignKeyProcess");
		sql = StringTool.ReplaceAll(sql, "#DESGIN_KEY#", designKey);
		sql = StringTool.ReplaceAll(sql, "#PROCESS_CODE#", processCode);
		
		return this.getData(sql);
	}
	
	/**
	 * 复制二级工艺文件
	 * @param ordLnPk
	 * @param craftSeq
	 * @return
	 */
	public CoreReturnObject CopyCraftFileLv2(String ordLnPk, String craftSeq, String craftSeq2){
		SqlBuilder b = new SqlBuilder("CraftOrdDesignDAL.CopyCraftFileLv2");
		b.Fill("ORD_LN_PK", ordLnPk);
		b.Fill("CRAFT_SEQ", craftSeq);
		b.Fill("CRAFT_SEQ2", craftSeq2);
		return this.setData(b.toString());
	}
	
	/**
	 * 获取上一个二级工艺文件
	 * @param ordLnPk
	 * @param craftSeq
	 * @return
	 */
	public CoreReturnObject GetPreviousCraftLv2(String ordLnPk, String craftSeq){
		SqlBuilder b = new SqlBuilder("CraftOrdDesignDAL.GetPreviousCraftLv2");
		b.Fill("ORD_LN_PK", ordLnPk);
		b.Fill("CRAFT_SEQ", craftSeq);
		return this.getData(b.toString());
	}
	
	/**
	 * 删除工艺文件（没有被已下发的合同引用过的都可以删除）
	 * @param craftNo
	 * @param craftNoV
	 * @return
	 */
	public CoreReturnObject DeleteCraftFile(ArrayList<String> listJSON){
		ArrayList<String> sqllist = new ArrayList<String>();
		for(String strJSON : listJSON){
			CraftOrdDesignEntity craftOrdDesign = EntityTool.getEntityByJSONStr(strJSON, CraftOrdDesignEntity.class);
			String sql = CoreXmlSqlParsersFactory.getSql("CraftOrdDesignDAL.DeleteCraftFile");
			sql = EntityTool.fillSqlByEntity(sql, craftOrdDesign);
			sqllist.add(sql);
		}
		return this.getDao(CoreConstantData.sysbeanstr).ExcuteTransction(sqllist);
	}
	
	public CoreReturnObject GetAutoCreateCraftParms(String ordLnPk){
		SqlBuilder b = new SqlBuilder("CraftOrdDesignDAL.GetMaxCraftSeq");
		b.Fill("ORD_LN_PK", ordLnPk);
		GenericEntity craftSeq = EntityTool.getEntityBySql(b.toString(),
				this.getDao(CoreConstantData.sysbeanstr).getConnection(), GenericEntity.class);
		
		b = new SqlBuilder("CraftOrdDesignDAL.createCraftNo");
		b.Fill("ORD_LN_PK", ordLnPk);
		b.Fill("CRAFT_SEQ", craftSeq.getMaxValue());
		GenericEntity craftNo = EntityTool.getEntityBySql(b.toString(),
				this.getDao(CoreConstantData.sysbeanstr).getConnection(), GenericEntity.class);
		
		b = new SqlBuilder("CraftOrdDesignDAL.createCraftPDFName");
		b.Fill("ORD_LN_PK", ordLnPk);
		b.Fill("CRAFT_SEQ", craftSeq.getMaxValue());
		GenericEntity pdfName = EntityTool.getEntityBySql(b.toString(),
				this.getDao(CoreConstantData.sysbeanstr).getConnection(), GenericEntity.class);
		
		b = new SqlBuilder("CraftOrdDesignDAL.queryByPk");
		b.Fill("ORD_LN_PK", ordLnPk);
		b.Fill("CRAFT_SEQ", craftSeq.getMaxValue());
		CraftOrdDesignEntity craftOrdDesign = EntityTool.getEntityBySql(b.toString(),
				this.getDao(CoreConstantData.sysbeanstr).getConnection(), CraftOrdDesignEntity.class);
		
		
		String[] results = new String[]{craftSeq.getMaxValue(), craftNo.getValue(), 
				craftOrdDesign.getDesginKey(), pdfName.getValue()};
		CoreReturnObject cro = new CoreReturnObject();
		cro.setResult(results);
		return cro;
	}
	
	public CoreReturnObject GetMaterialAutoCraft(String ordLnPk){
		SqlBuilder b = new SqlBuilder("CraftOrdDesignDAL.GetMaterialAutoCraft");
		b.Fill("ORD_LN_PK", ordLnPk);
		return this.getData(b.toString());
	}
	
	public CoreReturnObject GetBacklogAutoCraft(String ordLnPk){
		SqlBuilder b = new SqlBuilder("CraftOrdDesignDAL.GetBacklogAutoCraft");
		b.Fill("ORD_LN_PK", ordLnPk);
		return this.getData(b.toString());
	}
	
	public CoreReturnObject AutoCreateCraft(String ordLnPk, String craftSeq, String craftNo,
			String craftPath, String user, String designStyle){
		TransctionTool transction = new TransctionTool();
		Connection con = transction.getCon();
		CraftAutoPeopleEntity autoPeople = null;
		try{
			String getCraftNoV = CoreXmlSqlParsersFactory.getSql("CraftOrdDesignDAL.GetCraftNoV");
			getCraftNoV = StringTool.ReplaceAll(getCraftNoV, "#CRAFT_NO#", craftNo);
			GenericEntity craftNoV = EntityTool.getEntityBySqlNoClsCon(getCraftNoV, con, GenericEntity.class);

			CoreCastWell coreCastWell = new CoreCastWell();
			String[] saleInfo = coreCastWell.getSaleInfo(con, ordLnPk.substring(0, ordLnPk.length() - 3));

			if (saleInfo != null && saleInfo.length > 0 && "100105".equals(saleInfo[0])) {
				SqlBuilder b = new SqlBuilder("CraftOrdDesignDAL.GetAutoPeopleTh");
				b.Fill(new String[]{ ordLnPk, ordLnPk, craftSeq });
				autoPeople = EntityTool.getEntityBySqlNoClsCon(b.toString(), con, CraftAutoPeopleEntity.class);
			} else {
				SqlBuilder b = new SqlBuilder("CraftOrdDesignDAL.GetAutoPeople");
				b.Fill(new String[]{ ordLnPk, ordLnPk, craftSeq });
				autoPeople = EntityTool.getEntityBySqlNoClsCon(b.toString(), con, CraftAutoPeopleEntity.class);
			}

			SqlBuilder b = new SqlBuilder("CraftOrdDesignDAL.AutoCreateCraft");
			b.Fill("ORD_LN_PK", ordLnPk);
			b.Fill("CRAFT_SEQ", craftSeq);
			b.Fill("CRAFT_NO", craftNo);
			b.Fill("CRAFT_PATH", craftPath);
			b.Fill("CRAFT_NO_V", craftNoV.getMaxValue());
			b.Fill("CRAFT_PATH", craftPath);
			b.Fill("UPDATE_NAME", user);
			if(autoPeople != null){
				b.Fill("AUDIT_NAME", autoPeople.getAuditDesc());
				b.Fill("AUDIT_NAME1", autoPeople.getAuditDesc1());
				b.Fill("APPROVAL_NAME", autoPeople.getApproveDesc());
				b.Fill("APPROVAL_NAME1", autoPeople.getApproveDesc1());
				b.Fill("DELETE_NAME", autoPeople.getUpdateName());
			}else{
				b.Fill("APPROVAL_NAME", "");
				b.Fill("AUDIT_NAME", "");
				b.Fill("AUDIT_NAME1", "");
				b.Fill("APPROVAL_NAME1", "");
			}
			if(designStyle.equals("0")){
				b.Fill("CRAFT_SOURCE", "销售提报");
			}
			else if(designStyle.equals("2")){
				b.Fill("CRAFT_SOURCE", "系统生成");
			}
			else{
				b.Fill("CRAFT_SOURCE", "系统自动生成");
			}
			if(!transction.ExcuteTransction(b.toString())){
				return transction.getReturnObject();
			}
			
			String sqlUpdateOrderLine = CoreXmlSqlParsersFactory.getSql("CraftOrdDesignDAL.updateOrdLnCraft");
			sqlUpdateOrderLine = StringTool.ReplaceAll(sqlUpdateOrderLine, "#CRAFT_NO#", craftNo);
			sqlUpdateOrderLine = StringTool.ReplaceAll(sqlUpdateOrderLine, "#CRAFT_NO_V#", craftNoV.getMaxValue());
			sqlUpdateOrderLine = StringTool.ReplaceAll(sqlUpdateOrderLine, "#ORD_LN_PK#", ordLnPk);
			if(!transction.ExcuteTransction(sqlUpdateOrderLine)){
				return transction.getReturnObject();
			}
			
			return transction.commit();
		}catch(Exception ex){
			CoreReturnObject cro = transction.rollBack();
			cro.setV_errMsg(ex.getMessage());
			return cro;
		}
	}
	
	public CoreReturnObject SetValidflagByPk(String validflag, String ordLnPk, String craftSeq){
		SqlBuilder b = new SqlBuilder("CraftOrdDesignDAL.SetValidflagByPk");
		b.Fill(new String[]{validflag, ordLnPk, craftSeq});
		return this.setData(b.toString());
	}
	
	public CoreReturnObject GetMaxCraftSeq(String ordLnPk){
		SqlBuilder b = new SqlBuilder("CraftOrdDesignDAL.GetMaxCraftSeq");
		b.Fill("ORD_LN_PK", ordLnPk);
		return this.getData(b.toString());
	}
	
	public CoreReturnObject SetMaterialAutoCraft(String ordLnPk){
		SqlBuilder b = new SqlBuilder("CraftOrdDesignDAL.SetMaterialAutoCraft");
		b.Fill("ORD_LN_PK", ordLnPk);
		return this.setData(b.toString());
	}
	
	public CoreReturnObject SetBacklogAutoCraft(String ordLnPk){
		SqlBuilder b = new SqlBuilder("CraftOrdDesignDAL.SetBacklogAutoCraft");
		b.Fill("ORD_LN_PK", ordLnPk);
		return this.setData(b.toString());
	}
	
	public CoreReturnObject SetCraftOrdDesignMaterial(String ordLnPk){
		SqlBuilder b = new SqlBuilder("CraftOrdDesignDAL.SetCraftOrdDesignMaterial");
		b.Fill("ORD_LN_PK", ordLnPk);
		return this.setData(b.toString());
	}
	
	public CoreReturnObject SetCraftOrdDesignBacklog(String ordLnPk){
		SqlBuilder b = new SqlBuilder("CraftOrdDesignDAL.SetCraftOrdDesignBacklog");
		b.Fill("ORD_LN_PK", ordLnPk);
		return this.setData(b.toString());
	}
	
	/**
	 * 通过合同行(或者备料码)查询msc
	 * @param ordLnPk1
	 * @return
	 */
	public CoreReturnObject GetMscByOrdLnPk(String ordLnPk){
		SqlBuilder b = new SqlBuilder("CraftOrdDesignDAL.GetMscByOrdLnPk");
		b.Fill(new String[]{ordLnPk});
		return this.getData(b.toString());
	}
	
	/**
	 * 设置工序检验计划是否确认
	 * @param processCode
	 * @param flag
	 * @param ordLnPk
	 * @param craftSeq
	 * @return
	 */
	public CoreReturnObject SetProcessFlag(String processCode, String flag,
			String ordLnPk, String craftSeq){
		SqlBuilder b = new SqlBuilder("CraftOrdDesignDAL.SetProcessFlag");
		b.Fill(new String[]{ processCode, flag, ordLnPk, craftSeq });
		return this.setData(b.toString());
	}
	
	/**
	 * 设置工序检验是否计划科委托
	 * @param processCode
	 * @param flag
	 * @param ordLnPk
	 * @param craftSeq
	 * @return
	 */
	public CoreReturnObject SetProcessJhFlag(String processCode, String flag,
			String ordLnPk, String craftSeq){
		SqlBuilder b = new SqlBuilder("CraftOrdDesignDAL.SetProcessJhFlag");
		b.Fill(new String[]{ processCode, flag, ordLnPk, craftSeq });
		return this.setData(b.toString());
	}


	/**
	 * 设置工序检验计划是否确认
	 * @param processCode
	 * @param flag
	 * @param CraftNo
	 * @return
	 */
	public CoreReturnObject SetProcessFlag(String processCode, String flag,String CraftNo){
		SqlBuilder b = new SqlBuilder("CraftOrdDesignDAL.SetProcessFlagNew");
		b.Fill(new String[]{ processCode, flag, CraftNo });
		return this.setData(b.toString());
	}

	/**
	 * 设置工序检验计划是否确认
	 * @param flag
	 * @return
	 */
	public CoreReturnObject SetProcessFlag(ArrayList paras, String flag){
		ArrayList<String> Sql = new ArrayList<String>();

		for (Object para:paras
			 ) {
			ArrayList arrayList = (ArrayList)para;

			SqlBuilder b = new SqlBuilder("CraftOrdDesignDAL.SetProcessFlag");
			b.Fill(new String[]{String.valueOf(arrayList.get(0)), flag, String.valueOf(arrayList.get(1)), String.valueOf(arrayList.get(2)) });
			Sql.add(b.toString());




			if("1".endsWith(flag)){
				ArrayList<String> JosonTotals = (ArrayList)( (ArrayList) para ).get(3);
				ArrayList<String> JosonDetails = (ArrayList)( (ArrayList) para ).get(4);
				Sql.addAll(GetSampleSql(JosonTotals, JosonDetails));
			}
			else {
				JSONObject json = JSONObject.fromObject(((ArrayList) para ).get(3).toString());
				CraftSamplingMEntity CraftSamplingMEntity = (CraftSamplingMEntity) JSONObject.toBean(
						json, CraftSamplingMEntity.class);

				String DeleteSql1 = EntityTool.fillSqlByEntity(CoreXmlSqlParsersFactory.getSql("CraftOrdDesignDAL.DeleteCraftSamplingTotal"), CraftSamplingMEntity);
				Sql.add(DeleteSql1);

				String DeleteSql2 = EntityTool.fillSqlByEntity(CoreXmlSqlParsersFactory.getSql("CraftOrdDesignDAL.DeleteCraftSamplingDetail"), CraftSamplingMEntity);
				Sql.add(DeleteSql2);

			}

		}


		return this.setData(Sql,null);
	}
	
	public CoreReturnObject SetProcessJhFlag(ArrayList paras, String flag){
		ArrayList<String> Sql = new ArrayList<String>();

		for (Object para:paras
			 ) {
			ArrayList arrayList = (ArrayList)para;

			SqlBuilder b = new SqlBuilder("CraftOrdDesignDAL.SetProcessJhFlag");
			b.Fill(new String[]{String.valueOf(arrayList.get(0)), flag, String.valueOf(arrayList.get(1)), String.valueOf(arrayList.get(2)) });
			Sql.add(b.toString());

		}


		return this.setData(Sql,null);
	}
	/**
     * 获取工艺评审工序点类型
     * @param designKey
     * @param processCode
     * @return
     */
    public CoreReturnObject getCraftStationType(String designKey, String processCode){
    	SqlBuilder b = new SqlBuilder("CraftOrdDesignDAL.getCraftStationType");
    	b.Fill(new String[]{ designKey, processCode });
    	
    	return this.getData(b.toString());
    }
    
    /**
     * 获取取样要求
     * @param designKey
     * @param processCode
     * @return
     */
    public CoreReturnObject getCraftPicSmp(String designKey, String processCode){
    	SqlBuilder b = new SqlBuilder("CraftOrdDesignDAL.getCraftPicSmp");
    	b.Fill(new String[]{ designKey, processCode });
    	
    	return this.getData(b.toString());
    }


    
    /**
     * 获取取样要求分组
     * @param designKey
     * @param processCode
     * @return
     */
    public CoreReturnObject getCraftPicSmpGroup(String designKey, String processCode){
    	SqlBuilder b = new SqlBuilder("CraftOrdDesignDAL.getCraftPicSmpGroup");
    	b.Fill(new String[]{ designKey, processCode });
    	
    	return this.getData(b.toString());
    }


	/**
	 * 获取取样要求
	 *
	 * @param designKey
	 * @param processCode
	 * @return
	 */
	public CoreReturnObject getCraftPicSmpGroup(String designKey,String Msc, String processCode,String Cic,String CraftNo){
		SqlBuilder b = new SqlBuilder("CraftOrdDesignDAL.getCraftPicSmpGroupNew");
		b.Fill(new String[]{ designKey,Msc, processCode,Cic,CraftNo});

		return this.getData(b.toString());
	}

	/**
	 * 获取取样
	 *
	 * @param
	 * @return
	 */
	public CoreReturnObject getCraftSamplingTotal(String LnPk, String CraftSeq,String Process){
		SqlBuilder b = new SqlBuilder("CraftOrdDesignDAL.getCraftSamplingTotal");
		b.Fill(new String[]{ LnPk,CraftSeq,Process});

		return this.getData(b.toString());
	}

	public CoreReturnObject getSampleDetail(String designKey, String processCode){
		return this.getDataBySqlID("CraftOrdDesignDAL.getSampleDetail",new Object[]{designKey,processCode});
	}


	/**
	 * 删除
	 *
	 * @param
	 * @return
	 */
	public CoreReturnObject DeleteCraftSampling(String Joson){
		ArrayList<String> listsql = new ArrayList<String>();
		JSONObject json = JSONObject.fromObject(Joson);
		CraftSamplingMEntity CraftSamplingMEntity = (CraftSamplingMEntity) JSONObject.toBean(
				json, CraftSamplingMEntity.class);

		String DeleteSql1 = EntityTool.fillSqlByEntity(CoreXmlSqlParsersFactory.getSql("CraftOrdDesignDAL.DeleteCraftSamplingTotal"), CraftSamplingMEntity);
		listsql.add(DeleteSql1);

		String DeleteSql2 = EntityTool.fillSqlByEntity(CoreXmlSqlParsersFactory.getSql("CraftOrdDesignDAL.DeleteCraftSamplingDetail"), CraftSamplingMEntity);
		listsql.add(DeleteSql2);

		return this.setData(listsql,null);
	}

	/**
	 * 获取取样汇总
	 *
	 * @param
	 * @return
	 */
	public CoreReturnObject getCraftSamplingDetaill(String LnPk, String CraftSeq,String Process){
		SqlBuilder b = new SqlBuilder("CraftOrdDesignDAL.getCraftSamplingDetail");
		b.Fill(new String[]{ LnPk,CraftSeq,Process});

		return this.getData(b.toString());
	}



	/**
	 *  保存检验取样
	 *
	 * @param JosonTotals
	 * @param JosonDetails
	 * @return
	 */
	public CoreReturnObject SaveCraftSampling(  ArrayList<String> JosonTotals,  ArrayList<String> JosonDetails){

		ArrayList<String> listsql = GetSampleSql(JosonTotals, JosonDetails);

		return this.setData(listsql,null);
	}

	private ArrayList<String> GetSampleSql(ArrayList<String> JosonTotals, ArrayList<String> JosonDetails) {
		ArrayList<String> listsql = new ArrayList<String>();

		CraftSamplingMEntity CraftSamplingMEntity = null;
		CraftSamplingDetailEntity CraftSamplingDetailEntity= null;

       if(JosonTotals==null || JosonDetails ==null) return new ArrayList<String>();
		for (int i = 0; i < JosonTotals.size(); i++) {
			String JosonTotal = JosonTotals.get(i);
			JSONObject json = JSONObject.fromObject(JosonTotal);
			CraftSamplingMEntity = (CraftSamplingMEntity) JSONObject.toBean(
					json, CraftSamplingMEntity.class);


			if(i==0){
				String DeleteSql1 =  CoreXmlSqlParsersFactory.getSql("CraftOrdDesignDAL.DeleteCraftSamplingTotal");
				DeleteSql1 = EntityTool.fillSqlByEntity(DeleteSql1, CraftSamplingMEntity);
				listsql.add(DeleteSql1);
			}
			String Sql1 = CoreXmlSqlParsersFactory.getSql("CraftOrdDesignDAL.InsertCraftSamplingTotal");
			Sql1 =EntityTool.fillSqlByEntity(Sql1, CraftSamplingMEntity);
			listsql.add(Sql1);
		}


		for (int i = 0; i < JosonDetails.size(); i++) {
			String JosonDetail = JosonDetails.get(i);
			JSONObject json = JSONObject.fromObject(JosonDetail);
			CraftSamplingDetailEntity = (CraftSamplingDetailEntity) JSONObject.toBean(
					json, CraftSamplingDetailEntity.class);

			if(i==0){
				String DeleteSql2 =  CoreXmlSqlParsersFactory.getSql("CraftOrdDesignDAL.DeleteCraftSamplingDetail");
				DeleteSql2 =EntityTool.fillSqlByEntity(DeleteSql2, CraftSamplingDetailEntity);
				listsql.add(DeleteSql2);
			}
			String Sql2 = CoreXmlSqlParsersFactory.getSql("CraftOrdDesignDAL.InsertCraftSamplingDetail");
			Sql2=EntityTool.fillSqlByEntity(Sql2, CraftSamplingDetailEntity);
			listsql.add(Sql2);
		}
		return listsql;
	}
}
