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

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

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

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

    /**
     * 保存
     */
    public CoreReturnObject Save(String json, ArrayList<String> subJsons,
    		ArrayList<String> subTmpJsons){
    TransctionTool transction = new TransctionTool();
    try{
            Connection con = transction.getCon();
            GenericEntity maxPk = new GenericEntity();
            ComBaseImpactEntity comBaseImpactEntity = EntityTool.getEntityByGson(json, ComBaseImpactEntity.class);
            String sqlSave = "";
            
            if(comBaseImpactEntity != null){
            	if(comBaseImpactEntity.getValidflag().equals("")){
                    sqlSave = CoreXmlSqlParsersFactory.getSql("ComBaseImpactDAL.Insert");
                    String sqlMaxPk = CoreXmlSqlParsersFactory.getSql("ComBaseImpactDAL.GetMaxPk");
                    sqlMaxPk = EntityTool.fillSqlByEntity(sqlMaxPk, comBaseImpactEntity);
                    maxPk = EntityTool.getEntityBySqlNoClsCon(sqlMaxPk, con, GenericEntity.class);
                    comBaseImpactEntity.setId(maxPk.getMaxValue());
                }else{
                    sqlSave = CoreXmlSqlParsersFactory.getSql("ComBaseImpactDAL.Update");
                }
                if(comBaseImpactEntity.getAllowArc().equals("true")){
                	comBaseImpactEntity.setAllowArc("1");
                }else{
                	comBaseImpactEntity.setAllowArc("0");
                }
                sqlSave = EntityTool.fillSqlByEntity(sqlSave, comBaseImpactEntity);
                if(!transction.ExcuteTransction(sqlSave)){
                    return transction.rollBack();
                }
            }

            for(String subJson : subJsons){
            	ComBaseImpactCEntity impactCEntity = EntityTool.getEntityByGson(subJson, ComBaseImpactCEntity.class);
            	if(impactCEntity.getValidflag().equals("")){
                    sqlSave = CoreXmlSqlParsersFactory.getSql("ComBaseImpactCDAL.Insert");
                    if(comBaseImpactEntity != null){
                    	impactCEntity.setId(comBaseImpactEntity.getId());
                    }
                    String sqlMaxIndex = CoreXmlSqlParsersFactory.getSql("ComBaseImpactCDAL.GetMaxIndex");
                    sqlMaxIndex = EntityTool.fillSqlByEntity(sqlMaxIndex, impactCEntity);
                    maxPk = EntityTool.getEntityBySqlNoClsCon(sqlMaxIndex, con, GenericEntity.class);
                    impactCEntity.setIndexSeq(maxPk.getMaxValue());
                }else{
                    sqlSave = CoreXmlSqlParsersFactory.getSql("ComBaseImpactCDAL.Update");
                }
                sqlSave = EntityTool.fillSqlByEntity(sqlSave, impactCEntity);
                if(!transction.ExcuteTransction(sqlSave)){
                    return transction.rollBack();
                }
            }
            
            //递减温度
            for(String subTmpJson : subTmpJsons){
            	ComBaseImpactTempCEntity temp = EntityTool.getEntityByGson(
            			subTmpJson, ComBaseImpactTempCEntity.class);
            	if(temp.getValidflag().equals("")){
                    sqlSave = CoreXmlSqlParsersFactory.getSql("ComBaseImpactTempCDAL.Insert");
                    if(comBaseImpactEntity != null){
                    	temp.setId(comBaseImpactEntity.getId());
                    }
                    String sqlMaxIndex = CoreXmlSqlParsersFactory.getSql("ComBaseImpactTempCDAL.GetMaxIndex");
                    sqlMaxIndex = EntityTool.fillSqlByEntity(sqlMaxIndex, temp);
                    maxPk = EntityTool.getEntityBySqlNoClsCon(sqlMaxIndex, con, GenericEntity.class);
                    temp.setIndexSeq(maxPk.getMaxValue());
                }else{
                    sqlSave = CoreXmlSqlParsersFactory.getSql("ComBaseImpactTempCDAL.Update");
                }
                sqlSave = EntityTool.fillSqlByEntity(sqlSave, temp);
                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){
                ComBaseImpactEntity comBaseImpactEntity = EntityTool.getEntityByGson(json, ComBaseImpactEntity.class);
                String sqlUpdateValidflag = "";
                if(validflag.equals("0")){
                    sqlUpdateValidflag = CoreXmlSqlParsersFactory.getSql("ComBaseImpactDAL.Delete");
                }else{
                    sqlUpdateValidflag = CoreXmlSqlParsersFactory.getSql("ComBaseImpactDAL.Resume");
                }
                sqlUpdateValidflag = EntityTool.fillSqlByEntity(sqlUpdateValidflag, comBaseImpactEntity);
                if(!transction.ExcuteTransction(sqlUpdateValidflag)){
                    return transction.rollBack();
                }
            }
        }catch(Exception ex){
            CoreReturnObject cro = transction.rollBack();
            cro.setV_errMsg(ex.getMessage());
            return cro;
        }
        return transction.commit();
    }
    
    /**
     * 获取试验尺寸
     * @return
     */
    public CoreReturnObject getItemC(){
    	SqlBuilder b = new SqlBuilder("ComBaseImpactDAL.getItemC");
    	return this.getData(b.toString());
    }
}
