|
@@ -0,0 +1,176 @@
|
|
|
|
+package com.webchat.user.service.relation;
|
|
|
|
+
|
|
|
|
+
|
|
|
|
+import com.webchat.common.enums.AccountRelationTypeEnum;
|
|
|
|
+import com.webchat.common.enums.RoleCodeEnum;
|
|
|
|
+import com.webchat.common.util.ThreadPoolExecutorUtil;
|
|
|
|
+import com.webchat.domain.vo.response.UserBaseResponseInfoVO;
|
|
|
|
+import com.webchat.user.repository.dao.IAccountRelationDAO;
|
|
|
|
+import com.webchat.user.repository.entity.AccountRelationEntity;
|
|
|
|
+import com.webchat.user.service.UserService;
|
|
|
|
+import org.apache.commons.lang3.ObjectUtils;
|
|
|
|
+import org.springframework.beans.factory.annotation.Autowired;
|
|
|
|
+import org.springframework.util.Assert;
|
|
|
|
+
|
|
|
|
+import java.util.Date;
|
|
|
|
+
|
|
|
|
+public abstract class AbstractAccountRelationService implements AccountRelationValidator, AccountRelationWapper {
|
|
|
|
+
|
|
|
|
+ @Autowired
|
|
|
|
+ private IAccountRelationDAO accountRelationDAO;
|
|
|
|
+
|
|
|
|
+ @Autowired
|
|
|
|
+ private UserService userService;
|
|
|
|
+
|
|
|
|
+ /**
|
|
|
|
+ * 订阅关系类型,需由具体实现类返回
|
|
|
|
+ *
|
|
|
|
+ *
|
|
|
|
+ * @return
|
|
|
|
+ */
|
|
|
|
+ protected abstract AccountRelationTypeEnum getRelationType();
|
|
|
|
+
|
|
|
|
+ /**
|
|
|
|
+ * 是否需要异步执行后置处理流程
|
|
|
|
+ *
|
|
|
|
+ * @return
|
|
|
|
+ */
|
|
|
|
+ protected abstract boolean isAsyncDoAfterComplete();
|
|
|
|
+
|
|
|
|
+ /**
|
|
|
|
+ * 完成核心流程后的业务处理,由各实现类自己实现
|
|
|
|
+ * 比如:
|
|
|
|
+ * 1、基于不同关系的缓存构建、刷新
|
|
|
|
+ */
|
|
|
|
+ protected abstract void doAfterComplete(Long id, String sourceAccount, String targetAccount, boolean subscribe);
|
|
|
|
+
|
|
|
|
+ /**
|
|
|
|
+ * 异步执行后置业务处理流程
|
|
|
|
+ *
|
|
|
|
+ * @param id
|
|
|
|
+ * @param sourceAccount
|
|
|
|
+ * @param targetAccount
|
|
|
|
+ */
|
|
|
|
+ protected void asyncDoAfterComplete(Long id, String sourceAccount, String targetAccount, boolean subscribe) {
|
|
|
|
+
|
|
|
|
+ ThreadPoolExecutorUtil.execute(() -> doAfterComplete(id, sourceAccount, targetAccount, subscribe));
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ private void doExecAfterComplete(Long id, String sourceAccount, String targetAccount, boolean subscribe) {
|
|
|
|
+ if (isAsyncDoAfterComplete()) {
|
|
|
|
+ this.asyncDoAfterComplete(id, sourceAccount, targetAccount, subscribe);
|
|
|
|
+ } else {
|
|
|
|
+ this.doAfterComplete(id, sourceAccount, targetAccount, subscribe);
|
|
|
|
+ }
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ /**
|
|
|
|
+ * 前置基础校验
|
|
|
|
+ *
|
|
|
|
+ * @param sourceAccount
|
|
|
|
+ * @param targetAccount
|
|
|
|
+ */
|
|
|
|
+ @Override
|
|
|
|
+ public void validateAccountRequest(String sourceAccount, String targetAccount) {
|
|
|
|
+
|
|
|
|
+ UserBaseResponseInfoVO sourceAccountInfo = userService.getUserBaseInfoByUserId(sourceAccount);
|
|
|
|
+ Assert.notNull(sourceAccountInfo, "source account is null!");
|
|
|
|
+ Assert.isTrue(RoleCodeEnum.isUserRole(sourceAccountInfo.getRoleCode()), "source account role is not support!");
|
|
|
|
+ UserBaseResponseInfoVO targetAccountInfo = userService.getUserBaseInfoByUserId(targetAccount);
|
|
|
|
+ Assert.notNull(targetAccountInfo, "target account is null!");
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ @Override
|
|
|
|
+ public Boolean subscribe(String sourceAccount, String targetAccount) {
|
|
|
|
+ /**
|
|
|
|
+ * 前置参数校验
|
|
|
|
+ */
|
|
|
|
+ this.validateAccountRequest(sourceAccount, targetAccount);
|
|
|
|
+ /**
|
|
|
|
+ * 核心订阅流程处理
|
|
|
|
+ */
|
|
|
|
+ Long id = this.doSubscribe(sourceAccount, targetAccount);
|
|
|
|
+ /**
|
|
|
|
+ * 执行后置流程
|
|
|
|
+ */
|
|
|
|
+ this.doExecAfterComplete(id, sourceAccount, targetAccount, Boolean.TRUE);
|
|
|
|
+ return true;
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ /**
|
|
|
|
+ * 执行取消订阅的持久化更新逻辑
|
|
|
|
+ *
|
|
|
|
+ * @param sourceAccount
|
|
|
|
+ * @param targetAccount
|
|
|
|
+ * @return
|
|
|
|
+ */
|
|
|
|
+ @Override
|
|
|
|
+ public Boolean unsubscribe(String sourceAccount, String targetAccount) {
|
|
|
|
+ /**
|
|
|
|
+ * 前置参数校验
|
|
|
|
+ */
|
|
|
|
+ this.validateAccountRequest(sourceAccount, targetAccount);
|
|
|
|
+ /**
|
|
|
|
+ * 核心订阅流程处理
|
|
|
|
+ */
|
|
|
|
+ Long id = this.doUnSubscribe(sourceAccount, targetAccount);
|
|
|
|
+ /**
|
|
|
|
+ * 执行后置流程
|
|
|
|
+ */
|
|
|
|
+ this.doExecAfterComplete(id, sourceAccount, targetAccount, Boolean.FALSE);
|
|
|
|
+ return true;
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ /**
|
|
|
|
+ * 执行订阅的持久化更新逻辑
|
|
|
|
+ *
|
|
|
|
+ * @param sourceAccount
|
|
|
|
+ * @param targetAccount
|
|
|
|
+ * @return
|
|
|
|
+ */
|
|
|
|
+ private Long doSubscribe(String sourceAccount, String targetAccount) {
|
|
|
|
+ AccountRelationEntity entity = this.getAccountRelationEntity(sourceAccount, targetAccount);
|
|
|
|
+ Assert.isTrue(entity == null || ObjectUtils.equals(entity.getStatus(), false), "重复订阅!");
|
|
|
|
+ if (entity != null) {
|
|
|
|
+ entity.setStatus(Boolean.TRUE);
|
|
|
|
+ entity.setUpdateDate(new Date());
|
|
|
|
+ } else {
|
|
|
|
+ entity = buildAccountRelationEntity(sourceAccount, targetAccount);
|
|
|
|
+ }
|
|
|
|
+ return this.saveAccountRelation2DB(entity);
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ /**
|
|
|
|
+ * 执行取消订阅的持久化更新逻辑
|
|
|
|
+ * @param sourceAccount
|
|
|
|
+ * @param targetAccount
|
|
|
|
+ * @return
|
|
|
|
+ */
|
|
|
|
+ private Long doUnSubscribe(String sourceAccount, String targetAccount) {
|
|
|
|
+ AccountRelationEntity entity = this.getAccountRelationEntity(sourceAccount, targetAccount);
|
|
|
|
+ Assert.isTrue(entity != null || ObjectUtils.equals(entity.getStatus(), true), "未订阅,取消操作失败!");
|
|
|
|
+ entity.setStatus(Boolean.FALSE);
|
|
|
|
+ entity.setUpdateDate(new Date());
|
|
|
|
+ return this.saveAccountRelation2DB(entity);
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ private AccountRelationEntity getAccountRelationEntity(String sourceAccount, String targetAccount) {
|
|
|
|
+
|
|
|
|
+ return accountRelationDAO.findAllBySourceAccountAndTargetAccount(sourceAccount, targetAccount);
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ private Long saveAccountRelation2DB(AccountRelationEntity entity) {
|
|
|
|
+
|
|
|
|
+ return accountRelationDAO.save(entity).getId();
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ private AccountRelationEntity buildAccountRelationEntity(String sourceAccount, String targetAccount) {
|
|
|
|
+ AccountRelationEntity entity = new AccountRelationEntity();
|
|
|
|
+ entity.setSourceAccount(sourceAccount);
|
|
|
|
+ entity.setTargetAccount(targetAccount);
|
|
|
|
+ entity.setStatus(true);
|
|
|
|
+ entity.setType(getRelationType().getType());
|
|
|
|
+ entity.setCreateDate(new Date());
|
|
|
|
+ return entity;
|
|
|
|
+ }
|
|
|
|
+}
|