Authored by LUOXC

refactor

... ... @@ -17,9 +17,11 @@ import java.util.concurrent.ArrayBlockingQueue;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.TimeUnit;
import java.util.function.Consumer;
import java.util.function.Function;
import java.util.function.Supplier;
@Slf4j
@Slf4j(topic = "notice")
public class BaseNoticeFacade {
private ExecutorService executorService = new ThreadPoolExecutor(5, 10,
... ... @@ -42,6 +44,7 @@ public class BaseNoticeFacade {
public class Notice {
private final int uid;
private String logPrefix;
private InboxReqVO inBoxContent;
private Supplier<InboxReqVO> inBoxContentSupplier;
... ... @@ -55,6 +58,11 @@ public class BaseNoticeFacade {
this.uid = uid;
}
public Notice withLogPrefix(String logPrefix) {
this.logPrefix = logPrefix;
return this;
}
public Notice withInBox(InboxBusinessTypeEnum businessType, Object... args) {
inBoxContent = buildInboxContent(businessType, args);
return this;
... ... @@ -82,65 +90,76 @@ public class BaseNoticeFacade {
public void send() {
executorService.execute(() -> {
log.warn("send, uid {} inBoxContent {} smsContent {}", uid, inBoxContent, smsContent);
InboxReqVO inBoxContent = getInBoxContent();
final String logPrefix = getReplacedContent("{} send uid {}", this.logPrefix, this.uid);
log.warn("{}, inBoxContent {} smsContent {}", logPrefix, inBoxContent, smsContent);
// 发送站内信
InboxReqVO inBoxContent = getInBoxContentWhenExceptionAnd(e -> {
log.warn("{}, send in box fail, get content fail", logPrefix, e);
return null;
});
if (Objects.nonNull(inBoxContent)) {
try {
inBoxContent.setUid(uid);
InBoxResponse resp = inBoxSDK.addInbox(inBoxContent);
log.info("send in box success, uid {}, content {} response {}", uid, inBoxContent, resp);
log.info("{}, send in box success content {} response {}", logPrefix, inBoxContent, resp);
} catch (Exception e) {
log.warn("send in box fail, uid {} content {}", uid, inBoxContent, e);
log.warn("{}, send in box fail content {}", logPrefix, inBoxContent, e);
}
}
// 发送PUSH
if (Objects.nonNull(pushSupplier)) {
try {
SendMessageRspBo resp = pushSupplier.get();
log.info("send push success, uid {}, response {}", uid, inBoxContent, resp);
log.info("{}, send push success, response {}", logPrefix, resp);
} catch (Exception e) {
log.warn("send push fail, uid {}", uid, e);
log.warn("{}, send push fail", logPrefix, e);
}
}
String smsContent = getSmsContent();
// 发送短信
String smsContent = getSmsContentWhenExceptionAnd(e -> {
log.warn("{}, send sms fail, get content fail", logPrefix, e);
return null;
});
if (Objects.nonNull(smsContent)) {
try {
String phone = userProxyService.getMobile(uid);
if (StringUtils.isBlank(phone)) {
log.warn("send sms fail, uid {} content {} phone is blank", uid, smsContent);
log.warn("{}, send sms fail content {} phone is blank", logPrefix, smsContent);
} else {
List<String> mobileList = Arrays.asList(phone);
sendSmsService.smsSendByMobile(smsContent, mobileList);
log.info("send sms success, uid {} content {} phone {} ", uid, smsContent, phone);
log.info("{}, send sms success content {} phone {} ", logPrefix, smsContent, phone);
}
} catch (Exception e) {
log.warn("send sms fail, uid {} content {}", uid, smsContent, e);
log.warn("{}, send sms fail content {}", logPrefix, smsContent, e);
}
}
// END
});
}
private InboxReqVO getInBoxContent() {
private InboxReqVO getInBoxContentWhenExceptionAnd(Function<Exception, InboxReqVO> exceptionFunction) {
if (Objects.nonNull(inBoxContentSupplier)) {
try {
return inBoxContentSupplier.get();
} catch (Exception e) {
log.warn("send in box fail, uid {} get content fail", uid, e);
return null;
return exceptionFunction.apply(e);
}
} else {
return this.inBoxContent;
}
}
private String getSmsContent() {
private String getSmsContentWhenExceptionAnd(Function<Exception, String> exceptionFunction) {
if (Objects.nonNull(smsContentSupplier)) {
try {
return smsContentSupplier.get();
} catch (Exception e) {
log.warn("send sms fail, uid {} get content fail", uid, e);
return null;
return exceptionFunction.apply(e);
}
} else {
return this.smsContent;
... ... @@ -148,7 +167,6 @@ public class BaseNoticeFacade {
}
}
protected String buildParams(Object... objects) {
... ... @@ -172,7 +190,7 @@ public class BaseNoticeFacade {
return req;
}
protected InboxReqVO buildInboxContent(InboxBusinessTypeEnum ibt,Object... params) {
protected InboxReqVO buildInboxContent(InboxBusinessTypeEnum ibt, Object... params) {
InboxReqVO req = new InboxReqVO();
req.setType(ibt.getType());
req.setBusinessType(ibt.getBusinessType());
... ... @@ -184,7 +202,7 @@ public class BaseNoticeFacade {
return MessageFormatter.arrayFormat(content, params).getMessage();
}
protected String buildSmsContent(InboxBusinessTypeEnum ibte, Object... params){
protected String buildSmsContent(InboxBusinessTypeEnum ibte, Object... params) {
return getReplacedContent(ibte.getContent(), params);
}
... ...
... ... @@ -35,6 +35,7 @@ public class BuyerNoticeFacade extends BaseNoticeFacade {
String sizeName = psog.getSizeName();
String productCode = Optional.ofNullable(product).map(Product::getProductCode).orElse("");
newNotice(buyerUid)
.withLogPrefix("notice buyer delivery deposit goods to buyer")
.withInBox(InboxBusinessTypeEnum.NOTICE_BUYER_WHEN_DEPOSIT_GOODS_PURCHASE_SENDED, prdName, sizeName, productCode)
.withSms(InboxBusinessTypeEnum.SMS_NOTICE_BUYER_WHEN_DEPOSIT_GOODS_PURCHASE_SENDED, prdName, sizeName, productCode, wayBillCode)
.send();
... ... @@ -59,6 +60,7 @@ public class BuyerNoticeFacade extends BaseNoticeFacade {
String productCode = Optional.ofNullable(product).map(Product::getProductCode).orElse("");
newNotice(buyerUid)
.withLogPrefix("notice buyer delivery goods to buyer")
.withInBox(InboxBusinessTypeEnum.PURCHASE_SENDED, orderCode)
.withSms(InboxBusinessTypeEnum.SMS_SEND, skupTypeText, prdName, sizeName, productCode, orderCode)
.withPush(() -> ufoSendService.platformDeliverBuyer(String.valueOf(buyerUid), String.valueOf(orderCode)))
... ...
... ... @@ -73,6 +73,7 @@ public class InBoxFacade extends BaseNoticeFacade{
public void buyerOrderNotPayed(int buyerUid, long orderCode) {
logger.info("notice buyer not paid, buyerUid {}, orderCode {}", buyerUid, orderCode);
newNotice(buyerUid)
.withLogPrefix("notice buyer not paid")
.withInBox(InboxBusinessTypeEnum.PURCHASE_UNPAID, orderCode)
.withPush(() -> ufoSendService.buyerNotPay(String.valueOf(buyerUid), String.valueOf(orderCode)))
.send();
... ... @@ -88,6 +89,7 @@ public class InBoxFacade extends BaseNoticeFacade{
logger.info("notice buyer finish paid, buyerUid {}, orderCode {}", buyerUid, orderCode);
String timelimitDesc = new StringBuilder().append(deliveryHours).append(unit).toString();
newNotice(buyerUid)
.withLogPrefix("notice buyer finish paid")
.withInBox(InboxBusinessTypeEnum.PURCHASE_NOTIFIED_SELLER, orderCode, timelimitDesc)
.send();
}
... ... @@ -103,6 +105,7 @@ public class InBoxFacade extends BaseNoticeFacade{
try {
logger.info("notice buyer seller deliver 2 depot, buyerUid {}, orderCode {}", buyerUid, orderCode);
newNotice(buyerUid)
.withLogPrefix("notice buyer seller deliver 2 depot")
.withInBox(InboxBusinessTypeEnum.NOTICE_BUYER_WHEN_SELLER_DELIVER, orderCode)
.withPush(() -> ufoSendService.sellerDeliverNotice(String.valueOf(buyerUid), String.valueOf(orderCode)))
.withSms(() -> {
... ... @@ -128,6 +131,7 @@ public class InBoxFacade extends BaseNoticeFacade{
try {
logger.info("notice buyer appraise pass, buyerUid {}, orderCode {}", buyerUid, orderCode);
newNotice(buyerUid)
.withLogPrefix("notice buyer appraise pass")
.withInBox(InboxBusinessTypeEnum.PURCHASE_SENDED, orderCode)
.withSms(() -> {
Product product = productMapper.selectByPrimaryKey(psog.getProductId());
... ... @@ -147,6 +151,7 @@ public class InBoxFacade extends BaseNoticeFacade{
try {
logger.info("notice seller appraise pass, buyerUid {}, orderCode {}", buyerUid, orderCode);
newNotice(psog.getUid())
.withLogPrefix("notice seller appraise pass")
.withInBox(() -> {
String prdName = psog.getProductName();
Product product = productMapper.selectByPrimaryKey(psog.getProductId());
... ...