UpFileManageUnit.pas 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453
  1. {*******************************************************************************
  2. 单元名称: UpFileManageUnit.pas
  3. 单元说明: 计量支付附件,数据模块。云版和单机版区别很大。
  4. 作者时间: Chenshilong, 2015-01-16
  5. *******************************************************************************}
  6. unit UpFileManageUnit;
  7. interface
  8. uses
  9. Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls,
  10. ExtCtrls, StdCtrls, ShellAPI, ADODB;
  11. type
  12. TUpFileStatus = (ufsNormal, ufsNeedUp, ufsUping, ufsDowning, ufsDownFail); // 控制界面显示:常规、等待上传、上传中、下载中
  13. TStatusChangeEvent = procedure (AStatus: TUpFileStatus) of object;
  14. TUpFiles = class;
  15. TUpFile = class(TObject)
  16. private
  17. FUperName: string;
  18. FUpTime: string;
  19. FWebFile: string;
  20. FMemo: string;
  21. FStatus: TUpFileStatus;
  22. //FIcon: TIcon;
  23. FBillID: Integer;
  24. FID: Integer;
  25. FCategory: string;
  26. FDisplayName: string;
  27. FWebID: Integer;
  28. FUperID: Integer;
  29. FOwner: TUpFiles;
  30. FDownURL: string;
  31. FLocalFile: string;
  32. FOnStatusChange: TStatusChangeEvent;
  33. FOrgFile: string;
  34. FPhase: Integer;
  35. FNo: Integer;
  36. procedure SetWebFile(const Value: string);
  37. procedure SetMemo(const Value: string);
  38. procedure SetStatus(const Value: TUpFileStatus);
  39. procedure SetUperName(const Value: string);
  40. procedure SetUpTime(const Value: string);
  41. procedure SetBillID(const Value: Integer);
  42. procedure SetID(const Value: Integer);
  43. procedure SetCategory(const Value: string);
  44. procedure SetDisplayName(const Value: string);
  45. procedure SetWebID(const Value: Integer);
  46. procedure SetUperID(const Value: Integer);
  47. procedure SetOwner(const Value: TUpFiles);
  48. procedure SetDownURL(const Value: string);
  49. procedure SetLocalFile(const Value: string);
  50. procedure SetOrgFile(const Value: string);
  51. procedure SetPhase(const Value: Integer);
  52. procedure SetNo(const Value: Integer);
  53. function GetExt: string;
  54. public
  55. procedure AssignIcon(APicture: TPicture);
  56. property No: Integer read FNo write SetNo; // 索引号,进入TList的顺序号。
  57. property ID: Integer read FID write SetID; // ID,主键
  58. property WebID: Integer read FWebID write SetWebID; // 服务器端的标段ID
  59. property Phase: Integer read FPhase write SetPhase; // 期号
  60. property BillID: Integer read FBillID write SetBillID; // 用于关联清单
  61. property UperID: Integer read FUperID write SetUperID; // 上传者ID
  62. property UperName: string read FUperName write SetUperName; // 上传者姓名
  63. property UpTime: string read FUpTime write SetUpTime; // 上传时间
  64. property Category: string read FCategory write SetCategory; // 所属分类:清单附件、台帐附件等
  65. property OrgFile: string read FOrgFile write SetOrgFile; // 原始文件:随意放在硬盘某个角落,即将用来上传或用来本地统一管理的文件。(含路径)
  66. property WebFile: string read FWebFile write SetWebFile; // 网络文件:原始文件上传到服务器后,PHP因安全因素重新编码文件名后生成新的文件名,如"20150201.jpg"。(路径+编码文件名)
  67. property LocalFile: string read FLocalFile write SetLocalFile; // 本地文件:原始文件被统一管理后(单机版)或网络文件下载到本地后的文件。(完整路径+本地文件文件名)
  68. property DisplayName: string read FDisplayName write SetDisplayName; // 本地显示给用户看的文件名。
  69. property DownURL: string read FDownURL write SetDownURL; // 下载链接,用于从服务器下载文件到本地
  70. property Memo: string read FMemo write SetMemo; // 上传者的备注
  71. property Status: TUpFileStatus read FStatus write SetStatus; // 状态信息,用于控制界面的显示
  72. property Ext: string read GetExt;
  73. property Owner: TUpFiles read FOwner write SetOwner;
  74. property OnStatusChange: TStatusChangeEvent read FOnStatusChange write FOnStatusChange;
  75. end;
  76. TUpFiles = class(TObject)
  77. private
  78. FList: TList;
  79. FProjectData: TObject;
  80. FPath: string;
  81. // -------------业务扩展,单机版本地也要存储管理附件,以下这部分适用于单机版。
  82. // FLocalCon: TADOConnection;
  83. // FLocalQue: TADOQuery;
  84. // FLocalQue2: TADOQuery;
  85. function GetItem(Index: Integer): TUpFile;
  86. procedure Clear;
  87. function GetCount: Integer;
  88. procedure SetProjectData(const Value: TObject);
  89. public
  90. constructor Create;
  91. destructor Destroy; override;
  92. function Add(AUpFile: TUpFile): Integer; overload; // 返回索引号
  93. function Add(ABillID: Integer): TUpFile; overload;
  94. function Find(AID: Integer): TUpFile;
  95. procedure Delete(AIndex: Integer); overload;
  96. procedure Delete(AUpFile: TUpFile); overload;
  97. procedure Exchange(AUpFile1, AUpFile2: TUpFile);
  98. procedure LoadDatas;
  99. // function LocalSaveToDB(AUpFile: TUpFile): Integer; // 返回ID
  100. property Item[Index: Integer]: TUpFile read GetItem; default;
  101. property Count: Integer read GetCount;
  102. property ProjectData: TObject read FProjectData write SetProjectData;
  103. property Path: string read FPath;
  104. property List: TList read FList;
  105. end;
  106. implementation
  107. uses ProjectData, Forms, PHPWebDm, CslJson, ConstUnit, UtilMethods,
  108. StdConvs, superobject;
  109. { TUpFile }
  110. procedure TUpFile.SetWebFile(const Value: string);
  111. begin
  112. FWebFile := Value;
  113. end;
  114. procedure TUpFile.SetBillID(const Value: Integer);
  115. begin
  116. FBillID := Value;
  117. end;
  118. procedure TUpFile.SetMemo(const Value: string);
  119. begin
  120. FMemo := Value;
  121. end;
  122. procedure TUpFile.SetStatus(const Value: TUpFileStatus);
  123. begin
  124. FStatus := Value;
  125. if Assigned(FOnStatusChange) then
  126. FOnStatusChange(FStatus);
  127. end;
  128. procedure TUpFile.SetUperName(const Value: string);
  129. begin
  130. FUperName := Value;
  131. end;
  132. procedure TUpFile.SetUpTime(const Value: string);
  133. begin
  134. FUpTime := Value;
  135. end;
  136. { TUpFileList }
  137. function TUpFiles.Add(AUpFile: TUpFile): Integer;
  138. begin
  139. Result := FList.Add(AUpFile);
  140. end;
  141. procedure TUpFiles.Clear;
  142. var
  143. i: Integer;
  144. begin
  145. for i := 0 to FList.Count - 1 do
  146. TUpFile(FList[i]).Free;
  147. FList.Clear;
  148. end;
  149. constructor TUpFiles.Create;
  150. begin
  151. FList := TList.Create;
  152. // if not G_IsCloud then
  153. // begin
  154. // FLocalCon := TADOConnection.Create(nil);
  155. // FLocalCon.LoginPrompt := False;
  156. //
  157. // FLocalQue := TADOQuery.Create(nil);
  158. // FLocalQue.Connection := FLocalCon;
  159. //
  160. // FLocalQue2 := TADOQuery.Create(nil);
  161. // FLocalQue2.Connection := FLocalCon;
  162. // end;
  163. end;
  164. procedure TUpFiles.Delete(AUpFile: TUpFile);
  165. begin
  166. if AUpFile = nil then Exit;
  167. FList.Remove(AUpFile);
  168. end;
  169. procedure TUpFiles.Delete(AIndex: Integer);
  170. begin
  171. TUpFile(FList[AIndex]).Free;
  172. FList.Delete(AIndex);
  173. end;
  174. destructor TUpFiles.Destroy;
  175. begin
  176. Clear;
  177. FList.Free;
  178. // if not G_IsCloud then
  179. // begin
  180. // FLocalQue.Close;
  181. // FLocalQue.Free;
  182. // FLocalQue2.Close;
  183. // FLocalQue2.Free;
  184. // FLocalCon.Close;
  185. // FLocalCon.Free;
  186. // end;
  187. inherited;
  188. end;
  189. procedure TUpFiles.Exchange(AUpFile1, AUpFile2: TUpFile);
  190. var
  191. idx1, idx2: Integer;
  192. begin
  193. idx1 := FList.IndexOf(AUpFile1);
  194. idx2 := FList.IndexOf(AUpFile2);
  195. if idx1 < 0 then Exit;
  196. if idx2 < 0 then Exit;
  197. FList.Exchange(idx1, idx2);
  198. end;
  199. function TUpFiles.GetCount: Integer;
  200. begin
  201. Result := FList.Count;
  202. end;
  203. function TUpFiles.GetItem(Index: Integer): TUpFile;
  204. begin
  205. Result := TUpFile(FList[Index]);
  206. end;
  207. procedure TUpFiles.LoadDatas;
  208. procedure LoadData(AFile: ISuperObject);
  209. var
  210. vFile: TUpFile;
  211. begin
  212. vFile := Add(AFile.I['itemid']);
  213. vFile.DownURL := AFile.S['downurl'];
  214. vFile.DisplayName := AFile.S['filename'];
  215. vFile.LocalFile := FPath + vFile.DisplayName;
  216. vFile.UperName := AFile.S['ownerName'];
  217. vFile.Category := AFile.S['Category'];
  218. vFile.Memo := AFile.S['Memo'];
  219. vFile.UperID := AFile.I['owmerid'];
  220. vFile.UpTime := AFile.S['uptime'];
  221. vFile.ID := AFile.I['fileid'];
  222. vFile.Phase := AFile.I['phase'];
  223. vFile.WebID := TProjectData(FProjectData).WebID;
  224. vFile.Status := ufsNormal;
  225. end;
  226. var
  227. i, iCount: Integer;
  228. sUrl, sInfo: string;
  229. vJ: ISuperObject;
  230. begin
  231. if not G_IsCloud then Exit;
  232. sUrl := Format('%stender/attachment/%d/list', [PHPWeb.MeasureURL, TProjectData(FProjectData).WebID]);
  233. if PHPWeb.UrlGet(sUrl, nil, sInfo) = 1 then
  234. begin
  235. vJ := SO(sInfo);
  236. try
  237. if not Assigned(vJ.AsArray) then Exit;
  238. iCount := vJ.AsArray.Length;
  239. for i := 0 to iCount - 1 do
  240. LoadData(vJ.AsArray.O[i]);
  241. finally
  242. vJ := nil;
  243. end;
  244. end;
  245. end;
  246. procedure TUpFile.SetID(const Value: Integer);
  247. begin
  248. FID := Value;
  249. end;
  250. procedure TUpFile.SetCategory(const Value: string);
  251. begin
  252. FCategory := Value;
  253. end;
  254. procedure TUpFile.SetDisplayName(const Value: string);
  255. begin
  256. FDisplayName := Value;
  257. end;
  258. procedure TUpFile.SetWebID(const Value: Integer);
  259. begin
  260. FWebID := Value;
  261. end;
  262. procedure TUpFile.SetUperID(const Value: Integer);
  263. begin
  264. FUperID := Value;
  265. end;
  266. procedure TUpFiles.SetProjectData(const Value: TObject);
  267. var sLAFM, sModelFile: string;
  268. begin
  269. FProjectData := Value;
  270. if G_IsCloud then
  271. begin
  272. FPath := PHPWeb.WebPath + 'Projects\' + IntToStr(TProjectData(FProjectData).WebID) + '\Attachment\';
  273. if not DirectoryExists(FPath) then
  274. ForceDirectories(FPath);
  275. end;
  276. // else
  277. // begin
  278. // FPath := ExtractFilePath(Application.ExeName) + '我的项目\Attachment\' + ExtractFileName(TProjectData(FProjectData).FileName) + '\';
  279. // if not DirectoryExists(FPath) then
  280. // ForceDirectories(FPath);
  281. //
  282. // sLAFM := FPath + 'LocalAttachmentFileManager.dat';
  283. // sModelFile := ExtractFilePath(Application.ExeName) + 'Data\LocalAttachmentFileManager.dat';
  284. // if not FileExists(sLAFM) then
  285. // if not CopyFile(PChar(sModelFile), PChar(sLAFM), True) then
  286. // begin
  287. // Application.MessageBox(PChar('本地附件管理库模板文件“' + sModelFile + '”丢失或被占用无法复制。'), '警告', MB_OK + MB_ICONWARNING);
  288. // Exit;
  289. // end;
  290. //
  291. // FLocalCon.ConnectionString := Format('Provider=Microsoft.Jet.OLEDB.4.0;Data Source=%s;Persist Security Info=False;', [sLAFM]);
  292. // end;
  293. end;
  294. procedure TUpFile.SetOwner(const Value: TUpFiles);
  295. begin
  296. FOwner := Value;
  297. end;
  298. procedure TUpFile.SetDownURL(const Value: string);
  299. begin
  300. FDownURL := Value;
  301. end;
  302. procedure TUpFile.SetLocalFile(const Value: string);
  303. begin
  304. FLocalFile := Value;
  305. end;
  306. function TUpFiles.Add(ABillID: Integer): TUpFile;
  307. var vFile: TUpFile;
  308. begin
  309. vFile := TUpFile.Create;
  310. vFile.No := Add(vFile);
  311. vFile.Owner := Self;
  312. vFile.BillID := ABillID;
  313. Result := vFile;
  314. end;
  315. procedure TUpFile.SetOrgFile(const Value: string);
  316. begin
  317. FOrgFile := Value;
  318. end;
  319. //function TUpFiles.LocalSaveToDB(AUpFile: TUpFile): Integer;
  320. //begin
  321. // try
  322. // FLocalQue2.Close;
  323. // FLocalQue2.SQL.Text := 'Select * from AttachmentFile where 1 < 0';
  324. // FLocalQue2.Open;
  325. // FLocalQue2.Append;
  326. // FLocalQue2.FieldByName('BillID').AsInteger := AUpFile.BillID;
  327. // FLocalQue2.FieldByName('Phase').AsInteger := AUpFile.Phase;
  328. // FLocalQue2.FieldByName('FileName').AsString := AUpFile.DisplayName;
  329. // FLocalQue2.Post;
  330. // Result := FLocalQue2.FieldByName('ID').AsInteger;
  331. // FLocalQue2.Close;
  332. // except
  333. // Result := -1;
  334. // Application.MessageBox('本地保存失败,请重试!', '警告', MB_OK + MB_ICONWARNING);
  335. // end;
  336. //end;
  337. procedure TUpFile.SetPhase(const Value: Integer);
  338. begin
  339. FPhase := Value;
  340. end;
  341. procedure TUpFile.SetNo(const Value: Integer);
  342. begin
  343. FNo := Value;
  344. end;
  345. function TUpFile.GetExt: string;
  346. begin
  347. Result := ExtractFileExt(DisplayName);
  348. end;
  349. function TUpFiles.Find(AID: Integer): TUpFile;
  350. var i: Integer;
  351. vUpFile: TUpFile;
  352. begin
  353. vUpFile := nil;
  354. for i := 0 to FList.Count - 1 do
  355. begin
  356. vUpFile := TUpFile(FList[i]);
  357. if vUpFile.ID = AID then
  358. begin
  359. Result := vUpFile;
  360. Break;
  361. end;
  362. end;
  363. end;
  364. procedure TUpFile.AssignIcon(APicture: TPicture);
  365. var
  366. vIcon: TIcon;
  367. sExt: string;
  368. sInfo: SHFILEINFO;
  369. begin
  370. // 系统图标
  371. sExt := ExtractFileExt(FLocalFile);
  372. if sExt = '' then Exit;
  373. FillChar(sInfo, SizeOf(sInfo), 0);
  374. SHGetFileInfo(PChar(sExt), FILE_ATTRIBUTE_NORMAL, sInfo, SizeOf(sInfo),
  375. SHGFI_USEFILEATTRIBUTES or SHGFI_ICON or SHGFI_LARGEICON);
  376. if sInfo.hIcon > 0 then
  377. begin
  378. vIcon := TIcon.Create;
  379. try
  380. vIcon.Handle := sInfo.hIcon;
  381. APicture.Graphic := vIcon;
  382. finally
  383. vIcon.Free;
  384. end;
  385. end;
  386. end;
  387. end.