Exemplo n.º 1
0
        /// <summary>
        /// 修改
        /// </summary>
        /// <param name="requestObject"></param>
        /// <returns></returns>
        public async Task <ResponseObject <TSMUserAccountEditAllModel, bool> > PutAsync(RequestObject <TSMUserAccountEditAllModel> requestObject)
        {
            var currDb = _db.Instance;//事务需要使用同一个 SqlSugarClient对象实例

            try
            {
                //没有新增数据,返回错误信息
                if (requestObject.PostData == null)
                {
                    return(ResponseUtil <TSMUserAccountEditAllModel, bool> .FailResult(requestObject, false, "PostData不能为null"));
                }
                //开启事务
                currDb.BeginTran();

                TSMUserAccountDbModel dbMain = ExpressionGenericMapper <TSMUserAccountEditAllModel, TSMUserAccountDbModel> .Trans(requestObject.PostData);

                dbMain.Salt   = RandCodeCreate.GenerateRandomNumber(32);
                dbMain.Passwd = EncryptUtil.EnAESBykey(dbMain.Passwd, dbMain.Salt);

                if (string.IsNullOrWhiteSpace(dbMain.EmailAccount))
                {
                    dbMain.EmailAccount = null;
                }

                if (string.IsNullOrWhiteSpace(dbMain.TelAccount))
                {
                    dbMain.TelAccount = null;
                }


                TSMUserInfoDbModel dbDeatail = ExpressionGenericMapper <TSMUserAccountEditAllModel, TSMUserInfoDbModel> .Trans(requestObject.PostData);

                dbDeatail.ID = requestObject.PostData.CId;

                await currDb.Updateable(dbMain).IgnoreColumns(p => new { p.CreateTime, p.CompanyId, p.UserInfoId }).ExecuteCommandAsync();


                await currDb.Updateable(dbDeatail).ExecuteCommandAsync();


                currDb.Deleteable <TSMRoleUserRelationDbModel>().Where(p => p.UserId == dbMain.ID).ExecuteCommand();
                if (requestObject.PostData.RoleId != null && requestObject.PostData.RoleId > 0)
                {
                    TSMRoleUserRelationDbModel itemEntity = new TSMRoleUserRelationDbModel();
                    itemEntity.RoleId = requestObject.PostData.RoleId.Value;
                    itemEntity.UserId = dbMain.ID;

                    currDb.Insertable <TSMRoleUserRelationDbModel>(itemEntity).ExecuteCommand();
                }

                currDb.Deleteable <TSMDeptUserRelationDbModel>().Where(p => p.UserAccountId == dbMain.ID).ExecuteCommand();
                if (requestObject.PostData.DeptId != null && requestObject.PostData.DeptId > 0)
                {
                    TSMDeptUserRelationDbModel itemEntity = new TSMDeptUserRelationDbModel();
                    itemEntity.DeptId        = requestObject.PostData.DeptId.Value;
                    itemEntity.UserAccountId = dbMain.ID;

                    currDb.Insertable <TSMDeptUserRelationDbModel>(itemEntity).ExecuteCommand();
                }


                //提交事务
                currDb.CommitTran();
                //返回执行结果

                return(ResponseUtil <TSMUserAccountEditAllModel, bool> .SuccessResult(requestObject, true));
            }
            catch (Exception ex)
            {
                //回滚事务
                currDb.RollbackTran();
                //返回异常结果
                return(ResponseUtil <TSMUserAccountEditAllModel, bool> .FailResult(requestObject, false, ex.Message));
            }
        }
Exemplo n.º 2
0
        /// <summary>
        /// 新增
        /// </summary>
        /// <param name="requestObject"></param>
        /// <param name="UserID">操作人ID</param>
        /// <returns></returns>
        public async Task <ResponseObject <TSMUserAccountAddAllModel, bool> > PostAsync(RequestObject <TSMUserAccountAddAllModel> requestObject, int UserID)
        {
            var currDb = _db.Instance;//事务需要使用同一个 SqlSugarClient对象实例

            try
            {
                //没有新增数据,返回错误信息
                if (requestObject.PostData == null)
                {
                    return(ResponseUtil <TSMUserAccountAddAllModel, bool> .FailResult(requestObject, false, "PostData不能为null"));
                }
                //开启事务
                currDb.BeginTran();

                var user = _db.Instance.Queryable <TSMUserAccountDbModel, TSMCompanyDbModel>(
                    (t, t1) => new object[]
                {
                    JoinType.Left, t.CompanyId == t1.ID,
                }).Where((t, t1) => t.ID == UserID).Select((t, t1) => new { CompanyId = t.CompanyId, Name = t1.CompanyName }).First();


                TSMUserAccountDbModel dbMain = ExpressionGenericMapper <TSMUserAccountAddAllModel, TSMUserAccountDbModel> .Trans(requestObject.PostData);

                //处理密码
                dbMain.Salt       = RandCodeCreate.GenerateRandomNumber(32);
                dbMain.Passwd     = EncryptUtil.EnAESBykey(dbMain.Passwd, dbMain.Salt);
                dbMain.CreateTime = DateTime.Now;
                dbMain.CompanyId  = user.CompanyId.Value;

                if (string.IsNullOrWhiteSpace(dbMain.EmailAccount))
                {
                    dbMain.EmailAccount = null;
                }

                if (string.IsNullOrWhiteSpace(dbMain.TelAccount))
                {
                    dbMain.TelAccount = null;
                }

                TSMUserInfoDbModel dbDeatail = ExpressionGenericMapper <TSMUserAccountAddAllModel, TSMUserInfoDbModel> .Trans(requestObject.PostData);

                //插入主表数据
                var cId = await currDb.Insertable(dbDeatail).ExecuteReturnIdentityAsync();

                dbMain.UserInfoId = cId;
                //插入从表数据
                var mId = await currDb.Insertable(dbMain).ExecuteReturnIdentityAsync();

                if (requestObject.PostData.RoleId != null && requestObject.PostData.RoleId > 0)
                {
                    TSMRoleUserRelationDbModel itemEntity = new TSMRoleUserRelationDbModel();
                    itemEntity.RoleId = requestObject.PostData.RoleId.Value;
                    itemEntity.UserId = mId;

                    currDb.Insertable <TSMRoleUserRelationDbModel>(itemEntity).ExecuteCommand();
                }

                if (requestObject.PostData.DeptId != null && requestObject.PostData.DeptId > 0)
                {
                    TSMDeptUserRelationDbModel itemEntity = new TSMDeptUserRelationDbModel();
                    itemEntity.DeptId        = requestObject.PostData.DeptId.Value;
                    itemEntity.UserAccountId = mId;
                    currDb.Insertable <TSMDeptUserRelationDbModel>(itemEntity).ExecuteCommand();
                }



                //提交事务
                currDb.CommitTran();
                //返回执行结果

                return(ResponseUtil <TSMUserAccountAddAllModel, bool> .SuccessResult(requestObject, true));
            }
            catch (Exception ex)
            {
                //回滚事务
                currDb.RollbackTran();
                //返回异常结果
                return(ResponseUtil <TSMUserAccountAddAllModel, bool> .FailResult(requestObject, false, ex.Message));
            }
        }