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); } }
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 APIResult AddPrinter([FromBody] AddPrinter parameter) { if (string.IsNullOrEmpty(parameter.PrinterName)) { return(Error("打印机名称不能为空")); } if (string.IsNullOrEmpty(parameter.sn)) { return(Error("SnList(打印机#KEY)参数不能为空")); } if (!parameter.shopId.HasValue) { return(Error("商铺ID不能为空")); } if (!parameter.printerType.HasValue) { return(Error("打印机类型不能为空")); } if (!parameter.printWay.HasValue) { return(Error("打印机方式不能为空")); } Shop shop = shopDb.GetSingle <Shop>(parameter.shopId.Value); if (shop == null) { return(Error("商铺不存在")); } //表示是否已经添加过 Data.Printer printer = printDb.Printer.FirstOrDefault(s => !s.IsDel && s.SN == parameter.sn && s.SKey == parameter.skey && s.PrinterType == parameter.printerType); bool IsExists = printer != null; if (IsExists) { if (printer.ShopID == shop.Id) { return(Error("该设备已经添加过")); } else { return(Error("该设备已经被别的商家添加过")); } } PrinterBase @base = PrinterFactory.Create(parameter.printerType.Value);//) string result = @base.AddPrinter(new PrintParameter() { SnList = $"{parameter.sn}#{parameter.skey}#" }); //添加之后保存到数据库中 Dictionary <string, object> tempdic = JsonConvert.DeserializeObject <Dictionary <string, object> >(result); if (!tempdic.ContainsKey("data")) { return(Error("添加失败")); } Data.Printer _printermodel = new Data.Printer(); #region 实体赋值 _printermodel.ShopID = parameter.shopId.Value; _printermodel.SN = parameter.sn; _printermodel.PrinterName = parameter.PrinterName; _printermodel.SKey = parameter.skey; _printermodel.AddTime = DateTime.Now; _printermodel.PrinterType = parameter.printerType.Value; _printermodel.PrintWay = parameter.printWay.Value; _printermodel.IsEnable = true; _printermodel.IsSuccess = true; //_printermodel.Times = string.IsNullOrEmpty(parameter.Times) || int.TryParse(parameter.Times, out int t) ? 1 : t == 0 ? 1 : t; Dictionary <string, object> datadic = JsonConvert.DeserializeObject <Dictionary <string, object> >(tempdic["data"].ToString()); JArray ok = datadic["ok"] as JArray; JArray no = datadic["no"] as JArray; //验证打印机是否在接口添加成功 //如果之前添加失败了, 将修改原来的数据,否则重新添加 #endregion if (ok.Count > 0) { printDb.AddTo(_printermodel); printDb.SaveChanges(); return(Success(_printermodel)); } else if (no.Count > 0) { string mes = no[0].Value <string>(); Regex regex = new Regex(@".*添加过.*"); if (regex.IsMatch(mes)) { printDb.AddTo(_printermodel); printDb.SaveChanges(); return(Success(_printermodel)); } else { return(Error(mes)); } } else { return(Error("添加打印机到供应商后台失败")); } }