[go: up one dir, main page]

Skip to content
This repository has been archived by the owner on Aug 31, 2023. It is now read-only.

Commit

Permalink
商品品牌管理基础CRUD及数据校验
Browse files Browse the repository at this point in the history
  • Loading branch information
kirklin committed Jun 10, 2021
1 parent be70a27 commit c742c18
Show file tree
Hide file tree
Showing 46 changed files with 4,211 additions and 29 deletions.
1 change: 1 addition & 0 deletions kkmall-common/kkmall-common.iml
Original file line number Diff line number Diff line change
Expand Up @@ -79,5 +79,6 @@
<orderEntry type="library" name="Maven: mysql:mysql-connector-java:8.0.23" level="project" />
<orderEntry type="library" name="Maven: com.google.protobuf:protobuf-java:3.11.4" level="project" />
<orderEntry type="library" scope="PROVIDED" name="Maven: javax.servlet:servlet-api:2.5" level="project" />
<orderEntry type="library" name="Maven: javax.validation:validation-api:2.0.1.Final" level="project" />
</component>
</module>
5 changes: 5 additions & 0 deletions kkmall-common/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -84,5 +84,10 @@
<version>${javax.servlet.version}</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>javax.validation</groupId>
<artifactId>validation-api</artifactId>
<version>2.0.1.Final</version>
</dependency>
</dependencies>
</project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
package name.lkk.common.enume;

public enum OrderStatusEnum {
CREATE_NEW(0,"待付款"),
PAYED(1,"已付款"),
SENDED(2,"已发货"),
RECIEVED(3,"已完成"),
CANCLED(4,"已取消"),
SERVICING(5,"售后中"),
SERVICED(6,"售后完成");
private Integer code;
private String msg;

OrderStatusEnum(Integer code, String msg) {
this.code = code;
this.msg = msg;
}

public Integer getCode() {
return code;
}

public String getMsg() {
return msg;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
package name.lkk.common.exception;

/**
* <p>Title: BizCodeEnum</p>
* Description:
* 错误码和错误信息定义类
* 1. 错误码定义规则为5为数字
* 2. 前两位表示业务场景,最后三位表示错误码。例如:100001。10:通用 001:系统未知异常
* 3. 维护错误码后需要维护错误描述,将他们定义为枚举形式
* 错误码列表:
* 10: 通用
* 001:参数格式校验
* 002: 短信验证码频率太高
* 11: 商品
* 12: 订单
* 13: 购物车
* 14: 物流
* 15: 用户
* 21: 库存
*/
public enum BizCodeEnum {
UNKNOW_EXCEPTION(10000, "系统未知异常"),
VAILD_EXCEPTION(10001, "参数格式校验失败"),
SMS_CODE_EXCEPTION(10002, "验证码获取频率太高,稍后再试"),
TO_MANY_REQUEST(10003, "请求流量过大"),
SMS_SEND_CODE_EXCEPTION(10403, "短信发送失败"),
USER_EXIST_EXCEPTION(15001, "用户已经存在"),
PHONE_EXIST_EXCEPTION(15002, "手机号已经存在"),
LOGINACTT_PASSWORD_ERROR(15003, "账号或密码错误"),
SOCIALUSER_LOGIN_ERROR(15004, "社交账号登录失败"),
NOT_STOCK_EXCEPTION(21000, "商品库存不足"),
PRODUCT_UP_EXCEPTION(11000,"商品上架异常");

private int code;

private String msg;

BizCodeEnum(int code, String msg) {
this.code = code;
this.msg = msg;
}

public int getCode() {
return code;
}

public String getMsg() {
return msg;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
package name.lkk.common.exception;

/**
* <p>Title: NotStockException</p>
* Description:
* date:2020/7/2 11:43
*/
public class NotStockException extends RuntimeException{

private Long skuId;

public NotStockException(String msg) {
super(msg + "号商品没有足够的库存了");
}

public Long getSkuId() {
return skuId;
}

public void setSkuId(Long skuId) {
this.skuId = skuId;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package name.lkk.common.valid;

/**
* 新增分组
*/
public interface AddGroup {
}
30 changes: 30 additions & 0 deletions kkmall-common/src/main/java/name/lkk/common/valid/ListValue.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
package name.lkk.common.valid;

import javax.validation.Constraint;
import javax.validation.Payload;
import java.lang.annotation.Documented;
import java.lang.annotation.Retention;
import java.lang.annotation.Target;

import static java.lang.annotation.ElementType.*;
import static java.lang.annotation.RetentionPolicy.RUNTIME;

/**
* <p>Title: ListValue</p>
* Description:JSR303自定义注解 必须有前三个方法
* date:2020/6/1 23:25
*/
@Documented
// 指定校验器 这里可以指定多个不同的校验器
@Constraint(validatedBy = { ListValueConstraintValidator.class })
@Target({ METHOD, FIELD, ANNOTATION_TYPE, CONSTRUCTOR, PARAMETER, TYPE_USE })
@Retention(RUNTIME)
public @interface ListValue {
String message() default "{com.firenay.common.valid.ListValue.message}";

Class<?>[] groups() default { };

Class<? extends Payload>[] payload() default { };

int[] vals() default { };
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
package name.lkk.common.valid;

import javax.validation.ConstraintValidator;
import javax.validation.ConstraintValidatorContext;
import java.util.HashSet;
import java.util.Set;

/**
* <p>Title: ListValueConstraintValidator</p>
* Description:校验器:规定ListValue这个注解 用于校验 Integer 类型的数据
* POSTman :{"name":"aaa","logo":"https://github.com/1046762075","sort":0,"firstLetter":"d","showStatus":0}
* date:2020/6/1 23:33
*/
public class ListValueConstraintValidator implements ConstraintValidator<ListValue,Integer> {

/**
* set 里面就是使用注解时规定的值, 例如: @ListValue(vals = {0,1}) set= {0,1}
*/
private Set<Integer> set = new HashSet<>();

//初始化方法
@Override
public void initialize(ListValue constraintAnnotation) {

int[] vals = constraintAnnotation.vals();
for (int val : vals) {
set.add(val);
}
}

/**
* 判断是否校验成功
* @param value 需要校验的值
* 判断这个值再set里面没
*/
@Override
public boolean isValid(Integer value, ConstraintValidatorContext context) {
return set.contains(value);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package name.lkk.common.valid;

/**
* 修改分组
*/
public interface UpdateGroup {
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
package name.lkk.common.valid;

public interface UpdateStatusGroup {
}
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
com.firenay.common.valid.ListValue.message=\u5FC5\u987B\u63D0\u4EA4\u6307\u5B9A\u7684\u503C [0,1]
8 changes: 8 additions & 0 deletions kkmall-gateway/src/main/resources/application.yml
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,14 @@ spring:
- Path=/api/ware/**
filters:
- RewritePath=/api/(?<segment>.*), /$\{segment}
# OSS签名的转发
- id: third-server_route
uri: lb://kkmall-third-server
predicates:
- Path=/api/third/server/**
filters:
- RewritePath=/api/(?<segment>.*), /$\{segment}

#过滤器要放最下面才不会被替换路径/api/*路径
- id: admin_route
uri: lb://kkmall-admin
Expand Down
Original file line number Diff line number Diff line change
@@ -1,14 +1,15 @@
package name.lkk.kkmall.product.controller;

import java.util.Arrays;
import java.util.List;
import java.util.Map;

import name.lkk.common.valid.AddGroup;
import name.lkk.common.valid.UpdateGroup;
import name.lkk.common.valid.UpdateStatusGroup;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
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 org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import name.lkk.kkmall.product.entity.BrandEntity;
import name.lkk.kkmall.product.service.BrandService;
Expand Down Expand Up @@ -53,25 +54,40 @@ public R info(@PathVariable("brandId") Long brandId){
return R.ok().put("brand", brand);
}

@GetMapping("/infos")
public R info(@RequestParam("brandIds") List<Long> brandIds) {
List<BrandEntity> brand = brandService.getBrandByIds(brandIds);
return R.ok().put("data", brand);
}

/**
* 保存
* 保存 开启JSR303校验 规定这是新增分组 实现新增的规则
* POSTman:{"name":"aaa","logo":"abc","brandId":1}
* POSTman :{"name":"aaa","logo":"https://github.com/1046762075","sort":0,"firstLetter":"d","showStatus":0}
*/
@RequestMapping("/save")
//@RequiresPermissions("product:brand:save")
public R save(@RequestBody BrandEntity brand){
brandService.save(brand);
public R save(@Validated(AddGroup.class) @RequestBody BrandEntity brand) {
brandService.save(brand);

return R.ok();
}

/**
* 修改
* POSTman:{"name":"aaa","logo":"abc"}
*/
@RequestMapping("/update")
//@RequiresPermissions("product:brand:update")
public R update(@RequestBody BrandEntity brand){
brandService.updateById(brand);
public R update(@Validated(UpdateGroup.class) @RequestBody BrandEntity brand) {
brandService.updateDetail(brand);
return R.ok();
}

/**
* 修改状态
*/
@RequestMapping("/update/status")
public R updateStatus(@Validated(UpdateStatusGroup.class) @RequestBody BrandEntity brand) {
brandService.updateById(brand);
return R.ok();
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,35 +2,52 @@

import com.baomidou.mybatisplus.annotation.TableId;
import com.baomidou.mybatisplus.annotation.TableName;
import lombok.Data;
import name.lkk.common.valid.AddGroup;
import name.lkk.common.valid.ListValue;
import name.lkk.common.valid.UpdateGroup;
import name.lkk.common.valid.UpdateStatusGroup;
import org.hibernate.validator.constraints.URL;

import javax.validation.constraints.Min;
import javax.validation.constraints.NotBlank;
import javax.validation.constraints.NotEmpty;
import javax.validation.constraints.NotNull;
import javax.validation.constraints.Null;
import javax.validation.constraints.Pattern;
import java.io.Serializable;
import java.util.Date;
import lombok.Data;

/**
* 品牌
*
* @author KirkLin
* @email linkirk@163.com
* @date 2021-06-07 15:14:37
* 自定义JSR303校验
* 根据分组进行校验 Controller里面要进行规定
*/
@Data
@TableName("pms_brand")
public class BrandEntity implements Serializable {

private static final long serialVersionUID = 1L;

/**
* 品牌id
* POSTman:{"name":"aaa","logo":"abc","brandId":1}
*/
@NotNull(message = "修改必须定制品牌id", groups = {UpdateGroup.class})
@Null(message = "新增不能指定id", groups = {AddGroup.class})
@TableId
private Long brandId;

/**
* 品牌名
*/
@NotBlank(message = "品牌名必须提交", groups = {AddGroup.class, UpdateGroup.class})
private String name;

/**
* 品牌logo地址
* 品牌logo地址 修改可以不带上logoURL
*/
@NotBlank(groups = {AddGroup.class})
@URL(message = "logo必须是一个合法的URL地址", groups={AddGroup.class, UpdateGroup.class})
private String logo;
/**
* 介绍
Expand All @@ -39,14 +56,21 @@ public class BrandEntity implements Serializable {
/**
* 显示状态[0-不显示;1-显示]
*/
@NotNull(groups = {AddGroup.class, UpdateStatusGroup.class})
@ListValue(vals = {0,1}, groups = {AddGroup.class, UpdateGroup.class, UpdateStatusGroup.class})
private Integer showStatus;

/**
* 检索首字母
* 检索首字母 修改可以不带, 不管是新增还是修改都必须是一个字母
*/
@NotEmpty(groups = {AddGroup.class})
@Pattern(regexp = "^[a-zA-Z]$", message = "检索首字母必须是一个字母", groups = {AddGroup.class, UpdateGroup.class})
private String firstLetter;
/**
* 排序
*/
@NotNull(groups = {AddGroup.class})
@Min(value = 0, message = "排序必须是一个正整数" , groups = {AddGroup.class, UpdateGroup.class})
private Integer sort;

}
Loading

0 comments on commit c742c18

Please sign in to comment.