using ButcherFactory.BO; using ButcherFactory.BO.LocalBL; 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; namespace ButcherFactory.CarcassTakeOut_ { public partial class CarcassTakeOutForm : Form, IWithRoleForm { #region IWithRoleForm public List RoleName { get { return new List { (short)设备类别.白条领用 }; } } public Form Generate() { return this; } #endregion Thread syncBeforeInfo; Thread uploadData; BindingList needSubmitedList; BindingList historyList; BindingList weightList; long? workUnitID; public CarcassTakeOutForm() { InitializeComponent(); 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 CarcassTakeOutFormConfig { WorkUnitID = workUnitID }); }; 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(() => { if (netStateWatch1.NetState) { BaseInfoSyncRpc.SyncGoodsByTag(ApplyClient.白条出入库); BaseInfoSyncRpc.SyncBaseInfo(); } var config = XmlUtil.DeserializeFromFile(); workUnitSelect.EBindComboBox(x => x.ID == config.WorkUnitID); BindGrid(); })); } void BindGrid() { weightList = CarcassTakeOutBL.GetWeightList(); weightGrid.DataSource = weightList; weightGrid.Refresh(); needSubmitedList = CarcassTakeOutBL.GetLocalDataWithState(false); needSubmitGrid.DataSource = needSubmitedList; needSubmitGrid.Refresh(); historyList = CarcassTakeOutBL.GetLocalDataWithState(true); 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.BeforeWeight == null&&!string.IsNullOrEmpty(x.BarCode)).Take(5); if (!list.Any()) { list = historyList.Where(x => x.BeforeWeight == null && !string.IsNullOrEmpty(x.BarCode)).Take(5); ff = false; } if (list.Any()) { var back = CarcassTakeOutBL.GetBeforeInfo(list.Select(x => x.BarCode)); if (back.Any()) { foreach (var item in back) { var f = list.First(x => x.BarCode == item.StringExt1); f.BeforeWeight = 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) CarcassTakeOutBL.UploadCarcassInfo(); })); } Thread.Sleep(2000); } } void uScanPanel1_AfterScan() { var barCode = uScanPanel1.TextBox.Text.Trim(); if (string.IsNullOrEmpty(barCode)) throw new Exception("请先扫码"); if (barCode.Length != 23) throw new Exception("条码格式不正确"); FillCode(barCode); } void FillCode(string barCode) { bool isNew; var entity = CarcassTakeOutBL.InsertOrUpdate(workUnitID, barCode, out isNew); if (isNew) { needSubmitedList.Insert(0, entity); needSubmitGrid.Refresh(); } } private void closeBtn_Click(object sender, EventArgs e) { Close(); } private void submitBtn_Click(object sender, EventArgs e) { if (weightList.Count == 0) throw new Exception("没有称重记录"); if (needSubmitedList.Count == 0) throw new Exception("没有扫码记录"); var weight = weightList.Sum(x => x.Weight); var arr = needSubmitedList.ToList(); CarcassTakeOutBL.Submit(weight, arr); arr.Reverse(); foreach (var item in arr) { historyList.Insert(0, item); needSubmitedList.Remove(item); } weightList.Clear(); historyDataGrid.FirstDisplayedScrollingRowIndex = 0; historyDataGrid.Refresh(); needSubmitGrid.Refresh(); weightGrid.Refresh(); } private void readBtn_Click(object sender, EventArgs e) { if (uWeightControl1.Weight == 0) throw new Exception("重量为0,不能读入"); var entity = CarcassTakeOutBL.InsertWeight(uWeightControl1.Weight); weightList.Insert(0, entity); weightGrid.FirstDisplayedScrollingRowIndex = 0; weightGrid.Refresh(); } private void noBarCode_Click(object sender, EventArgs e) { FillCode(string.Empty); } } }