| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727 |
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Reflection;
- using System.Security.Claims;
- using System.Threading.Tasks;
- using MadRunFabric.Common;
- using MadRunFabric.Model;
- using Microsoft.AspNetCore.Authorization;
- using Microsoft.AspNetCore.Mvc;
- using Microsoft.Extensions.Configuration;
- using Microsoft.Extensions.Logging;
- using MongoDB.Driver;
- using NPOI.SS.Formula.Functions;
- using SignTokenApi.IRepositories;
- using SignTokenApi.Model.Dto;
- using SignTokenApi.Model.Input;
- namespace SignTokenApi.Controllers
- {
- [Authorize]
- [ApiVersion("6.0")]
- [Produces("application/json")]
- [Route("api/[controller]")]
- public class UserAccountController : BaseController
- {
- private readonly IConfiguration _configuration;
- private readonly ILogger<UserAccountController> _logger;
- private readonly ISys_User_AccountRepository _sys_user_accountRepository;
- public UserAccountController(IConfiguration configuration, ILogger<UserAccountController> logger, ISys_User_AccountRepository sys_user_accountRepository)
- {
- _configuration = configuration;
- _logger = logger;
- _sys_user_accountRepository = sys_user_accountRepository;
- }
- /// <summary>
- /// 用户信息列表 by page
- /// </summary>
- /// <param name="pageindex">当前页</param>
- /// <param name="pagesize">每页条数</param>
- /// <returns></returns>
- [HttpGet("getlistbypage")]
- public async Task<IActionResult> GetListByPageAsync(string keyword, string deptid, string roleid, string projectid, int type=-1, int pageindex = 1, int pagesize = 10)
- {
- #region 条件信息
- ////排序字段
- var sort = Builders<Sys_User_Account>.Sort.Descending("create_time");
- //根据条件查询集合
- var listFilter = new List<FilterDefinition<Sys_User_Account>>();
- listFilter.Add(Builders<Sys_User_Account>.Filter.Eq("delete_flag", false));
- listFilter.Add(Builders<Sys_User_Account>.Filter.Where(s => s.type !=2));
- //查询
- if (type!=-1)
- listFilter.Add(Builders<Sys_User_Account>.Filter.Where(s => s.type.Equals(type)));
- if (!string.IsNullOrEmpty(deptid))
- listFilter.Add(Builders<Sys_User_Account>.Filter.Where(s => s.dept_id.Equals(deptid)));
- if (!string.IsNullOrEmpty(roleid))
- listFilter.Add(Builders<Sys_User_Account>.Filter.Where(s => s.role_id.Equals(roleid)));
- if (!string.IsNullOrEmpty(projectid))
- listFilter.Add(Builders<Sys_User_Account>.Filter.Where(s => s.projectlist.Contains(projectid)));
- if (!string.IsNullOrEmpty(keyword))
- listFilter.Add(Builders<Sys_User_Account>.Filter.Where(s => s.usercode.Contains(keyword) || s.username.Contains(keyword) || s.mobile.Contains(keyword) || s.telephone.Contains(keyword)));
- #endregion
- var filter = Builders<Sys_User_Account>.Filter.And(listFilter);
- var list = await _sys_user_accountRepository.GetByPage(filter, pageindex, pagesize, sort);
- var redCount = await _sys_user_accountRepository.CountAsync(filter); //获取总数
- var obj = new
- {
- rows = list,
- total = redCount,
- };
- return Success("获取成功", obj);
- }
- /// <summary>
- /// 设备用户列表 by page - 关联查询
- /// </summary>
- /// <param name="pageindex">当前页</param>
- /// <param name="pagesize">每页条数</param>
- /// <returns></returns>
- [HttpGet("getlistsbypage")]
- public IActionResult GetListsByPage(string keyword, string deptid, string roleid, string projectid, int type = -1, int pageindex = 1, int pagesize = 10)
- {
- string allprojectrole = _configuration["allprojectrole"];
- int recordCount = 0;
- var result = _sys_user_accountRepository.GetListsByPage(keyword, deptid, roleid, projectid, type, allprojectrole, pageindex, pagesize, out recordCount);
- var obj = new
- {
- rows = result,
- total = recordCount,
- };
- return Success("获取成功", obj);
- }
- /// <summary>
- /// 获取用户列表 - 关联查询
- /// </summary>
- /// <returns></returns>
- [HttpGet("getapplists")]
- public IActionResult GetAppLists(string keyword, string deptid, string roleid, string projectid, int type = -1)
- {
- string allprojectrole = _configuration["allprojectrole"];
- var result = _sys_user_accountRepository.GetAppLists(keyword, deptid, roleid, projectid, type, allprojectrole);
-
- return Success("获取成功", result);
- }
- /// <summary>
- /// 获取用户详情 by id
- /// </summary>
- /// <param name="id">设备id</param>
- /// <returns></returns>
- [HttpGet("getdetailes")]
- public async Task<IActionResult> GetDetailsAsync(string id)
- {
- if (string.IsNullOrEmpty(id))
- return Error("参数错误");
- var model = await _sys_user_accountRepository.GetSingle(id);
- if (model != null)
- {
- return Success("获取成功!", model);
- }
- return Error("获取失败");
- }
- /// <summary>
- /// 获取用户详情 by id - 关联查询
- /// </summary>
- /// <param name="id"></param>
- /// <returns></returns>
- [HttpGet("getdetails")]
- public IActionResult GetDetails(string id)
- {
- if (string.IsNullOrEmpty(id))
- return Error("参数错误");
- var model = _sys_user_accountRepository.GetDetails(id);
- if (model != null)
- {
- return Success("获取成功!", model);
- }
- return Error("获取失败");
- }
- /// <summary>
- /// 添加用户信息
- /// </summary>
- /// <param name="input">用户信息参数</param>
- /// <returns></returns>
- [HttpPost("add")]
- public async Task<IActionResult> Add(UserAccountInput input)
- {
- #region 验证判断
- if (string.IsNullOrEmpty(input.usercode))
- return Error("账号不能为空");
- if (string.IsNullOrEmpty(input.password))
- return Error("密码不能为空");
- if (string.IsNullOrEmpty(input.mobile))
- return Error("手机号码不能为空");
- var modelold = new Sys_User_Account();
- modelold = await _sys_user_accountRepository.GetSingle(p => p.usercode == input.usercode && p.delete_flag == false);
- if (modelold != null)
- {
- return Error("账号已经存在");
- }
- #endregion
- var model = new Sys_User_Account();
- model.usercode = input.usercode;
- model.username = input.username;
- model.type = input.type;
- model.password = input.password;
- model.sex = input.sex;
- model.birthday = input.birthday;
- model.call_type = input.call_type;
- model.delete_flag = false;
- model.dept_id = input.dept_id;
- model.extensionnumber = input.extensionnumber;
- model.group = input.group;
- model.groupid = input.groupid;
- model.head_img = input.head_img;
- model.head_small_img = input.head_small_img;
- model.homephone = input.homephone;
- model.lock_flag = false;
- model.mail = input.mail;
- model.mobile = input.mobile;
- model.qq = input.qq;
- model.remark = input.remark;
- model.role_id = input.role_id;
- model.seat_flag = input.seat_flag;
- model.seat_level = input.seat_level;
- model.seat_right = input.seat_right;
- model.see_flag = input.see_flag;
- model.sina = input.sina;
- model.team_id = input.team_id;
- model.telephone = input.telephone;
- model.idcardno = input.idcardno;
- model.entrytime = input.entrytime;
- model.transfertime = input.transfertime;
- model.quittime = input.quittime;
- if (input.idcard != null)
- {
- model.idcard = input.idcard;
- }
- else
- {
- model.idcard = new List<FileBaseModel>();
- }
- if (input.certificate != null)
- {
- model.certificate = input.certificate;
- }
- else
- {
- model.certificate = new List<FileBaseModel>();
- }
- if (input.postlist != null)
- {
- model.postlist = input.postlist;
- }
- else
- {
- model.postlist = new List<string>();
- }
- if (input.projectlist != null)
- {
- model.projectlist = input.projectlist;
- }
- else
- {
- model.projectlist = new List<string>();
- }
- model.create_user = User.Claims.FirstOrDefault(c => c.Type == ClaimTypes.Sid).Value;
- bool b = await _sys_user_accountRepository.Add(model);
- if (b)
- return Success("添加成功");
- //日志
- _logger.LogError($"账号:{input.usercode}添加失败");
- return Error("添加失败");
- }
- /// <summary>
- /// 修改用户信息
- /// </summary>
- /// <param name="input"></param>
- /// <returns></returns>
- [HttpPost("update")]
- public async Task<IActionResult> Update(UserAccountInput input)
- {
- #region 验证判断
- if (string.IsNullOrEmpty(input.usercode))
- return Error("账号不能为空");
- //if (string.IsNullOrEmpty(input.password))
- // return Error("密码不能为空");
- var modelold = new Sys_User_Account();
- modelold = await _sys_user_accountRepository.GetSingle(p => p.usercode == input.usercode && p.id!= input.id && p.delete_flag == false);
- if (modelold != null)
- {
- return Error("账号已经存在");
- }
- #endregion
- var model = new Sys_User_Account();
- model = await _sys_user_accountRepository.GetSingle(input.id);
- if (model == null)
- return Error("操作失败");
- model.usercode = input.usercode;
- model.username = input.username;
- model.type = input.type;
- //model.password = input.password;
- model.sex = input.sex;
- model.birthday = input.birthday;
- model.call_type = input.call_type;
- //model.dept_id = input.dept_id;
- model.extensionnumber = input.extensionnumber;
- model.group = input.group;
- model.groupid = input.groupid;
- model.head_img = input.head_img;
- model.head_small_img = input.head_small_img;
- model.homephone = input.homephone;
- model.mail = input.mail;
- model.mobile = input.mobile;
- model.qq = input.qq;
- model.remark = input.remark;
- model.role_id = input.role_id;
- model.seat_flag = input.seat_flag;
- model.seat_level = input.seat_level;
- model.seat_right = input.seat_right;
- model.see_flag = input.see_flag;
- model.sina = input.sina;
- model.team_id = input.team_id;
- model.telephone = input.telephone;
- model.idcardno = input.idcardno;
- model.entrytime = input.entrytime;
- model.transfertime = input.transfertime;
- model.quittime = input.quittime;
- if (input.idcard != null)
- {
- model.idcard = input.idcard;
- }
- else
- {
- model.idcard = new List<FileBaseModel>();
- }
- if (input.certificate != null)
- {
- model.certificate = input.certificate;
- }
- else
- {
- model.certificate = new List<FileBaseModel>();
- }
- if (input.postlist != null)
- {
- model.postlist = input.postlist;
- }
- //else
- //{
- // model.postlist = new List<string>();
- //}
- if (input.projectlist != null)
- {
- model.projectlist = input.projectlist;
- }
- else
- {
- model.projectlist = new List<string>();
- }
- bool b = await _sys_user_accountRepository.UpdateOne(model);
- if (b)
- return Success("保存成功");
- //日志
- _logger.LogError($"账号:{input.usercode}修改失败");
- return Error("保存失败");
- }
- /// <summary>
- /// 逻辑删除用户
- /// </summary>
- /// <param name="ids"></param>
- /// <returns></returns>
- [HttpPost("delete")]
- public async Task<IActionResult> Delete(string[] ids)
- {
- string usercode = User.Claims.FirstOrDefault(c => c.Type == ClaimTypes.Sid).Value;
- //使用逻辑删除
- //物理删除的数据无法恢复
- if (ids != null && ids.Length > 0)
- {
- foreach (var item in ids)
- {
- var result = _sys_user_accountRepository.Remove(item);
- //var eq = await _sys_user_accountRepository.GetSingle(item);
- //if (eq != null)
- //{
- // eq.delete_flag = true;
- // eq.delete_user = usercode;
- // eq.delete_time = DateTime.Now.ToLocalTime();
- // bool bl = await _sys_user_accountRepository.UpdateOne(eq);
- // //if (bl)
- // //{
- // // var data = await _sys_user_dataRepository.GetSingle(p => p.usercode == eq.usercode);
- // // if (data != null)
- // // {
- // // await _sys_user_dataRepository.UpdateUserPost(data.usercode, "", usercode);
- // // await _sys_user_dataRepository.UpdateUserProject(data.usercode, "", usercode);
- // // data.isdelete = 1;
- // // data.deleteby = usercode;
- // // data.deletetime = DateTime.Now.ToLocalTime();
- // // await _sys_user_dataRepository.Update(data);
- // // }
- // //}
- //}
- }
- return Success("删除成功");
- }
- else
- return Error("请选择要删除的记录");
- }
- /// <summary>
- /// 修改用户密码
- /// </summary>
- /// <param name="input"></param>
- /// <returns></returns>
- [HttpPost("updatepassword")]
- public async Task<IActionResult> UpdatePassword(string usercode, string password, string surepassword)
- {
- #region 验证判断
- if (string.IsNullOrEmpty(usercode))
- return Error("请选择账号");
- if (string.IsNullOrEmpty(password))
- return Error("密码不能为空");
- if (string.IsNullOrEmpty(surepassword))
- return Error("确认密码不能为空");
- if (password!=surepassword)
- return Error("两次输入的密码不一致");
- #endregion
- var model = await _sys_user_accountRepository.GetSingle(p => p.usercode == usercode);
- if (model == null)
- return Error("操作失败");
- model.password = password;
- bool b = await _sys_user_accountRepository.UpdateOne(model);
- if (b)
- return Success("保存成功");
- return Error("保存失败");
- }
- /// <summary>
- /// 修改我的密码
- /// </summary>
- /// <param name="input"></param>
- /// <returns></returns>
- [HttpPost("updatemypassword")]
- public async Task<IActionResult> UpdateMyPassword(string oldpassword,string password, string surepassword)
- {
- #region 验证判断
- if (string.IsNullOrEmpty(oldpassword))
- return Error("原始密码不能为空");
- if (string.IsNullOrEmpty(surepassword))
- return Error("确认密码不能为空");
- if (password != surepassword)
- return Error("两次输入的密码不一致");
- #endregion
- var usercode= User.Claims.FirstOrDefault(c => c.Type == ClaimTypes.Sid).Value;
- var model = await _sys_user_accountRepository.GetSingle(p => p.usercode == usercode && p.password==oldpassword);
- if (model == null)
- return Error("原始密码输入错误");
- model.password = password;
- bool b = await _sys_user_accountRepository.UpdateOne(model);
- if (b)
- return Success("更新成功");
- return Error("更新失败");
- }
- /// <summary>
- /// 获取当前用户信息
- /// </summary>
- /// <returns></returns>
- [HttpGet("getnowuser")]
- public IActionResult GetUserInfo()
- {
- string allprojectrole = _configuration["allprojectrole"];
- string nowusercode = User.Claims.FirstOrDefault(c => c.Type == ClaimTypes.Sid).Value;
- var model = _sys_user_accountRepository.GetUserInfo(nowusercode, allprojectrole);
- if (model != null)
- {
- return Success("获取成功!", model);
- }
- return NoToken("获取失败");
- }
- /// <summary>
- /// 获取用户详情 by id - 关联查询
- /// </summary>
- /// <param name="id"></param>
- /// <returns></returns>
- [AllowAnonymous]
- [HttpGet("getuserinfo")]
- public IActionResult GetUserInfo(string usercode)
- {
- string allprojectrole = _configuration["allprojectrole"];
- var model = _sys_user_accountRepository.GetUserInfo(usercode, allprojectrole);
- if (model != null)
- {
- return Success("获取成功!", model);
- }
- return Error("获取失败");
- }
- //2018-7-21获取坐席实时状态使用
- [HttpGet("getlist")]
- public async Task<IActionResult> GetListsAsync(string groupid, string agentid)
- {
- //根据条件查询集合
- var list = new List<FilterDefinition<Sys_User_Account>>();
- list.Add(Builders<Sys_User_Account>.Filter.Eq("seat_flag", true));
- list.Add(Builders<Sys_User_Account>.Filter.Eq("delete_flag", false));
- if (groupid != null)
- list.Add(Builders<Sys_User_Account>.Filter.Where(s => s.group.Contains(groupid)));
- if (agentid != null)
- list.Add(Builders<Sys_User_Account>.Filter.Where(s => s.usercode.Contains(agentid)));
- var filter = Builders<Sys_User_Account>.Filter.And(list);
- var agentlist = await _sys_user_accountRepository.Get(filter, null, null);
- #region
- System.Data.DataTable dt = new System.Data.DataTable();
- dt.Columns.Add("groupid");
- dt.Columns.Add("agentid");
- //dt.Columns.Add("group");
- foreach (Object obj in agentlist)
- {
- if (obj is Sys_User_Account)//这个是类型的判断,类或结构
- {
- Sys_User_Account s = (Sys_User_Account)obj;
- System.Data.DataRow dr = dt.NewRow();
- dr["groupid"] = s.groupid;
- dr["agentid"] = s.usercode;
- //dr["group"] = s.group;
- dt.Rows.Add(dr);
- }
- }
- //var dtlist = new
- //{
- // rows=dt
- //};
- #endregion
- return Success("根据条件获取坐席id数据成功", dt);
- }
- /// <summary>
- /// 获取某项目某角色人员
- /// </summary>
- /// <returns></returns>
- [HttpGet("getprojectuser")]
- public IActionResult GetProjectUser(string projectid, string rolecode = "YWYG")
- {
- var result = _sys_user_accountRepository.GetProjectUser(projectid, rolecode);
- return Success("获取成功", result);
- }
- /// <summary>
- /// 获取坐席列表
- /// </summary>
- /// <param name="key"></param>
- /// <returns></returns>
- [HttpGet("getseatlist")]
- public async Task<IActionResult> GetSeatListsAsync(string key)
- {
- //根据条件查询集合
- var list = new List<FilterDefinition<Sys_User_Account>>();
- list.Add(Builders<Sys_User_Account>.Filter.Eq("seat_flag", true));
- list.Add(Builders<Sys_User_Account>.Filter.Eq("delete_flag", false));
- if (!string.IsNullOrEmpty(key))
- list.Add(Builders<Sys_User_Account>.Filter.Where(s => s.usercode.Contains(key) || s.username.Contains(key) ));
- var filter = Builders<Sys_User_Account>.Filter.And(list);
- var agentlist = await _sys_user_accountRepository.Get(filter, null, null);
- return Success("根据条件获取坐席数据成功", agentlist);
- }
- /// <summary>
- /// 导出模板
- /// </summary>
- /// <returns></returns>
- [HttpGet("downtemplate")]
- [AllowAnonymous]
- public IActionResult DownTemplateAsync()
- {
- string mbname = _configuration["upload:mbname"].ToString();
- string mbkeys = _configuration["upload:mbkeys"].ToString();
- NPOIHelper npoi = new NPOIHelper();
- byte[] sm = npoi.ExportToExcelTemplate(mbkeys.Split(","));
- if (sm != null)
- {
- return File(sm, "application/vnd.ms-excel", mbname);
- }
- else
- {
- return Error("下载失败");
- }
- }
- /// <summary>
- /// 上传文件并将设备信息导入数据库
- /// </summary>
- /// <returns></returns>
- [HttpPost("importexcel")]
- public async Task<IActionResult> ImportExcel(int headrow = 0)
- {
- string usercode = User.Claims.FirstOrDefault(c => c.Type == ClaimTypes.Sid).Value;
- Microsoft.AspNetCore.Http.IFormFile _upfile = Request.Form.Files[0];
- if (!_upfile.ContentType.Equals("application/vnd.ms-excel") && !_upfile.ContentType.Equals("application/x-xls") && !_upfile.ContentType.Equals("application/x-xlsx") && !_upfile.ContentType.Equals("application/vnd.openxmlformats-officedocument.spreadsheetml.sheet") && !_upfile.ContentType.Equals("application/octet-stream"))
- return Error($"请正确上传Excel文件:file.ContentType={_upfile.ContentType}");
- NPOIHelper npoi = new NPOIHelper();
- var dtExcel = npoi.ExcelToTable1(_upfile, headrow);
- int num = dtExcel.Rows.Count;
- var cols = dtExcel.Columns;
- int colnum = cols.Count;
- string dbkeys = _configuration["upload:dbkeys"].ToString().ToLower();
- string[] dbcols = dbkeys.Split(",");
- string errmsg = string.Empty;
- if (num > 0)
- {
- int index = 1;
- foreach (System.Data.DataRow dr in dtExcel.Rows)
- {
- Sys_User_Account model = new Sys_User_Account();
- model.create_time = DateTime.Now;
- model.create_user = User.Claims.FirstOrDefault(c => c.Type == ClaimTypes.Sid).Value;
- model.lock_flag = false;//导入禁用,需要重新编辑
- model.dept_id = "import";
- model.password = "e10adc3949ba59abbe56e057f20f883e";
- model.role_id = "5fb22143751cef5115b5da20";
- List<string> plist = new List<string>();
- string arry = dr["postlist"].ToString();
- if (!string.IsNullOrEmpty(arry)) {
- string[] arrystr = arry.Split(',');
- if (arrystr.Length > 0)
- {
- foreach (string s in arrystr)
- {
- plist.Add(s);
- }
-
- }
- }
- model.postlist = plist;
- plist = new List<string>();
- arry = dr["projectlist"].ToString();
- if (!string.IsNullOrEmpty(arry))
- {
- string[] arrystr = arry.Split(',');
- if (arrystr.Length > 0)
- {
- foreach (string s in arrystr)
- {
- plist.Add(s);
- }
-
- }
- }
- model.projectlist = plist;
- var dbcolslist = dbcols.ToList();
- Type t = model.GetType();
- PropertyInfo[] PropertyList = t.GetProperties();
- foreach (PropertyInfo item in PropertyList)
- {
- if (dbcolslist.Contains(item.Name))
- {
- if (item.Name == "id")
- continue;
- object v = null;
- if (item.Name.ToLower() == "postlist"|| item.Name.ToLower() == "projectlist")
- {
- continue;
- }
- else
- {
- try
- {
- v = Convert.ChangeType(dr[item.Name].ToString(), item.PropertyType);
- }
- catch
- {
- continue;
- }
- }
-
- item.SetValue(model, v, null);
- }
- }
- if (string.IsNullOrEmpty(model.usercode))
- {
- continue;
- }
- Sys_User_Account usernmelists = await _sys_user_accountRepository.GetSingle(p=>p.usercode==model.usercode);
- if (usernmelists != null)
- {
- continue;
- }
-
- bool b = await _sys_user_accountRepository.Add(model);
- if (!b)
- {
- if (!string.IsNullOrEmpty(errmsg))
- {
- errmsg = errmsg + "\r\n第" + index + "行导入失败!";
- return Error(errmsg);
- }
- else
- {
- errmsg = "第" + index + "行导入失败!";
- }
- }
- index++;
- }
- }
- else
- {
- return Error("文件中无数据");
- }
- if (!string.IsNullOrEmpty(errmsg))
- {
- //删除已导入的部分
- return Error(errmsg);
- }
- return Success("导入成功");
- }
- }
- }
|