You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

321 lines
13 KiB

  1. using System;
  2. using System.Collections.Generic;
  3. using System.Data.SqlClient;
  4. using System.Web;
  5. using System.Data;
  6. using System.Text;
  7. /// <summary>
  8. /// 管理员 角色
  9. /// </summary>
  10. namespace Basic.DAL
  11. {
  12. public class manager_role
  13. {
  14. /// <summary>
  15. /// 获得数据列表
  16. /// </summary>
  17. public DataSet GetList(string strWhere)
  18. {
  19. StringBuilder strSql = new StringBuilder();
  20. strSql.Append("select id,role_name,role_type ");
  21. strSql.Append(" from tb_manager_role ");
  22. if (strWhere.Trim() != "")
  23. {
  24. strSql.Append(" where " + strWhere);
  25. }
  26. return DbHelperSQL.Query(strSql.ToString());
  27. }
  28. /// <summary>
  29. /// 增加一条数据,及其子表数据
  30. /// </summary>
  31. public int Add(Model.manager_role model)
  32. {
  33. StringBuilder strSql = new StringBuilder();
  34. strSql.Append("insert into tb_manager_role(");
  35. strSql.Append("role_name,role_type)");
  36. strSql.Append(" values (");
  37. strSql.Append("@role_name,@role_type)");
  38. strSql.Append(";set @ReturnValue= @@IDENTITY");
  39. SqlParameter[] parameters = {
  40. new SqlParameter("@role_name", SqlDbType.NVarChar,100),
  41. new SqlParameter("@role_type", SqlDbType.TinyInt,1),
  42. new SqlParameter("@ReturnValue",SqlDbType.Int)};
  43. parameters[0].Value = model.role_name;
  44. parameters[1].Value = model.role_type;
  45. parameters[2].Direction = ParameterDirection.Output;
  46. List<CommandInfo> sqllist = new List<CommandInfo>();
  47. CommandInfo cmd = new CommandInfo(strSql.ToString(), parameters);
  48. sqllist.Add(cmd);
  49. StringBuilder strSql2;
  50. foreach (Model.manager_role_value models in model.manager_role_values)
  51. {
  52. strSql2 = new StringBuilder();
  53. strSql2.Append("insert into tb_manager_role_value(");
  54. strSql2.Append("role_id,channel_name,channel_id,action_type)");
  55. strSql2.Append(" values (");
  56. strSql2.Append("@role_id,@channel_name,@channel_id,@action_type)");
  57. SqlParameter[] parameters2 = {
  58. new SqlParameter("@role_id", SqlDbType.Int,4),
  59. new SqlParameter("@channel_name", SqlDbType.NVarChar,255),
  60. new SqlParameter("@channel_id", SqlDbType.Int,4),
  61. new SqlParameter("@action_type", SqlDbType.NVarChar,100)};
  62. parameters2[0].Direction = ParameterDirection.InputOutput;
  63. parameters2[1].Value = models.channel_name;
  64. parameters2[2].Value = models.channel_id;
  65. parameters2[3].Value = models.action_type;
  66. cmd = new CommandInfo(strSql2.ToString(), parameters2);
  67. sqllist.Add(cmd);
  68. }
  69. DbHelperSQL.ExecuteSqlTranWithIndentity(sqllist);
  70. return (int)parameters[2].Value;
  71. }
  72. /// <summary>
  73. /// 是否存在该记录
  74. /// </summary>
  75. public bool Exists(int id)
  76. {
  77. StringBuilder strSql = new StringBuilder();
  78. strSql.Append("select count(1) from tb_manager_role");
  79. strSql.Append(" where id=@id ");
  80. SqlParameter[] parameters = {
  81. new SqlParameter("@id", SqlDbType.Int,4)};
  82. parameters[0].Value = id;
  83. return DbHelperSQL.Exists(strSql.ToString(), parameters);
  84. }
  85. /// <summary>
  86. /// 得到一个对象实体
  87. /// </summary>
  88. public Model.manager_role GetModel(int id)
  89. {
  90. StringBuilder strSql = new StringBuilder();
  91. strSql.Append("select id,role_name,role_type from tb_manager_role ");
  92. strSql.Append(" where id=@id ");
  93. SqlParameter[] parameters = {
  94. new SqlParameter("@id", SqlDbType.Int,4)};
  95. parameters[0].Value = id;
  96. Model.manager_role model = new Model.manager_role();
  97. DataSet ds = DbHelperSQL.Query(strSql.ToString(), parameters);
  98. if (ds.Tables[0].Rows.Count > 0)
  99. {
  100. #region 父表信息
  101. if (ds.Tables[0].Rows[0]["id"].ToString() != "")
  102. {
  103. model.id = int.Parse(ds.Tables[0].Rows[0]["id"].ToString());
  104. }
  105. model.role_name = ds.Tables[0].Rows[0]["role_name"].ToString();
  106. if (ds.Tables[0].Rows[0]["role_type"].ToString() != "")
  107. {
  108. model.role_type = int.Parse(ds.Tables[0].Rows[0]["role_type"].ToString());
  109. }
  110. #endregion 父表信息end
  111. #region 子表信息
  112. StringBuilder strSql2 = new StringBuilder();
  113. strSql2.Append("select id,role_id,channel_name,channel_id,action_type from tb_manager_role_value ");
  114. strSql2.Append(" where role_id=@role_id ");
  115. SqlParameter[] parameters2 = {
  116. new SqlParameter("@role_id", SqlDbType.Int,4)};
  117. parameters2[0].Value = id;
  118. DataSet ds2 = DbHelperSQL.Query(strSql2.ToString(), parameters2);
  119. if (ds2.Tables[0].Rows.Count > 0)
  120. {
  121. #region 子表字段信息
  122. int i = ds2.Tables[0].Rows.Count;
  123. List<Model.manager_role_value> models = new List<Model.manager_role_value>();
  124. Model.manager_role_value modelt;
  125. for (int n = 0; n < i; n++)
  126. {
  127. modelt = new Model.manager_role_value();
  128. if (ds2.Tables[0].Rows[n]["id"].ToString() != "")
  129. {
  130. modelt.id = int.Parse(ds2.Tables[0].Rows[n]["id"].ToString());
  131. }
  132. if (ds2.Tables[0].Rows[n]["role_id"].ToString() != "")
  133. {
  134. modelt.role_id = int.Parse(ds2.Tables[0].Rows[n]["role_id"].ToString());
  135. }
  136. modelt.channel_name = ds2.Tables[0].Rows[n]["channel_name"].ToString();
  137. if (ds2.Tables[0].Rows[n]["channel_id"].ToString() != "")
  138. {
  139. modelt.channel_id = int.Parse(ds2.Tables[0].Rows[n]["channel_id"].ToString());
  140. }
  141. modelt.action_type = ds2.Tables[0].Rows[n]["action_type"].ToString();
  142. models.Add(modelt);
  143. }
  144. model.manager_role_values = models;
  145. #endregion 子表字段信息end
  146. }
  147. #endregion 子表信息end
  148. return model;
  149. }
  150. else
  151. {
  152. return null;
  153. }
  154. }
  155. /// <summary>
  156. /// 更新一条数据及其子表
  157. /// </summary>
  158. public bool Update(Model.manager_role model)
  159. {
  160. StringBuilder strSql = new StringBuilder();
  161. strSql.Append("update tb_manager_role set ");
  162. strSql.Append("role_name=@role_name,");
  163. strSql.Append("role_type=@role_type");
  164. strSql.Append(" where id=@id ");
  165. SqlParameter[] parameters = {
  166. new SqlParameter("@id", SqlDbType.Int,4),
  167. new SqlParameter("@role_name", SqlDbType.NVarChar,100),
  168. new SqlParameter("@role_type", SqlDbType.TinyInt,1)};
  169. parameters[0].Value = model.id;
  170. parameters[1].Value = model.role_name;
  171. parameters[2].Value = model.role_type;
  172. List<CommandInfo> sqllist = new List<CommandInfo>();
  173. CommandInfo cmd = new CommandInfo(strSql.ToString(), parameters);
  174. sqllist.Add(cmd);
  175. //删除权限
  176. StringBuilder strSql2 = new StringBuilder();
  177. strSql2.Append("delete from tb_manager_role_value where role_id=@role_id ");
  178. StringBuilder idList = new StringBuilder();
  179. if (model.manager_role_values != null)
  180. {
  181. foreach (Model.manager_role_value models in model.manager_role_values)
  182. {
  183. if (models.id > 0)
  184. {
  185. idList.Append(models.id + ",");
  186. }
  187. }
  188. string id_list = Basic.Tools.Utils.DelLastChar(idList.ToString(), ",");
  189. if (!string.IsNullOrEmpty(id_list))
  190. {
  191. strSql2.Append(" and id not in(" + id_list + ")");
  192. }
  193. }
  194. SqlParameter[] parameters2 = {
  195. new SqlParameter("@role_id", SqlDbType.Int,4)};
  196. parameters2[0].Value = model.id;
  197. cmd = new CommandInfo(strSql2.ToString(), parameters2);
  198. sqllist.Add(cmd);
  199. //添加权限
  200. if (model.manager_role_values != null)
  201. {
  202. StringBuilder strSql3;
  203. foreach (Model.manager_role_value models in model.manager_role_values)
  204. {
  205. strSql3 = new StringBuilder();
  206. if (models.id == 0)
  207. {
  208. strSql3.Append("insert into tb_manager_role_value(");
  209. strSql3.Append("role_id,channel_name,channel_id,action_type)");
  210. strSql3.Append(" values (");
  211. strSql3.Append("@role_id,@channel_name,@channel_id,@action_type)");
  212. SqlParameter[] parameters3 = {
  213. new SqlParameter("@role_id", SqlDbType.Int,4),
  214. new SqlParameter("@channel_name", SqlDbType.NVarChar,255),
  215. new SqlParameter("@channel_id", SqlDbType.Int,4),
  216. new SqlParameter("@action_type", SqlDbType.NVarChar,100)};
  217. parameters3[0].Value = model.id;
  218. parameters3[1].Value = models.channel_name;
  219. parameters3[2].Value = models.channel_id;
  220. parameters3[3].Value = models.action_type;
  221. cmd = new CommandInfo(strSql3.ToString(), parameters3);
  222. sqllist.Add(cmd);
  223. }
  224. }
  225. }
  226. int rowsAffected = DbHelperSQL.ExecuteSqlTran(sqllist);
  227. if (rowsAffected > 0)
  228. {
  229. return true;
  230. }
  231. else
  232. {
  233. return false;
  234. }
  235. }
  236. /// <summary>
  237. /// 返回角色名称
  238. /// </summary>
  239. public string GetTitle(int id)
  240. {
  241. StringBuilder strSql = new StringBuilder();
  242. strSql.Append("select top 1 role_name from tb_manager_role");
  243. strSql.Append(" where id=" + id);
  244. string title = Convert.ToString(DbHelperSQL.GetSingle(strSql.ToString()));
  245. if (string.IsNullOrEmpty(title))
  246. {
  247. return "";
  248. }
  249. return title;
  250. }
  251. /// <summary>
  252. /// 删除一条数据,及子表所有相关数据
  253. /// </summary>
  254. public bool Delete(int id)
  255. {
  256. List<CommandInfo> sqllist = new List<CommandInfo>();
  257. StringBuilder strSql = new StringBuilder();
  258. strSql.Append("delete from tb_manager_role ");
  259. strSql.Append(" where id=@id ");
  260. SqlParameter[] parameters = {
  261. new SqlParameter("@id", SqlDbType.Int,4)};
  262. parameters[0].Value = id;
  263. CommandInfo cmd = new CommandInfo(strSql.ToString(), parameters);
  264. sqllist.Add(cmd);
  265. StringBuilder strSql2 = new StringBuilder();
  266. strSql2.Append("delete from tb_manager_role_value ");
  267. strSql2.Append(" where role_id=@role_id ");
  268. SqlParameter[] parameters2 = {
  269. new SqlParameter("@role_id", SqlDbType.Int,4)};
  270. parameters2[0].Value = id;
  271. cmd = new CommandInfo(strSql2.ToString(), parameters2);
  272. sqllist.Add(cmd);
  273. int rowsAffected = DbHelperSQL.ExecuteSqlTran(sqllist);
  274. if (rowsAffected > 0)
  275. {
  276. return true;
  277. }
  278. else
  279. {
  280. return false;
  281. }
  282. }
  283. /// <summary>
  284. /// 获取Role_Type
  285. /// </summary>
  286. /// <param name="id"></param>
  287. public int GetRole_Type(int id)
  288. {
  289. int intRole_Type = 0;
  290. BasicPage bp = new BasicPage();
  291. SqlDataReader myread = bp.getRead("select * from tb_manager_role where id=" + id);
  292. if (myread.Read())
  293. {
  294. intRole_Type = int.Parse(myread["role_type"].ToString());
  295. }
  296. myread.Close();
  297. return intRole_Type;
  298. }
  299. }
  300. }