苍穹外卖--公共字段自动填充功能实现
This commit is contained in:
19
sky-server/src/main/java/com/sky/annotation/AutoFill.java
Normal file
19
sky-server/src/main/java/com/sky/annotation/AutoFill.java
Normal file
@@ -0,0 +1,19 @@
|
||||
package com.sky.annotation;
|
||||
|
||||
import com.sky.enumeration.OperationType;
|
||||
|
||||
import java.lang.annotation.ElementType;
|
||||
import java.lang.annotation.Retention;
|
||||
import java.lang.annotation.RetentionPolicy;
|
||||
import java.lang.annotation.Target;
|
||||
|
||||
/**
|
||||
* 公共字段填充注解(标记insert,update方法)
|
||||
*/
|
||||
|
||||
@Retention(RetentionPolicy.RUNTIME)
|
||||
@Target(ElementType.METHOD)
|
||||
public @interface AutoFill {
|
||||
//用来判断当前是什么操作:insert update
|
||||
OperationType value();
|
||||
}
|
||||
72
sky-server/src/main/java/com/sky/aspect/AutoFillAspect.java
Normal file
72
sky-server/src/main/java/com/sky/aspect/AutoFillAspect.java
Normal file
@@ -0,0 +1,72 @@
|
||||
package com.sky.aspect;
|
||||
|
||||
import com.sky.annotation.AutoFill;
|
||||
import com.sky.constant.AutoFillConstant;
|
||||
import com.sky.context.BaseContext;
|
||||
import com.sky.enumeration.OperationType;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.aspectj.lang.JoinPoint;
|
||||
import org.aspectj.lang.Signature;
|
||||
import org.aspectj.lang.annotation.Aspect;
|
||||
import org.aspectj.lang.annotation.Before;
|
||||
import org.aspectj.lang.annotation.Pointcut;
|
||||
import org.aspectj.lang.reflect.MethodSignature;
|
||||
import org.springframework.stereotype.Component;
|
||||
|
||||
import java.lang.reflect.Method;
|
||||
import java.time.LocalDateTime;
|
||||
|
||||
@Component
|
||||
@Aspect
|
||||
@Slf4j
|
||||
public class AutoFillAspect {
|
||||
//切入点
|
||||
@Pointcut("execution(* com.sky.mapper.*.*(..))&& @annotation(com.sky.annotation.AutoFill)")
|
||||
public void autoFillPointCut(){}
|
||||
|
||||
//通知方法
|
||||
@Before("autoFillPointCut()")
|
||||
public void autoFill(JoinPoint joinPoint){
|
||||
log.info("开始进行公共字段自动填充....");
|
||||
//获取连接点方法对象
|
||||
MethodSignature signature = (MethodSignature) joinPoint.getSignature();
|
||||
Method method = signature.getMethod();//获取反射中的方法对象(update()、insert()等)
|
||||
//获取方法的注解
|
||||
AutoFill annotation = method.getAnnotation(AutoFill.class);
|
||||
//判断当前注解的操作类型??如果是insert操作4个公共字段,如果是update操作2个公共字段
|
||||
OperationType type = annotation.value();
|
||||
//根据连接点获取方法的参数列表
|
||||
Object[] args = joinPoint.getArgs();
|
||||
//获取自定义实体类:Employee,category,Dish,Order.....等
|
||||
Object arg = args[0];
|
||||
|
||||
//进行公共字段赋值,赋什么值???
|
||||
LocalDateTime now = LocalDateTime.now();
|
||||
Long currentId = BaseContext.getCurrentId();
|
||||
|
||||
try {
|
||||
if (type == OperationType.INSERT) {
|
||||
//insert
|
||||
//使用反射操作对应的set方法进行赋值
|
||||
// Method setCreateTime = arg.getClass().getDeclaredMethod("setCreateTime", LocalDateTime.class);
|
||||
Method setCreateTime = arg.getClass().getDeclaredMethod(AutoFillConstant.SET_CREATE_TIME, LocalDateTime.class);
|
||||
setCreateTime.invoke(arg,now);
|
||||
Method setCreateUser = arg.getClass().getDeclaredMethod(AutoFillConstant.SET_CREATE_USER, Long.class);
|
||||
setCreateUser.invoke(arg,currentId);
|
||||
Method setUpdateTime = arg.getClass().getDeclaredMethod(AutoFillConstant.SET_UPDATE_TIME, LocalDateTime.class);
|
||||
setUpdateTime.invoke(arg,now);
|
||||
Method setUpdateUser = arg.getClass().getDeclaredMethod(AutoFillConstant.SET_UPDATE_USER, Long.class);
|
||||
setUpdateUser.invoke(arg,currentId);
|
||||
|
||||
} else {
|
||||
//update
|
||||
Method setUpdateTime = arg.getClass().getDeclaredMethod(AutoFillConstant.SET_UPDATE_TIME, LocalDateTime.class);
|
||||
setUpdateTime.invoke(arg,now);
|
||||
Method setUpdateUser = arg.getClass().getDeclaredMethod(AutoFillConstant.SET_UPDATE_USER, Long.class);
|
||||
setUpdateUser.invoke(arg,currentId);
|
||||
}
|
||||
} catch (Exception e) {
|
||||
throw new RuntimeException(e);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -1,6 +1,7 @@
|
||||
package com.sky.mapper;
|
||||
|
||||
import com.github.pagehelper.Page;
|
||||
import com.sky.annotation.AutoFill;
|
||||
import com.sky.enumeration.OperationType;
|
||||
import com.sky.dto.CategoryPageQueryDTO;
|
||||
import com.sky.entity.Category;
|
||||
@@ -16,6 +17,7 @@ public interface CategoryMapper {
|
||||
* 插入数据
|
||||
* @param category
|
||||
*/
|
||||
@AutoFill(OperationType.INSERT)
|
||||
@Insert("insert into category(type, name, sort, status, create_time, update_time, create_user, update_user)" +
|
||||
" VALUES" +
|
||||
" (#{type}, #{name}, #{sort}, #{status}, #{createTime}, #{updateTime}, #{createUser}, #{updateUser})")
|
||||
@@ -39,6 +41,7 @@ public interface CategoryMapper {
|
||||
* 根据id修改分类
|
||||
* @param category
|
||||
*/
|
||||
@AutoFill(OperationType.UPDATE)
|
||||
void update(Category category);
|
||||
|
||||
/**
|
||||
|
||||
@@ -1,8 +1,10 @@
|
||||
package com.sky.mapper;
|
||||
|
||||
import com.github.pagehelper.Page;
|
||||
import com.sky.annotation.AutoFill;
|
||||
import com.sky.dto.EmployeePageQueryDTO;
|
||||
import com.sky.entity.Employee;
|
||||
import com.sky.enumeration.OperationType;
|
||||
import org.apache.ibatis.annotations.Insert;
|
||||
import org.apache.ibatis.annotations.Mapper;
|
||||
import org.apache.ibatis.annotations.Select;
|
||||
@@ -22,12 +24,14 @@ public interface EmployeeMapper {
|
||||
* 新增员工
|
||||
* @param employee
|
||||
*/
|
||||
@AutoFill(OperationType.INSERT)
|
||||
@Insert("insert into employee (name, username, password, phone, sex, id_number, create_time, update_time, create_user, update_user) " +
|
||||
"values (#{name},#{username},#{password},#{phone},#{sex},#{idNumber},#{createTime},#{updateTime},#{createUser},#{updateUser})")
|
||||
void insert(Employee employee);
|
||||
|
||||
Page<Employee> pageQuery(EmployeePageQueryDTO employeePageQueryDTO);
|
||||
|
||||
@AutoFill(OperationType.UPDATE)
|
||||
void update(Employee employee);
|
||||
|
||||
@Select("select * from employee where id = #{id}")
|
||||
|
||||
@@ -48,10 +48,10 @@ public class CategoryServiceImpl implements CategoryService {
|
||||
category.setStatus(StatusConstant.DISABLE);
|
||||
|
||||
//设置创建时间、修改时间、创建人、修改人
|
||||
category.setCreateTime(LocalDateTime.now());
|
||||
category.setUpdateTime(LocalDateTime.now());
|
||||
category.setCreateUser(BaseContext.getCurrentId());
|
||||
category.setUpdateUser(BaseContext.getCurrentId());
|
||||
// category.setCreateTime(LocalDateTime.now());
|
||||
// category.setUpdateTime(LocalDateTime.now());
|
||||
// category.setCreateUser(BaseContext.getCurrentId());
|
||||
// category.setUpdateUser(BaseContext.getCurrentId());
|
||||
|
||||
categoryMapper.insert(category);
|
||||
}
|
||||
@@ -83,7 +83,7 @@ public class CategoryServiceImpl implements CategoryService {
|
||||
//查询当前分类是否关联了套餐,如果关联了就抛出业务异常
|
||||
count = setmealMapper.countByCategoryId(id);
|
||||
if(count > 0){
|
||||
//当前分类下有菜品,不能删除
|
||||
//当前分类下有套餐,不能删除
|
||||
throw new DeletionNotAllowedException(MessageConstant.CATEGORY_BE_RELATED_BY_SETMEAL);
|
||||
}
|
||||
|
||||
@@ -100,8 +100,8 @@ public class CategoryServiceImpl implements CategoryService {
|
||||
BeanUtils.copyProperties(categoryDTO,category);
|
||||
|
||||
//设置修改时间、修改人
|
||||
category.setUpdateTime(LocalDateTime.now());
|
||||
category.setUpdateUser(BaseContext.getCurrentId());
|
||||
// category.setUpdateTime(LocalDateTime.now());
|
||||
// category.setUpdateUser(BaseContext.getCurrentId());
|
||||
|
||||
categoryMapper.update(category);
|
||||
}
|
||||
|
||||
@@ -80,14 +80,14 @@ public class EmployeeServiceImpl implements EmployeeService {
|
||||
//2.设置账号的状态,默认正常状态:1,0表示禁用
|
||||
employee.setStatus(StatusConstant.ENABLE);
|
||||
//3.设置创建时间和更新时间
|
||||
employee.setCreateTime(LocalDateTime.now());
|
||||
employee.setUpdateTime(LocalDateTime.now());
|
||||
// employee.setCreateTime(LocalDateTime.now());
|
||||
// employee.setUpdateTime(LocalDateTime.now());
|
||||
//4.设置当前记录创建人和修改人的id
|
||||
//todo 后期要修改为当前登录用户的id
|
||||
//后期要修改为当前登录用户的id
|
||||
//取出拦截器中保存的登录员工ID,作为创建者和更新者ID数据
|
||||
Long empId = BaseContext.getCurrentId();
|
||||
employee.setCreateUser(empId);
|
||||
employee.setUpdateUser(empId);
|
||||
// employee.setCreateUser(empId);
|
||||
// employee.setUpdateUser(empId);
|
||||
|
||||
//添加默认密码
|
||||
employee.setPassword(DigestUtils.md5DigestAsHex(PasswordConstant.DEFAULT_PASSWORD.getBytes()));
|
||||
@@ -145,8 +145,8 @@ public class EmployeeServiceImpl implements EmployeeService {
|
||||
//属性拷贝
|
||||
BeanUtils.copyProperties(employeeDTO,employee);
|
||||
//补充属性
|
||||
employee.setUpdateUser(BaseContext.getCurrentId());
|
||||
employee.setUpdateTime(LocalDateTime.now());
|
||||
// employee.setUpdateUser(BaseContext.getCurrentId());
|
||||
// employee.setUpdateTime(LocalDateTime.now());
|
||||
employeeMapper.update(employee);
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user