package com.steering.pss.sale.order;

import java.sql.Connection;
import java.sql.SQLException;
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.SqlBuilder;
import com.steering.comm.tools.TransctionTool;
import com.steering.pss.qcm.model.GenericEntity;
import com.steering.pss.sale.order.model.CraftOrdFdPicEntity;
import com.steering.pss.sale.order.model.CraftOrdFdPicSmpEntity;

public class CraftOrdFdPic extends DBCommon {
	
	/**
	 * 查询首日检理化
	 * @param strDesignStdPicJSON
	 * @return
	 */
	public CoreReturnObject query(String ordLnPk, String mscPline, String processCode, String pic){
		SqlBuilder b = new SqlBuilder("CraftOrdFdPic.query");
		b.Fill(new String[]{ ordLnPk, mscPline, processCode, pic });
		
		return this.getData(b.toString());
	}
	
	/**
	 * 查询首日检汇总
	 * @param ordLnPk
	 * @return
	 */
	public CoreReturnObject queryM(String ordLnPk){
		SqlBuilder b = new SqlBuilder("CraftOrdFdPic.queryM");
		b.Fill(new String[]{ ordLnPk });
		
		return this.getData(b.toString());
	}
	
	/**
	 * 获取制程工序
	 * @param designKey
	 * @return
	 */
	public CoreReturnObject getMscPlineProcess(String designKey){
		SqlBuilder b= new SqlBuilder("CraftOrdFdPic.GetMscPlineProcess");
		b.Fill(new String[]{ designKey });
		
		return this.getData(b.toString());
	}
	
	/**
     * 保存
     */
    public CoreReturnObject save(ArrayList<String> jsons, String ordLnPk, String mscPline,
    		String processCode, String pic, String chkMscPline, String chkProcessCode,
    		String chkMemo){
	    TransctionTool transction = new TransctionTool();
	    try{
            for(String json : jsons){
            	CraftOrdFdPicEntity fdPic = EntityTool.getEntityByGson(json, CraftOrdFdPicEntity.class);
            	String sqlExist = CoreXmlSqlParsersFactory.getSql("CraftOrdFdPic.exist");
    			sqlExist = EntityTool.fillSqlByEntity(sqlExist, fdPic);
    			CraftOrdFdPicEntity picExist = EntityTool.getEntityBySqlNoClsCon(
    					sqlExist, transction.getCon(), CraftOrdFdPicEntity.class);
    			if(picExist != null){
    				throw new Exception("只能维护一套理化首日检！");
    			}
            	fdPic.setIscheck(fdPic.getIscheck().equals("True") ? "1" : "0");
            	fdPic.setIsjudge(fdPic.getIsjudge().equals("True") ? "1" : "0");
            	fdPic.setIsquote(fdPic.getIsquote().equals("True") ? "1" : "0");
            	fdPic.setPhyType(fdPic.getPhyType().equals("True") ? "1" : "0");
            	fdPic.setBilletSample(fdPic.getBilletSample().equals("True") ? "1" : "0");
                
            	String deletePic = CoreXmlSqlParsersFactory.getSql("CraftOrdFdPic.deleteFdPic");
            	deletePic = EntityTool.fillSqlByEntity(deletePic, fdPic);
            	if(!transction.ExcuteTransction(deletePic)){
                    return transction.rollBack();
                }
                String sqlSave = CoreXmlSqlParsersFactory.getSql("CraftOrdFdPic.Insert");
                sqlSave = EntityTool.fillSqlByEntity(sqlSave, fdPic);
                if(!transction.ExcuteTransction(sqlSave)){
                    return transction.rollBack();
                }
            }
            
            SqlBuilder b = new SqlBuilder("CraftOrdFdPic.deleteNoExistsFdPicSmp");
            b.Fill(new String[]{ ordLnPk, mscPline, processCode });
            if(!transction.ExcuteTransction(b.toString())){
                return transction.rollBack();
            }
            b = new SqlBuilder("CraftOrdFdPic.getNoExistsFdPicGroupSample");
            b.Fill(new String[]{ ordLnPk, mscPline, processCode });
            ArrayList<CraftOrdFdPicSmpEntity> smps = EntityTool.getEntitysBySqlNoClsCon(
            		b.toString(), transction.getCon(), CraftOrdFdPicSmpEntity.class);
            for(CraftOrdFdPicSmpEntity smp : smps){
            	String insert = CoreXmlSqlParsersFactory.getSql("CraftOrdFdPic.insertSample");
            	String sampleKey = getSampleKey(smp.getPhyCodeMin(), smp.getItemCodeF(),
            			smp.getItemCodeC(), smp.getItemCodeW());
            	String sampleDesc = getSampleKey(smp.getPhyNameMin(), smp.getItemNameF(),
            			smp.getItemNameC(), smp.getItemNameW());
            	smp.setSampleKey(sampleKey);
            	smp.setSampleKeyDesc(sampleDesc);
            	insert = EntityTool.fillSqlByEntity(insert, smp);
            	if(!transction.ExcuteTransction(insert)){
                    return transction.rollBack();
                }
            }
            
    		if(!chkProcessCode.equals("")){
    			b = new SqlBuilder("CraftOrdFdPic.updateFdPicMemo");
    			b.Fill(new String[]{ ordLnPk, chkMscPline, chkProcessCode, chkMemo });
    			if(!transction.ExcuteTransction(b.toString())){
                    return transction.rollBack();
                }
    		}
        }catch(Exception ex){
            CoreReturnObject cro = transction.rollBack();
            cro.setV_errMsg(ex.getMessage());
            return cro;
        }
        return transction.commit();
    }
    
    private String getSampleKey(String phyCodeMin, String itemCodeF, 
    		String itemCodeS, String itemCodeW){
    	String sampleKey = phyCodeMin;
        if (sampleKey.equals(""))
        {
        	sampleKey = itemCodeF;
        }
        else
        {
            if (!itemCodeF.equals(""))
            	sampleKey = sampleKey + "_" + itemCodeF;
        }
        if (sampleKey.equals(""))
        {
        	sampleKey = itemCodeW;
        }
        else
        {
            if (!itemCodeW.equals(""))
            	sampleKey = sampleKey + "_" + itemCodeW;
        }
        if (sampleKey.equals(""))
        {
        	sampleKey = itemCodeS;
        }
        else
        {
            if (!itemCodeS.equals(""))
            	sampleKey = sampleKey + "_" + itemCodeS;
        }
        
        return sampleKey;
    }
    
    /**
     * 删除
     */
    public CoreReturnObject Delete(String ordLnPk, String mscPline, String processCode, ArrayList<String> jsons){
	    TransctionTool transction = new TransctionTool();
	    try{
	    	if(!mscPline.equals("")){
	    		SqlBuilder b = new SqlBuilder("CraftOrdFdPic.DeleteAll");
	    		b.Fill(new String[]{ ordLnPk, mscPline, processCode });
	    		if(!transction.ExcuteTransction(b.toString())){
                    return transction.rollBack();
                }
	    		b = new SqlBuilder("CraftOrdFdPic.deleteFdPicSmp");
	    		b.Fill(new String[]{ ordLnPk, mscPline, processCode });
	    		if(!transction.ExcuteTransction(b.toString())){
                    return transction.rollBack();
                }
	    	}else{
	    		for(String json : jsons){
	            	CraftOrdFdPicEntity fdPic = EntityTool.getEntityByGson(json, CraftOrdFdPicEntity.class);
	            	
	            	//删除取样
	            	SqlBuilder b = new SqlBuilder("CraftOrdFdPic.getPhyMinCount");
	                b.Fill(new String[]{ ordLnPk, fdPic.getOrdSeq(), fdPic.getMscPline(), fdPic.getProcessCode() });
	                GenericEntity count = EntityTool.getEntityBySqlNoClsCon(b.toString(),
	                		transction.getCon(), GenericEntity.class);
	                if(count.getCount().equals("1")){
	                	b = new SqlBuilder("CraftOrdFdPic.DeleteFdSample");
	                	b.Fill(new String[]{ ordLnPk, fdPic.getMscPline(), fdPic.getProcessCode(), fdPic.getOrdSeq() });
	                	if(!transction.ExcuteTransction(b.toString())){
		                    return transction.rollBack();
		                }
	                }
	            	
	                String sqlUpdate = CoreXmlSqlParsersFactory.getSql("CraftOrdFdPic.Delete");
	                sqlUpdate = EntityTool.fillSqlByEntity(sqlUpdate, fdPic);
	                if(!transction.ExcuteTransction(sqlUpdate)){
	                    return transction.rollBack();
	                }
	                
//	                //更新ordSeq
//	                String setOrdSeq = CoreXmlSqlParsersFactory.getSql("CraftOrdFdPic.setOrdSeq");
//	                setOrdSeq = EntityTool.fillSqlByEntity(setOrdSeq, fdPic);
//	                if(!transction.ExcuteTransction(setOrdSeq)){
//	                    return transction.rollBack();
//	                }
//	                
//	                //更新indexSeq
//	                String setIndexSeq = CoreXmlSqlParsersFactory.getSql("CraftOrdFdPic.setIndexSeq");
//	                setIndexSeq = EntityTool.fillSqlByEntity(setIndexSeq, fdPic);
//	                if(!transction.ExcuteTransction(setIndexSeq)){
//	                    return transction.rollBack();
//	                }
	            }
	    	}
        }catch(Exception ex){
            CoreReturnObject cro = transction.rollBack();
            cro.setV_errMsg(ex.getMessage());
            return cro;
        }
        return transction.commit();
    }
    
    /**
     * 复制内控标准
     * @param designKey
     * @param ordLnPk
     * @return
     */
    public CoreReturnObject copyNkStd(String designKey, String ordLnPk, String mscPline,
    		String processCode, String pic){
    	Connection con = null;
    	try{
    		con = this.getDao(CoreConstantData.sysbeanstr).getConnection();
    		SqlBuilder b = new SqlBuilder("CraftOrdFdPic.getCraftPic");
        	b.Fill(new String[]{ designKey, mscPline, processCode, pic });
        	ArrayList<CraftOrdFdPicEntity> stdPics = EntityTool.getEntitysBySqlNoClsCon(
        			b.toString(), con, CraftOrdFdPicEntity.class);
        	ArrayList<String> sqllist = new ArrayList<String>();
        	for(CraftOrdFdPicEntity stdPic : stdPics){
        		String sqlExist = CoreXmlSqlParsersFactory.getSql("CraftOrdFdPic.exist");
    			sqlExist = EntityTool.fillSqlByEntity(sqlExist, stdPic);
    			CraftOrdFdPicEntity picExist = EntityTool.getEntityBySqlNoClsCon(
    					sqlExist, con, CraftOrdFdPicEntity.class);
    			if(picExist != null){
    				throw new Exception("只能维护一套理化首日检！");
    			}
        		String insert = CoreXmlSqlParsersFactory.getSql("CraftOrdFdPic.Insert");
        		stdPic.setOrdLnPk(ordLnPk);
        		insert = EntityTool.fillSqlByEntity(insert, stdPic);
        		sqllist.add(insert);
        	}
        	
        	//新增取样
        	b = new SqlBuilder("CraftOrdFdPic.getCraftSample");
        	b.Fill(new String[]{designKey, mscPline, processCode, pic});
        	ArrayList<CraftOrdFdPicSmpEntity> smps = EntityTool.getEntitysBySqlNoClsCon(
        			b.toString(), con, CraftOrdFdPicSmpEntity.class);
        	for(CraftOrdFdPicSmpEntity smp : smps){
        		b = new SqlBuilder("CraftOrdFdPic.getSamplePhyMinCount");
        		b.Fill(new String[]{ ordLnPk, mscPline, processCode, smp.getPhyCodeMin(),
        				smp.getItemCodeF(), smp.getItemCodeC(), smp.getItemCodeW() });
        		GenericEntity smpCount = EntityTool.getEntityBySqlNoClsCon(
        				b.toString(), con, GenericEntity.class);
        		if(smpCount.getCount().equals("0")){
        			String insert = CoreXmlSqlParsersFactory.getSql("CraftOrdFdPic.insertSample");
            		smp.setOrdLnPk(ordLnPk);
            		insert = EntityTool.fillSqlByEntity(insert, smp);
            		sqllist.add(insert);
        		}
        	}
        	
        	return this.getDao(CoreConstantData.sysbeanstr).ExcuteTransction(sqllist);
    		
    	}catch(Exception ex){
    		try {
				con.rollback();
			} catch (SQLException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
    		
    		CoreReturnObject cro = new CoreReturnObject();
    		cro.setV_errMsg(ex.getMessage());
    		return cro;
    	}finally{
    		try {
				con.close();
			} catch (SQLException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
    	}
    }
    
    /**
     * 获取取样主信息
     * @param ordLnPk
     * @param mscPline
     * @param processCode
     * @return
     */
    public CoreReturnObject getPhyMinSample(String ordLnPk, String mscPline, String processCode){
    	SqlBuilder b = new SqlBuilder("CraftOrdFdPic.getPhyMinSample");
    	b.Fill(new String[]{ ordLnPk, mscPline, processCode });
    	
    	return this.getData(b.toString());
    }
    
    /**
     * 获取试样主信息
     * @param ordLnPk
     * @param mscPline
     * @param processCode
     * @return
     */
    public CoreReturnObject getPhyMinSpline(String ordLnPk, String mscPline, String processCode){
    	SqlBuilder b = new SqlBuilder("CraftOrdFdPic.getPhyMinSpline");
    	b.Fill(new String[]{ ordLnPk, mscPline, processCode });
    	
    	return this.getData(b.toString());
    }
    
    /**
     * 修改取样
     * @param ordLnPk
     * @param phyCodeMin
     * @param itemCodeF
     * @param itemCodeS
     * @param itemCodeW
     * @param mscPline
     * @param processCode
     * @param stdCodeTest
     * @param sic
     * @return
     */
    public CoreReturnObject setSample(String ordLnPk, String phyCodeMin, String itemCodeF, String itemCodeS,
    		String itemCodeW, String mscPline, String processCode, String stdNameTest,
    		String stdCodeTest, String sic, String common){
    	SqlBuilder b = new SqlBuilder("CraftOrdFdPic.getSampleBySic");
    	b.Fill(new String[]{ sic });
    	CraftOrdFdPicSmpEntity sample = EntityTool.getEntityBySql(b.toString(), 
    			this.getDao(CoreConstantData.sysbeanstr).getConnection(), CraftOrdFdPicSmpEntity.class);
    	String update = CoreXmlSqlParsersFactory.getSql("CraftOrdFdPic.setSample");
    	sample.setItemCodeC(itemCodeS);
    	sample.setItemCodeF(itemCodeF);
    	sample.setItemCodeW(itemCodeW);
    	sample.setMscPline(mscPline);
    	sample.setProcessCode(processCode);
    	sample.setStdNameTest(stdNameTest);
    	sample.setStdCodeTest(stdCodeTest);
    	sample.setOrdLnPk(ordLnPk);
    	if(common.equals("True")){
			sample.setCommon("1");
		}else{
			sample.setCommon("0");
		}
    	sample.setStdCode(sample.getStdCode());
    	sample.setStdCode(sample.getStdCode());
    	update = EntityTool.fillSqlByEntity(update, sample);
    	
    	return this.setData(update);
    }
    
    /**
     * 修改试样
     * @param ordLnPk
     * @param phyCodeMin
     * @param itemCodeF
     * @param itemCodeS
     * @param itemCodeW
     * @param mscPline
     * @param processCode
     * @param stdCodeTest
     * @param sic
     * @return
     */
    public CoreReturnObject setSpline(String ordLnPk, String phyCodeMin, String itemCodeF,
    		String itemCodeS, String mscPline, String processCode, String stdNameTest,
    		String stdCodeTest, String eic){
    	SqlBuilder b = new SqlBuilder("CraftOrdFdPic.getSplineByEic");
    	b.Fill(new String[]{ eic });
    	CraftOrdFdPicSmpEntity sample = EntityTool.getEntityBySql(b.toString(), 
    			this.getDao(CoreConstantData.sysbeanstr).getConnection(), CraftOrdFdPicSmpEntity.class);
    	String update = CoreXmlSqlParsersFactory.getSql("CraftOrdFdPic.setSpline");
    	sample.setItemCodeC(itemCodeS);
    	sample.setItemCodeF(itemCodeF);
    	sample.setMscPline(mscPline);
    	sample.setProcessCode(processCode);
    	sample.setStdNameTest(stdNameTest);
    	sample.setStdCodeTest(stdCodeTest);
    	sample.setOrdLnPk(ordLnPk);
    	update = EntityTool.fillSqlByEntity(update, sample);
    	
    	return this.setData(update);
    }
    
    /**
     * 获取取样SIC
     * @param ordLnPk
     * @param mscPline
     * @param processCode
     * @param phyCodeMin
     * @param itemCodeF
     * @param itemCodeC
     * @param itemCodeW
     * @return
     */
    public CoreReturnObject getFdSampleSic(String ordLnPk, String mscPline, String processCode,
    		String phyCodeMin, String itemCodeF, String itemCodeC, String itemCodeW){
    	SqlBuilder b = new SqlBuilder("CraftOrdFdPic.getFdSampleSic");
    	b.Fill(new String[]{ ordLnPk, mscPline, processCode, phyCodeMin, itemCodeF, itemCodeC,
    			itemCodeW });
    	
    	return this.getData(b.toString());
    }
    
    /**
     * 获取取样EIC
     * @param ordLnPk
     * @param mscPline
     * @param processCode
     * @param phyCodeMin
     * @param itemCodeF
     * @param itemCodeC
     * @param itemCodeW
     * @return
     */
    public CoreReturnObject getFdSampleEic(String ordLnPk, String mscPline, String processCode,
    		String phyCodeMin, String itemCodeF, String itemCodeC){
    	SqlBuilder b = new SqlBuilder("CraftOrdFdPic.getFdSplineEic");
    	b.Fill(new String[]{ ordLnPk, mscPline, processCode, phyCodeMin, itemCodeF, itemCodeC,
    		 });
    	
    	return this.getData(b.toString());
    }
}
