IdealOutputConfigDal.cs 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  1. using Cksoft.Data;
  2. using DllEapEntity.Dtos;
  3. using DllEapEntity.Mes;
  4. using System;
  5. using System.Collections.Generic;
  6. using System.Linq;
  7. using System.Text;
  8. using Toolkits;
  9. namespace DllEapDal
  10. {
  11. public class IdealOutputConfigDal
  12. {
  13. public IDatabase CurrDb;
  14. public IdealOutputConfigDal(IDatabase db)
  15. {
  16. CurrDb = db;
  17. }
  18. public IEnumerable<IdealOutputConfig> Get(int start, int length, string order, string sort, string filter, string errorinfo)
  19. {
  20. int end = start + length;
  21. var baseSql = this.GetSelectSql(filter);
  22. var sql = $"select * from (select row_number() over (order by {sort} {order}) as rowNum,t.* from ({baseSql}) t) tl " +
  23. $"where tl.RowNum>={start} and tl.RowNum<{end}";
  24. return CurrDb.FindList<IdealOutputConfig>(sql);
  25. }
  26. public int GetCount(string filter)
  27. {
  28. string errorinfo = string.Empty;
  29. var entities = CurrDb.FindList<IdealOutputConfig>(this.GetSelectSql(filter));
  30. if (entities != null)
  31. {
  32. return entities.Count();
  33. }
  34. return 0;
  35. }
  36. public IdealOutputConfig Get(int id)
  37. {
  38. string errorinfo = string.Empty;
  39. var sql = this.GetSelectSql(string.Empty) + $" and a.id={id}";
  40. var pro = CurrDb.FindList<IdealOutputConfig>(sql).FirstOrDefault();
  41. return pro;
  42. }
  43. /// <summary>
  44. /// 添加角色并返回角色Id
  45. /// </summary>
  46. /// <param name="role"></param>
  47. /// <param name="userCode"></param>
  48. /// <returns></returns>
  49. public int Add(IdealOutputConfig pro, string userCode, ref string errorinfo)
  50. {
  51. pro.reccode = userCode;
  52. pro.modcode = userCode;
  53. pro.rectime = DateTime.Now;
  54. pro.modtime = DateTime.Now;
  55. var existsSql = $"select * from IdealOutputConfig where FCode='{pro.FCode}'";
  56. var entity = CurrDb.FindList<IdealOutputConfig>(existsSql).FirstOrDefault();
  57. if (entity != null)
  58. {
  59. errorinfo = WebErrorMsg.CodeDuplicate;
  60. return -1;
  61. }
  62. var sql = $"insert into IdealOutputConfig(FCode,FName,FLength,Remark,RecCode,recTime,modcode,modTime) values('{pro.FCode}','{pro.FName}'," +
  63. $"'{pro.FLength}','{pro.Remark}','{pro.reccode}','{pro.rectime.Value.ToString("yyyy-MM-dd")}','{pro.modcode}','{pro.modtime.Value.ToString("yyyy-MM-dd")}');" +
  64. $"select @@identity;";
  65. var id = Convert.ToInt32(CurrDb.FindList<string>(sql).FirstOrDefault() ?? "-1");
  66. return id;
  67. }
  68. public int Update(IdealOutputConfig role, string userCode, ref string error)
  69. {
  70. var existsSql = $"select * from IdealOutputConfig where FCode='{role.FCode}' and id<>{role.ID}";
  71. var entity = CurrDb.FindList<IdealOutputConfig>(existsSql).FirstOrDefault();
  72. if (entity != null)
  73. {
  74. error = WebErrorMsg.CodeDuplicate;
  75. return -1;
  76. }
  77. role.modcode = userCode;
  78. role.modtime = DateTime.Now;
  79. if (CurrDb.Update<IdealOutputConfig>(role) < 0)
  80. {
  81. error = WebErrorMsg.UpdateFailed;
  82. return -1;
  83. }
  84. return 1;
  85. }
  86. public int Delete(int id, ref string msg)
  87. {
  88. // 判断是否有产品正在使用该程序
  89. string sql = $"select * from ProductIdealOutputConfig where ConfigID='{id}'";
  90. var rule = CurrDb.FindList<ProductIdealOutputConfig>(sql).FirstOrDefault();
  91. if (rule != null)
  92. {
  93. msg = "该理想产量信息正在被使用,无法删除";
  94. return -1;
  95. }
  96. sql = $"delete from IdealOutputConfig where id={id}";
  97. if (CurrDb.ExecuteBySql(sql) < 0)
  98. {
  99. msg = WebErrorMsg.DeleteFailed;
  100. return -1;
  101. }
  102. msg = string.Empty;
  103. return 1;
  104. }
  105. private string GetSelectSql(string filter)
  106. {
  107. var sql = @"select a.*,b.Fname as recName,c.Fname as modname from IdealOutputConfig a
  108. left join Staff b on a.reccode=b.FCode
  109. left join Staff c on a.modcode=c.FCode "
  110. + $"where 1=1 {filter}";
  111. return sql;
  112. }
  113. public IEnumerable<SelectDto<int>> GetConfigs()
  114. {
  115. string sql = "select id as value,fname+' | '+convert(varchar(50),flength)+'件/小时' as label from IdealOutputConfig";
  116. return CurrDb.FindList<SelectDto<int>>(sql);
  117. }
  118. }
  119. }