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

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

public class CraftOrdDesignStdSicDAL extends DBCommon{
	
	/**
	 * 查询质量设计公差
	 * @param strDesignStdSicJSON
	 * @return
	 */
	public CoreReturnObject query(String strDesignStdSicJSON){
		DesignStdSicEntity designStdSicEntity = EntityTool.getEntityByJSONStr(strDesignStdSicJSON, DesignStdSicEntity.class);
		String sql = CoreXmlSqlParsersFactory.getSql("CraftOrdDesignStdSicDAL.query");
		sql = EntityTool.fillSqlByEntity(sql, designStdSicEntity);
		return this.getData(sql);
	}
	
	/**
	 * 查询质量设计公差
	 * @param strDesignStdSicJSON
	 * @return
	 */
	public CoreReturnObject queryByDesignKeySic(String[] parms){
		SqlBuilder b = new SqlBuilder("CraftOrdDesignStdSicDAL.queryByDesignKeySic");
		b.Fill(parms);
		return this.getData(b.toString());
	}
	
	/**
	 * 通过DESIGN_KEY查询质量设计公差
	 * @param strDesignStdSicJSON
	 * @return
	 */
	public CoreReturnObject querySicByDesignKey(String strDesignStdSicJSON){
		DesignStdSicEntity designStdSicEntity = EntityTool.getEntityByJSONStr(strDesignStdSicJSON, DesignStdSicEntity.class);
		String sql = CoreXmlSqlParsersFactory.getSql("CraftOrdDesignStdSicDAL.querySicByDesignKey");
		sql = EntityTool.fillSqlByEntity(sql, designStdSicEntity);
		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){
                CraftOrdDesignStdSicEntity craftOrdDesignStdSicEntity = EntityTool.getEntityByGson(json, CraftOrdDesignStdSicEntity.class);
                craftOrdDesignStdSicEntity.setIscheck(craftOrdDesignStdSicEntity.getIscheck().equals("True") ? "1" : "0");
                craftOrdDesignStdSicEntity.setStdMinFormulaV(craftOrdDesignStdSicEntity.getStdMinFormulaV().equals("True") ? "1" : "0");
                craftOrdDesignStdSicEntity.setStdMaxFormulaV(craftOrdDesignStdSicEntity.getStdMaxFormulaV().equals("True") ? "1" : "0");
                
                String sqlSave = "";
                if(craftOrdDesignStdSicEntity.getCreateTime().equals("")){
                    sqlSave = CoreXmlSqlParsersFactory.getSql("CraftOrdDesignStdSicDAL.Insert");
                    String sqlMaxPk = CoreXmlSqlParsersFactory.getSql("CraftOrdDesignStdSicDAL.GetMaxIndex");
                    sqlMaxPk = EntityTool.fillSqlByEntity(sqlMaxPk, craftOrdDesignStdSicEntity);
                    maxPk = EntityTool.getEntityBySqlNoClsCon(sqlMaxPk, con, GenericEntity.class);
                    craftOrdDesignStdSicEntity.setIndexSeq(maxPk.getMaxValue());
                }else{
                    sqlSave = CoreXmlSqlParsersFactory.getSql("CraftOrdDesignStdSicDAL.Update");
                }
                sqlSave = EntityTool.fillSqlByEntity(sqlSave, craftOrdDesignStdSicEntity);
                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 Update(ArrayList<String> jsons){
	    TransctionTool transction = new TransctionTool();
	    try{
            for(String json : jsons){
                CraftOrdDesignStdSicEntity craftOrdDesignStdSicEntity = EntityTool.getEntityByGson(json, CraftOrdDesignStdSicEntity.class);
                String sqlUpdate = CoreXmlSqlParsersFactory.getSql("CraftOrdDesignStdSicDAL.Delete");
                sqlUpdate = EntityTool.fillSqlByEntity(sqlUpdate, craftOrdDesignStdSicEntity);
                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("CraftOrdDesignStdSicDAL.getCraftMscPline");
    	b.Fill(new String[]{ designKey, processCode });
    	
    	return this.getData(b.toString());
    }
}
