package com.steering.pss.sale.order;

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.SqlBuilder;
import com.steering.comm.tools.TransctionTool;
import com.steering.pss.sale.order.model.CraftOrdFdWicEntity;

public class CraftOrdFdWic extends DBCommon{
	
	/**
	 * 查询水压首日检
	 * @return
	 */
	public CoreReturnObject query(String ordLnPk, String mscPline, 
			String processCode, String wic){
		SqlBuilder b = new SqlBuilder("CraftOrdFdWic.query");
		b.Fill(new String[]{ ordLnPk, mscPline, processCode, wic });
		
		return this.getData(b.toString());
	}
	
	/**
	 * 查询首日检汇总
	 * @param ordLnPk
	 * @return
	 */
	public CoreReturnObject queryM(String ordLnPk){
		SqlBuilder b = new SqlBuilder("CraftOrdFdWic.queryM");
		b.Fill(new String[]{ ordLnPk });
		
		return this.getData(b.toString());
	}
	
	/**
	 * 获取制程工序
	 * @param designKey
	 * @return
	 */
	public CoreReturnObject getMscPlineProcess(String designKey){
		SqlBuilder b= new SqlBuilder("CraftOrdFdWic.GetMscPlineProcess");
		b.Fill(new String[]{ designKey });
		
		return this.getData(b.toString());
	}
	
	/**
     * 保存
     */
    public CoreReturnObject save(ArrayList<String> jsons, String ordLnPk, String mscPline,
    		String processCode, String memo){
	    TransctionTool transction = new TransctionTool();
	    try{
    		for(String json : jsons){
            	CraftOrdFdWicEntity fdWic = EntityTool.getEntityByGson(json, CraftOrdFdWicEntity.class);
            	fdWic.setGetMaxvalue(fdWic.getGetMaxvalue().equals("True") ? "1" : "0");
            	fdWic.setGetMinvalue(fdWic.getGetMinvalue().equals("True") ? "1" : "0");
                String sqlSave = "";
                if(fdWic.getCreateTime().equals("")){
                    sqlSave = CoreXmlSqlParsersFactory.getSql("CraftOrdFdWic.Insert");
                }else{
                    sqlSave = CoreXmlSqlParsersFactory.getSql("CraftOrdFdWic.Update");
                }
                sqlSave = EntityTool.fillSqlByEntity(sqlSave, fdWic);
                if(!transction.ExcuteTransction(sqlSave)){
                    return transction.rollBack();
                }
            }
    		
    		SqlBuilder b;
    		if(!processCode.equals("")){
    			b = new SqlBuilder("CraftOrdFdWic.updateFdWicMemo");
    			b.Fill(new String[]{ ordLnPk, mscPline, processCode, memo });
    			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();
    }
    
    /**
     * 删除
     */
    public CoreReturnObject delete(ArrayList<String> jsons, String ordLnPk,
    		String mscPline, String processCode){
	    TransctionTool transction = new TransctionTool();
	    try{
	    	if(!mscPline.equals("")){
	    		SqlBuilder b = new SqlBuilder("CraftOrdFdWic.DeleteAll");
	    		b.Fill(new String[]{ ordLnPk, mscPline, processCode });
	    		if(!transction.ExcuteTransction(b.toString())){
                    return transction.rollBack();
                }
	    	}else{
	    		for(String json : jsons){
	            	CraftOrdFdWicEntity fdWic = EntityTool.getEntityByGson(json, CraftOrdFdWicEntity.class);
	                String sqlUpdate = CoreXmlSqlParsersFactory.getSql("CraftOrdFdWic.Delete");
	                sqlUpdate = EntityTool.fillSqlByEntity(sqlUpdate, fdWic);
	                if(!transction.ExcuteTransction(sqlUpdate)){
	                    return transction.rollBack();
	                }
	                
	                //更新ordSeq
	                String setOrdSeq = CoreXmlSqlParsersFactory.getSql("CraftOrdFdWic.setOrdSeq");
	                setOrdSeq = EntityTool.fillSqlByEntity(setOrdSeq, fdWic);
	                if(!transction.ExcuteTransction(setOrdSeq)){
	                    return transction.rollBack();
	                }
	                
	                //更新indexSeq
	                String setIndexSeq = CoreXmlSqlParsersFactory.getSql("CraftOrdFdWic.setIndexSeq");
	                setIndexSeq = EntityTool.fillSqlByEntity(setIndexSeq, fdWic);
	                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 wic){
    	Connection con = this.getDao(CoreConstantData.sysbeanstr).getConnection();
    	SqlBuilder b = new SqlBuilder("CraftOrdFdWic.getCraftWic");
    	b.Fill(new String[]{ designKey, mscPline, processCode, wic });
    	ArrayList<CraftOrdFdWicEntity> stdPics = EntityTool.getEntitysBySqlNoClsCon(
    			b.toString(), con, CraftOrdFdWicEntity.class);
    	ArrayList<String> sqllist = new ArrayList<String>();
    	for(CraftOrdFdWicEntity stdPic : stdPics){
    		String insert = CoreXmlSqlParsersFactory.getSql("CraftOrdFdWic.Insert");
    		stdPic.setOrdLnPk(ordLnPk);
    		insert = EntityTool.fillSqlByEntity(insert, stdPic);
    		sqllist.add(insert);
    	}
    	
    	return this.getDao(CoreConstantData.sysbeanstr).ExcuteTransction(sqllist);
    }
}
