sjz
昨天 bf780b6c83012b5beb3d7515b8fb61753c5fb736
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
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<ProductionOrders>
{
    /**
        * 初始化账号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 GetProductionDetail( string InstanceId)
    {
        string accessToken = GetToken();
        if (string.IsNullOrEmpty(accessToken))
        {
            return "获取 Access Token 失败";
        }
        AlibabaCloud.SDK.Dingtalkworkflow_1_0.Client client = CreateClientOa();
        AlibabaCloud.SDK.Dingtalkworkflow_1_0.Models.GetProcessInstanceHeaders getProcessInstanceHeaders = new AlibabaCloud.SDK.Dingtalkworkflow_1_0.Models.GetProcessInstanceHeaders();
        getProcessInstanceHeaders.XAcsDingtalkAccessToken = accessToken;
        AlibabaCloud.SDK.Dingtalkworkflow_1_0.Models.GetProcessInstanceRequest getProcessInstanceRequest = new AlibabaCloud.SDK.Dingtalkworkflow_1_0.Models.GetProcessInstanceRequest
        {
            ProcessInstanceId = InstanceId,
        };
        try
        {
 
            var response = client.GetProcessInstanceWithOptions(getProcessInstanceRequest, getProcessInstanceHeaders, new AlibabaCloud.TeaUtil.Models.RuntimeOptions());
 
            // 将响应体转换为 JSON 字符串
            var responseJson = Newtonsoft.Json.JsonConvert.SerializeObject(response.Body);
 
            // 解析 JSON 字符串为动态对象
            dynamic responseObj = Newtonsoft.Json.JsonConvert.DeserializeObject(responseJson);
 
 
            string fileName = null;
            // 查找 "生产制造单" 对应的 Value
            string fileId = null;
            foreach (var formValue in responseObj.Result.FormComponentValues)
            {
                if (formValue.Name == "生产制造单" && formValue.Value != null)
                {
                    // 解析 Value 中的 JSON 数组
                    var fileInfoArray = Newtonsoft.Json.JsonConvert.DeserializeObject<List<FileInfoProduction>>(formValue.Value.ToString());
                  
                    if (fileInfoArray != null && fileInfoArray.Count > 0)
                    {
 
                        // 获取第一个文件的文件名
                        fileName = fileInfoArray[0].fileName;
 
                        fileId = fileInfoArray[0].fileId; // 提取第一个文件的 fileId
                      
                        break;
                    }
                }
            }
 
            Debug.WriteLine(fileId);
 
            var result = GetDownloadUrl(fileId, InstanceId);
            // 在您的代码中处理
            var combinedResult = new CombinedResult
            {
                FileName = fileName,
                DownloadInfo = Newtonsoft.Json.JsonConvert.DeserializeObject(result)
            };
 
            return Newtonsoft.Json.JsonConvert.SerializeObject(combinedResult);
 
        }
        catch (TeaException err)
        {
            return $"TeaException: Code={err.Code}, Message={err.Message}";
        }
        catch (Exception _err)
        {
            return $"Exception: {_err.Message}";
        }
     }
 
    public string GetDownloadUrl(string fileId, string InstanceId)
    {
 
        string accessToken = GetToken();
        if (string.IsNullOrEmpty(accessToken))
        {
            return "获取 Access Token 失败";
        }
        AlibabaCloud.SDK.Dingtalkworkflow_1_0.Client client = CreateClientOa();
        AlibabaCloud.SDK.Dingtalkworkflow_1_0.Models.GrantProcessInstanceForDownloadFileHeaders grantProcessInstanceForDownloadFileHeaders = new AlibabaCloud.SDK.Dingtalkworkflow_1_0.Models.GrantProcessInstanceForDownloadFileHeaders();
        grantProcessInstanceForDownloadFileHeaders.XAcsDingtalkAccessToken = accessToken;
        AlibabaCloud.SDK.Dingtalkworkflow_1_0.Models.GrantProcessInstanceForDownloadFileRequest grantProcessInstanceForDownloadFileRequest = new AlibabaCloud.SDK.Dingtalkworkflow_1_0.Models.GrantProcessInstanceForDownloadFileRequest
        {
            ProcessInstanceId = InstanceId,
            FileId = fileId,
        };
        try
        {
 
            var response = client.GrantProcessInstanceForDownloadFileWithOptions(grantProcessInstanceForDownloadFileRequest, grantProcessInstanceForDownloadFileHeaders, 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> 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.SaleOrderNo
        };
 
        return mesProductionOrders;
    }
 
    private bool SaveOrUpdateData(SqlSugarScope db, ProductionOrders productionOrders)
    {
        var orUpdate = base.Insert(productionOrders);
        if (orUpdate) return true;
        throw new NotImplementedException("插入或更新失败");
    }
 
 
}