using EC.AutoWeightServer.Model.Config; using EC.AutoWeightServer.Model.EventArgsEx; using EC.AutoWeightServer.Model.State; using EC.Entity.Transport; using EC.Service.Transport; using EC.Utils; using System; using System.Windows.Forms; namespace EC.AutoWeightServer.UI.UC { public partial class UCCarIn2 : UserControl { private FrmMainServer _mainServer; private readonly AuthService _authService = new AuthService(); private readonly IniService _iniService = new IniService(); private readonly MonthPlanService _monthPlanService = new MonthPlanService(); private readonly TransportBillService _billService = new TransportBillService(); private readonly DriverService _driverService = new DriverService(); private readonly TruckService _truckService = new TruckService(); private readonly PhotoService _photoService = new PhotoService(); public UCCarIn2() { InitializeComponent(); txtBillWeight.GotFocus += TxtBillWeight_Enter; //获取焦点前发生事件 //txtBillWeight.LostFocus += TxtBillWeight_Leave; //失去焦点后发生事件 } #region Init & Close internal void InitForm(FrmMainServer server) { _mainServer = server; } internal void CloseForm() { _mainServer = null; } #endregion Init & Close #region Get & Set internal string GetPlate() { return txtPlate.Text.Trim(); } internal string GetInWeight() { return txtInWeight.Text; } internal void SetInWeight(decimal? inWeight) { if (inWeight != null) { txtInWeight.Text = CommonUtil.WeightDecToStr((decimal)inWeight, 3, '0'); } else { txtInWeight.Text = ""; } //txtInWeight.Text = CommonUtil.WeightDecToStr(inWeight); } #endregion Get & Set #region 界面操作响应事件 /// /// 入场开始 /// /// internal void InStartBill(PlateEventArgs plateArgs) { var plate = plateArgs.Plate; //判断车辆有无入场权限 if (!_authService.CheckTruckAuth(plate)) { var ex = new Exception(); ex.Data["msg"] = $"{plate},无运输权限!"; ex.Data["help"] = $"{plate},无运输权限!请联系管理人员!"; throw ex; } //判断有无中断的入场记录 var tempBill = _billService.GetLastBillByLicensePlate(plate); if (tempBill != null)//有记录,则继续判断;无记录,则继续(第一次入场) { if (tempBill.State == (int)BillState.InStart)//入场中断,则继续;其它,则退出 { InitBillComponent(tempBill.SerialNumber, tempBill.LicensePlate, tempBill.MaterialNo);//初始化组件内容 _mainServer.SetInStartState(plate);//进入入场界面 return; } //if (tempBill.State != (int)BillState.OutFinish)//非正常入场,则继续判断;正常入场,则继续 //{ // var ex = new Exception(); // ex.Data["msg"] = $"{plate},非正常入场!"; // ex.Data["help"] = $"{plate},非正常入场!请联系管理人员!"; // throw ex; //} } var siteEntity = _mainServer.site; var truckEntity = _truckService.GetTruckByPlate(plate); var planEntity = _monthPlanService.GetLastPlan(siteEntity.CompanyId, truckEntity.SupplierId); //判断月计划是否记录 if (planEntity == null) { var ex = new Exception(); ex.Data["msg"] = $"{plate},月计划获取失败!"; ex.Data["help"] = $"{plate},月计划获取失败!请重试或联系管理人员!"; throw ex; } //入场开始属性 var state = (int)BillState.InStart; var serialNo = _iniService.GetNewBillSerialNo(); var photoEntity = _photoService.InsertPhoto(plateArgs.FileName, state); var result = _billService.InsertBillInStart(serialNo, siteEntity, planEntity, truckEntity, photoEntity, state); if (result <= 0) { var ex = new Exception(); ex.Data["msg"] = $"{plate},运单添加失败!"; ex.Data["help"] = $"{plate},运单添加失败!请重试或联系管理人员!"; throw ex; } InitBillComponent(serialNo, truckEntity.LicensePlate, planEntity.MaterialNo);//初始化组件内容 _mainServer.SetInStartState(plate);//进入入场界面 } /// /// 初始化组件内容 /// /// /// /// private void InitBillComponent(string serialNo, string plate, string materialNo) { txtSerialNo.Text = serialNo; txtPlate.Text = plate; txtMaterialNo.Text = materialNo; StartTimeoutCheck(); } /// /// 入场结束 /// private void InFinishBill(object sender, EventArgs e) { if (!IsWeightConfirm()) { btnResetWeight.PerformClick(); return; } var plate = txtPlate.Text; if (string.IsNullOrEmpty(txtBillWeight.Text)) { var ex = new Exception(); ex.Data["msg"] = $"{plate},未输入运单重量!"; ex.Data["help"] = $"{plate},未输入运单重量,请输入运单重量!"; _mainServer.ShowOperateException(ex, false, true, true); return; } if (string.IsNullOrEmpty(txtInWeight.Text)) { var ex = new Exception(); ex.Data["msg"] = $"{plate},未完成自动计重!"; ex.Data["help"] = $"{plate},未完成自动计重,请等待自动计重!"; _mainServer.ShowOperateException(ex, false, true, true); return; } //入场结束属性 var state = (int)BillState.InFinish; var serialNumber = txtSerialNo.Text; var billWeight = CommonUtil.StrToDec(txtBillWeight.Text); var inWeight = CommonUtil.StrToDec(txtInWeight.Text); var photoEntity = _photoService.InsertPhoto(_mainServer.GetInPicture2(txtPlate.Text), state); var result = _billService.UpdateBillInFinish(serialNumber, billWeight, inWeight, photoEntity, state);//更新入场结束记录 if (result <= 0) { var ex = new Exception(); ex.Data["msg"] = $"{plate},运单更新失败!"; ex.Data["help"] = $"{plate},运单更新失败!请重试或联系管理人员!"; _mainServer.ShowOperateException(ex, false, true, true); return; } btnInConfirm.Enabled = false; _mainServer.SetInFinishState(plate);//离开入场界面 } /// /// 清空组件内容 /// internal void ClearBillComponent() { var empty = string.Empty; txtSerialNo.Text = empty; txtPlate.Text = empty; txtMaterialNo.Text = empty; txtBillWeight.Text = empty; txtInWeight.Text = empty; btnInConfirm.Enabled = true; TxtBillWeight_Leave(null, null); EndTimeoutCheck(); } /// /// 指纹对比 /// /// /// public DriverEntity CheckFingerPrint(string plate) { //SetTimeoutCheckFlag(false); var verFp = _mainServer.GetVerFingerPrint(); if (string.IsNullOrEmpty(verFp)) { //SetTimeoutCheckFlag(true); 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; } //SetTimeoutCheckFlag(true); return null; } #endregion 界面操作响应事件 #region 数字键盘输入事件 /// /// 进入输入框,打开数值输入面板 /// private void TxtBillWeight_Enter(object sender, EventArgs e) { numberPanel.Show(); } /// /// 离开输入框,关闭数值输入面板 /// private void TxtBillWeight_Leave(object sender, EventArgs e) { pnlOut.Focus(); numberPanel.Hide(); } /// /// 数值输入面板下的数字输入按钮响应事件 /// private void Btn_Number_Click(object sender, EventArgs e) { Button btn = (Button)sender; string txt = txtBillWeight.Text.Trim(); txtBillWeight.Text = txt + btn.Text; txtBillWeight.Focus(); txtBillWeight.SelectionStart = txtBillWeight.Text.Length; } /// /// 数值输入面板下的逗号输入按钮响应事件 /// private void Btn_dh_Click(object sender, EventArgs e) { var txt = txtBillWeight.Text.Trim(); if (txt.IndexOf(".", StringComparison.Ordinal) >= 0) { return; } txtBillWeight.Text = txt + @"."; } /// /// 数值输入面板下的退格按钮响应事件 /// private void Btn_tg_Click(object sender, EventArgs e) { string txt = txtBillWeight.Text.Trim(); if (txt.Length <= 0) { return; } txtBillWeight.Text = txt.Substring(0, txt.Length - 1); } private void btnResetWeight_Click(object sender, EventArgs e) { SetInWeight(null); } #endregion 数字键盘输入事件 #region TimeOutCheck private System.Timers.Timer _timeoutCheckTimer; private int timeout; private bool timeoutCheckFlag; private void StartTimeoutCheck() { timeout = ServerConfig.InTimeOut; SetTimeoutCheckFlag(true); LabelTimeout.Text = $@"{timeout}秒后超时"; var timeoutInterval = 1000; _timeoutCheckTimer = new System.Timers.Timer(timeoutInterval); _timeoutCheckTimer.Elapsed += (sender1, e1) => { MethodInvoker method = TimeoutCheck; if (InvokeRequired) BeginInvoke(method); else method.Invoke(); }; _timeoutCheckTimer.Enabled = true; } private void EndTimeoutCheck() { if (_timeoutCheckTimer == null) return; SetTimeoutCheckFlag(false); _timeoutCheckTimer.Enabled = false; _timeoutCheckTimer.Stop(); _timeoutCheckTimer.Dispose(); _timeoutCheckTimer.Close(); _timeoutCheckTimer = null; } private void TimeoutCheck() { if (!timeoutCheckFlag) return; timeout -= 1; LabelTimeout.Text = $@"{timeout}秒后超时"; if (timeout > 0) return; var msg = $"{txtPlate.Text},运单超时!"; var help = $"{txtPlate.Text},运单超时,已自动完成重置!可通过手动识别重新载入运单!"; _mainServer.SetResetState(msg, help); EndTimeoutCheck(); } private void SetTimeoutCheckFlag(bool flag) { timeoutCheckFlag = flag; } #endregion TimeOutCheck #region 临时 private bool IsWeightConfirm() { return Equals(_mainServer.GetLabelWeight(), txtInWeight.Text); } internal void SafeTempBill() { var serialNo = txtSerialNo.Text; if (string.IsNullOrEmpty(serialNo)) return; if (string.IsNullOrEmpty(txtInWeight.Text)) return; var billEntity = _billService.GetBillBySerialNumber(serialNo); var inWeight = CommonUtil.StrToDec(txtInWeight.Text); var msg = $"{txtPlate.Text},检测到未确认入场!"; var help = $"{txtPlate.Text},检测到未确认入场,已临时保存并记录!"; if (!string.IsNullOrEmpty(txtInWeight.Text)) { 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 += "未取得稳定重量"; } billEntity.InFinishTime = DateTime.Now; billEntity.State = (int)BillState.InFinish; _billService.SaveForm(billEntity); _mainServer.SetResetState(msg, help); } internal bool IsJustInFinish(string plate) { var bill = _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 临时 } }