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

public class CraftOrdDesignStdPicDAL extends DBCommon{
	
	/**
	 * 查询质量设计理化
	 * @param strDesignStdPicJSON
	 * @return
	 */
	public CoreReturnObject query(String strDesignStdPicJSON){
		DesignStdPicEntity designStdPicEntity = EntityTool.getEntityByJSONStr(strDesignStdPicJSON, DesignStdPicEntity.class);
		String sql = CoreXmlSqlParsersFactory.getSql("CraftOrdDesignStdPicDAL.query");
		sql = EntityTool.fillSqlByEntity(sql, designStdPicEntity);
		return this.getData(sql);
	}
	
	/**
	 * 查询质量设计理化
	 * @param strDesignStdPicJSON
	 * @return
	 */
	public CoreReturnObject queryByDesignKeyPic(String[] parms){
		SqlBuilder b = new SqlBuilder("CraftOrdDesignStdPicDAL.queryByDesignKeyPic");
		b.Fill(parms);
		return this.getData(b.toString());
	}
	
	/**
	 * 通过DESIGN_KEY查询质量设计理化
	 * @param strDesignStdPicJSON
	 * @return
	 */
	public CoreReturnObject queryPicByDesignKey(String strDesignStdPicJSON){
		DesignStdPicEntity designStdPicEntity = EntityTool.getEntityByJSONStr(strDesignStdPicJSON, DesignStdPicEntity.class);
		String sql = CoreXmlSqlParsersFactory.getSql("CraftOrdDesignStdPicDAL.queryPicByDesignKey");
		sql = EntityTool.fillSqlByEntity(sql, designStdPicEntity);
		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){
                CraftOrdDesignStdPicEntity craftOrdDesignStdPicEntity = EntityTool.getEntityByGson(json, CraftOrdDesignStdPicEntity.class);
                craftOrdDesignStdPicEntity.setIscheck(craftOrdDesignStdPicEntity.getIscheck().equals("True") ? "1" : "0");
                craftOrdDesignStdPicEntity.setIsjudge(craftOrdDesignStdPicEntity.getIsjudge().equals("True") ? "1" : "0");
                craftOrdDesignStdPicEntity.setIsquote(craftOrdDesignStdPicEntity.getIsquote().equals("True") ? "1" : "0");
                craftOrdDesignStdPicEntity.setPhyType(craftOrdDesignStdPicEntity.getPhyType().equals("True") ? "1" : "0");
                craftOrdDesignStdPicEntity.setBilletSample(craftOrdDesignStdPicEntity.getBilletSample().equals("True") ? "1" : "0");
                
                String sqlSave = "";
                if(craftOrdDesignStdPicEntity.getCreateTime().equals("")){
                    sqlSave = CoreXmlSqlParsersFactory.getSql("CraftOrdDesignStdPicDAL.Insert");
                    String sqlMaxPk = CoreXmlSqlParsersFactory.getSql("CraftOrdDesignStdPicDAL.GetMaxIndex");
                    sqlMaxPk = EntityTool.fillSqlByEntity(sqlMaxPk, craftOrdDesignStdPicEntity);
                    maxPk = EntityTool.getEntityBySqlNoClsCon(sqlMaxPk, con, GenericEntity.class);
                    craftOrdDesignStdPicEntity.setIndexSeq(maxPk.getMaxValue());
                }else{
                    sqlSave = CoreXmlSqlParsersFactory.getSql("CraftOrdDesignStdPicDAL.Update");
                }
                sqlSave = EntityTool.fillSqlByEntity(sqlSave, craftOrdDesignStdPicEntity);
                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){
                CraftOrdDesignStdPicEntity craftOrdDesignStdPicEntity = EntityTool.getEntityByGson(json, CraftOrdDesignStdPicEntity.class);
                String sqlUpdate = CoreXmlSqlParsersFactory.getSql("CraftOrdDesignStdPicDAL.Delete");
                sqlUpdate = EntityTool.fillSqlByEntity(sqlUpdate, craftOrdDesignStdPicEntity);
                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("CraftOrdDesignStdPicDAL.getCraftMscPline");
    	b.Fill(new String[]{ designKey, processCode });
    	
    	return this.getData(b.toString());
    }
    
    /**
     * 获取评审记录试样组的分组
     * @param designKey
     * @param mscPline
     * @param processCode
     * @param pic
     * @return
     */
    public CoreReturnObject getGroupPhyMin(String designKey, String pic, String mscPline,
    		String processCode){
    	SqlBuilder b = new SqlBuilder("CraftOrdDesignStdPicDAL.getGroupPhyMin");
    	b.Fill(new String[]{ designKey, pic, mscPline, processCode });
    	
    	return this.getData(b.toString());
    }
    
    /**
     * 获取试样组基础数据
     * @return
     */
    public CoreReturnObject getPhyNameMin(){
    	SqlBuilder b = new SqlBuilder("CraftOrdDesignStdPicDAL.getPhyNameMin");
    	return this.getData(b.toString());
    }
    
    /**
     * 通过实验组获取检验大项
     * @param phyCodeMin
     * @return
     */
    public CoreReturnObject getPhyMaxByPhyMin(String phyCodeMin){
    	SqlBuilder b = new SqlBuilder("CraftOrdDesignStdPicDAL.getPhyMaxByPhyMin");
    	b.Fill(new String[]{ phyCodeMin });
    	return this.getData(b.toString());
    }
}
