using System; using System.Collections.Generic; using System.IO; using System.Linq; using System.Security.Cryptography; using System.Text; using System.Threading.Tasks; using Bowin.Common.ServiceToken.Permission; using Bowin.Common.WebModels; using YLShipBuildLandMap.Entity; using YLShipBuildLandMap.Entity.ViewModel; using YLShipBuildLandMap.Entity.ViewModel.SystemSetting; using YLShipBuildLandMap.Services.SystemSetting; using Microsoft.AspNetCore.Authorization; using Microsoft.AspNetCore.Http; using Microsoft.AspNetCore.Mvc; using Newtonsoft.Json.Linq; using Bowin.Common.Data; using System.Data; using Bowin.Common.Office; namespace YLShipBuildLandMap.Web.Controllers.SystemSetting { [Route("api/systemsetting/[controller]/[action]")] [ApiController] [Authorize] public class UserController : ControllerBase { private IRoleService RoleService { get; set; } private IUserService UserService { get; set; } public UserController(IRoleService roleService, IUserService userService) { RoleService = roleService; UserService = userService; } [HttpPost] [FunctionCode("9903")] public ResultMessage GetUserList([FromBody] dynamic inputObj) { try { string userName = inputObj.userName; Guid? roleId = inputObj.RoleID; int? RecordStatus = inputObj.recordStatus; int? pageIndex = inputObj.pageIndex; int? pageSize = inputObj.pageSize; var userList = UserService.GetUserViewList(userName, roleId,RecordStatus,null, pageIndex, pageSize); return ResultMessage.Success(userList); } catch (Exception ex) { return ResultMessage.GetError(ex.Message); } } [HttpPost] [FunctionCode("9903")] public ResultMessage GetUser([FromBody] dynamic inputObj) { try { Guid userID = inputObj.userID; var user = UserService.GetUserView(userID); return ResultMessage.Success(user); } catch (Exception ex) { return ResultMessage.GetError(ex.Message); } } [HttpPost] [FunctionCode("990301")] public async Task Add([FromBody] JObject inputObj) { try { UserView user = inputObj["user"].ToObject(); UserView result = UserService.GetUserViewByName(user.LoginID, user.Name); if (result != null) { return ResultMessage.GetError("用户名或账号已存在"); } await UserService.Save(user, LoginUser.Current.UserID); return ResultMessage.Success(); } catch (Exception ex) { return ResultMessage.GetError(ex.Message); } } [HttpPost] [FunctionCode("990302")] public async Task Edit([FromBody] dynamic inputObj) { try { UserView user = inputObj["user"].ToObject(); if (!user.UserID.HasValue) { throw new Exception("实体ID不能为空。"); } await UserService.Save(user, LoginUser.Current.UserID); return ResultMessage.Success(); } catch (Exception ex) { return ResultMessage.GetError(ex.Message); } } [HttpPost] [FunctionCode("990303")] public async Task Delete([FromBody] dynamic inputObj) { try { List userIDList = inputObj.userIDs.ToObject>(); await UserService.Delete(userIDList); return ResultMessage.Success(); } catch (Exception ex) { return ResultMessage.GetError(ex.Message); } } [HttpGet] [FunctionCode("9903")] public ResultMessage GetUserListByUserName(string userName) { try { var userList = UserService.GetUserViewList(userName,null, null, null); var resultList = userList.rows.AsEnumerable().Select(x => new { loginId = x.LoginID, name = x.Name, id = x.UserID }).ToList(); return ResultMessage.Success(resultList); } catch (Exception ex) { return ResultMessage.GetError(ex.Message); } } [HttpPost] public async Task UpdatePwd([FromBody] dynamic inputObj) { string newPassword = inputObj.newPassword; await UserService.UpdatePwd(LoginUser.Current.UserID, newPassword); return ResultMessage.Success(); } [HttpGet] [FunctionCode("990304")] public async Task ResetPwd(Guid Id) { await UserService.ResetPwd(Id, LoginUser.Current.UserID); return ResultMessage.Success(); } [HttpPost] public ResultMessage GetUserRoleList([FromBody] dynamic inputObj) { try { var list= UserService.GetUserRoleList(); return ResultMessage.Success(list); } catch (Exception ex) { throw ex; } } [HttpPost] [FunctionCode("990305")] public ResultMessage ExportPort([FromBody] dynamic inputObj) { string userName = inputObj.userName; Guid? roleId = inputObj.RoleID; int? RecordStatus = inputObj.recordStatus; int? pageIndex = inputObj.pageIndex; int? pageSize = inputObj.pageSize; string userId = inputObj.userId; List useridList = !string.IsNullOrEmpty(userId) ? userId.Split(",").Where(s => !string.IsNullOrEmpty(s)).Select(s => Guid.Parse(s)).ToList() : null; var dataList = (UserService.GetUserViewList(userName, roleId, RecordStatus, useridList, pageIndex, pageSize).rows. Select((r, i) => new { index = (i + 1), r.Name, r.LoginID, r.CmWorkLinkAccountPsid, r.RoleNames, RecordStatus = r.RecordStatus == 1 ? "启用" : "停用" })).ToTable(); dataList.Columns["index"].ColumnName = "序号"; dataList.Columns["Name"].ColumnName = "用户名"; dataList.Columns["LoginID"].ColumnName = "账号"; dataList.Columns["CmWorkLinkAccountPsid"].ColumnName = "集团主账号"; dataList.Columns["RoleNames"].ColumnName = "用户角色"; dataList.Columns["RecordStatus"].ColumnName = "状态"; List headerList = new List(); foreach (DataColumn column in dataList.Columns) { headerList.Add(column.ColumnName); } AsposeExcelHelper excel = new AsposeExcelHelper(); var fileName = excel.ExportToFile(dataList, headerList.ToArray(), Configuration.Current.AppSettings.TemplatePhysicalPath); return ResultMessage.Success(fileName); } } }