hao
2025-05-21 711fd971275401adc48b6a25865ea4594bc7fb75
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
using gdbg.Models;
using gdbg.Services;
using Microsoft.AspNetCore.Mvc;
using Oracle.ManagedDataAccess.Client;
using System.Collections.Generic;
using System.Text.RegularExpressions;
using System.Threading.Tasks;
 
namespace gdbg.Controllers
{
    [ApiController]
    [Route("api/[controller]")]
    public class PrintController : ControllerBase
    {
        private readonly OracleDbService _oracleDbService;
 
        public PrintController(OracleDbService oracleDbService)
        {
            _oracleDbService = oracleDbService;
        }
        [HttpGet("printHistory")]
        public async Task<IActionResult> GetPrintHistory(string orderNo)
        {
            try
            {
                // 修改 SQL 查询以返回更多字段
                string query = 
 
                   " SELECT a.ITEM_BARCODE, TO_CHAR(a.CREATE_DATE, 'YYYY-MM-DD HH24:MI:SS') AS PRINT_DATE, bgr.bgr, a.quantity                   " +
                   " FROM MES_INV_ITEM_BARCODES a left join(select case when sta.STAFF_NAME is null then a.CREATE_BY  else sta.STAFF_NAME end bgr,a.ITEM_BARCODE      " +
                   " from MES_WORK_PROD_C_DETAILS a  left join mes_staff sta on a.CREATE_BY = sta.STAFF_NO) bgr on bgr.ITEM_BARCODE = a.ITEM_BARCODE                  " +
                        "     left join MES_STAFF b on a.CREATE_BY = b.STAFF_NO                                                                                       " +
                    "WHERE BILL_NO = :orderNo  and TO_CHAR(a.CREATE_DATE, 'YYYY-MM-DD')<=to_char(SYSDATE,'YYYY-MM-DD') and TO_CHAR(a.CREATE_DATE, 'YYYY-MM-DD')>=to_char(SYSDATE-2,'YYYY-MM-DD') " +
                    "order by a.CREATE_DATE desc";
 
 
                var parameters = new List<OracleParameter>
        {
            new OracleParameter("orderNo", orderNo)
        };
 
                var historyRecords = await _oracleDbService.GetAsync(query, reader => new
                {
                    ItemBarcode = reader.GetString(0),               // 打印条码
                    PrintDate = reader.GetString(1),                 // 打印时间 (字符串格式)
                    PrintedBy = reader.IsDBNull(2) ? "" : reader.GetString(2) , // 打印人
                    quantity= reader.GetString(3)
                }, parameters);
 
 
                return Ok(new { historyRecords });
            }
            catch (Exception ex)
            {
                return StatusCode(500, new { message = "An error occurred while fetching print history.", error = ex.Message });
            }
        }
 
        [HttpPost]
        public async Task<IActionResult> PrintAndUpdate([FromBody] PrintRequest request)
        {
            try
            {
                // 调用存储过程的 SQL
                string procedureCall = "BEGIN PrintAndUpdate(:p_orderNo,:p_person, :p_itemId, :p_barcodeValue, :p_quantity); END;";
 
                // 参数列表
                var parameters = new List<OracleParameter>
                {
                    new OracleParameter("p_orderNo", request.OrderNo),
                    new OracleParameter("p_person", request.p_person),
                   // new OracleParameter("p_reportedQuantity", request.ReportedQuantity),
                    new OracleParameter("p_itemId", request.ItemId),
                    new OracleParameter("p_barcodeValue", request.UniqueValue),
                   // new OracleParameter("p_itemCode", request.ItemCode),
                 //   new OracleParameter("p_itemName", request.ItemName),
                  //  new OracleParameter("p_itemSpec", request.ItemSpec),
                    new OracleParameter("p_quantity", request.Quantity),
 
                };
 
                // 调用存储过程
                await _oracleDbService.ExecuteProcedureAsync(procedureCall, parameters);
 
                return Ok(new { message = "Print and update successful." });
            }
            catch (OracleException ex)
            {
                if (ex.Number == -20002)
                {
                    string cleanMessage = ExtractErrorMessage(ex.Message);
                    return BadRequest(new { message = cleanMessage });
                }
                // 其他 Oracle 错误
                return StatusCode(500, new
                {
                   // message = "数据库错误:" + ex.Message
                    message = ExtractErrorMessage(ex.Message)
                });
            }
            catch (Exception ex)
            {
                return StatusCode(500, new { message = "An error occurred during printing and updating.", error = ex.Message });
            }
 
        }
 
       /* private static string ExtractErrorMessage(string oracleMessage)
        {
            // 匹配 "ORA-20002:" 后的具体错误信息
            string pattern = @"ORA-20002:.*?:\s(.*?)(\n|$)";
            var match = System.Text.RegularExpressions.Regex.Match(oracleMessage, pattern);
 
            if (match.Success)
            {
                return match.Groups[1].Value.Trim(); // 提取核心中文错误部分
            }
 
            // 如果无法匹配,则返回完整错误信息
            return oracleMessage;
        }*/
 /*       private static string ExtractErrorMessage(string oracleMessage)
        {
            // 匹配多个 ORA-20002: 后的段落,提取最后一个
            var matches = System.Text.RegularExpressions.Regex.Matches(
                oracleMessage,
                @"ORA-20002:(.*?)(?=ORA-\d{5}:|$)"
            );
 
            if (matches.Count > 0)
            {
                // 提取最后一个匹配项,去除换行和多余空格
                return matches[matches.Count - 1].Groups[1].Value.Trim();
            }
 
            // 默认返回原始错误
            return oracleMessage;
        }*/
 
 
        private static string ExtractErrorMessage(string oracleMessage)
        {
            // 匹配所有 ORA-20002: 错误段
            var matches = Regex.Matches(oracleMessage, @"ORA-20002:\s*(.*?)(?=(ORA-\d{5}:|$))", RegexOptions.Singleline);
 
            if (matches.Count > 0)
            {
                // 返回最里面一条(最后一个)
                string raw = matches[matches.Count - 1].Groups[1].Value;
 
                // 去掉类似 "Error in PrintAndUpdate procedure:" 开头
                string cleaned = Regex.Replace(raw, @"^Error.*?:", "", RegexOptions.IgnoreCase).Trim();
 
                return string.IsNullOrWhiteSpace(cleaned) ? raw.Trim() : cleaned;
            }
 
            return oracleMessage; // fallback
        }
 
 
 
 
    }
}