123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206 |
- 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) {
- // log.info("HAG find resource url param : {}", JSON.toJSONString(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为空");
- }
- /* HagSkillReq.HagInquire.Intent.Slots.ContentCode albumCode = slots.getAlbumCode();
- 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;
- }
- /*private String md5PlayUrl(String key, String url){
- 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;
- }
- }
|