| | |
| | | import com.baomidou.mybatisplus.core.toolkit.Wrappers; |
| | | import com.sandu.common.execption.BusinessException; |
| | | import com.sandu.common.service.impl.BaseServiceImpl; |
| | | import com.sandu.common.util.SpringContextHolder; |
| | | import com.sandu.ximon.admin.param.C3mChargingChargeParam; |
| | | import com.sandu.ximon.admin.utils.StoreOperationRecordsUtils; |
| | | import com.sandu.ximon.dao.domain.C3mCharging; |
| | | import com.sandu.ximon.dao.domain.C3mChargingCharge; |
| | | import com.sandu.ximon.dao.domain.PoleBinding; |
| | | import com.sandu.ximon.dao.mapper.C3mChargingChargeMapper; |
| | |
| | | import java.util.Comparator; |
| | | import java.util.Date; |
| | | import java.util.List; |
| | | import java.util.stream.Collectors; |
| | | |
| | | /** |
| | | * @Author liuhaonan |
| | |
| | | */ |
| | | public boolean updateCharge(List<C3mChargingCharge> chargeEntities) { |
| | | // 删除原本存在的费率 |
| | | c3mChargingChargeMapper.deleteCharge(chargeEntities.get(0).getC3Id()); |
| | | Integer c3Id = chargeEntities.get(0).getC3Id(); |
| | | |
| | | if (c3Id == null) { |
| | | throw new BusinessException("c3Id不能为空"); |
| | | } |
| | | c3mChargingChargeMapper.deleteCharge(c3Id); |
| | | Long timestamp = new Date().getTime(); |
| | | |
| | | C3ChargingService chargingService = SpringContextHolder.getBean(C3ChargingService.class); |
| | | C3mCharging byId = chargingService.getById(c3Id); |
| | | if (byId == null) { |
| | | throw new BusinessException("c3Id不存在"); |
| | | } |
| | | String s = chargingService.setRate(byId.getC3Mac(), chargeEntities,false); |
| | | if (!"操作成功".equals(s)) { |
| | | throw new BusinessException("设置失败,失败原因: " + s); |
| | | } |
| | | // 将费率添加至数据库 |
| | | for (C3mChargingCharge c3m : chargeEntities) { |
| | | c3m.setTimestamp(timestamp); |
| | |
| | | * @return |
| | | */ |
| | | public BigDecimal getCostByC3id(Long c3Id, Integer power) { |
| | | List<C3mChargingCharge> list = list(Wrappers.lambdaQuery(C3mChargingCharge.class).eq(C3mChargingCharge::getC3Id, c3Id)); |
| | | //获取当前时间 |
| | | LocalTime localTime = LocalTime.now(); |
| | | //遍历费率集合 判断当前时间是否在费率时间段内 |
| | | LocalTime startTime; |
| | | LocalTime endTime; |
| | | //默认费率 |
| | | Double charge = 8.8; |
| | | if (list.size() > 1) { |
| | | //费率条数大于1时,遍历费率集合 获取当前时间段的费率 |
| | | for (int i = 0; i < list.size(); i++) { |
| | | //获取费率时间段 转化为LocalTime |
| | | startTime = LocalTime.of(list.get(i).getHour(), list.get(i).getMin()); |
| | | endTime = LocalTime.of(list.get(i + 1).getHour(), list.get(i + 1).getMin()); |
| | | if (i == list.size()) { |
| | | startTime = LocalTime.of(list.get(i).getHour(), list.get(i).getMin()); |
| | | endTime = LocalTime.of(list.get(0).getHour(), list.get(0).getMin()); |
| | | } |
| | | //判断当前时间是否在费率时间段内 |
| | | if (localTime.isAfter(startTime) && localTime.isBefore(endTime)) { |
| | | //获取费率 |
| | | charge = list.get(i).getCharge(); |
| | | } |
| | | |
| | | } |
| | | } else if (list.size() == 1) { |
| | | //只有一条费率时,直接获取费率 |
| | | charge = list.get(0).getCharge(); |
| | | } else { |
| | | //其他费率时,获取默认费率 |
| | | charge = 8.8; |
| | | } |
| | | //计算花费 费率*电量 |
| | | BigDecimal chargeDecimal = new BigDecimal(charge); |
| | | BigDecimal chargeDecimal = getchargeDecimalByC3id(c3Id); |
| | | BigDecimal powerDecimal = new BigDecimal(power); |
| | | BigDecimal result = chargeDecimal.multiply(powerDecimal); |
| | | return result; |
| | | } |
| | | |
| | | |
| | | /** |
| | | * 根据c3Id查找费率 |
| | | * |
| | | * @param c3Id |
| | | * @return |
| | | */ |
| | | public BigDecimal getchargeDecimalByC3id(Long c3Id) { |
| | | List<C3mChargingCharge> list = list(Wrappers.lambdaQuery(C3mChargingCharge.class).eq(C3mChargingCharge::getC3Id, c3Id)); |
| | | |
| | | //只有一条费率时,直接返回 |
| | | if (list.size() == 1) { |
| | | return BigDecimal.valueOf(list.get(0).getCharge()); |
| | | } else {//费率大于一条时候,计算费率 |
| | | |
| | | //排序费率(根据时间排序) |
| | | List<C3mChargingCharge> collect = |
| | | list.stream().sorted(Comparator.comparing(C3mChargingCharge::getHour).thenComparing(C3mChargingCharge::getMin)).collect(Collectors.toList()); |
| | | System.out.println("----- " + collect); |
| | | |
| | | //获取当前时间 |
| | | LocalTime localTime = LocalTime.now(); |
| | | // LocalTime localTime = LocalTime.of(18, 00); |
| | | System.out.println("time"+localTime); |
| | | |
| | | C3mChargingCharge temp1 = collect.get(collect.size() - 1); |
| | | if (localTime.isAfter(LocalTime.of(temp1.getHour(), temp1.getMin()))) { |
| | | return BigDecimal.valueOf(temp1.getCharge()); |
| | | } |
| | | |
| | | C3mChargingCharge temp2= collect.get(0); |
| | | if (localTime.isBefore(LocalTime.of(temp2.getHour(), temp2.getMin()))) { |
| | | return BigDecimal.valueOf(temp1.getCharge()); |
| | | } |
| | | |
| | | |
| | | for (int i = 0; i < collect.size(); i++) { |
| | | int x = i; |
| | | int y = i + 1; |
| | | if (y == collect.size()) { |
| | | y = 0; |
| | | } |
| | | LocalTime startTime = LocalTime.of(collect.get(x).getHour(), collect.get(x).getMin()); |
| | | LocalTime endTime = LocalTime.of(collect.get(y).getHour(), collect.get(y).getMin()); |
| | | |
| | | //当前时间等于费率时间段的开始时间 |
| | | if (localTime.equals(startTime)) { |
| | | return BigDecimal.valueOf(collect.get(x).getCharge()); |
| | | } |
| | | |
| | | //当前时间在费率时间段内 |
| | | if (localTime.isAfter(startTime) && localTime.isBefore(endTime)) { |
| | | return BigDecimal.valueOf(collect.get(x).getCharge()); |
| | | } |
| | | } |
| | | } |
| | | |
| | | return BigDecimal.valueOf(8.8); |
| | | } |
| | | } |