/// <summary> /// 添加附件关联 /// </summary> /// <param name="input"></param> /// <returns></returns> public async Task AddObjectAttachmentInfos(AddObjectAttachmentInfosInput input) { var objectType = Enum.Parse <AttachmentObjectTypes>(input.ObjectType); if (objectType == AttachmentObjectTypes.ColumnInfo) { if (!CheckMaxItemCount(input)) { throw new UserFriendlyException(L("ExceedTheMaxCount")); } } var attachmentInfos = await _objectAttachmentInfoRepository.GetAll().Where(p => p.ObjectId == input.ObjectId && p.ObjectType == objectType).ToListAsync(); var objectAttachmentInfos = input.AttachmentInfoIds.Select(p => new ObjectAttachmentInfo { ObjectType = objectType, ObjectId = input.ObjectId, AttachmentInfoId = p }).ToList(); foreach (var objectAttachmentInfo in objectAttachmentInfos) { if (attachmentInfos == null || attachmentInfos.Count == 0 || (attachmentInfos.All(p => p.AttachmentInfoId != objectAttachmentInfo.AttachmentInfoId))) { await _objectAttachmentInfoRepository.InsertAsync(objectAttachmentInfo); } } }
private bool CheckMaxItemCount(AddObjectAttachmentInfosInput input) { var columnInfoMaxItemCount = _columnInfoRepository.Get(input.ObjectId).MaxItemCount; if (!columnInfoMaxItemCount.HasValue) { return(true); } var columnInfoCurrentCount = _objectAttachmentInfoRepository.GetAll().Count(a => a.ObjectId == input.ObjectId); return(columnInfoMaxItemCount > columnInfoCurrentCount); }