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.ComBaseSplineLengthEntity;
import com.steering.pss.qcm.model.GenericEntity;

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

 /**
     * 通用查询
     */
    public CoreReturnObject Query(String itemCodeC, String validflag){
    	SqlBuilder b = new SqlBuilder("ComBaseSplineLengthDAL.Query");
        b.Fill(new String[]{ itemCodeC, validflag });
    	return this.getData(b.toString());
    }

    /**
     * 通过主键查询
     */
    public CoreReturnObject QueryByPk(String id){
        String sqlQuery = CoreXmlSqlParsersFactory.getSql("ComBaseSplineLengthDAL.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){
                ComBaseSplineLengthEntity comBaseSplineLengthEntity = EntityTool.getEntityByGson(json, ComBaseSplineLengthEntity.class);
                String sqlSave = "";
                if(comBaseSplineLengthEntity.getValidflag().equals("")){
                    sqlSave = CoreXmlSqlParsersFactory.getSql("ComBaseSplineLengthDAL.Insert");
                    if(comBaseSplineLengthEntity.getId().equals("")){
                    	String sqlMaxPk = CoreXmlSqlParsersFactory.getSql("ComBaseSplineLengthDAL.GetMaxPk");
                        sqlMaxPk = EntityTool.fillSqlByEntity(sqlMaxPk, comBaseSplineLengthEntity);
                        maxPk = EntityTool.getEntityBySqlNoClsCon(sqlMaxPk, con, GenericEntity.class);
                        comBaseSplineLengthEntity.setId(maxPk.getMaxValue());
                    }
                }else{
                    sqlSave = CoreXmlSqlParsersFactory.getSql("ComBaseSplineLengthDAL.Update");
                }
                sqlSave = EntityTool.fillSqlByEntity(sqlSave, comBaseSplineLengthEntity);
                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 UpdateValidflag(ArrayList<String> jsons, String validflag){
        TransctionTool transction = new TransctionTool();
    try{
            Connection con = transction.getCon();
            for(String json : jsons){
                ComBaseSplineLengthEntity comBaseSplineLengthEntity = EntityTool.getEntityByGson(json, ComBaseSplineLengthEntity.class);
                String sqlUpdateValidflag = "";
                if(validflag.equals("0")){
                    sqlUpdateValidflag = CoreXmlSqlParsersFactory.getSql("ComBaseSplineLengthDAL.Delete");
                }else{
                    sqlUpdateValidflag = CoreXmlSqlParsersFactory.getSql("ComBaseSplineLengthDAL.Resume");
                }
                sqlUpdateValidflag = EntityTool.fillSqlByEntity(sqlUpdateValidflag, comBaseSplineLengthEntity);
                if(!transction.ExcuteTransction(sqlUpdateValidflag)){
                    return transction.rollBack();
                }
            }
        }catch(Exception ex){
            CoreReturnObject cro = transction.rollBack();
            cro.setV_errMsg(ex.getMessage());
            return cro;
        }
        return transction.commit();
    }
}
