MomentMediaService.java 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960
  1. package com.webchat.ugc.service.moment;
  2. import com.webchat.common.constants.MomentConstants;
  3. import com.webchat.domain.vo.request.MomentSaveOrUpdateVO;
  4. import com.webchat.domain.vo.response.moment.MomentMediaVO;
  5. import com.webchat.ugc.repository.dao.IMomentMediaDAO;
  6. import com.webchat.ugc.repository.entity.MomentMediaEntity;
  7. import org.apache.commons.collections.CollectionUtils;
  8. import org.springframework.beans.factory.annotation.Autowired;
  9. import org.springframework.stereotype.Service;
  10. import java.util.ArrayList;
  11. import java.util.List;
  12. import java.util.stream.Collectors;
  13. @Service
  14. public class MomentMediaService {
  15. @Autowired
  16. private IMomentMediaDAO momentMediaDAO;
  17. public boolean saveMomentMedia(Long momentId,
  18. MomentSaveOrUpdateVO momentSaveOrUpdate) {
  19. List<MomentMediaEntity> medias = momentMediaDAO.findAllByMomentId(momentId);
  20. if (CollectionUtils.isNotEmpty(medias)) {
  21. momentMediaDAO.deleteAll(medias);
  22. }
  23. medias = new ArrayList<MomentMediaEntity>();
  24. if (momentSaveOrUpdate.includeImage()) {
  25. medias = momentSaveOrUpdate.getImages().stream().map(img -> {
  26. MomentMediaEntity momentMediaEntity = new MomentMediaEntity();
  27. momentMediaEntity.setMomentId(momentId);
  28. momentMediaEntity.setResource(img);
  29. momentMediaEntity.setType(MomentConstants.MediaType.IMAGE.getType());
  30. return momentMediaEntity;
  31. }).collect(Collectors.toList());
  32. }
  33. if (momentSaveOrUpdate.includeVideo()) {
  34. MomentMediaEntity momentMediaEntity = new MomentMediaEntity();
  35. momentMediaEntity.setMomentId(momentId);
  36. momentMediaEntity.setType(MomentConstants.MediaType.VIDEO.getType());
  37. momentMediaEntity.setResource(momentSaveOrUpdate.getVideo());
  38. medias.add(momentMediaEntity);
  39. }
  40. momentMediaDAO.saveAll(medias);
  41. return true;
  42. }
  43. public List<MomentMediaEntity> medias(Long momentId, int type) {
  44. return momentMediaDAO.findAllByMomentIdAndType(momentId, type);
  45. }
  46. }