licailing 5 лет назад
Родитель
Сommit
1b8ac3f8b0

+ 28 - 0
src/main/java/com/izouma/dingdong/domain/merchant/Voice.java

@@ -0,0 +1,28 @@
+package com.izouma.dingdong.domain.merchant;
+
+import com.izouma.dingdong.domain.BaseEntity;
+import io.swagger.annotations.ApiModel;
+import io.swagger.annotations.ApiModelProperty;
+import lombok.AllArgsConstructor;
+import lombok.Builder;
+import lombok.Data;
+import lombok.NoArgsConstructor;
+
+import javax.persistence.Entity;
+
+@Builder
+@Entity
+@Data
+@AllArgsConstructor
+@NoArgsConstructor
+@ApiModel(value = "语音配置")
+public class Voice extends BaseEntity {
+    @ApiModelProperty(value = "用户ID", name = "userId")
+    private Long userId;
+
+    @ApiModelProperty(value = "内容")
+    private String content;
+
+    @ApiModelProperty(value = "是否开启")
+    private Boolean isOpen;
+}

+ 19 - 0
src/main/java/com/izouma/dingdong/repo/merchant/VoiceRepo.java

@@ -0,0 +1,19 @@
+package com.izouma.dingdong.repo.merchant;
+
+import com.izouma.dingdong.domain.merchant.Voice;
+import org.springframework.data.jpa.repository.JpaRepository;
+import org.springframework.data.jpa.repository.JpaSpecificationExecutor;
+import org.springframework.data.jpa.repository.Modifying;
+import org.springframework.data.jpa.repository.Query;
+
+import javax.transaction.Transactional;
+import java.util.List;
+
+public interface VoiceRepo extends JpaRepository<Voice, Long>, JpaSpecificationExecutor<Voice> {
+    @Query("update Voice t set t.isOpen = false where t.id <> ?1 and t.userId = ?2")
+    @Modifying
+    @Transactional
+    void updateIsOpen(Long id, Long userId);
+
+    List<Voice> findAllByUserId(Long userId);
+}

+ 14 - 0
src/main/java/com/izouma/dingdong/service/merchant/VoiceService.java

@@ -0,0 +1,14 @@
+package com.izouma.dingdong.service.merchant;
+
+import com.izouma.dingdong.domain.merchant.Voice;
+import com.izouma.dingdong.repo.merchant.VoiceRepo;
+import lombok.AllArgsConstructor;
+import org.springframework.stereotype.Service;
+
+@Service
+@AllArgsConstructor
+public class VoiceService {
+
+    private VoiceRepo voiceRepo;
+
+}

+ 74 - 0
src/main/java/com/izouma/dingdong/web/merchant/VoiceController.java

@@ -0,0 +1,74 @@
+package com.izouma.dingdong.web.merchant;
+
+import com.izouma.dingdong.utils.SecurityUtils;
+import com.izouma.dingdong.web.BaseController;
+import com.izouma.dingdong.domain.merchant.Voice;
+import com.izouma.dingdong.service.merchant.VoiceService;
+import com.izouma.dingdong.dto.PageQuery;
+import com.izouma.dingdong.exception.BusinessException;
+import com.izouma.dingdong.repo.merchant.VoiceRepo;
+import com.izouma.dingdong.utils.ObjUtils;
+import com.izouma.dingdong.utils.excel.ExcelUtils;
+
+import lombok.AllArgsConstructor;
+import org.springframework.data.domain.Page;
+import org.springframework.security.access.prepost.PreAuthorize;
+import org.springframework.web.bind.annotation.*;
+
+import javax.servlet.http.HttpServletResponse;
+import java.io.IOException;
+import java.util.List;
+
+@RestController
+@RequestMapping("/voice")
+@AllArgsConstructor
+public class VoiceController extends BaseController {
+    private VoiceService voiceService;
+    private VoiceRepo voiceRepo;
+
+    //@PreAuthorize("hasRole('ADMIN')")
+    @PostMapping("/save")
+    public Voice save(@RequestBody Voice record) {
+        if (record.getId() != null) {
+            Voice orig = voiceRepo.findById(record.getId()).orElseThrow(new BusinessException("无记录"));
+            ObjUtils.merge(orig, record);
+            record = voiceRepo.save(orig);
+        } else {
+            record = voiceRepo.save(record);
+        }
+        if (record.getIsOpen()) {
+            voiceRepo.updateIsOpen(record.getId(), record.getUserId());
+        }
+        return record;
+    }
+
+
+    //@PreAuthorize("hasRole('ADMIN')")
+    @GetMapping("/all")
+    public Page<Voice> all(PageQuery pageQuery) {
+        return voiceRepo.findAll(toSpecification(pageQuery, Voice.class), toPageRequest(pageQuery));
+    }
+
+    @GetMapping("/get/{id}")
+    public Voice get(@PathVariable Long id) {
+        return voiceRepo.findById(id).orElseThrow(new BusinessException("无记录"));
+    }
+
+    @PostMapping("/del/{id}")
+    public void del(@PathVariable Long id) {
+        voiceRepo.deleteById(id);
+    }
+
+    @GetMapping("/excel")
+    @ResponseBody
+    public void excel(HttpServletResponse response, PageQuery pageQuery) throws IOException {
+        List<Voice> data = all(pageQuery).getContent();
+        ExcelUtils.export(response, data);
+    }
+
+    @GetMapping("/my")
+    public List<Voice> my() {
+        return voiceRepo.findAllByUserId(SecurityUtils.getAuthenticatedUser().getId());
+    }
+}
+

+ 1 - 0
src/main/resources/genjson/Voice.json

@@ -0,0 +1 @@
+{"tableName":"Voice","className":"Voice","remark":"语音配置","genTable":true,"genClass":true,"genList":false,"genForm":false,"genRouter":false,"javaPath":"/Users/qiufangchao/Desktop/project/dingdong/src/main/java/com/izouma/dingdong","viewPath":"/Users/qiufangchao/Desktop/project/dingdong/src/main/vue/src/views","routerPath":"/Users/qiufangchao/Desktop/project/dingdong/src/main/vue/src","resourcesPath":"/Users/qiufangchao/Desktop/project/dingdong/src/main/resources","dataBaseType":"Mysql","fields":[{"name":"userId","modelName":"userId","remark":"用户ID","showInList":true,"showInForm":true,"formType":"number"},{"name":"content","modelName":"content","remark":"内容","showInList":true,"showInForm":true,"formType":"singleLineText"},{"name":"isOpen","modelName":"isOpen","remark":"是否开启","showInList":true,"showInForm":true,"formType":"switch"}],"readTable":false,"dataSourceCode":"dataSource","genJson":"","subtables":[],"update":false,"basePackage":"com.izouma.dingdong","tablePackage":"com.izouma.dingdong.domain.merchant.Voice","genPackage":"merchant"}

+ 36 - 0
src/test/java/com/izouma/dingdong/contorller/VoiceControllerTest.java

@@ -0,0 +1,36 @@
+package com.izouma.dingdong.contorller;
+
+import com.izouma.dingdong.domain.merchant.Voice;
+import com.izouma.dingdong.repo.merchant.VoiceRepo;
+import com.izouma.dingdong.web.merchant.VoiceController;
+import org.junit.Test;
+import org.junit.runner.RunWith;
+import org.springframework.beans.factory.annotation.Autowired;
+import org.springframework.boot.test.context.SpringBootTest;
+import org.springframework.test.context.junit4.SpringRunner;
+
+@RunWith(SpringRunner.class)
+@SpringBootTest
+public class VoiceControllerTest {
+    @Autowired
+    private VoiceController voiceController;
+
+    @Autowired
+    private VoiceRepo voiceRepo;
+
+    @Test
+    public void test(){
+        Voice build = Voice.builder()
+                .userId(656L)
+                .content("订单来啦!注意查收")
+                .isOpen(true)
+                .build();
+        build.setId(667L);
+        System.out.println(voiceController.save(build));
+    }
+
+    @Test
+    public void testMy(){
+        System.out.println(voiceRepo.findAllByUserId(656L));
+    }
+}