HagContentServiceImpl.java 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206
  1. package cn.efunbox.audio.impl.hag;
  2. import cn.efunbox.audio.entity.hag.HagAlbum;
  3. import cn.efunbox.audio.entity.hag.HagContent;
  4. import cn.efunbox.audio.repository.hag.HagAlbumRepository;
  5. import cn.efunbox.audio.repository.hag.HagContentRepository;
  6. import cn.efunbox.audio.service.hag.HagContentService;
  7. import cn.efunbox.audio.utils.DateUtil;
  8. import cn.efunbox.audio.utils.MD5;
  9. import cn.efunbox.audio.vo.hag.*;
  10. import com.alibaba.fastjson.JSON;
  11. import lombok.extern.slf4j.Slf4j;
  12. import org.apache.commons.lang3.StringUtils;
  13. import org.springframework.beans.factory.annotation.Autowired;
  14. import org.springframework.beans.factory.annotation.Value;
  15. import org.springframework.stereotype.Service;
  16. import org.springframework.util.CollectionUtils;
  17. import java.text.SimpleDateFormat;
  18. import java.util.*;
  19. /**
  20. * HagServiceImpl
  21. * Created by xusq on 2019/12/25.
  22. */
  23. @Slf4j
  24. @Service
  25. public class HagContentServiceImpl implements HagContentService {
  26. @Value("${hag.oss.audio.prefix}")
  27. private String hagAudioPrefix;
  28. @Value("${hag.cdn.secret.sign.key}")
  29. private String cdnSecret;
  30. @Autowired
  31. private HagContentRepository hagContentRepository;
  32. @Autowired
  33. private HagAlbumRepository hagAlbumRepository;
  34. @Override
  35. public HagBaseResp list(HagContentReq hagContentReq) {
  36. log.info("hag request content data param : {}",JSON.toJSONString(hagContentReq));
  37. Pagination paginationReq = hagContentReq.getPagination();
  38. Integer limit = paginationReq.getLimit();
  39. if (Objects.isNull(limit)) {
  40. limit = 10;
  41. }
  42. Integer start = paginationReq.getStart();
  43. if (Objects.isNull(start)) {
  44. start = 0;
  45. }
  46. HagContentResp hagContentResp = new HagContentResp();
  47. String updateTime = hagContentReq.getTimestamp();
  48. if (StringUtils.isBlank(updateTime)) {
  49. updateTime = "20190101000000";
  50. }
  51. Date date = DateUtil.strToDate(updateTime);
  52. if (Objects.isNull(date)) {
  53. hagContentResp.setErrorCode("400");
  54. hagContentResp.setErrorMessage("timestamp数据异常");
  55. return hagContentResp;
  56. }
  57. List<HagAlbum> hagAlbums = hagAlbumRepository.findByGmtModifiedAfter(date);
  58. List<ContentVO> contentVOList = new ArrayList<>();
  59. if (!CollectionUtils.isEmpty(hagAlbums)) {
  60. hagAlbums.forEach(album -> contentVOList.add(AlbumConverter.albumToContentVO(album)));
  61. }
  62. List<HagContent> hagContents = hagContentRepository.findByGmtModifiedAfter(date);
  63. if (!CollectionUtils.isEmpty(hagContents)) {
  64. hagContents.forEach(content -> contentVOList.add(ContentConverter.contentToContentVO(content)));
  65. }
  66. int size = contentVOList.size();
  67. if (size < start) {
  68. return hagContentResp;
  69. }
  70. int toIndex = start + limit;
  71. if (size < toIndex) {
  72. toIndex = size;
  73. }
  74. List<ContentVO> contentVOResp = contentVOList.subList(start, toIndex);
  75. Pagination pagination = new Pagination();
  76. pagination.setNext(toIndex + "");
  77. pagination.setTotal(Long.valueOf(contentVOList.size()));
  78. hagContentResp.setSource(DateUtil.getDateStr() +"-同步数据");
  79. hagContentResp.setPagination(pagination);
  80. hagContentResp.setContentItems(contentVOResp);
  81. return hagContentResp;
  82. }
  83. @Override
  84. public HagSkillResp getResUrl(HagSkillReq skillReq) {
  85. // log.info("HAG find resource url param : {}", JSON.toJSONString(skillReq));
  86. HagSkillReq.Endpoint endpoint = skillReq.getEndpoint();
  87. if (Objects.isNull(endpoint)) {
  88. return fillFailResp("400","参数错误");
  89. }
  90. HagSkillReq.Endpoint.Device device = endpoint.getDevice();
  91. if (Objects.isNull(device)) {
  92. return fillFailResp("400","device信息为空");
  93. }
  94. String deviceId = device.getDeviceId();
  95. if (StringUtils.isBlank(deviceId)) {
  96. return fillFailResp("400","deviceId为空");
  97. }
  98. HagSkillReq.HagInquire inquire = skillReq.getInquire();
  99. if (Objects.isNull(inquire.getIntent())) {
  100. return fillFailResp("400","intent为空");
  101. }
  102. HagSkillReq.HagInquire.Intent.Slots slots = inquire.getIntent().getSlots();
  103. if (Objects.isNull(slots)) {
  104. return fillFailResp("400","slots为空");
  105. }
  106. /* HagSkillReq.HagInquire.Intent.Slots.ContentCode albumCode = slots.getAlbumCode();
  107. if (Objects.isNull(albumCode)) {
  108. return fillFailResp("400","albumCode为空");
  109. }*/
  110. HagSkillReq.HagInquire.Intent.Slots.ContentCode contentCode = slots.getContentCode();
  111. if (Objects.isNull(contentCode)) {
  112. return fillFailResp("400","contentCode为空");
  113. }
  114. List<HagSkillReq.HagInquire.Intent.Slots.ContentCode.Value> values = contentCode.getValues();
  115. if (CollectionUtils.isEmpty(values)) {
  116. return fillFailResp("400","contentCode为空");
  117. }
  118. String real = values.get(0).getReal();
  119. HagContent hagContent = hagContentRepository.find(real);
  120. if (Objects.isNull(hagContent)) {
  121. return fillFailResp("404","媒资数据未找到");
  122. }
  123. return fillSuccessResp(hagContent.getUrl());
  124. }
  125. private HagSkillResp fillFailResp(String errorCode,String errorMessage) {
  126. HagSkillResp hagSkillResp = new HagSkillResp();
  127. hagSkillResp.setErrorCode(errorCode);
  128. hagSkillResp.setErrorMessage(errorMessage);
  129. hagSkillResp.setVersion("1.0.0");
  130. HagSkillResp.Reply<HagSkillReq.HagInquire.Intent> reply = new HagSkillResp.Reply();
  131. reply.setIsEndSession(Boolean.TRUE);
  132. HagSkillResp.Reply.Command command = new HagSkillResp.Reply.Command();
  133. command.setNamespace("AudioPlayer");
  134. command.setName("Play");
  135. AudioUrl audioUrl = new AudioUrl();
  136. audioUrl.setUrl("");
  137. command.setBody(audioUrl);
  138. reply.setCommands(Arrays.asList(command));
  139. hagSkillResp.setReply(reply);
  140. return hagSkillResp;
  141. }
  142. private HagSkillResp fillSuccessResp(String url) {
  143. HagSkillResp hagSkillResp = new HagSkillResp();
  144. hagSkillResp.setErrorCode("0");
  145. hagSkillResp.setErrorMessage("OK");
  146. hagSkillResp.setVersion("1.0.0");
  147. HagSkillResp.Reply<Map<String,List>> reply = new HagSkillResp.Reply();
  148. reply.setIsEndSession(Boolean.TRUE);
  149. HagSkillResp.Reply.Command command = new HagSkillResp.Reply.Command();
  150. command.setNamespace("AudioPlayer");
  151. command.setName("Play");
  152. AudioUrl audioUrl = new AudioUrl();
  153. audioUrl.setUrl(hagAudioPrefix + md5PlayUrl(cdnSecret, url));
  154. Map<String,List<AudioUrl>> map = new HashMap<>();
  155. map.put("audioItemList",Arrays.asList(audioUrl));
  156. Map<String,Map<String,List<AudioUrl>>> bodyMap = new HashMap<>();
  157. bodyMap.put("templateContent",map);
  158. command.setBody(bodyMap);
  159. reply.setCommands(Arrays.asList(command));
  160. hagSkillResp.setReply(reply);
  161. return hagSkillResp;
  162. }
  163. /*private String md5PlayUrl(String key, String url){
  164. SimpleDateFormat sdf = new SimpleDateFormat("YYYYMMddHHmm");
  165. long endTime = System.currentTimeMillis() + (3 * 60 * 1000L);
  166. Date date = new Date(endTime);
  167. String time = sdf.format(date);
  168. String md5Url = MD5.MD5Encode(key + time + url);
  169. return "/" + time + "/" + md5Url + url;
  170. }*/
  171. private String md5PlayUrl(String key, String url){
  172. long time = System.currentTimeMillis() / 1000;
  173. String md5Url = MD5.MD5Encode(key + url + time);
  174. return "/"+ md5Url + "/" + time + url;
  175. }
  176. }