using AlibabaCloud.SDK.Dingtalkworkflow_1_0.Models; using AlibabaCloud.SDK.Dingtalkyida_1_0.Models; using AlibabaCloud.TeaUtil.Models; using MES.Service.DB; using MES.Service.Dto.webApi.DeliveryNote; using MES.Service.Dto.webApi.ProductionOrder; using MES.Service.Modes.DeliveryNote; using MES.Service.Modes.ProductionOrder; using MES.Service.service; using SqlSugar; using System; using System.Collections.Generic; using System.Diagnostics; using System.Linq; using Tea; public class ProductionOrderManager : Repository { /** * 初始化账号Client,用于获取token * @return Client * @throws Exception */ public static AlibabaCloud.SDK.Dingtalkoauth2_1_0.Client CreateClient1() { 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); } /** * 获取钉钉的 Access Token * @return string 返回获取到的 Access Token */ public string GetToken() { AlibabaCloud.SDK.Dingtalkoauth2_1_0.Client client = CreateClient1(); AlibabaCloud.SDK.Dingtalkoauth2_1_0.Models.GetAccessTokenRequest getAccessTokenRequest = new AlibabaCloud.SDK.Dingtalkoauth2_1_0.Models.GetAccessTokenRequest { AppKey = "dingyzos0r1bizj7g6lr", AppSecret = "-HP4RvK2OUbqhG3iBUpd_TPe5MZRj8cfLc0b8Skt8rhC3I38kVLY9SS8P3kLWFcH", }; try { var response = client.GetAccessToken(getAccessTokenRequest); return response.Body.AccessToken; // 返回获取到的 Access Token } catch (TeaException err) { Console.WriteLine($"TeaException: Code={err.Code}, Message={err.Message}"); return null; } catch (Exception _err) { Console.WriteLine($"Exception: {_err.Message}"); return null; } } /** * 初始化账号Client,用于OA审批相关功能 * @return Client * @throws Exception */ public static AlibabaCloud.SDK.Dingtalkworkflow_1_0.Client CreateClientOa() { AlibabaCloud.OpenApiClient.Models.Config config = new AlibabaCloud.OpenApiClient.Models.Config(); config.Protocol = "https"; config.RegionId = "central"; return new AlibabaCloud.SDK.Dingtalkworkflow_1_0.Client(config); } public string GetSpaceId() { string accessToken = GetToken(); if (string.IsNullOrEmpty(accessToken)) { return "获取 Access Token 失败"; } AlibabaCloud.SDK.Dingtalkworkflow_1_0.Client client = CreateClientOa(); AlibabaCloud.SDK.Dingtalkworkflow_1_0.Models.GetAttachmentSpaceHeaders getAttachmentSpaceHeaders = new AlibabaCloud.SDK.Dingtalkworkflow_1_0.Models.GetAttachmentSpaceHeaders(); getAttachmentSpaceHeaders.XAcsDingtalkAccessToken = accessToken; AlibabaCloud.SDK.Dingtalkworkflow_1_0.Models.GetAttachmentSpaceRequest getAttachmentSpaceRequest = new AlibabaCloud.SDK.Dingtalkworkflow_1_0.Models.GetAttachmentSpaceRequest { UserId = "923188102", AgentId = 3179487642, }; try { var response = client.GetAttachmentSpaceWithOptions(getAttachmentSpaceRequest, getAttachmentSpaceHeaders, new AlibabaCloud.TeaUtil.Models.RuntimeOptions()); return (Newtonsoft.Json.JsonConvert.SerializeObject(response.Body)); } catch (TeaException err) { return $"TeaException: Code={err.Code}, Message={err.Message}"; } catch (Exception _err) { return $"Exception: {_err.Message}"; } } public string GetProcessList() { DateTime targetTime = new DateTime(2025, 3, 10, 0, 0, 0, DateTimeKind.Utc); // 转换为 Unix 时间戳 long unixTimestamp = ((DateTimeOffset)targetTime).ToUnixTimeMilliseconds(); Debug.WriteLine(unixTimestamp); long currentTimestamp = ((DateTimeOffset)DateTime.UtcNow).ToUnixTimeMilliseconds(); Debug.WriteLine(currentTimestamp); // 输出当前时间的 Unix 时间戳 string accessToken = GetToken(); if (string.IsNullOrEmpty(accessToken)) { return "获取 Access Token 失败"; } AlibabaCloud.SDK.Dingtalkworkflow_1_0.Client client = CreateClientOa(); AlibabaCloud.SDK.Dingtalkworkflow_1_0.Models.ListProcessInstanceIdsHeaders listProcessInstanceIdsHeaders = new AlibabaCloud.SDK.Dingtalkworkflow_1_0.Models.ListProcessInstanceIdsHeaders(); listProcessInstanceIdsHeaders.XAcsDingtalkAccessToken = accessToken; AlibabaCloud.SDK.Dingtalkworkflow_1_0.Models.ListProcessInstanceIdsRequest listProcessInstanceIdsRequest = new AlibabaCloud.SDK.Dingtalkworkflow_1_0.Models.ListProcessInstanceIdsRequest { ProcessCode = "PROC-BBA5632D-DC7C-4267-A3E2-757E2B7023BC", StartTime = unixTimestamp, EndTime = currentTimestamp, NextToken = 0, MaxResults = 10, UserIds = new List { "923188102" }, //Statuses = new List // { // }, }; try { var response = client.ListProcessInstanceIdsWithOptions(listProcessInstanceIdsRequest, listProcessInstanceIdsHeaders, new AlibabaCloud.TeaUtil.Models.RuntimeOptions()); return (Newtonsoft.Json.JsonConvert.SerializeObject(response.Body)); } catch (TeaException err) { return $"TeaException: Code={err.Code}, Message={err.Message}"; } catch (Exception _err) { return $"Exception: {_err.Message}"; } } public string GetPermissions() { string accessToken = GetToken(); if (string.IsNullOrEmpty(accessToken)) { return "获取 Access Token 失败"; } AlibabaCloud.SDK.Dingtalkworkflow_1_0.Client client = CreateClientOa(); AlibabaCloud.SDK.Dingtalkworkflow_1_0.Models.GetSpaceWithDownloadAuthHeaders getSpaceWithDownloadAuthHeaders = new AlibabaCloud.SDK.Dingtalkworkflow_1_0.Models.GetSpaceWithDownloadAuthHeaders(); getSpaceWithDownloadAuthHeaders.XAcsDingtalkAccessToken = accessToken; AlibabaCloud.SDK.Dingtalkworkflow_1_0.Models.GetSpaceWithDownloadAuthRequest getSpaceWithDownloadAuthRequest = new AlibabaCloud.SDK.Dingtalkworkflow_1_0.Models.GetSpaceWithDownloadAuthRequest { UserId = "071839512032683883", AgentId = 3179487642, ProcessInstanceId = "3vHyv40ZQUe2zK6wvl11dg06561742534701", FileId = "173564282626", //FileIdList = new List // { // "123" // }, }; try { var response = client.GetSpaceWithDownloadAuthWithOptions(getSpaceWithDownloadAuthRequest, getSpaceWithDownloadAuthHeaders, new AlibabaCloud.TeaUtil.Models.RuntimeOptions()); return (Newtonsoft.Json.JsonConvert.SerializeObject(response.Body)); } catch (TeaException err) { return $"TeaException: Code={err.Code}, Message={err.Message}"; } catch (Exception _err) { return $"Exception: {_err.Message}"; } } /** * 初始化账号Client,用于申请文件临时免登地址 * @return Client * @throws Exception */ public static AlibabaCloud.SDK.Dingtalkyida_1_0.Client CreateClient() { 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); } public string GetFileUrl(string imageUrl) { string accessToken = GetToken(); if (string.IsNullOrEmpty(accessToken)) { return "获取 Access Token 失败"; } AlibabaCloud.SDK.Dingtalkyida_1_0.Client client = CreateClient(); AlibabaCloud.SDK.Dingtalkyida_1_0.Models.GetOpenUrlHeaders getOpenUrlHeaders = new AlibabaCloud.SDK.Dingtalkyida_1_0.Models.GetOpenUrlHeaders(); getOpenUrlHeaders.XAcsDingtalkAccessToken = accessToken; AlibabaCloud.SDK.Dingtalkyida_1_0.Models.GetOpenUrlRequest getOpenUrlRequest = new AlibabaCloud.SDK.Dingtalkyida_1_0.Models.GetOpenUrlRequest { SystemToken = "54A66HC1YDSSO9DP61Y93CWQ2W9B36IZZOY6MC81", UserId = "923188102", Language = "zh_CN", FileUrl = imageUrl, Timeout = 3600000, }; try { var response = client.GetOpenUrlWithOptions("APP_Q7PLYKXMKFUJ5HY6DMWR", getOpenUrlRequest, getOpenUrlHeaders, new AlibabaCloud.TeaUtil.Models.RuntimeOptions()); return (Newtonsoft.Json.JsonConvert.SerializeObject(response.Body)); } catch (TeaException err) { return $"TeaException: Code={err.Code}, Message={err.Message}"; } catch (Exception _err) { return $"Exception: {_err.Message}"; } } public dynamic Save(ErpProductionOrder erpProductionOrder) { //var erpProductionOrder = productionOrder.erpProductionOrder; var mesProductionOrder = GetMesProductionOrder(erpProductionOrder); return UseTransaction(db => { return SaveOrUpdateData(db, mesProductionOrder) ? 1 : 0; }) > 0; } public dynamic SaveList(List erpProductionOrder) { var result = erpProductionOrder.Select(Save).ToList(); return result.All(b => b); } public ProductionOrders GetMesProductionOrder(ErpProductionOrder erpProductionOrder) { var mesProductionOrders = new ProductionOrders { ImageUrl = erpProductionOrder.ImageUrl, SalesOrder = erpProductionOrder.SalesOrder }; return mesProductionOrders; } private bool SaveOrUpdateData(SqlSugarScope db, ProductionOrders productionOrders) { var orUpdate = base.Insert(productionOrders); if (orUpdate) return true; throw new NotImplementedException("插入或更新失败"); } }