屠宰场客户端
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

625 lines
19 KiB

using ButcherManage.BO;
using ButcherManage.BO.Enums;
using ButcherManage.BO.LocalBL;
using ButcherManage.BO.Utils;
using System;
using System.Collections.Concurrent;
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 ButcherManage.Utils;
using WinFormControl;
using System.IO;
namespace ButcherManage.GradeAndWeight_
{
public partial class GradeAndWeightForm : Form, IWithRoleForm
{
#region IWithRoleForm
public List<short> RoleName
{
get { return new List<short> { (short). }; }
}
public Form Generate()
{
return this;
}
#endregion
BindingList<GradeAndWeight> tangList;
BindingList<GradeAndWeight> maoList;
BindingList<GradeAndWeight_Detail> details;
long? batchID = null;
int maxIndex = 0;
readonly ConcurrentQueue<GradeAndWeight_Detail> noLivestockList;
Dictionary<int, int> orderMaxIdx;
Thread syncTangGrid, syncMaoGrid, syncDetailGrid;
Thread syncToServer;
GradeAndWeight_Detail modifyDetail = null;
GradeAndWeight tangEntity;
GradeAndWeight maoEntity;
public GradeAndWeightForm()
{
InitializeComponent();
netStateWatch1.GetConnectState = () => LoginUtil.TestConnection(500);
datePicker.Text = DateTime.Today.ToString("yyyy-MM-dd");
productBatchSelect.SelectedIndexChanged += delegate
{
if (productBatchSelect.SelectedValue == null)
batchID = null;
else
batchID = (long)productBatchSelect.SelectedValue;
};
this.FormClosing += delegate
{
if (syncTangGrid != null && syncTangGrid.IsAlive)
syncTangGrid.Abort();
if (syncMaoGrid != null && syncMaoGrid.IsAlive)
syncMaoGrid.Abort();
if (syncDetailGrid != null && syncDetailGrid.IsAlive)
syncDetailGrid.Abort();
if (syncToServer != null && syncToServer.IsAlive)
syncToServer.Abort();
};
uWeightControl1.ReceivedValue += AddWeightDetail;
}
protected override void OnLoad(EventArgs e)
{
base.OnLoad(e);
var height = (Screen.PrimaryScreen.Bounds.Height - 236) / 2;
groupBox1.Height = groupBox2.Height = height;
groupBox2.Location = new Point(groupBox2.Location.X, 226 + height);
var initTask = new Thread(LoadBind);
initTask.Start();
syncToServer = new Thread(ToServerTask);
syncToServer.Start();
}
private void LoadBind()
{
this.Invoke(new Action(() =>
{
if (netStateWatch1.NetState)
{
BaseInfoSyncRpc.SyncBaseInfo<ProductBatch>();
BaseInfoSyncRpc.SyncBaseInfo<Livestock>();
BaseInfoSyncRpc.SyncBodyDiscont();
}
productBatchSelect.EBindComboBox<ProductBatch>(x => x.Date == DateTime.Today, 6, "Date");
BindLivestockBtn();
BindDiscountBtn();
}));
}
void BindLivestockBtn()
{
var livestocks = GradeAndWeightBL.GetLivestock();
var i = 0;
foreach (var item in livestocks)
{
i++;
var btn = new NButton() { Name = "_" + item.ID, Text = item.Name, Tag = item, Size = new Size(90, 75), Margin = new Padding { All = 15 }, Font = new Font("宋体", 18), PlaySound = true, PulseOnFocus = true };
btn.Click += (sender, e) =>
{
JiBieButtonClick(sender as NButton);
};
if (item.Technics == TANG_TECH)
{
ttPanel.Controls.Add(btn);
}
else
{
mbPanel.Controls.Add(btn);
}
}
SetMargin(ttPanel);
SetMargin(mbPanel);
}
void SetMargin(FlowLayoutPanel panel)
{
for (var i = 0; i < panel.Controls.Count; i++)
{
var c = panel.Controls[i];
if (i % 3 == 0)//left
c.Margin = new Padding(0, c.Margin.Top, c.Margin.Right, c.Margin.Bottom);
if ((i + 1) % 3 == 0)//right
c.Margin = new Padding(c.Margin.Left, c.Margin.Top, 0, c.Margin.Bottom);
if (i <= 2)//firstRow
c.Margin = new Padding(c.Margin.Left, 0, c.Margin.Right, c.Margin.Bottom);
}
}
void BindDiscountBtn()
{
var disconts = GradeAndWeightBL.GetBodyDiscont().Where(x => (x.Discont ?? 0) != 0);
discontPanel.Controls.Clear();
foreach (var item in disconts)
{
var btn = new NButton() { Name = "_D" + item.ID, Text = item.Name, Tag = item.Discont, Size = new Size(60, 60), 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);
}
}
void JiBieButtonClick(NButton btn)
{
if (details == null)
{
MessageBox.Show("请先同步数据");
return;
}
try
{
var livestockTag = btn.Tag as Livestock;
if (modifyDetail == null)
{
AddDetail(livestockTag);
}
else
{
UpdateDetial(modifyDetail, livestockTag);
cancelBtn_Click(btn, EventArgs.Empty);
}
SetlblSucessVisibleTrue();
SetlblSucessVisibleFalse();
}
catch { throw; }
}
static object _obj = new object();
void AddDetail(Livestock livestock)
{
lock (_obj)
{
var n = 0;
var currentRow = livestock.Technics == 0 ? tangEntity : maoEntity;
GradeAndWeight_Detail first;
//TryPeek 尝试返回 ConcurrentQueue<T> 开头处的对象但不将其移除
//TryDequeue 尝试移除并返回并发队列开头处的对象。
if (noLivestockList.TryDequeue(out first))
{
if (currentRow != null)
{
first.OrderDetail_ID = currentRow.OrderDetail_ID;
first.Order = currentRow.Order;
n = currentRow.Already;
SetOrderInex(first);
}
first.Date = DateTime.Parse(datePicker.Text);
first.Livestock_ID = livestock.ID;
first.Livestock_Name = livestock.Name;
first.Technics = livestock.Technics;
first.Technics_Name = livestock.Name;
if (disBtn != null)
{
first.Weight = (first.Weight ?? 0) - Convert.ToDecimal(disBtn.Tag);
SetBtnUnCheck(disBtn);
disBtn = null;
}
var barCode = StartPrintNewEntity(first);
first.BarCode = barCode;
LocalGradeAndWeightBL.Update(first, "OrderDetail_ID", "Order", "OrderIndex", "Date", "Livestock_ID", "Livestock_Name", "Technics", "Technics_Name", "Weight", "BarCode");
var tag = details.FirstOrDefault(x => x.SID == first.SID);
if (first != null)
{
tag.Date = first.Date;
tag.Livestock_ID = first.Livestock_ID;
tag.Livestock_Name = first.Livestock_Name;
tag.Order = first.Order;
tag.OrderDetail_ID = first.OrderDetail_ID;
tag.Technics = first.Technics;
tag.Technics_Name = first.Technics_Name;
tag.Weight = first.Weight;
historyGrid.Refresh();
}
}
else//add
{
maxIndex++;
var entity = new GradeAndWeight_Detail();
entity.Index = maxIndex;
entity.ProductBatch_ID = batchID;
if (currentRow != null)
{
entity.OrderDetail_ID = currentRow.OrderDetail_ID;
entity.Order = currentRow.Order;
n = currentRow.Already;
SetOrderInex(entity);
}
entity.Date = DateTime.Parse(datePicker.Text);
entity.Livestock_ID = livestock.ID;
entity.Livestock_Name = livestock.Name;
entity.Technics = livestock.Technics;
entity.Technics_Name = livestock.Technics_Name;
entity.Time = DateTime.Now;
if (disBtn != null)
{
entity.Weight = -Convert.ToDecimal(disBtn.Tag);
SetBtnUnCheck(disBtn);
disBtn = null;
}
entity.Weight = 0;
entity.BarCode = StartPrintNewEntity(entity);
LocalGradeAndWeightBL.Insert(entity);
details.Insert(0, entity);
AfterAddBindDetailGrid();
}
if (currentRow != null)
{
currentRow.Already = n + 1;
orderLabel.Text = currentRow.Order.ToString();
alreadyLabel.Text = currentRow.Already.ToString();
if (livestock.Technics==0)
tangGridView.Refresh();
else
maoGridView.Refresh();
}
else
{
orderLabel.Text = string.Empty;
alreadyLabel.Text = string.Empty;
}
}
}
void UpdateDetial(GradeAndWeight_Detail detail, Livestock livestock)
{
var techIsEmpty = detail.Technics == null;
detail.Livestock_ID = livestock.ID;
detail.Livestock_Name = livestock.Name;
detail.Technics = livestock.Technics;
detail.Technics_Name = livestock.Technics_Name;
var current = livestock.Technics == 0 ? tangEntity : maoEntity;
if (current != null)
{
detail.Order = current.Order;
detail.OrderDetail_ID = current.OrderDetail_ID;
}
var updateFileNames = new List<string> { "Order", "OrderDetail_ID", "Livestock_ID", "Livestock_Name", "Technics", "Technics_Name" };
LocalGradeAndWeightBL.Update(detail, updateFileNames.ToArray());
if (techIsEmpty)
ResetQueue();
}
private void SetOrderInex(GradeAndWeight_Detail detail)
{
if (detail.Order == null)
return;
if (!orderMaxIdx.ContainsKey(detail.Order.Value))
orderMaxIdx.Add(detail.Order.Value, 0);
orderMaxIdx[detail.Order.Value] += 1;
detail.OrderIndex = orderMaxIdx[detail.Order.Value];
}
void SetlblSucessVisibleFalse()
{
System.Timers.Timer tm = new System.Timers.Timer(1000);
tm.Elapsed += delegate(object sender, System.Timers.ElapsedEventArgs e)
{
tm.Enabled = false;
this.Invoke(new Action(delegate
{
lblSucessed.Visible = false;
}));
tm.Dispose();
};
tm.Enabled = true;
}
void SetlblSucessVisibleTrue()
{
this.Invoke(new Action(delegate
{
lblSucessed.Visible = true;
Application.DoEvents();
}));
}
void AfterAddBindDetailGrid()
{
if (details.Count > 50)
details.RemoveAt(50);
BindDetailGrid();
}
void AddWeightDetail(decimal weight)
{
lock (_obj)
{
maxIndex++;
var entity = new GradeAndWeight_Detail();
entity.Index = maxIndex;
entity.ProductBatch_ID = batchID;
entity.Weight = (entity.Weight ?? 0) + weight;
entity.Time = DateTime.Now;
entity.Date = DateTime.Parse(datePicker.Text);
LocalGradeAndWeightBL.Insert(entity);
details.Insert(0, entity);
noLivestockList.Enqueue(entity);
}
AfterAddBindDetailGrid();
}
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;
}
}
historyGrid.Refresh();
modifyDetail = null;
modifyPanel.Hide();
}
//删除选中
private void btnDeleteSelected_Click(object sender, EventArgs e)
{
if (MessageBox.Show("确定删除选中的称重记录?", "删除选中", MessageBoxButtons.OKCancel) == DialogResult.Cancel)
return;
if (modifyDetail == null)
{
UMessageBox.Show("请选中要删除的记录");
return;
}
//删除选中 更新 IsDeleted 和 Sync
lock (_obj)
{
modifyDetail.IsDeleted = true;
LocalGradeAndWeightBL.Update(modifyDetail, "IsDeleted");
details.Remove(modifyDetail);
if (modifyDetail.Technics == null)
{
ResetQueue();
}
modifyDetail = null;
modifyPanel.Hide();
}
if (details.Any())
historyGrid.DataSource = details;
historyGrid.Refresh();
}
private void historyGrid_CellClick(object sender, DataGridViewCellEventArgs e)
{
if (e.RowIndex < 0)
return;
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);
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 tangGridView_CellContentClick(object sender, DataGridViewCellEventArgs e)
{
if (e.RowIndex == -1)
return;
if (e.ColumnIndex != tangGridView.ColumnCount - 1)
return;
var entity = tangGridView.CurrentRow.DataBoundItem as GradeAndWeight;
if (entity.Finish)
{
tangEntity = null;
return;
}
entity.Finish = true;
LocalGradeAndWeightBL.SetGradeFinish(entity.Order,DateTime.Parse(datePicker.Text), 0);
tangEntity = null;
BindTangGrid();
}
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 maoGridView_CellContentClick(object sender, DataGridViewCellEventArgs e)
{
if (e.RowIndex == -1)
return;
if (e.ColumnIndex != maoGridView.ColumnCount - 1)
return;
var entity = maoGridView.CurrentRow.DataBoundItem as GradeAndWeight;
if (entity.Finish)
{
maoEntity = null;
return;
}
entity.Finish = true;
LocalGradeAndWeightBL.SetGradeFinish(entity.Order, DateTime.Parse(datePicker.Text), 1);
maoEntity = null;
BindMaoGrid();
}
private void discontBtn_Click(object sender, EventArgs e)
{
if (new BodyDiscontSetting().ShowDialog() == DialogResult.OK)
BuildDiscontPanel(false);
}
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();
}
#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
void ToServerTask()
{
while (true)
{
if (this.IsHandleCreated)
{
this.Invoke(new Action(() =>
{
if (netStateWatch1.NetState)
{
try
{
LocalGradeAndWeightBL.Sync();
}
catch (Exception ex)
{
File.WriteAllText(string.Format("Log\\{0:yyyyMMddHHmmss}log.txt", DateTime.Now), "错误:" + ex.Message + " \n详细信息:" + ex.StackTrace);
}
}
}));
}
Thread.Sleep(200);
}
}
private void closeBtn_Click(object sender, EventArgs e)
{
Close();
}
}
}