SecDal.cs 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213
  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
  9. {
  10. public class SecDal
  11. {
  12. private IDatabase CurrDb = null;
  13. public SecDal(IDatabase db)
  14. {
  15. CurrDb = db;
  16. }
  17. public Sec IUSec(Sec mst, string usercode, ref string errorinfo)
  18. {
  19. try
  20. {
  21. int result = 0;
  22. int id = mst.ID;
  23. if (mst.EntityStatusID == 1)
  24. {
  25. string code = GetSecCode(mst.FType, ref errorinfo);
  26. if (code == "")
  27. {
  28. return null;
  29. }
  30. mst.FCode = code;
  31. mst.RecCode = usercode;
  32. mst.RecTime = DateTime.Now;
  33. mst.ModCode = usercode;
  34. mst.ModTime = DateTime.Now;
  35. result = CurrDb.InsertFor(mst, usercode);
  36. if (result < 0)
  37. {
  38. return null;
  39. }
  40. object objid = CurrDb.FindObject("select @@IDENTITY");
  41. if (objid.ToString() == "")
  42. {
  43. return null;
  44. }
  45. id = int.Parse(objid.ToString());
  46. }
  47. else
  48. {
  49. mst.ModCode = usercode;
  50. mst.ModTime = DateTime.Now;
  51. result = CurrDb.UpdateFor(mst, usercode);
  52. if (result < 0)
  53. {
  54. return null;
  55. }
  56. }
  57. mst = CurrDb.FindEntityFor<Sec>(id);
  58. return mst;
  59. }
  60. catch (Exception e)
  61. {
  62. errorinfo = e.Message;
  63. return null;
  64. }
  65. }
  66. private string GetSecCode(int ftype, ref string errorinfo)
  67. {
  68. try
  69. {
  70. StringBuilder sqlstr = new StringBuilder(100);
  71. string code = "";
  72. switch (ftype)
  73. {
  74. case 1:
  75. code = "S";
  76. break;
  77. case 2:
  78. code = "E";
  79. break;
  80. case 3:
  81. code = "C";
  82. break;
  83. default:
  84. code = "F";
  85. break;
  86. }
  87. sqlstr.AppendFormat("SELECT max(fcode) FROM sec where fcode like '{0}%'", code);
  88. object obj = CurrDb.FindObject(sqlstr.ToString());
  89. if (obj.ToString() == "")
  90. return code + "00001";
  91. int fnum = int.Parse(obj.ToString().Substring(1, 5));
  92. fnum++;
  93. return code + fnum.ToString().PadLeft(5, '0');
  94. }
  95. catch (Exception ex)
  96. {
  97. errorinfo = ex.Message.ToString();
  98. return "";
  99. }
  100. }
  101. #region WEB
  102. public IEnumerable<Sec> GetSecs(string filter, ref string errorinfo)
  103. {
  104. return CurrDb.FindListForCondition<Sec>(filter, ref errorinfo);
  105. }
  106. public IEnumerable<Sec> Get(int start, int length, string order, string sort, string filter, string errorinfo)
  107. {
  108. var pros = CurrDb.FindListForCondition<Sec>($" {filter} order by {sort} {order} limit {start - 1},{length}", ref errorinfo).ToList();
  109. return pros;
  110. }
  111. public int GetCount(string filter)
  112. {
  113. string errorinfo = string.Empty;
  114. var entities = CurrDb.FindListForCondition<Sec>(filter, ref errorinfo);
  115. if (entities != null)
  116. {
  117. return entities.Count();
  118. }
  119. return 0;
  120. }
  121. public Sec Get(int id)
  122. {
  123. string errorinfo = string.Empty;
  124. var pro = CurrDb.FindEntityFor<Sec>(id);
  125. return pro;
  126. }
  127. /// <summary>
  128. /// 添加角色并返回角色Id
  129. /// </summary>
  130. /// <param name="role"></param>
  131. /// <param name="userCode"></param>
  132. /// <returns></returns>
  133. public int Add(Sec pro, string userCode, ref string errorinfo)
  134. {
  135. pro.RecCode = pro.ModCode = userCode;
  136. pro.RecTime = pro.ModTime = DateTime.Now;
  137. var entity = CurrDb.FindListForCondition<Sec>($" and a.FName ='{pro.FName}' ", ref errorinfo).FirstOrDefault();
  138. if (entity != null)
  139. {
  140. errorinfo = "参数名已存在,请修改后重新添加";
  141. return -1;
  142. }
  143. var fCode = string.Empty;
  144. fCode = this.GetSecCode(pro.FType, ref errorinfo);
  145. if (string.IsNullOrEmpty(fCode))
  146. {
  147. return -1;
  148. }
  149. string sql = $"insert into Sec(FCode,FName,DCode,FType,FVal,Remark,RecCode,RecTime,ModCode,ModTime) values('{fCode}','{pro.FName}'," +
  150. $"'{pro.DCode}','{pro.FType}','{pro.FVal}','{pro.Remark}','{pro.RecCode}','{pro.RecTime.ToString("yyyy-MM-dd HH:mm:ss")}','{pro.ModCode}','{pro.ModTime.ToString("yyyy-MM-dd HH:mm:ss")}');";
  151. sql += "select @@identity;";
  152. var id = Convert.ToInt32(CurrDb.FindList<string>(sql).FirstOrDefault() ?? "-1");
  153. errorinfo = "";
  154. return id;
  155. }
  156. public int Update(Sec role, string userCode, ref string error)
  157. {
  158. var entity = CurrDb.FindListForCondition<Sec>($" and a.FName ='{role.FName}' and a.id <> '{role.ID}'", ref error).FirstOrDefault();
  159. if (entity != null)
  160. {
  161. error = "参数名已存在,请确认";
  162. return -1;
  163. }
  164. if (CurrDb.UpdateFor(role, userCode) > 0)
  165. {
  166. return role.ID;
  167. }
  168. return -1;
  169. }
  170. public int Delete(int id, ref string msg)
  171. {
  172. string sql = $"select * from orderevent where preid='{id}'";
  173. var events = CurrDb.FindList<OrderEvent>(sql);
  174. if (events != null && events.Count() > 0)
  175. {
  176. msg = "该参数已有机型在使用,不能删除";
  177. return -1;
  178. }
  179. var secDetails = CurrDb.FindListForCondition<MMSecDetail>($" and a.SecID='{id}'",ref msg);
  180. if (secDetails != null && secDetails.Count() > 0)
  181. {
  182. msg = "该参数已有机型在使用,不能删除";
  183. return -1;
  184. }
  185. var reportParams = CurrDb.FindListForCondition<ReportDetail>($" and a.Secid={id}", ref msg);
  186. if (reportParams != null && reportParams.Count() > 0)
  187. {
  188. msg = "该参数已有机型在使用,不能删除";
  189. return -1;
  190. }
  191. if (CurrDb.DeleteFor<Sec>(id) < 0)
  192. {
  193. msg = "删除程序主表时出错,请联系管理员";
  194. return -1;
  195. }
  196. msg = string.Empty;
  197. return 1;
  198. }
  199. #endregion
  200. }
  201. }