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; 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() { 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).Take(5); if (!list.Any()) { list = historyList.Where(x => x.BeforeWeight == null).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 bar = uScanPanel1.TextBox.Text.Trim(); if (string.IsNullOrEmpty(bar)) throw new Exception("请先扫码"); if (bar.Length != 23) throw new Exception("条码格式不正确"); bool isNew; var entity = CarcassTakeOutBL.InsertOrUpdate(workUnitID, bar,out isNew); if (isNew) { needSubmitedList.Add(entity); needSubmitGrid.Refresh(); } } private void closeBtn_Click(object sender, EventArgs e) { Close(); } private void submitBtn_Click(object sender, EventArgs e) { var arrs = needSubmitedList.Where(x => x.Weight.HasValue).ToList(); CarcassTakeOutBL.Submit(arrs.Select(x => x.ID)); foreach (var item in arrs) { historyList.Add(item); needSubmitedList.Remove(item); } historyDataGrid.Refresh(); needSubmitGrid.Refresh(); } private void deleteBtn_Click(object sender, EventArgs e) { var tag = needSubmitGrid.SelectedRows.Cast().FirstOrDefault(); if (tag!=null) { CarcassTakeOutBL.Delete(tag.ID); needSubmitedList.Remove(tag); needSubmitGrid.Refresh(); } } private void readBtn_Click(object sender, EventArgs e) { if (uWeightControl1.Weight == 0) throw new Exception("重量为0,不能读入"); var tags = needSubmitedList.Where(x => x.Weight == null); if(!tags.Any()) return; CarcassTakeOutBL.SetWeight(tags.Select(x => x.ID), uWeightControl1.Weight); var id = tags.Max(x=>x.ID); foreach (var item in tags) { if (item.ID == id) item.Weight = uWeightControl1.Weight; else item.Weight = 0; } needSubmitGrid.Refresh(); } } }