BusinessFileDal.cs 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224
  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. namespace DllEapDal
  9. {
  10. public class BusinessFileDal
  11. {
  12. private IDatabase CurrDb = null;
  13. private string commonFilter = null;
  14. public BusinessFileDal(IDatabase db)
  15. {
  16. CurrDb = db;
  17. }
  18. public BusinessFileDal(IDatabase db, string userCode)
  19. {
  20. CurrDb = db;
  21. var smDal = new StaffMachineDal(CurrDb);
  22. string errorinfo = string.Empty;
  23. var idFilter = smDal.GetFilter(userCode, ref errorinfo);
  24. commonFilter = idFilter;
  25. }
  26. public BusinessFile IUBusinessFile(BusinessFile mst, string usercode, ref string errorinfo)
  27. {
  28. try
  29. {
  30. int result = 0;
  31. int id = mst.ID;
  32. if (mst.EntityStatusID == 1)
  33. {
  34. mst.RecCode = usercode;
  35. mst.RecTime = DateTime.Now;
  36. mst.ModCode = usercode;
  37. mst.ModTime = DateTime.Now;
  38. result = CurrDb.InsertFor(mst, usercode);
  39. if (result < 0)
  40. {
  41. return null;
  42. }
  43. object objid = CurrDb.FindObject("select @@IDENTITY");
  44. if (objid.ToString() == "")
  45. {
  46. return null;
  47. }
  48. id = int.Parse(objid.ToString());
  49. }
  50. else
  51. {
  52. mst.ModCode = usercode;
  53. mst.ModTime = DateTime.Now;
  54. result = CurrDb.UpdateFor(mst, usercode);
  55. if (result < 0)
  56. {
  57. return null;
  58. }
  59. }
  60. mst = CurrDb.FindEntityFor<BusinessFile>(id);
  61. return mst;
  62. }
  63. catch (Exception e)
  64. {
  65. errorinfo = e.Message;
  66. return null;
  67. }
  68. }
  69. /// <summary>
  70. /// 根据MD5添加程序文件,如果MD5不存在则不添加文件关系
  71. /// </summary>
  72. /// <param name="md5"></param>
  73. /// <param name="filename"></param>
  74. /// <param name="maccode"></param>
  75. /// <param name="remark"></param>
  76. /// <param name="usercode"></param>
  77. /// <param name="errorinfo"></param>
  78. /// <returns></returns>
  79. public BusinessFile UpdownFileForMd5(string md5, string filename, string maccode, string remark, string usercode, ref string errorinfo)
  80. {
  81. try
  82. {
  83. string filedir = AppConfigurtaionServices.Configuration["ProgramDir"];
  84. Machine mac = CurrDb.FindListForCondition<Machine>($" and a.fcode='{maccode}'", ref errorinfo).FirstOrDefault();
  85. if(mac==null)
  86. {
  87. errorinfo = $"未找到编号为={maccode}的机台信息。";
  88. return null;
  89. }
  90. TFileInfoDal dal = new TFileInfoDal(CurrDb);
  91. TFileInfo fileinfo = CurrDb.FindListForCondition<TFileInfo>($" and a.Md5Val='{md5}'", ref errorinfo).FirstOrDefault();
  92. if (fileinfo == null)
  93. {
  94. errorinfo = $"md5={md5}文件不存在。";
  95. return null;
  96. }
  97. BusinessFile busifile = UpdownFileFor(fileinfo, filename, 1, mac.ID, usercode, ref errorinfo);
  98. if (busifile == null)
  99. return null;
  100. //处理机台程序
  101. busifile = UpdownFileFor(fileinfo, filename, 2, mac.MModeID, usercode, ref errorinfo);
  102. if (busifile == null)
  103. return null;
  104. return busifile;
  105. }
  106. catch (Exception e)
  107. {
  108. errorinfo = e.Message;
  109. return null;
  110. }
  111. }
  112. /// <summary>
  113. /// 供文件方式上传程序使用
  114. /// </summary>
  115. /// <param name="filedatas"></param>
  116. /// <param name="filename"></param>
  117. /// <param name="maccode"></param>
  118. /// <param name="errorinfo"></param>
  119. /// <returns></returns>
  120. public BusinessFile UpdownFileForFile(byte[] filedatas, string filename,string maccode, ref string errorinfo)
  121. {
  122. try
  123. {
  124. string filedir = AppConfigurtaionServices.Configuration["ProgramDir"];
  125. Machine mac = CurrDb.FindListForCondition<Machine>($" and a.fcode='{maccode}'", ref errorinfo).FirstOrDefault();
  126. if (mac == null)
  127. {
  128. errorinfo = $"未找到编号为={maccode}的机台信息。";
  129. return null;
  130. }
  131. return UpdownFile(filedatas, filedir, filename, mac, "文件方式上传程序", "", ref errorinfo);
  132. }
  133. catch (Exception e)
  134. {
  135. errorinfo = e.Message;
  136. return null;
  137. }
  138. }
  139. /// <summary>
  140. /// 程序上次
  141. /// </summary>
  142. /// <param name="filedatas">程序数据</param>
  143. /// <param name="filedir">程序存放目录</param>
  144. /// <param name="filename">程序名称</param>
  145. /// <param name="mac">机台信息</param>
  146. /// <param name="remark">备注</param>
  147. /// <param name="usercode">操作员代码</param>
  148. /// <param name="errorinfo"></param>
  149. /// <returns></returns>
  150. public BusinessFile UpdownFile(byte[] filedatas, string filedir, string filename, Machine mac, string remark, string usercode, ref string errorinfo)
  151. {
  152. try
  153. {
  154. TFileInfoDal dal = new TFileInfoDal(CurrDb);
  155. TFileInfo fileinfo = dal.AddTFileInfo(filedatas, filedir, filename, mac.ID, remark, usercode, ref errorinfo);
  156. if (fileinfo == null)
  157. return null;
  158. BusinessFile busifile = UpdownFileFor(fileinfo, filename, 1, mac.ID, usercode, ref errorinfo);
  159. if (busifile == null)
  160. return null;
  161. //处理机台程序
  162. busifile = UpdownFileFor(fileinfo, filename, 2, mac.MModeID, usercode, ref errorinfo);
  163. if (busifile == null)
  164. return null;
  165. return busifile;
  166. }
  167. catch (Exception e)
  168. {
  169. errorinfo = e.Message;
  170. return null;
  171. }
  172. }
  173. private BusinessFile UpdownFileFor(TFileInfo fileinfo, string filename, int orgtypeid, int orgid, string usercode, ref string errorinfo)
  174. {
  175. try
  176. {
  177. //判断机台是否有此文件,如果没有则添加
  178. BusinessFile busifile = null;
  179. string condition = $" and a.FName='{filename}' and a.OrgTypeID={orgtypeid} and a.OrgID={orgid}";
  180. List<BusinessFile> busifiles = CurrDb.FindListForCondition<BusinessFile>(condition, ref errorinfo).ToList();
  181. if (busifiles.Count <= 0)
  182. {
  183. //说明机台没有此文件名称
  184. busifile = new BusinessFile();
  185. busifile.OrgTypeID = orgtypeid;
  186. busifile.OrgID = orgid;
  187. busifile.FName = filename;
  188. busifile = IUBusinessFile(busifile, usercode, ref errorinfo);
  189. if (busifile == null)
  190. return null;
  191. }
  192. else
  193. {
  194. busifile = busifiles[0];
  195. }
  196. //直接添加文件关系
  197. BusinessFileRelationDal reladal = new BusinessFileRelationDal(CurrDb);
  198. BusinessFileRelation rela = reladal.IUBusinessFileRelation(busifile.ID, fileinfo.ID, usercode, ref errorinfo);
  199. if (rela == null)
  200. return null;
  201. busifile.Version = rela.Version;
  202. return busifile;
  203. }
  204. catch (Exception e)
  205. {
  206. errorinfo = e.Message;
  207. return null;
  208. }
  209. }
  210. private string ComputerPath(TFileInfo entity)
  211. {
  212. int dir = entity.ID / 1000;
  213. return $"{dir}";
  214. }
  215. }
  216. }