using DevComponents.DotNetBar; using DevComponents.DotNetBar.Controls; using POSV.Card; using POSV.Component; using POSV.Entity; using POSV.Helper; using POSV.MessageEvent; using POSV.MsgCenter; using POSV.OtherWaiMai; using POSV.ServiceCenter; using POSV.ShoppingCart; using POSV.Utils; using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Linq; using System.Text; using System.Threading; using System.Threading.Tasks; using System.Windows.Forms; namespace POSV.WaiMai { public partial class BaiduForm : BusinessForm { private int orderType = 0;//订单类型 private int SelectType = 0;//待处理订单 private bool generalQuerying = false;//历史订单查询锁 private bool handleQuerying = false;//待处理订单查询锁 private int ConfirmType = 0; public BaiduForm() { InitializeComponent(); this.dateInputPicker.Value = DateTime.Now.Date; //是否开启自动接单 bool openConfirm = Global.Instance.GlobalConfigBoolValue(ConfigConstant.BAIDU_CONFIRM_PARAMETER, false); if (openConfirm) { ConfirmType = 1; this.radioButtonOpen.Checked = true; } else { ConfirmType = 0; this.radioButtonClose.Checked = true; } } protected override void OnLoad(EventArgs e) { base.OnLoad(e); //第一步加载门店信息 QueryBaiDuInfo(); //第二步,获取待处理订单 MqttClientUtils.Instance.OnMessageReceived += Instance_OnMessageReceived; DoHandQuery(); } private void Instance_OnMessageReceived(object sender, MessageEventArgs e) { try { string message = e.Message; int type = e.Type; if (type == 0) { Dictionary messageMap = JsonUtils.Deserialize>(message); if (messageMap.ContainsKey("baiduwaimai") && !"".Equals(messageMap["baiduwaimai"])) { DoHandQuery(); } } } catch (Exception ex) { LOGGER.Error(ex); } } private void OnControlBoxCloseClick(object sender, EventArgs e) { OnCloseTouchClick(sender, e); } private void OnControlBoxMinClick(object sender, EventArgs e) { this.WindowState = FormWindowState.Minimized; } private void btn_exit_Click(object sender, EventArgs e) { OnCloseTouchClick(sender, e); } private void OnCloseTouchClick(object sender, EventArgs e) { //先关闭父窗体 if (this.Owner != null) { this.Owner.Close(); } //再关闭当前窗体 this.Close(); } /// /// 门店营业 /// /// /// private void btn_openStore(object sender, EventArgs e) { BaiDuShopOpenRequest request = new BaiDuShopOpenRequest(); request.StoreId = Global.Instance.BusinessPlanLog.StoreId; var response = OtherWaiMaiUtils.BaiDuShopOpen(request); if (response != null) { this.ShowToastNotify(this, response.Item2); if (response.Item1) { QueryBaiDuInfo(); } } } /// /// 门店休息 /// /// /// private void btn_closeStore(object sender, EventArgs e) { BaiDuShopCloseRequest request = new BaiDuShopCloseRequest(); request.StoreId = Global.Instance.BusinessPlanLog.StoreId; var response = OtherWaiMaiUtils.BaiDuShopClose(request); if (response != null) { this.ShowToastNotify(this, response.Item2); if (response.Item1) { QueryBaiDuInfo(); } } } private void QueryBaiDuInfo() { Task.Factory.StartNew(() => { BaiDuShopInfoRequest request = new BaiDuShopInfoRequest(); request.StoreId = Global.Instance.BusinessPlanLog.StoreId; var response = OtherWaiMaiUtils.BaiDuShopInfo(request); if (response != null && response.Item1) { this.Invoke(new Action(() => { var responseMessage = response.Item3; if (responseMessage != null && responseMessage.Data != null) { BaiDuShopInfoResponse PoiInfo = responseMessage.Data; this.StoreNameLabelX.Text = PoiInfo.Name;//门店名称 switch (PoiInfo.Status) { case 1: this.isOpenLabelX.Text = "正常营业"; break; case 3: this.isOpenLabelX.Text = "休息中"; break; case 9: this.isOpenLabelX.Text = "停止营业"; break; default: this.isOpenLabelX.Text = "未营业"; break; } this.openTimeLabelX.Text = string.Format("{0}-{1}", PoiInfo.Start, PoiInfo.End); } })); } }); } /// /// 关闭窗口 /// /// /// private void btn_close(object sender, EventArgs e) { OnCloseTouchClick(sender, e); } /// /// 页签切换 /// /// /// private void tab_onclick(object sender, EventArgs e) { var obj = sender as SuperTabItem; var name = obj.Tag as string; switch (name) { case "superTabItem1"://待处理订单 SelectType = 0; break; case "superTabItem2"://历史订单 SelectType = 1; break; } } /// /// 待处理订单 /// /// /// private void btn_getHandOrder(object sender, EventArgs e) { DoHandQuery(); } /// /// 获取待处理订单 /// private void DoHandQuery() { if (handleQuerying) { this.ShowToastNotify(this, "查询中....."); return; } handleQuerying = true; this.tableLayoutPanel1.Controls.Clear(); Task.Factory.StartNew(() => { BaiDuOrderHandleRequest request = new BaiDuOrderHandleRequest(); string orderDate = this.dateInputPicker.Text; request.StoreId = Global.Instance.BusinessPlanLog.StoreId; var response = OtherWaiMaiUtils.QueryBaiDuOrderHandle(request); if (response != null && response.Item1) { if (this.IsDisposed || !this.IsHandleCreated) return; this.Invoke(new Action(() => { List detailList = new List(); foreach (BaiDuOrderHandleResponse order in response.Item3.List) { var item = new BaiDuItem(order); item.OnConfirm += Item_OnConfirm; item.OnCancel += Item_OnCancel; item.OnOrderPrint += Item_OnPrint; detailList.Add(item); } if (detailList.Count > 0) { this.tableLayoutPanel1.Controls.AddRange(detailList.ToArray()); //滚动至顶部 this.tableLayoutPanel1.AutoScrollPosition = new Point(0, 0); } //更新控件 sendNewView(response.Item3.List.Count); // 是否自动接单 bool openConfirm = Global.Instance.GlobalConfigBoolValue(ConfigConstant.BAIDU_CONFIRM_PARAMETER, false); if (openConfirm && response.Item3.List != null && response.Item3.List.Count > 0) { foreach (BaiDuOrderHandleResponse order in response.Item3.List) { LOGGER.Info("百度外卖自动接单"); OrderOnConfirm(order.OrderBody.OrderData.OrderInfo.Order_id); //接一单休息2秒钟 Thread.Sleep(2 * 1000); } } })); } handleQuerying = false; }); } /// /// 订单取消 /// /// /// private void Item_OnCancel(object sender, BaiDuOrderEventArgs e) { string orderId = e.OrderId; BaiDuOrderCancelRequest request = new BaiDuOrderCancelRequest(); request.StoreId = Global.Instance.BusinessPlanLog.StoreId; request.OrderId = orderId; request.Type = 3; request.Reason = "对不起,您点的美食已售完"; var response = OtherWaiMaiUtils.BaiDuOrderCancel(request); if (response != null) { LOGGER.Info("百度订单取消:" + response.Item2); this.ShowToastNotify(this, response.Item2); if (response.Item1) { switch (SelectType) { case 0: DoHandQuery(); break; case 1: DoQuery(1, pagerControl2.RowsPerPage); break; } } } } /// /// 订单接单 /// /// /// private void Item_OnConfirm(object sender, BaiDuOrderEventArgs e) { LOGGER.Info("百度外卖手动接单"); string orderId = e.OrderId; OrderOnConfirm(orderId); } public void OrderOnConfirm(string orderId) { BaiDuOrderConfirmRequest request = new BaiDuOrderConfirmRequest(); request.StoreId = Global.Instance.BusinessPlanLog.StoreId; request.OrderId = orderId; var response = OtherWaiMaiUtils.BaiDuOrderConfirm(request); if (response != null) { LOGGER.Info("百度订单接单:" + response.Item2); this.ShowToastNotify(this, response.Item2); if (response.Item1) { try { BaiDuOrderRequest request1 = new BaiDuOrderRequest(); request1.StoreId = Global.Instance.BusinessPlanLog.StoreId; request1.OrderId = orderId; var response1 = OtherWaiMaiUtils.BaiDuOrder(request1); this.Invoke(new Action(() => { if (response1 != null && response1.Item1) { //组装本地数据 OrderObject orderObject = OtherWaiMaiApi.getOrderLocalBaiDu(response1.Item3.Data); LOGGER.Info("百度外卖组装本地订单"); lock (Global.Instance.SyncLock) { using (var db = Global.Instance.OpenDataBase) { using (var trans = db.GetTransaction()) { //遍历获取成本价 foreach (OrderItem item in orderObject.Items) { ProductSpec productSpec = Global.ProductSpec._productSpecList.Find(x => x.Id.Equals(item.SpecId)); if (productSpec != null) { item.CostPrice = productSpec.CostPrice; } else { item.CostPrice = 0.00M; } } //整单 db.Insert(orderObject); //单品明细 db.InsertBatch(orderObject.Items); //添加支付汇总的必填字段 foreach (PayItem payItem in orderObject.Pays) { var payMode = OrderUtils.GetPayMode(payItem.No); if (payMode == null) { payItem.IncomeFlag = 1; payItem.OtherRateType = 0; payItem.OtherRateValue = 0.00M; } else { payItem.IncomeFlag = payMode.IncomeFlag; payItem.OtherRateType = payMode.OtherRateType; payItem.OtherRateValue = payMode.OtherRateValue; } } //支付明细 db.InsertBatch(orderObject.Pays); //做法明细 foreach (var flavor in orderObject.Items) { if (flavor.Flavors.Count > 0) { db.InsertBatch(flavor.Flavors); } } //更新是否有订单需要上传的状态 LOGGER.Info("有需要上传的营业数据,isHaveUpLoadTicket = true"); Global.isHaveUpLoadTicket = true; LOGGER.Info("有需要同步到服务中心的订单,isHaveCenterTicket = true"); Global.isHaveCenterTicket = true; trans.Complete(); } } Thread.Sleep(100); } LOGGER.Info("百度外卖存储本地订单"); Task.Factory.StartNew(() => { this.ShowToastNotify(this, "开始打印"); LOGGER.Info("百度外卖接单打印"); //构建收银小票模版参数 var vars = WaiMaiHelper.BuilderBaiDuVariable(response1.Item3.Data.OrderBody.OrderData.Shop.Name, response1.Item3.Data, false); Tuple result = WaiMaiHelper.PrinterTicket("百度外卖", vars); this.ShowToastNotify(this, string.Format("{0}", result.Item2)); //打印标签 LOGGER.Info("百度外卖标签打印"); LabelPrintHelper.PrintOrderLabel(orderObject); //执行厨打 LOGGER.Info("百度外卖执行厨打"); ChuDaHelper.KitchenPrinter(orderObject); //执行厨打标签 ChuDaHelper.KitchenLabelPrinter(orderObject); LOGGER.Info("百度外卖厨打标签成功"); //执行出品 LOGGER.Info("百度外卖执行出品"); ChuDaHelper.MadePrinter(orderObject); //通知厨显 LOGGER.Info("百度外卖通知厨显"); UploadSCNewOrder.UploadNewOrder2SC4KDS(orderObject); }); } })); } catch (Exception ex) { LOGGER.Error(ex, "美团接单异常"); } switch (SelectType) { case 0: DoHandQuery(); break; case 1: DoQuery(1, pagerControl2.RowsPerPage); break; } } } } /// /// 订单打印 /// /// /// private void Item_OnPrint(object sender, BaiDuOrderEventArgs e) { Task.Factory.StartNew(() => { string orderId = e.OrderId; BaiDuOrderRequest request = new BaiDuOrderRequest(); request.StoreId = Global.Instance.BusinessPlanLog.StoreId; request.OrderId = orderId; var response = OtherWaiMaiUtils.BaiDuOrder(request); if (response != null && response.Item1) { this.ShowToastNotify(this, "开始打印"); //构建收银小票模版参数 var vars = WaiMaiHelper.BuilderBaiDuVariable(response.Item3.Data.OrderBody.OrderData.Shop.Name, response.Item3.Data, true); Tuple result = WaiMaiHelper.PrinterTicket("百度外卖", vars); this.ShowToastNotify(this, string.Format("{0}", result.Item2)); //组装本地数据 OrderObject orderObject = OtherWaiMaiApi.getOrderLocalBaiDu(response.Item3.Data); //打印标签 LabelPrintHelper.PrintOrderLabel(orderObject); //执行厨打 ChuDaHelper.KitchenPrinter(orderObject); //执行厨打标签 ChuDaHelper.KitchenLabelPrinter(orderObject); //执行出品 ChuDaHelper.MadePrinter(orderObject); ////通知厨显 UploadSCNewOrder.UploadNewOrder2SC4KDS(orderObject); } }); } private void DoQuery(int pageNum, int pageSize) { if (generalQuerying) { this.ShowToastNotify(this, "查询中....."); return; } generalQuerying = true; this.tableLayoutPanel2.Controls.Clear(); Task.Factory.StartNew(() => { BaiDuOrderListRequest request = new BaiDuOrderListRequest(); string orderDate = this.dateInputPicker.Text; request.StoreId = Global.Instance.BusinessPlanLog.StoreId; request.PageNumber = pageNum; request.PageSize = pageSize; request.OrderDate = orderDate; request.Status = orderType; var response = OtherWaiMaiUtils.QueryBaiDuOrder(request); if (response != null && response.Item1) { if (this.IsDisposed || !this.IsHandleCreated) return; this.Invoke(new Action(() => { List detailList = new List(); int handCount = 0; foreach (BaiDuOrderListResponse order in response.Item3.List) { if (order.OrderBody.OrderData.OrderInfo.Status == 1) { handCount = handCount + 1; } var item = new BaiDuItem(order); item.OnConfirm += Item_OnConfirm; item.OnCancel += Item_OnCancel; item.OnOrderPrint += Item_OnPrint; detailList.Add(item); } //更新控件 sendNewView(handCount); if (detailList.Count > 0) { this.tableLayoutPanel2.Controls.AddRange(detailList.ToArray()); //滚动至顶部 this.tableLayoutPanel2.AutoScrollPosition = new Point(0, 0); } this.pagerControl2.TotalPage = response.Item3.PageCount; this.pagerControl2.CurrentPage = response.Item3.PageNumber; this.pagerControl2.RecordCount = response.Item3.TotalCount; this.pagerControl2.RowsPerPage = response.Item3.PageSize; })); } generalQuerying = false; }); } /// /// 历史订单查询 /// /// /// private void btn_query(object sender, EventArgs e) { DoQuery(1, pagerControl2.RowsPerPage); } /// /// 是否自动接单 /// /// /// private void cb_change_click(object sender, EventArgs e) { var obj = sender as CheckBoxX; var indexT = obj.Tag as string; int type = 0; if ("开启".Equals(indexT) && obj.Checked) { ConfirmType = 1; } if ("关闭".Equals(indexT) && obj.Checked) { ConfirmType = 0; } var config = new Config(); config.Id = IdWorkerUtils.Instance.NextId(); config.Group = ConfigConstant.DEVICE_GROUP; config.TenantId = Global.Instance.Authc.TenantId; config.Keys = ConfigConstant.BAIDU_CONFIRM_PARAMETER; config.Values = string.Format("{0}", ConfirmType); try { using (var db = Global.Instance.OpenDataBase) { using (var trans = db.GetTransaction()) { db.Save(config); trans.Complete(); } } Global.Instance.ReloadConfig(ConfigConstant.DEVICE_GROUP); if (ConfirmType == 1) { this.ShowToastNotify(this, "开启自动接单"); } else { this.ShowToastNotify(this, "关闭自动接单"); } } catch (Exception ex) { string message = "美团外卖参数更新异常"; LOGGER.Error(ex, message); } } //根据待处理订单的数量刷新提示 public void sendNewView(int orderCount) { //有百度外卖订单 MsgEvent.Send(Constant.MSGCENTER_NOTIFY, new MsgEntity() { Subject = Constant.MENU_TITLE_CHANGED_NOTIFY, SubSubject = ModuleKeyCode.百度外卖.ToString(), Operate = MsgEntityOperate.发布, Data = new Tuple(ModuleKeyCode.百度外卖, MenuTileNotifyType.数量, string.Format("{0}", orderCount)) }); } private void btn_time_Click(object sender, EventArgs e) { var obj = sender as ButtonX; var date = obj.Tag as string; switch (date) { case "today": this.dateInputPicker.Value = DateTime.Parse(DateTime.Now.ToString("yyyy-MM-dd")); break; case "yesterday": this.dateInputPicker.Value = DateTime.Parse(DateTime.Now.AddDays(-1).ToString("yyyy-MM-dd")); break; } DoQuery(1, pagerControl2.RowsPerPage); } } }