Commit 3c705a127c7050a4b10adfd4051cf50da2c57b57

Authored by “wangming”
1 parent a0ec3cdb

新增无人机充电状态更新功能,支持手动和定时自动更新超过30分钟的充电状态为“充电完成”,并在状态枚举中添加“充电完成”选项。

netcore/src/Modularity/Extend/NCC.Extend.Entitys/Enums/DeviceCellStatusEnum.cs
@@ -34,5 +34,11 @@ namespace NCC.Extend.Entitys.Enums @@ -34,5 +34,11 @@ namespace NCC.Extend.Entitys.Enums
34 /// </summary> 34 /// </summary>
35 [Description("已租接")] 35 [Description("已租接")]
36 已租接 = 5, 36 已租接 = 5,
  37 +
  38 + /// <summary>
  39 + /// 充电完成
  40 + /// </summary>
  41 + [Description("充电完成")]
  42 + 充电完成 = 6,
37 } 43 }
38 } 44 }
39 \ No newline at end of file 45 \ No newline at end of file
netcore/src/Modularity/Extend/NCC.Extend/UavDeviceService.cs
@@ -684,7 +684,6 @@ namespace NCC.Extend.UavDevice @@ -684,7 +684,6 @@ namespace NCC.Extend.UavDevice
684 } 684 }
685 #endregion 685 #endregion
686 686
687 -  
688 #region 机柜获取MQTT配置 687 #region 机柜获取MQTT配置
689 /// <summary> 688 /// <summary>
690 /// 机柜获取MQTT配置 689 /// 机柜获取MQTT配置
@@ -989,5 +988,101 @@ namespace NCC.Extend.UavDevice @@ -989,5 +988,101 @@ namespace NCC.Extend.UavDevice
989 return PageResult<UavDeviceListOutput>.SqlSugarPageResult(data); 988 return PageResult<UavDeviceListOutput>.SqlSugarPageResult(data);
990 } 989 }
991 #endregion 990 #endregion
  991 +
  992 + #region 修改时间超个半小时的无人机改为充电完成
  993 +
  994 + /// <summary>
  995 + /// 将超过30分钟充电状态的无人机格子状态改为充电完成
  996 + /// </summary>
  997 + /// <returns></returns>
  998 + [HttpPost("UpdateChargingStatusToComplete")]
  999 + public async Task<dynamic> UpdateChargingStatusToComplete()
  1000 + {
  1001 + try
  1002 + {
  1003 + // 计算30分钟前的时间
  1004 + var thirtyMinutesAgo = DateTime.Now.AddMinutes(-30);
  1005 +
  1006 + // 查询状态为充电且更新时间超过30分钟的无人机格子
  1007 + var chargingCells = await _db.Queryable<UavDeviceCellEntity>().Where(x => x.Status == DeviceCellStatusEnum.充电.GetHashCode() && x.UpdateTime.HasValue &&
  1008 + x.UpdateTime.Value < thirtyMinutesAgo)
  1009 + .ToListAsync();
  1010 +
  1011 + if (chargingCells == null || chargingCells.Count == 0)
  1012 + {
  1013 + return new { success = true, message = "没有需要更新的充电状态格子", updatedCount = 0 };
  1014 + }
  1015 +
  1016 + // 批量更新状态为充电完成
  1017 + var updateCount = await _db.Updateable<UavDeviceCellEntity>()
  1018 + .SetColumns(x => new UavDeviceCellEntity
  1019 + {
  1020 + Status = DeviceCellStatusEnum.充电完成.GetHashCode()
  1021 + })
  1022 + .Where(x => chargingCells.Select(c => c.Id).Contains(x.Id))
  1023 + .ExecuteCommandAsync();
  1024 +
  1025 + Log.Information($"成功将 {updateCount} 个充电状态超过30分钟的无人机格子状态更新为充电完成");
  1026 +
  1027 + return new
  1028 + {
  1029 + success = true,
  1030 + message = $"成功更新 {updateCount} 个无人机格子状态为充电完成",
  1031 + updatedCount = updateCount,
  1032 + updatedCells = chargingCells.Select(x => new { id = x.Id, cellCode = x.CellCode, deviceId = x.DeviceId })
  1033 + };
  1034 + }
  1035 + catch (Exception ex)
  1036 + {
  1037 + Log.Error($"更新充电状态失败: {ex.Message}");
  1038 + return new { success = false, message = $"更新失败: {ex.Message}", updatedCount = 0 };
  1039 + }
  1040 + }
  1041 +
  1042 + /// <summary>
  1043 + /// 定时任务:自动更新超过30分钟的充电状态
  1044 + /// </summary>
  1045 + /// <returns></returns>
  1046 + [HttpPost("AutoUpdateChargingStatus")]
  1047 + public async Task<dynamic> AutoUpdateChargingStatus()
  1048 + {
  1049 + try
  1050 + {
  1051 + // 计算30分钟前的时间
  1052 + var thirtyMinutesAgo = DateTime.Now.AddMinutes(-30);
  1053 +
  1054 + // 查询并更新状态
  1055 + var updateCount = await _db.Updateable<UavDeviceCellEntity>()
  1056 + .SetColumns(x => new UavDeviceCellEntity
  1057 + {
  1058 + Status = DeviceCellStatusEnum.充电完成.GetHashCode(),
  1059 + UpdateTime = DateTime.Now
  1060 + })
  1061 + .Where(x => x.Status == DeviceCellStatusEnum.充电.GetHashCode() &&
  1062 + x.UpdateTime.HasValue &&
  1063 + x.UpdateTime.Value < thirtyMinutesAgo)
  1064 + .ExecuteCommandAsync();
  1065 +
  1066 + if (updateCount > 0)
  1067 + {
  1068 + Log.Information($"定时任务:自动将 {updateCount} 个充电状态超过30分钟的无人机格子状态更新为充电完成");
  1069 + }
  1070 +
  1071 + return new
  1072 + {
  1073 + success = true,
  1074 + message = $"自动更新完成,共更新 {updateCount} 个格子",
  1075 + updatedCount = updateCount,
  1076 + updateTime = DateTime.Now
  1077 + };
  1078 + }
  1079 + catch (Exception ex)
  1080 + {
  1081 + Log.Error($"自动更新充电状态失败: {ex.Message}");
  1082 + return new { success = false, message = $"自动更新失败: {ex.Message}", updatedCount = 0 };
  1083 + }
  1084 + }
  1085 +
  1086 + #endregion
992 } 1087 }
993 } 1088 }