using ButcherManage.BO; using ButcherManage.BO.Enums; using ButcherManage.BO.LocalBL; using ButcherManage.BO.Utils; using ButcherManage.Dialogs; 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.Windows.Forms; using WinFormControl; namespace ButcherManage.PickOutConfirm_ { public partial class PickOutConfirm : Form, IWithRoleForm { #region IWithRoleForm public List RoleName { get { return new List { (short)设备类别.赶猪确认 }; } } public Form Generate() { return this; } #endregion IList list; List records; long lastID = 0; DateTime date = DateTime.Today; bool start = false; long startID = 0; public PickOutConfirm() { InitializeComponent(); this.FormClosing += (sender, e) => { if (start) { e.Cancel = MessageBox.Show("有开始状态的作业,确定退出?", "请确认", MessageBoxButtons.OKCancel, MessageBoxIcon.Warning) != DialogResult.OK; } }; } protected override void OnLoad(EventArgs e) { base.OnLoad(e); datePicker.Text = date.ToString("yyyy-MM-dd"); BindGrid(); FillNumberPad(); if (list.Any()) { var tag = list.FirstOrDefault(x => x.ConfirmState == 1); if (tag != null) { start = list.First().ConfirmState == 1; startID = list.First().ID; lastID = startID; orderLbl.Text = tag.Order.ToString(); } RefreshRecordGrid(list.First().ID); } } void RefreshRecordGrid(long id) { lastID = id; records = PickOutConfirmBL.GetRecordList(lastID); var row = records.Count % 5; if (records.Count % 5 == 0) { records.Add(new PickOutRecord() { Order_ID = lastID }); row = 0; } BindRecorGrid(); BindLabel(); recordGrid.Rows[records.Count / 5].Cells[row].Selected = true; } void BindLabel() { countLbl.Text = records.Where(x => x.Number > 0).Count().ToString(); numberLbl.Text = records.Sum(x => x.Number).ToString(); } private void BindGrid() { list = PickOutConfirmBL.GetDmoList(date).OrderBy(x => x.Order).OrderByDescending(x => x.ConfirmState).ToList(); orderGrid.DataSource = list; foreach (DataGridViewRow row in orderGrid.Rows) { var v = (int)row.Cells["L_ConfirmState"].Value; if (v == 1) { row.Cells[6] = new DataGridViewTextBoxCell(); } else if (v == -1 || list.Any(x => x.ConfirmState == 1)) { row.Cells[6] = new DataGridViewTextBoxCell(); row.Cells[7] = new DataGridViewTextBoxCell(); } } orderGrid.Refresh(); } private void FillNumberPad() { for (var i = 1; i < 10; i++) CreateBtn(i.ToString()); } void CreateBtn(string content) { var btn = new NButton() { Width = 100, Height = 60, Text = content, Font = new Font("宋体", 15), Margin = new Padding(18, 10, 18, 20), PlaySound = true }; btn.Click += NumberBtnClick; numPad.Controls.Add(btn); } private void NumberBtnClick(object sender, EventArgs e) { if (!start) { NMessageBox.ShowDialog("请先开始"); return; } else if (lastID != startID) { NMessageBox.ShowDialog("当前作业与选择的记录不一致"); return; } var number = int.Parse((sender as NButton).Text); var cell = recordGrid.CurrentCell; if (cell.Value == null && number == 0) return; var idx = cell.RowIndex * 5 + cell.ColumnIndex; var detail = new PickOutRecord(); if (idx > records.Count - 1) { if (cell.RowIndex != records.Count / 5 || cell.ColumnIndex != records.Count % 5) cell = recordGrid.Rows[records.Count / 5].Cells[records.Count % 5]; detail.Order_ID = lastID; detail.Number = number; records.Add(detail); } else { detail = records[idx]; detail.Number = number; } PickOutConfirmBL.SaveRecord(detail); cell.Value = number; var row = records.Count % 5; if (records[records.Count - 1].Number == null) row -= 1; if (records.Count % 5 == 0) { records.Add(new PickOutRecord() { Order_ID = lastID }); row = 0; BindRecorGrid(); } recordGrid.Rows[records.Count / 5].Cells[row].Selected = true; BindLabel(); orderGrid.CurrentRow.Cells["L_AlreadyNumber"].Value = records.Sum(x => x.Number ?? 0); } void BindRecorGrid() { recordGrid.DataSource = Expand.Build(records); recordGrid.Refresh(); } private void colseBtn_Click(object sender, EventArgs e) { Close(); } private void orderGrid_CellClick(object sender, DataGridViewCellEventArgs e) { var id = (long)orderGrid.CurrentRow.Cells[0].Value; if (id == lastID) return; RefreshRecordGrid(id); } private void orderGrid_CellContentClick(object sender, DataGridViewCellEventArgs e) { if (e.ColumnIndex < 6 || e.RowIndex == -1) return; if (e.ColumnIndex == 6)//开始 { PickOutConfirmBL.ChangeState(lastID, 1); start = true; startID = lastID; orderLbl.Text = orderGrid.CurrentRow.Cells["L_Order"].Value.ToString(); } else//结束 { PickOutConfirmBL.ChangeState(lastID, -1); start = false; startID = 0; orderLbl.Text = "0"; } BindGrid(); } private void datePicker_MouseDown(object sender, MouseEventArgs e) { var cs = new CalendarSelecter(); if (cs.ShowDialog() == true) { date = cs.Result; datePicker.Text = date.ToString("yyyy-MM-dd"); } } private void queryBtn_Click(object sender, EventArgs e) { if (start) { NMessageBox.ShowDialog("有开始状态的作业,请结束后再尝试"); return; } BindGrid(); } private void orderGrid_RowPrePaint(object sender, DataGridViewRowPrePaintEventArgs e) { DataGridViewRow dgrSingle = orderGrid.Rows[e.RowIndex]; var v = (int)dgrSingle.Cells["L_ConfirmState"].Value; if (v == 1) dgrSingle.DefaultCellStyle.BackColor = Color.LightBlue; else if (v == -1) dgrSingle.DefaultCellStyle.BackColor = Color.YellowGreen; } } class Expand { public int? Number1 { get; set; } public int? Number2 { get; set; } public int? Number3 { get; set; } public int? Number4 { get; set; } public int? Number5 { get; set; } public static List Build(List list) { var result = new List(); var t = typeof(Expand); for (var i = 0; i < list.Count; ) { var entity = new Expand(); result.Add(entity); for (var j = 1; j <= 5; j++) { t.GetProperty(string.Format("Number{0}", j)).SetValue(entity, list[i].Number); i++; if (i == list.Count) break; } } return result; } } }