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

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

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

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

    /**
     * 新增
     */
    public CoreReturnObject Insert(ArrayList<String> jsons){
        TransctionTool transction = new TransctionTool();
        try{
            Connection con = transction.getCon();
            GenericEntity maxPk = null;
            for(String json : jsons){
                ComBasePurchaseRangeEntity comBasePurchaseRangeEntity = EntityTool.getEntityByGson(json, ComBasePurchaseRangeEntity.class);
                String sqlMaxPk = CoreXmlSqlParsersFactory.getSql("ComBasePurchaseRangeDAL.GetMaxPk");
                sqlMaxPk = EntityTool.fillSqlByEntity(sqlMaxPk, comBasePurchaseRangeEntity);
                maxPk = EntityTool.getEntityBySqlNoClsCon(sqlMaxPk, con, GenericEntity.class);
                comBasePurchaseRangeEntity.setRangeId(maxPk.getMaxValue());
                String sqlInsert = CoreXmlSqlParsersFactory.getSql("ComBasePurchaseRangeDAL.Insert");
                sqlInsert = EntityTool.fillSqlByEntity(sqlInsert, comBasePurchaseRangeEntity);
                if(!transction.ExcuteTransction(sqlInsert)){
                    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{
            Connection con = transction.getCon();
            for(String json : jsons){
                ComBasePurchaseRangeEntity comBasePurchaseRangeEntity = EntityTool.getEntityByGson(json, ComBasePurchaseRangeEntity.class);
                String sqlUpdate = CoreXmlSqlParsersFactory.getSql("ComBasePurchaseRangeDAL.Update");
                sqlUpdate = EntityTool.fillSqlByEntity(sqlUpdate, comBasePurchaseRangeEntity);
                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 UpdateValidflag(ArrayList<String> jsons, String validflag){
        TransctionTool transction = new TransctionTool();
    try{
            Connection con = transction.getCon();
            for(String json : jsons){
                ComBasePurchaseRangeEntity comBasePurchaseRangeEntity = EntityTool.getEntityByGson(json, ComBasePurchaseRangeEntity.class);
                String sqlUpdateValidflag = "";
                if(validflag.equals("0")){
                    sqlUpdateValidflag = CoreXmlSqlParsersFactory.getSql("ComBasePurchaseRangeDAL.Delete");
                }else{
                    sqlUpdateValidflag = CoreXmlSqlParsersFactory.getSql("ComBasePurchaseRangeDAL.Resume");
                }
                sqlUpdateValidflag = EntityTool.fillSqlByEntity(sqlUpdateValidflag, comBasePurchaseRangeEntity);
                if(!transction.ExcuteTransction(sqlUpdateValidflag)){
                    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){
                ComBasePurchaseRangeEntity comBasePurchaseRangeEntity = EntityTool.getEntityByGson(json, ComBasePurchaseRangeEntity.class);
                String sql = CoreXmlSqlParsersFactory.getSql("ComBasePurchaseRangeDAL.Delete2");
                sql = EntityTool.fillSqlByEntity(sql, comBasePurchaseRangeEntity);
                if(!transction.ExcuteTransction(sql)){
                    return transction.rollBack();
                }
            }
        }catch(Exception ex){
            CoreReturnObject cro = transction.rollBack();
            cro.setV_errMsg(ex.getMessage());
            return cro;
        }
        return transction.commit();
    }
}
