using System.Data; using MES.Service.DB; using MES.Service.Dto.service; using MES.Service.Modes; using MES.Service.util; using Newtonsoft.Json; using SqlSugar; using DbType = System.Data.DbType; namespace MES.Service.service.Warehouse; public class MesInvItemInCDetailsManager : Repository { //当前类已经继承了 Repository 增、删、查、改的方法 public PurchaseInventory SaveBarCodes(WarehouseQuery entity) { // 采购入库[FILED3[userName[sectionCode[barcode var inputParam = "采购入库[FILED3[" + entity.userName + "[" + entity.sectionCode + "[" + entity.barcode; // 定义输出参数 var outputParam = new SugarParameter("c_result", null, DbType.String, ParameterDirection.Output, 4000); // 使用 SqlSugar 执行存储过程 Db.Ado.ExecuteCommand( "BEGIN prc_rf_pda_scan_in_barcode_N(:c_in_str, :c_result); END;", new SugarParameter("c_in_str", inputParam, DbType.String), outputParam); // 获取输出参数的值 var resultValue = outputParam.Value?.ToString(); // 根据返回值处理逻辑 if (resultValue.StartsWith("001")) { // 截取并处理结果 // 去掉前缀 "001[" 和最后的 "]" var content = resultValue.Substring(4).TrimEnd(']'); // 提取第一个逗号之前的部分 var parts = content.Split(','); var mesInvItemInCDetails = base.GetSingle(it => it.ItemBarcode == entity.barcode); if (mesInvItemInCDetails == null) throw new Exception("物料入库条码明细不存在"); // 抛出异常以供前台处理 var itemInId = mesInvItemInCDetails.ItemInId; entity.id = itemInId; entity.PageIndex = 1; entity.Limit = 1; var inventory = getPurchaseInventory(entity); inventory.ItemNo = parts[0]; inventory.SumQuantity = Convert.ToDecimal(parts[1]); return inventory; } if (resultValue.StartsWith("002")) { // 提取并抛出异常信息 var errorMessage = resultValue.Substring(4); // 获取 "002" 后面的部分 throw new Exception(errorMessage); // 抛出异常以供前台处理 } throw new Exception("未知错误: " + resultValue); } public PurchaseInventory getPurchaseInventory(WarehouseQuery query) { return new PurchaseInventory { ItemIns = GetInvItemInsList(query)[0], ItemInDetails = GetItemInDetails(query.id), InvItemInCDetails = GetInvItemInCDetails(query.id) }; } public List GetInvItemInsList(WarehouseQuery query) { return Db.Queryable((a, b, c) => new JoinQueryInfos(JoinType.Left, a.DepotsCode == b.DepotCode && b.Factory == a.Factory && b.Company == a.Company, JoinType.Left, a.SuppNo == c.SuppNo)) .WhereIF(query.id > 0, (a, b, c) => a.Id == query.id) .Select((a, b, c) => new MesInvItemIns { Id = a.Id, SuppNo = a.SuppNo, InsDate = a.InsDate, PaperBillNo = a.PaperBillNo, Remark = a.Remark, DepotsCode = a.DepotsCode, CbillNo = a.CbillNo, Status = a.Status, BillNo = a.BillNo, CreateDate = a.CreateDate, CreateBy = a.CreateBy, DepotName = b.DepotName, SuppName = c.SuppName }).ToPageList(query.PageIndex, query.Limit); } public List GetItemInDetails(decimal? pid) { var result = Db.Queryable() .WhereIF(pid > 0, g => g.ItemInId == pid).ToList(); return result; } public List GetInvItemInCDetails(decimal? pid) { var result = Db.Queryable( (g, c, d) => new JoinQueryInfos( JoinType.Left, g.ItemNo == c.ItemNo && g.Company == c.Company && g.Factory == c.Factory, JoinType.Inner, d.Id == Convert.ToDecimal(c.ItemUnit) ) ).WhereIF(pid > 0, (g, c, d) => g.ItemInId == pid) .Select((g, c, d) => new MesInvItemInCDetails { Id = g.Id, ItemName = c.ItemName, ItemModel = c.ItemModel, ItemUnit = d.Fname, DepotSectionCode = g.DepotSectionCode, CbillNo = g.CbillNo, ItemBarcode = g.ItemBarcode, UrgentFlag = g.UrgentFlag, ItemNo = g.ItemNo, WorkNo = g.WorkNo, Quantity = g.Quantity }) .ToList(); return result; } public MessageCenter MesToErpParam(WarehouseQuery query) { var erpParameters = ""; var title = ""; var tableName = "MES_INV_ITEM_INS_" + query.Type; if ("A".Equals(query.Type)) { erpParameters = GetErpParameters(query.billNo); title = "采购入库单" + query.billNo + "审核"; } else if ("B".Equals(query.Type)) { erpParameters = GetDeApprovePam(query.id); title = "采购入库单" + query.billNo + "反审核"; } var ErpUrl = AppsettingsUtility.Settings.ProductionErpUrl; var message = new MessageCenter { TableName = tableName, Url = ErpUrl, Status = 1, CreateBy = query.userName, Route = query.billNo, Title = title, PageName = "Warehouse/PurchaseInventory/Add?id=" + query.id + "&billNo=" + query.billNo, CreateDate = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss"), Method = "POST", Seq = 1, Data = erpParameters, IsMessage = 0, ContentType = "application/x-www-form-urlencoded" }; return message; } public MessageCenter SaveMessageCenter(WarehouseQuery query) { var message = MesToErpParam(query); var executeReturnIdentity = Db.Insertable(message).ExecuteReturnIdentity(); if (executeReturnIdentity > 0) { message.Id = executeReturnIdentity; message.Pid = executeReturnIdentity; return message; } throw new Exception("获取数据失败"); } //audit public bool audit(WarehouseQuery entity) { entity.date = DateTime.Now; entity.status = 1; return Update(entity); } public bool deApprove(WarehouseQuery entity) { entity.date = null; entity.status = 0; return Update(entity); } private bool Update(WarehouseQuery entity) { return Db.Updateable() .SetColumns(x => x.Status == entity.status) .SetColumns(x => x.InsDate == entity.date) .Where(x => x.BillNo == entity.billNo) .ExecuteCommand() > 0; } private string GetErpParameters(string? billNo) { var invItemIns = Db.Queryable() .Single(x => x.BillNo == billNo); if (invItemIns == null) throw new Exception("入库单号不存在"); if (invItemIns.Status == 1) throw new Exception("入库单已审核,不能重复推送"); var materials = Db.Queryable( (g, c, d, a) => new JoinQueryInfos( JoinType.Left, g.ItemNo == c.ItemNo && g.Company == c.Company && g.Factory == c.Factory, JoinType.Inner, d.Id == Convert.ToDecimal(c.ItemUnit), JoinType.Inner, a.Ebeln == g.WorkNo && a.WorkLine == g.EbelnLineNo && g.CbillNo == a.CbillNo ) ).Where((g, c, d, a) => g.BillNo == billNo).Select( (g, c, d, a) => new Material { FstockId = g.DepotCode, FuintId = d.Fnumber, FsrcEntryId = a.Id, FmesEntryId = g.Id, FmaterialId = a.ItemNo, DepotSectionCode = g.DepotSectionCode, WorkNo = g.WorkNo, Frealqty = g.Quantity }).ToList(); if (materials == null || materials.Count == 0) throw new Exception("没有找到相关数据"); // 抛出异常以供前台处理 // 构造 JSON var jsonEntries = materials.Select(d => new { FMaterialId = d.FmaterialId, FUintId = d.FuintId, FRealQty = d.Frealqty, FStockId = d.FstockId, FSRCENTRYID = d.FsrcEntryId.ToString(), F_MES_ENTRYID = d.FmesEntryId.ToString() }).ToList(); var fdate = DateTime.Now.ToString("yyyy-MM-dd"); var jsonString = JsonConvert.SerializeObject(jsonEntries); var encodedUrl = "taskname=CGRK&mesid=" + invItemIns.Id + "&optype=create&datajson={\"F_MES_ID\":\"" + invItemIns.Id + "\",\"FDate\":\"" + fdate + "\",\"cgrkentry\":" + jsonString + "}"; return encodedUrl; } private string GetDeApprovePam(decimal? id) { var sid = (int)id; var encodedUrl = "taskname=CGRK&mesid=" + sid + "&optype=delete&datajson={}"; return encodedUrl; } }