|
@@ -1,5 +1,6 @@
|
|
|
package com.jjt.elec.service.impl;
|
|
|
|
|
|
+import com.jjt.common.utils.StringUtils;
|
|
|
import com.jjt.elec.domain.ElecPeriod;
|
|
|
import com.jjt.elec.domain.ElecPrice;
|
|
|
import com.jjt.elec.mapper.ElecPriceMapper;
|
|
@@ -10,9 +11,10 @@ import org.springframework.stereotype.Service;
|
|
|
import javax.annotation.Resource;
|
|
|
import java.math.BigDecimal;
|
|
|
import java.time.LocalDate;
|
|
|
-import java.util.HashSet;
|
|
|
-import java.util.List;
|
|
|
-import java.util.Set;
|
|
|
+import java.util.*;
|
|
|
+import java.util.stream.Collectors;
|
|
|
+import java.util.stream.IntStream;
|
|
|
+import java.util.stream.Stream;
|
|
|
|
|
|
/**
|
|
|
* 电费价格配置Service业务层处理
|
|
@@ -94,74 +96,80 @@ public class ElecPriceServiceImpl implements IElecPriceService {
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
- * 根据月份和时段获取价格
|
|
|
+ * 根据时间获取时段价格列表
|
|
|
*
|
|
|
* @param localDate 时间,只取月份
|
|
|
- * @param hour 小时
|
|
|
* @return 电价
|
|
|
*/
|
|
|
@Override
|
|
|
- public BigDecimal getPrice(LocalDate localDate, int hour) {
|
|
|
+ public Map<Integer, BigDecimal> getPrice(LocalDate localDate) {
|
|
|
ElecPeriod period = periodService.selectElecPeriodByPeriodId(1L);
|
|
|
int year = localDate.getYear();
|
|
|
int month = localDate.getMonthValue();
|
|
|
ElecPrice priceObj = selectElecPriceByMonth(year, month);
|
|
|
|
|
|
// 判断月份是春秋还是夏冬
|
|
|
- boolean isSummerAutumn = getNumbers(period.getSaMonths(), true).contains(month);
|
|
|
+ boolean isSummerAutumn = getNumbers(period.getSaMonths(), PeriodType.MONTH).contains(month);
|
|
|
|
|
|
// 根据月份和时段获取价格
|
|
|
+ Map<Integer, BigDecimal> result;
|
|
|
+ // 根据月份和时段获取价格
|
|
|
if (isSummerAutumn) {
|
|
|
- return getPriceByPeriod(period.getSaPeak(), period.getSaFlat(), period.getSaValley(), period.getSaSuperPeak(), hour, priceObj);
|
|
|
+ result = getPriceByPeriod(period.getSaPeak(), period.getSaFlat(), period.getSaValley(), period.getSaSuperPeak(), priceObj);
|
|
|
} else {
|
|
|
- return getPriceByPeriod(period.getSwPeak(), period.getSwFlat(), period.getSwValley(), period.getSwSuperPeak(), hour, priceObj);
|
|
|
+ result = getPriceByPeriod(period.getSwPeak(), period.getSwFlat(), period.getSwValley(), period.getSwSuperPeak(), priceObj);
|
|
|
}
|
|
|
+ return result;
|
|
|
+
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
* 根据时段获取价格
|
|
|
*/
|
|
|
- private BigDecimal getPriceByPeriod(String peakPeriod, String flatPeriod, String valleyPeriod, String superPeakPeriod, int hour, ElecPrice priceObj) {
|
|
|
- if (getNumbers(peakPeriod, false).contains(hour)) {
|
|
|
- return priceObj.getPricePeak();
|
|
|
- } else if (getNumbers(flatPeriod, false).contains(hour)) {
|
|
|
- return priceObj.getPriceFlat();
|
|
|
- } else if (getNumbers(valleyPeriod, false).contains(hour)) {
|
|
|
- return priceObj.getPriceValley();
|
|
|
- } else if (getNumbers(superPeakPeriod, false).contains(hour)) {
|
|
|
- return priceObj.getPriceSuperPeak();
|
|
|
- }
|
|
|
- return BigDecimal.ZERO;
|
|
|
+ private Map<Integer, BigDecimal> getPriceByPeriod(String peakPeriod, String flatPeriod, String valleyPeriod, String superPeakPeriod, ElecPrice priceObj) {
|
|
|
+ Map<Integer, BigDecimal> map = new HashMap<>();
|
|
|
+ addPricesToMap(map, peakPeriod, priceObj.getPricePeak(), PeriodType.HOUR);
|
|
|
+ addPricesToMap(map, flatPeriod, priceObj.getPriceFlat(), PeriodType.HOUR);
|
|
|
+ addPricesToMap(map, valleyPeriod, priceObj.getPriceValley(), PeriodType.HOUR);
|
|
|
+ addPricesToMap(map, superPeakPeriod, priceObj.getPriceSuperPeak(), PeriodType.HOUR);
|
|
|
+ return map;
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 将指定时段的价格添加到Map中
|
|
|
+ */
|
|
|
+ private void addPricesToMap(Map<Integer, BigDecimal> map, String period, BigDecimal price, PeriodType periodType) {
|
|
|
+ getNumbers(period, periodType).forEach(hour -> map.put(hour, price));
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
* 解析 1-8,9-11这种数据
|
|
|
* 如果是月份,则包含头尾,如果是小时,则不包含尾部
|
|
|
*
|
|
|
- * @param d 字符串
|
|
|
- * @param flag 是否月份
|
|
|
- * @return
|
|
|
+ * @param data 字符串
|
|
|
+ * @param type 时段类型
|
|
|
+ * @return 时段集合
|
|
|
*/
|
|
|
- private Set<Long> getNumbers(String d, boolean flag) {
|
|
|
- String[] temp = d.split(",");
|
|
|
- Set<Long> list = new HashSet();
|
|
|
- for (String s : temp) {
|
|
|
- String[] ss = s.split("-");
|
|
|
- if (ss.length > 1) {
|
|
|
- long st = Long.parseLong(ss[0]);
|
|
|
- long ed = Long.parseLong(ss[1]);
|
|
|
-
|
|
|
- if (!flag) {
|
|
|
- ed = ed - 1;
|
|
|
- }
|
|
|
- for (long i = st; i <= ed; i++) {
|
|
|
- list.add(i);
|
|
|
- }
|
|
|
- } else {
|
|
|
- list.add(Long.parseLong(s));
|
|
|
- }
|
|
|
+ private Set<Integer> getNumbers(String data, PeriodType type) {
|
|
|
+ if (StringUtils.isEmpty(data)) {
|
|
|
+ return Collections.emptySet();
|
|
|
}
|
|
|
- return list;
|
|
|
+
|
|
|
+ return Arrays.stream(data.split(","))
|
|
|
+ .flatMap(s -> {
|
|
|
+ String[] range = s.split("-");
|
|
|
+ if (range.length > 1) {
|
|
|
+ int start = Integer.parseInt(range[0]);
|
|
|
+ int end = Integer.parseInt(range[1]);
|
|
|
+ if (type == PeriodType.HOUR) {
|
|
|
+ end = end - 1;
|
|
|
+ }
|
|
|
+ return IntStream.rangeClosed(start, end).boxed();
|
|
|
+ } else {
|
|
|
+ return Stream.of(Integer.parseInt(range[0]));
|
|
|
+ }
|
|
|
+ })
|
|
|
+ .collect(Collectors.toSet());
|
|
|
}
|
|
|
|
|
|
private ElecPrice selectElecPriceByMonth(int year, int month) {
|
|
@@ -169,9 +177,28 @@ public class ElecPriceServiceImpl implements IElecPriceService {
|
|
|
ElecPrice search = new ElecPrice();
|
|
|
search.setPriceMonth(monthStr);
|
|
|
List<ElecPrice> list = selectElecPriceList(search);
|
|
|
- if (list.size() != 0) {
|
|
|
- return list.get(0);
|
|
|
+ ElecPrice elecPrice;
|
|
|
+ if (list.size() == 0) {
|
|
|
+ //如果未找到,则取最后一条记录
|
|
|
+ list = selectElecPriceList(new ElecPrice());
|
|
|
+ elecPrice = list.get(0);
|
|
|
+ elecPrice.setPriceMonth(monthStr);
|
|
|
+ elecPrice.setPriceId(null);
|
|
|
+ insertElecPrice(elecPrice);
|
|
|
+ } else {
|
|
|
+ elecPrice = list.get(0);
|
|
|
}
|
|
|
- return null;
|
|
|
+ return elecPrice;
|
|
|
+ }
|
|
|
+
|
|
|
+ enum PeriodType {
|
|
|
+ /**
|
|
|
+ * 月份
|
|
|
+ */
|
|
|
+ MONTH,
|
|
|
+ /**
|
|
|
+ * 小时
|
|
|
+ */
|
|
|
+ HOUR
|
|
|
}
|
|
|
}
|