在 Groovy 中使用哪些循环来实现时隙可用性

Which loops to use for achieving time slot availability in Groovy

我正在尝试在某个时间段创建预订,但我不确定该时间段是否可用。所以我必须检查插槽可用性。

第 1 步:将当前开始和结束时间设置为变量。 例如:startDateTime(SDT) 和 endDateTime(EDT)

第 2 步:检查数据库中的插槽可用性。 例如:

def res = con.firstRow("select * from tblbookingitem where active=1 and fkItemID=$roomID and DateTimeFrom=$SDT and DateTimeTo=$EDT" )

第 3 步:如果槽不可用,则在临时变量中递增 startDateTime 和 endDateTime 并再次检查数据库可用性

注意:这应该在循环中直到插槽可用

第 4 步:当插槽可用时,然后设置为 属性。

我为所有步骤编写了代码,只需要我如何使用循环实现它。

在下面的方法中使用for循环你可以实现。

def flag = false

for(x=startdatetime,y=enddatetime;flag!=true  ; x++ , y++) // you can change x++ and y++ for the the way you want to increase time limit each time
{

 // check slot available
def res = con.firstRow("select * from tblbookingitem where active=1 and 
fkItemID=$roomID and DateTimeFrom=$SDT and DateTimeTo=$EDT" )

if(res!= null) // you can put the condition which says slot is available
{
 flag=true; // you can use even break statment here 
 }

 }

类似的逻辑也可以通过 while 循环实现,语法略有不同

def isAvailable = false
def SDT = context.expand('${#Project#StartDateTime}') 
def EDT = context.expand('${#Project#EndDateTime}')
while( isAvailable==false ) 
{
//Running while loop and checking slot availability.
def res = con.firstRow("select * from tblbookingitem where active=1 and fkItemID=$roomID and DateTimeFrom='$SDT' and DateTimeTo='$EDT'" )

if(res== null) // if null that means slot is available
    {
        //Time slot is available."
        isAvailable=true; // you can use even break statment here
        context.testCase.testSuite.project.setPropertyValue('StartDateTime', SDT)
        context.testCase.testSuite.project.setPropertyValue('EndDateTime', EDT)
        log.info SDT
        log.info EDT
        break;       
    }
else
    {
        //Increment Start and End Time
        SDT=EDT
        def slotinterval = context.expand('${#Project#SlotInterval}').toInteger()
        log.info "Slot Interval : " + slotinterval

        date1 =  new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSS'Z'").parse(EDT)
        use(TimeCategory) 
        {
            def date2 = date1 + slotinterval.minutes
            def outputDateFormat = "yyyy-MM-dd'T'HH:mm:ss.SSS'Z'"
            EDT = "${date2.format(outputDateFormat)}"                       
        }

    }

}