TFileInfoDal.cs 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  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 System.IO;
  9. namespace DllEapDal
  10. {
  11. public class TFileInfoDal
  12. {
  13. private IDatabase CurrDb = null;
  14. private string commonFilter = null;
  15. public TFileInfoDal(IDatabase db)
  16. {
  17. CurrDb = db;
  18. }
  19. public TFileInfoDal(IDatabase db, string userCode)
  20. {
  21. CurrDb = db;
  22. var smDal = new StaffMachineDal(CurrDb);
  23. string errorinfo = string.Empty;
  24. var idFilter = smDal.GetFilter(userCode, ref errorinfo);
  25. commonFilter = idFilter;
  26. }
  27. public TFileInfo IUTFileInfo(TFileInfo mst, string usercode, ref string errorinfo)
  28. {
  29. try
  30. {
  31. int result = 0;
  32. int id = mst.ID;
  33. if (mst.EntityStatusID == 1)
  34. {
  35. mst.RecCode = usercode;
  36. mst.RecTime = DateTime.Now;
  37. mst.ModCode = usercode;
  38. mst.ModTime = DateTime.Now;
  39. result = CurrDb.InsertFor(mst, usercode);
  40. if (result < 0)
  41. {
  42. return null;
  43. }
  44. object objid = CurrDb.FindObject("select @@IDENTITY");
  45. if (objid.ToString() == "")
  46. {
  47. return null;
  48. }
  49. id = int.Parse(objid.ToString());
  50. }
  51. else
  52. {
  53. mst.ModCode = usercode;
  54. mst.ModTime = DateTime.Now;
  55. result = CurrDb.UpdateFor(mst, usercode);
  56. if (result < 0)
  57. {
  58. return null;
  59. }
  60. }
  61. mst = CurrDb.FindEntityFor<TFileInfo>(id);
  62. return mst;
  63. }
  64. catch (Exception e)
  65. {
  66. errorinfo = e.Message;
  67. return null;
  68. }
  69. }
  70. public TFileInfo AddTFileInfo(byte[] filedatas,string filedir,string filename,int macid,string remark, string usercode, ref string errorinfo)
  71. {
  72. try
  73. {
  74. string md5 = FileHelper.GetMD5HashFromFile(filedatas, ref errorinfo);
  75. if (md5 == "")
  76. return null;
  77. string condition = $" and a.Md5Val='{md5}'";
  78. List<TFileInfo> list = CurrDb.FindListForCondition< TFileInfo>(condition, ref errorinfo).ToList();
  79. if (list.Count > 0)
  80. return list[0];
  81. //没有此文件,则新增
  82. TFileInfo entity = new TFileInfo();
  83. entity.FName = filename;
  84. entity.FileSize = filedatas.Length;
  85. entity.Md5Val = md5;
  86. entity.UploadMacID = macid;
  87. entity.remark = remark;
  88. entity = IUTFileInfo(entity, usercode, ref errorinfo);
  89. if (entity == null)
  90. return null;
  91. string dir = ComputerPath(entity);//文件相对路径
  92. //考虑到linux系统,这里只保存目录
  93. entity.FilePath = $"{dir}";
  94. entity.ModifyEntity();
  95. entity = IUTFileInfo(entity, usercode, ref errorinfo);
  96. if (entity == null)
  97. return null;
  98. //添加实体文件
  99. string tempfiledir = $"{filedir}{Path.DirectorySeparatorChar}{dir}";
  100. int result = UnityHelper.WriteFile(tempfiledir, entity.ID.ToString(), filedatas, ref errorinfo);
  101. if (result <= 0)
  102. return null;
  103. return entity;
  104. }
  105. catch (Exception e)
  106. {
  107. errorinfo = e.Message;
  108. return null;
  109. }
  110. }
  111. private string ComputerPath(TFileInfo entity)
  112. {
  113. int dir = entity.ID / 1000;
  114. return $"{dir}";
  115. }
  116. }
  117. }