rpgGatherData.pas 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384
  1. unit rpgGatherData;
  2. interface
  3. uses
  4. ADODB, ProjGather, rpgGatherProjDm, rpgBillsDm, rpgBillsCalcDm, Classes,
  5. ScAutoUpdateUnit, ProjGatherTree;
  6. type
  7. TrpgGatherData = class
  8. private
  9. FGatherFile: string;
  10. FConnection: TADOConnection;
  11. FQuery: TADOQuery;
  12. procedure ExecuteSql(const ASql: string);
  13. procedure ClearHistoryData;
  14. procedure CreateDataTables(AProjCount, ASProjCount: Integer);
  15. procedure SaveGatherProjInfo(AProjs, ASProjs: TList);
  16. procedure SaveBills(ATree: TProjGatherTree);
  17. procedure SaveBillsGatherCalc(ATree: TProjGatherTree);
  18. procedure SaveBillsProjCalc(ATree: TProjGatherTree; AProjIndex: Integer);
  19. procedure SaveBillsSpecialProjCalc(ATree: TProjGatherTree; AProjType: Integer);
  20. procedure SaveGatherData(AGather: TProjGather);
  21. procedure CalcDgnData(const ATableName: string);
  22. procedure CalcOtherData(AProjCount, ASProjCount: Integer);
  23. procedure TransposeProjCalc(AProjCount: Integer);
  24. function GetCurSpecialProjCount: Integer;
  25. protected
  26. procedure AddTables(AProjCount, ASProjCount: Integer; AUpdater: TScUpdater); virtual;
  27. public
  28. constructor Create;
  29. destructor Destroy; override;
  30. procedure WriteGatherData(AGather: TProjGather);
  31. procedure LoadRelaData(AProjectID: Integer);
  32. procedure UpdateDataBase(ASpecialProjTypes: TStrings);
  33. property GatherFile: string read FGatherFile;
  34. property Connection: TADOConnection read FConnection;
  35. end;
  36. implementation
  37. uses
  38. UtilMethods, SysUtils, Connections, ZhAPI, rProjGatherTables,
  39. ConditionalDefines, ProjectData, Globals;
  40. { TrpgGatherData }
  41. procedure TrpgGatherData.AddTables(AProjCount, ASProjCount: Integer;
  42. AUpdater: TScUpdater);
  43. var
  44. iProj: Integer;
  45. begin
  46. AUpdater.AddTableDef(SGatherProj, @tdGatherProj, Length(tdGatherProj), False, False);
  47. AUpdater.AddTableDef(SBills, @tdBills, Length(tdBills), False, False);
  48. AUpdater.AddTableDef(SBills_Gather, @tdBills_Calc, Length(tdBills_Calc), False, False);
  49. for iProj := 0 to AProjCount - 1 do
  50. AUpdater.AddTableDef(SBills_Proj+IntToStr(iProj+1), @tdBills_Calc, Length(tdBills_Calc), False, False);
  51. AUpdater.AddTableDef(SBills_TransProj, @tdBills_Calc, Length(tdBills_Calc), False, False);
  52. for iProj := 0 to ASProjCount - 1 do
  53. AUpdater.AddTableDef(SBills_SProj+IntToStr(iProj+1), @tdBills_Calc, Length(tdBills_Calc), False, False);
  54. end;
  55. procedure TrpgGatherData.CalcDgnData(const ATableName: string);
  56. const
  57. sFinalDgn = 'Update %s Set'+
  58. ' FinalDgnQuantity1 = DealDgnQuantity1 + CDgnQuantity1,'+
  59. ' FinalDgnQuantity2 = DealDgnQuantity2 + CDgnQuantity2';
  60. sDgnPrice1_2 = 'Update %s Set'+
  61. ' DgnPrice1 = iif(DgnQuantity1 <> 0, TotalPrice/DgnQuantity1, 0),'+
  62. ' DgnPrice1_Rc = iif(DgnQuantity1 <> 0, TotalPrice_Rc/DgnQuantity1, 0),'+
  63. ' DgnPrice2 = iif(DgnQuantity2 <> 0, TotalPrice/DgnQuantity2, 0),'+
  64. ' DgnPrice2_Rc = iif(DgnQuantity2 <> 0, TotalPrice_Rc/DgnQuantity2, 0),'+
  65. ' FinalDgnPrice1 = iif(FinalDgnQuantity1 <> 0, AddGatherTotalPrice/FinalDgnQuantity1, 0),'+
  66. ' FinalDgnPrice1_Rc = iif(FinalDgnQuantity1 <> 0, AddGatherTotalPrice_Rc/FinalDgnQuantity1, 0),'+
  67. ' FinalDgnPrice2 = iif(FinalDgnQuantity2 <> 0, AddGatherTotalPrice/FinalDgnQuantity2, 0),'+
  68. ' FinalDgnPrice2_Rc = iif(FinalDgnQuantity2 <> 0, AddGatherTotalPrice_Rc/FinalDgnQuantity2, 0)';
  69. sDgn = 'Update %s Set'+
  70. ' DgnQuantity = iif(DgnQuantity1 <> 0, iif(DgnQuantity2 <> 0, DgnQuantity1&''/''&DgnQuantity2, DgnQuantity1), ''''),'+
  71. ' DgnPrice = iif(DgnPrice1 <> 0, iif(DgnPrice2 <> 0, DgnPrice1&''/''&DgnPrice2, DgnPrice1), ''''),'+
  72. ' DgnPrice_Rc = iif(DgnPrice1_Rc <> 0, iif(DgnPrice2_Rc <> 0, DgnPrice1_Rc&''/''&DgnPrice2_Rc, DgnPrice1_Rc), ''''),'+
  73. ' DealDgnQuantity = iif(DealDgnQuantity1 <> 0, iif(DealDgnQuantity2 <> 0, DealDgnQuantity1&''/''&DealDgnQuantity2, DealDgnQuantity1), ''''),'+
  74. ' CDgnQuantity = iif(CDgnQuantity1 <> 0, iif(CDgnQuantity2 <> 0, CDgnQuantity1&''/''&CDgnQuantity2, CDgnQuantity1), ''''),'+
  75. ' FinalDgnQuantity = iif(FinalDgnQuantity1 <> 0, iif(FinalDgnQuantity2 <> 0, FinalDgnQuantity1&''/''&FinalDgnQuantity2, FinalDgnQuantity1), ''''),'+
  76. ' FinalDgnPrice = iif(FinalDgnPrice1 <> 0, iif(FinalDgnPrice2 <> 0, FinalDgnPrice1&''/''&FinalDgnPrice2, FinalDgnPrice1), ''''),'+
  77. ' FinalDgnPrice_Rc = iif(FinalDgnPrice1_Rc <> 0, iif(FinalDgnPrice2_Rc <> 0, FinalDgnPrice1_Rc&''/''&FinalDgnPrice2_Rc, FinalDgnPrice1_Rc), '''')';
  78. begin
  79. ExecuteSql(Format(sFinalDgn, [ATableName]));
  80. ExecuteSql(Format(sDgnPrice1_2, [ATableName]));
  81. ExecuteSql(Format(sDgn, [ATableName]));
  82. end;
  83. procedure TrpgGatherData.CalcOtherData(AProjCount, ASProjCount: Integer);
  84. var
  85. iProj: Integer;
  86. begin
  87. CalcDgnData(SBills_Gather);
  88. for iProj := 0 to AProjCount - 1 do
  89. CalcDgnData(SBills_Proj+IntToStr(iProj+1));
  90. for iProj := 0 to ASProjCount - 1 do
  91. CalcDgnData(SBills_SProj+IntToStr(iProj+1));
  92. end;
  93. procedure TrpgGatherData.ClearHistoryData;
  94. var
  95. FTableList: TStringList;
  96. iIndex: Integer;
  97. sDeleteTableSql: String;
  98. begin
  99. FTableList := TStringList.Create;
  100. try
  101. FConnection.GetTableNames(FTableList);
  102. iIndex := 0;
  103. while iIndex < FTableList.Count do
  104. begin
  105. if Pos('r_', FTableList.Strings[iIndex]) = 1 then
  106. begin
  107. sDeleteTableSql := Format('Drop Table %s', [FTableList.Strings[iIndex]]);
  108. ExecuteSql(sDeleteTableSql);
  109. end;
  110. Inc(iIndex);
  111. end;
  112. finally
  113. FTableList.Free;
  114. end;
  115. end;
  116. constructor TrpgGatherData.Create;
  117. begin
  118. FGatherFile := GetTempFileName;
  119. CopyFileOrFolder(GetEmptyDataBaseFileName, FGatherFile);
  120. FConnection := TADOConnection.Create(nil);
  121. FConnection.LoginPrompt := False;
  122. FConnection.ConnectionString := Format(SAdoConnectStr, [FGatherFile]);
  123. FConnection.Open;
  124. FQuery := TADOQuery.Create(nil);
  125. FQuery.Connection := FConnection;
  126. end;
  127. procedure TrpgGatherData.CreateDataTables(AProjCount, ASProjCount: Integer);
  128. var
  129. Updater: TScUpdater;
  130. begin
  131. Updater := TScUpdater.Create;
  132. try
  133. Updater.ForceUpdate := True;
  134. Updater.Open('', FConnection, '', '');
  135. AddTables(AProjCount, ASProjCount, Updater);
  136. Updater.ExcuteUpdate;
  137. finally
  138. Updater.Free;
  139. end;
  140. end;
  141. destructor TrpgGatherData.Destroy;
  142. begin
  143. FQuery.Free;
  144. FConnection.Free;
  145. if FileExists(FGatherFile) then
  146. DeleteFile(FGatherFile);
  147. inherited;
  148. end;
  149. procedure TrpgGatherData.ExecuteSql(const ASql: string);
  150. var
  151. vQuery: TADOQuery;
  152. begin
  153. vQuery := TADOQuery.Create(nil);
  154. try
  155. vQuery.Connection := FConnection;
  156. vQuery.SQL.Add(ASql);
  157. vQuery.ExecSQL;
  158. finally
  159. vQuery.Free;
  160. end;
  161. {FQuery.SQL.Clear;
  162. FQuery.SQL.Add(ASql);
  163. FQuery.ExecSQL;}
  164. end;
  165. function TrpgGatherData.GetCurSpecialProjCount: Integer;
  166. begin
  167. Result := 0;
  168. end;
  169. procedure TrpgGatherData.LoadRelaData(AProjectID: Integer);
  170. const
  171. sCopyProperty = 'Select * Into r_ProjProperties'+
  172. ' From ProjProperties In ''%s''';
  173. var
  174. sFileName: string;
  175. vProjectData: TProjectData;
  176. begin
  177. vProjectData := OpenProjectManager.FindProjectData(AProjectID);
  178. if Assigned(vProjectData) then Exit;
  179. try
  180. sFileName := GetTempFileName;
  181. vProjectData.SaveTempDataBaseFile(sFileName);
  182. ExecuteSql(Format(sCopyProperty, [sFileName]));
  183. finally
  184. if FileExists(sFileName) then
  185. DeleteFile(sFileName);
  186. end;
  187. end;
  188. procedure TrpgGatherData.SaveBills(ATree: TProjGatherTree);
  189. var
  190. vBillsData: TrpgBillsData;
  191. begin
  192. vBillsData := TrpgBillsData.Create(FConnection);
  193. try
  194. vBillsData.SaveDataTo(ATree, SBills);
  195. finally
  196. vBillsData.Free;
  197. end;
  198. end;
  199. procedure TrpgGatherData.SaveBillsGatherCalc(ATree: TProjGatherTree);
  200. var
  201. vBillsCalcData: TrpgBillsCalcData;
  202. begin
  203. vBillsCalcData := TrpgBillsCalcData.Create(FConnection);
  204. try
  205. vBillsCalcData.SaveGatherDataTo(ATree, SBills_Gather);
  206. finally
  207. vBillsCalcData.Free;
  208. end;
  209. end;
  210. procedure TrpgGatherData.SaveBillsProjCalc(ATree: TProjGatherTree;
  211. AProjIndex: Integer);
  212. var
  213. vBillsCalcData: TrpgBillsCalcData;
  214. begin
  215. vBillsCalcData := TrpgBillsCalcData.Create(FConnection);
  216. try
  217. vBillsCalcData.SaveProjDataTo(ATree, AProjIndex, SBills_Proj+IntToStr(AProjIndex+1));
  218. finally
  219. vBillsCalcData.Free;
  220. end;
  221. end;
  222. procedure TrpgGatherData.SaveBillsSpecialProjCalc(ATree: TProjGatherTree;
  223. AProjType: Integer);
  224. var
  225. vBillsCalcData: TrpgBillsCalcData;
  226. begin
  227. vBillsCalcData := TrpgBillsCalcData.Create(FConnection);
  228. try
  229. vBillsCalcData.SaveSpecialProjDataTo(ATree, AProjType, SBills_SProj+IntToStr(AProjType));
  230. finally
  231. vBillsCalcData.Free;
  232. end;
  233. end;
  234. procedure TrpgGatherData.SaveGatherData(AGather: TProjGather);
  235. const
  236. sInsert = 'Insert Into %s Select * From %s';
  237. var
  238. iProj: Integer;
  239. begin
  240. SaveGatherProjInfo(AGather.CommonProj, AGather.SpecialProj);
  241. SaveBills(AGather.Tree);
  242. SaveBillsGatherCalc(AGather.Tree);
  243. for iProj := 0 to AGather.CommonProj.Count - 1 do
  244. begin
  245. SaveBillsProjCalc(AGather.Tree, iProj);
  246. ExecuteSql(Format(sInsert, [SBills_TransProj, SBills_Proj+IntToStr(iProj+1)]));
  247. end;
  248. for iProj := 0 to AGather.Tree.SpecialProjCount - 1 do
  249. SaveBillsSpecialProjCalc(AGather.Tree, iProj+1);
  250. end;
  251. procedure TrpgGatherData.SaveGatherProjInfo(AProjs, ASProjs: TList);
  252. var
  253. vGatherInfoData: TrpgGatherProjData;
  254. begin
  255. vGatherInfoData := TrpgGatherProjData.Create(FConnection);
  256. try
  257. vGatherInfoData.SaveDataTo(AProjs, ASProjs, SGatherProj);
  258. finally
  259. vGatherInfoData.Free;
  260. end;
  261. end;
  262. procedure TrpgGatherData.TransposeProjCalc(AProjCount: Integer);
  263. const
  264. sInsert = 'Insert Into %s Select * From %s';
  265. var
  266. iProj: Integer;
  267. begin
  268. for iProj := 0 to AProjCount - 1 do
  269. ExecuteSql(Format(sInsert, [SBills_TransProj, SBills_Proj+IntToStr(iProj+1)]));
  270. end;
  271. procedure TrpgGatherData.UpdateDataBase(ASpecialProjTypes: TStrings);
  272. const
  273. sUpdateSql = 'Insert Into %s (BillsID, ProjID, ProjType,' +
  274. ' OrgQuantity, OrgTotalPrice, OrgTotalPrice_Rc,' +
  275. ' MisQuantity, MisTotalPrice, MisTotalPrice_Rc,' +
  276. ' OthQuantity, OthTotalPrice, OthTotalPrice_Rc,' +
  277. ' Quantity, TotalPrice, TotalPrice_Rc,' +
  278. ' DgnQuantity1, DgnQuantity2, DgnQuantity,' +
  279. ' DgnPrice1, DgnPrice2, DgnPrice,' +
  280. ' DgnPrice1_Rc, DgnPrice2_Rc, DgnPrice_Rc,' +
  281. ' DealDgnQuantity1, DealDgnQuantity2, DealDgnQuantity,' +
  282. ' CDgnQuantity1, CDgnQuantity2, CDgnQuantity,' +
  283. ' FinalDgnQuantity1, FinalDgnQuantity2, FinalDgnQuantity,' +
  284. ' FinalDgnPrice1, FinalDgnPrice2, FinalDgnPrice,' +
  285. ' FinalDgnPrice1_Rc, FinalDgnPrice2_Rc, FinalDgnPrice_Rc,' +
  286. ' AddDealQuantity, AddDealTotalPrice, AddDealTotalPrice_Rc,' +
  287. ' AddQcQuantity, AddQcTotalPrice, AddQcTotalPrice_Rc,' +
  288. ' AddGatherQuantity, AddGatherTotalPrice, AddGatherTotalPrice_Rc,' +
  289. ' CurDealQuantity, CurDealTotalPrice, CurDealTotalPrice_Rc,' +
  290. ' CurQcQuantity, CurQcTotalPrice, CurQcTotalPrice_Rc,' +
  291. ' CurGatherQuantity, CurGatherTotalPrice, CurGatherTotalPrice_Rc,' +
  292. ' PreDealQuantity, PreDealTotalPrice, PreDealTotalPrice_Rc,' +
  293. ' PreQcQuantity, PreQcTotalPrice, PreQcTotalPrice_Rc,' +
  294. ' PreGatherQuantity, PreGatherTotalPrice, PreGatherTotalPrice_Rc,' +
  295. ' EndDealQuantity, EndDealTotalPrice, EndDealTotalPrice_Rc,' +
  296. ' EndQcQuantity, EndQcTotalPrice, EndQcTotalPrice_Rc,' +
  297. ' EndGatherQuantity, EndGatherTotalPrice, EndGatherTotalPrice_Rc,' +
  298. ' Select BillsID, %d, %d,'+
  299. ' 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,'+ // 台账明细
  300. ' 0, 0, 0,'+ // 台账
  301. ' 0, 0, '''', 0, 0, '''', 0, 0, '''','+ // 台账--设计数量、经济指标
  302. ' 0, 0, '''', 0, 0, '''','+ // 计量--设计数量
  303. ' 0, 0, '''', 0, 0, '''', 0, 0, '''','+ // 计量--经济指标
  304. ' 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,'+ // 累计数据
  305. ' 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,'+ // 本期
  306. ' 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,'+ // 截止上期
  307. ' 0, 0, 0, 0, 0, 0, 0, 0, 0, 0'+ // 截止本期
  308. ' From r_Bills_Gather';
  309. var
  310. Updater: TScUpdater;
  311. iProj, iSpecialProjCount: Integer;
  312. begin
  313. iSpecialProjCount := GetCurSpecialProjCount;
  314. if ASpecialProjTypes.Count > iSpecialProjCount then
  315. begin
  316. Updater := TScUpdater.Create;
  317. try
  318. Updater.ForceUpdate := True;
  319. Updater.Open('', FConnection, '', '');
  320. for iProj := iSpecialProjCount to ASpecialProjTypes.Count - 1 do
  321. Updater.AddTableDef(SBills_SProj+IntToStr(iProj+1), @tdBills_Calc, Length(tdBills_Calc), False, False);
  322. Updater.ExcuteUpdate;
  323. finally
  324. Updater.Free;
  325. end;
  326. for iProj := iSpecialProjCount to ASpecialProjTypes.Count - 1 do
  327. ExecuteSql(Format(sUpdateSql, [SBills_SProj+IntToStr(iProj+1), iProj, iProj+1]));
  328. end;
  329. end;
  330. procedure TrpgGatherData.WriteGatherData(AGather: TProjGather);
  331. begin
  332. ClearHistoryData;
  333. CreateDataTables(AGather.Tree.ProjCount, AGather.Tree.SpecialProjCount);
  334. SaveGatherData(AGather);
  335. CalcOtherData(AGather.Tree.ProjCount, AGather.Tree.SpecialProjCount);
  336. // 集中处理TranProj时,最后一个标段数据丢失
  337. //TransposeProjCalc(AGather.Tree.ProjCount);
  338. if _IsDebugView then
  339. CopyFileOrFolder(FGatherFile, GetAppFilePath+'CommonProjGather.dat');
  340. end;
  341. end.