TEntityDal.cs 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173
  1. using Cksoft.Data;
  2. using DllEapEntity;
  3. using System;
  4. using System.Collections.Generic;
  5. using System.Text;
  6. using System.Linq;
  7. using DllHsms;
  8. using Cksoft.Unity;
  9. namespace DllStatusShowDal
  10. {
  11. public class TEntityDal
  12. {
  13. private IDatabase CurrDb = null;
  14. public TEntityDal(IDatabase db)
  15. {
  16. CurrDb = db;
  17. }
  18. public TEntity IUTEntity(TEntity mst, string usercode, ref string errorinfo)
  19. {
  20. try
  21. {
  22. int result = 0;
  23. int id = mst.ID;
  24. if (mst.EntityStatusID == 1)
  25. {
  26. //mst.reccode = usercode;
  27. //mst.RecTime = DateTime.Now;
  28. //mst.ModCode = usercode;
  29. //mst.ModTime = DateTime.Now;
  30. //string fcode = GetTLcdCode(ref errorinfo);
  31. //if (fcode == "")
  32. // return null;
  33. //mst.FCode = fcode;
  34. result = CurrDb.InsertFor(mst, usercode);
  35. if (result < 0)
  36. {
  37. return null;
  38. }
  39. object objid = CurrDb.FindObject("select @@IDENTITY");
  40. if (objid.ToString() == "")
  41. {
  42. return null;
  43. }
  44. id = int.Parse(objid.ToString());
  45. }
  46. else
  47. {
  48. //mst.ModCode = usercode;
  49. //mst.ModTime = DateTime.Now;
  50. result = CurrDb.UpdateFor(mst, usercode);
  51. if (result < 0)
  52. {
  53. return null;
  54. }
  55. }
  56. mst = CurrDb.FindEntityFor<TEntity>(id);
  57. return mst;
  58. }
  59. catch (Exception e)
  60. {
  61. errorinfo = e.Message;
  62. return null;
  63. }
  64. }
  65. private string GetTLcdCode(ref string errorinfo)
  66. {
  67. try
  68. {
  69. StringBuilder sqlstr = new StringBuilder(100);
  70. string code = "L";
  71. sqlstr.AppendFormat("SELECT max(fcode) FROM TLcd where fcode like '{0}%'", code);
  72. object obj = CurrDb.FindObject(sqlstr.ToString());
  73. if (obj.ToString() == "")
  74. return code + "00001";
  75. int fnum = int.Parse(obj.ToString().Substring(1, 5));
  76. fnum++;
  77. return code + fnum.ToString().PadLeft(5, '0');
  78. }
  79. catch (Exception ex)
  80. {
  81. errorinfo = ex.Message.ToString();
  82. return "";
  83. }
  84. }
  85. public TEntity DelTEntity(List<TEntity> mst, string usercode, ref string errorinfo)
  86. {
  87. try
  88. {
  89. int result = CurrDb.DeleteForEntity(mst);
  90. if (result < 0)
  91. return null;
  92. return mst[0];
  93. }
  94. catch (Exception e)
  95. {
  96. errorinfo = e.Message;
  97. return null;
  98. }
  99. }
  100. public int SynchronousMac(string usercode, ref string errorinfo)
  101. {
  102. try
  103. {
  104. return SynchronousMacFor(usercode,ref errorinfo);
  105. }
  106. catch(Exception ex)
  107. {
  108. errorinfo = ex.Message.ToString();
  109. return -1;
  110. }
  111. }
  112. private int SynchronousMacFor(string usercode,ref string errorinfo)
  113. {
  114. try
  115. {
  116. List<Machine> macs = CurrDb.FindListForCondition<Machine>("", ref errorinfo).ToList();
  117. if (macs == null)
  118. return -1;
  119. List<TEntity> entitys = CurrDb.FindListForCondition<TEntity>("", ref errorinfo).ToList();
  120. foreach(var item in macs)
  121. {
  122. List<TEntity> templist = entitys.Where(t => t.FType == EntityType.Machine && t.OrgID == item.ID).ToList();
  123. if(templist.Count<=0)
  124. {
  125. //查找代码是否存在
  126. templist = entitys.Where(t => t.FType == EntityType.Machine && t.OrgID <= 0 && t.FCode == item.FCode).ToList();
  127. if(templist.Count>0)
  128. {
  129. templist[0].FName = item.FName;
  130. templist[0].OrgID = item.ID;
  131. templist[0].ModifyEntity<TEntity>();
  132. }
  133. else
  134. {
  135. TEntity tempentity = new TEntity();
  136. tempentity.FType = EntityType.Machine;
  137. tempentity.FCode = item.FCode;
  138. tempentity.FName = item.FName;
  139. tempentity.OrgID = item.ID;
  140. tempentity.AddEntity(entitys);
  141. }
  142. }
  143. else
  144. {
  145. templist[0].FCode = item.FCode;
  146. templist[0].FName = item.FName;
  147. templist[0].ModifyEntity<TEntity>();
  148. }
  149. }
  150. List<TEntity> ttrows = entitys.Where(t => t.EntityStatusID == 1).ToList();
  151. int result = CurrDb.InsertFor<TEntity>(ttrows, usercode);
  152. if (result < 0)
  153. return -1;
  154. ttrows = entitys.Where(t => t.EntityStatusID == 2).ToList();
  155. result = CurrDb.UpdateFor<TEntity>(ttrows, usercode);
  156. if (result < 0)
  157. return -1;
  158. return 1;
  159. }
  160. catch (Exception ex)
  161. {
  162. errorinfo = ex.Message.ToString();
  163. return -1;
  164. }
  165. }
  166. }
  167. }