using ButcherFactory.BO; using ButcherFactory.BO.Rpcs; using ButcherFactory.BO.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; using ButcherFactory.Utils; using WinFormControl; using ButcherFactory.BO.LocalBL; using ButcherFactory.Dialogs; using ButcherFactory.BO.Bill; namespace ButcherFactory.SegmentPickUp_ { public partial class SegmentPickUpForm : Form, IWithRoleForm { #region IWithRoleForm public List RoleName { get { return new List { (short)设备类别.分割领用 }; } } public Form Generate() { return this; } #endregion const string FilePatch = @"Config\NumberSetDialog.cfg"; Thread syncBeforeInfo; Thread uploadData; BindingList needSubmitedList; BindingList historyList; long? workUnitID; long? batchID; public SegmentPickUpForm() { InitializeComponent(); BuildNumberPanel(); netStateWatch1.GetConnectState = () => LoginUtil.TestConnection(500); uScanPanel1.AfterScan += uScanPanel1_AfterScan; workUnitSelect.SelectedIndexChanged += delegate { if (workUnitSelect.SelectedValue == null) workUnitID = null; else workUnitID = (long)workUnitSelect.SelectedValue; XmlUtil.SerializerObjToFile(new SegmentPickUpConfig { WorkUnitID = workUnitID }); }; productBatchSelect.SelectedIndexChanged += delegate { if (productBatchSelect.SelectedValue == null) batchID = null; else batchID = (long)productBatchSelect.SelectedValue; }; this.FormClosing += delegate { if (syncBeforeInfo != null && syncBeforeInfo.IsAlive) syncBeforeInfo.Abort(); if (uploadData != null && uploadData.IsAlive) uploadData.Abort(); }; } protected override void OnLoad(EventArgs e) { base.OnLoad(e); var initTask = new Thread(LoadBind); initTask.Start(); syncBeforeInfo = new Thread(GetBeforeInfo); syncBeforeInfo.Start(); uploadData = new Thread(UpLoadLocalData); uploadData.Start(); } private void LoadBind() { this.Invoke(new Action(() => { BLUtil.DeleteLocalDb(); if (netStateWatch1.NetState) { BaseInfoSyncRpc.SyncGoodsByTag(ApplyClient.分割领用); BaseInfoSyncRpc.SyncBaseInfo(); BaseInfoSyncRpc.SyncProductBatch(1); } productBatchSelect.EBindComboBox(x => x.Date == DateTime.Today, 6, "Date"); var config = XmlUtil.DeserializeFromFile(); workUnitSelect.EBindComboBox(x => x.ID == config.WorkUnitID); BindGoods(); BindGrid(); })); } List goodsBtns = new List(); void BindGoods() { var goods = FormClientGoodsSetBL.GetGoodsList(); foreach (var item in goods) { var btn = new UButton() { Width = 120, Height = 75, Text = item.Goods_Name, Tag = item.Goods_ID, Font = new Font("宋体", 15), Margin = new Padding(22, 10, 22, 30), PlaySound = true }; btn.Click += GoodsBtnClick; goodsBtns.Add(btn); flowLayoutPanel1.Controls.Add(btn); } } void GoodsBtnClick(object sender, EventArgs e) { if (batchID == null) throw new Exception("请先选择批次"); if (uWeightControl1.Weight == 0) throw new Exception("重量不能为0!"); var c = sender as UButton; var entity = new SegmentPickUp(); entity.RowIndex = GetRowIndex(); entity.Weight = uWeightControl1.Weight; entity.Goods_ID = (long)c.Tag; entity.Goods_Name = c.Text; entity.ProductBatch_ID = batchID; entity.WorkUnit_ID = workUnitID; SegmentPickUpBL.InsertEntityByWeight(entity); needSubmitedList.Insert(0, entity); AfterInsert(); } void BindGrid() { needSubmitedList = SegmentPickUpBL.GetSegmentPickUp(true); needSubmitGrid.DataSource = needSubmitedList; needSubmitGrid.Refresh(); historyList = SegmentPickUpBL.GetSegmentPickUp(false); historyDataGrid.DataSource = historyList; historyDataGrid.Refresh(); } private void GetBeforeInfo() { while (true) { if (this.IsHandleCreated) { this.Invoke(new Action(() => { if (netStateWatch1.NetState) { bool ff = true; var list = needSubmitedList.Where(x => x.Weight == null).Take(5); if (!list.Any()) { list = historyList.Where(x => x.Weight == null).Take(5); ff = false; } if (list.Any()) { var back = SegmentPickUpBL.GetBeforeInfo(list.Select(x => x.BarCode)); if (back.Any()) { foreach (var item in back) { var f = list.First(x => x.BarCode == item.StringExt1); f.Weight = item.DecimalExt1; f.Goods_Name = item.StringExt2; } if (ff) needSubmitGrid.Refresh(); else historyDataGrid.Refresh(); } } } })); } Thread.Sleep(2000); } } private void UpLoadLocalData() { while (true) { if (this.IsHandleCreated) { this.Invoke(new Action(() => { if (netStateWatch1.NetState) SegmentPickUpBL.UploadSegmentInfo(); })); } Thread.Sleep(2000); } } void uScanPanel1_AfterScan() { var barCode = uScanPanel1.TextBox.Text.Trim(); if (string.IsNullOrEmpty(barCode)) throw new Exception("请先扫码"); if (barCode.Length != 24) throw new Exception("条码格式不正确"); if (needSubmitedList.Any(x => x.BarCode == barCode) || historyList.Any(x => x.BarCode == barCode)) return; var entity = new SegmentPickUp(); entity.BarCode = barCode; entity.WorkUnit_ID = workUnitID; entity.RowIndex = GetRowIndex(); SegmentPickUpBL.InsertEntityByCode(entity); needSubmitedList.Insert(0, entity); AfterInsert(); uScanPanel1.TextBox.Clear(); } void AfterInsert() { needSubmitGrid.FirstDisplayedScrollingRowIndex = 0; needSubmitGrid.Rows[0].Selected = true; needSubmitGrid.Refresh(); } int GetRowIndex() { var f = needSubmitedList.FirstOrDefault(x => x.CreateTime.Date == DateTime.Today); if (f != null) return f.RowIndex.Value + 1; else if (historyList.Any()) return historyList.First().RowIndex.Value + 1; else return 1; } private void closeBtn_Click(object sender, EventArgs e) { Close(); } private void submitBtn_Click(object sender, EventArgs e) { if (needSubmitedList.Count == 0) throw new Exception("没有待提交记录"); var arr = needSubmitedList.ToList(); SegmentPickUpBL.UpdateSubmit(needSubmitedList.Select(x => x.ID)); foreach (var item in arr) { historyList.Add(item); if (historyList.Count > 50) historyList.RemoveAt(50); needSubmitedList.Remove(item); } historyDataGrid.FirstDisplayedScrollingRowIndex = 0; historyDataGrid.Refresh(); needSubmitGrid.Refresh(); } private void numSetBtn_Click(object sender, EventArgs e) { new NumberSetDialog().ShowDialog(); BuildNumberPanel(); } void BuildNumberPanel() { numFlowPanel.Controls.Clear(); if (!System.IO.File.Exists(FilePatch)) return; var simpBtn = new UButton() { Width = 100, Height = 34, Text = "自定义", Font = new Font("宋体", 15), Margin = new Padding(6, 2, 6, 0), WithStataHode = true, EnableGroup = true }; simpBtn.Click += delegate { var cr = GetCurrentRowEntity(); if (cr == null) return; var keyBoard = new NumberPad(); if (keyBoard.ShowDialog() == true) { var v = 0; if (int.TryParse(keyBoard.Result, out v) && v > 0) { cr.Number = v; SegmentPickUpBL.UpdateNumber(cr.ID, cr.Number); needSubmitGrid.Refresh(); } else throw new Exception("输入数量有误!"); } }; numFlowPanel.Controls.Add(simpBtn); var arr = System.IO.File.ReadAllText(FilePatch).Split(new char[] { ',' }, StringSplitOptions.RemoveEmptyEntries).Reverse(); foreach (var item in arr) { var btn = new UButton() { Width = 100, Height = 34, Text = item, Font = new Font("宋体", 15), Margin = new Padding(6, 2, 6, 0), WithStataHode = true, EnableGroup = true }; btn.Click += (sender, e) => { var row = GetCurrentRowEntity(); if (row == null) return; var b = sender as UButton; row.Number = int.Parse(b.Text); SegmentPickUpBL.UpdateNumber(row.ID, row.Number); needSubmitGrid.Refresh(); }; numFlowPanel.Controls.Add(btn); } } SegmentPickUp GetCurrentRowEntity() { if (needSubmitGrid.CurrentRow == null) return null; var row = needSubmitGrid.CurrentRow.DataBoundItem as SegmentPickUp; if (string.IsNullOrEmpty(row.BarCode)) return row; return null; } } }