using CallCenterApi.Common; using CallCenterApi.DB; using CallCenterApi.Interface.Controllers.Base; using CallCenterApi.Interface.Models.Input; using System; using System.Collections.Generic; using System.Data; using System.Linq; using System.Text.RegularExpressions; using System.Web; using System.Web.Mvc; namespace CallCenterApi.Interface.Controllers { [Authority] public class UserAccountController : BaseController { private BLL.T_Sys_UserAccount sysUserAccountBll = new BLL.T_Sys_UserAccount(); private BLL.T_Sys_Department departmentBLL = new BLL.T_Sys_Department(); //用户列表 public ActionResult GetList() { DataTable dt = new DataTable(); string sql = " and F_IsDelete=0 "; try { string Key = RequestString.GetQueryString("Key"); int deptid = RequestString.GetInt("deptid", 0); int roleid = RequestString.GetInt("roleid", 0); string strpageindex = RequestString.GetQueryString("page"); int pageindex = 1; string strpagesize = RequestString.GetQueryString("pagesize"); int pagesize = 10; if (deptid > 0) { sql += " and F_DeptId=" + deptid; } if (roleid > 0) { sql += " and F_RoleId=" + roleid; } if (!string.IsNullOrWhiteSpace(Key)) { string str = string.Format(" and (F_UserCode like '%{0}%' or F_UserName like '%{1}%')", Key, Key); sql += str; } if (strpageindex.Trim() != "") { pageindex = Convert.ToInt32(strpageindex); } if (strpagesize.Trim() != "") { pagesize = Convert.ToInt32(strpagesize); } int recordCount = 0; dt = BLL.PagerBLL.GetListPager( "T_Sys_UserAccount", "F_Id", "*", sql, "ORDER BY F_CreateTime desc", pagesize, pageindex, true, out recordCount); var obj = new { rows = new BLL.T_Sys_UserAccount().DataTableToList(dt), total = recordCount }; return Content(obj.ToJson()); } catch (Exception err) { return Error("错误:" + err.ToString()); } finally { dt.Clear(); dt.Dispose(); } } //获取用户信息 public ActionResult GetUser(int userId = 0, string userCode = "") { string sql = " and F_IsDelete=0 "; if (userId > 0) { sql += " and F_Id=" + userId; } if (!string.IsNullOrWhiteSpace(userCode)) { sql += " and F_UserCode='" + userCode+"'"; } if (string.IsNullOrWhiteSpace(sql)) return Error("获取失败"); Model.T_Sys_UserAccount userModel = sysUserAccountBll.GetModelList(" 1=1 " + sql).FirstOrDefault(); return Success("获取成功", userModel); } //添加用户信息 public ActionResult AddUsers(UserAccountInput input) { Model.T_Sys_UserAccount userModel = new BLL.T_Sys_UserAccount().GetModel(User.UserData["F_UserCode"]); Regex reg = new Regex(@"^[1-9]\d*$"); if (!reg.IsMatch(input.Usercode.Trim())) { return Error("工号必须为正整数"); } var model = sysUserAccountBll.GetModel(input.Usercode); if (model != null) return Error("当前员工工号存在,请更换!"); Model.T_Sys_UserAccount userAccountModel = new Model.T_Sys_UserAccount(); userAccountModel.F_UserCode = input.Usercode.Trim(); userAccountModel.F_UserName = input.Username.Trim(); userAccountModel.F_Password = input.Password.Trim(); userAccountModel.F_DeptId = input.DeptId; userAccountModel.F_RoleId = input.RoleId; userAccountModel.F_Sex = input.Sex; userAccountModel.F_Remark = input.Remark; userAccountModel.F_Mobile = input.Mobile; userAccountModel.F_Telephone = input.Telphone; userAccountModel.F_CreateUser = userModel.F_UserCode; userAccountModel.F_CreateTime = DateTime.Now; userAccountModel.F_IsDelete = 0; //userAccountModel.F_HJType = input.HjType; if (sysUserAccountBll.Add(userAccountModel) > 0) return Success("新增成功!"); else return Error("新增失败!"); } //修改用户信息 public ActionResult EditUsers(UserAccountInput input) { Regex reg = new Regex(@"^[1-9]\d*$"); if (!reg.IsMatch(input.Usercode.Trim())) { return Error("工号必须为正整数"); } BLL.T_Sys_UserAccount sysUserAccountBll = new BLL.T_Sys_UserAccount(); Model.T_Sys_UserAccount userAccountModel = sysUserAccountBll.GetModelList(" F_IsDelete=0 and F_Id = " + input.UserId).FirstOrDefault(); if (userAccountModel == null) return Error("获取用户失败"); userAccountModel.F_UserCode = input.Usercode.Trim(); userAccountModel.F_UserName = input.Username.Trim(); userAccountModel.F_DeptId = input.DeptId; userAccountModel.F_RoleId = input.RoleId; userAccountModel.F_Sex = input.Sex; userAccountModel.F_Remark = input.Remark; userAccountModel.F_Mobile = input.Mobile; userAccountModel.F_Telephone = input.Telphone; if (sysUserAccountBll.Update(userAccountModel)) return Success("编辑成功!"); else return Error("编辑失败!"); } //删除/禁用/启用 用户 public ActionResult DelUsers(string[] ids) { Model.T_Sys_UserAccount userModel = new BLL.T_Sys_UserAccount().GetModel(User.UserData["F_UserCode"]); if (ids == null || ids.Length <= 0) { return Error("请选择要删除的用户"); } var idStr = string.Join(",", ids); if (string.IsNullOrEmpty(idStr.Trim())) { return Error("请选择要删除的用户"); } int n = DbHelperSQL.ExecuteSql(" update T_Sys_UserAccount set F_IsDelete=1,F_DeleteUser='" + userModel.F_UserCode + "',F_DeleteTime=getdate() where F_Id in (" + idStr + ")"); if (n > 0) { return Success("删除成功"); } return Error("删除失败"); } //根据部门获取用户 public ActionResult GetDeptUserList(int deptid = 0) { List DeptUserList = sysUserAccountBll.GetModelList("F_DeptId='" + deptid + "' order by F_Id desc "); return Success("列表加载成功", DeptUserList); } //获取当前用户信息 public ActionResult GetNowUser() { Model.T_Sys_UserAccount userModel = new BLL.T_Sys_UserAccount().GetModel(User.UserData["F_UserCode"]); if (userModel == null) return Error("获取失败"); Model.T_Sys_RoleInfo roleModel = new BLL.T_Sys_RoleInfo().GetModel(userModel.F_RoleId); var obj = new { user = userModel, role = roleModel }; return Success("获取成功", obj); } /// /// 重置密码 /// /// /// /// public ActionResult ResetPwd(string usercode = "", string pwd = "") { if (string.IsNullOrWhiteSpace(pwd)) return Error("请输入密码"); var model = sysUserAccountBll.GetModel(usercode); if (model == null) return Error("此用户不存在"); model.F_Password = pwd; if (sysUserAccountBll.Update(model)) return Success("重置密码成功"); else return Error("重置密码失败"); } public ActionResult UpdatePwd(string usercode = "", string pwd = "", string oldPwd = "") { if (string.IsNullOrWhiteSpace(pwd)) return Error("请输入密码"); var model = sysUserAccountBll.GetModel(usercode); if (model == null) return Error("此用户不存在"); if (!model.F_Password.Equals(oldPwd)) return Error("原密码不正确"); model.F_Password = pwd; if (sysUserAccountBll.Update(model)) return Success("重置密码成功"); else return Error("重置密码失败"); } //获取坐席列表 public ActionResult GetSeatList(string branchcode) { string sqlwhere = " F_IsDelete=0 "; if (!string.IsNullOrEmpty(branchcode)) { sqlwhere += " and F_Code='" + branchcode + "'"; } else { return Error("请选择区县"); } var list = new BLL.T_Branch_List().GetModelList(sqlwhere); if (list.Count == 0) { return Error("查询失败"); } string controllername = RouteData.Values["controller"].ToString(); string actionname = RouteData.Values["action"].ToString(); if (!string.IsNullOrEmpty(list[0].F_Url)) { string signcode = CommonHelper.getsigncode(controllername, actionname, list[0].F_Sign); string strparams = "?signcode=" + signcode; string result = HttpMethods.HttpGet(list[0].F_Url + "/" + controllername + "/" + actionname + strparams); return Content(result); } else { return Error("查询失败"); } } //根据角色获取用户 public ActionResult GetUsersList(string branchcode, string rolecode) { string sqlwhere = " F_IsDelete=0 "; if (!string.IsNullOrEmpty(branchcode)) { sqlwhere += " and F_Code='" + branchcode + "'"; } else { return Error("请选择区县"); } var list = new BLL.T_Branch_List().GetModelList(sqlwhere); if (list.Count == 0) { return Error("查询失败"); } if (string.IsNullOrEmpty(rolecode)) { return Error("查询失败"); } string controllername = RouteData.Values["controller"].ToString(); string actionname = RouteData.Values["action"].ToString(); if (!string.IsNullOrEmpty(list[0].F_Url)) { string signcode = CommonHelper.getsigncode(controllername, actionname, list[0].F_Sign); string strparams = "?rolecode=" + rolecode + "&signcode=" + signcode; string result = HttpMethods.HttpGet(list[0].F_Url + "/" + controllername + "/" + actionname + strparams); return Content(result); } else { return Error("查询失败"); } } } }