Forráskód Böngészése

Merge branch 'master' of http://gogs.efunbox.cn:3000/Rankin/rankin

xuchaolang 6 éve
szülő
commit
ce6c7b8208

+ 53 - 89
rankin-resource-service/src/main/java/cn/rankin/resourceservice/controller/ResourceController.java

@@ -5,7 +5,6 @@ import cn.rankin.common.utils.api.model.APIResult;
 import cn.rankin.common.utils.api.page.Page;
 import cn.rankin.common.utils.constant.ResourceType;
 import cn.rankin.common.utils.dto.resource.ResourceSearchDTO;
-import cn.rankin.common.utils.enums.BaseStatusEnum;
 import cn.rankin.common.utils.util.DateUtil;
 import cn.rankin.data.api.resource.dto.AudioImgDTO;
 import cn.rankin.data.api.resource.entity.AudioImg;
@@ -151,19 +150,40 @@ public class ResourceController {
         return APIResult.ok();
     }
 
-
     /* ------------ 有声读物  audioImg    ---------------------start---------------------------------*/
+    @RequestMapping(value = "/audioImg/batch", method = RequestMethod.GET)
+    public Map<String, Resource> getAudioImgBatch(@RequestParam("ids") List<String> ids) {
+        Map<String, Resource> audioImgMap= new HashMap<>();
+        List<AudioImg> audioImgs = audioImgService.findMixByIds(ids);
+        if(CollectionUtils.isEmpty(audioImgs)){
+            return audioImgMap;
+        }
+
+        audioImgs.forEach(ai -> {
+            Resource resource = resourceService.audioImgToResource(ai);
+            audioImgMap.put(ai.getId(),resource);
+
+        });
+
+        return audioImgMap;
+    }
+
     @RequestMapping(value="/audioImg",method = RequestMethod.POST)
     public AudioImg createAudioImg(@RequestBody AudioImgDTO dto) {
         if (audioImgService.exists(dto.getCode())) {
+            log.error("audioImg code Is exist");
             return null;
         }
 
-        Resource audio = toAudioResource(dto);
-        Resource img = toImgResource(dto);
+        Resource a = dto.getAudio();
+        Resource i = dto.getImg();
+
+        //设置code  name
+        setCodeName(a, dto);
+        setCodeName(i, dto);
 
-        img = saveResource(img);
-        audio = saveResource(audio);
+        Resource img = saveOrUpdateResource(i);
+        Resource audio = saveOrUpdateResource(a);
 
         if(null != audio && null != img){
             AudioImg ai = new AudioImg();
@@ -182,22 +202,26 @@ public class ResourceController {
 
             return audioImg;
         }else{
+            log.error("create AudioImg is error,audio={},img={}",audio, img);
             return null;
         }
     }
 
     @RequestMapping(value="/audioImg",method = RequestMethod.PUT)
     public AudioImg updateAudioImg(@RequestBody AudioImgDTO dto) {
+        if(StringUtils.isBlank(dto.getCode())){
+            return null;
+        }
+        Resource a = dto.getAudio();
+        Resource i = dto.getImg();
+        setCodeName(a, dto);
+        setCodeName(i, dto);
 
-        Resource audio = toAudioResource(dto);
-        Resource img = toImgResource(dto);
-
-        img = saveResource(img);
-        audio = saveResource(audio);
+        Resource img = saveOrUpdateResource(i);
+        Resource audio = saveOrUpdateResource(a);
 
         if(null != audio && null != img){
             AudioImg ai = audioImgService.get(dto.getId());
-
             ai.setStatus(dto.getStatus());
             ai.setName(dto.getName());
             ai.setCode(dto.getCode());
@@ -215,96 +239,36 @@ public class ResourceController {
         }
     }
 
-    @RequestMapping(value = "/audioImg/batch", method = RequestMethod.GET)
-    public Map<String, Resource> getAudioImgBatch(@RequestParam("ids") List<String> ids) {
-        Map<String, Resource> audioImgMap= new HashMap<>();
-        List<AudioImg> audioImgs = audioImgService.findMixByIds(ids);
-        if(CollectionUtils.isEmpty(audioImgs)){
-            return audioImgMap;
-        }
-
-        audioImgs.forEach(ai -> {
-            Resource resource = resourceService.audioImgToResource(ai);
-            audioImgMap.put(ai.getId(),resource);
-
-        });
-
-        return audioImgMap;
-    }
-
-    /**
-     * 有声读物 转  资源
-     * @param ai
-     * @return
-     */
-    /*private Resource audioImgToResource(AudioImg ai) {
-        String audioId = ai.getAudioId();
-        Resource audio = resourceService.get(audioId);
-        resourceService.initResourceUrl(audio);
-        String imgId = ai.getImgId();
-        Resource img = resourceService.get(imgId);
-        resourceService.initResourceUrl(img);
-
-        Resource resource = new Resource();
-        //有声读物info
-        resource.setType(ai.getType());
-        resource.setId(ai.getId());
-        resource.setCode(ai.getCode());
-        resource.setName(ai.getName());
-        resource.setStatus(ai.getStatus());
-        resource.setGmtCreated(ai.getGmtCreated());
-        resource.setGmtModified(ai.getGmtModified());
-
-        //图片info
-        resource.setImg(img);
-        //音频info
-        resource.setAudio(audio);
-        return resource;
-    }*/
-
-    private Resource toAudioResource(AudioImgDTO dto) {
-        Resource audio = new Resource();
-        audio.setCode(dto.getCode()+"_"+ResourceType.AUDIO+DateUtil.getTimeStamp());
-        audio.setName(dto.getName()+"_"+ResourceType.AUDIO+DateUtil.getTimeStamp());
-        audio.setFormat(dto.getAudioFormat());
-        audio.setPath(dto.getAudioPath());
-        audio.setSize(dto.getAudioSize());
-        audio.setStatus(BaseStatusEnum.NORMAL);
-        audio.setType(ResourceType.AUDIO);
-        audio.setQuality(dto.getAudioQuality());
-        return audio;
-    }
-
-    private Resource toImgResource(AudioImgDTO dto) {
-        Resource img = new Resource();
-        img.setCode(dto.getCode()+"_"+ResourceType.IMG+DateUtil.getTimeStamp());
-        img.setName(dto.getName()+"_"+ResourceType.IMG+DateUtil.getTimeStamp());
-        img.setFormat(dto.getImgFormat());
-        img.setPath(dto.getImgPath());
-        img.setSize(dto.getImgSize());
-        img.setQuality(dto.getImgQuality());
-        img.setStatus(BaseStatusEnum.NORMAL);
-        img.setType(ResourceType.IMG);
-        return img;
+    private void setCodeName(Resource resource, AudioImgDTO dto) {
+        resource.setCode(dto.getCode()+"_"+ resource.getType() + DateUtil.getTimeStamp());
+        resource.setName(dto.getName()+"_"+ resource.getType() + DateUtil.getTimeStamp());
     }
 
     /**
-     * 存储资源
+     * 更新/存储资源
      * @param resource
      * @return
      */
-    private Resource saveResource(Resource resource) {
+    private Resource saveOrUpdateResource(Resource resource) {
 
         if (StringUtils.isEmpty(resource.getCode()) || StringUtils.isEmpty(resource.getPath())) {
             return null;
         }
 
-        if (resourceService.exists(resource.getCode())) {
-            return null;
+        Resource result = null;
+        if(StringUtils.isBlank(resource.getId())){
+            if (resourceService.exists(resource.getCode())) {
+                log.error("resource code Is exist");
+                return null;
+            }
+            resource.setId(null);
+            //图片,音频类型  存本地
+            result = resourceService.save(resource);
+        }else{
+            //图片,音频类型  存本地
+            result = resourceService.updateResource(resource);
         }
 
-        //图片,音频类型  存本地
-        Resource result = resourceService.save(resource);
         return result;
     }
 

+ 42 - 4
rankin-resource-service/src/main/java/cn/rankin/resourceservice/service/ResourceService.java

@@ -210,14 +210,14 @@ public class ResourceService {
      * @throws DuplicateValueException
      * @throws NotFoundException
      */
-    public void update(Resource resource) throws DuplicateValueException, NotFoundException, UnsupportedOperationException {
+    public Resource update(Resource resource) throws DuplicateValueException, NotFoundException, UnsupportedOperationException {
         if (null == resource){
             log.error("Resource is null");
-            return;
+            return null;
         }
 
-        if (ResourceType.IMG == resource.getType()){
-            updateImg(resource);
+        if (ResourceType.IMG == resource.getType() || ResourceType.AUDIO == resource.getType() ){
+            return updateResource(resource);
         }
         else{
             log.error("Not-Supported-Resource-Type, id={}, type={}", resource.getId(), resource.getType());
@@ -264,6 +264,44 @@ public class ResourceService {
     }
 
     /**
+     * update Img,audio
+     * @param resource
+     * @throws DuplicateValueException
+     * @throws NotFoundException
+     */
+    public Resource updateResource(Resource resource) throws DuplicateValueException, NotFoundException {
+
+        if (ResourceType.IMG != resource.getType() && ResourceType.AUDIO!= resource.getType()){
+            log.error("Not-Img/audio -Resource");
+            return null;
+        }
+
+        //find target resource
+        Resource targetResouce = resourceRepository.find(resource.getId());
+
+        if(null == targetResouce){
+            log.error("Cannot Find Resource, id={}", resource.getId());
+            throw new NotFoundException("Resource-Not-Found");
+        }
+
+        //only support code,title,path
+        //first, make sure code is not duplicated
+        List<Resource> foundedResource = resourceRepository.findByCode(resource.getCode());
+        foundedResource.forEach( (each) -> {
+            if ( !resource.getId().equals(each.getId()) ){
+                log.error("Code-Already-Exist, code={}", resource.getCode());
+                throw new DuplicateValueException("Code-Already-Exist");
+            }
+        });
+
+        //set and save
+        targetResouce.setCode(resource.getCode());
+        targetResouce.setName(resource.getName());
+        targetResouce.setPath(resource.getPath());
+        return resourceRepository.save(targetResouce);
+    }
+
+    /**
      * saveRemoteResource
      * type 0 video , 1 audio
      * @param resource