package com.steering.pss.judge.DAL;

import CoreFS.SA06.CoreReturnObject;
import CoreFS.SA08.CoreXmlSqlParsersFactory;
import CoreFS.SC01.CoreConstantData;
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.QcmZgJugdeApplyEntity;
import com.steering.pss.qcm.model.GenericEntity;

import java.sql.Connection;
import java.util.ArrayList;

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

    /**
     * 通用查询
     */
    public CoreReturnObject Query(){
        String sqlQuery = CoreXmlSqlParsersFactory.getSql("QcmZgJugdeApplyDAL.Query");
        return this.getData(sqlQuery);
    }
    
    /**
     * 查询轧管理化
     * @param timeBegin
     * @param timeEnd
     * @param judgeState
     * @param judgeStoveNo
     * @param proPlanId
     * @param judgeType
     * @return
     */
    public CoreReturnObject QueryZgPhy(String timeBegin, String timeEnd, String judgeState,
    		String judgeStoveNo, String proPlanId, String judgeType){
    	SqlBuilder b = new SqlBuilder("QcmZgJugdeApplyDAL.QueryZgPhy");
    	b.Fill(new String[]{ timeBegin, timeEnd, judgeType, judgeState, judgeStoveNo, proPlanId });
    	
    	return this.getData(b.toString());
    }

    /**
     * 通过主键查询
     */
    public CoreReturnObject QueryByPk(String jugdeApplyCode, String judgeStoveNo, String batchNo, String batchGroudNo, String qualityGroup, String matGowhereCode){
        String sqlQuery = CoreXmlSqlParsersFactory.getSql("QcmZgJugdeApplyDAL.QueryByPk");
        return this.getData(sqlQuery);
    }

    /**
     * 修改
     */
    public CoreReturnObject Update(ArrayList<String> jsons){
        TransctionTool transction = new TransctionTool();
        try{
            Connection con = transction.getCon();
            for(String json : jsons){
                QcmZgJugdeApplyEntity qcmZgJugdeApplyEntity = EntityTool.getEntityByGson(json, QcmZgJugdeApplyEntity.class);
                String sqlUpdate = CoreXmlSqlParsersFactory.getSql("QcmZgJugdeApplyDAL.Update");
                sqlUpdate = EntityTool.fillSqlByEntity(sqlUpdate, qcmZgJugdeApplyEntity);
                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){
                QcmZgJugdeApplyEntity qcmZgJugdeApplyEntity = EntityTool.getEntityByGson(json, QcmZgJugdeApplyEntity.class);
                String sqlSave = "";
                if(qcmZgJugdeApplyEntity.getValidflag().equals("")){
                    sqlSave = CoreXmlSqlParsersFactory.getSql("QcmZgJugdeApplyDAL.Insert");
                    String sqlMaxPk = CoreXmlSqlParsersFactory.getSql("QcmZgJugdeApplyDAL.GetMaxPk");
                    sqlMaxPk = EntityTool.fillSqlByEntity(sqlMaxPk, qcmZgJugdeApplyEntity);
                    maxPk = EntityTool.getEntityBySqlNoClsCon(sqlMaxPk, con, GenericEntity.class);
                    //qcmZgJugdeApplyEntity.setPk(maxPk.getMaxValue());
                }else{
                    sqlSave = CoreXmlSqlParsersFactory.getSql("QcmZgJugdeApplyDAL.Update");
                }
                sqlSave = EntityTool.fillSqlByEntity(sqlSave, qcmZgJugdeApplyEntity);
                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){
                QcmZgJugdeApplyEntity qcmZgJugdeApplyEntity = EntityTool.getEntityByGson(json, QcmZgJugdeApplyEntity.class);
                String sqlUpdateValidflag = "";
                if(validflag.equals("0")){
                    sqlUpdateValidflag = CoreXmlSqlParsersFactory.getSql("QcmZgJugdeApplyDAL.Delete");
                }else{
                    sqlUpdateValidflag = CoreXmlSqlParsersFactory.getSql("QcmZgJugdeApplyDAL.Resume");
                }
                sqlUpdateValidflag = EntityTool.fillSqlByEntity(sqlUpdateValidflag, qcmZgJugdeApplyEntity);
                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 doUpdatePassrate(ArrayList<ArrayList<Object>> list) {
        ArrayList<String> sqllist = new ArrayList<String>();
        ArrayList<Object[]> params = new ArrayList<Object[]>();
        for (ArrayList<Object> o : list) {
            String sql = CoreXmlSqlParsersFactory
                    .getSql("QcmZgJugdeApplyDAL.doUpdatePassrate");
            Object[] param = o.toArray();
            sqllist.add(sql);
            params.add(param);
        }
        return this.getDao(CoreConstantData.sysbeanstr).ExcuteTransction(sqllist, params);
    }
}
