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

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

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

    /**
     * 通过主键查询
     */
    public CoreReturnObject QueryByPk(String cicGid, String xh){
        String sqlQuery = CoreXmlSqlParsersFactory.getSql("CraftLg2CicDAL.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){
                CraftLg2CicEntity craftLg2CicEntity = EntityTool.getEntityByGson(json, CraftLg2CicEntity.class);
                String sqlMaxPk = CoreXmlSqlParsersFactory.getSql("CraftLg2CicDAL.GetMaxPk");
                sqlMaxPk = EntityTool.fillSqlByEntity(sqlMaxPk, craftLg2CicEntity);
                maxPk = EntityTool.getEntityBySqlNoClsCon(sqlMaxPk, con, GenericEntity.class);
                //craftLg2CicEntity.setPk(maxPk.getMaxValue());
                String sqlInsert = CoreXmlSqlParsersFactory.getSql("CraftLg2CicDAL.Insert");
                sqlInsert = EntityTool.fillSqlByEntity(sqlInsert, craftLg2CicEntity);
                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){
                CraftLg2CicEntity craftLg2CicEntity = EntityTool.getEntityByGson(json, CraftLg2CicEntity.class);
                String sqlUpdate = CoreXmlSqlParsersFactory.getSql("CraftLg2CicDAL.Update");
                sqlUpdate = EntityTool.fillSqlByEntity(sqlUpdate, craftLg2CicEntity);
                if(!transction.ExcuteTransction(sqlUpdate)){
                    return transction.rollBack();
                }
            }
        }catch(Exception ex){
            CoreReturnObject cro = transction.rollBack();
            cro.setV_errMsg(ex.getMessage());
            return cro;
        }
        return transction.commit();
    }
}
