CM_UserController.cs 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389
  1. using JCSoft.WX.Framework.Api;
  2. using Microsoft.AspNetCore.Mvc;
  3. using Microsoft.AspNetCore.Http;
  4. using System;
  5. using System.Collections.Generic;
  6. using System.Linq;
  7. using System.Threading.Tasks;
  8. using Common.Wechat;
  9. using Common;
  10. using System.Data;
  11. using Common.Model;
  12. using ZcPeng.PublicLibrary;
  13. using CoreEntity.Entity;
  14. using CoreEntity.DAL;
  15. using System.Collections.Concurrent;
  16. using Newtonsoft.Json.Converters;
  17. using Newtonsoft.Json;
  18. using System.Data.SqlClient;
  19. using Microsoft.Extensions.Primitives;
  20. using Microsoft.Extensions.Caching.Memory;
  21. using Jwt;
  22. using PublicLibrary.Model;
  23. using Newtonsoft.Json.Linq;
  24. using SupplierWeb.Codes.mvc;
  25. using SupplierWeb.Codes.Auth;
  26. using Common.Config;
  27. namespace SupplierWeb.Controllers
  28. {
  29. [Route("web/CM_User")]
  30. public class CM_UserController : BaseController
  31. {
  32. public CM_UserController(IMemoryCache cache, IApiClient client) : base(cache, client)
  33. {
  34. }
  35. private ConcurrentDictionary<String, CM_User> UserMap = new ConcurrentDictionary<String, CM_User>();
  36. public class TempTableResult
  37. {
  38. public int id { get; set; }
  39. public string permissionName { get; set; }
  40. public string permissionUrl { get; set; }
  41. public int UserId { get; set; }
  42. public int checkedValue { get; set; }
  43. public string permissionRule { get; set; }
  44. public string permissionRuleType { get; set; }
  45. public int relateId { get; set; }
  46. public string AuthType { get; set; }
  47. }
  48. public class TempUserResult
  49. {
  50. public string parentValue { get; set; }
  51. public string label { get; set; }
  52. public int value { get; set; }
  53. }
  54. public class TempQuery
  55. {
  56. public int[] data { get; set; }
  57. public int UserId { get; set; }
  58. }
  59. public class SortContent
  60. {
  61. public Object content { get; set; }//权限内容
  62. public string code { get; set; }//权限id
  63. public int sort { get; set; }//权限序号
  64. }
  65. /// <summary>
  66. /// 用户列表
  67. /// </summary>
  68. /// <param name="filters"></param>
  69. /// <param name="pageIndex"></param>
  70. /// <param name="pageSize"></param>
  71. /// <param name="sortField"></param>
  72. /// <param name="sortDirection"></param>
  73. /// <param name="sumFields"></param>
  74. /// <param name="data"></param>
  75. /// <returns></returns>
  76. [AuthPermission]
  77. [HttpPost, Route("index")]
  78. public JsonResult Indexp(QueryFilter[] filters, Int32 pageIndex, Int32 pageSize,
  79. string sortField, Int32 sortDirection, string[] sumFields, [FromBody]dynamic data)
  80. {
  81. if (data != null)
  82. {
  83. filters = data.filters.ToObject<QueryFilter[]>();
  84. pageIndex = data.pageIndex;
  85. pageSize = data.pageSize;
  86. sortField = data.sortField;
  87. sortDirection = data.sortDirection;
  88. sumFields = data.sumFields.ToObject<string[]>();
  89. }
  90. #region 获取角色列表
  91. DataTable dt = new DataTable();
  92. string result;
  93. IList<CM_User> permss = new List<CM_User>(0);
  94. List<SqlParameter> parameters = new List<SqlParameter>();
  95. string filterstr = QueryFilter.getFilterSqlParam(filters, out parameters, new CM_User(), "A.");
  96. string direct = " desc ";
  97. if (sortDirection != 1)
  98. {
  99. direct = " asc";
  100. }
  101. int start = (pageIndex - 1) * pageSize;
  102. int end = (start + 1 + pageSize);
  103. string commandText0 = "select * from ";
  104. string commandText1 = "(" +
  105. "select A.*,row_number() over" +
  106. "( order by A.UserId " + direct + " ) as rownum from " +
  107. "CM_User as A " +
  108. " where 1=1 " +
  109. //" and A.CreationPerson = '" + userids + "'" +
  110. filterstr +
  111. ")AAA ";
  112. string commandText2 = " where AAA.rownum>" + start + " and AAA.rownum<" + end;
  113. string commandText3 = commandText0 + commandText1 + commandText2;
  114. bool success = DataAccess.GetValues(commandText3, ref dt, parameters.ToArray(), out result);
  115. #endregion
  116. if (dt != null && dt.Rows.Count > 0)
  117. {
  118. // 把DataTable转换为IList<Permission>
  119. permss = ModelConvertHelper<CM_User>.ConvertToModel(dt);
  120. #region 获取权限的角色列表,角色列表
  121. //角色列表
  122. IList<CM_User> Users = GetUsers();
  123. // 把DataTable转换为IList<User>
  124. if (UserMap.Count < Users.Count)
  125. {
  126. foreach (CM_User User in Users)
  127. {
  128. UserMap.TryAdd(User.UserId, User);
  129. }
  130. }
  131. #region
  132. foreach (CM_User perms in permss)
  133. {
  134. string direct1 = " desc ";
  135. dt = DataAccess.GetDataTable("CM_User", "UserId", "*", "UserId=" + perms.UserId, "", "UserId" + direct1, 1, 100, out var msg);
  136. // 把DataTable转换为IList<UserRelatePermission>
  137. if (dt != null && dt.Rows.Count > 0)
  138. {
  139. IList<CM_User> permRelates = ModelConvertHelper<CM_User>.ConvertToModel(dt);
  140. List<CM_User> listUsers = new List<CM_User>();
  141. List<int> superior_list = new List<int>(0);
  142. foreach (CM_User permsr in permRelates)
  143. {
  144. CM_User User;
  145. if (UserMap.TryGetValue(permsr.UserId, out User))
  146. {
  147. listUsers.Add(User);
  148. }
  149. superior_list.Add(Convert.ToInt32(permsr.UserId));
  150. }
  151. //perms.UserList = listUsers;
  152. perms.UserId = string.Join(",", superior_list.ToArray());
  153. }
  154. }
  155. #endregion
  156. #endregion
  157. }
  158. string result1;
  159. long totalcount = DataAccess.GetRowCountDefine("select count(UserId) from " + commandText1, parameters.ToArray(), out result1);
  160. IsoDateTimeConverter timejson = new IsoDateTimeConverter
  161. {
  162. DateTimeFormat = "yyyy'-'MM'-'dd' 'HH':'mm':'ss"
  163. };
  164. //IList<Menu> menus = Permission.Convert(permss);
  165. var jsonData = JsonConvert.SerializeObject(permss, timejson);
  166. return Json(new
  167. {
  168. items = JsonConvert.DeserializeObject(jsonData),
  169. sum = new { },
  170. totalCount = totalcount
  171. });
  172. }
  173. /// <summary>
  174. /// 新增用户
  175. /// </summary>
  176. /// <param name="UserId"></param>
  177. /// <param name="UserCode"></param>
  178. /// <param name="UserName"></param>
  179. /// <param name="data"></param>
  180. /// <returns></returns>
  181. [AuthPermission]
  182. [HttpPost, Route("add")]
  183. public JsonResult Addp(
  184. String UserId, String OrgId,
  185. string LoginName, [FromBody]dynamic data
  186. )
  187. {
  188. string UserName = "";
  189. string IsActive = "";
  190. if (data != null)
  191. {
  192. UserId = data.UserId;
  193. OrgId = data.OrgId;
  194. LoginName = data.LoginName;
  195. UserName = data.UserName;
  196. IsActive = data.IsActive;
  197. }
  198. #region 添加角色
  199. String uuid = System.Guid.NewGuid().ToString("N");
  200. string commandText = "INSERT INTO CM_User (UserId," +
  201. "OrgId,LoginName,UserName,IsActive)" +
  202. " VALUES ('" + uuid + "',@OrgId,@LoginName,@UserName,@IsActive)";
  203. string result;
  204. //准备参数
  205. List<List<Object>> parameters = new List<List<Object>>();
  206. parameters.Add(new List<Object>() { "UserId", uuid });
  207. parameters.Add(new List<Object>() { "OrgId", OrgId });
  208. parameters.Add(new List<Object>() { "LoginName", LoginName });
  209. parameters.Add(new List<Object>() { "UserName", UserName });
  210. parameters.Add(new List<Object>() { "IsActive", IsActive });
  211. List<SqlParameter> parameters1 = DataAccess.ToParameters(parameters);
  212. int success = DataAccess.ExecuteCommand(commandText, parameters1, out result);
  213. #endregion
  214. //#region 添加角色
  215. //int id = BaseDAL.GetId(Config.TablePrefix + "Permission");
  216. //UserDAL.SaveUsersRelatePermission(id, UserIdis);
  217. //#endregion
  218. return Json(new
  219. {
  220. success = success
  221. });
  222. }
  223. /// <summary>
  224. /// 修改用户
  225. /// </summary>
  226. /// <param name="UserId"></param>
  227. /// <param name="UserCode"></param>
  228. /// <param name="UserName"></param>
  229. /// <param name="data"></param>
  230. /// <returns></returns>
  231. [AuthPermission]
  232. [HttpPost, Route("edit")]
  233. public JsonResult editp(String UserId, String OrgId,
  234. string LoginName, [FromBody]dynamic data)
  235. {
  236. string UserName = "";
  237. string IsActive = "";
  238. if (data != null)
  239. {
  240. UserId = data.UserId;
  241. OrgId = data.OrgId;
  242. LoginName = data.LoginName;
  243. UserName = data.UserName;
  244. IsActive = data.IsActive;
  245. }
  246. #region 编辑用户
  247. string commandText = "UPDATE CM_User " +
  248. " SET OrgId = @OrgId,LoginName = @LoginName,UserName = @UserName,IsActive = @IsActive " +
  249. "WHERE UserId= @UserId";
  250. string result;
  251. //准备参数
  252. List<List<Object>> parameters = new List<List<Object>>();
  253. parameters.Add(new List<Object>() { "UserId", UserId });
  254. parameters.Add(new List<Object>() { "OrgId", OrgId });
  255. parameters.Add(new List<Object>() { "LoginName", LoginName });
  256. parameters.Add(new List<Object>() { "UserName", UserName });
  257. parameters.Add(new List<Object>() { "IsActive", IsActive });
  258. List<SqlParameter> parameters1 = DataAccess.ToParameters(parameters);
  259. int success = DataAccess.ExecuteCommand(commandText, parameters1, out result);
  260. #endregion
  261. return Json(new
  262. {
  263. success = success
  264. });
  265. }
  266. /// <summary>
  267. /// 删除用户
  268. /// </summary>
  269. /// <param name="id"></param>
  270. /// <returns></returns>
  271. [AuthPermission]
  272. [HttpPost, Route("delete")]
  273. public ActionResult Delete(string id)
  274. {
  275. string commandText = "DELETE FROM CM_User WHERE UserId = @UserId";
  276. //准备参数
  277. List<List<Object>> parameters = new List<List<Object>>();
  278. parameters.Add(new List<Object>() { "UserId", id });
  279. string result = "";
  280. List<SqlParameter> parameters1 = DataAccess.ToParameters(parameters);
  281. int success = DataAccess.ExecuteCommand(commandText, parameters1, out result);
  282. return Content("{success:" + success + "}");
  283. }
  284. [HttpGet, Route("Users")]
  285. public ActionResult Users()
  286. {
  287. var permss = GetTopUsers();
  288. IList<Option> options = new List<Option>();
  289. foreach (CM_User perms in permss)
  290. {
  291. var option = new Option() { label = perms.UserName, value = perms.UserId.ToString() };
  292. options.Add(option);
  293. }
  294. var jsonData = JsonConvert.SerializeObject(options);
  295. return Content(jsonData);
  296. }
  297. public static IList<CM_User> GetUsers()
  298. {
  299. IList<CM_User> UserList = new List<CM_User>(0);
  300. DataTable result;
  301. string direct = " desc ";
  302. result = DataAccess.GetDataTable("CM_User", "Id", "UserId,UserName,UserCode", "", "", "Id" + direct, 1, 100, out var msg);
  303. if (result != null && result.Rows.Count > 0)
  304. {
  305. // 把DataTable转换为IList<User>
  306. UserList = ModelConvertHelper<CM_User>.ConvertToModel(result);
  307. }
  308. return UserList;
  309. }
  310. public IList<CM_User> GetTopUsers()
  311. {
  312. IList<CM_User> permsList = new List<CM_User>(0);
  313. DataTable dt = new DataTable();
  314. string result = string.Empty;
  315. var sortDirection = 1;
  316. var pageIndex = 1;
  317. var pageSize = Config.MaxPageSize;
  318. string direct = " desc ";
  319. if (sortDirection != 1)
  320. direct = " asc";
  321. int start = (pageIndex - 1) * pageSize;
  322. int end = (start + 1 + pageSize);
  323. string commandText = "select * from (" +
  324. "select A.*, row_number() over" +
  325. "( order by A.UserId " + direct + " ) as rownum" +
  326. " from CM_User A ) AAA" +
  327. " where AAA.rownum>" + start + " and AAA.rownum<" + end +
  328. " ";
  329. List<List<Object>> parameters1 = new List<List<Object>>();
  330. parameters1.Add(new List<Object>() { "UserId", 0 });
  331. bool result1 = DataAccess.GetValues(commandText, ref dt, DataAccess.ToParameters(parameters1).ToArray(), out result);
  332. if (result1 && dt.Rows.Count > 0)
  333. {
  334. // 把DataTable转换为IList<User>
  335. permsList = ModelConvertHelper<CM_User>.ConvertToModel(dt);
  336. }
  337. return permsList;
  338. }
  339. }
  340. }