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

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

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.CraftOrdDesignStdWicEntity;
import com.steering.pss.sale.order.model.DesignStdWicEntity;

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

public class CraftOrdDesignStdWicDAL extends DBCommon{
	
	/**
	 * 查询质量设计水压
	 * @param strDesignStdWicJSON
	 * @return
	 */
	public CoreReturnObject query(String strDesignStdWicJSON){
		DesignStdWicEntity designStdWicEntity = EntityTool.getEntityByJSONStr(strDesignStdWicJSON, DesignStdWicEntity.class);
		String sql = CoreXmlSqlParsersFactory.getSql("CraftOrdDesignStdWicDAL.query");
		sql = EntityTool.fillSqlByEntity(sql, designStdWicEntity);
		return this.getData(sql);
	}
	
	/**
	 * 查询质量设计水压
	 * @param strDesignStdWicJSON
	 * @return
	 */
	public CoreReturnObject queryByDesignKeyWic(String[] parms){
		SqlBuilder b = new SqlBuilder("CraftOrdDesignStdWicDAL.queryDesignKeyWic");
		b.Fill(parms);
		return this.getData(b.toString());
	}
	
	/**
	 * 通过DESIGN_KEY查询质量设计水压
	 * @param strDesignStdWicJSON
	 * @return
	 */
	public CoreReturnObject queryWicByDesignKey(String strDesignStdWicJSON){
		DesignStdWicEntity designStdWicEntity = EntityTool.getEntityByJSONStr(strDesignStdWicJSON, DesignStdWicEntity.class);
		String sql = CoreXmlSqlParsersFactory.getSql("CraftOrdDesignStdWicDAL.queryWicByDesignKey");
		sql = EntityTool.fillSqlByEntity(sql, designStdWicEntity);
		return this.getData(sql);
	}
	
	 /**
     * 保存
     */
    public CoreReturnObject Save(ArrayList<String> jsons){
    TransctionTool transction = new TransctionTool();
    try{
            Connection con = transction.getCon();
            GenericEntity maxPk = null;
            for(String json : jsons){
                CraftOrdDesignStdWicEntity craftOrdDesignStdWicEntity = EntityTool.getEntityByGson(json, CraftOrdDesignStdWicEntity.class);
                craftOrdDesignStdWicEntity.setGetMinvalue(craftOrdDesignStdWicEntity.getGetMinvalue().equals("True") ? "1" : "0");
                craftOrdDesignStdWicEntity.setGetMaxvalue(craftOrdDesignStdWicEntity.getGetMaxvalue().equals("True") ? "1" : "0");
                
                String sqlSave = "";
                if(craftOrdDesignStdWicEntity.getCreateTime().equals("")){
                    sqlSave = CoreXmlSqlParsersFactory.getSql("CraftOrdDesignStdWicDAL.Insert");
                    String sqlMaxPk = CoreXmlSqlParsersFactory.getSql("CraftOrdDesignStdWicDAL.GetMaxIndex");
                    sqlMaxPk = EntityTool.fillSqlByEntity(sqlMaxPk, craftOrdDesignStdWicEntity);
                    maxPk = EntityTool.getEntityBySqlNoClsCon(sqlMaxPk, con, GenericEntity.class);
                    craftOrdDesignStdWicEntity.setIndexSeq(maxPk.getMaxValue());
                }else{
                    sqlSave = CoreXmlSqlParsersFactory.getSql("CraftOrdDesignStdWicDAL.Update");
                }
                sqlSave = EntityTool.fillSqlByEntity(sqlSave, craftOrdDesignStdWicEntity);
                if(!transction.ExcuteTransction(sqlSave)){
                    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){
    TransctionTool transction = new TransctionTool();
    try{
            for(String json : jsons){
                CraftOrdDesignStdWicEntity craftOrdDesignStdWicEntity = EntityTool.getEntityByGson(json, CraftOrdDesignStdWicEntity.class);
                String sqlUpdate = CoreXmlSqlParsersFactory.getSql("CraftOrdDesignStdWicDAL.Delete");
                sqlUpdate = EntityTool.fillSqlByEntity(sqlUpdate, craftOrdDesignStdWicEntity);
                if(!transction.ExcuteTransction(sqlUpdate)){
                    return transction.rollBack();
                }
            }
        }catch(Exception ex){
            CoreReturnObject cro = transction.rollBack();
            cro.setV_errMsg(ex.getMessage());
            return cro;
        }
        return transction.commit();
    }
    
    /**
     * 获取工艺评审制程
     * @param designKey
     * @return
     */
    public CoreReturnObject getCraftMscPline(String designKey, String processCode){
    	SqlBuilder b = new SqlBuilder("CraftOrdDesignStdWicDAL.getCraftMscPline");
    	b.Fill(new String[]{ designKey, processCode });
    	
    	return this.getData(b.toString());
    }
}
