Files
dyp/pas/uProWorkOrderEntryUnit.pas
T
2026-05-07 20:25:34 +08:00

358 lines
16 KiB
ObjectPascal

unit uProWorkOrderEntryUnit;
interface
uses
System.SysUtils, System.Classes, Data.DB, System.DateUtils, System.JSON,
System.JSON.Types, System.JSON.Readers, System.JSON.Writers;
type
{ 必须字段验证异常 }
ERequiredFieldException = class(Exception);
{ JSON转换异常 }
EJSONConversionException = class(Exception);
{ 工单子表记录结构 - 对应数据库工单子表 }
TProWorkOrderEntryRecord = record
id: Int64; // id - 主键
workshop_id: Int64; // 车间ID
workshop_name: string; // 车间名称
station_id: Int64; // 工位ID
station_name: string; // 工位名称
workorder_id: Int64; // 工单ID
process_id: Int64; // 工序ID
process_name: string; // 工序名称
sort: Integer; // 排序,默认1
qc_type: string; // 质检类型
first_qc_quantity: Double; // 首检数量
type_: string; // 类型 (使用type_避免与关键字冲突)
report_material_id: Int64; // 报工物料ID
report_specification: string; // 报工物料规格
report_material_name: string; // 报工物料名称
report_quantity: Double; // 报工数量
pick_material_id: Int64; // 领料ID
pick_specification: string; // 领料规格
pick_material_name: string; // 领料名称
pick_quantity: Double; // 领料数量
in_warehouse_id: Int64; // 入库ID
in_warehouse_name: string; // 入库名称
out_warehouse_id: Int64; // 出库ID
out_warehouse_name: string; // 出库名称
extend_field: string; // 扩展字段
machine_id: Int64; // 设备ID
machine_name: string; // 设备名称
status: string; // 完成状态
remark: string; // 备注
process_sort: Integer; // 工序顺序
{ 初始化记录为默认值 }
procedure Initialize;
{ 检查记录是否为空(新记录) }
function IsEmpty: Boolean;
{ 将记录转换为JSON对象 }
function ToJSONObject: TJSONObject;
{ 将记录转换为JSON字符串 }
function ToJSONString: string;
end;
{ 工单子表映射器类 - 负责数据转换和验证 }
TProWorkOrderEntryMapper = class
public
{ 创建并返回一个具有默认值的新记录 }
class function CreateDefaultRecord: TProWorkOrderEntryRecord;
{ 从数据集转换为记录 }
class function DataSetToRecord(DataSet: TDataSet): TProWorkOrderEntryRecord;
{ 从JSON对象转换为记录 }
class function JSONToRecord(JSON: TJSONObject): TProWorkOrderEntryRecord; overload;
{ 从JSON字符串转换为记录 }
class function JSONToRecord(const JSONString: string): TProWorkOrderEntryRecord; overload;
{ 将记录转换为参数,用于数据库操作 }
class procedure RecordToParams(const ARecord: TProWorkOrderEntryRecord; Params: TParams);
{ 验证记录必须字段 }
class function ValidateRequiredFields(const ARecord: TProWorkOrderEntryRecord): Boolean;
{ 获取验证错误信息 }
class function GetValidationErrors(const ARecord: TProWorkOrderEntryRecord): string;
end;
implementation
{ TProWorkOrderEntryRecord }
procedure TProWorkOrderEntryRecord.Initialize;
begin
id := 0;
workshop_id := 0;
workshop_name := '';
station_id := 0;
station_name := '';
workorder_id := 0;
process_id := 0;
process_name := '';
sort := 1;
qc_type := '';
first_qc_quantity := 0;
type_ := '';
report_material_id := 0;
report_specification := '';
report_material_name := '';
report_quantity := 0;
// pick_material_id := ;
pick_specification := '';
pick_material_name := '';
pick_quantity := 0;
in_warehouse_id := 0;
in_warehouse_name := '';
out_warehouse_id := 0;
out_warehouse_name := '';
extend_field := '';
machine_id := 0;
machine_name := '';
status := '';
remark := '';
process_sort := 0;
end;
function TProWorkOrderEntryRecord.IsEmpty: Boolean;
begin
Result := id = 0;
end;
function TProWorkOrderEntryRecord.ToJSONObject: TJSONObject;
begin
Result := TJSONObject.Create;
try
Result.AddPair('id', TJSONNumber.Create(id));
Result.AddPair('workshop_id', TJSONNumber.Create(workshop_id));
Result.AddPair('workshop_name', workshop_name);
Result.AddPair('station_id', TJSONNumber.Create(station_id));
Result.AddPair('station_name', station_name);
Result.AddPair('workorder_id', TJSONNumber.Create(workorder_id));
Result.AddPair('process_id', TJSONNumber.Create(process_id));
Result.AddPair('process_name', process_name);
Result.AddPair('sort', TJSONNumber.Create(sort));
Result.AddPair('qc_type', qc_type);
Result.AddPair('first_qc_quantity', TJSONNumber.Create(first_qc_quantity));
Result.AddPair('type', type_);
Result.AddPair('report_material_id', TJSONNumber.Create(report_material_id));
Result.AddPair('report_specification', report_specification);
Result.AddPair('report_material_name', report_material_name);
Result.AddPair('report_quantity', TJSONNumber.Create(report_quantity));
Result.AddPair('pick_material_id', TJSONNumber.Create(pick_material_id));
Result.AddPair('pick_specification', pick_specification);
Result.AddPair('pick_material_name', pick_material_name);
Result.AddPair('pick_quantity', TJSONNumber.Create(pick_quantity));
Result.AddPair('in_warehouse_id', TJSONNumber.Create(in_warehouse_id));
Result.AddPair('in_warehouse_name', in_warehouse_name);
Result.AddPair('out_warehouse_id', TJSONNumber.Create(out_warehouse_id));
Result.AddPair('out_warehouse_name', out_warehouse_name);
Result.AddPair('extend_field', extend_field);
Result.AddPair('machine_id', TJSONNumber.Create(machine_id));
Result.AddPair('machine_name', machine_name);
Result.AddPair('status', status);
Result.AddPair('remark', remark);
Result.AddPair('process_sort', TJSONNumber.Create(process_sort));
except
on E: Exception do
begin
Result.Free;
raise EJSONConversionException.Create('Error converting record to JSON: ' + E.Message);
end;
end;
end;
function TProWorkOrderEntryRecord.ToJSONString: string;
var
JSONObject: TJSONObject;
begin
JSONObject := ToJSONObject;
try
Result := JSONObject.ToString;
finally
JSONObject.Free;
end;
end;
{ TProWorkOrderEntryMapper }
class function TProWorkOrderEntryMapper.CreateDefaultRecord: TProWorkOrderEntryRecord;
begin
Result.Initialize;
end;
class function TProWorkOrderEntryMapper.DataSetToRecord(DataSet: TDataSet): TProWorkOrderEntryRecord;
begin
Result.Initialize;
if not DataSet.IsEmpty then
begin
Result.id := DataSet.FieldByName('id').AsLargeInt;
Result.workshop_id := DataSet.FieldByName('workshop_id').AsLargeInt;
Result.workshop_name := DataSet.FieldByName('workshop_name').AsString;
Result.station_id := DataSet.FieldByName('station_id').AsLargeInt;
Result.station_name := DataSet.FieldByName('station_name').AsString;
Result.workorder_id := DataSet.FieldByName('workorder_id').AsLargeInt;
Result.process_id := DataSet.FieldByName('process_id').AsLargeInt;
Result.process_name := DataSet.FieldByName('process_name').AsString;
Result.sort := DataSet.FieldByName('sort').AsInteger;
Result.qc_type := DataSet.FieldByName('qc_type').AsString;
Result.first_qc_quantity := DataSet.FieldByName('first_qc_quantity').AsFloat;
Result.type_ := DataSet.FieldByName('type').AsString;
Result.report_material_id := DataSet.FieldByName('report_material_id').AsLargeInt;
Result.report_specification := DataSet.FieldByName('report_specification').AsString;
Result.report_material_name := DataSet.FieldByName('report_material_name').AsString;
Result.report_quantity := DataSet.FieldByName('report_quantity').AsFloat;
Result.pick_material_id := DataSet.FieldByName('pick_material_id').AsLargeInt;
Result.pick_specification := DataSet.FieldByName('pick_specification').AsString;
Result.pick_material_name := DataSet.FieldByName('pick_material_name').AsString;
Result.pick_quantity := DataSet.FieldByName('pick_quantity').AsFloat;
Result.in_warehouse_id := DataSet.FieldByName('in_warehouse_id').AsLargeInt;
Result.in_warehouse_name := DataSet.FieldByName('in_warehouse_name').AsString;
Result.out_warehouse_id := DataSet.FieldByName('out_warehouse_id').AsLargeInt;
Result.out_warehouse_name := DataSet.FieldByName('out_warehouse_name').AsString;
Result.extend_field := DataSet.FieldByName('extend_field').AsString;
Result.machine_id := DataSet.FieldByName('machine_id').AsLargeInt;
Result.machine_name := DataSet.FieldByName('machine_name').AsString;
Result.status := DataSet.FieldByName('status').AsString;
Result.remark := DataSet.FieldByName('remark').AsString;
Result.process_sort := DataSet.FieldByName('process_sort').AsInteger;
end;
end;
class function TProWorkOrderEntryMapper.JSONToRecord(JSON: TJSONObject): TProWorkOrderEntryRecord;
begin
Result.Initialize;
try
if JSON.GetValue('id') <> nil then
Result.id := (JSON.GetValue('id') as TJSONNumber).AsInt64;
if JSON.GetValue('workshop_id') <> nil then
Result.workshop_id := (JSON.GetValue('workshop_id') as TJSONNumber).AsInt64;
if JSON.GetValue('workshop_name') <> nil then
Result.workshop_name := JSON.GetValue('workshop_name').Value;
if JSON.GetValue('station_id') <> nil then
Result.station_id := (JSON.GetValue('station_id') as TJSONNumber).AsInt64;
if JSON.GetValue('station_name') <> nil then
Result.station_name := JSON.GetValue('station_name').Value;
if JSON.GetValue('workorder_id') <> nil then
Result.workorder_id := (JSON.GetValue('workorder_id') as TJSONNumber).AsInt64;
if JSON.GetValue('process_id') <> nil then
Result.process_id := (JSON.GetValue('process_id') as TJSONNumber).AsInt64;
if JSON.GetValue('process_name') <> nil then
Result.process_name := JSON.GetValue('process_name').Value;
if JSON.GetValue('sort') <> nil then
Result.sort := (JSON.GetValue('sort') as TJSONNumber).AsInt64;
if JSON.GetValue('qc_type') <> nil then
Result.qc_type := JSON.GetValue('qc_type').Value;
if JSON.GetValue('first_qc_quantity') <> nil then
Result.first_qc_quantity := (JSON.GetValue('first_qc_quantity') as TJSONNumber).AsDouble;
if JSON.GetValue('type') <> nil then
Result.type_ := JSON.GetValue('type').Value;
if JSON.GetValue('report_material_id') <> nil then
Result.report_material_id := (JSON.GetValue('report_material_id') as TJSONNumber).AsInt64;
if JSON.GetValue('report_specification') <> nil then
Result.report_specification := JSON.GetValue('report_specification').Value;
if JSON.GetValue('report_material_name') <> nil then
Result.report_material_name := JSON.GetValue('report_material_name').Value;
if JSON.GetValue('report_quantity') <> nil then
Result.report_quantity := (JSON.GetValue('report_quantity') as TJSONNumber).AsDouble;
if JSON.GetValue('pick_material_id') <> nil then
Result.pick_material_id := (JSON.GetValue('pick_material_id') as TJSONNumber).AsInt64;
if JSON.GetValue('pick_specification') <> nil then
Result.pick_specification := JSON.GetValue('pick_specification').Value;
if JSON.GetValue('pick_material_name') <> nil then
Result.pick_material_name := JSON.GetValue('pick_material_name').Value;
if JSON.GetValue('pick_quantity') <> nil then
Result.pick_quantity := (JSON.GetValue('pick_quantity') as TJSONNumber).AsDouble;
if JSON.GetValue('in_warehouse_id') <> nil then
Result.in_warehouse_id := (JSON.GetValue('in_warehouse_id') as TJSONNumber).AsInt64;
if JSON.GetValue('in_warehouse_name') <> nil then
Result.in_warehouse_name := JSON.GetValue('in_warehouse_name').Value;
if JSON.GetValue('out_warehouse_id') <> nil then
Result.out_warehouse_id := (JSON.GetValue('out_warehouse_id') as TJSONNumber).AsInt64;
if JSON.GetValue('out_warehouse_name') <> nil then
Result.out_warehouse_name := JSON.GetValue('out_warehouse_name').Value;
if JSON.GetValue('extend_field') <> nil then
Result.extend_field := JSON.GetValue('extend_field').Value;
if JSON.GetValue('machine_id') <> nil then
Result.machine_id := (JSON.GetValue('machine_id') as TJSONNumber).AsInt64;
if JSON.GetValue('machine_name') <> nil then
Result.machine_name := JSON.GetValue('machine_name').Value;
if JSON.GetValue('status') <> nil then
Result.status := JSON.GetValue('status').Value;
if JSON.GetValue('remark') <> nil then
Result.remark := JSON.GetValue('remark').Value;
if JSON.GetValue('process_sort') <> nil then
Result.process_sort := (JSON.GetValue('process_sort') as TJSONNumber).AsInt64;
except
on E: Exception do
raise EJSONConversionException.Create('Error converting JSON to record: ' + E.Message);
end;
end;
class function TProWorkOrderEntryMapper.JSONToRecord(const JSONString: string): TProWorkOrderEntryRecord;
var
JSONObject: TJSONObject;
begin
JSONObject := TJSONObject.ParseJSONValue(JSONString) as TJSONObject;
try
Result := JSONToRecord(JSONObject);
finally
JSONObject.Free;
end;
end;
class procedure TProWorkOrderEntryMapper.RecordToParams(const ARecord: TProWorkOrderEntryRecord; Params: TParams);
begin
Params.ParamByName('id').AsLargeInt := ARecord.id;
Params.ParamByName('workshop_id').AsLargeInt := ARecord.workshop_id;
Params.ParamByName('workshop_name').AsString := ARecord.workshop_name;
Params.ParamByName('station_id').AsLargeInt := ARecord.station_id;
Params.ParamByName('station_name').AsString := ARecord.station_name;
Params.ParamByName('workorder_id').AsLargeInt := ARecord.workorder_id;
Params.ParamByName('process_id').AsLargeInt := ARecord.process_id;
Params.ParamByName('process_name').AsString := ARecord.process_name;
Params.ParamByName('sort').AsInteger := ARecord.sort;
Params.ParamByName('qc_type').AsString := ARecord.qc_type;
Params.ParamByName('first_qc_quantity').AsFloat := ARecord.first_qc_quantity;
Params.ParamByName('type').AsString := ARecord.type_;
Params.ParamByName('report_material_id').AsLargeInt := ARecord.report_material_id;
Params.ParamByName('report_specification').AsString := ARecord.report_specification;
Params.ParamByName('report_material_name').AsString := ARecord.report_material_name;
Params.ParamByName('report_quantity').AsFloat := ARecord.report_quantity;
Params.ParamByName('pick_material_id').AsLargeInt := ARecord.pick_material_id;
Params.ParamByName('pick_specification').AsString := ARecord.pick_specification;
Params.ParamByName('pick_material_name').AsString := ARecord.pick_material_name;
Params.ParamByName('pick_quantity').AsFloat := ARecord.pick_quantity;
Params.ParamByName('in_warehouse_id').AsLargeInt := ARecord.in_warehouse_id;
Params.ParamByName('in_warehouse_name').AsString := ARecord.in_warehouse_name;
Params.ParamByName('out_warehouse_id').AsLargeInt := ARecord.out_warehouse_id;
Params.ParamByName('out_warehouse_name').AsString := ARecord.out_warehouse_name;
Params.ParamByName('extend_field').AsString := ARecord.extend_field;
Params.ParamByName('machine_id').AsLargeInt := ARecord.machine_id;
Params.ParamByName('machine_name').AsString := ARecord.machine_name;
Params.ParamByName('status').AsString := ARecord.status;
Params.ParamByName('remark').AsString := ARecord.remark;
Params.ParamByName('process_sort').AsInteger := ARecord.process_sort;
end;
class function TProWorkOrderEntryMapper.ValidateRequiredFields(const ARecord: TProWorkOrderEntryRecord): Boolean;
begin
// 根据表结构,所有字段都允许NULL,所以这里不强制验证任何字段
// 但可以根据业务需求添加验证逻辑
Result := True;
end;
class function TProWorkOrderEntryMapper.GetValidationErrors(const ARecord: TProWorkOrderEntryRecord): string;
begin
// 根据表结构,所有字段都允许NULL,所以这里不返回错误信息
// 但可以根据业务需求添加验证逻辑
Result := '';
end;
end.