SupplierDal.cs 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  1. using Cksoft.Data;
  2. using DllEapEntity;
  3. using System;
  4. using System.Collections.Generic;
  5. using System.Linq;
  6. using System.Text;
  7. namespace DllEapDal
  8. {
  9. public class SupplierDal
  10. {
  11. private IDatabase CurrDb;
  12. public SupplierDal(IDatabase db)
  13. {
  14. CurrDb = db;
  15. }
  16. public IEnumerable<Supplier> Get(int start, int length, string order, string sort, string filter, string errorinfo)
  17. {
  18. var pros = CurrDb.FindListForCondition<Supplier>($" {filter} order by {sort} {order} limit {start - 1},{length}", ref errorinfo);
  19. return pros;
  20. }
  21. public int GetCount(string filter)
  22. {
  23. string errorinfo = string.Empty;
  24. var entities = CurrDb.FindListForCondition<Supplier>(filter, ref errorinfo);
  25. if (entities != null)
  26. {
  27. return entities.Count();
  28. }
  29. return 0;
  30. }
  31. public Supplier Get(int id)
  32. {
  33. var pro = CurrDb.FindEntityFor<Supplier>(id);
  34. return pro;
  35. }
  36. /// <summary>
  37. /// 添加角色并返回角色Id
  38. /// </summary>
  39. /// <param name="role"></param>
  40. /// <param name="userCode"></param>
  41. /// <returns></returns>
  42. public int Add(Supplier pro, string userCode, ref string errorinfo)
  43. {
  44. var entities = CurrDb.FindListForCondition<Supplier>($" and a.FCode='{pro.FCode}' ", ref errorinfo);
  45. if (entities != null && entities.Count() > 0)
  46. {
  47. errorinfo = "厂家已存在,请确认";
  48. return -1;
  49. }
  50. pro.RecCode = userCode;
  51. pro.ModCode = userCode;
  52. pro.RecTime = DateTime.Now;
  53. pro.ModTime = pro.RecTime;
  54. string sql = $"insert into Supplier(FCode,FName,Remark,RecCode,RecTime," +
  55. $"ModCode,ModTime) values('{pro.FCode}','{pro.FName}','{pro.Remark}','{pro.RecCode}','{pro.RecTime.ToString("yyyy-MM-dd HH:mm:ss")}','{pro.ModCode}','{pro.ModTime.ToString("yyyy-MM-dd HH:mm:ss")}');";
  56. sql += "select @@identity;";
  57. var id = Convert.ToInt32(CurrDb.FindList<string>(sql).FirstOrDefault() ?? "-1");
  58. return id;
  59. }
  60. /// <summary>
  61. /// 待优化 存在删除后不能
  62. /// </summary>
  63. /// <param name="role"></param>
  64. /// <param name="userCode"></param>
  65. /// <param name="errorinfo"></param>
  66. /// <returns></returns>
  67. public int Update(Supplier role, string userCode, ref string errorinfo)
  68. {
  69. var entities = CurrDb.FindListForCondition<Supplier>($" and a.FCode='{role.FCode}' " +
  70. $"and a.ID<>{role.ID}", ref errorinfo);
  71. if (entities != null && entities.Count() > 0)
  72. {
  73. errorinfo = "已存在相同的厂家,请确认";
  74. return -1;
  75. }
  76. if (CurrDb.UpdateFor(role, userCode) < 0)
  77. {
  78. return -1;
  79. }
  80. return role.ID;
  81. }
  82. public int Delete(int id, ref string msg)
  83. {
  84. string sql = $"select * from MacModel where SupplierID={id}";
  85. var macmodels = CurrDb.FindList<MacModel>(sql);
  86. if (macmodels != null && macmodels.Count() > 0)
  87. {
  88. msg = "该厂家有关联的机型未删除,请先删除机型";
  89. return -1;
  90. }
  91. if (CurrDb.DeleteFor<Supplier>(id) < 0)
  92. {
  93. msg = "删除失败";
  94. return -1;
  95. }
  96. msg = string.Empty;
  97. return 1;
  98. }
  99. public IEnumerable<Supplier> Get(string filter)
  100. {
  101. string error = string.Empty;
  102. return CurrDb.FindListForCondition<Supplier>(filter, ref error);
  103. }
  104. public Supplier AddOrGet(Supplier supplier, string userCode, ref string errorinfo)
  105. {
  106. var exist = CurrDb.FindListForCondition<Supplier>($" and a.FCode='{supplier.FCode}'", ref errorinfo)
  107. .FirstOrDefault();
  108. if (exist != null)
  109. return exist;
  110. var entity = new Supplier { FCode = supplier.FCode, FName = supplier.FName };
  111. if (CurrDb.InsertFor(entity, userCode) < 0)
  112. {
  113. return null;
  114. }
  115. exist = CurrDb.FindListForCondition<Supplier>($" and a.FCode='{supplier.FCode}'", ref errorinfo)
  116. .FirstOrDefault();
  117. return exist;
  118. }
  119. }
  120. }