using JCSoft.WX.Framework.Api; using Microsoft.AspNetCore.Mvc; using Microsoft.AspNetCore.Http; using System; using System.Collections.Generic; using System.Linq; using System.Threading.Tasks; using Common.Wechat; using Common; using System.Data; using Common.Model; using ZcPeng.PublicLibrary; using CoreEntity.Entity; using CoreEntity.DAL; using System.Collections.Concurrent; using Newtonsoft.Json.Converters; using Newtonsoft.Json; using System.Data.SqlClient; using Microsoft.Extensions.Primitives; using Microsoft.Extensions.Caching.Memory; using Jwt; using PublicLibrary.Model; using Newtonsoft.Json.Linq; using SupplierWeb.Codes.mvc; using SupplierWeb.Codes.Auth; using Common.Config; namespace SupplierWeb.Controllers { [Route("web/Role")] public class RoleController : BaseController { public RoleController(IMemoryCache cache, IApiClient client) : base(cache, client) { } private ConcurrentDictionary roleMap = new ConcurrentDictionary(); public class TempTableResult { public int id { get; set; } public string permissionName { get; set; } public string permissionUrl { get; set; } public int Id { get; set; } public int checkedValue { get; set; } public string permissionRule { get; set; } public string permissionRuleType { get; set; } public int relateId { get; set; } public string AuthType { get; set; } } public class TreeNode { public string parentValue { get; set; } public string label { get; set; } public int value { get; set; } } public class TempQuery { public int[] data { get; set; } public int Id { get; set; } } public class SortContent { public Object content { get; set; }//权限内容 public string code { get; set; }//权限id public int sort { get; set; }//权限序号 } /// /// 角色列表 /// /// /// /// /// /// /// /// /// [AuthPermission] [HttpPost, Route("index")] public JsonResult Indexp(QueryFilter[] filters, Int32 pageIndex, Int32 pageSize, string sortField, Int32 sortDirection, string[] sumFields, [FromBody]dynamic data) { //取出参数 if (data != null) { filters = data.filters.ToObject(); pageIndex = data.pageIndex; pageSize = data.pageSize; sortField = data.sortField; sortDirection = data.sortDirection; sumFields = data.sumFields.ToObject(); } #region 获取角色列表 DataTable dt = new DataTable(); string result; IList permss = new List(0); List parameters = new List(); string filterstr = QueryFilter.getFilterSqlParam(filters, out parameters, new Role(), "A."); string direct = " desc "; if (sortDirection != 1) { direct = " asc"; } int start = (pageIndex - 1) * pageSize; int end = (start + 1 + pageSize); string commandText0 = "select * from "; string commandText1 = "(" + "select A.*,row_number() over" + "( order by A.Id " + direct + " ) as rownum from " + Config.TablePrefix+"Role as A " + " where 1=1 " + //" and A.CreationPerson = '" + userids + "'" + filterstr + ")AAA "; string commandText2 = " where AAA.rownum>" + start + " and AAA.rownum<" + end; string commandText3 = commandText0 + commandText1 + commandText2; bool success = DataAccess.GetValues(commandText3, ref dt, parameters.ToArray(), out result); #endregion if (dt != null && dt.Rows.Count > 0) { // 把DataTable转换为IList permss = ModelConvertHelper.ConvertToModel(dt); // #region 获取权限的角色列表,角色列表 // //角色列表 // IList roles = GetRoles(); // // 把DataTable转换为IList // if (roleMap.Count < roles.Count) // { // foreach (Role role in roles) // { // roleMap.TryAdd(role.Id.ToString(), role); // } // } // #region // //foreach (Role perms in permss) // //{ // // string direct1 = " desc "; // // dt = DataAccess.GetDataTable("Role", "Id", "*", "Id=" + perms.Id, "", "Id" + direct1, 1, 100, out var msg); // // // 把DataTable转换为IList // // if (dt != null && dt.Rows.Count > 0) // // { // // IList permRelates = ModelConvertHelper.ConvertToModel(dt); // // List listRoles = new List(); // // List superior_list = new List(0); // // foreach (Role permsr in permRelates) // // { // // Role role; // // if (roleMap.TryGetValue(permsr.Id.ToString(), out role)) // // { // // listRoles.Add(role); // // } // // superior_list.Add(Convert.ToInt32(permsr.Id)); // // } // // //perms.RoleList = listRoles; // // perms.Id = Convert.ToInt32(string.Join(",", superior_list.ToArray())); // // } // //} // //#endregion // #endregion } string result1; long totalcount = DataAccess.GetRowCountDefine("select count(Id) from " + commandText1, parameters.ToArray(), out result1); IsoDateTimeConverter timejson = new IsoDateTimeConverter { DateTimeFormat = "yyyy'-'MM'-'dd' 'HH':'mm':'ss" }; //IList menus = Permission.Convert(permss); var jsonData = JsonConvert.SerializeObject(permss, timejson); return Json(new { items = JsonConvert.DeserializeObject(jsonData), sum = new { }, totalCount = totalcount }); } /// /// 新增角色 /// /// /// /// /// /// [AuthPermission] [HttpPost, Route("add")] public JsonResult Addp( String Id, String RoleCode, string RoleName, [FromBody]dynamic data ) { int RoleDepartmentId = 0; string RoleRemark = ""; int IsDelete = 0; int CreationPerson = 0; DateTime CreationDate = DateTime.Now; int LastModifiedPerson = 0; DateTime LastModified = DateTime.Now; if (data != null) { Id = data.id; RoleName = data.RoleName; //RoleDepartmentId = data.RoleDepartmentId; RoleRemark = data.RoleRemark; if (data.IsDelete != null) { IsDelete = data.IsDelete; } if (data.CreationPerson != null) { CreationPerson = data.CreationPerson; } if (data.CreationDate != null) { CreationDate = data.CreationDate; } if (data.LastModifiedPerson != null) { LastModifiedPerson = data.LastModifiedPerson; } if (data.LastModified != null) {LastModified = data.LastModified; } } #region 添加角色 object rid = ""; string sql = "SELECT MAX(Id) AS ID from " + Config.TablePrefix + "role"; string result2 = ""; List parameters3 = new List(); bool success1 = DataAccess.GetOneValue(sql,parameters3.ToArray(),out rid,out result2); RoleDepartmentId = Convert.ToInt32(rid.ToString()); //String uuid = System.Guid.NewGuid().ToString("N"); string commandText = "INSERT INTO " + Config.TablePrefix + "ROLE (" + "RoleName,RoleDepartmentId,RoleRemark,IsDelete,CreationPerson," + "CreationDate,LastModifiedPerson,LastModified)" + " VALUES (@RoleName,@RoleDepartmentId,@RoleRemark,@IsDelete," + "@CreationPerson,@CreationDate,@LastModifiedPerson,@LastModified)"; string result; //准备参数 List> parameters = new List>(); //parameters.Add(new List() { "Id", rrid }); parameters.Add(new List() { "RoleName", RoleName }); parameters.Add(new List() { "RoleDepartmentId", RoleDepartmentId }); parameters.Add(new List() { "RoleRemark", RoleRemark }); parameters.Add(new List() { "IsDelete", IsDelete }); parameters.Add(new List() { "CreationPerson", CreationPerson }); parameters.Add(new List() { "CreationDate", CreationDate }); parameters.Add(new List() { "LastModifiedPerson", LastModifiedPerson }); parameters.Add(new List() { "LastModified", LastModified }); List parameters1 = DataAccess.ToParameters(parameters); int success = DataAccess.ExecuteCommand(commandText, parameters1, out result); #endregion return Json(new { success = success }); } /// /// 修改角色 /// /// /// /// /// /// [AuthPermission] [HttpPost, Route("edit")] public JsonResult editp(String Id, String RoleCode, string RoleName, [FromBody]dynamic data) { int RoleDepartmentId = 0; string RoleRemark = ""; int IsDelete = 0; int CreationPerson = 0; DateTime CreationDate = DateTime.Now; int LastModifiedPerson = 0; DateTime LastModified = DateTime.Now; if (data != null) { Id = data.Id; RoleName = data.RoleName; RoleDepartmentId = data.RoleDepartmentId; RoleRemark = data.RoleRemark; if (data.IsDelete != null) { IsDelete = data.IsDelete; } if (data.CreationPerson != null) { CreationPerson = data.CreationPerson; } if (data.CreationDate != null) { CreationDate = data.CreationDate; } } #region 编辑用户 string commandText = "UPDATE "+Config.TablePrefix+"Role " + " SET RoleName = @RoleName,RoleDepartmentId = @RoleDepartmentId,RoleRemark = @RoleRemark," + "IsDelete = @IsDelete," + //"CreationPerson = @CreationPerson," + //"CreationDate = @CreationDate," + "LastModifiedPerson = @LastModifiedPerson,LastModified = @LastModified " + "WHERE Id= @Id"; string result; //准备参数 List> parameters = new List>(); parameters.Add(new List() { "Id", Id }); parameters.Add(new List() { "RoleName", RoleName }); parameters.Add(new List() { "RoleDepartmentId", RoleDepartmentId }); parameters.Add(new List() { "RoleRemark", RoleRemark }); parameters.Add(new List() { "IsDelete", IsDelete }); //parameters.Add(new List() { "CreationPerson", CreationPerson }); //parameters.Add(new List() { "CreationDate", CreationDate }); parameters.Add(new List() { "LastModifiedPerson", LastModifiedPerson }); parameters.Add(new List() { "LastModified", LastModified }); List parameters1 = DataAccess.ToParameters(parameters); int success = DataAccess.ExecuteCommand(commandText, parameters1, out result); #endregion return Json(new { success = success }); } /// /// 删除角色 /// /// /// [AuthPermission] [HttpPost, Route("delete")] public ActionResult Delete(string Id) { //准备SQL语句 string commandText = "DELETE FROM " + Config.TablePrefix + "Role WHERE Id = @Id"; //准备参数 List> parameters = new List>(); parameters.Add(new List() { "Id", Id }); string result = ""; //转换参数 List parameters1 = DataAccess.ToParameters(parameters); //执行并返回结果 int success = DataAccess.ExecuteCommand(commandText, parameters1, out result); return Content("{success:" + success + "}"); } [HttpGet, Route("roles")] public ActionResult Roles() { var permss = GetTopRoles(); IList