ShareFileDal.cs 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  1. using Cksoft.Data;
  2. using Cksoft.Unity;
  3. using DllEapEntity;
  4. using System;
  5. using System.Collections.Generic;
  6. using System.Linq;
  7. using DllEapEntity.Dtos;
  8. using DllHsms;
  9. namespace DllEapDal
  10. {
  11. public class ShareFileDal
  12. {
  13. private IDatabase CurrDb = null;
  14. public ShareFileDal(IDatabase db)
  15. {
  16. CurrDb = db;
  17. }
  18. public ShareFile IShareFile(byte[] filedatas,ref string errorinfo)
  19. {
  20. try
  21. {
  22. if(filedatas==null)
  23. {
  24. filedatas = new byte[10];
  25. }
  26. int result = 0;
  27. ShareFile entity = new ShareFile();
  28. entity.FileLen = filedatas.Length;
  29. result = CurrDb.InsertFor<ShareFile>(entity, "");
  30. if(result<0)
  31. {
  32. errorinfo = "插入记录发生未知错误。";
  33. return null;
  34. }
  35. object objid = CurrDb.FindObject("select @@IDENTITY");
  36. if (objid.ToString() == "")
  37. {
  38. return null;
  39. }
  40. int id = int.Parse(objid.ToString());
  41. string filedir = AppConfigurtaionServices.Configuration["ShareFileDir"];
  42. entity = CurrDb.FindEntityFor<ShareFile>(id);
  43. entity.FilePath = $"{filedir}\\{id}";
  44. CurrDb.UpdateFor<ShareFile>(entity, "");
  45. //写入文件
  46. result = UnityHelper.WriteFile(filedir, id.ToString(), filedatas, ref errorinfo);
  47. if (result <= 0)
  48. return null;
  49. return entity;
  50. }
  51. catch (Exception e)
  52. {
  53. errorinfo = e.Message;
  54. return null;
  55. }
  56. }
  57. public ShareFile IShareFile(OrderBlock sendentity, ref string errorinfo)
  58. {
  59. try
  60. {
  61. List<OrderData> datas = sendentity.Datalists.Where(t => t.ParentID == 0).OrderBy(t => t.FNum).ToList();
  62. int parentid = datas[0].ID;//获取L的ID值
  63. datas = sendentity.Datalists.Where(t => t.ParentID == parentid).OrderBy(t => t.FNum).ToList();
  64. string programname = datas[0].FContent;
  65. byte[] filedatas = datas[1].OrgDatas;
  66. datas[1].OrgDatas = null;//清空原始数据
  67. ShareFile entity = IShareFile(filedatas, ref errorinfo);
  68. if (entity == null)
  69. return null;
  70. datas[1].FContent = entity.FilePath;
  71. return entity;
  72. }
  73. catch (Exception e)
  74. {
  75. errorinfo = e.Message;
  76. return null;
  77. }
  78. }
  79. /// <summary>
  80. /// 根据文件路径,将文件复制到共享目录
  81. /// </summary>
  82. /// <param name="filepath"></param>
  83. /// <param name="errorinfo"></param>
  84. /// <returns></returns>
  85. public ShareFile MadeShareFile(string filepath, ref string errorinfo)
  86. {
  87. try
  88. {
  89. byte[] filedatas = UnityHelper.ReadFile(filepath, ref errorinfo);
  90. if (filedatas == null)
  91. return null;
  92. ShareFile entity = IShareFile(filedatas, ref errorinfo);
  93. if (entity == null)
  94. return null;
  95. return entity;
  96. }
  97. catch (Exception e)
  98. {
  99. errorinfo = e.Message;
  100. return null;
  101. }
  102. }
  103. public ShareFile MadeShareFileForDad3350(string filepath,string orgprogram, ref string errorinfo)
  104. {
  105. try
  106. {
  107. byte[] filedatas = UnityHelper.ReadFile(filepath, ref errorinfo);
  108. if (filedatas == null)
  109. return null;
  110. //转换程序名称
  111. filedatas = DiodesProgramFileHelper.ReplaceProgramName(filedatas, orgprogram, ref errorinfo);
  112. if (filedatas == null)
  113. return null;
  114. ShareFile entity = IShareFile(filedatas, ref errorinfo);
  115. if (entity == null)
  116. return null;
  117. return entity;
  118. }
  119. catch (Exception e)
  120. {
  121. errorinfo = e.Message;
  122. return null;
  123. }
  124. }
  125. }
  126. }