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.

466 lines
14 KiB

3 years ago
using EC.AutoWeightServer.Center;
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.Utils;
using System;
using System.Globalization;
using System.Windows.Forms;
namespace EC.AutoWeightServer.UI.UC
{
public partial class UCCarOut : UserControl
{
private FrmMainServer _mainServer;
public UCCarOut()
{
InitializeComponent();
}
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 string GetLabelPlate()
{
return txtPlate.Text;
}
internal void SetLabelPlate(string txt)
{
txtPlate.Invoke((MethodInvoker)delegate ()
{
txtPlate.Text = txt;
});
}
internal string GetLabelTrailerPlate()
{
return txtTrailerPlate.Text;
}
internal void SetLabelTrailerPlate(string txt)
{
txtTrailerPlate.Invoke((MethodInvoker)delegate ()
{
txtTrailerPlate.Text = txt;
});
}
internal string GetLabelInWeight()
{
return txtInWeight.Text;
}
internal string GetLabelOutWeight()
{
return txtOutWeight.Text;
}
internal void SetOutWeight(decimal? weightDec)
{
txtOutWeight.Invoke((MethodInvoker)delegate ()
{
var txt = (weightDec != null) ? CommonUtil.WeightDecToStr((decimal)weightDec, 3, '0') : string.Empty;
txtOutWeight.Text = txt;
var inWeight = Convert.ToDecimal(GetLabelInWeight());
var netWeight = (weightDec != null) ? inWeight - weightDec : null;
SetLabelNetWeight(netWeight);
});
}
internal void SetLabelNetWeight(decimal? weightDec)
{
txtNetWeight.Invoke((MethodInvoker)delegate ()
{
var txt = (weightDec != null) ? CommonUtil.WeightDecToStr((decimal)weightDec, 3, '0') : string.Empty;
txtNetWeight.Text = txt;
});
}
internal string GetLabelNetWeight()
{
return txtNetWeight.Text;
}
internal void SetLabelDriver1(string name)
{
labelDriver1.Invoke((MethodInvoker)delegate ()
{
labelDriver1.Text = name;
});
}
internal void SetLabelDriver2(string name)
{
labelDriver2.Invoke((MethodInvoker)delegate ()
{
labelDriver2.Text = name;
});
}
/// <summary>
/// 初始化组件内容
/// </summary>
/// <param name="billEntity"></param>
private void InitBillComponent(TransportBillEntity billEntity)
{
checkBillEntity = billEntity;
Invoke((MethodInvoker)delegate ()
{
txtSerialNo.Text = billEntity.SerialNumber;
txtPlate.Text = billEntity.LicensePlate;
txtTrailerPlate.Text = billEntity.TrailerPlate;
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);
});
}
/// <summary>
/// 清空组件内容
/// </summary>
internal void ClearBillComponent()
{
Invoke((MethodInvoker)delegate ()
{
var empty = string.Empty;
txtSerialNo.Text = empty;
txtPlate.Text = empty;
txtTrailerPlate.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;
checkBillEntity = null;
driver1Entity = null;
driver2Entity = null;
btnConfirm.Enabled = true;
});
}
#endregion 界面组件内容读写方法
#region 界面操作响应事件
private TransportBillEntity checkBillEntity { get; set; }
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 || (checkBillEntity.DriverId > 0 && tempDriver.Id != checkBillEntity.DriverId))
{
_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 || (checkBillEntity.Driver2Id > 0 && tempDriver.Id != checkBillEntity.Driver2Id))
{
_mainServer?.SetScrollLabelText($"指纹对比失败!{DateTime.Now:yyyy/MM/dd HH:mm:ss}");
return;
}
driver2Entity = tempDriver;
SetLabelDriver2(driver2Entity.Name);
}
private void Btn_ResetWeight_Click(object sender, EventArgs e)
{
SetOutWeight(null);
}
public DriverEntity CheckFingerPrint(string plate)
{
var verFp = _mainServer.DeviceCenter.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 = 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;
}
#endregion 界面操作响应事件
#region 流程
/// <summary>
/// 出场开始
/// </summary>
/// <param name="plateArgs"></param>
internal void OutStartBill(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)//无记录,则退出
{
ex.Data["msg"] = $"{plate},无运单记录!";
ex.Data["help"] = $"{plate},无运单记录!请联系管理人员!";
_mainServer.ShowOperateException(ex, false, true, true);
return;
}
if (tempBill.State != (int)BillState.InFinish)//非正常出场,则继续判断;正常出场,则继续
{
if (tempBill.State == (int)BillState.OutStart)//出场中断,则继续;其它,则退出
{
InitBillComponent(tempBill);//初始化组件内容
_mainServer.SetOutStartState(plate);//进入出场界面
return;
}
ex.Data["msg"] = $"{plate},非正常出场!";
ex.Data["help"] = $"{plate},非正常出场!请联系管理人员!";
_mainServer.ShowOperateException(ex, false, true, true);
return;
}
//出场开始属性
var state = (int)BillState.OutStart;
var photoEntity = ServiceCenter.PhotoService.InsertPhoto(plateArgs.FileName, state);
var result = ServiceCenter.BillService.UpdateBillOutStart(tempBill, photoEntity, state);//更新出场开始记录
if (result <= 0)
{
ex.Data["msg"] = $"{plate},运单更新失败!";
ex.Data["help"] = $"{plate},运单更新失败!请重试或联系管理人员!";
_mainServer.ShowOperateException(ex, false, true, true);
return;
}
tempBill = ServiceCenter.BillService.SelectEntityByWhere($"Id='{tempBill.Id}'");//获取更新后的bill
InitBillComponent(tempBill);//初始化组件内容
_mainServer.SetOutStartState(plate);//进入出场界面
}
/// <summary>
/// 出场结束
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void OutFinishBill(object sender, EventArgs e)
{
var plate = GetLabelPlate();
var ex = new Exception();
var serialNo = GetLabelSerialNo();
var tempBill = ServiceCenter.BillService.GetBillBySerialNumber(serialNo);
if (tempBill == null)
{
ex.Data["msg"] = $"{plate},无运单记录!";
ex.Data["help"] = $"{plate},无运单记录!请联系管理人员!";
_mainServer.ShowOperateException(ex, false, true, true);
return;
}
if (string.IsNullOrEmpty(GetLabelOutWeight()))
{
ex.Data["msg"] = $"{plate},未完成自动计重!";
ex.Data["help"] = $"{plate},未完成自动计重,请等待自动计重!";
_mainServer.ShowOperateException(ex, false, true, true);
return;
}
if (!IsWeightConfirm())
{
btnResetWeight.PerformClick();
return;
}
//出场结束属性
var state = (int)BillState.OutFinish;
var outWeight = CommonUtil.StrToDec(GetLabelOutWeight());
var netWeight = CommonUtil.StrToDec(GetLabelNetWeight());
var picPath = _mainServer.DeviceCenter.GetOutPicture2(GetLabelPlate());
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 outPhoto2Entity = ServiceCenter.PhotoService.InsertPhoto(picPath, (int)PhotoType.OutFinish);
var result = (checkBillEntity?.DriverId > 0 && checkBillEntity?.Driver2Id > 0) ?
ServiceCenter.BillService.UpdateBillOutFinish(serialNo, outWeight, netWeight, outPhoto2Entity, state) :
ServiceCenter.BillService.UpdateBillOutFinish(serialNo, outWeight, netWeight, driver1Entity, driver2Entity, outPhoto2Entity, state); //更新出场结束记录
if (result <= 0)
{
ex.Data["msg"] = $"{plate},运单更新失败!";
ex.Data["help"] = $"{plate},运单更新失败!请重试或联系管理人员!";
_mainServer.ShowOperateException(ex, false, true, true);
return;
}
var billEntity = ServiceCenter.BillService.GetBillBySerialNumber(serialNo);
var monthPlanEntity = ServiceCenter.MonthPlanService.GetPlanBySerialNo(billEntity.PlanSerialNumber);
monthPlanEntity.ActQty += billEntity.NetWeight;
result = ServiceCenter.MonthPlanService.Update(monthPlanEntity);
if (result <= 0)
{
ex.Data["msg"] = $"{plate},月计划更新失败!";
ex.Data["help"] = $"{plate},月计划更新失败!请重试或联系管理人员!";
_mainServer.ShowOperateException(ex, false, true, true);
return;
}
btnConfirm.Enabled = false;
_mainServer.DeviceCenter.PrintBill(billEntity);//打印运输小票
//_mainServer.DeviceCenter.PrintBill(billEntity);//打印存根小票
_mainServer.SetOutFinishState(plate);//离开出场界面
}
#endregion 流程
#region Other
private bool IsWeightConfirm()
{
return Equals(_mainServer.GetLabelWeight(), GetLabelOutWeight());
}
internal void SafeTempBill()
{
var serialNo = GetLabelSerialNo();
if (string.IsNullOrEmpty(serialNo))
return;
if (string.IsNullOrEmpty(GetLabelOutWeight()))
return;
var billEntity = ServiceCenter.BillService.GetBillBySerialNumber(serialNo);
var outWeight = CommonUtil.StrToDec(GetLabelOutWeight());
var msg = $"{GetLabelPlate()},检测到未确认出场!";
var help = $"{GetLabelPlate()},检测到未确认出场,已临时保存并记录!";
if (!string.IsNullOrEmpty(GetLabelOutWeight()))
{
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;
ServiceCenter.BillService.SaveForm(billEntity);
_mainServer.SetResetState(msg, help);
}
internal bool IsJustOutFinish(string plate)
{
var bill = ServiceCenter.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 Other
}
}