using System; using System.Collections.Generic; using System.Data.Entity; using System.Linq; using System.Threading.Tasks; using System.Web.Mvc; using Abp.Application.Services.Dto; using Abp.Auditing; using Abp.Authorization; using Abp.Domain.Repositories; using Abp.Runtime.Caching; using IwbZero.Auditing; using IwbZero.AppServiceBase; using IwbZero.ToolCommon.Lambda; using WeOnlineApp.Authorization.Users; using WeOnlineApp.Configuration; using WeOnlineApp.Question.Dto; using WeOnlineApp.TrainingCamp; using IwbZero.ToolCommon.StringModel; namespace WeOnlineApp.Question { [AbpAuthorize, AuditLog("问题")] public class QuestionAppService : IwbAsyncCrudAppService, IQuestionAppService { public QuestionAppService( ICacheManager cacheManager, IRepository repository, IRepository qaRepository, IRepository campRepository, IRepository userRepository, IRepository qfRepository) : base(repository, "Id") { QaRepository = qaRepository; CampRepository = campRepository; UserRepository = userRepository; QfRepository = qfRepository; CacheManager = cacheManager; } protected override bool KeyIsAuto { get; set; } = false; protected IRepository QaRepository { get; } protected IRepository CampRepository { get; } protected IRepository QfRepository { get; } protected IRepository UserRepository { get; } #region GetSelect [DisableAuditing] public override async Task> GetSelectList() { var list = await Repository.GetAllListAsync(); var sList = new List {new SelectListItem {Text = @"请选择...", Value = "", Selected = true}}; foreach (var l in list) { //sList.Add(new SelectListItem { Value = l.Id, Text = l. }); } return sList; } [DisableAuditing] public override async Task GetSelectStr() { var list = await Repository.GetAllListAsync(); string str = ""; foreach (var l in list) { //str += $""; } return str; } #endregion #region CURD //[AbpAuthorize(PermissionNames.PagesQuestionMgCreate)] [AuditLog("提问")] public override async Task Create(QuestionCreateDto input) { var camp = await CampRepository.FirstOrDefaultAsync(a => a.Id == input.CampNo); if (camp != null) { input.SubjectCategoryNo = camp.SubjectType; } input.Id = await AppGuidManager.GetNextRecordIdAsync(DataLibType.Question); input.QuestionState = QuestionStateDefinition.New; await CreateEntity(input); } //[AbpAuthorize(PermissionNames.PagesQuestionMgUpdate)] public override async Task Update(QuestionUpdateDto input) { await UpdateEntity(input); } //[AbpAuthorize(PermissionNames.PagesQuestionMgDelete)] public override Task Delete(EntityDto input) { return DeleteEntity(input); } [DisableAuditing] //[AbpAuthorize(PermissionNames.PagesQuestionMgQuery)] public override async Task> GetAll(IwbPagedRequestDto input) { var query = CreateFilteredQuery(input); //query = ApplyFilter(query, input); var totalCount = await AsyncQueryableExecuter.CountAsync(query); query = ApplySorting(query, input); query = ApplyPaging(query, input); var q1 = query.Select(a => new QuestionDto() { Id = a.Id, Title = a.Title, QuestionState = a.QuestionState, AnswerDateTime = a.AnswerDateTime, IsFavorite = false, SubjectCategoryNo = a.SubjectCategoryNo, SubjectCategoryName = a.SubjectCategory.CategoryName, CampName = a.Camp.Name }); ; var entities = await AsyncQueryableExecuter.ToListAsync(q1); var dtoList = new PagedResultDto(totalCount, entities); return dtoList; } #region GetEntity/Dto /// /// 查询实体Dto /// /// /// [DisableAuditing] //[AbpAuthorize(PermissionNames.PagesQuestionMgQuery)] public override async Task GetDto(EntityDto input) { var entity = await GetEntity(input); return MapToEntityDto(entity); } /// /// 查询实体Dto /// /// /// [DisableAuditing] //[AbpAuthorize(PermissionNames.PagesQuestionMgQuery)] public override async Task GetDtoById(string id) { var entity = await GetEntityById(id); return MapToEntityDto(entity); } /// /// 查询实体Dto(需指明自定义字段) /// /// /// [DisableAuditing] //[AbpAuthorize(PermissionNames.PagesQuestionMgQuery)] public override async Task GetDtoByNo(string no) { var entity = await GetEntityByNo(no); return MapToEntityDto(entity); } /// /// 查询实体 /// /// /// [DisableAuditing] //[AbpAuthorize(PermissionNames.PagesQuestionMgQuery)] public override async Task GetEntity(EntityDto input) { var entity = await GetEntityById(input.Id); return entity; } /// /// 查询实体 /// /// /// [DisableAuditing] //[AbpAuthorize(PermissionNames.PagesQuestionMgQuery)] public override async Task GetEntityById(string id) { return await Repository.FirstOrDefaultAsync(a=>a.Id==id); } /// /// 查询实体(需指明自定义字段) /// /// /// [DisableAuditing] //[AbpAuthorize(PermissionNames.PagesQuestionMgQuery)] public override async Task GetEntityByNo(string no) { //CheckGetPermission(); if (string.IsNullOrEmpty(KeyFiledName)) { ThrowError("NoKeyFieldName"); } return await base.GetEntityByNo(no); } #endregion #region Hide /// /// 根据给定的创建 过滤查询. /// /// The input. protected override IQueryable CreateFilteredQuery(IwbPagedRequestDto input) { var query = Repository.GetAllIncluding(a=>a.SubjectCategory,a=>a.Camp,a=>a.Favorites); var pagedInput = input as IIwbPagedRequest; if (pagedInput == null) { return query; } query = query.Where(a => string.IsNullOrEmpty(input.KeyWords) || a.SubjectCategory.CategoryPath.Contains(input.KeyWords)); if (!string.IsNullOrEmpty(input.KeyField)) { query = query.Where(a => a.Favorites.Count(s => s.UserId == AbpSession.UserId) > 0); } if (pagedInput.SearchList != null && pagedInput.SearchList.Count > 0) { List objList = new List(); foreach (var o in pagedInput.SearchList) { if (string.IsNullOrEmpty(o.KeyWords)) continue; object keyWords = o.KeyWords; objList.Add(new LambdaObject { FieldType = (LambdaFieldType)o.FieldType, FieldName = o.KeyField, FieldValue = keyWords, ExpType = (LambdaExpType)o.ExpType }); } var exp = objList.GetExp(); query = exp != null ? query.Where(exp) : query; } return query; } //protected override IQueryable ApplySorting(IQueryable query, IwbPagedRequestDto input) //{ // return query.OrderBy(a => a.No); //} //protected override IQueryable ApplyPaging(IQueryable query, IwbPagedRequestDto input) //{ // if (input is IPagedResultRequest pagedInput) // { // return query.Skip(pagedInput.SkipCount).Take(pagedInput.MaxResultCount); // } // return query; //} #endregion #endregion /// /// 查询问题详情 /// /// /// [DisableAuditing] public async Task GeQuestionDetail(string no) { var entity = await Repository.GetAllIncluding(a => a.Camp, a => a.SubjectCategory,a=>a.AnswerInfos) .FirstOrDefaultAsync(a => a.Id == no); if (entity == null) { CheckErrors("未查询到问题!"); return null; } var dto = new QuestionDto() { Id = entity.Id, Title = entity.Title, QuestionState = entity.QuestionState, AnswerDateTime = entity.AnswerDateTime, SubjectCategoryNo = entity.SubjectCategoryNo, SubjectCategoryName = entity.SubjectCategory.CategoryName, IsFavorite = (await QfRepository.CountAsync(a => a.QuestionNo == entity.Id && a.UserId == AbpSession.UserId)) > 0, Camp = entity.Camp, CampName = entity.Camp.Name, ContentInfo = entity.ContentInfo, Answers = entity.AnswerInfos.Where(a => a.AnswerNo == null).OrderByDescending(a => a.AnswerState) .ThenByDescending(a => a.Id).Skip(0).Take(IwbConsts.AnswerTakeCount).Select(AnswerDtoMap).ToList(), CreatorUserId = entity.CreatorUserId }; return dto; } /// /// 获取答疑回复 /// /// /// [DisableAuditing] public async Task> GetChildAnswer(QueryChildAnswerDto input) { var query = QaRepository.GetAll(); if (input.QuestionNo.IsEmpty()) { query = query.Where(a => a.AnswerNo == input.AnswerNo); } else { query = query.Where(a => a.QuestionNo == input.QuestionNo); } query = query.OrderByDescending(a => a.AnswerState).ThenByDescending(a => a.Id); int skip = input.Skip ?? 0, take = input.Take ?? IwbConsts.AnswerTakeCount; query = query.Skip(skip).Take(take); var entities = await query.ToListAsync(); var list = entities.Select(AnswerDtoMap).ToList(); return list; } private AnswerDto AnswerDtoMap(QuestionAnswerInfo input) { var dto = ObjectMapper.Map(input); dto.ChildrenCount = QaRepository.Count(a => a.AnswerNo == input.Id); var user = QueryUserInfo(input.CreatorUserId); dto.ImagePath = user?.AvatarImagePath; dto.UserName = user?.UserName; dto.RealName = user?.Name; return dto; } private User QueryUserInfo(long? id) { return CacheManager.GetCache(IwbCacheNames.UserInfoCache).Get($"{id}", () => { var user = UserRepository.FirstOrDefault(a => a.Id == id); return user; }); } /// /// 答疑 /// /// /// [AuditLog("答疑")] public async Task AddAnswer(AnswerDto input) { input.Id = $"QA{DateTime.Now:yyyyMMddHHmmssfff}"; input.AnswerNo = input.AnswerNo.IsEmpty() ? null : input.AnswerNo; input.AnswerState = AnswerStateDefinition.New; var answer = ObjectMapper.Map(input); answer = await QaRepository.InsertAsync(answer); await CurrentUnitOfWork.SaveChangesAsync(); var dto = AnswerDtoMap(answer); return dto; } /// /// 收藏 /// /// /// [AuditLog("收藏")] public async Task Favorite(FavoriteDto input) { var entity = await GetEntity(input); if (entity == null) { CheckErrors("未查询到问题!"); return; } if (input.IsFavorite) { await QfRepository.InsertAsync(new QuestionFavoriteInfo() { QuestionNo = entity.Id, UserId = AbpSession.UserId ?? 0 }); } else { await QfRepository.DeleteAsync(a => a.QuestionNo == entity.Id && a.UserId == AbpSession.UserId); } //entity.IsFavorite = input.IsFavorite; await CurrentUnitOfWork.SaveChangesAsync(); } /// /// 采纳答疑 /// /// /// [AuditLog("采纳答疑")] public async Task SelectAnswer(SelectAnswerDto input) { var entity = await GetEntity(input); if (entity == null) { CheckErrors("未查询到问题!"); return; } var answer = await QaRepository.FirstOrDefaultAsync(a => a.Id == input.AnswerNo); if (answer == null) { CheckErrors("未查询到答疑!"); return; } entity.QuestionState = QuestionStateDefinition.Answer; entity.AnswerNo = answer.Id; entity.AnswerDateTime=DateTime.Now; await Repository.UpdateAsync(entity); answer.AnswerState = AnswerStateDefinition.Answer; await QaRepository.UpdateAsync(answer); await CurrentUnitOfWork.SaveChangesAsync(); } } }