|
@@ -0,0 +1,206 @@
|
|
|
+package cn.efunbox.audio.impl.hag;
|
|
|
+
|
|
|
+import cn.efunbox.audio.entity.hag.HagAlbum;
|
|
|
+import cn.efunbox.audio.entity.hag.HagContent;
|
|
|
+import cn.efunbox.audio.repository.hag.HagAlbumRepository;
|
|
|
+import cn.efunbox.audio.repository.hag.HagContentRepository;
|
|
|
+import cn.efunbox.audio.service.hag.HagContentService;
|
|
|
+import cn.efunbox.audio.utils.DateUtil;
|
|
|
+import cn.efunbox.audio.utils.MD5;
|
|
|
+import cn.efunbox.audio.vo.hag.*;
|
|
|
+import com.alibaba.fastjson.JSON;
|
|
|
+import lombok.extern.slf4j.Slf4j;
|
|
|
+import org.apache.commons.lang3.StringUtils;
|
|
|
+import org.springframework.beans.factory.annotation.Autowired;
|
|
|
+import org.springframework.beans.factory.annotation.Value;
|
|
|
+import org.springframework.stereotype.Service;
|
|
|
+import org.springframework.util.CollectionUtils;
|
|
|
+
|
|
|
+import java.text.SimpleDateFormat;
|
|
|
+import java.util.*;
|
|
|
+
|
|
|
+
|
|
|
+ * HagServiceImpl
|
|
|
+ * Created by xusq on 2019/12/25.
|
|
|
+ */
|
|
|
+@Slf4j
|
|
|
+@Service
|
|
|
+public class HagContentServiceImpl implements HagContentService {
|
|
|
+
|
|
|
+ @Value("${hag.oss.audio.prefix}")
|
|
|
+ private String hagAudioPrefix;
|
|
|
+
|
|
|
+ @Value("${hag.cdn.secret.sign.key}")
|
|
|
+ private String cdnSecret;
|
|
|
+
|
|
|
+ @Autowired
|
|
|
+ private HagContentRepository hagContentRepository;
|
|
|
+ @Autowired
|
|
|
+ private HagAlbumRepository hagAlbumRepository;
|
|
|
+
|
|
|
+
|
|
|
+ @Override
|
|
|
+ public HagBaseResp list(HagContentReq hagContentReq) {
|
|
|
+
|
|
|
+ log.info("hag request content data param : {}",JSON.toJSONString(hagContentReq));
|
|
|
+ Pagination paginationReq = hagContentReq.getPagination();
|
|
|
+ Integer limit = paginationReq.getLimit();
|
|
|
+ if (Objects.isNull(limit)) {
|
|
|
+ limit = 10;
|
|
|
+ }
|
|
|
+ Integer start = paginationReq.getStart();
|
|
|
+ if (Objects.isNull(start)) {
|
|
|
+ start = 0;
|
|
|
+ }
|
|
|
+
|
|
|
+ HagContentResp hagContentResp = new HagContentResp();
|
|
|
+
|
|
|
+ String updateTime = hagContentReq.getTimestamp();
|
|
|
+ if (StringUtils.isBlank(updateTime)) {
|
|
|
+ updateTime = "20190101000000";
|
|
|
+ }
|
|
|
+ Date date = DateUtil.strToDate(updateTime);
|
|
|
+ if (Objects.isNull(date)) {
|
|
|
+ hagContentResp.setErrorCode("400");
|
|
|
+ hagContentResp.setErrorMessage("timestamp数据异常");
|
|
|
+ return hagContentResp;
|
|
|
+ }
|
|
|
+
|
|
|
+ List<HagAlbum> hagAlbums = hagAlbumRepository.findByGmtModifiedAfter(date);
|
|
|
+
|
|
|
+ List<ContentVO> contentVOList = new ArrayList<>();
|
|
|
+ if (!CollectionUtils.isEmpty(hagAlbums)) {
|
|
|
+ hagAlbums.forEach(album -> contentVOList.add(AlbumConverter.albumToContentVO(album)));
|
|
|
+ }
|
|
|
+
|
|
|
+ List<HagContent> hagContents = hagContentRepository.findByGmtModifiedAfter(date);
|
|
|
+ if (!CollectionUtils.isEmpty(hagContents)) {
|
|
|
+ hagContents.forEach(content -> contentVOList.add(ContentConverter.contentToContentVO(content)));
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+ int size = contentVOList.size();
|
|
|
+ if (size < start) {
|
|
|
+ return hagContentResp;
|
|
|
+ }
|
|
|
+ int toIndex = start + limit;
|
|
|
+ if (size < toIndex) {
|
|
|
+ toIndex = size;
|
|
|
+ }
|
|
|
+ List<ContentVO> contentVOResp = contentVOList.subList(start, toIndex);
|
|
|
+ Pagination pagination = new Pagination();
|
|
|
+ pagination.setNext(toIndex + "");
|
|
|
+ pagination.setTotal(Long.valueOf(contentVOList.size()));
|
|
|
+
|
|
|
+
|
|
|
+ hagContentResp.setSource(DateUtil.getDateStr() +"-同步数据");
|
|
|
+ hagContentResp.setPagination(pagination);
|
|
|
+
|
|
|
+ hagContentResp.setContentItems(contentVOResp);
|
|
|
+ return hagContentResp;
|
|
|
+ }
|
|
|
+
|
|
|
+ @Override
|
|
|
+ public HagSkillResp getResUrl(HagSkillReq skillReq) {
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+ HagSkillReq.Endpoint endpoint = skillReq.getEndpoint();
|
|
|
+ if (Objects.isNull(endpoint)) {
|
|
|
+ return fillFailResp("400","参数错误");
|
|
|
+ }
|
|
|
+
|
|
|
+ HagSkillReq.Endpoint.Device device = endpoint.getDevice();
|
|
|
+ if (Objects.isNull(device)) {
|
|
|
+ return fillFailResp("400","device信息为空");
|
|
|
+ }
|
|
|
+ String deviceId = device.getDeviceId();
|
|
|
+ if (StringUtils.isBlank(deviceId)) {
|
|
|
+ return fillFailResp("400","deviceId为空");
|
|
|
+ }
|
|
|
+ HagSkillReq.HagInquire inquire = skillReq.getInquire();
|
|
|
+ if (Objects.isNull(inquire.getIntent())) {
|
|
|
+ return fillFailResp("400","intent为空");
|
|
|
+ }
|
|
|
+ HagSkillReq.HagInquire.Intent.Slots slots = inquire.getIntent().getSlots();
|
|
|
+ if (Objects.isNull(slots)) {
|
|
|
+ return fillFailResp("400","slots为空");
|
|
|
+ }
|
|
|
+
|
|
|
+ if (Objects.isNull(albumCode)) {
|
|
|
+ return fillFailResp("400","albumCode为空");
|
|
|
+ }*/
|
|
|
+ HagSkillReq.HagInquire.Intent.Slots.ContentCode contentCode = slots.getContentCode();
|
|
|
+ if (Objects.isNull(contentCode)) {
|
|
|
+ return fillFailResp("400","contentCode为空");
|
|
|
+ }
|
|
|
+ List<HagSkillReq.HagInquire.Intent.Slots.ContentCode.Value> values = contentCode.getValues();
|
|
|
+ if (CollectionUtils.isEmpty(values)) {
|
|
|
+ return fillFailResp("400","contentCode为空");
|
|
|
+ }
|
|
|
+
|
|
|
+ String real = values.get(0).getReal();
|
|
|
+ HagContent hagContent = hagContentRepository.find(real);
|
|
|
+ if (Objects.isNull(hagContent)) {
|
|
|
+ return fillFailResp("404","媒资数据未找到");
|
|
|
+ }
|
|
|
+
|
|
|
+ return fillSuccessResp(hagContent.getUrl());
|
|
|
+ }
|
|
|
+
|
|
|
+ private HagSkillResp fillFailResp(String errorCode,String errorMessage) {
|
|
|
+ HagSkillResp hagSkillResp = new HagSkillResp();
|
|
|
+ hagSkillResp.setErrorCode(errorCode);
|
|
|
+ hagSkillResp.setErrorMessage(errorMessage);
|
|
|
+ hagSkillResp.setVersion("1.0.0");
|
|
|
+ HagSkillResp.Reply<HagSkillReq.HagInquire.Intent> reply = new HagSkillResp.Reply();
|
|
|
+ reply.setIsEndSession(Boolean.TRUE);
|
|
|
+ HagSkillResp.Reply.Command command = new HagSkillResp.Reply.Command();
|
|
|
+ command.setNamespace("AudioPlayer");
|
|
|
+ command.setName("Play");
|
|
|
+ AudioUrl audioUrl = new AudioUrl();
|
|
|
+ audioUrl.setUrl("");
|
|
|
+ command.setBody(audioUrl);
|
|
|
+ reply.setCommands(Arrays.asList(command));
|
|
|
+ hagSkillResp.setReply(reply);
|
|
|
+ return hagSkillResp;
|
|
|
+ }
|
|
|
+
|
|
|
+ private HagSkillResp fillSuccessResp(String url) {
|
|
|
+ HagSkillResp hagSkillResp = new HagSkillResp();
|
|
|
+ hagSkillResp.setErrorCode("0");
|
|
|
+ hagSkillResp.setErrorMessage("OK");
|
|
|
+ hagSkillResp.setVersion("1.0.0");
|
|
|
+ HagSkillResp.Reply<Map<String,List>> reply = new HagSkillResp.Reply();
|
|
|
+ reply.setIsEndSession(Boolean.TRUE);
|
|
|
+ HagSkillResp.Reply.Command command = new HagSkillResp.Reply.Command();
|
|
|
+ command.setNamespace("AudioPlayer");
|
|
|
+ command.setName("Play");
|
|
|
+ AudioUrl audioUrl = new AudioUrl();
|
|
|
+ audioUrl.setUrl(hagAudioPrefix + md5PlayUrl(cdnSecret, url));
|
|
|
+ Map<String,List<AudioUrl>> map = new HashMap<>();
|
|
|
+ map.put("audioItemList",Arrays.asList(audioUrl));
|
|
|
+ Map<String,Map<String,List<AudioUrl>>> bodyMap = new HashMap<>();
|
|
|
+ bodyMap.put("templateContent",map);
|
|
|
+ command.setBody(bodyMap);
|
|
|
+ reply.setCommands(Arrays.asList(command));
|
|
|
+ hagSkillResp.setReply(reply);
|
|
|
+ return hagSkillResp;
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+ SimpleDateFormat sdf = new SimpleDateFormat("YYYYMMddHHmm");
|
|
|
+ long endTime = System.currentTimeMillis() + (3 * 60 * 1000L);
|
|
|
+ Date date = new Date(endTime);
|
|
|
+ String time = sdf.format(date);
|
|
|
+ String md5Url = MD5.MD5Encode(key + time + url);
|
|
|
+ return "/" + time + "/" + md5Url + url;
|
|
|
+ }*/
|
|
|
+
|
|
|
+ private String md5PlayUrl(String key, String url){
|
|
|
+ long time = System.currentTimeMillis() / 1000;
|
|
|
+ String md5Url = MD5.MD5Encode(key + url + time);
|
|
|
+ return "/"+ md5Url + "/" + time + url;
|
|
|
+ }
|
|
|
+}
|