DateTimeParamService.java 4.38 KB
package com.viontech.keliu.service;

import cn.hutool.core.date.DateTime;
import cn.hutool.core.date.DateUtil;
import com.viontech.keliu.entity.DateTimeParam;
import com.viontech.keliu.entity.MallBusinessTime;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.collections4.CollectionUtils;
import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;

/**
 * Created with IntelliJ IDEA.
 *
 * @author: zhuhai
 * Date: 2023-03-23
 * Time: 10:51
 */
@Service
@Slf4j
public class DateTimeParamService {


    /**
     * Calendar 中 1 是周日,2 是周一,3 是周二,4 是周三,5 是周四,6 是周五,7 是周六
     * 下标 0 占位用
     */
    public static final int[] WEEKS = {0, 7, 1, 2, 3, 4, 5, 6};


    @Resource
    private JdbcTemplate jdbcTemplate;


    public MallBusinessTime getMallOpenTimeByDate(Long mallId, Date date) {
        // 查出指定的日期和周几的营业时间配置
        String sql = "select mall_id,account_id,start_time,end_time,type from b_mall_business_hours where mall_id = ? and (custom_date = ? or week = ?)";
        List<MallBusinessTime> list = jdbcTemplate.query(sql, new BeanPropertyRowMapper<>(MallBusinessTime.class), mallId, DateUtil.beginOfDay(date), WEEKS[DateUtil.dayOfWeek(date)]);
        if (CollectionUtils.isEmpty(list)) {
            log.error("商场营业时间配置缺失:{}", mallId);
            return null;
        }
        MallBusinessTime customB = list.stream().filter(b -> b.getType() == 1).findFirst().orElse(null);
        if (customB != null) {
            return getRealBusinessTime(customB, date);
        }
        MallBusinessTime weekB = list.stream().filter(b -> b.getType() == 0).findFirst().orElse(null);
        if (weekB != null) {
            return getRealBusinessTime(weekB, date);
        }
        log.error("商场营业时间配置不完整:{}", mallId);
        return null;
    }


    /**
     * 组装countDate对应的营业时间范围
     * @param mallBusinessTime
     * @param countDate
     * @return
     */
    private MallBusinessTime getRealBusinessTime(MallBusinessTime mallBusinessTime, Date countDate) {
        Date startTime = mallBusinessTime.getStartTime();
        Date endTime = mallBusinessTime.getEndTime();
        String formatDate = DateUtil.formatDate(countDate);
        String beginDateTimeFormat = formatDate + " " + DateUtil.formatTime(startTime);
        String endDateTimeFormat = formatDate + " " + DateUtil.formatTime(endTime);
        DateTime beginDateTime = DateUtil.parseDateTime(beginDateTimeFormat);
        DateTime endDateTime = DateUtil.parseDateTime(endDateTimeFormat);
        //如果是跨天的
        if (startTime.getTime() >= endTime.getTime()) {
            endDateTime = DateUtil.offsetDay(endDateTime, 1);
        }
        mallBusinessTime.setStartTime(beginDateTime);
        mallBusinessTime.setEndTime(endDateTime);
        mallBusinessTime.setCountDate(countDate);
        return mallBusinessTime;

    }

    /**
     * 根据营业时间获取需要查询的日期范围
     * @param mallId
     * @param countDate
     * @return
     */
    public DateTimeParam getDateTimeParam(Long mallId, Date countDate) {
        MallBusinessTime mallBusinessTime = getMallOpenTimeByDate(mallId, countDate);
        Date shopBeginDate = DateUtil.beginOfDay(countDate);
        Date shopEndDate = DateUtil.endOfDay(countDate);
        if (mallBusinessTime != null) {
            shopBeginDate = mallBusinessTime.getStartTime();
            shopEndDate = mallBusinessTime.getEndTime();
        }
        Date startDate = DateUtil.beginOfDay(countDate);
        Date endDate = startDate;
        long days = DateUtil.betweenDay(shopBeginDate, shopEndDate, true);
        //跨天
        if (days > 0 && !shopEndDate.equals(DateUtil.beginOfDay(shopEndDate))) {
            endDate = DateUtil.offsetDay(endDate, 1);
        }
        DateTimeParam dateTimeParam = new DateTimeParam();
        dateTimeParam.setStartDate(startDate);
        dateTimeParam.setEndDate(endDate);
        dateTimeParam.setStartDateTime(shopBeginDate);
        dateTimeParam.setEndDateTime(shopEndDate);

        log.info("mallId:{},获取countDate:{} 的时间范围:{}", mallId, countDate, dateTimeParam);
        return dateTimeParam;
    }
}