123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449 |
- unit rpgGatherData;
- interface
- uses
- ADODB, ProjGather, rpgGatherProjDm, rpgBillsDm, rpgBillsCalcDm, Classes,
- ScAutoUpdateUnit, ProjGatherTree, GatherProjInfo;
- type
- TrpgGatherData = class
- private
- FGatherFile: string;
- FConnection: TADOConnection;
- procedure ClearHistoryData;
- procedure CreateDataTables(AProjCount, ASProjCount: Integer);
- procedure SaveGatherProjInfo(AProjs, ASProjs: TList);
- procedure SaveBills(ATree: TProjGatherTree);
- procedure SaveBillsGatherCalc(ATree: TProjGatherTree);
- procedure SaveBillsProjCalc(ATree: TProjGatherTree; AProjIndex: Integer);
- procedure SaveBillsSpecialProjCalc(ATree: TProjGatherTree; AProjType: Integer);
- procedure CalcDgnData(const ATableName: string);
- procedure CalcOtherData(AProjCount, ASProjCount: Integer);
- procedure TransposeProjCalc(AProjCount: Integer);
- function GetCacheProjFile(AProj: TGatherProjInfo): string;
- procedure CopyBGLData(const AFileName: string; AProjIndex, AProjType: Integer);
- procedure CopyProjRelaGatherData(AProj: TGatherProjInfo; AProjIndex: Integer);
- function GetCurSpecialProjCount: Integer;
- protected
- procedure AddTables(AProjCount, ASProjCount: Integer; AUpdater: TScUpdater); virtual;
- procedure SaveGatherData(AGather: TProjGather); virtual;
- procedure CopyRelaGatherData(AGather: TProjGather); virtual;
- public
- constructor Create;
- destructor Destroy; override;
- procedure WriteGatherData(AGather: TProjGather);
- procedure LoadRelaData(AProjectID: Integer);
- procedure UpdateDataBase(ASpecialProjTypes: TStrings);
- property GatherFile: string read FGatherFile;
- property Connection: TADOConnection read FConnection;
- end;
- implementation
- uses
- UtilMethods, SysUtils, Connections, ZhAPI, rProjGatherTables,
- ConditionalDefines, ProjectData, Globals;
- { TrpgGatherData }
- procedure TrpgGatherData.AddTables(AProjCount, ASProjCount: Integer;
- AUpdater: TScUpdater);
- var
- iProj: Integer;
- begin
- AUpdater.AddTableDef(SGatherProj, @tdGatherProj, Length(tdGatherProj), False, False);
- AUpdater.AddTableDef(SBills, @tdBills, Length(tdBills), False, False);
- AUpdater.AddTableDef(SBills_Gather, @tdBills_Calc, Length(tdBills_Calc), False, False);
- for iProj := 0 to AProjCount - 1 do
- AUpdater.AddTableDef(SBills_Proj+IntToStr(iProj+1), @tdBills_Calc, Length(tdBills_Calc), False, False);
- AUpdater.AddTableDef(SBills_TransProj, @tdBills_Calc, Length(tdBills_Calc), False, False);
- for iProj := 0 to ASProjCount - 1 do
- AUpdater.AddTableDef(SBills_SProj+IntToStr(iProj+1), @tdBills_Calc, Length(tdBills_Calc), False, False);
- AUpdater.AddTableDef(SBGL_TransProj, @tdBGL_TransProj, Length(tdBGL_TransProj), False, False);
- end;
- procedure TrpgGatherData.CalcDgnData(const ATableName: string);
- const
- sFinalDgn = 'Update %s Set'+
- ' FinalDgnQuantity1 = DealDgnQuantity1 + CDgnQuantity1,'+
- ' FinalDgnQuantity2 = DealDgnQuantity2 + CDgnQuantity2';
- sDgnPrice1_2 = 'Update %s Set'+
- ' DgnPrice1 = iif(DgnQuantity1 <> 0, TotalPrice/DgnQuantity1, 0),'+
- ' DgnPrice1_Rc = iif(DgnQuantity1 <> 0, TotalPrice_Rc/DgnQuantity1, 0),'+
- ' DgnPrice2 = iif(DgnQuantity2 <> 0, TotalPrice/DgnQuantity2, 0),'+
- ' DgnPrice2_Rc = iif(DgnQuantity2 <> 0, TotalPrice_Rc/DgnQuantity2, 0),'+
- ' FinalDgnPrice1 = iif(FinalDgnQuantity1 <> 0, AddGatherTotalPrice/FinalDgnQuantity1, 0),'+
- ' FinalDgnPrice1_Rc = iif(FinalDgnQuantity1 <> 0, AddGatherTotalPrice_Rc/FinalDgnQuantity1, 0),'+
- ' FinalDgnPrice2 = iif(FinalDgnQuantity2 <> 0, AddGatherTotalPrice/FinalDgnQuantity2, 0),'+
- ' FinalDgnPrice2_Rc = iif(FinalDgnQuantity2 <> 0, AddGatherTotalPrice_Rc/FinalDgnQuantity2, 0)';
- sDgn = 'Update %s Set'+
- ' DgnQuantity = iif(DgnQuantity1 <> 0, iif(DgnQuantity2 <> 0, DgnQuantity1&''/''&DgnQuantity2, DgnQuantity1), ''''),'+
- ' DgnPrice = iif(DgnPrice1 <> 0, iif(DgnPrice2 <> 0, DgnPrice1&''/''&DgnPrice2, DgnPrice1), ''''),'+
- ' DgnPrice_Rc = iif(DgnPrice1_Rc <> 0, iif(DgnPrice2_Rc <> 0, DgnPrice1_Rc&''/''&DgnPrice2_Rc, DgnPrice1_Rc), ''''),'+
- ' DealDgnQuantity = iif(DealDgnQuantity1 <> 0, iif(DealDgnQuantity2 <> 0, DealDgnQuantity1&''/''&DealDgnQuantity2, DealDgnQuantity1), ''''),'+
- ' CDgnQuantity = iif(CDgnQuantity1 <> 0, iif(CDgnQuantity2 <> 0, CDgnQuantity1&''/''&CDgnQuantity2, CDgnQuantity1), ''''),'+
- ' FinalDgnQuantity = iif(FinalDgnQuantity1 <> 0, iif(FinalDgnQuantity2 <> 0, FinalDgnQuantity1&''/''&FinalDgnQuantity2, FinalDgnQuantity1), ''''),'+
- ' FinalDgnPrice = iif(FinalDgnPrice1 <> 0, iif(FinalDgnPrice2 <> 0, FinalDgnPrice1&''/''&FinalDgnPrice2, FinalDgnPrice1), ''''),'+
- ' FinalDgnPrice_Rc = iif(FinalDgnPrice1_Rc <> 0, iif(FinalDgnPrice2_Rc <> 0, FinalDgnPrice1_Rc&''/''&FinalDgnPrice2_Rc, FinalDgnPrice1_Rc), '''')';
- begin
- ExecuteSql(FConnection, Format(sFinalDgn, [ATableName]));
- ExecuteSql(FConnection, Format(sDgnPrice1_2, [ATableName]));
- ExecuteSql(FConnection, Format(sDgn, [ATableName]));
- end;
- procedure TrpgGatherData.CalcOtherData(AProjCount, ASProjCount: Integer);
- var
- iProj: Integer;
- begin
- CalcDgnData(SBills_Gather);
- for iProj := 0 to AProjCount - 1 do
- CalcDgnData(SBills_Proj+IntToStr(iProj+1));
- CalcDgnData(SBills_TransProj);
- for iProj := 0 to ASProjCount - 1 do
- CalcDgnData(SBills_SProj+IntToStr(iProj+1));
- end;
- procedure TrpgGatherData.ClearHistoryData;
- var
- FTableList: TStringList;
- iIndex: Integer;
- sDeleteTableSql: String;
- begin
- FTableList := TStringList.Create;
- try
- FConnection.GetTableNames(FTableList);
- iIndex := 0;
- while iIndex < FTableList.Count do
- begin
- if Pos('r_', FTableList.Strings[iIndex]) = 1 then
- begin
- sDeleteTableSql := Format('Drop Table %s', [FTableList.Strings[iIndex]]);
- ExecuteSql(FConnection, sDeleteTableSql);
- end;
- Inc(iIndex);
- end;
- finally
- FTableList.Free;
- end;
- end;
- procedure TrpgGatherData.CopyProjRelaGatherData(AProj: TGatherProjInfo;
- AProjIndex: Integer);
- var
- sTempFile: string;
- begin
- try
- sTempFile := GetCacheProjFile(AProj);
- CopyBGLData(sTempFile, AProjIndex, AProj.ProjType);
- finally
- if FileExists(sTempFile) then
- DeleteFile(sTempFile);
- end;
- end;
- constructor TrpgGatherData.Create;
- begin
- FGatherFile := GetTempFileName;
- CopyFileOrFolder(GetEmptyDataBaseFileName, FGatherFile);
- FConnection := TADOConnection.Create(nil);
- FConnection.LoginPrompt := False;
- FConnection.ConnectionString := Format(SAdoConnectStr, [FGatherFile]);
- FConnection.Open;
- end;
- procedure TrpgGatherData.CreateDataTables(AProjCount, ASProjCount: Integer);
- var
- Updater: TScUpdater;
- begin
- Updater := TScUpdater.Create;
- try
- Updater.ForceUpdate := True;
- Updater.Open('', FConnection, '', '');
- AddTables(AProjCount, ASProjCount, Updater);
- Updater.ExcuteUpdate;
- finally
- Updater.Free;
- end;
- end;
- destructor TrpgGatherData.Destroy;
- begin
- FConnection.Free;
- if FileExists(FGatherFile) then
- DeleteFile(FGatherFile);
- inherited;
- end;
- function TrpgGatherData.GetCurSpecialProjCount: Integer;
- var
- sgsTables: TStringList;
- iTable: Integer;
- begin
- Result := 0;
- sgsTables := TStringList.Create;
- try
- FConnection.GetTableNames(sgsTables);
- for iTable := 0 to sgsTables.Count - 1 do
- begin
- if Pos(SBills_SProj, sgsTables.Strings[iTable]) > 0 then
- Inc(Result);
- end;
- finally
- sgsTables.Free;
- end;
- end;
- procedure TrpgGatherData.CopyBGLData(const AFileName: string; AProjIndex, AProjType: Integer);
- const
- sCopyBGL = 'Insert Into r_BGL_TransProj' +
- ' Select ID, %d As ProjID, %d As ProjType,' +
- ' Code, Name, TotalPrice, Pos_Reason, Direction, DrawingCode, ApprovalCode, BGLType' +
- ' From BGL In ''%s''';
- begin
- ExecuteSql(FConnection, Format(sCopyBGL, [AProjIndex, AProjType, AFileName]));
- end;
- procedure TrpgGatherData.LoadRelaData(AProjectID: Integer);
- const
- sCopyProperty = 'Select * Into r_ProjProperties'+
- ' From ProjProperties In ''%s''';
- var
- sFileName: string;
- vProjectData: TProjectData;
- begin
- vProjectData := OpenProjectManager.FindProjectData(AProjectID);
- if Assigned(vProjectData) then Exit;
- try
- sFileName := GetTempFileName;
- vProjectData.SaveTempDataBaseFile(sFileName);
- ExecuteSql(FConnection, Format(sCopyProperty, [sFileName]));
- finally
- if FileExists(sFileName) then
- DeleteFile(sFileName);
- end;
- end;
- procedure TrpgGatherData.SaveBills(ATree: TProjGatherTree);
- var
- vBillsData: TrpgBillsData;
- begin
- vBillsData := TrpgBillsData.Create(FConnection);
- try
- vBillsData.SaveDataTo(ATree, SBills);
- finally
- vBillsData.Free;
- end;
- end;
- procedure TrpgGatherData.SaveBillsGatherCalc(ATree: TProjGatherTree);
- var
- vBillsCalcData: TrpgBillsCalcData;
- begin
- vBillsCalcData := TrpgBillsCalcData.Create(FConnection);
- try
- vBillsCalcData.SaveGatherDataTo(ATree, SBills_Gather);
- finally
- vBillsCalcData.Free;
- end;
- end;
- procedure TrpgGatherData.SaveBillsProjCalc(ATree: TProjGatherTree;
- AProjIndex: Integer);
- var
- vBillsCalcData: TrpgBillsCalcData;
- begin
- vBillsCalcData := TrpgBillsCalcData.Create(FConnection);
- try
- vBillsCalcData.SaveProjDataTo(ATree, AProjIndex, SBills_Proj+IntToStr(AProjIndex+1));
- finally
- vBillsCalcData.Free;
- end;
- end;
- procedure TrpgGatherData.SaveBillsSpecialProjCalc(ATree: TProjGatherTree;
- AProjType: Integer);
- var
- vBillsCalcData: TrpgBillsCalcData;
- begin
- vBillsCalcData := TrpgBillsCalcData.Create(FConnection);
- try
- vBillsCalcData.SaveSpecialProjDataTo(ATree, AProjType, SBills_SProj+IntToStr(AProjType));
- finally
- vBillsCalcData.Free;
- end;
- end;
- procedure TrpgGatherData.SaveGatherData(AGather: TProjGather);
- const
- sInsert = 'Insert Into %s Select * From %s';
- var
- iProj: Integer;
- begin
- SaveGatherProjInfo(AGather.CommonProj, AGather.SpecialProj);
- SaveBills(AGather.Tree);
- SaveBillsGatherCalc(AGather.Tree);
- for iProj := 0 to AGather.CommonProj.Count - 1 do
- begin
- SaveBillsProjCalc(AGather.Tree, iProj);
- ExecuteSql(FConnection, Format(sInsert, [SBills_TransProj, SBills_Proj+IntToStr(iProj+1)]));
- CopyProjRelaGatherData(TGatherProjInfo(AGather.CommonProj.Items[iProj]), iProj);
- end;
- for iProj := 0 to AGather.Tree.SpecialProjCount - 1 do
- SaveBillsSpecialProjCalc(AGather.Tree, iProj+1);
- for iProj := 0 to AGather.SpecialProj.Count - 1 do
- CopyProjRelaGatherData(TGatherProjInfo(AGather.SpecialProj.Items[iProj]), -3);
- end;
- procedure TrpgGatherData.SaveGatherProjInfo(AProjs, ASProjs: TList);
- var
- vGatherInfoData: TrpgGatherProjData;
- begin
- vGatherInfoData := TrpgGatherProjData.Create(FConnection);
- try
- vGatherInfoData.SaveDataTo(AProjs, ASProjs, SGatherProj);
- finally
- vGatherInfoData.Free;
- end;
- end;
- procedure TrpgGatherData.TransposeProjCalc(AProjCount: Integer);
- const
- sInsert = 'Insert Into %s Select * From %s';
- var
- iProj: Integer;
- begin
- for iProj := 0 to AProjCount - 1 do
- ExecuteSql(FConnection, Format(sInsert, [SBills_TransProj, SBills_Proj+IntToStr(iProj+1)]));
- end;
- procedure TrpgGatherData.UpdateDataBase(ASpecialProjTypes: TStrings);
- const
- sUpdateSql = 'Insert Into %s (ID, ProjID, ProjType,' +
- ' OrgQuantity, OrgTotalPrice, OrgTotalPrice_Rc,' +
- ' MisQuantity, MisTotalPrice, MisTotalPrice_Rc,' +
- ' OthQuantity, OthTotalPrice, OthTotalPrice_Rc,' +
- ' Quantity, TotalPrice, TotalPrice_Rc,' +
- ' DgnQuantity1, DgnQuantity2, DgnQuantity,' +
- ' DgnPrice1, DgnPrice2, DgnPrice,' +
- ' DgnPrice1_Rc, DgnPrice2_Rc, DgnPrice_Rc,' +
- ' DealDgnQuantity1, DealDgnQuantity2, DealDgnQuantity,' +
- ' CDgnQuantity1, CDgnQuantity2, CDgnQuantity,' +
- ' FinalDgnQuantity1, FinalDgnQuantity2, FinalDgnQuantity,' +
- ' FinalDgnPrice1, FinalDgnPrice2, FinalDgnPrice,' +
- ' FinalDgnPrice1_Rc, FinalDgnPrice2_Rc, FinalDgnPrice_Rc,' +
- ' AddDealQuantity, AddDealTotalPrice, AddDealTotalPrice_Rc,' +
- ' AddQcQuantity, AddQcTotalPrice, AddQcTotalPrice_Rc,' +
- ' AddGatherQuantity, AddGatherTotalPrice, AddGatherTotalPrice_Rc,' +
- ' CurDealQuantity, CurDealTotalPrice, CurDealTotalPrice_Rc,' +
- ' CurQcQuantity, CurQcTotalPrice, CurQcTotalPrice_Rc,' +
- ' CurGatherQuantity, CurGatherTotalPrice, CurGatherTotalPrice_Rc,' +
- ' PreDealQuantity, PreDealTotalPrice, PreDealTotalPrice_Rc,' +
- ' PreQcQuantity, PreQcTotalPrice, PreQcTotalPrice_Rc,' +
- ' PreGatherQuantity, PreGatherTotalPrice, PreGatherTotalPrice_Rc,' +
- ' EndDealQuantity, EndDealTotalPrice, EndDealTotalPrice_Rc,' +
- ' EndQcQuantity, EndQcTotalPrice, EndQcTotalPrice_Rc,' +
- ' EndGatherQuantity, EndGatherTotalPrice, EndGatherTotalPrice_Rc)' +
- ' Select ID, %d, %d,'+
- ' 0, 0, 0,'+ // Org
- ' 0, 0, 0,'+ // Mis
- ' 0, 0, 0,'+ // Oth
- ' 0, 0, 0,'+ // 台账
- ' 0, 0, '''','+ // DgnQuantity
- ' 0, 0, '''','+ // DgnPrice
- ' 0, 0, '''','+ // DgnPrice_Rc
- ' 0, 0, '''','+ // DealDgnQuantity
- ' 0, 0, '''','+ // CDgnQuantity
- ' 0, 0, '''','+ // FinalDgnQuantity
- ' 0, 0, '''','+ // FinalDgnPrice
- ' 0, 0, '''','+ // FinalDgnPrice_Rc
- ' 0, 0, 0,'+ // AddDeal
- ' 0, 0, 0,'+ // AddQc
- ' 0, 0, 0,'+ // AddGather
- ' 0, 0, 0,'+ // CurDeal
- ' 0, 0, 0,'+ // CurQc
- ' 0, 0, 0,'+ // CurGather
- ' 0, 0, 0,'+ // PreDeal
- ' 0, 0, 0,'+ // PreQc
- ' 0, 0, 0,'+ // PreGather
- ' 0, 0, 0,'+ // EndDeal
- ' 0, 0, 0,'+ // EndQc
- ' 0, 0, 0'+ // EndGather
- ' From r_Bills';
- var
- Updater: TScUpdater;
- iProj, iSpecialProjCount: Integer;
- begin
- iSpecialProjCount := GetCurSpecialProjCount;
- if ASpecialProjTypes.Count > iSpecialProjCount then
- begin
- Updater := TScUpdater.Create;
- try
- Updater.ForceUpdate := True;
- Updater.Open('', FConnection, '', '');
- for iProj := iSpecialProjCount to ASpecialProjTypes.Count - 1 do
- Updater.AddTableDef(SBills_SProj+IntToStr(iProj+1), @tdBills_Calc, Length(tdBills_Calc), False, False);
- Updater.ExcuteUpdate;
- finally
- Updater.Free;
- end;
- for iProj := iSpecialProjCount to ASpecialProjTypes.Count - 1 do
- ExecuteSql(FConnection, Format(sUpdateSql, [SBills_SProj+IntToStr(iProj+1), -3, iProj+1]));
- end;
- end;
- procedure TrpgGatherData.WriteGatherData(AGather: TProjGather);
- begin
- ClearHistoryData;
- CreateDataTables(AGather.Tree.ProjCount, AGather.Tree.SpecialProjCount);
- SaveGatherData(AGather);
- CalcOtherData(AGather.Tree.ProjCount, AGather.Tree.SpecialProjCount);
- //CopyRelaGatherData(AGather);
- // 集中处理TranProj时,最后一个标段数据丢失
- //TransposeProjCalc(AGather.Tree.ProjCount);
- if _IsDebugView then
- CopyFileOrFolder(FGatherFile, GetAppFilePath+'CommonProjGather.dat');
- end;
- procedure TrpgGatherData.CopyRelaGatherData(AGather: TProjGather);
- var
- iProj: Integer;
- begin
- for iProj := 0 to AGather.CommonProj.Count - 1 do
- CopyProjRelaGatherData(TGatherProjInfo(AGather.CommonProj.Items[iProj]), iProj);
- for iProj := 0 to AGather.SpecialProj.Count - 1 do
- CopyProjRelaGatherData(TGatherProjInfo(AGather.SpecialProj.Items[iProj]), -3);
- end;
- function TrpgGatherData.GetCacheProjFile(AProj: TGatherProjInfo): string;
- var
- vProjData: TProjectData;
- begin
- Result := GetTempFileName;
- vProjData := OpenProjectManager.FindProjectData(AProj.ProjectID);
- if not Assigned(vProjData) then
- begin
- vProjData := TProjectData.Create;
- vProjData.SimpleOpen(GetMyProjectsFilePath + AProj.FileName);
- vProjData.SaveTempDataBaseFile(Result);
- vProjData.Free;
- end
- else
- vProjData.SaveTempDataBaseFile(Result);
- end;
- end.
|