| | |
| | | // OaApiService.cs |
| | | // OaApiService.cs |
| | | using System; |
| | | using System.Collections.Generic; |
| | | using System.Dynamic; |
| | | using System.Data.SqlClient; |
| | | using System.Data; |
| | | using System.IO; |
| | | using System.Net.Http; |
| | | using System.Security.Cryptography; |
| | |
| | | public async Task<dynamic> SubmitIQCToOA(dynamic queryObj) |
| | | { |
| | | // 修改参数验证字段名 |
| | | //if (queryObj.userId.IsNullOrEmpty()) throw new Exception("用户id不允许为空"); |
| | | if (string.IsNullOrEmpty(queryObj.userId.ToString())) throw new Exception("用户id不允许为空"); |
| | | if (string.IsNullOrEmpty(queryObj.qcczdGuid.ToString())) throw new Exception("异常处置单id不能为空"); |
| | | //if (queryObj.qcczdGuid.IsNullOrEmpty()) throw new Exception("异常处置单id不能为空"); // 原参数名为 qcczdGuid |
| | | |
| | | try |
| | |
| | | |
| | | // 5. 发送请求 |
| | | var response = await PostFormAsync(WorkflowUrl, requestData); |
| | | return JObject.Parse(response); |
| | | |
| | | // 新增日志记录(在返回响应前) |
| | | using (var conn = new SqlConnection(DbHelperSQL.strConn)) |
| | | { |
| | | using (var cmd = new SqlCommand("prc_log_create", conn)) |
| | | { |
| | | try |
| | | { |
| | | conn.Open(); |
| | | cmd.CommandType = CommandType.StoredProcedure; |
| | | SqlParameter[] logParams = |
| | | { |
| | | new("@edtUserGuid", SqlDbType.UniqueIdentifier) { Value = Guid.Parse(queryObj.userId.ToString()) }, |
| | | new("@abtGuid", SqlDbType.UniqueIdentifier) { Value = Guid.Parse(queryObj.qcczdGuid.ToString()) }, |
| | | new("@abtTable", SqlDbType.NVarChar, 40) { Value = "Mes_QC_Exceptional" }, |
| | | new("@detail", SqlDbType.NVarChar, 2500) { Value = "提交IQC到OA系统" }, |
| | | new("@hNo", SqlDbType.NVarChar, 100) { Value = "" }, |
| | | new("@SendJson", SqlDbType.NVarChar) { Value = JObject.FromObject(requestData).ToString() }, |
| | | new("@RtnJson", SqlDbType.NVarChar) { Value = response.Content.ToString() } |
| | | }; |
| | | |
| | | foreach (var param in logParams) |
| | | cmd.Parameters.Add(param); |
| | | |
| | | cmd.ExecuteNonQuery(); |
| | | } |
| | | catch (Exception logEx) |
| | | { |
| | | // 日志记录失败不中断主流程 |
| | | Console.WriteLine($"日志记录失败: {logEx.Message}"); |
| | | } |
| | | finally |
| | | { |
| | | conn.Close(); |
| | | } |
| | | } |
| | | } |
| | | |
| | | var result = JObject.Parse(response.Content.ToString()); |
| | | |
| | | // 新增状态更新(当OA返回成功时) |
| | | if (result.code?.ToString() == "SUCCESS") |
| | | { |
| | | var updateSql = @"UPDATE Mes_QC_Exceptional |
| | | SET checkStatus = 1, |
| | | checkDate = GETDATE(), |
| | | checkBy = (select top 1 u.[ACCOUNT] from [dbo].[SYS_USER] u where u.guid = @edtUserCode) |
| | | WHERE GUID = @inOrderGuid"; |
| | | |
| | | using (var conn = new SqlConnection(DbHelperSQL.strConn)) |
| | | { |
| | | using (var cmd = new SqlCommand(updateSql, conn)) |
| | | { |
| | | try |
| | | { |
| | | conn.Open(); |
| | | //cmd.Parameters.AddWithValue("@dt", DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss")); |
| | | cmd.Parameters.AddWithValue("@edtUserCode", queryObj.userId?.ToString()); |
| | | cmd.Parameters.AddWithValue("@inOrderGuid", queryObj.qcczdGuid?.ToString()); |
| | | cmd.ExecuteNonQuery(); |
| | | } |
| | | catch (Exception updateEx) |
| | | { |
| | | Console.WriteLine($"状态更新失败: {updateEx.Message}"); |
| | | } |
| | | finally |
| | | { |
| | | conn.Close(); |
| | | } |
| | | } |
| | | } |
| | | } |
| | | |
| | | return result; // 直接解析为JObject |
| | | |
| | | } |
| | | catch (Exception ex) |
| | | { |
| | | dynamic error = new ExpandoObject(); |
| | | error.Error = true; |
| | | error.Message = ex.Message; |
| | | error.code = "SYSTEM_INNER_ERROR"; // 新增标准错误码 |
| | | error.errMsg = ex.Message; // 修正字段名匹配规范 |
| | | error.data = new JObject(); |
| | | error.reqFailMsg = new JObject(); |
| | | return error; |
| | | } |
| | | } |
| | |
| | | {"requestName", $"IQC异常处置单流程-{DateTime.Now:yyyyMMddHHmmss}"}, |
| | | {"workflowId", "379"}, |
| | | //{"otherParams", BuildOtherParams()}, |
| | | //{"detailData", BuildDetailData(queryObj)} |
| | | {"detailData", BuildDetailData(queryObj)} |
| | | }; |
| | | } |
| | | |
| | | |
| | | private JArray createbhgpqxmszp(dynamic files) |
| | | { |
| | | var attachments = new JArray(); |
| | | |
| | | |
| | | foreach (var file in files ?? Enumerable.Empty<dynamic>()) |
| | | { |
| | | attachments.Add(new JObject |
| | |
| | | ["fileName"] = file.FileName?.ToString() ?? "未命名文件" |
| | | }); |
| | | } |
| | | |
| | | |
| | | return attachments; |
| | | } |
| | | private JArray BuildMainData(dynamic queryObj) |
| | |
| | | C.item_name AS jzmcwlmc, |
| | | D.supp_name AS khgys, |
| | | B.bill_no AS dhdhmes, |
| | | B.remark AS bhgpqxms |
| | | (SELECT STRING_AGG(releaseNo+':' + mx.fng_desc+'\n', ',') AS OrderIDs |
| | | FROM [Mes_QC_Exceptional_Detail] mx where mx.pGuid=a.guid |
| | | ) AS bhgpqxms |
| | | FROM Mes_QC_Exceptional A |
| | | LEFT JOIN MES_INV_ITEM_ARN B ON A.aboutGuid = b.guid |
| | | LEFT JOIN MES_ITEMS C ON A.itemId = c.item_id |
| | |
| | | 'http://192.168.1.145:81/upload/'+url_Path as url |
| | | FROM [Mes_QC_Exceptional_Detail] A |
| | | LEFT JOIN MES_QA_ITEMS_DETECT_01 B ON A.releaseNo = B.release_no |
| | | LEFT JOIN MES_FILE C ON C.parent_Guid = B.guid |
| | | RIGHT JOIN MES_FILE C ON C.parent_Guid = B.guid |
| | | WHERE pGuid = @guid"; |
| | | |
| | | var fileData = Db.Ado.SqlQuery<dynamic>(fileSql, new { guid = queryObj.qcczdGuid }); |
| | |
| | | return array; |
| | | } |
| | | |
| | | private JArray BuildDetailData(JObject query) |
| | | private JArray BuildDetailData(dynamic queryObj) |
| | | { |
| | | // 示例明细数据,根据实际业务调整 |
| | | // 使用参数化查询获取明细数据 |
| | | const string mainSql = @"select ROW_NUMBER() over (ORDER BY Jy_Date) AS xh,releaseNo AS jydh, Jy_Date AS jydrq, batchQty as sjsl, |
| | | chouQty AS cys, badQty AS bls, badProb AS bll |
| | | from [Mes_QC_Exceptional_Detail] |
| | | WHERE pGuid = @guid"; |
| | | |
| | | var details = Db.Ado.SqlQuery<dynamic>(mainSql, new { guid = queryObj.qcczdGuid }); |
| | | |
| | | var tableRecords = new JArray(); |
| | | |
| | | // 遍历每条明细数据 |
| | | foreach (var record in details ?? Enumerable.Empty<dynamic>()) |
| | | { |
| | | // 修正字段类型转换(数值类型去除字符串包裹) |
| | | var fields = new JArray |
| | | { |
| | | //new JObject { ["fieldName"] = "xh", ["fieldValue"] = record.xh?.ToString() ?? "" }, |
| | | new JObject { ["fieldName"] = "jydh", ["fieldValue"] = record.jydh?.ToString() ?? "" }, |
| | | new JObject { |
| | | ["fieldName"] = "jydrq", |
| | | // 格式化为"yyyy-MM-dd"字符串 |
| | | ["fieldValue"] = (record.jydrq != null |
| | | ? Convert.ToDateTime(record.jydrq).ToString("yyyy-MM-dd ") |
| | | : DateTime.Now.ToString("yyyy-MM-dd")) |
| | | }, |
| | | // 数值字段保持数字类型而非字符串 |
| | | // 修正数值类型转换方式 |
| | | new JObject { ["fieldName"] = "sjsl", ["fieldValue"] = (float)(record.sjsl ?? 0m) }, |
| | | new JObject { ["fieldName"] = "cys", ["fieldValue"] = (float)(record.cys ?? 0) }, |
| | | new JObject { ["fieldName"] = "bls", ["fieldValue"] = (float)(record.bls ?? 0) }, |
| | | new JObject { ["fieldName"] = "bll", ["fieldValue"] = 1 } |
| | | }; |
| | | |
| | | tableRecords.Add(new JObject |
| | | { |
| | | ["recordOrder"] = 0, // 实际业务中可能需要根据行号设置 |
| | | ["workflowRequestTableFields"] = fields |
| | | }); |
| | | } |
| | | |
| | | return new JArray |
| | | { |
| | | new JObject |
| | | { |
| | | ["tableDBName"] = "formtable_main_33_dt1", |
| | | ["workflowRequestTableRecords"] = new JArray |
| | | { |
| | | new JObject |
| | | { |
| | | ["recordOrder"] = "0", |
| | | ["workflowRequestTableFields"] = new JArray |
| | | { |
| | | new JObject { ["fieldName"] = "jydh", ["fieldValue"] = query[""] ?? "检验单号" }, |
| | | new JObject { ["fieldName"] = "jydrq", ["fieldValue"] = query[""] ?? "检验单日期" }, |
| | | new JObject { ["fieldName"] = "sjsl", ["fieldValue"] = query[""] ?? "涉及数量" }, |
| | | new JObject { ["fieldName"] = "cys", ["fieldValue"] = query[""] ?? "抽样数" }, |
| | | new JObject { ["fieldName"] = "bls", ["fieldValue"] = query[""] ?? "不良数" }, |
| | | new JObject { ["fieldName"] = "bll", ["fieldValue"] = query[""] ?? "不良率" } |
| | | } |
| | | } |
| | | } |
| | | ["workflowRequestTableRecords"] = tableRecords |
| | | } |
| | | }; |
| | | } |
| | |
| | | return encryptedContent; |
| | | } |
| | | |
| | | private async Task<string> PostFormAsync(string url, Dictionary<string, object> data) |
| | | private async Task<dynamic> PostFormAsync(string url, Dictionary<string, object> data) |
| | | { |
| | | // 改用FormUrlEncodedContent并正确设置Content-Type |
| | | var formData = new List<KeyValuePair<string, string>>(); |
| | |
| | | { |
| | | formData.Add(new KeyValuePair<string, string>(item.Key, item.Value.ToString())); |
| | | } |
| | | |
| | | |
| | | var content = new FormUrlEncodedContent(formData); |
| | | content.Headers.ContentType = new System.Net.Http.Headers.MediaTypeHeaderValue("application/x-www-form-urlencoded"); |
| | | |
| | | var response = await _httpClient.PostAsync(url, content); |
| | | var responseString = await response.Content.ReadAsStringAsync(); |
| | | |
| | | |
| | | // 添加响应元数据 |
| | | var responseInfo = new JObject |
| | | { |
| | |
| | | ["Headers"] = JToken.FromObject(response.Headers), |
| | | ["Content"] = responseString |
| | | }; |
| | | |
| | | |
| | | // 调试输出完整响应信息 |
| | | Console.WriteLine($"完整响应:\n{responseInfo.ToString(Formatting.Indented)}"); |
| | | |
| | | return responseInfo.ToString(); |
| | | |
| | | return responseInfo; |
| | | } |
| | | |
| | | public void Dispose() |
| | | { |
| | | _httpClient?.Dispose(); |
| | | } |
| | | } |
| | | } |