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

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

public class CraftOrdDesignStdDicDAL extends DBCommon{
	
	/**
	 * 查询质量设计探伤
	 * @param strDesignStdDicJSON
	 * @return
	 */
	public CoreReturnObject query(String strDesignStdDicJSON){
		DesignStdDicEntity designStdDicEntity = EntityTool.getEntityByJSONStr(strDesignStdDicJSON, DesignStdDicEntity.class);
		String sql = CoreXmlSqlParsersFactory.getSql("CraftOrdDesignStdDicDAL.query");
		sql = EntityTool.fillSqlByEntity(sql, designStdDicEntity);
		return this.getData(sql);
	}
	
	/**
	 * 查询质量设计探伤
	 * @param strDesignStdDicJSON
	 * @return
	 */
	public CoreReturnObject queryByDesignKeyDic(String[] parms){
		SqlBuilder b = new SqlBuilder("CraftOrdDesignStdDicDAL.queryByDesignKeyDic");
		b.Fill(parms);
		return this.getData(b.toString());
	}
	
	/**
	 * 通过DESIGN_KEY查询质量设计探伤
	 * @param strDesignStdDicJSON
	 * @return
	 */
	public CoreReturnObject queryDicByDesignKey(String strDesignStdDicJSON){
		DesignStdDicEntity designStdDicEntity = EntityTool.getEntityByJSONStr(strDesignStdDicJSON, DesignStdDicEntity.class);
		String sql = CoreXmlSqlParsersFactory.getSql("CraftOrdDesignStdDicDAL.queryDicByDesignKey");
		sql = EntityTool.fillSqlByEntity(sql, designStdDicEntity);
		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){
                CraftOrdDesignStdDicEntity craftOrdDesignStdDicEntity = EntityTool.getEntityByGson(json, CraftOrdDesignStdDicEntity.class);
                String sqlSave = "";
                if(craftOrdDesignStdDicEntity.getCreateTime().equals("")){
                    sqlSave = CoreXmlSqlParsersFactory.getSql("CraftOrdDesignStdDicDAL.Insert");
                    String sqlMaxPk = CoreXmlSqlParsersFactory.getSql("CraftOrdDesignStdDicDAL.GetMaxIndex");
                    sqlMaxPk = EntityTool.fillSqlByEntity(sqlMaxPk, craftOrdDesignStdDicEntity);
                    maxPk = EntityTool.getEntityBySqlNoClsCon(sqlMaxPk, con, GenericEntity.class);
                    craftOrdDesignStdDicEntity.setXh(maxPk.getMaxValue());
                }else{
                    sqlSave = CoreXmlSqlParsersFactory.getSql("CraftOrdDesignStdDicDAL.Update");
                }
                sqlSave = EntityTool.fillSqlByEntity(sqlSave, craftOrdDesignStdDicEntity);
                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){
                CraftOrdDesignStdDicEntity craftOrdDesignStdDicEntity = EntityTool.getEntityByGson(json, CraftOrdDesignStdDicEntity.class);
                String sqlUpdate = CoreXmlSqlParsersFactory.getSql("CraftOrdDesignStdDicDAL.Delete");
                sqlUpdate = EntityTool.fillSqlByEntity(sqlUpdate, craftOrdDesignStdDicEntity);
                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("CraftOrdDesignStdDicDAL.getCraftMscPline");
    	b.Fill(new String[]{ designKey, processCode });
    	
    	return this.getData(b.toString());
    }
}
