package com.webchat.ugc.service.moment; import com.webchat.common.constants.MomentConstants; import com.webchat.domain.vo.request.MomentSaveOrUpdateVO; import com.webchat.domain.vo.response.moment.MomentMediaVO; import com.webchat.ugc.repository.dao.IMomentMediaDAO; import com.webchat.ugc.repository.entity.MomentMediaEntity; import org.apache.commons.collections.CollectionUtils; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Service; import java.util.ArrayList; import java.util.List; import java.util.stream.Collectors; @Service public class MomentMediaService { @Autowired private IMomentMediaDAO momentMediaDAO; public boolean saveMomentMedia(Long momentId, MomentSaveOrUpdateVO momentSaveOrUpdate) { List medias = momentMediaDAO.findAllByMomentId(momentId); if (CollectionUtils.isNotEmpty(medias)) { momentMediaDAO.deleteAll(medias); } medias = new ArrayList(); if (momentSaveOrUpdate.includeImage()) { medias = momentSaveOrUpdate.getImages().stream().map(img -> { MomentMediaEntity momentMediaEntity = new MomentMediaEntity(); momentMediaEntity.setMomentId(momentId); momentMediaEntity.setResource(img); momentMediaEntity.setType(MomentConstants.MediaType.IMAGE.getType()); return momentMediaEntity; }).collect(Collectors.toList()); } if (momentSaveOrUpdate.includeVideo()) { MomentMediaEntity momentMediaEntity = new MomentMediaEntity(); momentMediaEntity.setMomentId(momentId); momentMediaEntity.setType(MomentConstants.MediaType.VIDEO.getType()); momentMediaEntity.setResource(momentSaveOrUpdate.getVideo()); medias.add(momentMediaEntity); } momentMediaDAO.saveAll(medias); return true; } public List medias(Long momentId, int type) { return momentMediaDAO.findAllByMomentIdAndType(momentId, type); } }