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
|
}
|
|
|
|
|
}
|
}
|