种子,促销

Change-Id: I0ce919ce4228dcefec26ef636bacd3298c0dc77a
diff --git a/src/main/java/com/example/myproject/common/CommonResultStatus.java b/src/main/java/com/example/myproject/common/CommonResultStatus.java
new file mode 100644
index 0000000..76e63a6
--- /dev/null
+++ b/src/main/java/com/example/myproject/common/CommonResultStatus.java
@@ -0,0 +1,37 @@
+package com.example.myproject.common;
+
+
+public enum CommonResultStatus implements ResultStatus {
+
+    OK(0, "成功"),
+
+    FAIL(500, "失败"),
+
+    PARAM_ERROR(400, "参数非法"),
+
+    RECORD_NOT_EXIST(404, "记录不存在"),
+
+    UNAUTHORIZED(401, "未授权"),
+
+    FORBIDDEN(403, "无权限"),
+
+    SERVER_ERROR(500, "服务器内部错误");
+
+    private final int code;
+    private final String message;
+
+    CommonResultStatus(int code, String message) {
+        this.code = code;
+        this.message = message;
+    }
+
+    @Override
+    public int getCode() {
+        return code;
+    }
+
+    @Override
+    public String getMessage() {
+        return message;
+    }
+}
diff --git a/src/main/java/com/example/myproject/common/Constants.java b/src/main/java/com/example/myproject/common/Constants.java
new file mode 100644
index 0000000..4e3d864
--- /dev/null
+++ b/src/main/java/com/example/myproject/common/Constants.java
@@ -0,0 +1,54 @@
+package com.example.myproject.common;
+
+
+public interface Constants {
+
+    String TOKEN_HEADER_NAME = "Authorization";
+    String SESSION_CURRENT_USER = "currentUser";
+
+    /**
+     * 菜单根id
+     */
+    Integer RESOURCE_ROOT_ID = 0;
+
+    interface Order {
+        String DEFAULT_ORDER_TYPE = "desc";
+
+        String[] ORDER_TYPE = new String[]{"desc", "asc", "DESC", "ASC"};
+    }
+
+    interface FinishStatus {
+
+        /**
+         * 已完成并测试
+         */
+        String FINISHED = " (已完成并测试通过)";
+
+        /**
+         * 未完成未测试
+         */
+        String UNFINISHED = " (未完成未测试)";
+
+        /**
+         * 已完成但未测试
+         */
+        String FINISHED_NOT_TEST = " (已完成但未测试)";
+
+    }
+
+    interface Source {
+        String PREFIX = "[RKT] ";
+
+        String NAME = "rocket pt";
+    }
+
+    interface Announce {
+
+        String PROTOCOL = "http";
+
+        String HOSTNAME = "192.168.6.112";
+
+        Integer PORT = 9966;
+
+    }
+}
diff --git a/src/main/java/com/example/myproject/common/ResultStatus.java b/src/main/java/com/example/myproject/common/ResultStatus.java
new file mode 100644
index 0000000..f6c7afe
--- /dev/null
+++ b/src/main/java/com/example/myproject/common/ResultStatus.java
@@ -0,0 +1,14 @@
+package com.example.myproject.common;
+
+
+public interface ResultStatus {
+    /**
+     * 错误码
+     */
+    int getCode();
+
+    /**
+     * 错误信息
+     */
+    String getMessage();
+}
diff --git a/src/main/java/com/example/myproject/common/base/I18nMessage.java b/src/main/java/com/example/myproject/common/base/I18nMessage.java
new file mode 100644
index 0000000..a725e9b
--- /dev/null
+++ b/src/main/java/com/example/myproject/common/base/I18nMessage.java
@@ -0,0 +1,61 @@
+package com.example.myproject.common.base;
+
+import org.springframework.context.MessageSource;
+import org.springframework.context.i18n.LocaleContextHolder;
+import org.springframework.context.support.ReloadableResourceBundleMessageSource;
+
+import java.util.Objects;
+
+import lombok.experimental.UtilityClass;
+import lombok.extern.slf4j.Slf4j;
+
+@Slf4j
+@UtilityClass
+public class I18nMessage {
+
+    static {
+        ReloadableResourceBundleMessageSource messageSource =
+                new ReloadableResourceBundleMessageSource();
+        messageSource.setCacheSeconds(5);
+        messageSource.setBasenames("classpath:i18n/message");
+        I18nMessage.init(messageSource);
+    }
+
+    private static MessageSource messageSource;
+
+    public static void init(MessageSource messageSource) {
+        Objects.requireNonNull(messageSource, "MessageSource can't be null");
+        I18nMessage.messageSource = messageSource;
+    }
+
+    /**
+     * 读取国际化消息
+     *
+     * @param msgCode 消息码
+     * @param args    消息参数 例: new String[]{"1","2","3"}
+     * @return
+     */
+    public static String getMessage(String msgCode, Object[] args) {
+        try {
+            return I18nMessage.messageSource.getMessage(msgCode, args,
+                    LocaleContextHolder.getLocale());
+        } catch (Exception e) {
+            if (log.isDebugEnabled()) {
+                e.printStackTrace();
+            }
+            log.error("===> 读取国际化消息失败, code:{}, args:{}, ex:{}", msgCode, args,
+                    e.getMessage() == null ? e.toString() : e.getMessage());
+        }
+        return "-Unknown-";
+    }
+
+    /**
+     * 获取一条语言配置信息
+     *
+     * @param msgCode 消息码
+     * @return 对应配置的信息
+     */
+    public static String getMessage(String msgCode) {
+        return I18nMessage.getMessage(msgCode, null);
+    }
+}
diff --git a/src/main/java/com/example/myproject/common/base/OrderPageParam.java b/src/main/java/com/example/myproject/common/base/OrderPageParam.java
new file mode 100644
index 0000000..21ff38b
--- /dev/null
+++ b/src/main/java/com/example/myproject/common/base/OrderPageParam.java
@@ -0,0 +1,99 @@
+package com.example.myproject.common.base;
+
+
+import com.example.myproject.common.CommonResultStatus;
+import com.example.myproject.common.Constants;
+import com.example.myproject.common.exception.RocketPTException;
+
+import org.apache.commons.lang3.StringUtils;
+
+import java.lang.reflect.Field;
+import java.util.ArrayList;
+import java.util.Arrays;
+import java.util.List;
+
+import cn.hutool.core.util.ArrayUtil;
+import cn.hutool.core.util.StrUtil;
+import io.swagger.v3.oas.annotations.media.Schema;
+import lombok.Getter;
+import lombok.Setter;
+
+@Getter
+@Setter
+public class OrderPageParam extends PageParam {
+
+    /**
+     * 排序字段
+     */
+    @Schema(description = "排序字段")
+    protected String prop;
+
+    /**
+     * 排序规则
+     */
+    @Schema(description = "排序规则")
+    protected String sort;
+
+    public void validOrder(List<String> orderKey) throws RocketPTException {
+        prop = StringUtils.isBlank(prop) ? null : StrUtil.toUnderlineCase(prop);
+        sort = StringUtils.isBlank(sort) ? Constants.Order.DEFAULT_ORDER_TYPE : sort;
+
+        if (Arrays.asList(Constants.Order.ORDER_TYPE).indexOf(sort) < 0) {
+            throw new RocketPTException(CommonResultStatus.PARAM_ERROR, "排序方式錯誤");
+        }
+
+        if (StringUtils.isNotBlank(prop) && Arrays.asList(orderKey).indexOf(prop) < 0) {
+            throw new RocketPTException(CommonResultStatus.PARAM_ERROR, "排序欄位錯誤");
+        }
+    }
+
+    public void validOrder() throws RocketPTException {
+        List<String> orderKey = getOrderKey();
+        prop = StringUtils.isBlank(prop) ? null : StrUtil.toUnderlineCase(prop);
+        sort = StringUtils.isBlank(sort) ? Constants.Order.DEFAULT_ORDER_TYPE : sort;
+
+        if (!ArrayUtil.contains(Constants.Order.ORDER_TYPE, sort)) {
+            throw new RocketPTException(CommonResultStatus.PARAM_ERROR, "排序方式錯誤");
+        }
+
+        if (StringUtils.isNotBlank(prop) && !orderKey.contains(prop)) {
+            throw new RocketPTException(CommonResultStatus.PARAM_ERROR, "排序欄位錯誤");
+        }
+    }
+
+    /**
+     * @return 反射获取字段列表
+     */
+    public List<String> getOrderKey() {
+        List<String> list = new ArrayList<>();
+
+        Field[] fields = getClass().getDeclaredFields();
+        if (fields != null) {
+            for (Field field : fields) {
+                field.setAccessible(true);
+                String name = field.getName();
+
+                list.add(StrUtil.toUnderlineCase(name));
+            }
+        }
+        return list;
+    }
+    /**
+     * @return 反射获取字段列表
+     */
+    public List<String> getOrderKey(Class clazz) {
+        List<String> list = new ArrayList<>();
+
+        Field[] fields = clazz.getDeclaredFields();
+        if (fields != null) {
+            for (Field field : fields) {
+                field.setAccessible(true);
+                String name = field.getName();
+
+                list.add(StrUtil.toUnderlineCase(name));
+            }
+        }
+        return list;
+    }
+
+}
diff --git a/src/main/java/com/example/myproject/common/base/PageParam.java b/src/main/java/com/example/myproject/common/base/PageParam.java
new file mode 100644
index 0000000..c59108d
--- /dev/null
+++ b/src/main/java/com/example/myproject/common/base/PageParam.java
@@ -0,0 +1,25 @@
+package com.example.myproject.common.base;
+
+import javax.validation.constraints.Max;
+import javax.validation.constraints.Min;
+import javax.validation.constraints.NotNull;
+import lombok.Getter;
+import lombok.Setter;
+import lombok.ToString;
+
+
+@Getter
+@Setter
+@ToString
+public class PageParam {
+    @NotNull(message = "page参数不能为空")
+    @Min(value = 1L, message = "page参数必须是数字或数值小于限制")
+    protected Integer page;
+
+    @NotNull(message = "参数不能为空")
+    @Min(value = 1L, message = "参数必须是数字或数值小于限制")
+    @Max(value = 200L, message = "参数必须是数字或数值大于限制")
+    protected Integer size;
+
+
+}
diff --git a/src/main/java/com/example/myproject/common/base/PageUtil.java b/src/main/java/com/example/myproject/common/base/PageUtil.java
new file mode 100644
index 0000000..a4b51ce
--- /dev/null
+++ b/src/main/java/com/example/myproject/common/base/PageUtil.java
@@ -0,0 +1,54 @@
+package com.example.myproject.common.base;
+
+import com.github.pagehelper.PageHelper;
+import com.github.pagehelper.PageInfo;
+
+import java.util.List;
+
+public class PageUtil {
+
+
+    public static int DEFAULT_PAGE_SIZE = 20;
+
+    /**
+     * 开始分页
+     *
+     * @param param
+     */
+    public static void startPage(OrderPageParam param) {
+        Integer page = param.getPage();
+        if (page == null) {
+            param.setPage(1);
+            param.setSize(DEFAULT_PAGE_SIZE);
+        }
+
+        PageHelper.startPage(param.getPage(), param.getSize());
+
+    }
+
+    /**
+     * 开始分页
+     *
+     * @param param
+     */
+    public static void startPage(PageParam param) {
+        Integer page = param.getPage();
+        if (page == null) {
+            param.setPage(1);
+            param.setSize(DEFAULT_PAGE_SIZE);
+        }
+
+        PageHelper.startPage(param.getPage(), param.getSize());
+    }
+
+    /**
+     * 分页结果
+     *
+     * @param list
+     */
+    public static ResPage getPage(List list) {
+        PageInfo pageInfo = new PageInfo(list);
+        return new ResPage(pageInfo.getTotal(), pageInfo.getPageNum(), pageInfo.getSize());
+
+    }
+}
diff --git a/src/main/java/com/example/myproject/common/base/ResPage.java b/src/main/java/com/example/myproject/common/base/ResPage.java
new file mode 100644
index 0000000..45a22dd
--- /dev/null
+++ b/src/main/java/com/example/myproject/common/base/ResPage.java
@@ -0,0 +1,31 @@
+package com.example.myproject.common.base;
+import lombok.AllArgsConstructor;
+import lombok.Getter;
+import lombok.NoArgsConstructor;
+import lombok.Setter;
+import lombok.ToString;
+
+/**
+ * 分页的返回值
+ */
+@Getter
+@Setter
+@ToString(callSuper = false)
+@NoArgsConstructor
+@AllArgsConstructor
+public class ResPage {
+
+    private long total;
+
+    private int page;
+
+    private int size;
+
+    public static ResPage getPage(long total, int page, int size) {
+        return new ResPage(total, page, size);
+    }
+
+    public static ResPage defaultPage() {
+        return new ResPage(10, 1, 10);
+    }
+}
diff --git a/src/main/java/com/example/myproject/common/base/Result.java b/src/main/java/com/example/myproject/common/base/Result.java
new file mode 100644
index 0000000..6d20f4f
--- /dev/null
+++ b/src/main/java/com/example/myproject/common/base/Result.java
@@ -0,0 +1,178 @@
+package com.example.myproject.common.base;
+
+
+import com.fasterxml.jackson.annotation.JsonIgnore;
+import com.fasterxml.jackson.annotation.JsonProperty;
+
+import lombok.Getter;
+import lombok.NoArgsConstructor;
+import lombok.Setter;
+import lombok.ToString;
+
+/**
+ * 返回值实体类
+ */
+@Getter
+@Setter
+@ToString
+@NoArgsConstructor
+public class Result<T> {
+
+    private int code;
+
+    private String msg;
+
+    @JsonProperty
+    private T data;
+
+    private ResPage page;
+
+
+    public Result(Status status) {
+        this.code = status.getCode();
+        this.msg = status.getMsg();
+        this.data = null;
+    }
+
+    public Result(Status status, T data) {
+        this.code = status.getCode();
+        this.msg = status.getMsg();
+        this.data = data;
+    }
+
+    public Result(Status status, String msg) {
+        this.code = status.getCode();
+        this.msg = msg;
+        this.data = null;
+    }
+
+    public Result(Status status, int msgCode) {
+        this.code = status.getCode();
+        this.msg = I18nMessage.getMessage(String.valueOf(msgCode));
+        this.data = null;
+    }
+
+    public Result(Status status, String msg, T data) {
+        this.code = status.getCode();
+        this.msg = msg;
+        this.data = data;
+    }
+
+    public Result(Status status, int msgCode, T data) {
+        this.code = status.getCode();
+        this.msg = I18nMessage.getMessage(String.valueOf(msgCode));
+        this.data = data;
+    }
+
+    public Result(Status status, T data, ResPage page) {
+        this.code = status.getCode();
+        this.msg = status.getMsg();
+        this.data = data;
+        this.page = page;
+    }
+
+    public Result(Status status, String msg, T data, ResPage page) {
+        this.code = status.getCode();
+        this.msg = msg;
+        this.data = data;
+        this.page = page;
+    }
+
+    public Result(Status status, int msgCode, T data, ResPage page) {
+        this.code = status.getCode();
+        this.msg = I18nMessage.getMessage(String.valueOf(msgCode));
+        this.data = data;
+        this.page = page;
+    }
+
+    @JsonIgnore
+    public boolean isSuccess() {
+        return this.code == Status.SUCCESS.getCode();
+    }
+
+    @JsonIgnore
+    public boolean nonSuccess() {
+        return this.code != Status.SUCCESS.getCode();
+    }
+
+    public static <T> Result<T> success(String 取消收藏成功) {
+        return new Result<T>(Status.SUCCESS);
+    }
+
+    public static <T> Result<T> ok() {
+        return new Result<T>(Status.SUCCESS);
+    }
+
+    public static <T> Result<T> ok(T data) {
+        return new Result<T>(Status.SUCCESS, data);
+    }
+
+    public static <T> Result<T> illegal() {
+        return new Result<T>(Status.BAD_REQUEST);
+    }
+
+    public static <T> Result<T> unauthorized() {
+        return new Result<T>(Status.UNAUTHORIZED);
+    }
+
+    public static <T> Result<T> forbidden() {
+        return new Result<T>(Status.FORBIDDEN);
+    }
+
+    public static <T> Result<T> notFound() {
+        return new Result<T>(Status.NOT_FOUND);
+    }
+
+    public static <T> Result<T> failure() {
+        return new Result<T>(Status.FAILURE);
+    }
+
+    public static <T> Result<T> failure(String msg) {
+        return new Result<T>(Status.FAILURE, msg);
+    }
+
+    public static <T> Result<T> error(String msg) {
+        return new Result<T>(Status.FAILURE, msg);
+    }
+
+    public static <T> Result<T> conflict() {
+        return new Result<T>(Status.CONFLICT);
+    }
+
+    public static <T> Result<T> build(Status status, T data) {
+        return new Result<T>(status, data);
+    }
+
+    public static <T> Result<T> build(Status status, String msg) {
+        return new Result<T>(status, msg);
+    }
+
+    public static <T> Result<T> build(Status status, int msgCode) {
+        return new Result<T>(status, msgCode);
+    }
+
+    public static <T> Result<T> build(Status status, String msg, T data) {
+        return new Result<T>(status, msg, data);
+    }
+
+    public static <T> Result<T> build(Status status, int msgCode, T data) {
+        return new Result<T>(status, msgCode, data);
+    }
+
+    public static Result ok(Object data, ResPage page) {
+        return new Result(Status.SUCCESS, data, page);
+    }
+
+    public static Result build(Status status, Object data, ResPage page) {
+        return new Result(status, data, page);
+    }
+
+    public static Result build(Status status, String msg, Object data, ResPage page) {
+        return new Result(status, msg, data, page);
+    }
+
+    public static Result build(Status status, int msgCode, Object data, ResPage page) {
+        return new Result(status, msgCode, data, page);
+    }
+
+}
diff --git a/src/main/java/com/example/myproject/common/base/Status.java b/src/main/java/com/example/myproject/common/base/Status.java
new file mode 100644
index 0000000..4949d0c
--- /dev/null
+++ b/src/main/java/com/example/myproject/common/base/Status.java
@@ -0,0 +1,57 @@
+package com.example.myproject.common.base;
+
+
+public enum Status {
+
+    /**
+     * 请求执行成功
+     */
+    SUCCESS(0, "操作成功"),
+
+    /**
+     * 请求验证失败
+     */
+    BAD_REQUEST(400, "操作验证失败"),
+
+    /**
+     * 权限不足
+     */
+    UNAUTHORIZED(401, "操作未授权"),
+
+    /**
+     * 请求拒绝
+     */
+    FORBIDDEN(403, "操作被拒绝"),
+
+    /**
+     * 未知请求
+     */
+    NOT_FOUND(404, "未知操作"),
+
+    /**
+     * 未知请求
+     */
+    CONFLICT(409, "请求发生冲突"),
+
+    /**
+     * 请求执行失败
+     */
+    FAILURE(500, "操作失败");
+
+    private int code;
+
+    private String msg;
+
+    Status(int code, String msg) {
+        this.code = code;
+        this.msg = msg;
+    }
+
+    public int getCode() {
+        return this.code;
+    }
+
+    public String getMsg() {
+        return this.msg;
+    }
+}
diff --git a/src/main/java/com/example/myproject/common/exception/RocketPTException.java b/src/main/java/com/example/myproject/common/exception/RocketPTException.java
new file mode 100644
index 0000000..7a475f2
--- /dev/null
+++ b/src/main/java/com/example/myproject/common/exception/RocketPTException.java
@@ -0,0 +1,29 @@
+package com.example.myproject.common.exception;
+
+import com.example.myproject.common.CommonResultStatus;
+import com.example.myproject.common.ResultStatus;
+
+public class RocketPTException extends RuntimeException {
+    private final ResultStatus status;
+
+    public RocketPTException(ResultStatus status) {
+        super(status.getMessage());
+        this.status = status;
+    }
+
+    public RocketPTException(ResultStatus status, String message) {
+        super(message);
+        this.status = status;
+    }
+
+    public RocketPTException(String message) {
+        super(message);
+        this.status = CommonResultStatus.FAIL;
+    }
+
+    public ResultStatus getStatus() {
+        return status;
+    }
+
+
+}