OtherMeasureOnceDm.pas 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155
  1. unit OtherMeasureOnceDm;
  2. interface
  3. uses
  4. SysUtils, Classes, sdDB, sdProvider, ADODB;
  5. type
  6. TOtherMeasureOnceData = class(TDataModule)
  7. sdpOnce: TsdADOProvider;
  8. sddOnce: TsdDataSet;
  9. sdvOnce: TsdDataView;
  10. procedure sddOnceAfterAddRecord(ARecord: TsdDataRecord);
  11. procedure sdvOnceSetText(var Text: String; ARecord: TsdDataRecord;
  12. AValue: TsdValue; AColumn: TsdViewColumn; var Allow: Boolean);
  13. procedure sdvOnceGetText(var Text: String; ARecord: TsdDataRecord;
  14. AValue: TsdValue; AColumn: TsdViewColumn; DisplayText: Boolean);
  15. private
  16. FProjectData: TObject;
  17. function GetNewID: Integer;
  18. function GetNewSerialNo: Integer;
  19. public
  20. constructor Create(AProjectData: TObject);
  21. destructor Destroy; override;
  22. procedure Open(AConnection: TADOConnection);
  23. procedure Close;
  24. procedure Save;
  25. end;
  26. implementation
  27. uses
  28. ProjectData, UtilMethods, Variants, DB;
  29. {$R *.dfm}
  30. { TOtherMeasureOnceData }
  31. procedure TOtherMeasureOnceData.Close;
  32. begin
  33. sddOnce.Close;
  34. end;
  35. constructor TOtherMeasureOnceData.Create(AProjectData: TObject);
  36. begin
  37. inherited Create(nil);
  38. FProjectData := AProjectData;
  39. end;
  40. destructor TOtherMeasureOnceData.Destroy;
  41. begin
  42. inherited;
  43. end;
  44. function TOtherMeasureOnceData.GetNewID: Integer;
  45. var
  46. idx: TsdIndex;
  47. begin
  48. idx := sddOnce.FindIndex('idxID');
  49. if idx.RecordCount > 0 then
  50. Result := idx.Records[idx.RecordCount - 1].ValueByName('ID').AsInteger + 1
  51. else
  52. Result := 1;
  53. end;
  54. function TOtherMeasureOnceData.GetNewSerialNo: Integer;
  55. var
  56. idx: TsdIndex;
  57. begin
  58. idx := sddOnce.FindIndex('idxSerial');
  59. if idx.RecordCount > 0 then
  60. Result := idx.Records[idx.RecordCount - 1].ValueByName('SerialNo').AsInteger + 1
  61. else
  62. Result := 1;
  63. end;
  64. procedure TOtherMeasureOnceData.Open(AConnection: TADOConnection);
  65. begin
  66. sdpOnce.Connection := AConnection;
  67. sddOnce.Open;
  68. if not Assigned(sddOnce.FindIndex('idxID')) then
  69. sddOnce.AddIndex('idxID', 'ID');
  70. if not Assigned(sddOnce.FindIndex('idxSerial')) then
  71. sddOnce.AddIndex('idxSerial', 'SerialNo');
  72. sdvOnce.Open;
  73. sdvOnce.IndexName := 'idxSerial';
  74. end;
  75. procedure TOtherMeasureOnceData.Save;
  76. begin
  77. sddOnce.Save;
  78. end;
  79. procedure TOtherMeasureOnceData.sddOnceAfterAddRecord(
  80. ARecord: TsdDataRecord);
  81. begin
  82. ARecord.ValueByName('ID').AsInteger := GetNewID;
  83. ARecord.ValueByName('SerialNo').AsInteger := GetNewSerialNo;
  84. ARecord.ValueByName('CreatePhaseID').AsInteger := TProjectData(FProjectData).PhaseIndex;
  85. ARecord.ValueByName('CreateStageID').AsInteger := TProjectData(FProjectData).StageIndex;
  86. end;
  87. procedure TOtherMeasureOnceData.sdvOnceSetText(var Text: String;
  88. ARecord: TsdDataRecord; AValue: TsdValue; AColumn: TsdViewColumn;
  89. var Allow: Boolean);
  90. begin
  91. if SameText(AColumn.FieldName, 'Code') then
  92. begin
  93. if (Text = '') then
  94. begin
  95. if (ARecord.ValueByName('Name').AsString = '') then
  96. DataSetErrorMessage(Allow, '编号和名称不可同时为空。');
  97. if (ARecord.ValueByName('Code').AsString = '') and (ARecord.ValueByName('Name').AsString = '') then
  98. sddOnce.Remove(ARecord);
  99. end;
  100. end
  101. else if SameText(AColumn.FieldName, 'Name') then
  102. begin
  103. if (Text = '') then
  104. begin
  105. if (ARecord.ValueByName('Code').AsString = '') then
  106. DataSetErrorMessage(Allow, '编号和名称不可同时为空。');
  107. if(ARecord.ValueByName('Code').AsString = '') and (ARecord.ValueByName('Name').AsString = '') then
  108. sddOnce.Remove(ARecord);
  109. end;
  110. end
  111. else
  112. begin
  113. if (ARecord.ValueByName('Code').AsString = '') and (ARecord.ValueByName('Name').AsString = '') then
  114. begin
  115. DataSetErrorMessage(Allow, '编号、名称不可同时为空,请先填写编号或名称,再填写其他信息。');
  116. sddOnce.Remove(AValue.Owner);
  117. end;
  118. end;
  119. end;
  120. procedure TOtherMeasureOnceData.sdvOnceGetText(var Text: String;
  121. ARecord: TsdDataRecord; AValue: TsdValue; AColumn: TsdViewColumn;
  122. DisplayText: Boolean);
  123. procedure GetDisplayText;
  124. begin
  125. if AValue.DataType = ftFloat then
  126. if AValue.AsFloat = 0 then
  127. Text := '';
  128. end;
  129. begin
  130. if DisplayText then
  131. GetDisplayText;
  132. end;
  133. end.