fg_yida_2/YiDa_WinForm/Service/YiDaUploadService.cs

180 lines
7.5 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.Threading.Tasks;
using AlibabaCloud.SDK.Dingtalkoauth2_1_0.Models;
using Newtonsoft.Json;
using Tea;
using YiDa_WinForm;
using YiDa_WinForm.Config;
using YiDa_WinForm.Model;
using YiDa_WinForm.Service.Mqtt;
namespace MQTT_WinformV1.Service
{
public class YiDaUploadService
{
ButtonOperationService _buttonService;
// 钉钉token
static readonly string _tokenAppKey = AppConfig.YiDaTokenAppKey;
static readonly string _tokenAppSecret = AppConfig.YiDaTokenAppSecret;
// 宜搭上传配置
static readonly string _appType = AppConfig.YiDaAppType;
static readonly string _systemToken = AppConfig.YiDaSystemToken;
static readonly string _userId = AppConfig.YiDaUserId;
static readonly string _formUuid = AppConfig.YiDaFormUuid;
static readonly string _processCode = AppConfig.YiDaProcessCode;
/// <summary>
/// 创建 OAuth2 客户端(获取 token 用)
/// </summary>
/// <returns></returns>
private static AlibabaCloud.SDK.Dingtalkoauth2_1_0.Client CreateClient()
{
AlibabaCloud.OpenApiClient.Models.Config config = new AlibabaCloud.OpenApiClient.Models.Config();
config.Protocol = "https";
config.RegionId = "central";
return new AlibabaCloud.SDK.Dingtalkoauth2_1_0.Client(config);
}
/// <summary>
/// 获取钉钉 AccessToken
/// </summary>
/// <returns>AccessToken 字符串</returns>
public string GetDingDingToken()
{
string strToken = string.Empty;
GetAccessTokenResponse token = null;
var client = CreateClient();
var getAccessTokenRequest = new GetAccessTokenRequest
{
AppKey = _tokenAppKey,
AppSecret = _tokenAppSecret,
};
try
{
token = client.GetAccessToken(getAccessTokenRequest);
}
catch (TeaException err)
{
string errorMsg = $"获取钉钉Token异常【TeaException】错误码={err.Code},错误信息={err.Message}";
Console.WriteLine(errorMsg);
}
catch (Exception ex)
{
string errorMsg = $"获取 token 异常:{ex.Message}";
Console.WriteLine(errorMsg);
}
return token != null ? token.Body.AccessToken : string.Empty;
}
//加上逻辑判断的上传
//记载宜搭上传成功日志
private async Task RecordSuccessLog(List<MqttModel> mqttLists)
{
if (mqttLists != null && mqttLists.Count > 0)
{
var service = new MqttClientService();
await _buttonService.CreateSuccessLog(mqttLists);
}
}
//新版宜搭API
private static AlibabaCloud.SDK.Dingtalkyida_2_0.Client CreateYiDaClient()
{
AlibabaCloud.OpenApiClient.Models.Config config = new AlibabaCloud.OpenApiClient.Models.Config();
config.Protocol = "https";
config.RegionId = "central";
return new AlibabaCloud.SDK.Dingtalkyida_2_0.Client(config);
}
public void UploadDatabaseDataToYiDaWithLogging(string accessToken, List<YiDaModel> yidaLists, List<MqttModel> mqttLists, MainForm form)
{
var client = CreateYiDaClient();
AlibabaCloud.SDK.Dingtalkyida_2_0.Models.StartInstanceHeaders startInstanceHeaders = new AlibabaCloud.SDK.Dingtalkyida_2_0.Models.StartInstanceHeaders();
startInstanceHeaders.XAcsDingtalkAccessToken = accessToken;
for (int i = 0; i < yidaLists.Count; i++)
{
var item = yidaLists[i];
var mqttItem = mqttLists[i];
var dataDict = new Dictionary<string, object>
{
//左侧为宜搭表中字段唯一标识值,右侧为要传递给宜搭的值。
{ "textField_mha98neu", item.textField_mha98neu },//供应商代码
{ "textField_mha98nev", item.textField_mha98nev },//供应商名称
{ "textField_mha98new", item.textField_mha98new },//车型
{ "textField_mha98nex", item.textField_mha98nex },//零件号
{ "textField_mha98ney", item.textField_mha98ney },//零件名称
{ "textField_mha98nf1", item.textField_mha98nf1 },//参数名
{ "textField_mhx44i2i", item.textField_mhx44i2i },//参数值
{ "textField_mhx44i2j", item.textField_mhx44i2j },//下公差
{ "textField_mhx44i2k", item.textField_mhx44i2k },//上公差
{ "textField_mha98nf7", item.textField_mha98nf7 },//零件责任人
{ "textField_mhlvt8ht", DateTime.Now.ToString("yyyy-MM-dd") },//写入时间
{ "textField_mha98nf5", item.textField_mha98nf5 },//合格判断
{ "textField_mha98nf0", item.textField_mha98nf0 },//工位
{ "textField_mha98nfh", item.textField_mha98nfh }//外保负责人
};
string jsonData = JsonConvert.SerializeObject(dataDict);
AlibabaCloud.SDK.Dingtalkyida_2_0.Models.StartInstanceRequest startInstanceRequest = new AlibabaCloud.SDK.Dingtalkyida_2_0.Models.StartInstanceRequest
{
AppType = _appType,
SystemToken = _systemToken,
UserId = _userId,
Language = "zh_CN",
FormUuid = _formUuid,
FormDataJson = jsonData,
ProcessCode = _processCode,
};
try
{
var response = client.StartInstanceWithOptions(startInstanceRequest, startInstanceHeaders, new AlibabaCloud.TeaUtil.Models.RuntimeOptions());
if (response != null && response.Body != null)
{
// 上传成功就存入 MySQL
RecordSuccessLog(new List<MqttModel> { mqttItem });
string logData = JsonConvert.SerializeObject(dataDict, Formatting.Indented);
form.AppendLog($"上传数据成功:\r\n{logData}");
Console.WriteLine($"上传数据成功:\r\n{logData}");
}
else
{
Console.WriteLine($"上传返回空结果,数据未存入 MySQL。");
string logData = JsonConvert.SerializeObject(dataDict, Formatting.Indented);
form.AppendLog($"上传返回空结果,数据未存入 MySQL:\r\n{logData}");
}
}
catch (TeaException err)
{
Console.WriteLine($"上传异常:{err.Code} --- {err.Message}");
string logData = JsonConvert.SerializeObject(dataDict, Formatting.Indented);
form.AppendLog($"上传异常:{err.Code} --- {err.Message}\r\n数据:\r\n{logData}");
}
catch (Exception ex)
{
Console.WriteLine($"上传异常:{ex.Message}");
string logData = JsonConvert.SerializeObject(dataDict, Formatting.Indented);
form.AppendLog($"上传异常:{ex.Message}\r\n数据:\r\n{logData}");
}
}
}
}
}