CHEN-ZW\acer e24ee57355 1
2025-09-23 16:38:40 +08:00

341 lines
12 KiB
C#
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

using System;
using System.Collections.Generic;
using System.IO;
using System.Net.Http;
using System.Net;
using System.Threading;
using System.Threading.Tasks;
using Newtonsoft.Json;
using SWS.Commons;
using SWS.Model;
using System.Collections;
using System.Runtime.InteropServices.ComTypes;
namespace SWS.Service
{
public class WireGroupService : HttpService
{
public WireGroupService() : base()
{
}
/// <summary>
/// 获取所有的信号
/// </summary>
/// <param name="projId"></param>
/// <param name="Assigned"></param>
/// <returns></returns>
public async Task<List<ec_Wire_Group>> GetSignals(string projId, bool Assigned)
{
var res = await this.GetAsync<List<ec_Wire_Group>>($"WireGroupApi/GetSignals?projId={projId}&Assigned={Assigned}");
if (res.code == 200)
{
return res.data;
}
else
{
}
return null;
}
/// <summary>
/// 获取当前组可用的编码
/// </summary>
/// <param name="group"></param>
/// <returns></returns>
public async Task<string> GetNextAvailableSeq(string group)
{
var res = await this.GetAsync<object>($"WireGroupApi/GetNextAvailableSeq?projId={GlobalObject.curProject?.ProjectId}&group={group}");
if (res.code == 200)
{
return res.info;
}
else
{
}
return null;
}
/// <summary>
/// 验证是否可以保存
/// </summary>
/// <param name="entity"></param>
/// <returns></returns>
public async Task<string> CanSaveSignals(WireGroups entity ,Share.Action ActionType)
{
var res = await this.PostBodyAsync<List<ec_Wire_Group>, WireGroups>($"WireGroupApi/CanSaveSignals?projId={GlobalObject.curProject?.ProjectId}&ActionType={ActionType}", entity);
if (res.code == 200)
{
return "OK";
}
else
{
return res.info;
}
}
/// <summary>
/// 保存
/// </summary>
/// <param name="entity"></param>
/// <returns></returns>
public async Task<object> SaveSignals(WireGroups entity)
{
var res = await this.PostBodyAsync<List<ec_Wire_Group>, WireGroups>($"WireGroupApi/SaveSignals?projId={GlobalObject.curProject?.ProjectId}&mode=0", entity);
//if (res.code == 200)
//{
// return res.data;
//}
//else if (res.code ==400)
//{
// return res.info;
//}
return res;
}
/// <summary>
/// 查询信号信息
/// </summary>
/// <param name="entity"></param>
/// <returns></returns>
public async Task<List<ec_wire_group_notice>> GetNotification()
{
var res = await this.GetAsync<List<ec_wire_group_notice>>($"WireGroupApi/GetNotification?projId={GlobalObject.curProject?.ProjectId}");
if (res.code == 200)
{
return res.data;
}
else
{
}
return null;
}
/// <summary>
/// 查询信号历史
/// </summary>
/// <param name="projId">项目ID</param>
/// <param name="wireGroupIds">信号ID多个用都逗号隔开</param>
/// <returns></returns>
public async Task<List<ec_wire_group_propertyhis>> GetSignalPropertyhis(string wireGroupIds)
{
var res = await this.GetAsync<List<ec_wire_group_propertyhis>>($"WireGroupApi/GetSignalPropertyhis?projId={GlobalObject.curProject?.ProjectId}&wireGroupIds={wireGroupIds}");
if (res.code == 200)
{
return res.data;
}
else
{
}
return null;
}
/// <summary>
/// 查询信号日志
/// </summary>
/// <param name="projId">项目ID</param>
/// <param name="wireGroupIds">信号ID多个用都逗号隔开</param>
/// <returns></returns>
public async Task<List<ec_wire_group_log>> GetSignalLogs(string wireGroupIds)
{
var res = await this.GetAsync<List<ec_wire_group_log>>($"WireGroupApi/GetSignalLogs?projId={GlobalObject.curProject?.ProjectId}&wireGroupIds={wireGroupIds}");
if (res.code == 200)
{
return res.data;
}
else
{
}
return null;
}
/// <summary>
/// 读信号通知
/// </summary>
/// <param name="NoticeIds"></param>
/// <returns></returns>
public async Task<List<ec_wire_group_notice>> ReadNotification(List<string> NoticeIds)
{
var res = await this.PostBodyAsync<List<ec_wire_group_notice>,List<string>>($"WireGroupApi/ReadNotification?projId={GlobalObject.curProject?.ProjectId}", NoticeIds);
if (res.code == 200)
{
return res.data;
}
else
{
}
return null;
}
/// <summary>
/// 获取全部输出信号的情况
/// </summary>
/// <returns></returns>
public async Task<List<signalGroup>> GetGroupInfo()
{
var res = await this.GetAsync<List<signalGroup>>($"WireGroupApi/GetGroupInfo?projId={GlobalObject.curProject?.ProjectId}");
if (res.code == 200)
{
return res.data;
}
else
{
}
return null;
}
/// <summary>
/// 下载指定时间范围内的信号历史记录的excel
/// </summary>
/// <param name="start"></param>
/// <param name="end"></param>
/// <param name="delete"></param>
/// <returns></returns>
public async Task<List<ec_wire_group_log>> Exportchanges(DateTime start, DateTime end, bool delete = false)
{
var res = await this.GetAsync<List<ec_wire_group_log>>($"WireGroupApi/Exportchanges?projId={GlobalObject.curProject?.ProjectId}&start={start}&end={end}&delete={delete}");
if (res.code == 200)
{
return res.data;
}
else
{
}
return null;
}
public async Task<string> Exportchanges(
string savePath,
DateTime start, DateTime end, bool delete = false,
IProgress<double> progress = null,
CancellationToken cancellationToken = default)
{
try
{
string startValue = start.ToString("yyyy-MM-dd");
string endValue = end.ToString("yyyy-MM-dd");
var p = Path.GetDirectoryName(savePath);
if (!Directory.Exists(p))
Directory.CreateDirectory(p);
string url = $"WireGroupApi/ExportChanges?projectId={GlobalObject.curProject?.ProjectId}&start={startValue}&end={endValue}&delete={delete}";
//url = Uri.EscapeDataString(url);
// 发送请求并获取响应
using (var response = await GlobalObject.client.GetAsync(
url,
HttpCompletionOption.ResponseHeadersRead,
cancellationToken))
{
if (response.StatusCode != HttpStatusCode.OK)
{
string errorMsg = $"服务器地址 [{url}] 文件下载失败, 返回HTTP代码" + response.StatusCode;
LoggerHelper.Current.Error(errorMsg);
throw new HttpRequestException(errorMsg);
}
response.EnsureSuccessStatusCode(); // 确保响应成功
var result = await response.Content.ReadAsStringAsync();
learunHttpRes<object> resultObj = null;
try
{
resultObj = JsonConvert.DeserializeObject<learunHttpRes<object>>(result);
}
catch (JsonException)
{
//对下载文件来说如果正常的话这里反而是合理的因为返回的就是stream的东西不能被解析为learunHttpRes
await HandleStreamAsync();
return "";
}
if (resultObj.code != 200)
{
switch (resultObj.code)
{
case 400:
case 500:
return resultObj.info.ToString(); //业务错误不是http本质错误
default:
string errorMsg = $"服务器地址 [{url}] Get失败, 返回自定义代码:" + resultObj.code;
LoggerHelper.Current.Error(errorMsg);
throw new HttpRequestException(errorMsg);
}
}
else
{
await HandleStreamAsync();
return "";
}
async Task HandleStreamAsync()
{
#region core
// 获取文件总大小(可能为 null
var totalBytes = response.Content.Headers.ContentLength;
// 创建文件流
using (var contentStream = await response.Content.ReadAsStreamAsync())
using (var fileStream = new FileStream(
savePath,
FileMode.Create,
FileAccess.Write,
FileShare.None,
bufferSize: 8192,
useAsync: true))
{
var buffer = new byte[8192];
var totalBytesRead = 0L;
var bytesRead = 0;
// 分块读取并写入文件
while ((bytesRead = await contentStream.ReadAsync(
buffer,
0,
buffer.Length,
cancellationToken)) > 0)
{
await fileStream.WriteAsync(
buffer,
0,
bytesRead,
cancellationToken);
totalBytesRead += bytesRead;
// 报告进度(如果有总大小)
if (totalBytes.HasValue)
{
var percent = (double)totalBytesRead / totalBytes.Value * 100;
progress?.Report(percent);
}
}
}
#endregion
}
}
}
catch (HttpRequestException ex)
{
return ex.Message;
//throw new Exception($"下载请求失败: {ex.Message}");
}
catch (TaskCanceledException ex2)
{
return ex2.Message;
// 取消操作时删除已下载的部分文件
//if (File.Exists(savePath)) File.Delete(savePath);
//throw;
}
catch (Exception ex3)
{
return ex3.Message;
//throw new Exception($"下载失败: {ex.Message}");
}
}
}
}