public static void PrintOrder(this PrintDbContext printDbContext, ShopDbContext db, ShopOrder shopOrder, string shopname) { try { if (shopOrder == null) { throw new Exception("订单错误"); } List <ShopOrderItem> items = db.Query <ShopOrderItem>() .Where(m => !m.IsDel) .Where(m => m.ShopOrderId == shopOrder.Id) .ToList(); if (items.Count == 0) { return; } List <OrderInfo> orderInfo = items.Select(s => new OrderInfo() { Name = $"{s.CommodityName}({s.SkuSummary})", Price = Math.Round(s.SalePrice / 100d, 2), Count = s.Count }).ToList(); string stringorderlist = JsonConvert.SerializeObject(orderInfo); List <Printer> printers = printDbContext.Query <Printer>() .Where(s => !s.IsDel) .Where(s => s.ShopID == shopOrder.ShopId && s.IsEnable).ToList(); if (printers.Count == 0) { return; } PrintParameter parameter = GetPrintParameter(db, shopOrder); parameter.ShopName = shopname; parameter.List = orderInfo; foreach (var item in printers) { //StringBuilder postData = new StringBuilder("sn=" + item.SN); parameter.SN = item.SN; parameter.Times = item.Times + ""; PrintModel model = printDbContext.Query <PrintModel>().FirstOrDefault(s => s.ID == item.ModelID); if (model == null) { model = printDbContext.Set <PrintModel>().Find(2); } parameter.ModelContent = model.ModelContent; PrinterBase @base = PrinterFactory.Create(item.PrinterType);//) string temp = @base.PrinterRequest(parameter, item); #region 数据库操作 PrintRecord record = new PrintRecord(); //处理接口返回数据 Dictionary <string, object> dic = JsonConvert.DeserializeObject <Dictionary <string, object> >(temp); //实体赋值 record.SN = item.SN; record.OrderID = @base.GetOrderID(temp);// dic.ContainsKey("data") && dic["msg"].Equals("ok") ? dic["data"].ToString() : "未能成功打印"; record.Title = parameter.Title; record.OrderList = stringorderlist; record.TotalMoney = (float)parameter.TotalMoney; record.Address = parameter.Address; record.OrderName = parameter.OrderName; record.Mobile = parameter.Mobile; record.OrderTime = Convert.ToDateTime(parameter.OrderTime); record.QRAddress = parameter.QRAddress; record.Remark = shopOrder.Remark; printDbContext.AddTo(record); #endregion } shopOrder.IsPrint = true; } catch (Exception e) { //_logger.LogError("打印机错误:{0}", e.Message); } }
public static void PrintOrder(PrintDbContext printDbContext, ShopDbContext db, ShopOrder shopOrder, string shopname, ILogger _logger) { _logger.LogInformation($"Remark{shopOrder.Remark}"); if (shopOrder.IsPrint) { return; } try { List <ShopOrderItem> items = db.Query <ShopOrderItem>() .Where(m => !m.IsDel) .Where(m => m.ShopOrderId == shopOrder.Id) .ToList(); if (items.Count == 0) { return; } List <OrderInfo> orderInfo = items.Select(s => new OrderInfo() { Name = $"{s.CommodityName}({s.SkuSummary})", Price = Math.Round(s.SalePrice / 100d, 2), Count = s.Count, ComboConten = GetPrintComboContenParameter(db, s, _logger) }).ToList(); string stringorderlist = JsonConvert.SerializeObject(orderInfo); List <Printer.Data.Printer> printers = printDbContext.Query <Printer.Data.Printer>() .Where(s => !s.IsDel) .Where(s => s.ShopID == shopOrder.ShopId && s.IsEnable).ToList(); if (printers.Count == 0) { return; } PrintParameter parameter = GetPrintParameter(db, shopOrder); parameter.ShopName = shopname; parameter.List = orderInfo; parameter.IsTakeOut = shopOrder.IsTakeOut; //获取优惠 if (shopOrder.MoneyOffRuleId != null && shopOrder.MoneyOffRuleId != 0) { var shopOrderMoneyOffRules = db.ShopOrderMoneyOffRules.Find(shopOrder.MoneyOffRuleId); parameter.ShopOrderMoneyOffRule = new ShopOrderMoneyOffRuleModel() { Discount = Math.Round(shopOrderMoneyOffRules.Discount / 100d, 2), FullAmount = Math.Round(shopOrderMoneyOffRules.FullAmount / 100d, 2), MoneyOffId = shopOrderMoneyOffRules.MoneyOffId, MoneyOffName = db.ShopOrderMoneyOffs.Find(shopOrderMoneyOffRules.MoneyOffId).Name }; } //获取其他费用 if (shopOrder.OtherFeeId != null && shopOrder.OtherFeeId != 0) { var shopOrderMoneyOffs = db.ShopOrderOtherFees.Find(shopOrder.OtherFeeId); parameter.ShopOrderOtherFee = new Dictionary <string, double>() { { "餐盒费", Math.Round(shopOrderMoneyOffs.BoxFee / 100d, 2) }, { "配送费", Math.Round(shopOrderMoneyOffs.DeliveryFee / 100d, 2) } }; if (TakeWay.自提.ToString().Equals(parameter.TakeWay)) { parameter.ShopOrderOtherFee.Remove("配送费"); } } if (shopOrder.ShopOrderSelfHelpId.HasValue) //自助点餐 { var selfHelp = db.GetSingle <ShopOrderSelfHelp>(shopOrder.ShopOrderSelfHelpId.Value); if (selfHelp != null) { parameter.SelfHelpPrintParameter = new SelfHelpPrintParameter() { SelfHelpNumber = selfHelp.Number, DingingWay = selfHelp.IsTakeOut ? "外带" : "堂食" }; } } foreach (var item in printers) { //StringBuilder postData = new StringBuilder("sn=" + item.SN); parameter.SN = item.SN; parameter.Times = item.Times + ""; PrintModel model = printDbContext.Query <PrintModel>().FirstOrDefault(s => s.ID == item.ModelID); if (model == null) { model = printDbContext.Set <PrintModel>().Find(2); } parameter.ModelContent = model.ModelContent; PrinterBase @base = PrinterFactory.Create(item.PrinterType);//) string temp = @base.PrinterRequest(parameter, item); #region 数据库操作 PrintRecord record = new PrintRecord(); //处理接口返回数据 Dictionary <string, object> dic = JsonConvert.DeserializeObject <Dictionary <string, object> >(temp); //实体赋值 record.SN = item.SN; record.OrderID = @base.GetOrderID(temp);// dic.ContainsKey("data") && dic["msg"].Equals("ok") ? dic["data"].ToString() : "未能成功打印"; record.Title = parameter.Title; record.OrderList = stringorderlist; record.TotalMoney = (float)parameter.TotalMoney; record.Address = parameter.Address; record.OrderName = parameter.OrderName; record.Mobile = parameter.Mobile; record.OrderTime = Convert.ToDateTime(parameter.OrderTime); record.QRAddress = parameter.QRAddress; record.Remark = shopOrder.Remark; printDbContext.AddTo(record); #endregion } shopOrder.IsPrint = true; } catch (Exception e) { _logger.LogError("打印机错误:{0}", e.Message); } }