package com.xiaopeng.map.service.serviceimpl; import cn.hutool.core.util.StrUtil; import com.alibaba.fastjson2.JSONArray; import com.alibaba.fastjson2.JSONObject; import com.xiaopeng.api.model.operation.XpPoint; import com.xiaopeng.api.vo.order.XpPointsListVo; import com.xiaopeng.common.core.utils.DateUtils; import com.xiaopeng.map.service.XpStationService; import com.xiaopeng.map.utils.TimeRangeChecker; import com.xiaopeng.map.utils.holiday.HolidayUtil; import org.springframework.stereotype.Service; import javax.annotation.Resource; import java.time.LocalTime; @Service public class BootCommonService { @Resource private HolidayUtil holidayUtil; // /** * 判断围栏运营策略 * 自定义时 选择每天只能有一个运营策略 * type = 0工作日 1法定节假日 2非工作日 3自定义时间 typeStr 代表周 = [1,2,3,4,5,6,7] typeStr= 0的时候代表每天 * [{"name": "策略组1", "time": ["09:00-18:00"], "type": 0, "typeStr": []}, * {"name": "策略组2", "time": ["08:00-21:00"], "type": 2, "typeStr": []}, * {"name": "策略组3", "time": ["07:00-22:00"], "type": 1, "typeStr": []}, * {"name": "策略组4", "time": ["12:00-19:00"], "type": 3, "typeStr": [1, 3, 5, 7]}] * * [{"name": "策略组1", "time": ["08:00-22:00"], "type": 3, "typeStr": [0]}] * @param electronicOperationStrategy * @return * */ public Boolean getFenceOperation(String electronicOperationStrategy) { return getFenceOperation(LocalTime.now(),electronicOperationStrategy); } public Boolean getFenceOperation(LocalTime localTime,String electronicOperationStrategy) { if(StrUtil.isBlank(electronicOperationStrategy)){ return true; } JSONArray jsonArray = JSONArray.parseArray(electronicOperationStrategy); Integer holidayToday = holidayUtil.isHolidayToday(); //0工作日 1节假日 2非工作日 for (Object o : jsonArray) { JSONObject jsonObject =JSONObject.parseObject(o.toString()); String type = jsonObject.getString("type"); String timeStr = null; if(type.equals(holidayToday.toString())){ timeStr = jsonObject.getString("time"); }else if("3".equals(type)){ // 判断是什么类型 0每天 1-7 想起 String typeStr = jsonObject.getString("typeStr"); // 查看今天周几 Integer week = DateUtils.getWeek(); // 包含每天 或者 包含当天 if(typeStr.contains("0") || typeStr.contains(week.toString())){ timeStr = jsonObject.getString("time"); } } if(StrUtil.isNotBlank(timeStr)){ JSONArray time = jsonObject.getJSONArray("time"); // 判断当前时间 是否在time时间段中 for (Object object : time) { String[] timeArray = object.toString().split("-"); String startTime = timeArray[0]; String endTime = timeArray[1]; boolean currentTimeInRange = DateUtils.isCurrentTimeInRange(localTime,startTime, endTime); if(currentTimeInRange){ return true; } } } } return false; } /** * 判断场站运营策略 * ["06:00-12:00", "13:00-21:00"] * @param localTime * @param operationType 运营类型 1=与运营区同步 2=自定义时间段 * @param effectivePeriod 生效周期 1=每日 2=仅工作日 3=仅非工作日 4=仅节假日 * @param effectiveTimePeriod 场站生效时间段 json * @param effectiveTimePeriod 围栏运营json * @return */ public boolean getStationOperation(LocalTime localTime,Integer operationType,Integer effectivePeriod, String effectiveTimePeriod,String fenceOperationStrategy){ if(operationType == 1){ return getFenceOperation(localTime,fenceOperationStrategy); }else { Integer holidayToday = holidayUtil.isHolidayToday(); if (effectivePeriod == 1) { boolean inTime = TimeRangeChecker.isCurrentTimeInRanges(localTime, effectiveTimePeriod); if (!inTime) { return false; } } else if (effectivePeriod == 2) { if (holidayToday != 0) { return false; } boolean inTime = TimeRangeChecker.isCurrentTimeInRanges(localTime, effectiveTimePeriod); if (!inTime) { return false; } } else if (effectivePeriod == 3) { if (holidayToday != 2) { return false; } boolean inTime = TimeRangeChecker.isCurrentTimeInRanges(localTime, effectiveTimePeriod); if (!inTime) { return false; } } else if (effectivePeriod == 4) { if (holidayToday != 1) { return false; } boolean inTime = TimeRangeChecker.isCurrentTimeInRanges(localTime, effectiveTimePeriod); if (!inTime) { return false; } } } return true; } /** * * @param localTime 时间 * @param xpPoint 点位对象 * @return */ public boolean getPointOperation(LocalTime localTime, XpPoint xpPoint){ if(xpPoint.getOperationType()==2){//运营类型 1=与运营区同步 2=自定义时间段 return getPointOperation(localTime,xpPoint.getEffectivePeriod(),xpPoint.getEffectiveTimePeriod()); }else { if(xpPoint.getStationId()==null){//调用围栏运营策略 return getFenceOperation(localTime,xpPoint.getFenceOperationStrategy()); }else { return this.getStationOperation(localTime,xpPoint.getStationOperationType(),xpPoint.getStationEffectivePeriod(),xpPoint.getStationEffectiveTimePeriod(),xpPoint.getFenceOperationStrategy()); } } } public boolean getPointOperation_vo(LocalTime localTime, XpPointsListVo xpPoint){ if(xpPoint.getOperationType()==2){//运营类型 1=与运营区同步 2=自定义时间段 return getPointOperation(localTime,xpPoint.getEffectivePeriod(),xpPoint.getEffectiveTimePeriod()); }else { if(xpPoint.getStationId()==null){//调用围栏运营策略 return getFenceOperation(localTime,xpPoint.getFenceOperationStrategy()); }else { return this.getStationOperation(localTime,xpPoint.getStationOperationType(),xpPoint.getStationEffectivePeriod(),xpPoint.getStationEffectiveTimePeriod(),xpPoint.getFenceOperationStrategy()); } } } /** * 判断点位是否运营策略 * ["06:00-12:00", "13:00-21:00"] * @param localTime * @param effectivePeriod 生效周期 1=每日 2=仅工作日 3=仅非工作日 4=仅节假日 * @param effectiveTimePeriod 场站运营json/ 生效时间段 json * @return */ private boolean getPointOperation(LocalTime localTime, Integer effectivePeriod, String effectiveTimePeriod){ // 2非工作日 0工作日 1节假日 3调休日 Integer holidayToday = holidayUtil.isHolidayToday(); if (effectivePeriod == 1) { boolean inTime = TimeRangeChecker.isCurrentTimeInRanges(localTime, effectiveTimePeriod); if (!inTime) { return false; } } else if (effectivePeriod == 2) { if (holidayToday != 0) { return false; } boolean inTime = TimeRangeChecker.isCurrentTimeInRanges(localTime, effectiveTimePeriod); if (!inTime) { return false; } } else if (effectivePeriod == 3) { if (holidayToday != 2) { return false; } boolean inTime = TimeRangeChecker.isCurrentTimeInRanges(localTime, effectiveTimePeriod); if (!inTime) { return false; } } else if (effectivePeriod == 4) { if (holidayToday != 1) { return false; } boolean inTime = TimeRangeChecker.isCurrentTimeInRanges(localTime, effectiveTimePeriod); if (!inTime) { return false; } } return true; } }