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("添加打印机到供应商后台失败")); } }
private void chooserPrinter_Click(object sender, RoutedEventArgs e) { AddPrinter addWnd = new AddPrinter(); addWnd.ShowDialog(); }