|
|
@@ -1,672 +0,0 @@
|
|
|
-package com.zsElectric.boot.app.api;
|
|
|
-
|
|
|
-import cn.hutool.core.util.ObjectUtil;
|
|
|
-import cn.hutool.core.util.StrUtil;
|
|
|
-import com.zsElectric.boot.app.model.entity.*;
|
|
|
-import com.zsElectric.boot.app.service.*;
|
|
|
-import com.zsElectric.boot.core.web.Result;
|
|
|
-import jakarta.servlet.http.HttpServletRequest;
|
|
|
-import lombok.RequiredArgsConstructor;
|
|
|
-import lombok.extern.slf4j.Slf4j;
|
|
|
-import org.springframework.data.redis.core.RedisTemplate;
|
|
|
-import org.springframework.transaction.annotation.Transactional;
|
|
|
-import org.springframework.web.bind.annotation.*;
|
|
|
-
|
|
|
-import java.math.BigDecimal;
|
|
|
-import java.math.RoundingMode;
|
|
|
-import java.time.LocalTime;
|
|
|
-import java.time.format.DateTimeFormatter;
|
|
|
-import java.util.*;
|
|
|
-import java.util.concurrent.TimeUnit;
|
|
|
-import java.util.stream.Collectors;
|
|
|
-
|
|
|
-/**
|
|
|
- * 充电API接口 - 面向小程序/APP
|
|
|
- * 包含充电站查询、充电桩控制等核心功能
|
|
|
- */
|
|
|
-@Slf4j
|
|
|
-@RestController
|
|
|
-@RequestMapping("/chargeApi")
|
|
|
-@RequiredArgsConstructor
|
|
|
-public class ChargeApiController {
|
|
|
-
|
|
|
- private final ChargeOrderInfoService chargeOrderInfoService;
|
|
|
- private final ChargeStationService chargeStationService;
|
|
|
- private final ChargeDeviceService chargeDeviceService;
|
|
|
- private final StationTimePriceService stationTimePriceService;
|
|
|
- private final UserAccountService userAccountService;
|
|
|
- private final EcUserAccountService ecUserAccountService;
|
|
|
- private final EcInfoService ecInfoService;
|
|
|
- private final NewUserDiscountService newUserDiscountService;
|
|
|
- private final FirmStationTimePriceService firmStationTimePriceService;
|
|
|
- private final RedisTemplate<String, Object> redisTemplate;
|
|
|
-
|
|
|
- private final String TOKEN_PREFIX = "user:token:";
|
|
|
-
|
|
|
- /**
|
|
|
- * 查询用电量统计
|
|
|
- * TODO: 需要实现具体的统计逻辑
|
|
|
- */
|
|
|
- @PostMapping("/queryUsedAmount")
|
|
|
- public Result<?> queryUsedAmount(@RequestBody Map<String, Object> data, HttpServletRequest request) {
|
|
|
- try {
|
|
|
- String startDate = data.get("startDate") != null ? data.get("startDate").toString() : null;
|
|
|
- String endDate = data.get("endDate") != null ? data.get("endDate").toString() : null;
|
|
|
-
|
|
|
- // TODO: 实现用电量统计逻辑
|
|
|
- Map<String, Object> result = new HashMap<>();
|
|
|
- result.put("totalAmount", BigDecimal.ZERO);
|
|
|
- result.put("totalMoney", BigDecimal.ZERO);
|
|
|
-
|
|
|
- return Result.success(result);
|
|
|
- } catch (Exception e) {
|
|
|
- log.error("查询用电量失败", e);
|
|
|
- return Result.failed("查询失败");
|
|
|
- }
|
|
|
- }
|
|
|
-
|
|
|
- /**
|
|
|
- * 查询充电站列表(带距离、价格、空闲状态等信息)
|
|
|
- */
|
|
|
- @PostMapping("/getStations")
|
|
|
- public Result<?> getStationsByLoc(@RequestBody Map<String, Object> data, HttpServletRequest request) {
|
|
|
- try {
|
|
|
- UserInfo cacheUser = getUserFromToken(request);
|
|
|
-
|
|
|
- // 排序方式:0-距离 1-空闲 2-电费
|
|
|
- Integer order = data.get("order") != null ? Integer.valueOf(data.get("order").toString()) : 0;
|
|
|
- String key = data.get("key") != null ? data.get("key").toString() : null;
|
|
|
- String latStr = data.get("lat") != null ? data.get("lat").toString() : "";
|
|
|
- String lngStr = data.get("lng") != null ? data.get("lng").toString() : "";
|
|
|
-
|
|
|
- // 查询所有上架的充电站
|
|
|
- List<ChargeStation> stationList = chargeStationService.lambdaQuery()
|
|
|
- .eq(ChargeStation::getShowStatus, 1)
|
|
|
- .list();
|
|
|
-
|
|
|
- // 如果有搜索关键字,过滤充电站
|
|
|
- if (StrUtil.isNotBlank(key)) {
|
|
|
- stationList = stationList.stream()
|
|
|
- .filter(s -> s.getStationName() != null && s.getStationName().contains(key))
|
|
|
- .collect(Collectors.toList());
|
|
|
- }
|
|
|
-
|
|
|
- List<Map<String, Object>> resultList = new ArrayList<>();
|
|
|
-
|
|
|
- for (ChargeStation station : stationList) {
|
|
|
- Map<String, Object> stationMap = new HashMap<>();
|
|
|
- stationMap.put("id", station.getId());
|
|
|
- stationMap.put("stationName", station.getStationName());
|
|
|
- stationMap.put("address", station.getAddress());
|
|
|
- stationMap.put("lat", station.getLat());
|
|
|
- stationMap.put("lng", station.getLng());
|
|
|
-
|
|
|
- // 计算距离
|
|
|
- if (StrUtil.isNotBlank(latStr) && StrUtil.isNotBlank(lngStr)) {
|
|
|
- double distance = calculateDistance(
|
|
|
- Double.parseDouble(lngStr),
|
|
|
- Double.parseDouble(latStr),
|
|
|
- Double.parseDouble(station.getLng()),
|
|
|
- Double.parseDouble(station.getLat())
|
|
|
- );
|
|
|
- stationMap.put("range", distance);
|
|
|
- stationMap.put("rangeShow", formatDistance(distance));
|
|
|
- } else {
|
|
|
- stationMap.put("range", 0);
|
|
|
- stationMap.put("rangeShow", "0m");
|
|
|
- }
|
|
|
-
|
|
|
- // 统计充电桩信息
|
|
|
- List<ChargeDevice> devices = chargeDeviceService.lambdaQuery()
|
|
|
- .eq(ChargeDevice::getStationId, station.getId())
|
|
|
- .list();
|
|
|
-
|
|
|
- int totalFast = 0, emptyFast = 0, totalSlow = 0, emptySlow = 0;
|
|
|
- for (ChargeDevice device : devices) {
|
|
|
- if (device.getPower().compareTo(new BigDecimal("15")) <= 0) {
|
|
|
- totalSlow++;
|
|
|
- if (device.getDeviceStatus() == 1) emptySlow++;
|
|
|
- } else {
|
|
|
- totalFast++;
|
|
|
- if (device.getDeviceStatus() == 1) emptyFast++;
|
|
|
- }
|
|
|
- }
|
|
|
-
|
|
|
- stationMap.put("totalFast", totalFast);
|
|
|
- stationMap.put("emptyFast", emptyFast);
|
|
|
- stationMap.put("totalSlow", totalSlow);
|
|
|
- stationMap.put("emptySlow", emptySlow);
|
|
|
- stationMap.put("totalEmpty", emptyFast + emptySlow);
|
|
|
-
|
|
|
- // 查询当前时段价格
|
|
|
- List<StationTimePrice> timePrices = stationTimePriceService.lambdaQuery()
|
|
|
- .eq(StationTimePrice::getStationId, station.getId())
|
|
|
- .list();
|
|
|
-
|
|
|
- BigDecimal nowPrice = BigDecimal.ZERO;
|
|
|
- String priceShow = "";
|
|
|
-
|
|
|
- for (StationTimePrice timePrice : timePrices) {
|
|
|
- if (isCurrentTimeInRange(timePrice.getTime())) {
|
|
|
- nowPrice = timePrice.getPrice();
|
|
|
- String timeType = switch (timePrice.getTimeType()) {
|
|
|
- case 1 -> "谷 ";
|
|
|
- case 2 -> "平 ";
|
|
|
- case 3 -> "峰 ";
|
|
|
- default -> "";
|
|
|
- };
|
|
|
- priceShow = timeType + timePrice.getTime();
|
|
|
-
|
|
|
- // 如果用户已登录,检查企业专享价
|
|
|
- if (cacheUser != null && cacheUser.getFirmId() != null) {
|
|
|
- FirmStationTimePrice firmPrice = firmStationTimePriceService.lambdaQuery()
|
|
|
- .eq(FirmStationTimePrice::getFirmId, cacheUser.getFirmId())
|
|
|
- .eq(FirmStationTimePrice::getStationId, station.getId())
|
|
|
- .eq(FirmStationTimePrice::getStartTime, timePrice.getStartTime())
|
|
|
- .eq(FirmStationTimePrice::getEndTime, timePrice.getEndTime())
|
|
|
- .one();
|
|
|
-
|
|
|
- if (firmPrice != null) {
|
|
|
- // 计算企业专享总价格
|
|
|
- BigDecimal firmElec = firmPrice.getElecPrice() != null ? firmPrice.getElecPrice() : BigDecimal.ZERO;
|
|
|
- BigDecimal firmService = firmPrice.getServicePrice() != null ? firmPrice.getServicePrice() : BigDecimal.ZERO;
|
|
|
- nowPrice = firmElec.add(firmService);
|
|
|
- }
|
|
|
- }
|
|
|
- break;
|
|
|
- }
|
|
|
- }
|
|
|
-
|
|
|
- stationMap.put("nowPrice", nowPrice);
|
|
|
- stationMap.put("priceShow", priceShow);
|
|
|
-
|
|
|
- resultList.add(stationMap);
|
|
|
- }
|
|
|
-
|
|
|
- // 排序
|
|
|
- switch (order) {
|
|
|
- case 0: // 按距离排序
|
|
|
- resultList.sort(Comparator.comparing(m -> ((Number) m.get("range")).doubleValue()));
|
|
|
- break;
|
|
|
- case 1: // 按空闲数量排序
|
|
|
- resultList.sort(Comparator.comparing(m -> ((Number) m.get("totalEmpty")).intValue(), Comparator.reverseOrder()));
|
|
|
- break;
|
|
|
- case 2: // 按电费排序
|
|
|
- resultList.sort(Comparator.comparing(m -> ((BigDecimal) m.get("nowPrice"))));
|
|
|
- break;
|
|
|
- }
|
|
|
-
|
|
|
- Map<String, Object> result = new HashMap<>();
|
|
|
- result.put("stations", resultList);
|
|
|
-
|
|
|
- // 检查用户是否有新用户优惠
|
|
|
- if (cacheUser != null) {
|
|
|
- Map<String, Object> discountInfo = checkUserDiscount(cacheUser);
|
|
|
- result.put("discountInfo", discountInfo);
|
|
|
- }
|
|
|
-
|
|
|
- return Result.success(result);
|
|
|
-
|
|
|
- } catch (Exception e) {
|
|
|
- log.error("查询充电站失败", e);
|
|
|
- return Result.failed("查询失败");
|
|
|
- }
|
|
|
- }
|
|
|
-
|
|
|
- /**
|
|
|
- * 获取充电站详情
|
|
|
- */
|
|
|
- @PostMapping("/getStationInfo")
|
|
|
- public Result<?> getStationInfo(@RequestBody Map<String, Object> data, HttpServletRequest request) {
|
|
|
- try {
|
|
|
- Long stationId = Long.valueOf(data.get("stationId").toString());
|
|
|
- ChargeStation station = chargeStationService.getById(stationId);
|
|
|
-
|
|
|
- if (station == null) {
|
|
|
- return Result.failed("充电站不存在");
|
|
|
- }
|
|
|
-
|
|
|
- Map<String, Object> result = new HashMap<>();
|
|
|
- result.put("station", station);
|
|
|
-
|
|
|
- // 查询充电桩列表
|
|
|
- List<ChargeDevice> devices = chargeDeviceService.lambdaQuery()
|
|
|
- .eq(ChargeDevice::getStationId, stationId)
|
|
|
- .list();
|
|
|
- result.put("devices", devices);
|
|
|
-
|
|
|
- // 查询价格时段
|
|
|
- List<StationTimePrice> timePrices = stationTimePriceService.lambdaQuery()
|
|
|
- .eq(StationTimePrice::getStationId, stationId)
|
|
|
- .orderByAsc(StationTimePrice::getTime)
|
|
|
- .list();
|
|
|
- result.put("timePrices", timePrices);
|
|
|
-
|
|
|
- return Result.success(result);
|
|
|
-
|
|
|
- } catch (Exception e) {
|
|
|
- log.error("查询充电站详情失败", e);
|
|
|
- return Result.failed("查询失败");
|
|
|
- }
|
|
|
- }
|
|
|
-
|
|
|
- /**
|
|
|
- * 获取充电桩详情
|
|
|
- */
|
|
|
- @PostMapping("/getDeviceInfo")
|
|
|
- public Result<?> getDeviceInfo(@RequestBody Map<String, Object> data, HttpServletRequest request) {
|
|
|
- UserInfo cacheUser = getUserFromToken(request);
|
|
|
-
|
|
|
- try {
|
|
|
- Long deviceId = Long.valueOf(data.get("deviceId").toString());
|
|
|
- ChargeDevice device = chargeDeviceService.getById(deviceId);
|
|
|
-
|
|
|
- if (device == null) {
|
|
|
- return Result.failed("充电桩不存在");
|
|
|
- }
|
|
|
-
|
|
|
- Map<String, Object> result = new HashMap<>();
|
|
|
- result.put("device", device);
|
|
|
-
|
|
|
- // 检查用户是否有新用户优惠
|
|
|
- if (cacheUser != null) {
|
|
|
- Map<String, Object> discountInfo = checkUserDiscount(cacheUser);
|
|
|
- result.put("discountInfo", discountInfo);
|
|
|
- }
|
|
|
-
|
|
|
- return Result.success(result);
|
|
|
-
|
|
|
- } catch (Exception e) {
|
|
|
- log.error("查询充电桩详情失败", e);
|
|
|
- return Result.failed("查询失败");
|
|
|
- }
|
|
|
- }
|
|
|
-
|
|
|
- /**
|
|
|
- * 开始充电
|
|
|
- * TODO: 需要对接充电桩硬件接口
|
|
|
- */
|
|
|
- @PostMapping("/startCharge")
|
|
|
- @Transactional(rollbackFor = Exception.class)
|
|
|
- public Result<?> startCharge(@RequestBody Map<String, Object> data, HttpServletRequest request) {
|
|
|
- UserInfo cacheUser = getUserFromToken(request);
|
|
|
- if (cacheUser == null) {
|
|
|
- return Result.failed("登录已过期");
|
|
|
- }
|
|
|
-
|
|
|
- try {
|
|
|
- Long deviceId = Long.valueOf(data.get("deviceId").toString());
|
|
|
-
|
|
|
- // TODO: 实现充电逻辑
|
|
|
- // 1. 检查充电桩状态
|
|
|
- // 2. 检查用户余额
|
|
|
- // 3. 创建充电订单
|
|
|
- // 4. 调用硬件接口启动充电
|
|
|
-
|
|
|
- log.warn("开始充电功能待实现,设备ID: {}", deviceId);
|
|
|
- return Result.failed("充电功能正在开发中");
|
|
|
-
|
|
|
- } catch (Exception e) {
|
|
|
- log.error("开始充电失败", e);
|
|
|
- return Result.failed("充电失败");
|
|
|
- }
|
|
|
- }
|
|
|
-
|
|
|
- /**
|
|
|
- * 停止充电
|
|
|
- * TODO: 需要对接充电桩硬件接口
|
|
|
- */
|
|
|
- @PostMapping("/stopCharge")
|
|
|
- @Transactional(rollbackFor = Exception.class)
|
|
|
- public Result<?> stopCharge(@RequestBody Map<String, Object> data, HttpServletRequest request) {
|
|
|
- UserInfo cacheUser = getUserFromToken(request);
|
|
|
- if (cacheUser == null) {
|
|
|
- return Result.failed("登录已过期");
|
|
|
- }
|
|
|
-
|
|
|
- try {
|
|
|
- Long orderId = Long.valueOf(data.get("orderId").toString());
|
|
|
-
|
|
|
- // TODO: 实现停止充电逻辑
|
|
|
- // 1. 查询充电订单
|
|
|
- // 2. 调用硬件接口停止充电
|
|
|
- // 3. 结算订单
|
|
|
- // 4. 扣费
|
|
|
-
|
|
|
- log.warn("停止充电功能待实现,订单ID: {}", orderId);
|
|
|
- return Result.failed("停止充电功能正在开发中");
|
|
|
-
|
|
|
- } catch (Exception e) {
|
|
|
- log.error("停止充电失败", e);
|
|
|
- return Result.failed("停止失败");
|
|
|
- }
|
|
|
- }
|
|
|
-
|
|
|
- /**
|
|
|
- * 查询充电订单列表
|
|
|
- */
|
|
|
- @PostMapping("/getChargeOrders")
|
|
|
- public Result<?> getChargeOrders(@RequestBody Map<String, Object> data, HttpServletRequest request) {
|
|
|
- UserInfo cacheUser = getUserFromToken(request);
|
|
|
- if (cacheUser == null) {
|
|
|
- return Result.failed("登录已过期");
|
|
|
- }
|
|
|
-
|
|
|
- try {
|
|
|
- List<ChargeOrderInfo> orders = chargeOrderInfoService.lambdaQuery()
|
|
|
- .eq(ChargeOrderInfo::getUserId, cacheUser.getId())
|
|
|
- .orderByDesc(ChargeOrderInfo::getCreateTime)
|
|
|
- .list();
|
|
|
-
|
|
|
- Map<String, Object> result = new HashMap<>();
|
|
|
- result.put("orders", orders);
|
|
|
- return Result.success(result);
|
|
|
-
|
|
|
- } catch (Exception e) {
|
|
|
- log.error("查询充电订单失败", e);
|
|
|
- return Result.failed("查询失败");
|
|
|
- }
|
|
|
- }
|
|
|
-
|
|
|
- /**
|
|
|
- * 获取用户账户信息
|
|
|
- */
|
|
|
- @PostMapping("/getUserAccount")
|
|
|
- public Result<?> getUserAccount(HttpServletRequest request) {
|
|
|
- UserInfo cacheUser = getUserFromToken(request);
|
|
|
- if (cacheUser == null) {
|
|
|
- return Result.failed("登录已过期");
|
|
|
- }
|
|
|
-
|
|
|
- try {
|
|
|
- UserAccount userAccount = userAccountService.lambdaQuery()
|
|
|
- .eq(UserAccount::getUserId, cacheUser.getId())
|
|
|
- .one();
|
|
|
-
|
|
|
- if (userAccount == null) {
|
|
|
- return Result.failed("账户不存在");
|
|
|
- }
|
|
|
-
|
|
|
- Map<String, Object> result = new HashMap<>();
|
|
|
- result.put("userAccount", userAccount);
|
|
|
- return Result.success(result);
|
|
|
-
|
|
|
- } catch (Exception e) {
|
|
|
- log.error("查询用户账户失败", e);
|
|
|
- return Result.failed("查询失败");
|
|
|
- }
|
|
|
- }
|
|
|
-
|
|
|
- /**
|
|
|
- * 获取用户集团账户信息
|
|
|
- */
|
|
|
- @PostMapping("/getEcUserAccount")
|
|
|
- public Result<?> getEcUserAccount(HttpServletRequest request) {
|
|
|
- UserInfo cacheUser = getUserFromToken(request);
|
|
|
- if (cacheUser == null) {
|
|
|
- return Result.failed("登录已过期");
|
|
|
- }
|
|
|
-
|
|
|
- try {
|
|
|
- // 查询集团用户账户
|
|
|
- EcUserAccount ecUserAccount = ecUserAccountService.lambdaQuery()
|
|
|
- .eq(EcUserAccount::getUserId, cacheUser.getId())
|
|
|
- .one();
|
|
|
-
|
|
|
- if (ecUserAccount == null) {
|
|
|
- return Result.failed("您不是集团用户");
|
|
|
- }
|
|
|
-
|
|
|
- // 查询集团信息
|
|
|
- EcInfo ecInfo = null;
|
|
|
- if (cacheUser.getEcId() != null) {
|
|
|
- ecInfo = ecInfoService.getById(cacheUser.getEcId());
|
|
|
- }
|
|
|
-
|
|
|
- Map<String, Object> result = new HashMap<>();
|
|
|
- result.put("ecUserAccount", ecUserAccount);
|
|
|
- result.put("ecInfo", ecInfo);
|
|
|
- return Result.success(result);
|
|
|
-
|
|
|
- } catch (Exception e) {
|
|
|
- log.error("查询集团账户失败", e);
|
|
|
- return Result.failed("查询失败");
|
|
|
- }
|
|
|
- }
|
|
|
-
|
|
|
- /**
|
|
|
- * 查询正在充电的订单
|
|
|
- */
|
|
|
- @PostMapping("/queryInChangeByUserId")
|
|
|
- public Result<?> queryInChangeByUserId(HttpServletRequest request) {
|
|
|
- UserInfo cacheUser = getUserFromToken(request);
|
|
|
- if (cacheUser == null) {
|
|
|
- return Result.failed("登录已过期");
|
|
|
- }
|
|
|
-
|
|
|
- try {
|
|
|
- // 查询充电中的订单(状态:1-充电中)
|
|
|
- ChargeOrderInfo orderInfo = chargeOrderInfoService.lambdaQuery()
|
|
|
- .eq(ChargeOrderInfo::getUserId, cacheUser.getId())
|
|
|
- .eq(ChargeOrderInfo::getStatus, 1)
|
|
|
- .one();
|
|
|
-
|
|
|
- Map<String, Object> result = new HashMap<>();
|
|
|
- if (orderInfo == null) {
|
|
|
- result.put("isChange", 0); // 没有订单
|
|
|
- } else {
|
|
|
- result.put("isChange", 1);
|
|
|
- result.put("orderInfo", orderInfo);
|
|
|
- }
|
|
|
- return Result.success(result);
|
|
|
-
|
|
|
- } catch (Exception e) {
|
|
|
- log.error("查询充电订单失败", e);
|
|
|
- return Result.failed("查询失败");
|
|
|
- }
|
|
|
- }
|
|
|
-
|
|
|
- /**
|
|
|
- * 查询用户订单列表(按时间和状态筛选)
|
|
|
- */
|
|
|
- @PostMapping("/queryOrderList")
|
|
|
- public Result<?> queryOrderList(@RequestBody Map<String, Object> data, HttpServletRequest request) {
|
|
|
- UserInfo cacheUser = getUserFromToken(request);
|
|
|
- if (cacheUser == null) {
|
|
|
- return Result.failed("登录已过期");
|
|
|
- }
|
|
|
-
|
|
|
- try {
|
|
|
- String startDate = data.get("startDate") != null ? data.get("startDate").toString() : null;
|
|
|
- String endDate = data.get("endDate") != null ? data.get("endDate").toString() : null;
|
|
|
- String type = data.get("type") != null ? data.get("type").toString() : "-1";
|
|
|
-
|
|
|
- // 构建查询
|
|
|
- var query = chargeOrderInfoService.lambdaQuery()
|
|
|
- .eq(ChargeOrderInfo::getUserId, cacheUser.getId());
|
|
|
-
|
|
|
- // 根据类型筛选状态
|
|
|
- if ("-1".equals(type)) {
|
|
|
- // 全部订单
|
|
|
- query.in(ChargeOrderInfo::getStatus, 0, 1, 2, 3);
|
|
|
- } else if ("1".equals(type)) {
|
|
|
- // 进行中订单(充电中、结算中)
|
|
|
- query.in(ChargeOrderInfo::getStatus, 1, 2);
|
|
|
- } else if ("2".equals(type)) {
|
|
|
- // 已完成订单
|
|
|
- query.eq(ChargeOrderInfo::getStatus, 3);
|
|
|
- }
|
|
|
-
|
|
|
- // 按时间筛选(如果需要)
|
|
|
- // TODO: 添加时间筛选逻辑
|
|
|
-
|
|
|
- List<ChargeOrderInfo> list = query.orderByDesc(ChargeOrderInfo::getCreateTime).list();
|
|
|
-
|
|
|
- Map<String, Object> result = new HashMap<>();
|
|
|
- result.put("list", list);
|
|
|
- result.put("total", list.size());
|
|
|
- return Result.success(result);
|
|
|
-
|
|
|
- } catch (Exception e) {
|
|
|
- log.error("查询订单列表失败", e);
|
|
|
- return Result.failed("查询失败");
|
|
|
- }
|
|
|
- }
|
|
|
-
|
|
|
- /**
|
|
|
- * 查询用户欠费订单
|
|
|
- */
|
|
|
- @PostMapping("/queryOrderList-arrearage")
|
|
|
- public Result<?> queryOrderListByArrearage(HttpServletRequest request) {
|
|
|
- UserInfo cacheUser = getUserFromToken(request);
|
|
|
- if (cacheUser == null) {
|
|
|
- return Result.failed("登录已过期");
|
|
|
- }
|
|
|
-
|
|
|
- try {
|
|
|
- // 查询待补缴的订单
|
|
|
- ChargeOrderInfo orderInfo = chargeOrderInfoService.lambdaQuery()
|
|
|
- .eq(ChargeOrderInfo::getUserId, cacheUser.getId())
|
|
|
- .eq(ChargeOrderInfo::getMaspStatus, 1) // 待补缴
|
|
|
- .one();
|
|
|
-
|
|
|
- if (orderInfo == null) {
|
|
|
- return Result.success(null);
|
|
|
- }
|
|
|
-
|
|
|
- return Result.success(orderInfo);
|
|
|
-
|
|
|
- } catch (Exception e) {
|
|
|
- log.error("查询欠费订单失败", e);
|
|
|
- return Result.failed("查询失败");
|
|
|
- }
|
|
|
- }
|
|
|
-
|
|
|
- /**
|
|
|
- * 查询集团信息
|
|
|
- */
|
|
|
- @PostMapping("/queryEcInfo")
|
|
|
- public Result<?> queryEcInfo(@RequestBody Map<String, Object> data, HttpServletRequest request) {
|
|
|
- try {
|
|
|
- Long ecId = Long.valueOf(data.get("ecId").toString());
|
|
|
- EcInfo ecInfo = ecInfoService.getById(ecId);
|
|
|
-
|
|
|
- Map<String, Object> result = new HashMap<>();
|
|
|
- result.put("ecInfo", ecInfo);
|
|
|
- return Result.success(result);
|
|
|
-
|
|
|
- } catch (Exception e) {
|
|
|
- log.error("查询集团信息失败", e);
|
|
|
- return Result.failed("查询失败");
|
|
|
- }
|
|
|
- }
|
|
|
-
|
|
|
- /**
|
|
|
- * 判断当前时间是否在时间段内
|
|
|
- */
|
|
|
- private boolean isCurrentTimeInRange(String timeRange) {
|
|
|
- try {
|
|
|
- String[] times = timeRange.split("-");
|
|
|
- if (times.length != 2) {
|
|
|
- return false;
|
|
|
- }
|
|
|
-
|
|
|
- DateTimeFormatter formatter = DateTimeFormatter.ofPattern("HH:mm");
|
|
|
- LocalTime now = LocalTime.now();
|
|
|
- LocalTime start = LocalTime.parse(times[0].trim(), formatter);
|
|
|
- LocalTime end = LocalTime.parse(times[1].trim(), formatter);
|
|
|
-
|
|
|
- if (start.isBefore(end)) {
|
|
|
- return !now.isBefore(start) && !now.isAfter(end);
|
|
|
- } else {
|
|
|
- // 跨天的情况
|
|
|
- return !now.isBefore(start) || !now.isAfter(end);
|
|
|
- }
|
|
|
- } catch (Exception e) {
|
|
|
- log.error("解析时间范围失败: {}", timeRange, e);
|
|
|
- return false;
|
|
|
- }
|
|
|
- }
|
|
|
-
|
|
|
- /**
|
|
|
- * 计算两点之间的距离(米)
|
|
|
- * 使用Haversine公式
|
|
|
- */
|
|
|
- private double calculateDistance(double lng1, double lat1, double lng2, double lat2) {
|
|
|
- double earthRadius = 6371000; // 地球半径(米)
|
|
|
- double dLat = Math.toRadians(lat2 - lat1);
|
|
|
- double dLng = Math.toRadians(lng2 - lng1);
|
|
|
- double a = Math.sin(dLat / 2) * Math.sin(dLat / 2) +
|
|
|
- Math.cos(Math.toRadians(lat1)) * Math.cos(Math.toRadians(lat2)) *
|
|
|
- Math.sin(dLng / 2) * Math.sin(dLng / 2);
|
|
|
- double c = 2 * Math.atan2(Math.sqrt(a), Math.sqrt(1 - a));
|
|
|
- return earthRadius * c;
|
|
|
- }
|
|
|
-
|
|
|
- /**
|
|
|
- * 格式化距离显示
|
|
|
- */
|
|
|
- private String formatDistance(double meters) {
|
|
|
- if (meters > 1000) {
|
|
|
- BigDecimal km = BigDecimal.valueOf(meters / 1000).setScale(2, RoundingMode.HALF_UP);
|
|
|
- return km + "km";
|
|
|
- } else {
|
|
|
- return (int) meters + "m";
|
|
|
- }
|
|
|
- }
|
|
|
-
|
|
|
- /**
|
|
|
- * 从请求头获取token并从缓存中获取用户信息
|
|
|
- */
|
|
|
- private UserInfo getUserFromToken(HttpServletRequest request) {
|
|
|
- String token = request.getHeader("token");
|
|
|
- if (StrUtil.isBlank(token)) {
|
|
|
- return null;
|
|
|
- }
|
|
|
-
|
|
|
- Object obj = redisTemplate.opsForValue().get(TOKEN_PREFIX + token);
|
|
|
- if (obj instanceof UserInfo) {
|
|
|
- // 刷新token过期时间
|
|
|
- redisTemplate.expire(TOKEN_PREFIX + token, 30, TimeUnit.MINUTES);
|
|
|
- return (UserInfo) obj;
|
|
|
- }
|
|
|
- return null;
|
|
|
- }
|
|
|
-
|
|
|
- /**
|
|
|
- * 检查用户是否有新用户优惠
|
|
|
- */
|
|
|
- private Map<String, Object> checkUserDiscount(UserInfo userInfo) {
|
|
|
- Map<String, Object> result = new HashMap<>();
|
|
|
- try {
|
|
|
- // 查询该用户是否有充电记录
|
|
|
- long chargeCount = chargeOrderInfoService.lambdaQuery()
|
|
|
- .eq(ChargeOrderInfo::getUserId, userInfo.getId())
|
|
|
- .eq(ChargeOrderInfo::getStatus, 3) // 已完成的订单
|
|
|
- .count();
|
|
|
-
|
|
|
- // 如果是新用户(没有充电记录),查询新用户优惠活动
|
|
|
- if (chargeCount == 0) {
|
|
|
- NewUserDiscount discount = newUserDiscountService.lambdaQuery()
|
|
|
- .eq(NewUserDiscount::getStatus, 1)
|
|
|
- .le(NewUserDiscount::getBeginTime, java.time.LocalDateTime.now())
|
|
|
- .ge(NewUserDiscount::getEndTime, java.time.LocalDateTime.now())
|
|
|
- .one();
|
|
|
-
|
|
|
- if (discount != null) {
|
|
|
- result.put("hasDiscount", true);
|
|
|
- result.put("discount", discount);
|
|
|
- return result;
|
|
|
- }
|
|
|
- }
|
|
|
-
|
|
|
- result.put("hasDiscount", false);
|
|
|
- return result;
|
|
|
-
|
|
|
- } catch (Exception e) {
|
|
|
- log.error("检查新用户优惠失败", e);
|
|
|
- result.put("hasDiscount", false);
|
|
|
- return result;
|
|
|
- }
|
|
|
- }
|
|
|
-}
|