using EC.AutoWeightServer.Model.Config; using EC.AutoWeightServer.Model.EventArgsEx; using EC.AutoWeightServer.Model.State; using EC.AutoWeightServer.Model.Type; using EC.Entity.Transport; using EC.Service.Transport; using EC.Utils; using System; using System.Globalization; using System.Windows.Forms; namespace EC.AutoWeightServer.UI.UC { public partial class UCCarOut2 : UserControl { private FrmMainServer _mainServer; private readonly AuthService _authService = new AuthService(); private readonly TruckService _truckService = new TruckService(); private readonly TransportBillService _billService = new TransportBillService(); private readonly MonthPlanService _monthPlanService = new MonthPlanService(); private readonly PhotoService _photoService = new PhotoService(); private readonly DriverService _driverService = new DriverService(); public UCCarOut2() { InitializeComponent(); } #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 GetOutWeight() { return txtOutWeight.Text; } internal void SetOutWeight(decimal? outWeight) { var inWeight = Convert.ToDecimal(txtInWeight.Text); if (outWeight != null) { var netWeight = inWeight - (decimal)outWeight; txtOutWeight.Text = CommonUtil.WeightDecToStr((decimal)outWeight, 3, '0'); txtNetWeight.Text = CommonUtil.WeightDecToStr(netWeight, 3, '0'); } else { txtOutWeight.Text = ""; txtNetWeight.Text = ""; } //txtOutWeight.Text = CommonUtil.WeightDecToStr(outWeight); //txtNetWeight.Text = CommonUtil.WeightDecToStr(netWeight); } #endregion Get & Set #region 界面操作响应事件 internal DriverEntity driver1Entity; internal DriverEntity driver2Entity; internal void Btn_Finger1_Click(object sender, EventArgs e) { var plate = txtPlate.Text.Trim(); var tempDriver = CheckFingerPrint(plate); if (tempDriver == null) return; driver1Entity = tempDriver; labelDriver1.Text = driver1Entity.Name; } internal void Btn_Finger2_Click(object sender, EventArgs e) { var plate = txtPlate.Text.Trim(); var tempDriver = CheckFingerPrint(plate); if (tempDriver == null) return; driver2Entity = tempDriver; labelDriver2.Text = driver2Entity.Name; } private void btnResetWeight_Click(object sender, EventArgs e) { SetOutWeight(null); } /// /// 出场开始 /// /// internal void OutStartBill(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)//无记录,则退出 { var ex = new Exception(); ex.Data["msg"] = $"{plate},无运单记录!"; ex.Data["help"] = $"{plate},无运单记录!请联系管理人员!"; throw ex; } if (tempBill.State != (int)BillState.InFinish)//非正常出场,则继续判断;正常出场,则继续 { if (tempBill.State == (int)BillState.OutStart)//出场中断,则继续;其它,则退出 { InitBillComponent(tempBill);//初始化组件内容 _mainServer.SetOutStartState(plate);//进入出场界面 return; } var ex = new Exception(); ex.Data["msg"] = $"{plate},非正常出场!"; ex.Data["help"] = $"{plate},非正常出场!请联系管理人员!"; throw ex; } //出场开始属性 var state = (int)BillState.OutStart; var photoEntity = _photoService.InsertPhoto(plateArgs.FileName, state); var result = _billService.UpdateBillOutStart(tempBill, photoEntity, state);//更新出场开始记录 if (result <= 0) { var ex = new Exception(); ex.Data["msg"] = $"{plate},运单更新失败!"; ex.Data["help"] = $"{plate},运单更新失败!请重试或联系管理人员!"; throw ex; } tempBill = _billService.SelectEntityByWhere($"Id='{tempBill.Id}'");//获取更新后的bill InitBillComponent(tempBill);//初始化组件内容 _mainServer.SetOutStartState(plate);//进入出场界面 } /// /// 初始化组件内容 /// /// private void InitBillComponent(TransportBillEntity billEntity) { txtSerialNo.Text = billEntity.SerialNumber; txtPlate.Text = billEntity.LicensePlate; txtMaterialNo.Text = billEntity.MaterialNo; txtBillWeight.Text = CommonUtil.WeightDecToStr(billEntity.BillWeight, 3, '0'); txtInWeight.Text = CommonUtil.WeightDecToStr(billEntity.InWeight, 3, '0'); txtInStartTime.Text = billEntity.InStartTime.ToString(CultureInfo.CurrentCulture); txtOutStartTime.Text = billEntity.OutStartTime.ToString(CultureInfo.CurrentCulture); //StartTimeoutCheck(); } /// /// 出场结束 /// /// /// private void OutFinishBill(object sender, EventArgs e) { if (!IsWeightConfirm()) { btnResetWeight.PerformClick(); return; } var plate = txtPlate.Text; var serialNo = txtSerialNo.Text; var tempBill = _billService.GetBillBySerialNumber(serialNo); if (tempBill == null) { var ex = new Exception(); ex.Data["msg"] = $"{plate},无运单记录!"; ex.Data["help"] = $"{plate},无运单记录!请联系管理人员!"; _mainServer.ShowOperateException(ex, false, true, true); return; } if (string.IsNullOrEmpty(txtOutWeight.Text)) { var ex = new Exception(); ex.Data["msg"] = $"{plate},未完成自动计重!"; ex.Data["help"] = $"{plate},未完成自动计重,请等待自动计重!"; _mainServer.ShowOperateException(ex, false, true, true); return; } //出场结束属性 var state = (int)BillState.OutFinish; var outWeight = CommonUtil.StrToDec(txtOutWeight.Text); var netWeight = CommonUtil.StrToDec(txtNetWeight.Text); var picPath = _mainServer.GetOutPicture2(txtPlate.Text); if (driver1Entity == null) { var ex = new Exception(); ex.Data["msg"] = $"{plate},运输员指纹对比失败!"; ex.Data["help"] = $"{plate},运输员指纹对比失败,请进行指纹对比!"; _mainServer.ShowOperateException(ex, false, true, true); Btn_Finger1_Click(null,null); return; } if (driver2Entity == null) { var ex = new Exception(); ex.Data["msg"] = $"{plate},押运员指纹对比失败!"; ex.Data["help"] = $"{plate},押运员指纹对比失败,请进行指纹对比!"; _mainServer.ShowOperateException(ex, false, true, true); Btn_Finger2_Click(null, null); return; } //保存图片信息 var outPhoto2Entity = _photoService.InsertPhoto(picPath, (int)PhotoType.OutFinish); var result = _billService.UpdateBillOutFinish(serialNo, outWeight, netWeight, driver1Entity, driver2Entity, outPhoto2Entity, state); //更新出场结束记录 if (result <= 0) { var ex = new Exception(); ex.Data["msg"] = $"{plate},运单更新失败!"; ex.Data["help"] = $"{plate},运单更新失败!请重试或联系管理人员!"; _mainServer.ShowOperateException(ex, false, true, true); return; } var billEntity = _billService.GetBillBySerialNumber(serialNo); var monthPlanEntity = _monthPlanService.GetPlanBySerialNo(billEntity.PlanSerialNumber); monthPlanEntity.ActQty += billEntity.NetWeight; result = _monthPlanService.Update(monthPlanEntity); if (result <= 0) { var ex = new Exception(); ex.Data["msg"] = $"{plate},月计划更新失败!"; ex.Data["help"] = $"{plate},月计划更新失败!请重试或联系管理人员!"; _mainServer.ShowOperateException(ex, false, true, true); return; } btnConfirm.Enabled = false; _mainServer.PrintBill(billEntity);//打印运输小票 _mainServer.PrintBill(billEntity);//打印存根小票 _mainServer.SetOutFinishState(plate);//离开出场界面 } /// /// 清空组件内容 /// internal void ClearBillComponent() { var empty = string.Empty; txtSerialNo.Text = empty; txtPlate.Text = empty; txtMaterialNo.Text = empty; txtBillWeight.Text = empty; txtNetWeight.Text = empty; txtInWeight.Text = empty; txtOutWeight.Text = empty; txtInStartTime.Text = empty; txtOutStartTime.Text = empty; labelDriver1.Text = empty; labelDriver2.Text = empty; btnConfirm.Enabled = true; driver1Entity = null; driver2Entity = 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; //} var truck = _truckService.SelectEntityByWhere($"LicensePlate = '{plate}'"); var driverList = _driverService.SelectEntityListByWhere($"SupplierId = '{truck.SupplierId}'"); //var driverList = _driverService.SelectEntityListByWhere($"SupplierName = '{truck.SupplierName}'"); foreach (var driver in driverList) { 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 TimeOutCheck private System.Timers.Timer _timeoutCheckTimer; private int timeout; private bool timeoutCheckFlag; private void StartTimeoutCheck() { timeout = ServerConfig.OutTimeOut; SetTimeoutCheckFlag(true); LabelTimeout.Text = $@"{timeout}秒后超时"; var timeoutInterval = 1000; _timeoutCheckTimer = new System.Timers.Timer(timeoutInterval); _timeoutCheckTimer.Elapsed += (sender, e) => { 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(), txtOutWeight.Text); } internal void SafeTempBill() { var serialNo = txtSerialNo.Text; if (string.IsNullOrEmpty(serialNo)) return; if (string.IsNullOrEmpty(txtOutWeight.Text)) return; var billEntity = _billService.GetBillBySerialNumber(serialNo); var outWeight = CommonUtil.StrToDec(txtOutWeight.Text); var msg = $"{txtPlate.Text},检测到未确认出场!"; var help = $"{txtPlate.Text},检测到未确认出场,已临时保存并记录!"; if (!string.IsNullOrEmpty(txtOutWeight.Text)) { billEntity.OutWeight = outWeight; billEntity.NetWeight = billEntity.InWeight - billEntity.OutWeight; billEntity.Remark += $"{DateTime.Now}:检测到未确认出场(重量 - {outWeight}t)!\n"; help += $"已取得稳定重量({outWeight}t)"; } else { billEntity.OutWeight = 0; billEntity.NetWeight = billEntity.InWeight - billEntity.OutWeight; billEntity.Remark += $"{DateTime.Now}:检测到未确认出场(重量 - {outWeight}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.OutFinishTime = DateTime.Now; billEntity.State = (int)BillState.OutFinish; _billService.SaveForm(billEntity); _mainServer.SetResetState(msg, help); } internal bool IsJustOutFinish(string plate) { var bill = _billService.GetLastBillByLicensePlate(plate); if (bill == null) return false; if (bill.State != (int)BillState.OutFinish) return false; var span = DateTime.Now - bill.OutFinishTime; return span.TotalSeconds <= ServerConfig.BillInterval; } #endregion 临时保存 } }