238 lines
9.4 KiB
C#
238 lines
9.4 KiB
C#
using AlibabaCloud.SDK.Dingtalkyida_1_0.Models;
|
|
using Infrastructure.Attribute;
|
|
using Microsoft.AspNetCore.Mvc.RazorPages;
|
|
using Microsoft.Extensions.DependencyInjection;
|
|
using SqlSugar.IOC;
|
|
using System;
|
|
using System.Collections;
|
|
using System.Collections.Generic;
|
|
using System.Linq;
|
|
using System.Text;
|
|
using System.Threading.Tasks;
|
|
using Tea;
|
|
using DOAN.Model;
|
|
using DOAN.Model.MES.upload;
|
|
using DOAN.Model.System;
|
|
using DOAN.Repository;
|
|
using DOAN.Service.System.IService;
|
|
using Tea;
|
|
using AlibabaCloud.SDK.Dingtalkoauth2_1_0.Models;
|
|
using JinianNet.JNTemplate.Nodes;
|
|
using Newtonsoft.Json.Linq;
|
|
using AlibabaCloud.SDK.Dingtalkyida_1_0.Models;
|
|
using NLog;
|
|
using Microsoft.AspNetCore.Http.HttpResults;
|
|
|
|
namespace DOAN.Service.quality.upload.service
|
|
{
|
|
|
|
[AppService(ServiceType = typeof(IUploadServive), ServiceLifetime = LifeTime.Transient)]
|
|
public class UploadService : BaseService<QcYidaExcel>, IUploadServive
|
|
{
|
|
private readonly NLog.Logger logger = NLog.LogManager.GetCurrentClassLogger();
|
|
/// <summary>
|
|
/// 获取上传数据
|
|
/// </summary>
|
|
/// <param name="partnumber"></param>
|
|
/// <param name="workstation"></param>
|
|
/// <param name="paramter"></param>
|
|
/// <param name="pager"></param>
|
|
/// <returns></returns>
|
|
/// <exception cref="NotImplementedException"></exception>
|
|
public PagedInfo<QcYidaExcel> SelectExcelList(string partnumber, string workstation, string paramter, PagerInfo pager)
|
|
{
|
|
|
|
|
|
var exp = Expressionable.Create<QcYidaExcel>()
|
|
.AndIF(!string.IsNullOrEmpty(partnumber), it => it.Partnumber.Contains(partnumber))
|
|
.AndIF(!string.IsNullOrEmpty(workstation), it => it.Partnumber.Contains(workstation))
|
|
.AndIF(!string.IsNullOrEmpty(paramter), it => it.Partnumber.Contains(paramter))
|
|
.ToExpression();
|
|
var query = Context.Queryable<QcYidaExcel>().Where(exp);
|
|
|
|
return query.ToPage(pager);
|
|
}
|
|
|
|
/// <summary>
|
|
/// 保存
|
|
/// </summary>
|
|
/// <param name="qcYidas"></param>
|
|
/// <returns></returns>
|
|
public int GOcloud(List<QcYidaExcel> qcYidas,string CreatedBy)
|
|
{
|
|
foreach(var item in qcYidas)
|
|
{
|
|
item.Id = SnowFlakeSingle.Instance.NextId(); ;
|
|
item.CreatedBy = CreatedBy;
|
|
item.CreatedTime= DateTime.Now;
|
|
item.UpdatedTime = DateTime.Now;
|
|
Context.Insertable(item).ExecuteCommand();
|
|
}
|
|
int reuslt = dataUpLoad(qcYidas).Result;
|
|
return reuslt;
|
|
}
|
|
|
|
|
|
/// <summary>
|
|
/// 异步任务 数据上传
|
|
/// </summary>
|
|
/// <returns></returns>
|
|
public Task<int> dataUpLoad(List<QcYidaExcel> qcYidas)
|
|
{
|
|
return Task.Run(() =>
|
|
{
|
|
int flag = 0;
|
|
//2.上传到宜搭
|
|
//2.1 获取token
|
|
GetAccessTokenResponse tokenResponse = GetDINGIDNG_token();
|
|
//2.2 上传数据
|
|
if (qcYidas != null && qcYidas.Count > 0)
|
|
{
|
|
foreach (QcYidaExcel item in qcYidas)
|
|
{
|
|
|
|
QcYidaForm form = new QcYidaForm();
|
|
form.textField_ls3yl76o = item.SupplierCode;
|
|
form.textField_ls3yl76n=item.SupplierName;
|
|
form.textField_ls3yl76p = item.Vehicle;
|
|
form.textField_ls3yl76q = item.Partnumber;
|
|
form.textField_ls3yl76r = item.Partname;
|
|
form.textField_ls3yl76s = item.Config;
|
|
form.textField_ls3yl76t = item.Workstation;
|
|
form.textField_ls3yl76u = item.Partname;
|
|
form.numberField_ls4040pf = item.Value;
|
|
form.numberField_ls3yl76z = item.LowLimit;
|
|
form.numberField_ls3yl76y = item.UpLimit;
|
|
|
|
form.dateField_ls3yl770 = (long)((DateTime.Now).ToUniversalTime().Subtract(new DateTime(1970, 1, 1))).TotalMilliseconds;
|
|
form.textField_ls3yw11j = "徐惠栋";
|
|
SaveFormDataResponse response = UpdateTable(tokenResponse.Body.AccessToken.ToString(), JsonConvert.SerializeObject(form));
|
|
|
|
if (response != null && response.StatusCode == 200)
|
|
{
|
|
flag = 1;
|
|
|
|
}
|
|
}
|
|
}
|
|
return flag;
|
|
});
|
|
|
|
}
|
|
|
|
/// <summary>
|
|
/// 获取钉钉的token
|
|
/// </summary>
|
|
/// <returns></returns>
|
|
public GetAccessTokenResponse GetDINGIDNG_token()
|
|
{
|
|
GetAccessTokenResponse token = null;
|
|
|
|
AlibabaCloud.SDK.Dingtalkoauth2_1_0.Client client = CreateClient();
|
|
AlibabaCloud.SDK.Dingtalkoauth2_1_0.Models.GetAccessTokenRequest getAccessTokenRequest = new AlibabaCloud.SDK.Dingtalkoauth2_1_0.Models.GetAccessTokenRequest
|
|
{
|
|
AppKey = "dingsot65fcqc1uvci5d",
|
|
AppSecret = "sd77u-_thX6wqq9BIZmIgv8j7P0lrhstTuoGmvqQc8whoRf3Cmof7iCU3sVTpSBa",
|
|
};
|
|
try
|
|
{
|
|
token = client.GetAccessToken(getAccessTokenRequest);
|
|
|
|
|
|
}
|
|
catch (TeaException err)
|
|
{
|
|
if (!AlibabaCloud.TeaUtil.Common.Empty(err.Code) && !AlibabaCloud.TeaUtil.Common.Empty(err.Message))
|
|
{
|
|
// err 中含有 code 和 message 属性,可帮助开发定位问题
|
|
logger.Error($"token获取异常" + err.Code + "---" + err.Message);
|
|
}
|
|
}
|
|
catch (Exception _err)
|
|
{
|
|
TeaException err = new TeaException(new Dictionary<string, object>
|
|
{
|
|
{ "message", _err.Message }
|
|
});
|
|
if (!AlibabaCloud.TeaUtil.Common.Empty(err.Code) && !AlibabaCloud.TeaUtil.Common.Empty(err.Message))
|
|
{
|
|
// err 中含有 code 和 message 属性,可帮助开发定位问题
|
|
logger.Error($"token获取异常" + err.Code + "---" + err.Message);
|
|
}
|
|
}
|
|
|
|
return token;
|
|
|
|
}
|
|
/// <summary>
|
|
/// 更新宜搭表单
|
|
/// </summary>
|
|
/// <param name="token">token</param>
|
|
/// <param name="data_json">json形式的数据</param>
|
|
public SaveFormDataResponse UpdateTable(string token, string data_json)
|
|
{
|
|
SaveFormDataResponse response = null;
|
|
|
|
|
|
|
|
AlibabaCloud.SDK.Dingtalkyida_1_0.Client client = CreateClient_2();
|
|
AlibabaCloud.SDK.Dingtalkyida_1_0.Models.SaveFormDataHeaders saveFormDataHeaders = new AlibabaCloud.SDK.Dingtalkyida_1_0.Models.SaveFormDataHeaders();
|
|
saveFormDataHeaders.XAcsDingtalkAccessToken = token;
|
|
AlibabaCloud.SDK.Dingtalkyida_1_0.Models.SaveFormDataRequest saveFormDataRequest = new AlibabaCloud.SDK.Dingtalkyida_1_0.Models.SaveFormDataRequest
|
|
{
|
|
AppType = "APP_RHJKY5Q92THNXG742HQ4",
|
|
SystemToken = "CA966X91WS5IXAJL87YTB70RM9YA3975GY3SL2A",
|
|
UserId = "512960355835054196",
|
|
Language = "zh_CN",
|
|
FormUuid = "FORM-DB37916ED4A147ACBF3FCEA4421EA506TZ4P",
|
|
FormDataJson = data_json,
|
|
};
|
|
try
|
|
{
|
|
response = client.SaveFormDataWithOptions(saveFormDataRequest, saveFormDataHeaders, new AlibabaCloud.TeaUtil.Models.RuntimeOptions());
|
|
|
|
}
|
|
catch (TeaException err)
|
|
{
|
|
Console.WriteLine(err.ToString());
|
|
if (!AlibabaCloud.TeaUtil.Common.Empty(err.Code) && !AlibabaCloud.TeaUtil.Common.Empty(err.Message))
|
|
{
|
|
// err 中含有 code 和 message 属性,可帮助开发定位问题
|
|
logger.Error($"token获取异常" + err.Code + "---" + err.Message);
|
|
}
|
|
}
|
|
catch (Exception _err)
|
|
{
|
|
TeaException err = new TeaException(new Dictionary<string, object>
|
|
{
|
|
{ "message", _err.Message }
|
|
});
|
|
if (!AlibabaCloud.TeaUtil.Common.Empty(err.Code) && !AlibabaCloud.TeaUtil.Common.Empty(err.Message))
|
|
{
|
|
// err 中含有 code 和 message 属性,可帮助开发定位问题
|
|
logger.Error($"token获取异常" + err.Code + "---" + err.Message);
|
|
}
|
|
}
|
|
|
|
return response;
|
|
}
|
|
|
|
|
|
public 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);
|
|
}
|
|
|
|
public static AlibabaCloud.SDK.Dingtalkyida_1_0.Client CreateClient_2()
|
|
{
|
|
AlibabaCloud.OpenApiClient.Models.Config config = new AlibabaCloud.OpenApiClient.Models.Config();
|
|
config.Protocol = "https";
|
|
config.RegionId = "central";
|
|
return new AlibabaCloud.SDK.Dingtalkyida_1_0.Client(config);
|
|
}
|
|
}
|
|
}
|