lrf 8 miesięcy temu
rodzic
commit
71b83a2eb4

+ 0 - 2
src/main/java/com/free/config/GlobalExceptionController.java

@@ -2,7 +2,6 @@ package com.free.config;
 
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
-import org.springframework.http.HttpStatus;
 import org.springframework.util.StringUtils;
 import org.springframework.validation.BindingResult;
 import org.springframework.validation.FieldError;
@@ -10,7 +9,6 @@ import org.springframework.web.bind.MethodArgumentNotValidException;
 import org.springframework.web.bind.annotation.ControllerAdvice;
 import org.springframework.web.bind.annotation.ExceptionHandler;
 import org.springframework.web.bind.annotation.ResponseBody;
-import org.springframework.web.bind.annotation.ResponseStatus;
 
 import javax.servlet.http.HttpServletRequest;
 import java.io.ByteArrayOutputStream;

+ 122 - 114
src/main/java/com/free/controller/HistoryController.java

@@ -1,137 +1,145 @@
 package com.free.controller;
 
-        import java.util.HashMap;
-        import java.util.List;
-        import java.util.Map;
+import java.util.HashMap;
+import java.util.List;
+import java.util.Map;
 
-        import javax.validation.Valid;
+import javax.validation.Valid;
 
-        import com.free.entity.system.History;
-        import com.free.service.HistoryService;
-        import org.springframework.beans.factory.annotation.Autowired;
-        import org.springframework.web.bind.annotation.DeleteMapping;
-        import org.springframework.web.bind.annotation.GetMapping;
-        import org.springframework.web.bind.annotation.PathVariable;
-        import org.springframework.web.bind.annotation.PostMapping;
-        import org.springframework.web.bind.annotation.RequestBody;
-        import org.springframework.web.bind.annotation.RequestMapping;
-        import org.springframework.web.bind.annotation.RequestParam;
-        import org.springframework.web.bind.annotation.RestController;
+import com.free.service.HistoryService;
+import org.springframework.beans.factory.annotation.Autowired;
+import org.springframework.web.bind.annotation.DeleteMapping;
+import org.springframework.web.bind.annotation.GetMapping;
+import org.springframework.web.bind.annotation.PathVariable;
+import org.springframework.web.bind.annotation.PostMapping;
+import org.springframework.web.bind.annotation.RequestBody;
+import org.springframework.web.bind.annotation.RequestMapping;
+import org.springframework.web.bind.annotation.RequestParam;
+import org.springframework.web.bind.annotation.RestController;
 
-        import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
-        import com.baomidou.mybatisplus.core.metadata.IPage;
-        import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
-        import com.fasterxml.jackson.core.JsonProcessingException;
-        import com.fasterxml.jackson.databind.ObjectMapper;
-        import com.fasterxml.jackson.databind.type.CollectionType;
-        import com.free.annotation.PassToken;
-        import com.free.config.CustomizationException;
-        import com.free.config.ExceptionEnum;
-        import com.free.config.ResponseFormat;
-        import com.free.utils.Utils;
+import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
+import com.baomidou.mybatisplus.core.metadata.IPage;
+import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
+import com.free.annotation.PassToken;
+import com.free.config.CustomizationException;
+import com.free.config.ExceptionEnum;
+import com.free.config.ResponseFormat;
+import com.free.entity.History;
 
-        import io.swagger.annotations.Api;
-        import io.swagger.annotations.ApiOperation;
+import io.swagger.annotations.Api;
+import io.swagger.annotations.ApiOperation;
 
 @RestController
 @RequestMapping("/history")
 @Api(tags = "历史记录")
 public class HistoryController {
-    @Autowired
-    private HistoryService service;
+  @Autowired
+  private HistoryService service;
 
-    @PassToken
-    /** 创建数据 */
-    @ApiOperation("创建数据")
-    @PostMapping("")
-    public Object save(@RequestBody @Valid History data) {
-        this.service.save(data);
-        QueryWrapper qw = new QueryWrapper<>();
-        qw.eq("id", data.getId());
-        Map returnData = this.service.getMap(qw);
-        return ResponseFormat.success(returnData);
-        // return ResponseFormat.success();
+  @PassToken
+  /** 创建数据 */
+  @ApiOperation("创建数据")
+  @PostMapping("")
+  public Object save(@RequestBody @Valid History data) {
+    QueryWrapper checkHasQw = new QueryWrapper<>();
+    checkHasQw.eq("user_id", data.getUser_id());
+    checkHasQw.eq("question_id", data.getQuestion_id());
+    History dbData = this.service.getOne(checkHasQw);
+    if (null == dbData) {
+      // 没有数据,创建新的数据
+      this.service.save(data);
+      QueryWrapper qw = new QueryWrapper<>();
+      qw.eq("id", data.getId());
+      Map returnData = this.service.getMap(qw);
+      return ResponseFormat.success(returnData);
+    } else {
+      // 有数据, 浏览次数加1
+      dbData.setViews(dbData.getViews() + 1);
+      this.service.updateById(dbData);
+      Object newData = this.service.getById(dbData.getId());
+      return ResponseFormat.success(newData);
     }
+  }
 
-    /** 修改数据 */
-    @PassToken
-    @ApiOperation("修改数据")
-    @PostMapping("/{id}")
-    public Object update(@PathVariable long id, @RequestBody History data) {
-        QueryWrapper qw = new QueryWrapper<>();
-        qw.eq("id", id);
-        Long num = this.service.count(qw);
-        if (num <= 0) {
-            throw new CustomizationException(ExceptionEnum.NOT_FOUND);
-        }
-        data.setId(id);
-        this.service.updateById(data);
-        Object newData = this.service.getById(id);
-        return ResponseFormat.success(newData);
+  /** 修改数据 */
+  @PassToken
+  @ApiOperation("修改数据")
+  @PostMapping("/{id}")
+  public Object update(@PathVariable long id, @RequestBody History data) {
+    QueryWrapper qw = new QueryWrapper<>();
+    qw.eq("id", id);
+    Long num = this.service.count(qw);
+    if (num <= 0) {
+      throw new CustomizationException(ExceptionEnum.NOT_FOUND);
     }
+    data.setId(id);
+    this.service.updateById(data);
+    Object newData = this.service.getById(id);
+    return ResponseFormat.success(newData);
+  }
 
-    /** 列表查询 */
-    @PassToken
-    @ApiOperation("查询数据列表")
-    @SuppressWarnings({ "unchecked" })
-    @GetMapping()
-    public Object list(@RequestParam Map<String, Object> allParams) {
-        Long skip = null, limit = null;
-        Map map = new HashMap();
-        QueryWrapper qw = new QueryWrapper<>();
-        qw.select("id","title","platform","is_use");
-        /** 参数处理处理 */
-        for (String key : allParams.keySet()) {
-            Object value = allParams.get(key);
-            if (key.equals("skip")) {
-                skip = Long.valueOf(String.valueOf(value));
-            } else if (key.equals("limit")) {
-                limit = Long.valueOf(String.valueOf(value));
-            } else {
-                if(key.equals("title")||key.equals("platform")){
-                    qw.like(key,value);
-                } else {
-                    // 其他为查询条件
-                    qw.eq(key, value);
-                }
-            }
-        }
-        /** 分页处理 */
-        if (null != skip && null != limit) {
-            IPage page = new Page<>(skip, limit);
-            IPage pageResult = service.page(page, qw);
-            List data = pageResult.getRecords();
-            long total = pageResult.getTotal();
-            map.put("data", data);
-            map.put("total", total);
+  /** 列表查询 */
+  @PassToken
+  @ApiOperation("查询数据列表")
+  @SuppressWarnings({ "unchecked" })
+  @GetMapping()
+  public Object list(@RequestParam Map<String, Object> allParams) {
+    Long skip = null, limit = null;
+    Map map = new HashMap();
+    QueryWrapper qw = new QueryWrapper<>();
+    qw.select("id", "title", "platform", "is_use");
+    /** 参数处理处理 */
+    for (String key : allParams.keySet()) {
+      Object value = allParams.get(key);
+      if (key.equals("skip")) {
+        skip = Long.valueOf(String.valueOf(value));
+      } else if (key.equals("limit")) {
+        limit = Long.valueOf(String.valueOf(value));
+      } else {
+        if (key.equals("title") || key.equals("platform")) {
+          qw.like(key, value);
         } else {
-            List list = service.list(qw);
-            map.put("data", list);
+          // 其他为查询条件
+          qw.eq(key, value);
         }
-        return ResponseFormat.success(map);
+      }
     }
-
-    /** 根据id查询 */
-    @PassToken
-    @ApiOperation("查询数据")
-    @GetMapping("/{id}")
-    public Object fetch(@PathVariable long id) {
-        Object newData = service.getById(id);
-        return ResponseFormat.success(newData);
+    /** 分页处理 */
+    if (null != skip && null != limit) {
+      IPage page = new Page<>(skip, limit);
+      IPage pageResult = service.page(page, qw);
+      List data = pageResult.getRecords();
+      long total = pageResult.getTotal();
+      map.put("data", data);
+      map.put("total", total);
+    } else {
+      List list = service.list(qw);
+      map.put("data", list);
     }
+    return ResponseFormat.success(map);
+  }
 
-    /** 根据id删除数据 */
-    @PassToken
-    @ApiOperation("删除数据")
-    @DeleteMapping("/{id}")
-    public Object delete(@PathVariable long id) {
-        QueryWrapper qw = new QueryWrapper<>();
-        qw.eq("id", id);
-        Long num = service.count(qw);
-        if (num <= 0) {
-            throw new CustomizationException(ExceptionEnum.NOT_FOUND);
-        }
-        service.removeById(id);
-        return ResponseFormat.success();
+  /** 根据id查询 */
+  @PassToken
+  @ApiOperation("查询数据")
+  @GetMapping("/{id}")
+  public Object fetch(@PathVariable long id) {
+    Object newData = service.getById(id);
+    return ResponseFormat.success(newData);
+  }
+
+  /** 根据id删除数据 */
+  @PassToken
+  @ApiOperation("删除数据")
+  @DeleteMapping("/{id}")
+  public Object delete(@PathVariable long id) {
+    QueryWrapper qw = new QueryWrapper<>();
+    qw.eq("id", id);
+    Long num = service.count(qw);
+    if (num <= 0) {
+      throw new CustomizationException(ExceptionEnum.NOT_FOUND);
     }
+    service.removeById(id);
+    return ResponseFormat.success();
+  }
 }

+ 5 - 1
src/main/java/com/free/controller/TransferApplyController.java

@@ -23,6 +23,7 @@ import com.free.config.CustomizationException;
 import com.free.config.ExceptionEnum;
 import com.free.config.ResponseFormat;
 import com.free.entity.TransferApply;
+import com.free.mq.MqService;
 import com.free.service.TransferApplyService;
 
 import io.swagger.annotations.Api;
@@ -34,6 +35,8 @@ import io.swagger.annotations.ApiOperation;
 public class TransferApplyController {
   @Autowired
   private TransferApplyService service;
+  @Autowired
+  private MqService mqService;
 
   /** 创建数据 */
   @ApiOperation("创建数据")
@@ -43,7 +46,8 @@ public class TransferApplyController {
     QueryWrapper qw = new QueryWrapper<>();
     qw.eq("id", data.getId());
     Map returnData = this.service.getMap(qw);
-    // TODO: 创建完数据后需要通过 mq 给管理员提示
+    // 创建完数据后需要通过 mq 给管理员提示
+    mqService.sendMsgToAdminForApplyCreate(returnData);
     return ResponseFormat.success(returnData);
     // return ResponseFormat.success();
   }

+ 40 - 40
src/main/java/com/free/entity/system/History.java

@@ -1,41 +1,41 @@
-package com.free.entity.system;
-
-import com.baomidou.mybatisplus.annotation.FieldFill;
-import com.baomidou.mybatisplus.annotation.TableField;
-import com.baomidou.mybatisplus.annotation.TableName;
-import com.fasterxml.jackson.annotation.JsonFormat;
-import com.free.utils.BaseEntity;
-import com.gitee.sunchenbin.mybatis.actable.annotation.Column;
-import com.gitee.sunchenbin.mybatis.actable.annotation.Table;
-import io.swagger.annotations.ApiModel;
-import io.swagger.annotations.ApiModelProperty;
-import lombok.Data;
-import lombok.EqualsAndHashCode;
-import org.springframework.format.annotation.DateTimeFormat;
-
-import java.time.LocalDateTime;
-
-@Data
-@EqualsAndHashCode(callSuper = false)
-@Table(name = "history")
-@TableName(value = "history")
-@ApiModel("历史记录")
-public class History extends BaseEntity {
-    @Column(comment = "用户id")
-    @TableField(value = "user_id")
-    @ApiModelProperty("用户id")
-    private String user_id;
-    @Column(comment = "题库id")
-    @TableField(value = "question_id")
-    @ApiModelProperty("题库id")
-    private String question_id;
-    @Column(comment = "浏览次数")
-    @TableField(value = "views")
-    @ApiModelProperty("浏览次数")
-    private String views;
-    @TableField(fill = FieldFill.INSERT_UPDATE)
-    @JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss", timezone = "Asia/Shanghai")
-    @DateTimeFormat(pattern = "yyyy-MM-dd HH:mm:ss")
-    @ApiModelProperty("数据创建时间")
-    private LocalDateTime time;
+package com.free.entity;
+
+import com.baomidou.mybatisplus.annotation.FieldFill;
+import com.baomidou.mybatisplus.annotation.TableField;
+import com.baomidou.mybatisplus.annotation.TableName;
+import com.fasterxml.jackson.annotation.JsonFormat;
+import com.free.utils.BaseEntity;
+import com.gitee.sunchenbin.mybatis.actable.annotation.Column;
+import com.gitee.sunchenbin.mybatis.actable.annotation.Table;
+import io.swagger.annotations.ApiModel;
+import io.swagger.annotations.ApiModelProperty;
+import lombok.Data;
+import lombok.EqualsAndHashCode;
+import org.springframework.format.annotation.DateTimeFormat;
+
+import java.time.LocalDateTime;
+
+@Data
+@EqualsAndHashCode(callSuper = false)
+@Table(name = "history")
+@TableName(value = "history")
+@ApiModel("历史记录")
+public class History extends BaseEntity {
+    @Column(comment = "用户id")
+    @TableField(value = "user_id")
+    @ApiModelProperty("用户id")
+    private String user_id;
+    @Column(comment = "题库id")
+    @TableField(value = "question_id")
+    @ApiModelProperty("题库id")
+    private String question_id;
+    @Column(comment = "浏览次数")
+    @TableField(value = "views")
+    @ApiModelProperty("浏览次数")
+    private int views;
+    @TableField(fill = FieldFill.INSERT_UPDATE)
+    @JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss", timezone = "Asia/Shanghai")
+    @DateTimeFormat(pattern = "yyyy-MM-dd HH:mm:ss")
+    @ApiModelProperty("最后查询时间")
+    private LocalDateTime time;
 }

+ 2 - 1
src/main/java/com/free/mapper/HistoryMapper.java

@@ -1,7 +1,8 @@
 package com.free.mapper;
 
 import com.baomidou.mybatisplus.core.mapper.BaseMapper;
-import com.free.entity.system.History;
+import com.free.entity.History;
+
 import org.apache.ibatis.annotations.Mapper;
 
 @Mapper

+ 32 - 0
src/main/java/com/free/mq/MqService.java

@@ -1,21 +1,53 @@
 package com.free.mq;
 
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+import java.util.Map;
+import java.util.HashMap;
 import org.springframework.amqp.rabbit.core.RabbitTemplate;
 import org.springframework.beans.factory.annotation.Autowired;
 import org.springframework.stereotype.Service;
 
 import com.fasterxml.jackson.core.JsonProcessingException;
 import com.fasterxml.jackson.databind.ObjectMapper;
+import com.free.service.system.CustomerService;
 
 @Service
 public class MqService {
+  private static final Logger log = LoggerFactory.getLogger(MqService.class);
+
   @Autowired
   RabbitTemplate rabbitTemplate;
 
+  /**
+   * 发送mq消息
+   * 
+   * @param ex    交换机名
+   * @param queue 队列名
+   * @param msg   消息内容
+   * @throws JsonProcessingException JSON处理异常
+   */
   public void sendMsg(String ex, String queue, Object msg) throws JsonProcessingException {
     String strMsg = null;
     ObjectMapper mapper = new ObjectMapper();
     strMsg = mapper.writeValueAsString(msg);
     rabbitTemplate.convertAndSend(ex, queue, strMsg);
   }
+
+  /**
+   * 向管理员订阅队列发送消息,通知管理员去处理转人工申请
+   * 
+   * @param data 转人工申请数据
+   */
+  public void sendMsgToAdminForApplyCreate(Map data) {
+    String ex = MqListeners.adminExName;
+    String queue = MqListeners.adminQueue;
+    try {
+      this.sendMsg(ex, queue, data);
+    } catch (JsonProcessingException e) {
+      e.printStackTrace();
+      log.error("转人工申请通知mq发送失败", e);
+    }
+  }
 }

+ 4 - 4
src/main/java/com/free/service/HistoryService.java

@@ -1,9 +1,9 @@
 package com.free.service;
 
-        import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
-        import com.free.entity.system.History;
-        import com.free.mapper.HistoryMapper;
-        import org.springframework.stereotype.Service;
+import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
+import com.free.entity.History;
+import com.free.mapper.HistoryMapper;
+import org.springframework.stereotype.Service;
 
 @Service
 public class HistoryService extends ServiceImpl<HistoryMapper, History> {