package com.casic.PgInterface.overtimeInPG.manager;

import com.casic.PgInterface.core.hibernate.HibernateEntityDao;
import com.casic.PgInterface.overtimeInPG.domain.PGTagMac;
import org.hibernate.SQLQuery;
import org.hibernate.procedure.ProcedureCall;
import org.springframework.stereotype.Service;

import javax.persistence.ParameterMode;
import java.sql.CallableStatement;
import java.util.Date;

@Service
public class PGTagMacManager extends HibernateEntityDao<PGTagMac>{
    public String getMinInTime()
    {
        String minInTime=null;
        String sql = "select min(intime) from pg_inpg_status where outtime is null and intime between sysdate-1 and sysdate-1/48";
        sql+= " and exists (select count(1) from pg_tagmac)";//感应设备表数据存在
        try {

            minInTime = this.getSession().createSQLQuery(sql).uniqueResult().toString();
        }
        catch (Exception e)
        {
            e.getMessage();
            e.printStackTrace();
        }
        return minInTime;
    }
        public int inPGalarm (String hostId , String inTime)
    {
        int iRet=0;
        SQLQuery sqlQuery = this.getSession().createSQLQuery("{call inPGalarm(?,?,?)}");
        sqlQuery.setString(0,hostId);
        sqlQuery.setString(1,inTime);
        sqlQuery.setInteger(2,iRet);
        sqlQuery.executeUpdate();
        return iRet;
    }
    public int updateINPGstatus(String hostIdStr,String inTimeStr,String outTimeStr)
    {
        int iRet=0;
        ProcedureCall procedureCall = this.getSession().createStoredProcedureCall("updateINPGstatus");
        procedureCall.registerParameter(0,String.class,ParameterMode.IN).bindValue(hostIdStr);
        procedureCall.registerParameter(1,String.class,ParameterMode.IN).bindValue(inTimeStr);
        procedureCall.registerParameter(2,String.class,ParameterMode.IN).bindValue(outTimeStr);
        procedureCall.registerParameter(3,Integer.class,ParameterMode.OUT);
        iRet = (int) procedureCall.getOutputs().getOutputParameterValue(3);
        return iRet;
    }

}


