當經(jīng)紀人創(chuàng)建客戶時,需要給對應的經(jīng)紀人增加戰(zhàn)報信息。在代碼層面上,客源的相關(guān)類只針對客源數(shù)據(jù)表操作。而戰(zhàn)報信息包含了多種業(yè)務(wù)統(tǒng)計數(shù)據(jù),客源只是其中統(tǒng)計的部分數(shù)據(jù)。鑒于兩者相對獨立,且客源的戰(zhàn)報信息會有所修改。因此,采用AOP+觀察者模式構(gòu)建代碼。
網(wǎng)站建設(shè)哪家好,找成都創(chuàng)新互聯(lián)公司!專注于網(wǎng)頁設(shè)計、網(wǎng)站建設(shè)、微信開發(fā)、小程序定制開發(fā)、集團企業(yè)網(wǎng)站建設(shè)等服務(wù)項目。為回饋新老客戶創(chuàng)新互聯(lián)還提供了萬山免費建站歡迎大家使用!
定義一個注解,用于AOP攔截。
/**
* 戰(zhàn)報注解
*/
@Retention(RetentionPolicy.RUNTIME)
@Target({ElementType.METHOD, ElementType.PARAMETER})
@Documented
public @interface AchievementAnnotation {
OperateEnum operate() default OperateEnum.ADD;
enum OperateEnum{
ADD,UPDATE,DELETE
}
}
定義AOP,用戶獲取數(shù)據(jù),并轉(zhuǎn)發(fā)給觀察者
/**
* 戰(zhàn)報AOP
*/
@Aspect
@Component
public class AchievementAop {
/**
* 戰(zhàn)報觀察者列表
*/
private List observerList;
public AchievementAop() {
this.observerList = new ArrayList<>();
}
public List getObserverList() {
return observerList;
}
public void setObserverList(List observerList) {
if (null != this.observerList)
this.observerList.addAll(observerList);
this.observerList = observerList;
}
/**
*注入客源的觀察者
*/
@Autowired
public void setCustomerAchievementObserver(CustomerAchievementObserver customerAchievementObserver) {
getObserverList().add(customerAchievementObserver);
}
@Pointcut("@annotation(com.pretang.cloud.aop.AchievementAnnotation)")
private void pointCut() {
}
@AfterReturning(pointcut = "pointCut()", returning = "retVal")
public void after(JoinPoint joinPoint, Object retVal) {
Signature signature = joinPoint.getSignature();
MethodSignature methodSignature = (MethodSignature) signature;
Method targetMethod = methodSignature.getMethod();
AchievementAnnotation annotation = targetMethod.getAnnotation(AchievementAnnotation.class);
AchievementAnnotation.OperateEnum operateEnum = annotation.operate();
for (AchievementObserver observer : observerList) {
if (observer.isSupport(retVal))
observer.execute(retVal);
}
}
}
定義觀察者通用接口
/**
* 戰(zhàn)報信息觀察者接口
* @param
*/
public interface AchievementObserver {
/**
* 是否支持該對象
* @param obj
* @return
*/
boolean isSupport(Object obj);
/**
* 操作業(yè)務(wù)數(shù)據(jù)
* @param t
* @throws RuntimeException
*/
void execute(T t) throws RuntimeException;
}
客源觀察者
/**
* 客源信息的觀察者
*/
@Component
public class CustomerAchievementObserver implements AchievementObserver {
@Autowired
private CustomerRpcService customerRpcService;
@Override
public boolean isSupport(Object obj) {
return obj instanceof CustomerBase;
}
@Override
public void execute(CustomerBase customerBase) throws RuntimeException {
// 實際業(yè)務(wù)處理
customerRpcService.saveAchievement(customerBase.getAgentUserId(), "ADD_CUSTOMER", customerBase.getId());
}
}