Ejemplo n.º 1
0
        public JsonResult Reply(ExpertQuestionReplyInput input)
        {
            using (var result = new ResponseResult <object>())
            {
                var model    = Mapper.Map <T_QUESTION_REPLY>(input);
                var question = _expertQuestionService.GetByKey(input.QuestionId);

                //判断该条的问题是否存在
                if (question == null)
                {
                    throw new CustomException("问题不存在!");
                }

                //判断该问题是否已经公开
                if (question.IsOpen)
                {
                    throw new CustomException("该问题已公开,不能执行该操作!");
                }

                result.IsSuccess = _expertQuestionReplyService.Insert(model) > 0;
                if (!result.IsSuccess)
                {
                    return(new JsonResultEx(result));
                }

                result.Message = "回答成功!";
                //更新该问题的回复数
                question.ReplyCount += 1;
                _expertQuestionService.Update(question);

                //添加先锋币
                var roleType = (RoleType)question.RoleId;
                if (roleType == RoleType.Farmer)
                {
                    var userInfo = _userService.GetByKey(input.ReplyUserId);
                    if (userInfo != null)
                    {
                        userInfo.DPoint = (userInfo.DPoint ?? 0) + 5;
                        _userService.Update(userInfo);
                    }
                }

                //给问题的提问者推送通知
                _notificationService.Insert(new T_NOTIFICATION
                {
                    MsgContent           = "有人回答了你的问题,快去看看吧!",
                    IsPublic             = false,
                    TargetUserId         = question.UserId,
                    NotificationType     = 2,
                    NotificationSource   = "",
                    NotificationSourceId = question.Id,
                    IsOpen = question.IsOpen,
                });

                return(new JsonResultEx(result));
            }
        }
Ejemplo n.º 2
0
        public JsonResult Detail(long questionId, int replyPageIndex, int replyPageSize)
        {
            CheckPermission();
            using (var result = new ResponseResult <QuestionDetailWithReplyListOutput>())
            {
                //获取问题详情
                var question = _expertQuestionService.GetByKey(questionId);
                if (question == null)
                {
                    throw new CustomException("问题不存在!");
                }

                if (question.IsDeleted)
                {
                    throw new CustomException("问题已被删除!");
                }

                var model = new QuestionDetailWithReplyListOutput
                {
                    CreateTime       = question.CreateTime,
                    LastModifiedTime = question.LastModifiedTime,
                    QuestionId       = question.Id,
                    Title            = question.Title,
                    Description      = question.Description
                };
                var user = _userService.GetByKey(question.UserId);
                model.CreateUser = user.UserName.DefaultIfEmpty(user.LoginUserName);

                long totalCount;
                var  list = _expertQuestionReplyService.GetAll <DateTime?>(m => m.QuestionId == questionId,
                                                                           m => m.LastModifiedTime, null, replyPageIndex, replyPageSize, out totalCount);

                var questionReplys = list as T_QUESTION_REPLY[] ?? list.ToArray();
                if (questionReplys.Any())
                {
                    //提取用户编号
                    var userIdList = questionReplys.Select(m => m.UserId).Distinct().ToArray();
                    var userList   = _userService.GetAll(m => userIdList.Contains(m.Id));
                    model.ReplyList = Mapper.Map <List <QuestionReply> >(list);
                    var enumerable = userList as T_USER[] ?? userList.ToArray();
                    foreach (var reply in model.ReplyList)
                    {
                        var replyUser = enumerable.First(m => m.Id == reply.UserId);
                        reply.UserName = string.IsNullOrEmpty(replyUser.UserName) ? replyUser.LoginUserName : replyUser.UserName;
                    }
                }
                result.Entity = model;
                SetJosnResult <QuestionDetailWithReplyListOutput>(result, replyPageIndex, replyPageSize, totalCount, "获取问题详情成功!");
                return(new JsonResultEx(result));
            }
        }