Ver código fonte

搜索音频时增加点播记录

yaobo 7 anos atrás
pai
commit
3f6dc9b90e

+ 1 - 1
src/main/java/cn/efunbox/audio/config/AuthConfig.java

@@ -32,7 +32,7 @@ public class AuthConfig extends WebMvcConfigurerAdapter{
     public void addInterceptors(InterceptorRegistry registry) {
         registry.addInterceptor(authInterceptor())
                 .addPathPatterns("/**")
-                .excludePathPatterns("/device/**");
+                .excludePathPatterns("/device/**", "/channel/insert");
     }
 
 }

+ 22 - 2
src/main/java/cn/efunbox/audio/controller/AudioController.java

@@ -2,8 +2,10 @@ package cn.efunbox.audio.controller;
 
 import cn.efunbox.audio.entity.Audio;
 import cn.efunbox.audio.entity.Device;
+import cn.efunbox.audio.entity.Record;
 import cn.efunbox.audio.service.AudioService;
 import cn.efunbox.audio.service.DeviceService;
+import cn.efunbox.audio.service.RecordService;
 import cn.efunbox.audio.util.ApiCode;
 import cn.efunbox.audio.util.HttpUtil;
 import com.netflix.ribbon.proxy.annotation.Http;
@@ -28,9 +30,14 @@ public class AudioController {
 
     @Autowired
     AudioService audioService;
+    @Autowired
+    DeviceService deviceService;
+    @Autowired
+    RecordService recordService;
 
     @RequestMapping(value = "/search" ,method = RequestMethod.POST)
     public void Search(HttpServletRequest request, HttpServletResponse response){
+        String id = request.getParameter("id");
         String name = request.getParameter("name");
         String album = request.getParameter("album");
         if(name==null && album==null){
@@ -49,10 +56,23 @@ public class AudioController {
             HttpUtil.responseApiCode(request, response, ApiCode.NOT_FOUND);
             return;
         }
-
+        Device device = deviceService.GetOne(Long.valueOf(id));
+        if(device==null){
+            HttpUtil.responseApiCode(request, response, ApiCode.PARAMETER_ERROR);
+            return;
+        }
         Random random = new Random();
         int r = Math.abs(random.nextInt())%list.size();
-        HttpUtil.responseOkData(request, response, list.get(r));
+        Audio audio = list.get(r);
+
+        Record record = new Record();
+        record.setIdDevice(device.getId());
+        record.setIdChannel(device.getIdChannel());
+        record.setIdAudio(audio.getId());
+        record.setCreated(new Date());
+        recordService.Insert(record);
+
+        HttpUtil.responseOkData(request, response, audio);
         return;
     }
 

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

@@ -61,7 +61,7 @@ public class DeviceController {
             map.put("msg", ApiCode.PARAMETER_ERROR.getMessage());
             return map;
         }
-        if(deviceService.CheckRegister(idChannel, idDevice)){
+        if(deviceService.CheckRegister(Long.valueOf(idChannel), Long.valueOf(idDevice))){
             map.put("code", ApiCode.RECORD_EXIST.getCode());
             map.put("msg", ApiCode.RECORD_EXIST.getMessage());
             return map;
@@ -72,7 +72,7 @@ public class DeviceController {
             map.put("msg", ApiCode.PARAMETER_ERROR.getMessage());
             return map;
         }
-        Device device = deviceService.Register(idChannel, idDevice);
+        Device device = deviceService.Register(Long.valueOf(idChannel), Long.valueOf(idDevice));
         if(device!=null){
             map.put("code", ApiCode.OK.getCode());
             Map data = new HashMap<>();

+ 62 - 0
src/main/java/cn/efunbox/audio/controller/RecordController.java

@@ -0,0 +1,62 @@
+package cn.efunbox.audio.controller;
+
+import cn.efunbox.audio.entity.Audio;
+import cn.efunbox.audio.entity.Record;
+import cn.efunbox.audio.service.AudioService;
+import cn.efunbox.audio.service.RecordService;
+import cn.efunbox.audio.util.ApiCode;
+import cn.efunbox.audio.util.HttpUtil;
+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.RestController;
+
+import javax.servlet.http.HttpServletRequest;
+import javax.servlet.http.HttpServletResponse;
+import java.util.Date;
+import java.util.List;
+import java.util.Random;
+
+/**
+ * 音频点播记录
+ * Created by yao on 17-9-26.
+ */
+@RestController
+@Slf4j
+@RequestMapping(value = "/record")
+public class RecordController {
+
+    @Autowired
+    RecordService recordService;
+
+    @RequestMapping(value = "/search" ,method = RequestMethod.POST)
+    public void Search(HttpServletRequest request, HttpServletResponse response){
+        String id = request.getParameter("rid");
+        String idChannel = request.getParameter("idChannel");
+        String idDevice = request.getParameter("idDevice");
+        String idAudio = request.getParameter("idAudio");
+        if(id==null && idChannel==null && idDevice==null && idAudio==null){
+            HttpUtil.responseApiCode(request, response, ApiCode.PARAMETER_ERROR);
+            return;
+        }
+        List<Record> list = null;
+        if(id!=null && id.length()>0)
+            list = recordService.SearchById(Long.valueOf(id));
+        else if(idChannel!=null && idChannel.length()>0)
+            list = recordService.SearchByIdChannel(Long.valueOf(idChannel));
+        else if(idDevice!=null && idDevice.length()>0)
+            list = recordService.SearchByIdDevice(Long.valueOf(idDevice));
+        else if(idAudio!=null && idAudio.length()>0)
+            list = recordService.SearchByIdAudio(Long.valueOf(idAudio));
+
+        if(list==null || list.size()<1){
+            HttpUtil.responseApiCode(request, response, ApiCode.NOT_FOUND);
+            return;
+        }
+
+        HttpUtil.responseOkData(request, response, list);
+        return;
+    }
+
+}

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

@@ -38,11 +38,11 @@ public class Device implements Serializable, Cloneable{
 
     //合作渠道编号
     @Column
-    private String idChannel;
+    private Long idChannel;
 
     //设备编号
     @Column
-    private String idDevice;
+    private Long idDevice;
 
     //状态
     @Column

+ 55 - 0
src/main/java/cn/efunbox/audio/entity/Record.java

@@ -0,0 +1,55 @@
+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 Record implements Serializable, Cloneable {
+
+    @Id
+    @GeneratedValue
+    private Long id;
+
+    //渠道编号
+    @Column
+    private Long idChannel;
+
+    //用户、设备编号
+    @Column
+    private Long idDevice;
+
+    //音频编号
+    @Column
+    private Long idAudio;
+
+    @Column
+    private int status= Status.ONLINE.getCode();
+
+    @Column
+    @Temporal(TemporalType.TIMESTAMP)
+    private Date created;
+
+    public Object clone() throws CloneNotSupportedException {
+        return super.clone();
+    }
+
+
+}

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

@@ -20,7 +20,7 @@ public class DeviceServiceImpl implements DeviceService {
     DeviceRepo deviceRepo;
 
     @Override
-    public boolean CheckRegister(String idChannel, String idDevice){
+    public boolean CheckRegister(Long idChannel, Long idDevice){
         List<Device> list = deviceRepo.findByChannelDevice(idChannel, idDevice);
         if(list.size()>0)
             return true;
@@ -29,7 +29,7 @@ public class DeviceServiceImpl implements DeviceService {
     }
 
     @Override
-    public Device Register(String idChannel, String idDevice){
+    public Device Register(Long idChannel, Long idDevice){
         if(this.CheckRegister(idChannel, idDevice))
             return null;
 

+ 54 - 0
src/main/java/cn/efunbox/audio/impl/RecordServiceImpl.java

@@ -0,0 +1,54 @@
+package cn.efunbox.audio.impl;
+
+import cn.efunbox.audio.entity.Audio;
+import cn.efunbox.audio.entity.Record;
+import cn.efunbox.audio.repository.AudioRepo;
+import cn.efunbox.audio.repository.RecordRepo;
+import cn.efunbox.audio.service.AudioService;
+import cn.efunbox.audio.service.RecordService;
+import org.springframework.beans.factory.annotation.Autowired;
+import org.springframework.stereotype.Service;
+
+import java.util.List;
+
+/**
+ * Created by yao on 17-9-26.
+ */
+@Service
+public class RecordServiceImpl implements RecordService {
+
+    @Autowired
+    RecordRepo recordRepo;
+
+    @Override
+    public List<Record> SearchById(Long id){
+        List<Record> list = recordRepo.findById(id);
+        return list;
+    }
+
+    @Override
+    public List<Record> SearchByIdChannel(Long id){
+        List<Record> list = recordRepo.findByIdChannel(id);
+        return list;
+    }
+
+    @Override
+    public List<Record> SearchByIdDevice(Long id){
+        List<Record> list = recordRepo.findByIdDevice(id);
+        return list;
+    }
+
+    @Override
+    public List<Record> SearchByIdAudio(Long id){
+        List<Record> list = recordRepo.findByIdAudio(id);
+        return list;
+    }
+
+    @Override
+    public Record Insert(Record r){
+        r = recordRepo.save(r);
+        return r;
+    }
+
+
+}

+ 1 - 1
src/main/java/cn/efunbox/audio/repository/DeviceRepo.java

@@ -23,6 +23,6 @@ public interface DeviceRepo extends JpaRepository<Device, Long> {
     List<Device> findByChannel(@Param("idChannel")String idChannel);
 
     @Query("from Device d where d.idChannel=:idChannel and d.idDevice=:idDevice")
-    List<Device> findByChannelDevice(@Param("idChannel")String idChannel, @Param("idDevice")String idDevice);
+    List<Device> findByChannelDevice(@Param("idChannel")Long idChannel, @Param("idDevice")Long idDevice);
 
 }

+ 26 - 0
src/main/java/cn/efunbox/audio/repository/RecordRepo.java

@@ -0,0 +1,26 @@
+package cn.efunbox.audio.repository;
+
+import cn.efunbox.audio.entity.Audio;
+import cn.efunbox.audio.entity.Record;
+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 RecordRepo extends JpaRepository<Record, Long> {
+
+    List<Record> findById(Long id);
+
+    List<Record> findByIdChannel(Long id);
+
+    List<Record> findByIdDevice(Long id);
+
+    List<Record> findByIdAudio(Long id);
+
+}

+ 2 - 2
src/main/java/cn/efunbox/audio/service/DeviceService.java

@@ -7,9 +7,9 @@ import cn.efunbox.audio.entity.Device;
  */
 public interface DeviceService {
 
-    public boolean CheckRegister(String idChannel, String idDevice);
+    public boolean CheckRegister(Long idChannel, Long idDevice);
 
-    public Device Register(String idChannel, String idDevice);
+    public Device Register(Long idChannel, Long idDevice);
 
     public Device Login(Long id, String token);
 

+ 22 - 0
src/main/java/cn/efunbox/audio/service/RecordService.java

@@ -0,0 +1,22 @@
+package cn.efunbox.audio.service;
+
+import cn.efunbox.audio.entity.Audio;
+import cn.efunbox.audio.entity.Record;
+
+import java.util.List;
+
+/**
+ * Created by yao on 17-9-26.
+ */
+public interface RecordService {
+
+    public List<Record> SearchById(Long id);
+
+    public List<Record> SearchByIdChannel(Long id);
+
+    public List<Record> SearchByIdDevice(Long id);
+
+    public List<Record> SearchByIdAudio(Long id);
+
+    public Record Insert(Record record);
+}

+ 1 - 1
src/test/java/cn/efunbox/audio/AudioApplicationTests.java

@@ -30,7 +30,7 @@ public class AudioApplicationTests {
 
 	@Test
 	public void register(){
-		Device device = deviceService.Register("1", "1");
+		Device device = deviceService.Register(Long.valueOf(1), Long.valueOf(1));
 		if(device==null){
 			System.out.println("fail to register");
 			return;