CmkDal.cs 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235
  1. using Cksoft.Data;
  2. using Cksoft.Unity;
  3. using DllEapEntity;
  4. using DllEapEntity.Dtos;
  5. using Newtonsoft.Json;
  6. using System;
  7. using System.Collections.Generic;
  8. using System.Linq;
  9. using System.Text;
  10. namespace DllEapDal.OFILM
  11. {
  12. public class CmkDal
  13. {
  14. private IDatabase CurrDb = null;
  15. public CmkDal(IDatabase db)
  16. {
  17. CurrDb = db;
  18. }
  19. public IEnumerable<Ofilmcmk> Get(int start, int length, string order, string sort, string filter, string errorinfo)
  20. {
  21. try
  22. {
  23. var pros = CurrDb.FindListForCondition<Ofilmcmk>($" {filter} order by {sort} {order} limit {start - 1},{length}", ref errorinfo);
  24. return pros;
  25. }
  26. catch (Exception ex)
  27. {
  28. Console.WriteLine(ex.ToString() + ex.StackTrace);
  29. return null;
  30. }
  31. }
  32. public int GetCount(string filter)
  33. {
  34. string errorinfo = string.Empty;
  35. var entities = CurrDb.FindListForCondition<Ofilmcmk>(filter, ref errorinfo);
  36. if (entities != null)
  37. {
  38. return entities.Count();
  39. }
  40. return 0;
  41. }
  42. public Ofilmcmk Get(int id)
  43. {
  44. var pro = CurrDb.FindEntityFor<Ofilmcmk>(id);
  45. return pro;
  46. }
  47. /// <summary>
  48. /// 添加WorkingProcedure并返回Id
  49. /// </summary>
  50. /// <param name="role"></param>
  51. /// <param name="userCode"></param>
  52. /// <returns></returns>
  53. public int Add(Ofilmcmk pro, string userCode, ref string errorinfo)
  54. {
  55. var entities = CurrDb.FindListForCondition<Ofilmcmk>($" and a.macID='{pro.macID}' ", ref errorinfo);
  56. if (entities != null && entities.Count() > 0)
  57. {
  58. errorinfo = "已存在相同设备ID,请确认!";
  59. return -1;
  60. }
  61. CurrDb.InsertFor(pro, userCode);
  62. var sql = "select @@identity;";
  63. var id = Convert.ToInt32(CurrDb.FindList<string>(sql).FirstOrDefault() ?? "-1");
  64. return id;
  65. }
  66. /// <summary>
  67. /// 批量新增
  68. /// </summary>
  69. /// <param name="pros"></param>
  70. /// <param name="userCode"></param>
  71. /// <param name="errorinfo"></param>
  72. /// <returns></returns>
  73. //public int Adds(IEnumerable<Ofilmcmk> pros, string userCode, ref string errorinfo)
  74. //{
  75. // try
  76. // {
  77. // var gpors = pros.GroupBy(l => new { l.Park, l.Floor, l.MachineType }).Select(p => new { p.Key.Park, p.Key.Floor, p.Key.MachineType });
  78. // foreach (var item in gpors)
  79. // {
  80. // string sqldel = $"delete from WorkingProcedure where Park='{item.Park}' and Floor='{item.Floor}' and MachineType='{item.MachineType}'";
  81. // CurrDb.ExecuteBySql(sqldel);
  82. // }
  83. // CurrDb.InsertFor<Ofilmcmk>(pros, userCode);
  84. // var sql = "select @@identity;";
  85. // var id = Convert.ToInt32(CurrDb.FindList<string>(sql).FirstOrDefault() ?? "-1");
  86. // return id;
  87. // }
  88. // catch (Exception e)
  89. // {
  90. // errorinfo = e.ToString();
  91. // return -1;
  92. // }
  93. //}
  94. /// <summary>
  95. /// 查询是否有重复数据
  96. /// </summary>
  97. /// <param name="pros"></param>
  98. /// <param name="errorinfo"></param>
  99. /// <returns></returns>
  100. public int CheckSame(IEnumerable<Ofilmcmk> pros, ref Object errorinfo)
  101. {
  102. List<Ofilmcmk> workingProcedures = new List<Ofilmcmk>();
  103. var result = from r in pros
  104. group r by new { r.macID } into g
  105. where g.Count() > 1
  106. select g;
  107. //遍历分组结果集
  108. foreach (var item in result)
  109. {
  110. foreach (Ofilmcmk u in item)
  111. {
  112. workingProcedures.Add(u);
  113. }
  114. }
  115. if (workingProcedures.Count() >= 1)
  116. {
  117. var gpors = workingProcedures.GroupBy(l => new { l.macID }).Select(p => new { p.Key.macID });
  118. errorinfo = gpors;
  119. return -2;
  120. }
  121. return 1;
  122. }
  123. /// <summary>
  124. /// 待优化 存在删除后不能
  125. /// </summary>
  126. /// <param name="role"></param>
  127. /// <param name="userCode"></param>
  128. /// <param name="errorinfo"></param>
  129. /// <returns></returns>
  130. public int Update(Ofilmcmk role, string userCode, ref string errorinfo)
  131. {
  132. var entities = CurrDb.FindListForCondition<Ofilmcmk>($" and a.macID='{role.macID}' " +
  133. $"and a.ID<>{role.ID}", ref errorinfo);
  134. if (entities != null && entities.Count() > 0)
  135. {
  136. errorinfo = "已存在相同MacID,请确认";
  137. return -1;
  138. }
  139. if (CurrDb.UpdateFor(role, userCode) < 0)
  140. {
  141. return -1;
  142. }
  143. return role.ID;
  144. }
  145. public IEnumerable<Ofilmcmk> getWorkingProcedure(int id)
  146. {
  147. var sql = $"select * from WorkingProcedure where ID={id}";
  148. return CurrDb.FindList<Ofilmcmk>(sql);
  149. }
  150. public int Delete(int id, ref string msg)
  151. {
  152. if (CurrDb.DeleteFor<Ofilmcmk>(id) < 0)
  153. {
  154. msg = "删除失败";
  155. return -1;
  156. }
  157. msg = string.Empty;
  158. return 1;
  159. }
  160. public int DeleteAll(string filter, ref string msg)
  161. {
  162. string sql = $@" delete from Ofilmcmk where 1=1 {filter}";
  163. if (CurrDb.ExecuteBySql(sql) < 0)
  164. {
  165. msg = "删除失败";
  166. return -1;
  167. }
  168. msg = string.Empty;
  169. return 1;
  170. }
  171. public IEnumerable<Ofilmcmk> GetWorkingProcedureExprort(string filter, string sortField, string sortOrder, int take)
  172. {
  173. var sql = $"select * from Ofilmcmk limit {take}";
  174. IEnumerable<Ofilmcmk> dtos = CurrDb.FindList<Ofilmcmk>(sql).Take(take);
  175. if (dtos == null || dtos.Count() <= 0)
  176. return null;
  177. return dtos;
  178. }
  179. public IEnumerable<SelectDto<string>> GetMultipleClassSelects(string filter)
  180. {
  181. var sql = $@"select distinct classes value,classes label
  182. FROM ofilmcmk ";
  183. var entities = CurrDb.FindList<SelectDto<string>>(sql);
  184. return entities;
  185. }
  186. public IEnumerable<SelectDto<string>> GetMultipleFloorSelects(string filter)
  187. {
  188. var sql = $@"select distinct floor value,floor label
  189. FROM ofilmcmk ";
  190. var entities = CurrDb.FindList<SelectDto<string>>(sql);
  191. return entities;
  192. }
  193. public IEnumerable<SelectDto<string>> GetMultipleCustomerSelects(string filter)
  194. {
  195. var sql = $@"select distinct customer value,customer label
  196. FROM ofilmcmk ";
  197. var entities = CurrDb.FindList<SelectDto<string>>(sql);
  198. return entities;
  199. }
  200. public IEnumerable<SelectDto<string>> GetMultipleMacIDSelects(string filter)
  201. {
  202. var sql = $@"select distinct macID value,macID label
  203. FROM ofilmcmk ";
  204. var entities = CurrDb.FindList<SelectDto<string>>(sql);
  205. return entities;
  206. }
  207. public IEnumerable<SelectDto<string>> GetMultiplePositionSelects(string filter)
  208. {
  209. var sql = $@"select distinct position value,position label
  210. FROM ofilmcmk ";
  211. var entities = CurrDb.FindList<SelectDto<string>>(sql);
  212. return entities;
  213. }
  214. public IEnumerable<SelectDto<string>> GetMultipleRecCodeSelects(string filter)
  215. {
  216. var sql = $@"select distinct RecCode value,RecCode label
  217. FROM ofilmcmk ";
  218. var entities = CurrDb.FindList<SelectDto<string>>(sql);
  219. return entities;
  220. }
  221. }
  222. }