using BO; using BO.BO.BaseInfo; using BO.BO.Bill; using BO.Utils; using BO.Utils.BillRpc; using BWP.WinFormControl; using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.IO; using System.Linq; using System.Reflection; using System.Text; using System.Threading; using System.Threading.Tasks; using System.Windows.Forms; namespace WeighAndGrading { enum ModifyType { None = 0, Tech = 1, Index = 2, Order = 3, Weight = 4 } public partial class DataViewForm : Form, IAfterLogin { #region IAfterLogin public string RoleName { get { return "定级校验"; } } public Form Generate() { if (string.IsNullOrEmpty(ButcherAppContext.Context.UrlConfig.OfflineSqlConnection)) throw new Exception("请先设置离线数据库并保存"); if (!LocalDmoSession.ConnectionTest()) throw new Exception("离线数据库连接失败"); return this; } #endregion ModifyType modifyType = ModifyType.None; private delegate void InvokeHandler(); public const string DATA_PATH = "G_A_W_Data"; const short TANG_TECH = 0; const short MAO_TECH = 1; List tangList; List maoList; BindingList details; string discontPath = Path.Combine(DATA_PATH, "Disconts.xml"); bool connection = false; bool onWorking = false; Thread syncTangGrid, syncMaoGrid, syncDetailGrid; public DataViewForm() { InitializeComponent(); butcherTimeInput.Date = DateTime.Today; tangGridView.AutoGenerateColumns = false; tangGridView.DataSource = null; maoGridView.AutoGenerateColumns = false; maoGridView.DataSource = null; historyGrid.AutoGenerateColumns = false; historyGrid.DataSource = null; tangList = new List(); maoList = new List(); if (!Directory.Exists(DATA_PATH)) Directory.CreateDirectory(DATA_PATH); connection = ButcherAppContext.Context.UserConfig.Connection; AddLivestockBtn(); BuildDiscontPanel(true); AddKeyPad(); this.FormClosing += delegate { if (syncTangGrid != null && syncTangGrid.IsAlive) syncTangGrid.Abort(); if (syncMaoGrid != null && syncMaoGrid.IsAlive) syncMaoGrid.Abort(); if (syncDetailGrid != null && syncDetailGrid.IsAlive) syncDetailGrid.Abort(); }; } private void AddKeyPad() { for (var i = 1; i < 10; i++) { var btn = new Button() { Name = "_" + i, Text = i.ToString(), Size = new Size(70, 70), TextAlign = ContentAlignment.MiddleCenter, Margin = new Padding { All = 8 }, Font = new Font("宋体", 15) }; btn.Click += (sender, e) => { numberInput.Text += btn.Text; }; keyPanel.Controls.Add(btn); } var zero = new Button() { Name = "_0", Text = "0", Size = new Size(70, 70), TextAlign = ContentAlignment.MiddleCenter, Margin = new Padding { All = 8 }, Font = new Font("宋体", 15) }; zero.Click += (sender, e) => { if (!string.IsNullOrEmpty(numberInput.Text)) numberInput.Text += "0"; }; keyPanel.Controls.Add(zero); var dot = new Button() { Name = "_dot", Text = ".", Size = new Size(70, 70), TextAlign = ContentAlignment.MiddleCenter, Margin = new Padding { All = 8 }, Font = new Font("宋体", 15) }; dot.Click += (sender, e) => { if (!string.IsNullOrEmpty(numberInput.Text)) numberInput.Text += "."; }; keyPanel.Controls.Add(dot); var back = new Button() { Name = "_back", Text = "←", Size = new Size(70, 70), TextAlign = ContentAlignment.MiddleCenter, Margin = new Padding { All = 8 }, Font = new Font("宋体", 15) }; back.Click += (sender, e) => { if (!string.IsNullOrEmpty(numberInput.Text)) numberInput.Text = numberInput.Text.Substring(0, numberInput.Text.Length - 1); }; keyPanel.Controls.Add(back); var clear = new Button() { Name = "_clear", Text = "清空", Size = new Size(70, 70), TextAlign = ContentAlignment.MiddleCenter, Margin = new Padding { All = 8 }, Font = new Font("宋体", 15) }; clear.Click += (sender, e) => { numberInput.Text = null; }; keyPanel.Controls.Add(clear); SetMargin(keyPanel, 7); } GradeAndWeight_Detail modifyDetail; void JiBieButtonClick(Button btn) { if (details == null) { MessageBox.Show("请先同步数据"); return; } try { btn.Enabled = false; Application.DoEvents(); var livestockTag = btn.Tag as CTuple; var tech = livestockTag.Item3 == TANG_TECH ? "烫褪" : "毛剥"; if (modifyDetail == null) { //不需要打码 //StartPrintNewEntity(tech); } else { UpdateDetial(modifyDetail, livestockTag); cancelBtn_Click(btn, EventArgs.Empty); } } catch { throw; } finally { btn.Enabled = true; } } void UpdateDetial(GradeAndWeight_Detail detail, CTuple btnTag) { detail.Livestock_ID = btnTag.Item1; detail.Livestock_Name = btnTag.Item2; detail.Technics = btnTag.Item3; detail.Technics_Name = detail.Technics == 0 ? "烫褪" : "毛剥"; var current = btnTag.Item3 == 0 ? tangEntity : maoEntity; if (current != null) { detail.Order = current.Order; detail.OrderDetail_ID = current.OrderDetail_ID; } var updateFileNames = new List { "Order", "OrderDetail_ID", "Livestock_ID", "Livestock_Name", "Technics", "Technics_Name" }; LocalGradeAndWeightBL.Update(detail, updateFileNames.ToArray()); } void AddLivestockBtn() { var livestocks = new List>(); var fileName = Path.Combine(DATA_PATH, "Livestocks.xml"); if (connection) { livestocks = BaseInfoRpcUtil.GetLivestockList(); XmlUtil.SerializerObjToFile(livestocks, fileName); } else livestocks = XmlUtil.DeserializeFromFile>>(fileName); foreach (var item in livestocks) { var btn = new Button() { Name = "_" + item.Item1, Text = item.Item2, Tag = item, Size = new Size(90, 75), TextAlign = ContentAlignment.MiddleCenter, Margin = new Padding { All = 15 }, Font = new Font("宋体", 18) }; btn.Click += (sender, e) => { JiBieButtonClick(sender as Button); }; if (item.Item3 == TANG_TECH) { ttPanel.Controls.Add(btn); } else { mbPanel.Controls.Add(btn); } } SetMargin(ttPanel, 3); SetMargin(mbPanel, 3); } void SetMargin(FlowLayoutPanel panel, int prerRowNumber) { for (var i = 0; i < panel.Controls.Count; i++) { var c = panel.Controls[i]; if (i % prerRowNumber == 0)//left c.Margin = new Padding(0, c.Margin.Top, c.Margin.Right, c.Margin.Bottom); if ((i + 1) % prerRowNumber == 0)//right c.Margin = new Padding(c.Margin.Left, c.Margin.Top, 0, c.Margin.Bottom); if (i < prerRowNumber)//firstRow c.Margin = new Padding(c.Margin.Left, 0, c.Margin.Right, c.Margin.Bottom); } } private void syncBtn_Click(object sender, EventArgs e) { details = LocalGradeAndWeightBL.GetDetails(butcherTimeInput.Date.Value, 50); ThreadStartOrAbort(ref syncTangGrid, RefreshTangData); ThreadStartOrAbort(ref syncMaoGrid, RefreshMaoData); ThreadStartOrAbort(ref syncDetailGrid, RefreshDetailData); ChangeSyncBtnState(); } void ThreadStartOrAbort(ref Thread thread, ThreadStart task) { if (thread == null || !thread.IsAlive) { thread = new Thread(task); thread.Start(); } else { thread.Abort(); } } void ChangeSyncBtnState() { onWorking = !onWorking; butcherTimeInput.Enabled = !onWorking; if (onWorking) { syncBtn.BackColor = Color.FromArgb(15, 215, 107); syncBtn.ForeColor = Color.White; } else { syncBtn.BackColor = Color.FromKnownColor(KnownColor.Control); syncBtn.ForeColor = Color.FromKnownColor(KnownColor.ControlText); } } void RefreshTangData() { while (true) { GetLeftList(ref tangList, true); this.Invoke(new InvokeHandler(delegate { BindTangGrid(); })); Thread.Sleep(5000); } } void RefreshMaoData() { while (true) { GetLeftList(ref maoList, false); this.Invoke(new InvokeHandler(delegate { BindMaoGrid(); })); Thread.Sleep(5000); } } void RefreshDetailData() { while (true) { details = LocalGradeAndWeightBL.GetDetails(butcherTimeInput.Date.Value, 50); this.Invoke(new InvokeHandler(delegate { BindDetailGrid(); })); Thread.Sleep(3000); } } void GetLeftList(ref List list, bool tang) { VerifyConnection(); if (connection) { try { list = GradeAndWeightRpc.GetGradeAndWeightList(butcherTimeInput.Date.Value, tang); } catch (TimeoutException) { } } } void BindTangGrid() { tangGridView.DataSource = tangList.OrderBy(x => x.Order).OrderBy(x => x.Finish).ToList(); if (tangEntity == null && tangGridView.CurrentRow != null) { tangEntity = tangGridView.CurrentRow.DataBoundItem as GradeAndWeight; } foreach (DataGridViewRow row in tangGridView.Rows) { if ((bool)row.Cells["T_Finish"].Value) row.DefaultCellStyle.BackColor = Color.YellowGreen; if (tangEntity != null && tangEntity.OrderDetail_ID == (long)row.Cells["T_OrderDetail_ID"].Value) { tangEntity = row.DataBoundItem as GradeAndWeight; if (tangEntity.Finish) row.DefaultCellStyle.BackColor = Color.Yellow; else row.DefaultCellStyle.BackColor = tangGridView.RowsDefaultCellStyle.SelectionBackColor; } } InitTangScrollBar(); tangGridView.ClearSelection(); try { if (tangRoll != -1) tangGridView.FirstDisplayedScrollingRowIndex = tangRoll; } catch { tangRoll = -1; } tangGridView.Refresh(); } void BindMaoGrid() { maoGridView.DataSource = maoList.OrderBy(x => x.Order).OrderBy(x => x.Finish).ToList(); if (maoEntity == null && maoGridView.CurrentRow != null) { maoEntity = maoGridView.CurrentRow.DataBoundItem as GradeAndWeight; } foreach (DataGridViewRow row in maoGridView.Rows) { if ((bool)row.Cells["M_Finish"].Value) row.DefaultCellStyle.BackColor = Color.YellowGreen; if (maoEntity != null && maoEntity.OrderDetail_ID == (long)row.Cells["M_OrderDetail_ID"].Value) { maoEntity = row.DataBoundItem as GradeAndWeight; if (maoEntity.Finish) row.DefaultCellStyle.BackColor = Color.Yellow; else row.DefaultCellStyle.BackColor = maoGridView.RowsDefaultCellStyle.SelectionBackColor; } } InitMaoScrollBar(); maoGridView.ClearSelection(); try { if (maoRoll != -1) maoGridView.FirstDisplayedScrollingRowIndex = maoRoll; } catch { maoRoll = -1; } maoGridView.Refresh(); } void BindDetailGrid() { historyGrid.DataSource = details; foreach (DataGridViewRow row in historyGrid.Rows) { if ((bool)row.Cells["H_IsDrop"].Value) row.DefaultCellStyle.BackColor = Color.Red; if (modifyDetail != null && modifyDetail.SID == (long)row.Cells["H_SID"].Value) { modifyDetail = row.DataBoundItem as GradeAndWeight_Detail; row.DefaultCellStyle.BackColor = maoGridView.RowsDefaultCellStyle.SelectionBackColor; } } InitDetailScrollBar(); historyGrid.ClearSelection(); try { if (rightRoll != -1) historyGrid.FirstDisplayedScrollingRowIndex = rightRoll; } catch { rightRoll = -1; } historyGrid.Refresh(); } private void printBtn_Click(object sender, EventArgs e) { if (modifyDetail == null) { return; } StartPrintExistEntity(modifyDetail); cancelBtn_Click(sender, EventArgs.Empty); } void StartPrintExistEntity(GradeAndWeight_Detail modifyDetail) { if (string.IsNullOrWhiteSpace(modifyDetail.Technics_Name)) { return; } var entity = WeightGradePrint.CreatePrintEntity(butcherTimeInput.Date.Value, modifyDetail.Technics_Name, modifyDetail.BarCode); if (isPrintCheckBox.Checked) { WeightGradePrint.Print(entity); } } private void historyGrid_CellClick(object sender, DataGridViewCellEventArgs e) { if (e.RowIndex < 0) return; var label = "级别"; switch (e.ColumnIndex) { case 4: modifyType = ModifyType.Index; label = "序号"; break; case 5: modifyType = ModifyType.Order; label = "顺序"; break; case 8: modifyType = ModifyType.Weight; label = "重量"; break; default: break; } var currentRow = historyGrid.CurrentRow.DataBoundItem as GradeAndWeight_Detail; if (modifyDetail != null) { foreach (DataGridViewRow row in historyGrid.Rows) { if (modifyDetail.SID == (long)row.Cells["H_SID"].Value) { row.DefaultCellStyle.BackColor = historyGrid.RowsDefaultCellStyle.BackColor; break; } } } modifyDetail = currentRow; historyGrid.Refresh(); stateLabel.Text = string.Format("您正在修改序号为 {0} 的", modifyDetail.Index); flagLabel.Text = label; if (label != "级别") withOkPanel.Show(); else withOkPanel.Hide(); modifyPanel.Show(); } private void tangGridView_CellClick(object sender, DataGridViewCellEventArgs e) { if (e.RowIndex == -1) return; if (e.ColumnIndex == tangGridView.Columns.Count - 1) return; var entity = tangGridView.CurrentRow.DataBoundItem as GradeAndWeight; if (tangEntity != null) { foreach (DataGridViewRow row in tangGridView.Rows) { if (tangEntity.OrderDetail_ID == (long)row.Cells["T_OrderDetail_ID"].Value) { row.DefaultCellStyle.BackColor = tangEntity.Finish ? Color.YellowGreen : tangGridView.RowsDefaultCellStyle.BackColor; break; } } } tangEntity = entity; tangGridView.CurrentRow.DefaultCellStyle.SelectionBackColor = tangEntity.Finish ? Color.Yellow : tangGridView.RowsDefaultCellStyle.SelectionBackColor; tangGridView.Refresh(); } private void maoGridView_CellClick(object sender, DataGridViewCellEventArgs e) { if (e.RowIndex == -1) return; if (e.ColumnIndex == maoGridView.Columns.Count - 1) return; var entity = maoGridView.CurrentRow.DataBoundItem as GradeAndWeight; if (maoEntity != null) { foreach (DataGridViewRow row in maoGridView.Rows) { if (maoEntity.OrderDetail_ID == (long)row.Cells["M_OrderDetail_ID"].Value) { row.DefaultCellStyle.BackColor = maoEntity.Finish ? Color.YellowGreen : maoGridView.RowsDefaultCellStyle.BackColor; break; } } } maoEntity = entity; maoGridView.CurrentRow.DefaultCellStyle.SelectionBackColor = maoEntity.Finish ? Color.Yellow : maoGridView.RowsDefaultCellStyle.SelectionBackColor; maoGridView.Refresh(); } private void discontBtn_Click(object sender, EventArgs e) { if (new BodyDiscontSetting().ShowDialog() == DialogResult.OK) BuildDiscontPanel(false); } Button disBtn = null; void BuildDiscontPanel(bool firstLoad) { var disconts = new List(); if (!firstLoad) VerifyConnection(); if (connection) { disconts = GradeAndWeightRpc.GetBodyDiscontItem(); XmlUtil.SerializerObjToFile(disconts, discontPath); } else disconts = XmlUtil.DeserializeFromFile>(discontPath); disconts = disconts.Where(x => x.Discont > 0).OrderBy(x => x.ID).ToList(); discontPanel.Controls.Clear(); foreach (var item in disconts) { var btn = new Button() { Name = "_D" + item.ID, Text = item.Name, Tag = item.Discont, Size = new Size(70, 60), TextAlign = ContentAlignment.MiddleCenter, Margin = new Padding { Bottom = 30 }, Font = new Font("宋体", 15) }; btn.Click += (sender, e) => { if (modifyDetail == null) { if (disBtn != btn) { disBtn = btn; } else disBtn = null; } else { modifyDetail.Weight = (modifyDetail.Weight ?? 0) - Convert.ToDecimal(btn.Tag); LocalGradeAndWeightBL.Update(modifyDetail, "Weight"); cancelBtn_Click(sender, EventArgs.Empty); } }; discontPanel.Controls.Add(btn); } } private void dropPigBtn_Click(object sender, EventArgs e) { if (modifyDetail == null) { UMessageBox.Show("请先选择要设置为掉猪的行", "错误"); return; } else { modifyDetail.IsDrop = !modifyDetail.IsDrop; LocalGradeAndWeightBL.Update(modifyDetail, "IsDrop"); cancelBtn_Click(sender, EventArgs.Empty); } } private void dataConfirmBtn_Click(object sender, EventArgs e) { new DataConfirm(butcherTimeInput.Date.Value).ShowDialog(); } GradeAndWeight tangEntity; GradeAndWeight maoEntity; private void cancelBtn_Click(object sender, EventArgs e) { foreach (DataGridViewRow row in historyGrid.Rows) { if (modifyDetail.SID == (long)row.Cells["H_SID"].Value) { row.DefaultCellStyle.BackColor = historyGrid.RowsDefaultCellStyle.BackColor; break; } } modifyType = ModifyType.None; numberInput.Text = string.Empty; historyGrid.Refresh(); modifyDetail = null; if (withOkPanel.Visible) withOkPanel.Hide(); modifyPanel.Hide(); } Color btnUnSelectForeColor = SystemColors.ControlText; Color btnUnSelectBackColor = Color.FromArgb(225, 225, 225); void SetBtnUnCheck(Button btn) { btn.BackColor = btnUnSelectBackColor; btn.ForeColor = btnUnSelectForeColor; } #region scrollBar int tangRoll = -1; private void InitTangScrollBar() { tangScrollBar.Maximum = (tangGridView.RowCount - tangGridView.DisplayedRowCount(false) + 30) * tangGridView.RowTemplate.Height; tangScrollBar.Minimum = 0; tangScrollBar.SmallChange = tangGridView.RowTemplate.Height; tangScrollBar.LargeChange = tangGridView.RowTemplate.Height * 30; this.tangScrollBar.Scroll += (sender, e) => { tangRoll = e.NewValue / tangGridView.RowTemplate.Height; tangGridView.FirstDisplayedScrollingRowIndex = tangRoll; }; } int maoRoll = -1; private void InitMaoScrollBar() { maoScrollBar.Maximum = (maoGridView.RowCount - maoGridView.DisplayedRowCount(false) + 30) * maoGridView.RowTemplate.Height; maoScrollBar.Minimum = 0; maoScrollBar.SmallChange = maoGridView.RowTemplate.Height; maoScrollBar.LargeChange = maoGridView.RowTemplate.Height * 30; maoScrollBar.Scroll += (sender, e) => { maoRoll = e.NewValue / maoGridView.RowTemplate.Height; maoGridView.FirstDisplayedScrollingRowIndex = maoRoll; }; } int rightRoll = -1; private void InitDetailScrollBar() { vScrollBar2.Maximum = (historyGrid.RowCount - historyGrid.DisplayedRowCount(false) + 30) * historyGrid.RowTemplate.Height; vScrollBar2.Minimum = 0; vScrollBar2.SmallChange = historyGrid.RowTemplate.Height; vScrollBar2.LargeChange = historyGrid.RowTemplate.Height * 30; this.vScrollBar2.Scroll += (sender, e) => { rightRoll = e.NewValue / historyGrid.RowTemplate.Height; historyGrid.FirstDisplayedScrollingRowIndex = rightRoll; }; } #endregion bool? last = null; void VerifyConnection() { connection = LoginRpcUtil.TestConnection(); if (last == connection) return; var png = "stop.png"; if (connection) png = "working.png"; var imgPath = Path.Combine(Application.StartupPath, "BWP.WinFormControl.dll"); var s = Assembly.LoadFile(imgPath).GetManifestResourceStream("BWP.WinFormControl.Images." + png); if (this.InvokeRequired) { this.BeginInvoke(new Action(() => { statePic.Image = Image.FromStream(s); statePic.Refresh(); })); } else { statePic.Image = Image.FromStream(s); statePic.Refresh(); } last = connection; } private void SaveBtn_Click(object sender, EventArgs e) { if (modifyDetail == null) return; if (modifyType < ModifyType.Index) return; decimal number = 0; if (!decimal.TryParse(numberInput.Text.Trim(), out number)) throw new Exception("输入数据格式不正确"); switch (modifyType) { case ModifyType.Index: modifyDetail.Index = (int)number; LocalGradeAndWeightBL.Update(modifyDetail, "Index"); break; case ModifyType.Order: modifyDetail.Order = (int)number; LocalGradeAndWeightBL.Update(modifyDetail, "Order"); break; case ModifyType.Weight: modifyDetail.Weight = number; LocalGradeAndWeightBL.Update(modifyDetail, "Weight"); break; default: break; } cancelBtn_Click(sender, e); } } }