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

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

public class CraftOrdDesignStdGicDAL extends DBCommon{
	
	/**
	 * 查询质量设计加工
	 * @param strDesignStdGicJSON
	 * @return
	 */
	public CoreReturnObject query(String strDesignStdGicJSON){
		DesignStdGicEntity designStdGicEntity = EntityTool.getEntityByJSONStr(strDesignStdGicJSON, DesignStdGicEntity.class);
		String sql = CoreXmlSqlParsersFactory.getSql("CraftOrdDesignStdGicDAL.query");
		sql = EntityTool.fillSqlByEntity(sql, designStdGicEntity);
		return this.getData(sql);
	}
	
	/**
	 * 查询质量设计加工
	 * @param strDesignStdGicJSON
	 * @return
	 */
	public CoreReturnObject queryByDesignKeyGic(String[] parms){
		SqlBuilder b = new SqlBuilder("CraftOrdDesignStdGicDAL.queryDesignKeyByGic");
		b.Fill(parms);
		return this.getData(b.toString());
	}
	
	/**
	 * 通过DESIGN_KEY查询质量设计加工
	 * @param strDesignStdGicJSON
	 * @return
	 */
	public CoreReturnObject queryGicByDesignKey(String strDesignStdGicJSON){
		DesignStdGicEntity designStdGicEntity = EntityTool.getEntityByJSONStr(strDesignStdGicJSON, DesignStdGicEntity.class);
		String sql = CoreXmlSqlParsersFactory.getSql("CraftOrdDesignStdGicDAL.queryGicByDesignKey");
		sql = EntityTool.fillSqlByEntity(sql, designStdGicEntity);
		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){
                CraftOrdDesignStdGicEntity craftOrdDesignStdGicEntity = EntityTool.getEntityByGson(json, CraftOrdDesignStdGicEntity.class);
                String sqlSave = "";
                if(craftOrdDesignStdGicEntity.getCreateTime().equals("")){
                    sqlSave = CoreXmlSqlParsersFactory.getSql("CraftOrdDesignStdGicDAL.Insert");
                    String sqlMaxPk = CoreXmlSqlParsersFactory.getSql("CraftOrdDesignStdGicDAL.GetMaxIndex");
                    sqlMaxPk = EntityTool.fillSqlByEntity(sqlMaxPk, craftOrdDesignStdGicEntity);
                    maxPk = EntityTool.getEntityBySqlNoClsCon(sqlMaxPk, con, GenericEntity.class);
                    craftOrdDesignStdGicEntity.setXh(maxPk.getMaxValue());
                }else{
                    sqlSave = CoreXmlSqlParsersFactory.getSql("CraftOrdDesignStdGicDAL.Update");
                }
                sqlSave = EntityTool.fillSqlByEntity(sqlSave, craftOrdDesignStdGicEntity);
                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){
                CraftOrdDesignStdGicEntity craftOrdDesignStdGicEntity = EntityTool.getEntityByGson(json, CraftOrdDesignStdGicEntity.class);
                String sqlUpdate = CoreXmlSqlParsersFactory.getSql("CraftOrdDesignStdGicDAL.Delete");
                sqlUpdate = EntityTool.fillSqlByEntity(sqlUpdate, craftOrdDesignStdGicEntity);
                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("CraftOrdDesignStdGicDAL.getCraftMscPline");
    	b.Fill(new String[]{ designKey, processCode });
    	
    	return this.getData(b.toString());
    }
}
