PhaseData.pas 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334
  1. unit PhaseData;
  2. interface
  3. uses
  4. StageDm, PhasePayDm, ZJJLDm, ADODB, StageCompareDm,
  5. Connections, UpdateDataBase, UtilMethods, ZhAPI, PhaseProperty,
  6. Classes, SysUtils, sdDB;
  7. const
  8. ReferIndex = 0; // 原报序号
  9. type
  10. TPhaseData = class
  11. private
  12. FConnection: TEncryptConnection;
  13. FUpdator: TUpdatePhaseDB;
  14. FStageData: TStageData;
  15. FAuditList: TList;
  16. FPhaseProperty: TPhaseProperties;
  17. //FCurStageIndex: Integer;
  18. FStageIndex: Integer;
  19. FProjectData: TObject;
  20. FPhasePayData: TPhasePayData;
  21. FZJJLData: TZJJLData;
  22. FStageCompareData: TStageCompareData;
  23. procedure UpdatePhaseDataBase;
  24. procedure CopyPreStageData;
  25. procedure CheckAuditCount;
  26. function GetFileName: string;
  27. function GetPhaseIndex: Integer;
  28. function GetActive: Boolean;
  29. function GetStageDataReadOnly: Boolean;
  30. function GetAllowInsert: Boolean;
  31. function GetmdbFileName: string;
  32. function GetADOConnection: TADOConnection;
  33. procedure SetStageIndex(const Value: Integer);
  34. function GetStageTableName(AIndex: Integer): string;
  35. function GetAuditCount: Integer;
  36. function GetStageCount: Integer;
  37. function GetIsLastStage: Boolean;
  38. public
  39. constructor Create(AProjectData: TObject);
  40. destructor Destroy; override;
  41. procedure Open(const AProjectName: string);
  42. procedure Save;
  43. {仅打开项目属性}
  44. procedure SimpleOpen(const AProjectName: string);
  45. procedure SimpleSave;
  46. procedure SimpleOpen2(const AProjectName: string);
  47. procedure CreateNewAuditData;
  48. procedure CopyPreData;
  49. property StageIndex: Integer read FStageIndex write SetStageIndex;
  50. property StageData: TStageData read FStageData;
  51. property IsLastStage: Boolean read GetIsLastStage;
  52. // Other
  53. property PhaseProperty: TPhaseProperties read FPhaseProperty;
  54. property PhasePayData: TPhasePayData read FPhasePayData;
  55. property ZJJLData: TZJJLData read FZJJLData;
  56. property StageCompareData: TStageCompareData read FStageCompareData;
  57. property StageDataReadOnly: Boolean read GetStageDataReadOnly;
  58. property AllowInsert: Boolean read GetAllowInsert;
  59. property PhaseIndex: Integer read GetPhaseIndex;
  60. property FileName: string read GetFileName;
  61. property mdbFileName: string read GetmdbFileName;
  62. property ProjectData: TObject read FProjectData;
  63. property Active: Boolean read GetActive;
  64. property ADOConnection: TADOConnection read GetADOConnection;
  65. property StageTableName[AIndex: Integer]: string read GetStageTableName;
  66. property AuditCount: Integer read GetAuditCount;
  67. property StageCount: Integer read GetStageCount;
  68. end;
  69. implementation
  70. uses
  71. ProjectData, Globals;
  72. { TPhaseData }
  73. constructor TPhaseData.Create(AProjectData: TObject);
  74. begin
  75. FProjectData := AProjectData;
  76. FConnection := TEncryptConnection.Create;
  77. FUpdator := TUpdatePhaseDB.Create;
  78. FStageData := TStageData.Create(Self);
  79. FPhaseProperty := TPhaseProperties.Create(FConnection.Connection);
  80. FPhasePayData := TPhasePayData.Create(Self);
  81. FZJJLData := TZJJLData.Create(Self);
  82. FStageCompareData := TStageCompareData.Create(Self);
  83. end;
  84. procedure TPhaseData.CreateNewAuditData;
  85. var
  86. iAuditCount: Integer;
  87. begin
  88. try
  89. iAuditCount := FPhaseProperty.AuditCount;
  90. FPhaseProperty.AuditCount := iAuditCount + 1;
  91. CopyPreStageData;
  92. StageIndex := FPhaseProperty.AuditCount;
  93. PhasePayData.UpdateDataForNewAudit;
  94. except
  95. FPhaseProperty.AuditCount := iAuditCount;
  96. end;
  97. end;
  98. destructor TPhaseData.Destroy;
  99. begin
  100. FStageCompareData.Free;
  101. FPhasePayData.Free;
  102. FPhaseProperty.Free;
  103. FStageData.Free;
  104. FUpdator.Free;
  105. FConnection.Free;
  106. inherited;
  107. end;
  108. function TPhaseData.GetActive: Boolean;
  109. begin
  110. Result := FConnection.Connection.Connected;
  111. end;
  112. function TPhaseData.GetAllowInsert: Boolean;
  113. begin
  114. Result := not PhaseProperty.FinalAudit;
  115. end;
  116. function TPhaseData.GetStageDataReadOnly: Boolean;
  117. begin
  118. Result := not IsLastStage or PhaseProperty.FinalAudit;
  119. end;
  120. function TPhaseData.GetFileName: string;
  121. begin
  122. Result := FConnection.OrgFile;
  123. end;
  124. function TPhaseData.GetPhaseIndex: Integer;
  125. begin
  126. Result := TProjectData(FProjectData).PhaseIndex;
  127. end;
  128. procedure TPhaseData.Open(const AProjectName: string);
  129. begin
  130. UpdateSysProgress(35, '正在打开文件');
  131. FConnection.Open(AProjectName);
  132. UpdateSysProgress(40, '正在升级文件');
  133. UpdatePhaseDataBase;
  134. UpdateSysProgress(50, '正在读取数据');
  135. FPhaseProperty.Open;
  136. CheckAuditCount;
  137. FStageIndex := FPhaseProperty.AuditCount;
  138. UpdateSysProgress(140, '正在读取数据');
  139. FStageData.TableName := StageTableName[FStageIndex];
  140. FStageData.Open(FConnection.Connection);
  141. UpdateSysProgress(160, '正在读取数据');
  142. FPhasePayData.Open(FConnection.Connection);
  143. UpdateSysProgress(180, '正在读取数据');
  144. FZJJLData.Open(FConnection.Connection);
  145. UpdateSysProgress(200, '就绪');
  146. end;
  147. procedure TPhaseData.Save;
  148. begin
  149. TProjectData(FProjectData).AppendProjectLog('Save Phase Data');
  150. UpdateSysProgress(30, '正在保存数据');
  151. FPhaseProperty.Save;
  152. UpdateSysProgress(80, '正在保存数据');
  153. FStageData.Save;
  154. UpdateSysProgress(150, '正在保存数据');
  155. FPhasePayData.Save;
  156. UpdateSysProgress(170, '正在保存数据');
  157. FZJJLData.Save;
  158. UpdateSysProgress(180, '正在保存数据');
  159. FConnection.Save;
  160. UpdateSysProgress(200, '就绪');
  161. TProjectData(FProjectData).AppendProjectLog('Save Phase Data --> End');
  162. end;
  163. procedure TPhaseData.SimpleOpen(const AProjectName: string);
  164. begin
  165. FConnection.Open(AProjectName);
  166. UpdatePhaseDataBase;
  167. FPhaseProperty.Open;
  168. CheckAuditCount;
  169. FPhasePayData.Open(FConnection.Connection);
  170. end;
  171. procedure TPhaseData.SimpleSave;
  172. begin
  173. FPhasePayData.Save;
  174. FPhaseProperty.Save;
  175. FConnection.Save;
  176. end;
  177. procedure TPhaseData.UpdatePhaseDataBase;
  178. begin
  179. FUpdator.Update(FConnection);
  180. end;
  181. function TPhaseData.GetmdbFileName: string;
  182. begin
  183. Result := FConnection.FileName;
  184. end;
  185. procedure TPhaseData.CopyPreData;
  186. begin
  187. StageData.CopyPrePhaseData;
  188. PhasePayData.CopyPrePhasePayData;
  189. end;
  190. function TPhaseData.GetADOConnection: TADOConnection;
  191. begin
  192. Result := FConnection.Connection;
  193. end;
  194. procedure TPhaseData.SetStageIndex(const Value: Integer);
  195. procedure ReCreateStageData;
  196. begin
  197. if StageData.Active then
  198. StageData.Save;
  199. StageData.Free;
  200. FStageData := TStageData.Create(Self);
  201. FStageData.TableName := StageTableName[fStageIndex];
  202. FStageData.Open(ADOConnection);
  203. end;
  204. begin
  205. TProjectData(FProjectData).SaveLastestPhaseMainData;
  206. FStageIndex := Value;
  207. ReCreateStageData;
  208. TProjectData(FProjectData).ResetPhaseDataLink;
  209. end;
  210. function TPhaseData.GetStageTableName(AIndex: Integer): string;
  211. begin
  212. if AIndex > 0 then
  213. Result := 'Audit' + IntToStr(AIndex)
  214. else
  215. Result := 'Refer';
  216. end;
  217. function TPhaseData.GetAuditCount: Integer;
  218. begin
  219. Result := PhaseProperty.AuditCount;
  220. end;
  221. function TPhaseData.GetStageCount: Integer;
  222. begin
  223. Result := PhaseProperty.AuditCount + 1;
  224. end;
  225. procedure TPhaseData.CheckAuditCount;
  226. function GetRealAuditCount: Integer;
  227. var
  228. Tables: TStrings;
  229. begin
  230. Tables := TStringList.Create;
  231. try
  232. FConnection.Connection.GetTableNames(Tables);
  233. Result := 0;
  234. while Tables.IndexOf('Audit' + IntToStr(Result + 1)) > -1 do
  235. Inc(Result);
  236. finally
  237. Tables.Free;
  238. end;
  239. end;
  240. var
  241. iAuditCount: Integer;
  242. begin
  243. // 改为强制检验
  244. //if FPhaseProperty.AuditCount = 0 then
  245. //begin
  246. iAuditCount := GetRealAuditCount;
  247. if iAuditCount <> FPhaseProperty.AuditCount then
  248. FPhaseProperty.AuditCount := iAuditCount;
  249. //end;
  250. end;
  251. function TPhaseData.GetIsLastStage: Boolean;
  252. begin
  253. Result := FStageIndex = FPhaseProperty.AuditCount;
  254. end;
  255. procedure TPhaseData.CopyPreStageData;
  256. const
  257. sCopySql = 'Insert Into %s Select * From %s';
  258. var
  259. sPre, sCur, sSql: string;
  260. begin
  261. FUpdator.AddAuditData(FConnection.Connection, FPhaseProperty.AuditCount);
  262. sPre := StageTableName[FPhaseProperty.AuditCount-1];
  263. sCur := StageTableName[FPhaseProperty.AuditCount];
  264. sSql := Format(sCopySql, [sCur, sPre]);
  265. ExecuteSql(FConnection.Connection, sSql);
  266. end;
  267. procedure TPhaseData.SimpleOpen2(const AProjectName: string);
  268. begin
  269. FConnection.Open(AProjectName);
  270. UpdatePhaseDataBase;
  271. FPhaseProperty.Open;
  272. CheckAuditCount;
  273. FStageIndex := FPhaseProperty.AuditCount;
  274. FStageData.TableName := StageTableName[FStageIndex];
  275. FStageData.Open(FConnection.Connection);
  276. FPhasePayData.Open(FConnection.Connection);
  277. FZJJLData.Open(FConnection.Connection);
  278. end;
  279. end.