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.CraftFileLv2CEntity;
import CoreFS.SA06.CoreReturnObject;
import CoreFS.SA08.CoreXmlSqlParsersFactory;

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

 /**
     * 通用查询
     */
    public CoreReturnObject Query(String fileLv2Pk, String validflag){
        SqlBuilder b = new SqlBuilder("CraftFileLv2CDAL.Query");
        b.Fill("FILE_LV2_PK", fileLv2Pk);
        b.Fill("VALIDFLAG", validflag);
        return this.getData(b.GetSql());
    }

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