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.sale.order.model.CraftOrdFdCicEntity;

public class CraftOrdFdCic extends DBCommon{
	
	/**
	 * 查询水压首日检
	 * @param strDesignStdPicJSON
	 * @return
	 */
	public CoreReturnObject query(String ordLnPk, String mscPline, String processCode, String wic){
		SqlBuilder b = new SqlBuilder("CraftOrdFdCic.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("CraftOrdFdCic.queryM");
		b.Fill(new String[]{ ordLnPk });
		
		return this.getData(b.toString());
	}
	
	/**
	 * 获取制程工序
	 * @param designKey
	 * @return
	 */
	public CoreReturnObject getMscPlineProcess(String designKey){
		SqlBuilder b= new SqlBuilder("CraftOrdFdCic.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, String productCntFd,
    		String rateFd, String common){
	    TransctionTool transction = new TransctionTool();
	    try{
    		for(String json : jsons){
    			CraftOrdFdCicEntity fdCic = EntityTool.getEntityByGson(json, CraftOrdFdCicEntity.class);
    			String sqlExist = CoreXmlSqlParsersFactory.getSql("CraftOrdFdCic.exist");
    			sqlExist = EntityTool.fillSqlByEntity(sqlExist, fdCic);
    			CraftOrdFdCicEntity cicExist = EntityTool.getEntityBySqlNoClsCon(
    					sqlExist, transction.getCon(), CraftOrdFdCicEntity.class);
    			if(cicExist != null){
    				throw new Exception("只能维护一套成分首日检！");
    			}
            	fdCic.setIsjudge(fdCic.getIsjudge().equals("True") ? "1" : "0");
            	fdCic.setIscheck(fdCic.getIscheck().equals("True") ? "1" : "0");
            	fdCic.setIsquote(fdCic.getIsquote().equals("True") ? "1" : "0");
            	fdCic.setComplexType(fdCic.getComplexType().equals("True") ? "1" : "0");
                String sqlSave = "";
                if(fdCic.getCreateTime().equals("")){
                    sqlSave = CoreXmlSqlParsersFactory.getSql("CraftOrdFdCic.Insert");
                }else{
                    sqlSave = CoreXmlSqlParsersFactory.getSql("CraftOrdFdCic.Update");
                }
                sqlSave = EntityTool.fillSqlByEntity(sqlSave, fdCic);
                if(!transction.ExcuteTransction(sqlSave)){
                    return transction.rollBack();
                }
            }
    		
    		SqlBuilder b;
    		if(!processCode.equals("")){
    			b = new SqlBuilder("CraftOrdFdCic.updateFdCicMemo");
    			b.Fill(new String[]{ ordLnPk, mscPline, processCode, memo });
    			if(!transction.ExcuteTransction(b.toString())){
                    return transction.rollBack();
                }
    		}
    		
    		b = new SqlBuilder("CraftOrdFdCic.deleteFdCicCount");
    		b.Fill(new String[]{ ordLnPk });
    		if(!transction.ExcuteTransction(b.toString())){
                return transction.rollBack();
            }
    		b = new SqlBuilder("CraftOrdFdCic.insertFdCicCount");
    		b.Fill(new String[]{ ordLnPk, productCntFd, rateFd, common });
    		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("CraftOrdFdCic.DeleteAll");
	    		b.Fill(new String[]{ ordLnPk, mscPline, processCode });
	    		if(!transction.ExcuteTransction(b.toString())){
                    return transction.rollBack();
                }
	    	}else{
	    		for(String json : jsons){
	    			CraftOrdFdCicEntity fdCic = EntityTool.getEntityByGson(json, CraftOrdFdCicEntity.class);
	                String sqlUpdate = CoreXmlSqlParsersFactory.getSql("CraftOrdFdCic.Delete");
	                sqlUpdate = EntityTool.fillSqlByEntity(sqlUpdate, fdCic);
	                if(!transction.ExcuteTransction(sqlUpdate)){
	                    return transction.rollBack();
	                }
	                
	                //更新ordSeq
	                String setOrdSeq = CoreXmlSqlParsersFactory.getSql("CraftOrdFdCic.setOrdSeq");
	                setOrdSeq = EntityTool.fillSqlByEntity(setOrdSeq, fdCic);
	                if(!transction.ExcuteTransction(setOrdSeq)){
	                    return transction.rollBack();
	                }
	                
	                //更新indexSeq
	                String setIndexSeq = CoreXmlSqlParsersFactory.getSql("CraftOrdFdCic.setIndexSeq");
	                setIndexSeq = EntityTool.fillSqlByEntity(setIndexSeq, fdCic);
	                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 = null;
    	try{
    		con = this.getDao(CoreConstantData.sysbeanstr).getConnection();
        	SqlBuilder b = new SqlBuilder("CraftOrdFdCic.getCraftCic");
        	b.Fill(new String[]{ designKey, mscPline, processCode, wic });
        	ArrayList<CraftOrdFdCicEntity> stdCics = EntityTool.getEntitysBySqlNoClsCon(
        			b.toString(), con, CraftOrdFdCicEntity.class);
        	ArrayList<String> sqllist = new ArrayList<String>();
        	for(CraftOrdFdCicEntity stdCic : stdCics){
        		String sqlExist = CoreXmlSqlParsersFactory.getSql("CraftOrdFdCic.exist");
    			sqlExist = EntityTool.fillSqlByEntity(sqlExist, stdCic);
    			CraftOrdFdCicEntity cicExist = EntityTool.getEntityBySqlNoClsCon(
    					sqlExist, con, CraftOrdFdCicEntity.class);
    			if(cicExist != null){
    				throw new Exception("只能维护一套成分首日检！");
    			}
        		String insert = CoreXmlSqlParsersFactory.getSql("CraftOrdFdCic.Insert");
        		stdCic.setOrdLnPk(ordLnPk);
        		insert = EntityTool.fillSqlByEntity(insert, stdCic);
        		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();
			}
    	}
    }
    
    public CoreReturnObject getFdCicCount(String ordLnPk){
    	SqlBuilder b = new SqlBuilder("CraftOrdFdCic.getFdCicCount");
    	b.Fill(new String[]{ ordLnPk });
    	
    	return this.getData(b.toString());
    }
}
