MtbacodeDal.cs 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153
  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 System.Text;
  8. namespace DllEapDal.OFILM
  9. {
  10. public class MtbacodeDal
  11. {
  12. private IDatabase CurrDb = null;
  13. public MtbacodeDal(IDatabase db)
  14. {
  15. CurrDb = db;
  16. }
  17. public IEnumerable<Mtbacode> Get(int start, int length, string order, string sort, string filter, string errorinfo)
  18. {
  19. var pros = CurrDb.FindListForCondition<Mtbacode>($" {filter} order by {sort} {order} limit {start - 1},{length}", ref errorinfo);
  20. return pros;
  21. }
  22. public int GetCount(string filter)
  23. {
  24. string errorinfo = string.Empty;
  25. var entities = CurrDb.FindListForCondition<Mtbacode>(filter, ref errorinfo);
  26. if (entities != null)
  27. {
  28. return entities.Count();
  29. }
  30. return 0;
  31. }
  32. public Mtbacode Get(int id)
  33. {
  34. var pro = CurrDb.FindEntityFor<Mtbacode>(id);
  35. return pro;
  36. }
  37. /// <summary>
  38. /// 添加Mtbacode并返回Id
  39. /// </summary>
  40. /// <param name="role"></param>
  41. /// <param name="userCode"></param>
  42. /// <returns></returns>
  43. public int Add(Mtbacode pro, string userCode, ref string errorinfo)
  44. {
  45. //var entities = CurrDb.FindListForCondition<Mtbacode>($" and a.park='{pro.Park}' and a.MachineType='{pro.MachineType}' and a.workshopSection='{pro.WorkshopSection}' and a.assignmentContent='{pro.AssignmentContent}' ", ref errorinfo);
  46. //if (entities != null && entities.Count() > 0)
  47. //{
  48. // errorinfo = "已存在相同园区,机种,工段,作业内容,请确认";
  49. // return -1;
  50. //}
  51. CurrDb.InsertFor(pro, userCode);
  52. var sql = "select @@identity;";
  53. var id = Convert.ToInt32(CurrDb.FindList<string>(sql).FirstOrDefault() ?? "-1");
  54. return id;
  55. }
  56. public int Update(Mtbacode role, string userCode, ref string errorinfo)
  57. {
  58. //var entities = CurrDb.FindListForCondition<WorkingProcedure>($" and a.park='{role.Park}' and a.MachineType='{role.MachineType}' and a.workshopSection='{role.WorkshopSection}' and a.assignmentContent='{role.AssignmentContent}' " +
  59. // $"and a.ID<>{role.ID}", ref errorinfo);
  60. //if (entities != null && entities.Count() > 0)
  61. //{
  62. // errorinfo = "已存在相同园区,机种,工段,作业内容,请确认";
  63. // return -1;
  64. //}
  65. if (CurrDb.UpdateFor(role, userCode) < 0)
  66. {
  67. return -1;
  68. }
  69. return role.ID;
  70. }
  71. public int Delete(int id, ref string msg)
  72. {
  73. if (CurrDb.DeleteFor<Mtbacode>(id) < 0)
  74. {
  75. msg = "删除失败";
  76. return -1;
  77. }
  78. msg = string.Empty;
  79. return 1;
  80. }
  81. public int SetControlByTrans(IEnumerable<int> ids, string fileld, string value, string usercode, ref string errorinfo)
  82. {
  83. var idFilter = $" and id in ({string.Join(",", ids.Select(c => c))})";
  84. var sql = $"Update Mtbacode set {fileld}={value},ModCode='{usercode}',ModTime='{DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss")}' where 1=1 {idFilter}";
  85. if (CurrDb.ExecuteBySql(sql) < 0)
  86. {
  87. errorinfo = "更新数据库失败";
  88. return -1;
  89. }
  90. return 1;
  91. }
  92. public int Updates(List<Mtbacode> mtbacodes, string usercode, ref string errorinfo)
  93. {
  94. int i = 0;
  95. foreach (var item in mtbacodes)
  96. {
  97. string sql = $"Update Mtbacode set chalarm='{item.Chalarm}',ModCode='{usercode}',ModTime='{DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss")}' where 1=1 and id={item.ID}";
  98. if (CurrDb.ExecuteBySql(sql) < 0)
  99. {
  100. errorinfo = "更新数据库失败";
  101. return -1;
  102. }
  103. i = 1;
  104. }
  105. return i;
  106. }
  107. public int InsertMtabcode(McaSecVMst mst, List<McaSecVDetail> details, ref string errorinfo)
  108. {
  109. try
  110. {
  111. string filter = $@" and a.fcode='{mst.McaCode}'";
  112. int modelID = CurrDb.FindListForCondition<Machine>(filter, ref errorinfo).Select(c => c.MModeID).FirstOrDefault();
  113. if (details != null)
  114. {
  115. Mtbacode pro = new Mtbacode();
  116. pro.alarmcode = details.Where(c=>c.FCode== "S00006").Select(l=>l.FVal).FirstOrDefault();
  117. pro.Enalarm = details.Where(c=>c.FCode== "S00007").Select(l=>l.FVal).FirstOrDefault();
  118. pro.faulttype = 1;
  119. pro.MModeID = modelID;
  120. string codefilter = $@" and a.MModeID={modelID} and a.alarmcode='{pro.alarmcode}' ";
  121. var mtba = CurrDb.FindListForCondition<Mtbacode>(codefilter,ref errorinfo).FirstOrDefault();
  122. if(mtba==null)
  123. {
  124. Add(pro,null,ref errorinfo);
  125. }
  126. else
  127. {
  128. if (mtba.Enalarm != pro.Enalarm)
  129. {
  130. mtba.Enalarm = pro.Enalarm;
  131. Update(mtba, null, ref errorinfo);
  132. }
  133. }
  134. }
  135. return 1;
  136. }
  137. catch (Exception ex)
  138. {
  139. errorinfo = "添加mtbacode:"+ex.StackTrace;
  140. return -1;
  141. }
  142. }
  143. }
  144. }