Example #1
0
        public override AddGroupResponseBody ExecuteCore()
        {
            AddGroupResponseBody res = new AddGroupResponseBody()
            {
                Status = ResultStatus.Failed
            };

            string groupGuid = Guid.NewGuid().ToString();

            using (HWLEntities db = new HWLEntities())
            {
                t_group group = new t_group()
                {
                    build_user_id    = this.request.BuildUserId,
                    group_guid       = groupGuid,
                    group_name       = this.request.GroupName,
                    group_user_count = this.request.GroupUserIds.Count,
                    build_date       = DateTime.Now,
                    update_date      = DateTime.Now,
                    group_note       = ""
                };
                db.t_group.Add(group);
                int groupId = db.SaveChanges();
                if (groupId <= 0)
                {
                    throw new Exception("组创建失败");
                }

                List <t_group_user> users = this.request.GroupUserIds.ConvertAll(f => new t_group_user
                {
                    group_guid = group.group_guid,
                    user_id    = f,
                    add_date   = DateTime.Now
                });
                db.t_group_user.AddRange(users);
                db.SaveChanges();

                res.Status    = ResultStatus.Success;
                res.GroupGuid = groupGuid;
                res.BuildTime = GenericUtility.FormatDate2(group.build_date);

                //不能在这里发送创建成功的消息,因为创建群组是客户端的行为,应由客户端来决定MQ消息的发送规则
                //往redis中添加数据
                new Redis.GroupAction().SaveGroupUser(groupGuid, this.request.GroupUserIds.ToArray());
            }

            return(res);
        }
Example #2
0
        public override GetGroupAndUsersResponseBody ExecuteCore()
        {
            GetGroupAndUsersResponseBody res = new GetGroupAndUsersResponseBody();

            using (HWLEntities db = new HWLEntities())
            {
                t_group group = db.t_group.Where(g => g.group_guid == this.request.GroupGuid).FirstOrDefault();
                if (group == null)
                {
                    //如果组不存在,则删除下面所有用户信息
                    DeleteGroupUsers(db);
                    return(res);
                }

                var groupUsers = db.t_group_user.Where(g => g.group_guid == this.request.GroupGuid).ToList();
                if (!groupUsers.Exists(g => g.user_id == this.request.UserId))
                {
                    return(res);
                }

                List <int> userIds = groupUsers.Select(u => u.user_id).ToList();
                var        users   = db.t_user.Where(u => userIds.Contains(u.id)).Select(u => new { u.id, u.name, u.head_image }).ToList();

                res.GroupInfo = new GroupInfo()
                {
                    GroupGuid      = group.group_guid,
                    BuildUserId    = group.build_user_id,
                    GroupName      = group.group_name,
                    GroupNote      = group.group_note,
                    GroupUserCount = group.group_user_count,
                    GroupUsers     = groupUsers.Select(u =>
                    {
                        return(new GroupUserInfo()
                        {
                            UserId = u.user_id,
                            GroupGuid = u.group_guid,
                            UserHeadImage = users.Where(i => i.id == u.user_id).Select(i => i.head_image).FirstOrDefault(),
                            UserName = users.Where(i => i.id == u.user_id).Select(i => i.name).FirstOrDefault()
                        });
                    }).ToList(),
                    BuildDate  = GenericUtility.FormatDate2(group.build_date),
                    UpdateDate = GenericUtility.FormatDate2(group.update_date),
                };
                res.GroupInfo.GroupUserImages = res.GroupInfo.GroupUsers.Select(u => u.UserHeadImage).Take(ConfigManager.GROUP_USER_IMAGE_COUNT).ToList();
            }

            return(res);
        }
        public IHttpActionResult CreateGroup(GroupViewModels create)
        {
            using (var transaction = db.Database.BeginTransaction())
            {
                try
                {
                    // add group
                    var group = new t_group()
                    {
                        GroupName   = create.GroupName,
                        GroupType   = create.GroupType,
                        Description = create.Description,
                        Members     = create.Members
                    };
                    db.t_group.Add(group);
                    db.SaveChanges();
                    // add members
                    int groupId = group.GroupId;
                    foreach (var item in create.TagList)
                    {
                        var member = new t_memeber
                        {
                            Name    = item,
                            Email   = item,
                            GroupId = groupId,
                            Status  = "1"
                        };
                        db.t_memeber.Add(member);
                    }
                    db.SaveChanges();
                    transaction.Commit();
                }
                catch (Exception)
                {
                    transaction.Rollback();
                    return(NotFound());
                }
            }

            return(Ok());
        }