Ver código fonte

音频资源、接口、服务的实现

yaobo 7 anos atrás
pai
commit
b6a76cfc6b

+ 88 - 0
src/main/java/cn/efunbox/audio/controller/AudioController.java

@@ -0,0 +1,88 @@
+package cn.efunbox.audio.controller;
+
+import cn.efunbox.audio.entity.Audio;
+import cn.efunbox.audio.entity.Device;
+import cn.efunbox.audio.service.AudioService;
+import cn.efunbox.audio.service.DeviceService;
+import cn.efunbox.audio.util.ApiCode;
+import lombok.extern.slf4j.Slf4j;
+import org.springframework.beans.factory.annotation.Autowired;
+import org.springframework.web.bind.annotation.RequestMapping;
+import org.springframework.web.bind.annotation.RequestMethod;
+import org.springframework.web.bind.annotation.RequestParam;
+import org.springframework.web.bind.annotation.RestController;
+
+import javax.servlet.http.HttpServletRequest;
+import java.util.*;
+
+/**
+ * Created by yao on 17-9-26.
+ */
+@RestController
+@Slf4j
+@RequestMapping(value = "/audio")
+public class AudioController {
+
+    @Autowired
+    AudioService audioService;
+
+    @RequestMapping(value = "/search" ,method = RequestMethod.POST)
+    public Map Search(HttpServletRequest request){
+        Map map = new HashMap<>();
+        String name = request.getParameter("name");
+        String album = request.getParameter("album");
+        if(name==null && album==null){
+            map.put("code", ApiCode.PARAMETER_ERROR.getCode());
+            map.put("msg", ApiCode.PARAMETER_ERROR.getMessage());
+            return map;
+        }
+        List<Audio> list = null;
+        if(name!=null && name.length()>0  && album!=null && album.length()>0)
+            list = audioService.SearchByNameAlbum(name, album);
+        else if(name!=null && name.length()>0)
+            list = audioService.SearchByName(name);
+        else
+            list = audioService.SearchByAlbum(album);
+
+        if(list==null || list.size()<1){
+            map.put("code", ApiCode.NOT_FOUND.getCode());
+            map.put("msg", ApiCode.NOT_FOUND.getMessage());
+            return map;
+        }
+
+        Random random = new Random();
+        int r = Math.abs(random.nextInt())%list.size();
+        map.put("code", ApiCode.OK.getCode());
+        map.put("data", list.get(r));
+
+        return map;
+    }
+
+    @RequestMapping(value = "/insert", method = RequestMethod.POST)
+    public Map Insert(HttpServletRequest request){
+        Map map = new HashMap<>();
+        String album = request.getParameter("album");
+        String lesson = request.getParameter("lesson");
+        String name = request.getParameter("name");
+        String url = request.getParameter("url");
+        if(album==null || name==null && url==null){
+            map.put("code", ApiCode.PARAMETER_ERROR.getCode());
+            map.put("msg", ApiCode.PARAMETER_ERROR.getMessage());
+            return map;
+        }
+
+        Audio audio = new Audio();
+        audio.setAlbum(album);
+        if(lesson!=null && lesson.length()>0)
+            audio.setLesson(Integer.valueOf(lesson));
+        audio.setName(name);
+        audio.setUrl(url);
+        audio.setCreated(new Date());
+        audio = audioService.Insert(audio);
+
+        map.put("code", ApiCode.OK.getCode());
+        map.put("data", audio);
+        return map;
+    }
+
+}

+ 1 - 1
src/main/java/cn/efunbox/audio/controller/DeviceController.java

@@ -32,7 +32,7 @@ public class DeviceController {
         Device device = deviceService.Login(id, token);
         if(device==null){
             map.put("code", ApiCode.INVALID_TOKEN.getCode());
-            map.put("msg", ApiCode.INVALID_TOKEN.getCode());
+            map.put("msg", ApiCode.INVALID_TOKEN.getMessage());
             return map;
         }
 

+ 62 - 0
src/main/java/cn/efunbox/audio/entity/Audio.java

@@ -0,0 +1,62 @@
+package cn.efunbox.audio.entity;
+
+
+import cn.efunbox.audio.enums.Status;
+import lombok.Data;
+import lombok.ToString;
+import org.hibernate.annotations.DynamicInsert;
+import org.hibernate.annotations.DynamicUpdate;
+
+import javax.persistence.*;
+import java.io.Serializable;
+import java.util.Date;
+
+/**
+ * Created by yao on 17-9-26.
+ */
+
+
+@Data
+@ToString
+@Entity
+@DynamicInsert
+@DynamicUpdate
+public class Audio implements Serializable, Cloneable {
+
+    @Id
+    @GeneratedValue
+    private Long id;
+
+    //专辑名,如儿歌
+    @Column
+    private String album;
+
+    //专辑下的第几集
+    @Column
+    private int lesson;
+
+    //音频名称,如两只老虎
+    @Column
+    private String name;
+
+    //来自资源平台的id
+    @Column
+    private String idResource;
+
+    //播放地址
+    @Column
+    private String url;
+
+    @Column
+    private int status= Status.ONLINE.getCode();
+
+    @Column
+    @Temporal(TemporalType.TIMESTAMP)
+    private Date created;
+
+    public Object clone() throws CloneNotSupportedException {
+        return super.clone();
+    }
+
+
+}

+ 12 - 4
src/main/java/cn/efunbox/audio/entity/Device.java

@@ -22,35 +22,43 @@ import java.util.Date;
 @Table(name = "device")
 @DynamicInsert
 @DynamicUpdate
-public class Device{
+public class Device implements Serializable, Cloneable{
 
     @Id
     @GeneratedValue
     private Long id;
 
-    @Column(name = "tokenNew")
-    private String tokenNew;
+    //新token
+    @Column(name = "tokenNew", columnDefinition="varchar(100) comment '新token'")
+    private String tokenNew="";
 
+    //旧token,新token被验证后旧token失效
     @Column
-    private String tokenOld;
+    private String tokenOld="";
 
+    //合作渠道编号
     @Column
     private String idChannel;
 
+    //设备编号
     @Column
     private String idDevice;
 
+    //状态
     @Column
     private int status= Status.ONLINE.getCode();
 
+    //设备的会员有效期
     @Column
     @Temporal(TemporalType.TIMESTAMP)
     private Date valid;
 
+    //上次更新、登陆时间
     @Column
     @Temporal(TemporalType.TIMESTAMP)
     private Date updated;
 
+    //创建时间
     @Column
     @Temporal(TemporalType.TIMESTAMP)
     private Date created;

+ 66 - 0
src/main/java/cn/efunbox/audio/impl/AudioServiceImpl.java

@@ -0,0 +1,66 @@
+package cn.efunbox.audio.impl;
+
+import cn.efunbox.audio.entity.Audio;
+import cn.efunbox.audio.entity.Device;
+import cn.efunbox.audio.repository.AudioRepo;
+import cn.efunbox.audio.repository.DeviceRepo;
+import cn.efunbox.audio.service.AudioService;
+import cn.efunbox.audio.service.DeviceService;
+import org.springframework.beans.factory.annotation.Autowired;
+import org.springframework.stereotype.Service;
+
+import java.util.Date;
+import java.util.List;
+import java.util.UUID;
+
+/**
+ * Created by yao on 17-9-26.
+ */
+@Service
+public class AudioServiceImpl implements AudioService {
+
+    @Autowired
+    AudioRepo audioRepo;
+
+    @Override
+    public List<Audio> SearchByName(String name){
+        if(name==null || name.isEmpty())
+            return null;
+        List<Audio> list = audioRepo.findByName(name);
+        System.out.println("list1:"+list.toString());
+        if(list==null)
+            list = audioRepo.findByNameLike("%"+name+"%");
+        System.out.println("list2:"+list.toString());
+        return list;
+    }
+
+    @Override
+    public List<Audio> SearchByAlbum(String album){
+        if(album==null || album.isEmpty())
+            return null;
+        List<Audio> list = audioRepo.findByAlbum(album);
+        if(list==null)
+            list = audioRepo.findByAlbumLike(album);
+        return list;
+    }
+
+    @Override
+    public List<Audio> SearchByNameAlbum(String name, String album){
+        if(name==null || name.isEmpty()
+                || album==null || album.isEmpty())
+            return null;
+        List<Audio> list = audioRepo.findByNameAndAlbum(name, album);
+        if(list==null)
+            list = audioRepo.searchByNameAndAlbum(name, album);
+
+        return list;
+    }
+
+    @Override
+    public Audio Insert(Audio audio){
+        Audio a = audioRepo.save(audio);
+        return a;
+    }
+
+
+}

+ 4 - 2
src/main/java/cn/efunbox/audio/impl/DeviceServiceImpl.java

@@ -50,8 +50,10 @@ public class DeviceServiceImpl implements DeviceService {
         Device device = deviceRepo.findById(id);
         if(device==null)
             return null;
-        if(false == device.getTokenNew().equalsIgnoreCase(token)
-                && false == device.getTokenOld().equalsIgnoreCase(token))
+        System.out.println("token:"+token);
+        System.out.println(device.toString());
+        if(device.getTokenNew()!=null && false == device.getTokenNew().equals(token)
+            && device.getTokenOld()!=null && false == device.getTokenOld().equals(token))
             return null;
         if(device.getTokenNew().equalsIgnoreCase(token)){
             device.setTokenOld(device.getTokenNew());

+ 35 - 0
src/main/java/cn/efunbox/audio/repository/AudioRepo.java

@@ -0,0 +1,35 @@
+package cn.efunbox.audio.repository;
+
+import cn.efunbox.audio.entity.Audio;
+import cn.efunbox.audio.entity.Device;
+import org.springframework.data.jpa.repository.JpaRepository;
+import org.springframework.data.jpa.repository.Query;
+import org.springframework.data.repository.query.Param;
+import org.springframework.stereotype.Repository;
+
+import java.util.List;
+
+/**
+ * Created by yao on 17-9-26.
+ */
+@Repository
+public interface AudioRepo extends JpaRepository<Audio, Long> {
+
+    Audio findById(Long id);
+
+    List<Audio> findByName(String name);
+
+    @Query("from Audio a where a.name LIKE :name")
+    List<Audio> findByNameLike(String name);
+
+    List<Audio> findByNameContaining(String name);
+
+    List<Audio> findByAlbum( String Album);
+
+    List<Audio> findByAlbumLike( String Album);
+
+    List<Audio> findByNameAndAlbum(String name, String Album);
+
+    @Query("from Audio a where a.name like CONCAT('%',:name,'%') and a.album like CONCAT('%',:album,'%') ")
+    List<Audio> searchByNameAndAlbum(@Param("name") String name, @Param("album") String album);
+}

+ 20 - 0
src/main/java/cn/efunbox/audio/service/AudioService.java

@@ -0,0 +1,20 @@
+package cn.efunbox.audio.service;
+
+import cn.efunbox.audio.entity.Audio;
+import cn.efunbox.audio.entity.Device;
+
+import java.util.List;
+
+/**
+ * Created by yao on 17-9-26.
+ */
+public interface AudioService {
+
+    public List<Audio> SearchByName(String name);
+
+    public List<Audio> SearchByAlbum(String album);
+
+    public List<Audio> SearchByNameAlbum(String name, String album);
+
+    public Audio Insert(Audio audio);
+}

+ 4 - 2
src/main/resources/application.properties

@@ -1,6 +1,8 @@
-spring.datasource.url=jdbc:mysql://localhost:3306/efunbox_audio
+spring.datasource.url=jdbc:mysql://localhost:3306/efunbox_audio?useUnicode=true&characterEncoding=utf-8&characterSetResults=UTF-8&autoReconnect=true&allowMultiQueries=true
 spring.datasource.username=root
 spring.datasource.password=223732
 spring.datasource.driver-class-name=com.mysql.jdbc.Driver
 
-spring.jpa.properties.hibernate.hbm2ddl.auto=update
+spring.jpa.properties.hibernate.hbm2ddl.auto=update
+
+spring.jpa.show-sql=true