using EC.AutoWeightServer.Center;
using EC.AutoWeightServer.Model.Config;
using EC.AutoWeightServer.Model.EventArgsEx;
using EC.AutoWeightServer.Model.State;
using EC.Entity.Transport;
using EC.Utils;
using System;
using System.Drawing;
using System.Windows.Forms;
namespace EC.AutoWeightServer.UI.UC
{
public partial class UCCarIn : UserControl
{
private FrmMainServer _mainServer;
public UCCarIn()
{
InitializeComponent();
txtBillWeight.GotFocus += TxtBillWeight_Enter; //获取焦点前发生事件
}
internal void SetMainServer(FrmMainServer server)
{
_mainServer = server;
}
#region 界面组件内容读写方法
internal string GetLabelSerialNo()
{
return txtSerialNo.Text;
}
internal void SetLabelSerialNo(string txt)
{
txtSerialNo.Invoke((MethodInvoker)delegate ()
{
txtSerialNo.Text = txt;
});
}
internal void SetLabelMaterialNo(string txt)
{
txtMaterialNo.Invoke((MethodInvoker)delegate ()
{
txtMaterialNo.Text = txt;
});
}
internal string GetLabelBillWeight()
{
return txtBillWeight.Text;
}
internal void SetLabelBillWeight(string txt)
{
txtBillWeight.Invoke((MethodInvoker)delegate ()
{
txtBillWeight.Text = txt;
});
}
internal string GetLabelPlate()
{
return txtPlate.Text;
}
internal void SetLabelPlate(string txt)
{
txtPlate.Invoke((MethodInvoker)delegate ()
{
txtPlate.Text = txt;
});
}
internal string GetComboBoxTrailerPlate()
{
return comboBoxTrailer.SelectedItem?.ToString();
}
internal string GetLabelInWeight()
{
return txtInWeight.Text;
}
internal void SetLabelInWeight(decimal? weightDec)
{
txtInWeight.Invoke((MethodInvoker)delegate ()
{
var txt = (weightDec != null) ? CommonUtil.WeightDecToStr((decimal)weightDec, 3, '0') : string.Empty;
txtInWeight.Text = txt;
});
}
internal void SetLabelDriver1(string name)
{
labelDriver1.Invoke((MethodInvoker)delegate ()
{
labelDriver1.Text = name;
});
}
internal void SetLabelDriver2(string name)
{
labelDriver2.Invoke((MethodInvoker)delegate ()
{
labelDriver2.Text = name;
});
}
///
/// 初始化组件内容
///
///
///
///
private void InitBillComponent(string serialNo, string plate, string materialNo)
{
SetLabelSerialNo(serialNo);
SetLabelPlate(plate);
SetLabelMaterialNo(materialNo);
InitCombBoxTrailer(plate);
}
///
/// 清空组件内容
///
internal void ClearBillComponent()
{
Invoke((MethodInvoker)delegate ()
{
var empty = string.Empty;
txtSerialNo.Text = empty;
txtMaterialNo.Text = empty;
txtPlate.Text = empty;
comboBoxTrailer.Items.Clear();
txtBillWeight.Text = empty;
txtInWeight.Text = empty;
btnConfirm.Enabled = true;
TxtBillWeight_Leave(null, null);
labelDriver1.Text = empty;
labelDriver2.Text = empty;
driver1Entity = null;
driver2Entity = null;
});
}
#endregion 界面组件内容读写方法
#region 界面操作响应事件
private DriverEntity driver1Entity { get; set; }
private DriverEntity driver2Entity { get; set; }
internal void Btn_Finger1_Click(object sender, EventArgs e)
{
var plate = GetLabelPlate();
var tempDriver = CheckFingerPrint(plate);
if (tempDriver == null)
{
_mainServer?.SetScrollLabelText($"指纹对比失败!{DateTime.Now:yyyy/MM/dd HH:mm:ss}");
return;
}
driver1Entity = tempDriver;
SetLabelDriver1(driver1Entity.Name);
}
internal void Btn_Finger2_Click(object sender, EventArgs e)
{
var plate = GetLabelPlate();
var tempDriver = CheckFingerPrint(plate);
if (tempDriver == null)
{
_mainServer?.SetScrollLabelText($"指纹对比失败!{DateTime.Now:yyyy/MM/dd HH:mm:ss}");
return;
}
driver2Entity = tempDriver;
SetLabelDriver2(driver2Entity.Name);
}
private void ComboBox_Trailer_DropDownClosed(object sender, EventArgs e)
{
label4.Focus();//让 ComboBoxTrailer 失去焦点,避免整个Box获取焦点为蓝色
}
private void ComboBox_Trailer_DrawItem(object sender, DrawItemEventArgs e)
{
if (e.Index < 0)
{
return;
}
e.DrawBackground();
e.DrawFocusRectangle();
e.Graphics.DrawString(comboBoxTrailer.Items[e.Index].ToString(), e.Font, new SolidBrush(e.ForeColor), e.Bounds.X, e.Bounds.Y + 5);
}
private void Btn_ResetTrailer_Click(object sender, EventArgs e)
{
var plate = GetLabelPlate();
InitCombBoxTrailer(plate);
}
private void Btn_ResetWeight_Click(object sender, EventArgs e)
{
SetLabelInWeight(null);
}
///
/// 指纹对比
///
///
///
public DriverEntity CheckFingerPrint(string plate)
{
var verFp = _mainServer.DeviceCenter.GetVerFingerPrint();
if (string.IsNullOrEmpty(verFp))
return null;
//var authList = _authService.SelectEntityListByWhere($"TruckPlate = '{plate}'");
//foreach (var auth in authList)
//{
//var driver = _driverService.SelectEntityByWhere($"Id='{auth.DriverId}'");
//if (driver == null)
//continue;
//if (_mainServer.MatchFingerPrint(verFp, driver.Finger1))
//return driver;
//if (_mainServer.MatchFingerPrint(verFp, driver.Finger2))
//return driver;
//}
var truck = ServiceCenter.TruckService.SelectEntityByWhere($"LicensePlate = '{plate}'");
var driverList = ServiceCenter.DriverService.SelectEntityListByWhere($"SupplierId = '{truck.SupplierId}'");
//var driverList = _driverService.SelectEntityListByWhere($"SupplierName = '{truck.SupplierName}'");
foreach (var driver in driverList)
{
if (driver == null)
continue;
if (_mainServer.DeviceCenter.MatchFingerPrint(verFp, driver.Finger1))
return driver;
if (_mainServer.DeviceCenter.MatchFingerPrint(verFp, driver.Finger2))
return driver;
}
return null;
}
private void InitCombBoxTrailer(string plate)
{
comboBoxTrailer.Items.Clear();
var truck = ServiceCenter.TruckService.SelectEntityByWhere($"LicensePlate = '{plate}'");
var trailerList = ServiceCenter.TrailerService.SelectEntityListByWhere($"SupplierId = '{truck.SupplierId}'");
foreach (var trailer in trailerList)
{
comboBoxTrailer.Items.Add(trailer.LicensePlate);
}
}
#region 数字键盘输入事件
///
/// 进入输入框,打开数值输入面板
///
private void TxtBillWeight_Enter(object sender, EventArgs e)
{
numberPanel.Show();
}
///
/// 离开输入框,关闭数值输入面板
///
private void TxtBillWeight_Leave(object sender, EventArgs e)
{
panelOut.Focus();
numberPanel.Hide();
}
///
/// 数值输入面板下的数字输入按钮响应事件
///
private void Btn_Number_Click(object sender, EventArgs e)
{
var btn = (Button)sender;
var txt = GetLabelBillWeight();
txt += btn.Text;
SetLabelBillWeight(txt);
txtBillWeight.Focus();
txtBillWeight.SelectionStart = txt.Length;
}
///
/// 数值输入面板下的逗号输入按钮响应事件
///
private void Btn_dh_Click(object sender, EventArgs e)
{
var txt = GetLabelBillWeight();
if (txt.IndexOf(".", StringComparison.Ordinal) >= 0)
return;
txt += ".";
SetLabelBillWeight(txt);
}
///
/// 数值输入面板下的退格按钮响应事件
///
private void Btn_tg_Click(object sender, EventArgs e)
{
var txt = GetLabelBillWeight();
if (txt.Length <= 0)
return;
SetLabelBillWeight(txt.Substring(0, txt.Length - 1));
}
#endregion 数字键盘输入事件
#endregion 界面操作响应事件
#region 流程
///
/// 入场开始
///
///
internal void InStartBill(PlateEventArgs plateArgs)
{
var plate = plateArgs.Plate;
var ex = new Exception();
//判断车辆有无入场权限
if (!ServiceCenter.AuthService.CheckTruckAuth(plate))
{
ex.Data["msg"] = $"{plate},无运输权限!";
ex.Data["help"] = $"{plate},无运输权限!请联系管理人员!";
_mainServer.ShowOperateException(ex, false, true, true);
return;
}
//判断有无中断的入场记录
var tempBill = ServiceCenter.BillService.GetLastBillByLicensePlate(plate);
if (tempBill != null)//有记录,则继续判断;无记录,则继续(第一次入场)
{
if (tempBill.State == (int)BillState.InStart)//入场中断,则继续;其它,则退出
{
InitBillComponent(tempBill.SerialNumber, tempBill.LicensePlate, tempBill.MaterialNo);//初始化组件内容
_mainServer.SetInStartState(plate);//进入入场界面
return;
}
}
var siteEntity = _mainServer.LocalAttrCenter.Site;
var truckEntity = ServiceCenter.TruckService.GetTruckByPlate(plate);
var planEntity = ServiceCenter.MonthPlanService.GetLastPlan(siteEntity.CompanyId, truckEntity.SupplierId);
//判断月计划是否记录
if (planEntity == null)
{
ex.Data["msg"] = $"{plate},月计划获取失败!";
ex.Data["help"] = $"{plate},月计划获取失败!请重试或联系管理人员!";
_mainServer.ShowOperateException(ex, false, true, true);
return;
}
//入场开始属性
var state = (int)BillState.InStart;
var serialNo = ServiceCenter.IniService.GetNewBillSerialNo();
var photoEntity = ServiceCenter.PhotoService.InsertPhoto(plateArgs.FileName, state);
var result = ServiceCenter.BillService.InsertBillInStart(serialNo, siteEntity, planEntity, truckEntity, photoEntity, state);
if (result <= 0)
{
ex.Data["msg"] = $"{plate},运单添加失败!";
ex.Data["help"] = $"{plate},运单添加失败!请重试或联系管理人员!";
_mainServer.ShowOperateException(ex, false, true, true);
return;
}
InitBillComponent(serialNo, truckEntity.LicensePlate, planEntity.MaterialNo);//初始化组件内容
_mainServer.SetInStartState(plate);//进入入场界面
}
///
/// 入场结束
///
private void InFinishBill(object sender, EventArgs e)
{
var plate = GetLabelPlate();
var ex = new Exception();
if (comboBoxTrailer.SelectedIndex < 0)
{
ex.Data["msg"] = $"{plate},未选择挂车号!";
ex.Data["help"] = $"{plate},未选择挂车号,请选择挂车号!";
_mainServer.ShowOperateException(ex, false, true, true);
comboBoxTrailer.DroppedDown = true;
return;
}
if (string.IsNullOrEmpty(GetLabelBillWeight()))
{
ex.Data["msg"] = $"{plate},未输入运单重量!";
ex.Data["help"] = $"{plate},未输入运单重量,请输入运单重量!";
_mainServer.ShowOperateException(ex, false, true, true);
txtBillWeight.Focus();
return;
}
if (string.IsNullOrEmpty(GetLabelInWeight()))
{
ex.Data["msg"] = $"{plate},未完成自动计重!";
ex.Data["help"] = $"{plate},未完成自动计重,请等待自动计重!";
_mainServer.ShowOperateException(ex, false, true, true);
return;
}
if (!IsWeightConfirm())
{
btnResetWeight.PerformClick();
return;
}
//入场结束属性
var state = (int)BillState.InFinish;
var serialNumber = GetLabelSerialNo();
var trailerPlate = GetComboBoxTrailerPlate();
var billWeight = CommonUtil.StrToDec(GetLabelBillWeight());
var inWeight = CommonUtil.StrToDec(GetLabelInWeight());
if (driver1Entity == null)
{
ex.Data["msg"] = $"{plate},运输员指纹对比失败!";
ex.Data["help"] = $"{plate},运输员指纹对比失败,请进行指纹对比!";
_mainServer.ShowOperateException(ex, false, true, true);
Btn_Finger1_Click(null, null);
return;
}
if (driver2Entity == null)
{
ex.Data["msg"] = $"{plate},押运员指纹对比失败!";
ex.Data["help"] = $"{plate},押运员指纹对比失败,请进行指纹对比!";
_mainServer.ShowOperateException(ex, false, true, true);
Btn_Finger2_Click(null, null);
return;
}
var trailerEntity = ServiceCenter.TrailerService.SelectEntityByWhere($"LicensePlate = '{trailerPlate}'");
//保存图片信息
var photoEntity = ServiceCenter.PhotoService.InsertPhoto(_mainServer.DeviceCenter.GetInPicture2(GetLabelPlate()), state);
var result = ServiceCenter.BillService.UpdateBillInFinish(serialNumber, billWeight, inWeight, trailerEntity, driver1Entity, driver2Entity, photoEntity, state);//更新入场结束记录
if (result <= 0)
{
ex.Data["msg"] = $"{plate},运单更新失败!";
ex.Data["help"] = $"{plate},运单更新失败!请重试或联系管理人员!";
_mainServer.ShowOperateException(ex, false, true, true);
return;
}
btnConfirm.Enabled = false;
_mainServer.SetInFinishState(plate);//离开入场界面
}
#endregion 流程
#region Other
private bool IsWeightConfirm()
{
return Equals(_mainServer.GetLabelWeight(), GetLabelInWeight());
}
internal void SafeTempBill()
{
var serialNo = GetLabelSerialNo();
var plate = GetLabelPlate();
var inWeightStr = GetLabelInWeight();
if (string.IsNullOrEmpty(serialNo))
return;
if (string.IsNullOrEmpty(inWeightStr))
return;
var billEntity = ServiceCenter.BillService.GetBillBySerialNumber(serialNo);
var inWeight = CommonUtil.StrToDec(inWeightStr);
var msg = $"{plate},检测到未确认入场!";
var help = $"{plate},检测到未确认入场,已临时保存并记录!";
if (!string.IsNullOrEmpty(inWeightStr))
{
billEntity.InWeight = inWeight;
billEntity.Remark += $"{DateTime.Now}:检测到未确认入场(重量 - {inWeight}t)!\n";
help += $"已取得稳定重量({inWeight}t)";
}
else
{
billEntity.InWeight = 0;
billEntity.Remark += $"{DateTime.Now}:检测到未确认入场(重量 - {inWeight}t)!\n";
help += "未取得稳定重量";
}
if (driver1Entity != null)
{
billEntity.DriverId = driver1Entity.Id;
billEntity.DriverName = driver1Entity.Name;
}
if (driver2Entity != null)
{
billEntity.Driver2Id = driver2Entity.Id;
billEntity.Driver2Name = driver2Entity.Name;
}
billEntity.InFinishTime = DateTime.Now;
billEntity.State = (int)BillState.InFinish;
ServiceCenter.BillService.SaveForm(billEntity);
_mainServer.SetResetState(msg, help);
}
internal bool IsJustInFinish(string plate)
{
var bill = ServiceCenter.BillService.GetLastBillByLicensePlate(plate);
if (bill == null)
return false;
if (bill.State != (int)BillState.InFinish)
return false;
var span = DateTime.Now - bill.InFinishTime;
return span.TotalSeconds <= ServerConfig.BillInterval;
}
#endregion Other
}
}