Esempio n. 1
0
        /// <summary>
        /// 修改密码
        /// </summary>
        /// <param name="userId">用户Id</param>
        /// <param name="newPwd">新密码</param>
        /// <param name="errMsg">错误信息</param>
        /// <returns></returns>
        public static bool ModifyPassword(Guid userId, string newPwd, out string errMsg)
        {
            errMsg = string.Empty;
            if (ModelConfigHelper.ModelIsViewMode(typeof(Sys_User)))
            {
                errMsg = "用户管理模块为视图模式不允许更新密码!";
                return(false);
            }
            Sys_User user = CommonOperate.GetEntity <Sys_User>(x => x.Id == userId && !x.IsDeleted && x.IsValid && x.IsActivated, string.Empty, out errMsg);

            if (user == null)
            {
                errMsg = "非法用户!";
                return(false);
            }
            //获取混淆码
            string passwordSalt = SecurityHelper.GenerateSalt();
            //获取混淆码加密过的密码
            string passwordHash = SecurityHelper.EncodePassword(newPwd, passwordSalt); //SecurityHelper.EncodePassword(MySecurity.MD5(newPwd), passwordSalt);

            user.PasswordHash = passwordHash;
            user.PasswordSalt = passwordSalt;
            Guid rs = CommonOperate.OperateRecord <Sys_User>(user, ModelRecordOperateType.Edit, out errMsg, new List <string>()
            {
                "PasswordHash", "PasswordSalt"
            }, false);

            if (rs != Guid.Empty) //修改密码成功
            {
                errMsg = TempUserOperate(userId, newPwd);
            }
            return(string.IsNullOrEmpty(errMsg));
        }
Esempio n. 2
0
        /// <summary>
        /// 修改用户别名
        /// </summary>
        /// <param name="username">用户名</param>
        /// <param name="aliasName">用户别名</param>
        /// <returns></returns>
        public static string UpdateUserAliasName(string username, string aliasName)
        {
            if (ModelConfigHelper.ModelIsViewMode(typeof(Sys_User)))
            {
                return("用户管理模块为视图模式不允许更新用户别名!");
            }
            if (string.IsNullOrWhiteSpace(aliasName))
            {
                return("用户别名不能为空!");
            }
            string   errMsg = string.Empty;
            Sys_User user   = CommonOperate.GetEntity <Sys_User>(x => x.UserName == username, null, out errMsg);

            if (user == null)
            {
                return("用户信息不存在!");
            }
            if (user.AliasName != aliasName)
            {
                user.AliasName = aliasName;
                Guid rs = CommonOperate.OperateRecord <Sys_User>(user, ModelRecordOperateType.Edit, out errMsg, new List <string>()
                {
                    "AliasName"
                }, false);
            }
            return(errMsg);
        }
Esempio n. 3
0
        /// <summary>
        /// 账户是否有效
        /// </summary>
        /// <param name="username"></param>
        /// <param name="errMsg">异常信息</param>
        /// <returns></returns>
        public static bool UserIsValid(string username, out string errMsg)
        {
            errMsg = string.Empty;
            Sys_User tempUser = CommonOperate.GetEntity <Sys_User>(x => !x.IsDeleted && x.IsValid && x.IsActivated && x.UserName.ToLower() == username.Trim().ToLower(), string.Empty, out errMsg);

            return(tempUser != null);
        }
Esempio n. 4
0
 /// <summary>
 /// 网格数据处理
 /// </summary>
 /// <param name="data"></param>
 /// <param name="otherParams"></param>
 /// <param name="currUser"></param>
 public bool PageGridDataHandle(List <Bpm_WorkToDoList> data, object[] otherParams = null, UserInfo currUser = null)
 {
     if (data != null && data.Count > 0)
     {
         string errMsg = string.Empty;
         foreach (Bpm_WorkToDoList todo in data)
         {
             todo.ModuleName = SystemOperate.GetModuleNameById(todo.ModuleId);
             if (!todo.Bpm_WorkFlowInstanceId.HasValue)
             {
                 continue;
             }
             Bpm_WorkFlowInstance flowInst = BpmOperate.GetWorkflowInstanceById(todo.Bpm_WorkFlowInstanceId.Value);
             if (flowInst == null)
             {
                 continue;
             }
             todo.StatusOfEnum = flowInst.StatusOfEnum;
             int status = (int)flowInst.StatusOfEnum;
             Bpm_WorkToDoList tempTodo = CommonOperate.GetEntity <Bpm_WorkToDoList>(x => x.Bpm_WorkFlowInstanceId == todo.Bpm_WorkFlowInstanceId.Value && x.Status == status, null, out errMsg);
             if (tempTodo != null && !string.IsNullOrEmpty(todo.NextNodeHandler))
             {
                 List <Guid> tempEmpIds = tempTodo.NextNodeHandler.Split(",".ToCharArray(), StringSplitOptions.RemoveEmptyEntries).Select(x => x.ObjToGuid()).Where(x => x != Guid.Empty).ToList();
                 if (tempEmpIds.Count > 0)
                 {
                     todo.OrgM_EmpName = string.Join(",", tempEmpIds.Select(x => OrgMOperate.GetEmpNameContainsQuit(x)).Where(x => !string.IsNullOrEmpty(x)));
                 }
             }
         }
     }
     return(false);
 }
 /// <summary>
 /// 部门操作完成
 /// </summary>
 /// <param name="operateType"></param>
 /// <param name="t"></param>
 /// <param name="result"></param>
 /// <param name="currUser"></param>
 /// <param name="otherParams"></param>
 public void OperateCompeletedHandle(ModelRecordOperateType operateType, OrgM_Dept t, bool result, UserInfo currUser, object[] otherParams = null)
 {
     if (result)
     {
         string errMsg   = string.Empty;
         Guid?  parentId = null;
         if (t.ParentId.HasValue && t.ParentId.Value != Guid.Empty)
         {
             OrgM_Dept parentDept = OrgMOperate.GetParentDept(t.ParentId.Value);
             if (parentDept != null && !string.IsNullOrEmpty(parentDept.Name))
             {
                 Sys_Organization parentOrg = CommonOperate.GetEntity <Sys_Organization>(x => x.Name == parentDept.Name && x.Flag == parentDept.Id.ToString() && !x.IsDeleted, null, out errMsg);
                 if (parentOrg != null)
                 {
                     parentId = parentOrg.Id;
                 }
             }
         }
         //部门新增后增加到系统组织中
         if (operateType == ModelRecordOperateType.Add)
         {
             Sys_Organization org = new Sys_Organization()
             {
                 Name           = t.Name,
                 ParentId       = parentId,
                 Flag           = t.Id.ToString(),
                 Des            = t.Alias,
                 CreateUserId   = t.CreateUserId,
                 CreateUserName = t.CreateUserName,
                 ModifyUserId   = t.ModifyUserId,
                 ModifyUserName = t.ModifyUserName
             };
             CommonOperate.OperateRecord <Sys_Organization>(org, ModelRecordOperateType.Add, out errMsg, null, false);
         }
         else if (operateType == ModelRecordOperateType.Edit)
         {
             Sys_Organization org = CommonOperate.GetEntity <Sys_Organization>(x => x.Name == t.Name && x.Flag == t.Id.ToString() && !x.IsDeleted, null, out errMsg);
             if (org != null)
             {
                 org.ParentId       = parentId;
                 org.ModifyDate     = DateTime.Now;
                 org.ModifyUserId   = t.ModifyUserId;
                 org.ModifyUserName = t.ModifyUserName;
                 List <string> fileNames = new List <string>()
                 {
                     "ParentId", "ModifyDate", "ModifyUserId", "ModifyUserName"
                 };
                 CommonOperate.OperateRecord <Sys_Organization>(org, ModelRecordOperateType.Edit, out errMsg, fileNames, false);
             }
         }
     }
 }
Esempio n. 6
0
        /// <summary>
        /// 根据用户名获取用户Id
        /// </summary>
        /// <param name="username">用户名</param>
        /// <returns></returns>
        public static Guid GetUserIdByUserName(string username)
        {
            string errMsg = string.Empty;

            if (!string.IsNullOrEmpty(username))
            {
                Sys_User tempUser = CommonOperate.GetEntity <Sys_User>(x => x.UserName == username, null, out errMsg);
                if (tempUser != null)
                {
                    return(tempUser.Id);
                }
            }
            return(Guid.Empty);
        }
Esempio n. 7
0
        /// <summary>
        /// 获取用户密码,找回密码
        /// </summary>
        /// <param name="userId">用户Id</param>
        /// <returns></returns>
        public static string GetPassword(Guid userId)
        {
            string errMsg = string.Empty;
            //从用户临时表中找密码
            Sys_TempUser tempUser = CommonOperate.GetEntity <Sys_TempUser>(x => x.FieldInfo1 == userId.ToString(), string.Empty, out errMsg);

            if (tempUser == null)
            {
                return(string.Empty);
            }
            string pwd = MySecurity.DES3EncryptString(string.Format("{0}_0123456789", Guid.Empty.ToString()), tempUser.FieldInfo2, "sy654321");

            return(pwd);
        }
 /// <summary>
 /// 操作前验证,如果流程已经在运行则不能删除
 /// </summary>
 /// <param name="operateType"></param>
 /// <param name="t"></param>
 /// <param name="errMsg"></param>
 /// <param name="otherParams"></param>
 /// <returns></returns>
 public bool BeforeOperateVerifyOrHandle(ModelRecordOperateType operateType, Bpm_WorkFlow t, out string errMsg, object[] otherParams = null)
 {
     errMsg = string.Empty;
     if (operateType == ModelRecordOperateType.Del)
     {
         //如果该流程存在流程实例则不允许删除
         Bpm_WorkFlowInstance workflowInst = CommonOperate.GetEntity <Bpm_WorkFlowInstance>(x => x.Bpm_WorkFlowId == t.Id, null, out errMsg);
         if (workflowInst != null)
         {
             errMsg = "运行中的流程不允许删除!";
             return(false);
         }
     }
     else if (operateType == ModelRecordOperateType.Add)
     {
         if (t.ValidEndTime != null && t.ValidEndTime.Value <= DateTime.Now)
         {
             errMsg = "结束时间必须大于今天!";
             return(false);
         }
         if (t.ValidEndTime != null)
         {
             if (t.ValidStartTime == null)
             {
                 errMsg = "设置了结束时间必须也要设置起始时间!";
                 return(false);
             }
             else if (t.ValidStartTime.Value < DateTime.Now)
             {
                 errMsg = "起始时间必须大于等于今天!";
                 return(false);
             }
         }
         if (t.ValidStartTime == null && t.ValidEndTime == null)
         {
             if (BpmOperate.GetAllWorkflows(x => x.Sys_ModuleId == t.Sys_ModuleId && x.ValidStartTime == null && x.ValidEndTime == null).Count > 0)
             {
                 errMsg = "该模块已经存在了默认流程(起始时间和结束时间为空),不允许再添加默认流程!";
                 return(false);
             }
         }
     }
     return(true);
 }
 /// <summary>
 /// 操作前验证
 /// </summary>
 /// <param name="operateType"></param>
 /// <param name="ts"></param>
 /// <param name="errMsg"></param>
 /// <param name="otherParams"></param>
 /// <returns></returns>
 public bool BeforeOperateVerifyOrHandles(ModelRecordOperateType operateType, List <Bpm_WorkFlow> ts, out string errMsg, object[] otherParams = null)
 {
     errMsg = string.Empty;
     if (operateType == ModelRecordOperateType.Del)
     {
         foreach (Bpm_WorkFlow t in ts)
         {
             //如果该流程存在流程实例则不允许删除
             Bpm_WorkFlowInstance workflowInst = CommonOperate.GetEntity <Bpm_WorkFlowInstance>(x => x.Bpm_WorkFlowId == t.Id, null, out errMsg);
             if (workflowInst != null)
             {
                 errMsg += string.Format("【{0}】,", t.Name);
             }
         }
         if (errMsg.Length > 0)
         {
             errMsg = string.Format("流程{0}正在运行不允许删除!", errMsg);
             return(false);
         }
     }
     return(true);
 }
Esempio n. 10
0
        /// <summary>
        /// 登录系统,获取用户信息
        /// </summary>
        /// <param name="username">用户名</param>
        /// <param name="userpwd">密码</param>
        /// <param name="errMsg">异常信息</param>
        /// <returns></returns>
        public static UserInfo GetUserInfo(string username, string userpwd, out string errMsg)
        {
            errMsg = string.Empty;
            List <string> fn           = new List <string>(); //标识不从本地缓存中取
            Sys_User      tempUser     = CommonOperate.GetEntity <Sys_User>(x => !x.IsDeleted && x.UserName.ToLower() == username.Trim().ToLower(), string.Empty, out errMsg, fn);
            string        passwordSalt = tempUser == null ? string.Empty : tempUser.PasswordSalt;

            if (tempUser == null || tempUser.IsDeleted)
            {
                errMsg = "账户不存在!";
                return(null);
            }
            if (string.IsNullOrEmpty(passwordSalt))
            {
                errMsg = "账户存在异常,请联系管理员!";
                return(null);
            }
            string   passwordHash = SecurityHelper.EncodePassword(userpwd, passwordSalt); //SecurityHelper.EncodePassword(MySecurity.MD5(userpwd), passwordSalt);
            Sys_User user         = CommonOperate.GetEntity <Sys_User>(x => !x.IsDeleted && x.UserName.ToLower() == username.Trim().ToLower() &&
                                                                       x.PasswordHash == passwordHash, string.Empty, out errMsg, fn);

            if (user == null)
            {
                errMsg = "密码输入有误!";
                return(null);
            }
            if (!user.IsValid)
            {
                errMsg = "账户被冻结!";
                return(null);
            }
            if (!user.IsActivated)
            {
                errMsg = "账户未激活,请联系管理员激活账户!";
                return(null);
            }
            return(GetUserInfo(user));
        }
Esempio n. 11
0
        /// <summary>
        /// 删除用户
        /// </summary>
        /// <param name="username">用户名</param>
        /// <returns></returns>
        public static string DelUser(string username)
        {
            string errMsg = string.Empty;

            if (ModelConfigHelper.ModelIsViewMode(typeof(Sys_User)))
            {
                errMsg = "用户管理模块为视图模块不允许删除!";
                return(errMsg);
            }
            Sys_User user = CommonOperate.GetEntity <Sys_User>(x => x.UserName == username && !x.IsDeleted && x.IsValid && x.IsActivated, string.Empty, out errMsg);

            if (user == null)
            {
                return("非法用户!");
            }
            bool rs = CommonOperate.DeleteRecordsByExpression <Sys_User>(x => x.UserName == username, out errMsg);

            if (rs) //用户删除成功后删除临时用户
            {
                CommonOperate.DeleteRecordsByExpression <Sys_TempUser>(x => x.FieldInfo1 == user.Id.ToString(), out errMsg);
            }
            return(errMsg);
        }
Esempio n. 12
0
        /// <summary>
        /// 冻结账号
        /// </summary>
        /// <param name="username">用户名</param>
        /// <returns></returns>
        public static string FrozenUser(string username)
        {
            string errMsg = string.Empty;

            if (ModelConfigHelper.ModelIsViewMode(typeof(Sys_User)))
            {
                errMsg = "用户管理模块为视图模式,不允许冻结!";
                return(errMsg);
            }
            Sys_User user = CommonOperate.GetEntity <Sys_User>(x => x.UserName == username && !x.IsDeleted && x.IsValid && x.IsActivated, string.Empty, out errMsg);

            if (user == null)
            {
                return("非法用户!");
            }
            user.IsValid = false;
            Guid rs = CommonOperate.OperateRecord <Sys_User>(user, ModelRecordOperateType.Edit, out errMsg, new List <string>()
            {
                "IsValid"
            }, false);

            return(errMsg);
        }
Esempio n. 13
0
        /// <summary>
        /// 临时用户操作
        /// </summary>
        /// <param name="userId">用户Id</param>
        /// <param name="userpwd">用户密码</param>
        private static string TempUserOperate(Guid userId, string userpwd)
        {
            string errMsg = string.Empty;

            try
            {
                ModelRecordOperateType operateType = ModelRecordOperateType.Edit;
                Sys_TempUser           tempUser    = CommonOperate.GetEntity <Sys_TempUser>(x => x.FieldInfo1 == userId.ToString(), string.Empty, out errMsg);
                if (tempUser == null)
                {
                    operateType         = ModelRecordOperateType.Add;
                    tempUser            = new Sys_TempUser();
                    tempUser.FieldInfo1 = userId.ToString();
                }
                tempUser.FieldInfo2 = MySecurity.DES3EncryptString(string.Format("{0}_0123456789", Guid.Empty.ToString()), userpwd, "sy654321");
                CommonOperate.OperateRecord <Sys_TempUser>(tempUser, operateType, out errMsg, null, false);
            }
            catch (Exception ex)
            {
                errMsg = ex.Message;
            }
            return(errMsg);
        }
Esempio n. 14
0
        /// <summary>
        /// 获取超级管理员用户信息
        /// </summary>
        /// <returns></returns>
        public static UserInfo GetSuperAdmin()
        {
            if (_adminUser != null)
            {
                return(_adminUser);
            }
            string   errMsg = string.Empty;
            Sys_User user   = CommonOperate.GetEntity <Sys_User>(x => x.UserName == "admin", null, out errMsg);

            if (user == null)
            {
                user = new Sys_User()
                {
                    Id = Guid.Empty, UserName = null, AliasName = null
                }
            }
            ;
            UserInfo userInfo = new UserInfo()
            {
                UserId    = user.Id,
                UserName  = user.UserName,
                AliasName = string.IsNullOrEmpty(user.AliasName) ? user.UserName : user.AliasName,
                EmpId     = Guid.Empty,
                EmpName   = "系统管理员"
            };
            OrgM_Emp emp = OrgMOperate.GetEmpByUserName(user.UserName);

            if (emp != null)
            {
                userInfo.EmpId   = emp.Id;
                userInfo.EmpCode = emp.Code;
                userInfo.EmpName = emp.Name;
            }
            _adminUser = userInfo;
            return(_adminUser);
        }
Esempio n. 15
0
        /// <summary>
        /// 添加用户
        /// </summary>
        /// <param name="errMsg">异常信息</param>
        /// <param name="username">用户名</param>
        /// <param name="userpwd">用户密码</param>
        /// <param name="orgId">组织Id</param>
        /// <param name="aliasName">用户别名</param>
        /// <returns></returns>
        public static Guid AddUser(out string errMsg, string username, string userpwd, Guid?orgId = null, string aliasName = null)
        {
            errMsg = string.Empty;
            if (ModelConfigHelper.ModelIsViewMode(typeof(Sys_User)))
            {
                errMsg = "用户管理模块为视图模式,不允许添加!";
                return(Guid.Empty);
            }
            if (string.IsNullOrWhiteSpace(username))
            {
                errMsg = "用户名不能为空!";
                return(Guid.Empty);
            }
            if (string.IsNullOrWhiteSpace(userpwd))
            {
                errMsg = "用户密码不能为空!";
                return(Guid.Empty);
            }
            Sys_User tempUser = CommonOperate.GetEntity <Sys_User>(x => x.UserName.ToLower() == username.Trim().ToLower(), null, out errMsg);

            if (tempUser != null)
            {
                errMsg = string.Format("用户【{0}】已存在!", username);
                return(Guid.Empty);
            }
            UserInfo adminUser = GetSuperAdmin(); //超级管理员用户
            //获取混淆码
            string passwordSalt = SecurityHelper.GenerateSalt();
            //获取混淆码加密过的密码
            string   passwordHash = SecurityHelper.EncodePassword(userpwd, passwordSalt); //SecurityHelper.EncodePassword(MySecurity.MD5(userpwd), passwordSalt);
            Sys_User user         = new Sys_User()
            {
                UserName      = username,
                AliasName     = aliasName,
                PasswordHash  = passwordHash,
                PasswordSalt  = passwordSalt,
                IsValid       = true,
                IsActivated   = true,
                ActivatedDate = DateTime.Now
            };

            if (orgId.HasValue && orgId.Value != Guid.Empty)
            {
                user.Sys_OrganizationId = orgId.Value;
            }
            if (adminUser != null)
            {
                user.CreateUserName = adminUser.AliasName;
                user.CreateDate     = DateTime.Now;
                user.CreateUserId   = adminUser.UserId;
                user.ModifyUserName = adminUser.AliasName;
                user.ModifyDate     = DateTime.Now;
                user.ModifyUserId   = adminUser.UserId;
            }
            Guid userId = CommonOperate.OperateRecord <Sys_User>(user, ModelRecordOperateType.Add, out errMsg, null, false);

            //临时用户操作
            if (userId != Guid.Empty)
            {
                TempUserOperate(userId, userpwd);
            }
            return(userId);
        }
Esempio n. 16
0
 /// <summary>
 /// 员工操作完成
 /// </summary>
 /// <param name="operateType">操作类型</param>
 /// <param name="t">员工对象</param>
 /// <param name="result">操作结果</param>
 /// <param name="currUser">当前用户</param>
 /// <param name="otherParams"></param>
 public void OperateCompeletedHandle(ModelRecordOperateType operateType, OrgM_Emp t, bool result, UserInfo currUser, object[] otherParams = null)
 {
     if (result)
     {
         string errMsg      = string.Empty;
         string username    = OrgMOperate.GetUserNameByEmp(t);
         string userInitPwd = WebConfigHelper.GetAppSettingValue("UserInitPwd");
         if (string.IsNullOrEmpty(userInitPwd))
         {
             userInitPwd = "123456";
         }
         if (operateType == ModelRecordOperateType.Add)
         {
             if (!string.IsNullOrEmpty(username))
             {
                 UserOperate.AddUser(out errMsg, username, userInitPwd, null, t.Name);
             }
         }
         else if (operateType == ModelRecordOperateType.Edit)
         {
             if (!string.IsNullOrEmpty(username))
             {
                 Sys_User user = UserOperate.GetUser(username);
                 if (user != null) //用户已存在
                 {
                     UserOperate.UpdateUserAliasName(username, t.Name);
                 }
                 else //用户不存在
                 {
                     UserOperate.AddUser(out errMsg, username, userInitPwd, null, t.Name);
                 }
             }
         }
         else if (operateType == ModelRecordOperateType.Del)
         {
             if (!string.IsNullOrEmpty(username))
             {
                 UserOperate.DelUser(username); //删除账号
             }
             //删除员工同时删除员工岗位
             CommonOperate.DeleteRecordsByExpression <OrgM_EmpDeptDuty>(x => x.OrgM_EmpId == t.Id, out errMsg, t.IsDeleted);
         }
         if (operateType == ModelRecordOperateType.Add || operateType == ModelRecordOperateType.Edit)
         {
             //新增编辑时同时设置员工主职岗位
             if (t.DeptId.HasValue && t.DeptId.Value != Guid.Empty && t.DutyId.HasValue && t.DutyId.Value != Guid.Empty)
             {
                 OrgM_EmpDeptDuty empPosition = null;
                 if (operateType == ModelRecordOperateType.Edit)
                 {
                     empPosition = CommonOperate.GetEntity <OrgM_EmpDeptDuty>(x => x.OrgM_EmpId == t.Id && x.IsMainDuty == true, null, out errMsg);
                     if (empPosition != null)
                     {
                         empPosition.OrgM_DeptId    = t.DeptId.Value;
                         empPosition.OrgM_DutyId    = t.DutyId.Value;
                         empPosition.IsValid        = true;
                         empPosition.ModifyDate     = DateTime.Now;
                         empPosition.ModifyUserId   = currUser.UserId;
                         empPosition.ModifyUserName = currUser.EmpName;
                         CommonOperate.OperateRecord <OrgM_EmpDeptDuty>(empPosition, ModelRecordOperateType.Edit, out errMsg, null, false);
                         return;
                     }
                 }
                 Guid   moduleId = SystemOperate.GetModuleIdByModelType(typeof(OrgM_EmpDeptDuty));
                 string code     = SystemOperate.GetBillCode(moduleId);
                 empPosition = new OrgM_EmpDeptDuty()
                 {
                     Code           = code,
                     OrgM_DeptId    = t.DeptId.Value,
                     OrgM_DutyId    = t.DutyId.Value,
                     OrgM_EmpId     = t.Id,
                     IsMainDuty     = true,
                     IsValid        = true,
                     EffectiveDate  = DateTime.Now,
                     CreateDate     = DateTime.Now,
                     CreateUserId   = currUser.UserId,
                     CreateUserName = currUser.EmpName,
                     ModifyDate     = DateTime.Now,
                     ModifyUserId   = currUser.UserId,
                     ModifyUserName = currUser.EmpName
                 };
                 Guid rs = CommonOperate.OperateRecord <OrgM_EmpDeptDuty>(empPosition, ModelRecordOperateType.Add, out errMsg, null, false);
                 if (rs != Guid.Empty)
                 {
                     SystemOperate.UpdateBillCode(moduleId, code);
                 }
             }
         }
     }
 }
 /// <summary>
 /// 取分布式锁(基于DB方式)
 /// </summary>
 /// <param name="moduleFlag">模块标识,模块表名/类名</param>
 /// <param name="method_Flag">方法名标识</param>
 /// <param name="expirtime">自定义过期时间(秒)</param>
 /// <param name="des">锁描述</param>
 /// <returns></returns>
 public static string DistributeDbLock(string moduleFlag, string method_Flag, double?expirtime = null, string des = null)
 {
     try
     {
         if (WebConfigHelper.GetAppSettingValue("EnabledDistributeLock") != "true") //未启用分布式锁
         {
             return(string.Empty);
         }
         string hostname  = System.Net.Dns.GetHostName();            //当前服务器
         string processId = ApplicationObject.GetCurrentProcessId(); //当前进程
         string threadId  = ApplicationObject.GetCurrentThreadId();  //当前线程
         lock (tempObjDistriLock)
         {
             string       errMsg   = string.Empty;
             int          timeout  = 30; //30秒超时
             DateTime     initTime = DateTime.Now;
             DatabaseType dbType   = DatabaseType.MsSqlServer;
             string       connStr  = ModelConfigHelper.GetModelConnStr(typeof(Other_DistributedLock), out dbType, false);
             while ((DateTime.Now - initTime).TotalSeconds <= timeout)
             {
                 double updateTimesamp            = Globals.GetTimestamp(DateTime.Now);                                                                 //当前时间戳
                 double invalidTimesamp           = expirtime.HasValue && expirtime.Value > 0 ? updateTimesamp + expirtime.Value : updateTimesamp + 20; //过期时间戳
                 Other_DistributedLock methodLock = CommonOperate.GetEntity <Other_DistributedLock>(x => x.ModuleFlag == moduleFlag && x.Method_Flag == method_Flag && x.Invalid_Timesamp > updateTimesamp, null, out errMsg);
                 //锁存在,继续循环再取
                 if (methodLock != null)
                 {
                     Thread.Sleep(10);
                     continue;
                 }
                 //锁不存在,取得锁成功,插入锁标识
                 methodLock = new Other_DistributedLock()
                 {
                     ModuleFlag       = moduleFlag,
                     Method_Flag      = method_Flag,
                     Update_Timesamp  = updateTimesamp,
                     Invalid_Timesamp = invalidTimesamp,
                     Maching          = hostname,
                     ProcessId        = processId,
                     ThreadId         = threadId,
                     Des = des
                 };
                 TransactionTask tranAction = (conn) =>
                 {
                     CommonOperate.DeleteRecordsByExpression <Other_DistributedLock>(x => x.ModuleFlag == moduleFlag && x.Method_Flag == method_Flag, out errMsg, false, connStr, dbType, conn);
                     if (!string.IsNullOrEmpty(errMsg))
                     {
                         throw new Exception(errMsg);
                     }
                     CommonOperate.OperateRecord <Other_DistributedLock>(methodLock, ModelRecordOperateType.Add, out errMsg, null, false, false, connStr, dbType, conn);
                     if (!string.IsNullOrEmpty(errMsg))
                     {
                         throw new Exception(errMsg);
                     }
                 };
                 CommonOperate.TransactionHandle(tranAction, out errMsg, connStr, dbType);
                 //取锁成功
                 if (string.IsNullOrEmpty(errMsg))
                 {
                     return(string.Empty);
                 }
                 else
                 {
                     WritLockLog(moduleFlag, method_Flag, errMsg);
                 }
                 //取锁失败,继续循环取
                 Thread.Sleep(10);
             }
             return("获取分布式锁超时"); //取锁失败
         }
     }
     catch
     {
         return(string.Empty);
     }
 }
Esempio n. 18
0
 /// <summary>
 /// 员工操作完成
 /// </summary>
 /// <param name="operateType">操作类型</param>
 /// <param name="t">员工对象</param>
 /// <param name="result">操作结果</param>
 /// <param name="currUser">当前用户</param>
 /// <param name="otherParams"></param>
 public void OperateCompeletedHandle(ModelRecordOperateType operateType, OrgM_Emp t, bool result, UserInfo currUser, object[] otherParams = null)
 {
     if (result)
     {
         string errMsg   = string.Empty;
         string username = OrgMOperate.GetUserNameByEmp(t);
         if (operateType == ModelRecordOperateType.Add)
         {
             if (!string.IsNullOrEmpty(username))
             {
                 UserOperate.AddUser(out errMsg, username, string.Format("{0}_123456", username), null, t.Name);
             }
         }
         else if (operateType == ModelRecordOperateType.Edit)
         {
             if (!string.IsNullOrEmpty(username))
             {
                 UserOperate.UpdateUserAliasName(username, t.Name);
             }
         }
         else if (operateType == ModelRecordOperateType.Del)
         {
             if (!string.IsNullOrEmpty(username))
             {
                 UserOperate.DelUser(username); //删除账号
             }
             //删除员工同时删除员工岗位
             CommonOperate.DeleteRecordsByExpression <OrgM_EmpDeptDuty>(x => x.OrgM_EmpId == t.Id, out errMsg, t.IsDeleted);
         }
         if (operateType == ModelRecordOperateType.Add || operateType == ModelRecordOperateType.Edit)
         {
             //新增编辑时同时设置员工主职岗位
             if (t.DeptId.HasValue && t.DeptId.Value != Guid.Empty && t.DutyId.HasValue && t.DutyId.Value != Guid.Empty)
             {
                 OrgM_EmpDeptDuty empPosition = null;
                 if (operateType == ModelRecordOperateType.Edit)
                 {
                     empPosition = CommonOperate.GetEntity <OrgM_EmpDeptDuty>(x => x.OrgM_EmpId == t.Id && x.IsMainDuty == true, null, out errMsg);
                     if (empPosition != null)
                     {
                         empPosition.OrgM_DeptId    = t.DeptId.Value;
                         empPosition.OrgM_DutyId    = t.DutyId.Value;
                         empPosition.IsValid        = true;
                         empPosition.ModifyDate     = DateTime.Now;
                         empPosition.ModifyUserId   = currUser.UserId;
                         empPosition.ModifyUserName = currUser.EmpName;
                         CommonOperate.OperateRecord <OrgM_EmpDeptDuty>(empPosition, ModelRecordOperateType.Edit, out errMsg, null, false);
                         return;
                     }
                 }
                 empPosition = new OrgM_EmpDeptDuty()
                 {
                     OrgM_DeptId    = t.DeptId.Value,
                     OrgM_DutyId    = t.DutyId.Value,
                     OrgM_EmpId     = t.Id,
                     IsMainDuty     = true,
                     IsValid        = true,
                     EffectiveDate  = DateTime.Now,
                     CreateDate     = DateTime.Now,
                     CreateUserId   = currUser.UserId,
                     CreateUserName = currUser.EmpName,
                     ModifyDate     = DateTime.Now,
                     ModifyUserId   = currUser.UserId,
                     ModifyUserName = currUser.EmpName
                 };
                 CommonOperate.OperateRecord <OrgM_EmpDeptDuty>(empPosition, ModelRecordOperateType.Add, out errMsg, null, false);
             }
         }
     }
 }