xiangpei
4 天以前 2f68e5600f0b60d6f8d170f4536e1fc410662ea7
framework/src/main/java/cn/lili/modules/lmk/service/impl/VideoServiceImpl.java
@@ -2,9 +2,11 @@
import cn.lili.cache.Cache;
import cn.lili.cache.CachePrefix;
import cn.lili.common.properties.RocketmqCustomProperties;
import cn.lili.common.security.context.UserContext;
import cn.lili.elasticsearch.EsSuffix;
import cn.lili.modules.lmk.constant.RedisKeyExpireConstant;
import cn.lili.modules.lmk.domain.dto.VideoEsUpdateDTO;
import cn.lili.modules.lmk.domain.entity.*;
import cn.lili.modules.lmk.domain.es.VideoIndex;
import cn.lili.modules.lmk.domain.form.*;
@@ -16,6 +18,9 @@
import cn.lili.modules.member.entity.dos.Member;
import cn.lili.modules.member.service.FootprintService;
import cn.lili.modules.member.service.MemberService;
import cn.lili.rocketmq.RocketmqSendCallbackBuilder;
import cn.lili.rocketmq.tags.CommentTagsEnum;
import cn.lili.rocketmq.tags.VideoTagsEnum;
import cn.lili.utils.COSUtil;
import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.core.metadata.IPage;
@@ -28,6 +33,7 @@
import org.apache.commons.collections4.CollectionUtils;
import org.apache.commons.collections4.ListUtils;
import org.apache.commons.lang3.StringUtils;
import org.apache.rocketmq.spring.core.RocketMQTemplate;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Service;
import lombok.RequiredArgsConstructor;
@@ -65,37 +71,9 @@
    private final KitchenTypeService kitchenTypeService;
    private final Cache cache;
    @Qualifier("videoEsServiceImpl")
    private final EsService videoEsService;
    private final RocketmqCustomProperties rocketmqCustomProperties;
    private final RocketMQTemplate rocketMQTemplate;
    /**
     * 添加
     * @param form
     * @return
     */
    @Override
    public Result add(WxVideoForm form) {
        Video entity = WxVideoForm.getEntityByForm(form, null);
        baseMapper.insert(entity);
        return Result.ok("添加成功");
    }
    /**
     * 修改
     * @param form
     * @return
     */
    @Override
    public Result update(WxVideoForm form) {
        Video entity = baseMapper.selectById(form.getId());
        // 为空抛IllegalArgumentException,做全局异常处理
        Assert.notNull(entity, "记录不存在");
        BeanUtils.copyProperties(form, entity);
        baseMapper.updateById(entity);
        return Result.ok("修改成功");
    }
    /**
     * 批量删除
@@ -123,7 +101,9 @@
        new LambdaUpdateChainWrapper<>(videoTagRefService.getBaseMapper())
                .eq(VideoTagRef::getVideoId, id)
                .remove();
        videoEsService.deleteDocument(EsSuffix.VIDEO_INDEX_NAME, id);
        // mq异步删除es数据
        String destination = rocketmqCustomProperties.getVideoTopic() + ":" + VideoTagsEnum.ES_DOC_DEL.name();
        rocketMQTemplate.asyncSend(destination, id, RocketmqSendCallbackBuilder.commonCallback());
        return Result.ok("删除成功");
    }
@@ -185,6 +165,7 @@
        video.setStatus(VideoStatusEnum.AUDITING.getValue());
        video.setCoverUrl(form.getCover());
        video.setVideoType(VideoTypeEnum.VIDEO.getValue());
        video.setRecommend(Boolean.FALSE);
        if (VideoContentTypeEnum.IMG.getValue().equals(form.getVideoContentType())) {
            video.setVideoImgs(JSON.toJSONString(form.getVideoImgs()));
        }
@@ -234,7 +215,7 @@
            }
            videoGoodsService.saveBatch(videoGoods);
        }
        // 5. 构建es中数据
        // 5. 构建es中数据,mq异步处理
        VideoIndex videoIndex = new VideoIndex();
        BeanUtils.copyProperties(video, videoIndex);
        videoIndex.setCoverFileKey(video.getCoverUrl());
@@ -245,7 +226,8 @@
        }).collect(Collectors.toList());
        videoIndex.setGoodsList(esGoodsList);
        videoIndex.setTagList(esTagList);
        videoEsService.addOrUpdateDocument(EsSuffix.VIDEO_INDEX_NAME, video.getId(), videoIndex);
        String destination = rocketmqCustomProperties.getVideoTopic() + ":" + VideoTagsEnum.ES_DOC_ADD_OR_UPDATE.name();
        rocketMQTemplate.asyncSend(destination, JSON.toJSONString(videoIndex), RocketmqSendCallbackBuilder.commonCallback());
        return Result.ok("发布成功,视频审核中~");
    }
@@ -316,7 +298,7 @@
            }
            videoGoodsService.saveBatch(videoGoods);
        }
        // 5. 更新es中的数据
        // 5. 更新es中的数据,mq异步处理
        VideoIndex videoIndex = new VideoIndex();
        BeanUtils.copyProperties(video, videoIndex);
        videoIndex.setCoverFileKey(video.getCoverUrl());
@@ -327,7 +309,8 @@
        }).collect(Collectors.toList());
        videoIndex.setGoodsList(esGoodsList);
        videoIndex.setTagList(esTagList);
        videoEsService.addOrUpdateDocument(EsSuffix.VIDEO_INDEX_NAME, video.getId(), videoIndex);
        String destination = rocketmqCustomProperties.getVideoTopic() + ":" + VideoTagsEnum.ES_DOC_ADD_OR_UPDATE.name();
        rocketMQTemplate.asyncSend(destination, JSON.toJSONString(videoIndex), RocketmqSendCallbackBuilder.commonCallback());
        return Result.ok("发布成功,视频审核中~");
    }
@@ -356,9 +339,15 @@
                .eq(Video::getId, form.getId())
                .set(Video::getRecommend, form.getRecommend())
                .update();
        // mq异步更新es
        Map<String, Object> fields = new HashMap<>(2);
        fields.put("recommend", form.getRecommend());
        videoEsService.updateSomeField(EsSuffix.VIDEO_INDEX_NAME, form.getId(), fields);
        VideoEsUpdateDTO dto = new VideoEsUpdateDTO();
        dto.setId(form.getId());
        dto.setFields(fields);
        String destination = rocketmqCustomProperties.getVideoTopic() + ":" + VideoTagsEnum.ES_DOC_UPDATE_SOME_FIELD.name();
        rocketMQTemplate.asyncSend(destination, JSON.toJSONString(dto), RocketmqSendCallbackBuilder.commonCallback());
        return Result.ok("设置成功");
    }
@@ -378,17 +367,24 @@
        }
        videoAuditRecordService.save(auditRecord);
        // 2. 修改视频状态
        Map<String, Object> fields = new HashMap<>(2);
        if (form.getResult()) {
            video.setStatus(VideoStatusEnum.PUBLISHED.getValue());
            video.setAuditPassTime(new Date());
            Map<String, Object> fields = new HashMap<>(2);
            fields.put("status", VideoStatusEnum.PUBLISHED.getValue());
            videoEsService.updateSomeField(EsSuffix.VIDEO_INDEX_NAME, video.getId(), fields);
        } else {
            video.setStatus(VideoStatusEnum.REJECT.getValue());
            fields.put("status", VideoStatusEnum.REJECT.getValue());
        }
        baseMapper.updateById(video);
        // 3. mq异步更新es
        VideoEsUpdateDTO dto = new VideoEsUpdateDTO();
        dto.setId(video.getId());
        dto.setFields(fields);
        String destination = rocketmqCustomProperties.getVideoTopic() + ":" + VideoTagsEnum.ES_DOC_UPDATE_SOME_FIELD.name();
        rocketMQTemplate.asyncSend(destination, JSON.toJSONString(dto), RocketmqSendCallbackBuilder.commonCallback());
        return Result.ok();
    }
@@ -400,10 +396,14 @@
                .eq(Video::getId, id)
                .set(Video::getStatus, VideoStatusEnum.PUBLISHED.getValue())
                .update();
        // 2. 更新es
        // 2. mq异步更新es
        Map<String, Object> fields = new HashMap<>(2);
        fields.put("status", VideoStatusEnum.PUBLISHED.getValue());
        videoEsService.updateSomeField(EsSuffix.VIDEO_INDEX_NAME, id, fields);
        VideoEsUpdateDTO dto = new VideoEsUpdateDTO();
        dto.setId(id);
        dto.setFields(fields);
        String destination = rocketmqCustomProperties.getVideoTopic() + ":" + VideoTagsEnum.ES_DOC_UPDATE_SOME_FIELD.name();
        rocketMQTemplate.asyncSend(destination, JSON.toJSONString(dto), RocketmqSendCallbackBuilder.commonCallback());
        return Result.ok("上架成功");
    }
@@ -415,10 +415,14 @@
                .eq(Video::getId, form.getId())
                .set(Video::getStatus, VideoStatusEnum.DISABLE.getValue())
                .update();
        // 2. 更新es
        // 2. mq异步更新es
        Map<String, Object> fields = new HashMap<>(2);
        fields.put("status", VideoStatusEnum.DISABLE.getValue());
        videoEsService.updateSomeField(EsSuffix.VIDEO_INDEX_NAME, form.getId(), fields);
        VideoEsUpdateDTO dto = new VideoEsUpdateDTO();
        dto.setId(form.getId());
        dto.setFields(fields);
        String destination = rocketmqCustomProperties.getVideoTopic() + ":" + VideoTagsEnum.ES_DOC_UPDATE_SOME_FIELD.name();
        rocketMQTemplate.asyncSend(destination, JSON.toJSONString(dto), RocketmqSendCallbackBuilder.commonCallback());
        // TODO 将下架原因以通知的方式告知用户
        return Result.ok("下架成功");
@@ -430,6 +434,14 @@
                .eq(Video::getId, id)
                .set(Video::getStatus, VideoStatusEnum.DISABLE.getValue())
                .update();
        // 2. mq异步更新es
        Map<String, Object> fields = new HashMap<>(2);
        fields.put("status", VideoStatusEnum.DISABLE.getValue());
        VideoEsUpdateDTO dto = new VideoEsUpdateDTO();
        dto.setId(id);
        dto.setFields(fields);
        String destination = rocketmqCustomProperties.getVideoTopic() + ":" + VideoTagsEnum.ES_DOC_UPDATE_SOME_FIELD.name();
        rocketMQTemplate.asyncSend(destination, JSON.toJSONString(dto), RocketmqSendCallbackBuilder.commonCallback());
        return Result.ok("下架成功");
    }
@@ -883,4 +895,12 @@
                    .update();
        }
    }
    @Override
    public Result recreateEsIndex() {
        String destination = rocketmqCustomProperties.getVideoTopic() + ":" + VideoTagsEnum.ES_RECREATE.name();
        // 消息体不能为空,随便传一个1
        rocketMQTemplate.asyncSend(destination, "1", RocketmqSendCallbackBuilder.commonCallback());
        return Result.ok("已成功发起构建请求,稍作等待后便会自动完成");
    }
}