查询营业状态

项目中有个场景是根据当前时间通过商家的营业时间查询该商家的营业状态.记录下过程.

首先在设计商家数据表示,我把商家的营业开始时间及结束时间直接设置成time属性.

而在后台添加数据时,直接存取字符串,通过正则表达式规定格式,再通过后台处理.ps:因为用的插件较老,不好直接用时间插件

页面如下:

<#input id="startTime" name="营业时间" underline="true"  value="${startTime}"/>
<#input id="stopTime" name="打烊时间" underline="true" value="${stopTime}"/>

正则验证是:

startTime: {
            validators: {
                notEmpty: {
                    message: '不能为空'
                },
                regexp: {/* 只需加此键值对,包含正则表达式,和提示 */
                    regexp:  /^(20|21|22|23|[0-1]\d):[0-5]\d:[0-5]\d$/,
                    message: '时间格式必须是08:00:00'
                },
            }
        },
stopTime: {
     validators: {
        notEmpty: {
             message: '不能为空'
        },
        regexp: {/* 只需加此键值对,包含正则表达式,和提示 */
             regexp:  /^(20|21|22|23|[0-1]\d):[0-5]\d:[0-5]\d$/,
             message: '时间格式必须是08:00:00'
        },
  }
},

提交时后台做时间格式化处理,除了接收整个对象之外,因为要对时间单独处理,所以多接收两个字段,代码如下:

public Object add(Hotel hotel, String startTime, String stopTime) {
        if (ToolUtil.isOneEmpty(hotel, hotel.getHotelName(), comInfo.getProvince())) {
            throw new BussinessException(BizExceptionEnum.REQUEST_NULL);
        }
        try {
            SimpleDateFormat sdf = new SimpleDateFormat("HH:mm:ss");
            Date hotelStartTime = sdf.parse(startTime);
            Date hotelStopTime = sdf.parse(stopTime);
            hotel.setHotelStartTime(hotelStartTime);
            hotel.setHotelStopTime(hotelStopTime);
        } catch (ParseException e) {
            System.out.println(e.getMessage());
        }
        ihotelService.insert(hotel);
        return super.SUCCESS_TIP;
    }

 

mybatis中查询营业时间,代码如下:

SELECT
  (DATE_FORMAT(NOW(),'%T') BETWEEN hotel_start_time AND hotel_stop_time) AS 'isOpen',
   h.hotel_id hotelId
from hotel h

其中(DATE_FORMAT(NOW(),'%T')是获取当前时间的time部分.

这样返回的isOpen则是营业状态,1为营业中,2为歇业中.

posted @ 2018-01-06 15:03  三生石龙  阅读(565)  评论(0编辑  收藏  举报