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.CraftOrdDesignStdCicEntity;
import com.steering.pss.sale.order.model.DesignStdCicEntity;
import CoreFS.SA06.CoreReturnObject;
import CoreFS.SA08.CoreXmlSqlParsersFactory;

public class CraftOrdDesignStdCicDAL extends DBCommon{
	
	/**
	 * 查询质量设计成分
	 * @param strDesignStdCicJSON
	 * @return
	 */
	public CoreReturnObject query(String strDesignStdCicJSON){
		DesignStdCicEntity designStdCicEntity = EntityTool.getEntityByJSONStr(strDesignStdCicJSON, DesignStdCicEntity.class);
		String sql = CoreXmlSqlParsersFactory.getSql("CraftOrdDesignStdCicDAL.query");
		sql = EntityTool.fillSqlByEntity(sql, designStdCicEntity);
		return this.getData(sql);
	}
	
	/**
	 * 查询质量设计成分
	 * @param strDesignStdCicJSON
	 * @return
	 */
	public CoreReturnObject queryByDesignKeyCic(String[] parms){
		SqlBuilder b = new SqlBuilder("CraftOrdDesignStdCicDAL.queryByDesignKeyCic");
		b.Fill(parms);
		return this.getData(b.toString());
	}
	
	/**
	 * 通过DESIGN_KEY查询质量设计成分
	 * @param strDesignStdCicJSON
	 * @return
	 */
	public CoreReturnObject queryCicByDesignKey(String strDesignStdCicJSON){
		DesignStdCicEntity designStdCicEntity = EntityTool.getEntityByJSONStr(strDesignStdCicJSON, DesignStdCicEntity.class);
		String sql = CoreXmlSqlParsersFactory.getSql("CraftOrdDesignStdCicDAL.queryCicByDesignKey");
		sql = EntityTool.fillSqlByEntity(sql, designStdCicEntity);
		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){
                CraftOrdDesignStdCicEntity craftOrdDesignStdCicEntity = EntityTool.getEntityByGson(json, CraftOrdDesignStdCicEntity.class);
                craftOrdDesignStdCicEntity.setIscheck(craftOrdDesignStdCicEntity.getIscheck().equals("True") ? "1" : "0");
                craftOrdDesignStdCicEntity.setIsjudge(craftOrdDesignStdCicEntity.getIsjudge().equals("True") ? "1" : "0");
                craftOrdDesignStdCicEntity.setIsquote(craftOrdDesignStdCicEntity.getIsquote().equals("True") ? "1" : "0");
                craftOrdDesignStdCicEntity.setComplexType(craftOrdDesignStdCicEntity.getComplexType().equals("True") ? "1" : "0");
                
                String sqlSave = "";
                if(craftOrdDesignStdCicEntity.getCreateTime().equals("")){
                    sqlSave = CoreXmlSqlParsersFactory.getSql("CraftOrdDesignStdCicDAL.Insert");
                    String sqlMaxPk = CoreXmlSqlParsersFactory.getSql("CraftOrdDesignStdCicDAL.GetMaxIndex");
                    sqlMaxPk = EntityTool.fillSqlByEntity(sqlMaxPk, craftOrdDesignStdCicEntity);
                    maxPk = EntityTool.getEntityBySqlNoClsCon(sqlMaxPk, con, GenericEntity.class);
                    craftOrdDesignStdCicEntity.setIndexSeq(maxPk.getMaxValue());
                }else{
                    sqlSave = CoreXmlSqlParsersFactory.getSql("CraftOrdDesignStdCicDAL.Update");
                }
                sqlSave = EntityTool.fillSqlByEntity(sqlSave, craftOrdDesignStdCicEntity);
                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){
                CraftOrdDesignStdCicEntity craftOrdDesignStdCicEntity = EntityTool.getEntityByGson(json, CraftOrdDesignStdCicEntity.class);
                String sqlUpdate = CoreXmlSqlParsersFactory.getSql("CraftOrdDesignStdCicDAL.Delete");
                sqlUpdate = EntityTool.fillSqlByEntity(sqlUpdate, craftOrdDesignStdCicEntity);
                if(!transction.ExcuteTransction(sqlUpdate)){
                    return transction.rollBack();
                }
            }
        }catch(Exception ex){
            CoreReturnObject cro = transction.rollBack();
            cro.setV_errMsg(ex.getMessage());
            return cro;
        }
        return transction.commit();
    }
    
    public CoreReturnObject getCraftMscPline(String designKey, String processCode){
    	SqlBuilder b = new SqlBuilder("CraftOrdDesignStdCicDAL.getCraftMscPline");
    	b.Fill(new String[]{ designKey, processCode });
    	
    	return this.getData(b.toString());
    }
}
