PMSchemaDal.cs 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  1. using Cksoft.Data;
  2. using System;
  3. using System.Collections.Generic;
  4. using System.Text;
  5. using DllEapEntity.PM;
  6. using System.Linq;
  7. namespace DllEapDal
  8. {
  9. public class PMSchemaDal
  10. {
  11. private IDatabase CurrDb;
  12. public PMSchemaDal(IDatabase db)
  13. {
  14. CurrDb = db;
  15. }
  16. public IEnumerable<PMSchema> Get()
  17. {
  18. var models = CurrDb.FindList<PMSchema>();
  19. return models;
  20. }
  21. public IEnumerable<PMSchema> Get(int start, int length, string order, string sort, string filter, string errorinfo)
  22. {
  23. var pros = CurrDb.FindListForCondition<PMSchema>($" {filter} order by {sort} {order} limit {start - 1},{length}", ref errorinfo);
  24. return pros;
  25. }
  26. public int GetCount(string filter)
  27. {
  28. string errorinfo = string.Empty;
  29. //string sql = $"select count(1) from ProgramRule a where 1=1 {filter}";
  30. var entities = CurrDb.FindListForCondition<PMSchema>(filter, ref errorinfo);
  31. if (entities != null)
  32. {
  33. return entities.Count();
  34. }
  35. return 0;
  36. }
  37. public PMSchema Get(int id)
  38. {
  39. var pro = CurrDb.FindEntityFor<PMSchema>(id);
  40. return pro;
  41. }
  42. public IEnumerable<PMMachine> getpmmachine(int id)
  43. {
  44. var sql = $"select * from pmmachine where pmid={id}";
  45. var pro = CurrDb.FindList<PMMachine>(sql);
  46. return pro;
  47. }
  48. /// <summary>
  49. /// 添加角色并返回角色Id
  50. /// </summary>
  51. /// <param name="role"></param>
  52. /// <param name="userCode"></param>
  53. /// <returns></returns>
  54. public int Add(PMSchema pro, string userCode, ref string errorinfo)
  55. {
  56. var entities = CurrDb.FindListForCondition<PMSchema>($" and a.FCode='{pro.FCode}' ", ref errorinfo);
  57. if (entities != null && entities.Count() > 0)
  58. {
  59. errorinfo = "方案已存在,请确认";
  60. return -1;
  61. }
  62. pro.RecCode = pro.ModCode = userCode;
  63. pro.RecTime = pro.ModTime = DateTime.Now;
  64. string sql = $"insert into PMSchema(FCode,FName,Remark,RecCode,RecTime," +
  65. $"ModCode,ModTime,Maintenance,TimeSpan) values('{pro.FCode}'," +
  66. $"'{pro.FName}','{pro.Remark}','{pro.RecCode}'," +
  67. $"'{pro.RecTime.ToString("yyyy-MM-dd HH:mm:ss")}','{pro.ModCode}'," +
  68. $"'{pro.ModTime.ToString("yyyy-MM-dd HH:mm:ss")}','{pro.Maintenance}','{pro.TimeSpan}');";
  69. sql += "select @@identity;";
  70. var id = Convert.ToInt32(CurrDb.FindList<string>(sql).FirstOrDefault() ?? "-1");
  71. return id;
  72. }
  73. public int Update(PMSchema role, string userCode, ref string errorinfo)
  74. {
  75. var entities = CurrDb.FindListForCondition<PMSchema>($" and a.FCode='{role.FCode}' " +
  76. $"and a.ID<>{role.ID}", ref errorinfo);
  77. if (entities != null && entities.Count() > 0)
  78. {
  79. errorinfo = "已存在相同的方案,请确认";
  80. return -1;
  81. }
  82. if (CurrDb.UpdateFor(role, userCode) < 0)
  83. {
  84. return -1;
  85. }
  86. return role.ID;
  87. }
  88. public int Delete(int id, ref string msg)
  89. {
  90. var sql = $"delete from pmmachine where pmid={id}";
  91. if (CurrDb.ExecuteBySql(sql) < 0)
  92. {
  93. msg = "删除保养中间表失败";
  94. return -1;
  95. }
  96. if (CurrDb.DeleteFor<PMSchema>(id) < 0)
  97. {
  98. msg = "删除失败";
  99. return -1;
  100. }
  101. msg = string.Empty;
  102. return 1;
  103. }
  104. }
  105. }