feat: SRM排程承诺导入(不触发断线重算)+ ParseExcelDate 兼容日期格式
This commit is contained in:
1 parent
5b3f1eb9d7
commit
de4516ab5d
2 files changed
+179
No files matched your search
@@ -3221,6 +3221,163 @@ LEFT JOIN #SupByMat SP ON SP.MaterialCode=D.ChildMaterialId;
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 导入 SRM 供应商排程承诺(SRM 原生导出 Excel)
|
||||
/// 只写入 Arrival_Plan 表作为供应商交货承诺参考,**不触发断线风险重算**
|
||||
/// (即不调用 UpdateDeliveryArrivalDate,不改 Material_Delivery.ExpectedArrivalDate/DeliveryRisk)
|
||||
/// Excel 列:[2]物料编码 / [10]承诺交货日期 / [11]承诺交货数量 / [13]接受状态
|
||||
/// </summary>
|
||||
[HttpPost]
|
||||
public IHttpActionResult ImportSRMSchedule([FromBody] JObject obj)
|
||||
{
|
||||
try
|
||||
{
|
||||
string fileBase64 = obj["FileBase64"]?.ToString() ?? "";
|
||||
if (string.IsNullOrEmpty(fileBase64)) return Ok(new { success = false, message = "文件为空" });
|
||||
|
||||
byte[] fileBytes;
|
||||
try { fileBytes = Convert.FromBase64String(fileBase64); }
|
||||
catch { return Ok(new { success = false, message = "文件 Base64 解码失败" }); }
|
||||
|
||||
List<string[]> rows = ExcelHelper.ReadXlsxRows(fileBytes);
|
||||
if (rows == null || rows.Count < 2)
|
||||
return Ok(new { success = false, message = "文件无有效数据行" });
|
||||
|
||||
// 校验表头(第 0 行)
|
||||
string[] header = rows[0];
|
||||
if (header.Length < 14 || (header[2] ?? "").Trim() != "物料编码"
|
||||
|| (header[10] ?? "").Trim() != "承诺交货日期"
|
||||
|| (header[11] ?? "").Trim() != "承诺交货数量")
|
||||
return Ok(new { success = false, message = "表头不符合 SRM 排程格式(需含:物料编码/承诺交货日期/承诺交货数量)" });
|
||||
|
||||
// 收集有效行:(物料编码, 承诺交货日期, 承诺交货数量)
|
||||
// 过滤:接受状态 != "待供应商接受";承诺交货日期非空;承诺交货数量 > 0
|
||||
var matPlans = new Dictionary<string, List<KeyValuePair<string, decimal>>>();
|
||||
int skippedPending = 0, skippedNoDate = 0, skippedNoQty = 0;
|
||||
var inv = System.Globalization.CultureInfo.InvariantCulture;
|
||||
|
||||
for (int r = 1; r < rows.Count; r++)
|
||||
{
|
||||
string[] cells = rows[r];
|
||||
if (cells == null || cells.Length < 12) continue;
|
||||
|
||||
string code = (cells[2] ?? "").Trim();
|
||||
if (string.IsNullOrEmpty(code)) continue;
|
||||
|
||||
// 接受状态(第 13 列可能越界)
|
||||
string status = cells.Length > 13 ? (cells[13] ?? "").Trim() : "";
|
||||
if (status == "待供应商接受") { skippedPending++; continue; }
|
||||
|
||||
// 承诺交货日期(兼容字符串日期 yyyy-MM-dd/yyyy/MM/dd 和 Excel 序列号)
|
||||
string dateRaw = (cells[10] ?? "").Trim();
|
||||
if (string.IsNullOrEmpty(dateRaw)) { skippedNoDate++; continue; }
|
||||
string arrivalDate = ParseExcelDate(dateRaw);
|
||||
if (string.IsNullOrEmpty(arrivalDate)) { skippedNoDate++; continue; }
|
||||
|
||||
// 承诺交货数量
|
||||
string qtyRaw = (cells[11] ?? "").Trim();
|
||||
if (string.IsNullOrEmpty(qtyRaw)) { skippedNoQty++; continue; }
|
||||
if (!decimal.TryParse(qtyRaw, out decimal qty) || qty <= 0) { skippedNoQty++; continue; }
|
||||
|
||||
if (!matPlans.ContainsKey(code)) matPlans[code] = new List<KeyValuePair<string, decimal>>();
|
||||
matPlans[code].Add(new KeyValuePair<string, decimal>(arrivalDate, qty));
|
||||
}
|
||||
|
||||
if (matPlans.Count == 0)
|
||||
return Ok(new { success = false, message = "未找到有效的供应商承诺数据(全部为待接受/无日期/无数量)" });
|
||||
|
||||
// 匹配 Material_Delivery.Id
|
||||
string codes = string.Join(",", matPlans.Keys.Select(x => $"'{Sql(x)}'"));
|
||||
DataTable dtMap = db.ExecuteTable($"SELECT Id, ChildMaterialId FROM Material_Delivery WHERE ChildMaterialId IN ({codes})");
|
||||
var deliveryMap = new Dictionary<string, List<int>>();
|
||||
foreach (DataRow dr in dtMap.Rows)
|
||||
{
|
||||
string c = dr["ChildMaterialId"].ToStringEx().Trim();
|
||||
int id = Int(dr["Id"]);
|
||||
if (!deliveryMap.ContainsKey(c)) deliveryMap[c] = new List<int>();
|
||||
deliveryMap[c].Add(id);
|
||||
}
|
||||
|
||||
int totalInserted = 0, totalDeleted = 0;
|
||||
var notFound = new List<string>();
|
||||
|
||||
db.BeginTransaction();
|
||||
try
|
||||
{
|
||||
foreach (var kv in matPlans)
|
||||
{
|
||||
string code = kv.Key;
|
||||
if (!deliveryMap.ContainsKey(code) || deliveryMap[code].Count == 0)
|
||||
{ notFound.Add(code); continue; }
|
||||
|
||||
var list = kv.Value.OrderBy(x => x.Key).ToList();
|
||||
foreach (int deliveryId in deliveryMap[code])
|
||||
{
|
||||
// 先清旧承诺,保证幂等
|
||||
totalDeleted += db.ExecuteNonQuery($"DELETE FROM Arrival_Plan WHERE DeliveryId={deliveryId}");
|
||||
|
||||
int nextNo = 0;
|
||||
foreach (var item in list)
|
||||
{
|
||||
nextNo++;
|
||||
db.ExecuteNonQuery($@"INSERT INTO Arrival_Plan (DeliveryId, BatchNo, ExpectedArrivalDate, ArrivalQty, Status)
|
||||
VALUES ({deliveryId}, {nextNo}, '{item.Key}', {item.Value.ToString(inv)}, N'SRM承诺')");
|
||||
totalInserted++;
|
||||
}
|
||||
}
|
||||
}
|
||||
db.CommitTransaction();
|
||||
}
|
||||
catch
|
||||
{
|
||||
db.RollbackTransaction();
|
||||
throw;
|
||||
}
|
||||
|
||||
string msg = $"SRM 承诺导入完成:清除 {totalDeleted} 条旧承诺,新增 {totalInserted} 条承诺,涉及 {deliveryMap.Count} 个物料";
|
||||
if (notFound.Count > 0) msg += $";未匹配到交付记录的物料 {notFound.Count} 个:{string.Join("、", notFound.Take(20))}";
|
||||
if (skippedPending > 0) msg += $";跳过待接受 {skippedPending} 行";
|
||||
if (skippedNoDate > 0) msg += $";跳过无日期 {skippedNoDate} 行";
|
||||
if (skippedNoQty > 0) msg += $";跳过无数量 {skippedNoQty} 行";
|
||||
|
||||
return Ok(new { success = true, message = msg, inserted = totalInserted, deleted = totalDeleted, notFound = notFound });
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
Logger.Error("ImportSRMSchedule异常:", ex);
|
||||
return Ok(new { success = false, message = ex.Message });
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 解析 Excel 日期:兼容 yyyy-MM-dd / yyyy/MM/dd 字符串 和 Excel 序列号(OADate)
|
||||
/// </summary>
|
||||
private string ParseExcelDate(string raw)
|
||||
{
|
||||
if (string.IsNullOrEmpty(raw)) return "";
|
||||
raw = raw.Trim();
|
||||
|
||||
// 1) 纯数字 → Excel 序列号
|
||||
if (double.TryParse(raw, System.Globalization.NumberStyles.Any,
|
||||
System.Globalization.CultureInfo.InvariantCulture, out double serial))
|
||||
{
|
||||
try { return DateTime.FromOADate(serial).ToString("yyyy-MM-dd"); }
|
||||
catch { return ""; }
|
||||
}
|
||||
|
||||
// 2) 字符串日期
|
||||
string[] formats = { "yyyy-MM-dd", "yyyy/MM/dd", "yyyy.MM.dd", "yyyyMMdd" };
|
||||
if (DateTime.TryParseExact(raw, formats, System.Globalization.CultureInfo.InvariantCulture,
|
||||
System.Globalization.DateTimeStyles.None, out DateTime dt))
|
||||
return dt.ToString("yyyy-MM-dd");
|
||||
|
||||
// 3) 兜底:让 .NET 自己猜
|
||||
if (DateTime.TryParse(raw, out DateTime dt2))
|
||||
return dt2.ToString("yyyy-MM-dd");
|
||||
|
||||
return "";
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 删除到货计划批次
|
||||
/// </summary>
|
||||
|
||||
Reference in new issue
Block a user