package com.steering.pss.qcm.DAL;

import java.sql.Connection;
import java.util.ArrayList;
import CoreFS.SA06.CoreReturnObject;
import CoreFS.SA08.CoreXmlSqlParsersFactory;
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.ComBaseDetectionRltEntity;
import com.steering.pss.qcm.model.GenericEntity;

/**
 * 数据库表COM_BASE_DETECTION_RLT所对应的Java类（生成工具：代码生成工具4.0   访问地址：http://172.16.2.128/tool/）
 * @author tgcx-test   date 2018-01-27
 */
public class ComBaseDetectionRltDAL extends DBCommon{

    /**
     * 通用查询
     */
    public CoreReturnObject QueryStd(String phyCode){
        SqlBuilder b = new SqlBuilder("ComBaseDetectionRltDAL.QueryStd");
        b.Fill(new String[]{ phyCode });
        return this.getData(b.toString());
    }
    
    /**
     * 通用查询
     */
    public CoreReturnObject QueryLv(String phyCode){
        SqlBuilder b = new SqlBuilder("ComBaseDetectionRltDAL.QueryLv");
        b.Fill(new String[]{ phyCode });
        return this.getData(b.toString());
    }
    
    /**
     * 通用查询
     */
    public CoreReturnObject QueryStdBaseInfo(String phyCode){
        SqlBuilder b = new SqlBuilder("ComBaseDetectionRltDAL.QueryStdBaseInfo");
        b.Fill(new String[]{ phyCode });
        return this.getData(b.toString());
    }
    
    /**
     * 通用查询
     */
    public CoreReturnObject QueryLvBaseInfo(String phyCode){
        SqlBuilder b = new SqlBuilder("ComBaseDetectionRltDAL.QueryLvBaseInfo");
        b.Fill(new String[]{ phyCode });
        return this.getData(b.toString());
    }

    /**
     * 通过主键查询
     */
    public CoreReturnObject QueryByPk(String id){
        String sqlQuery = CoreXmlSqlParsersFactory.getSql("ComBaseDetectionRltDAL.QueryByPk");
        return this.getData(sqlQuery);
    }

    /**
     * 保存
     */
    public CoreReturnObject Save(ArrayList<String> jsons){
        TransctionTool transction = new TransctionTool();
        try{
            Connection con = transction.getCon();
            GenericEntity maxPk = null;
            for(String json : jsons){
                ComBaseDetectionRltEntity comBaseDetectionRltEntity = EntityTool.getEntityByGson(json, ComBaseDetectionRltEntity.class);
                String sqlSave = "";
                if(comBaseDetectionRltEntity.getId().equals("")){
                    sqlSave = CoreXmlSqlParsersFactory.getSql("ComBaseDetectionRltDAL.Insert");
                    String sqlMaxPk = CoreXmlSqlParsersFactory.getSql("ComBaseDetectionRltDAL.GetMaxPk");
                    sqlMaxPk = EntityTool.fillSqlByEntity(sqlMaxPk, comBaseDetectionRltEntity);
                    maxPk = EntityTool.getEntityBySqlNoClsCon(sqlMaxPk, con, GenericEntity.class);
                    comBaseDetectionRltEntity.setId(maxPk.getMaxValue());
                }else{
                    sqlSave = CoreXmlSqlParsersFactory.getSql("ComBaseDetectionRltDAL.Update");
                }
                sqlSave = EntityTool.fillSqlByEntity(sqlSave, comBaseDetectionRltEntity);
                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){
                ComBaseDetectionRltEntity comBaseDetectionRltEntity = EntityTool.getEntityByGson(json, ComBaseDetectionRltEntity.class);
                String sqlDelete = CoreXmlSqlParsersFactory.getSql("ComBaseDetectionRltDAL.Delete");
                sqlDelete = EntityTool.fillSqlByEntity(sqlDelete, comBaseDetectionRltEntity);
                if(!transction.ExcuteTransction(sqlDelete)){
                    return transction.rollBack();
                }
            }
        }catch(Exception ex){
            CoreReturnObject cro = transction.rollBack();
            cro.setV_errMsg(ex.getMessage());
            return cro;
        }
        return transction.commit();
    }
}
