Authored by LUOXC

refactor

@@ -17,9 +17,11 @@ import java.util.concurrent.ArrayBlockingQueue; @@ -17,9 +17,11 @@ import java.util.concurrent.ArrayBlockingQueue;
17 import java.util.concurrent.ExecutorService; 17 import java.util.concurrent.ExecutorService;
18 import java.util.concurrent.ThreadPoolExecutor; 18 import java.util.concurrent.ThreadPoolExecutor;
19 import java.util.concurrent.TimeUnit; 19 import java.util.concurrent.TimeUnit;
  20 +import java.util.function.Consumer;
  21 +import java.util.function.Function;
20 import java.util.function.Supplier; 22 import java.util.function.Supplier;
21 23
22 -@Slf4j 24 +@Slf4j(topic = "notice")
23 public class BaseNoticeFacade { 25 public class BaseNoticeFacade {
24 26
25 private ExecutorService executorService = new ThreadPoolExecutor(5, 10, 27 private ExecutorService executorService = new ThreadPoolExecutor(5, 10,
@@ -42,6 +44,7 @@ public class BaseNoticeFacade { @@ -42,6 +44,7 @@ public class BaseNoticeFacade {
42 public class Notice { 44 public class Notice {
43 45
44 private final int uid; 46 private final int uid;
  47 + private String logPrefix;
45 48
46 private InboxReqVO inBoxContent; 49 private InboxReqVO inBoxContent;
47 private Supplier<InboxReqVO> inBoxContentSupplier; 50 private Supplier<InboxReqVO> inBoxContentSupplier;
@@ -55,6 +58,11 @@ public class BaseNoticeFacade { @@ -55,6 +58,11 @@ public class BaseNoticeFacade {
55 this.uid = uid; 58 this.uid = uid;
56 } 59 }
57 60
  61 + public Notice withLogPrefix(String logPrefix) {
  62 + this.logPrefix = logPrefix;
  63 + return this;
  64 + }
  65 +
58 public Notice withInBox(InboxBusinessTypeEnum businessType, Object... args) { 66 public Notice withInBox(InboxBusinessTypeEnum businessType, Object... args) {
59 inBoxContent = buildInboxContent(businessType, args); 67 inBoxContent = buildInboxContent(businessType, args);
60 return this; 68 return this;
@@ -82,65 +90,76 @@ public class BaseNoticeFacade { @@ -82,65 +90,76 @@ public class BaseNoticeFacade {
82 90
83 public void send() { 91 public void send() {
84 executorService.execute(() -> { 92 executorService.execute(() -> {
85 - log.warn("send, uid {} inBoxContent {} smsContent {}", uid, inBoxContent, smsContent);  
86 - InboxReqVO inBoxContent = getInBoxContent(); 93 + final String logPrefix = getReplacedContent("{} send uid {}", this.logPrefix, this.uid);
  94 + log.warn("{}, inBoxContent {} smsContent {}", logPrefix, inBoxContent, smsContent);
  95 +
  96 + // 发送站内信
  97 + InboxReqVO inBoxContent = getInBoxContentWhenExceptionAnd(e -> {
  98 + log.warn("{}, send in box fail, get content fail", logPrefix, e);
  99 + return null;
  100 + });
87 if (Objects.nonNull(inBoxContent)) { 101 if (Objects.nonNull(inBoxContent)) {
88 try { 102 try {
89 inBoxContent.setUid(uid); 103 inBoxContent.setUid(uid);
90 InBoxResponse resp = inBoxSDK.addInbox(inBoxContent); 104 InBoxResponse resp = inBoxSDK.addInbox(inBoxContent);
91 - log.info("send in box success, uid {}, content {} response {}", uid, inBoxContent, resp); 105 + log.info("{}, send in box success content {} response {}", logPrefix, inBoxContent, resp);
92 } catch (Exception e) { 106 } catch (Exception e) {
93 - log.warn("send in box fail, uid {} content {}", uid, inBoxContent, e); 107 + log.warn("{}, send in box fail content {}", logPrefix, inBoxContent, e);
94 } 108 }
95 } 109 }
  110 +
  111 + // 发送PUSH
96 if (Objects.nonNull(pushSupplier)) { 112 if (Objects.nonNull(pushSupplier)) {
97 try { 113 try {
98 SendMessageRspBo resp = pushSupplier.get(); 114 SendMessageRspBo resp = pushSupplier.get();
99 - log.info("send push success, uid {}, response {}", uid, inBoxContent, resp); 115 + log.info("{}, send push success, response {}", logPrefix, resp);
100 } catch (Exception e) { 116 } catch (Exception e) {
101 - log.warn("send push fail, uid {}", uid, e); 117 + log.warn("{}, send push fail", logPrefix, e);
102 } 118 }
103 } 119 }
104 120
105 - String smsContent = getSmsContent(); 121 + // 发送短信
  122 + String smsContent = getSmsContentWhenExceptionAnd(e -> {
  123 + log.warn("{}, send sms fail, get content fail", logPrefix, e);
  124 + return null;
  125 + });
106 if (Objects.nonNull(smsContent)) { 126 if (Objects.nonNull(smsContent)) {
107 try { 127 try {
108 String phone = userProxyService.getMobile(uid); 128 String phone = userProxyService.getMobile(uid);
109 if (StringUtils.isBlank(phone)) { 129 if (StringUtils.isBlank(phone)) {
110 - log.warn("send sms fail, uid {} content {} phone is blank", uid, smsContent); 130 + log.warn("{}, send sms fail content {} phone is blank", logPrefix, smsContent);
111 } else { 131 } else {
112 List<String> mobileList = Arrays.asList(phone); 132 List<String> mobileList = Arrays.asList(phone);
113 sendSmsService.smsSendByMobile(smsContent, mobileList); 133 sendSmsService.smsSendByMobile(smsContent, mobileList);
114 - log.info("send sms success, uid {} content {} phone {} ", uid, smsContent, phone); 134 + log.info("{}, send sms success content {} phone {} ", logPrefix, smsContent, phone);
115 } 135 }
116 } catch (Exception e) { 136 } catch (Exception e) {
117 - log.warn("send sms fail, uid {} content {}", uid, smsContent, e); 137 + log.warn("{}, send sms fail content {}", logPrefix, smsContent, e);
118 } 138 }
119 } 139 }
120 140
  141 + // END
121 }); 142 });
122 } 143 }
123 144
124 - private InboxReqVO getInBoxContent() { 145 + private InboxReqVO getInBoxContentWhenExceptionAnd(Function<Exception, InboxReqVO> exceptionFunction) {
125 if (Objects.nonNull(inBoxContentSupplier)) { 146 if (Objects.nonNull(inBoxContentSupplier)) {
126 try { 147 try {
127 return inBoxContentSupplier.get(); 148 return inBoxContentSupplier.get();
128 } catch (Exception e) { 149 } catch (Exception e) {
129 - log.warn("send in box fail, uid {} get content fail", uid, e);  
130 - return null; 150 + return exceptionFunction.apply(e);
131 } 151 }
132 } else { 152 } else {
133 return this.inBoxContent; 153 return this.inBoxContent;
134 } 154 }
135 } 155 }
136 156
137 - private String getSmsContent() { 157 + private String getSmsContentWhenExceptionAnd(Function<Exception, String> exceptionFunction) {
138 if (Objects.nonNull(smsContentSupplier)) { 158 if (Objects.nonNull(smsContentSupplier)) {
139 try { 159 try {
140 return smsContentSupplier.get(); 160 return smsContentSupplier.get();
141 } catch (Exception e) { 161 } catch (Exception e) {
142 - log.warn("send sms fail, uid {} get content fail", uid, e);  
143 - return null; 162 + return exceptionFunction.apply(e);
144 } 163 }
145 } else { 164 } else {
146 return this.smsContent; 165 return this.smsContent;
@@ -148,7 +167,6 @@ public class BaseNoticeFacade { @@ -148,7 +167,6 @@ public class BaseNoticeFacade {
148 } 167 }
149 168
150 169
151 -  
152 } 170 }
153 171
154 protected String buildParams(Object... objects) { 172 protected String buildParams(Object... objects) {
@@ -172,7 +190,7 @@ public class BaseNoticeFacade { @@ -172,7 +190,7 @@ public class BaseNoticeFacade {
172 return req; 190 return req;
173 } 191 }
174 192
175 - protected InboxReqVO buildInboxContent(InboxBusinessTypeEnum ibt,Object... params) { 193 + protected InboxReqVO buildInboxContent(InboxBusinessTypeEnum ibt, Object... params) {
176 InboxReqVO req = new InboxReqVO(); 194 InboxReqVO req = new InboxReqVO();
177 req.setType(ibt.getType()); 195 req.setType(ibt.getType());
178 req.setBusinessType(ibt.getBusinessType()); 196 req.setBusinessType(ibt.getBusinessType());
@@ -184,7 +202,7 @@ public class BaseNoticeFacade { @@ -184,7 +202,7 @@ public class BaseNoticeFacade {
184 return MessageFormatter.arrayFormat(content, params).getMessage(); 202 return MessageFormatter.arrayFormat(content, params).getMessage();
185 } 203 }
186 204
187 - protected String buildSmsContent(InboxBusinessTypeEnum ibte, Object... params){ 205 + protected String buildSmsContent(InboxBusinessTypeEnum ibte, Object... params) {
188 return getReplacedContent(ibte.getContent(), params); 206 return getReplacedContent(ibte.getContent(), params);
189 } 207 }
190 208
@@ -35,6 +35,7 @@ public class BuyerNoticeFacade extends BaseNoticeFacade { @@ -35,6 +35,7 @@ public class BuyerNoticeFacade extends BaseNoticeFacade {
35 String sizeName = psog.getSizeName(); 35 String sizeName = psog.getSizeName();
36 String productCode = Optional.ofNullable(product).map(Product::getProductCode).orElse(""); 36 String productCode = Optional.ofNullable(product).map(Product::getProductCode).orElse("");
37 newNotice(buyerUid) 37 newNotice(buyerUid)
  38 + .withLogPrefix("notice buyer delivery deposit goods to buyer")
38 .withInBox(InboxBusinessTypeEnum.NOTICE_BUYER_WHEN_DEPOSIT_GOODS_PURCHASE_SENDED, prdName, sizeName, productCode) 39 .withInBox(InboxBusinessTypeEnum.NOTICE_BUYER_WHEN_DEPOSIT_GOODS_PURCHASE_SENDED, prdName, sizeName, productCode)
39 .withSms(InboxBusinessTypeEnum.SMS_NOTICE_BUYER_WHEN_DEPOSIT_GOODS_PURCHASE_SENDED, prdName, sizeName, productCode, wayBillCode) 40 .withSms(InboxBusinessTypeEnum.SMS_NOTICE_BUYER_WHEN_DEPOSIT_GOODS_PURCHASE_SENDED, prdName, sizeName, productCode, wayBillCode)
40 .send(); 41 .send();
@@ -59,6 +60,7 @@ public class BuyerNoticeFacade extends BaseNoticeFacade { @@ -59,6 +60,7 @@ public class BuyerNoticeFacade extends BaseNoticeFacade {
59 String productCode = Optional.ofNullable(product).map(Product::getProductCode).orElse(""); 60 String productCode = Optional.ofNullable(product).map(Product::getProductCode).orElse("");
60 61
61 newNotice(buyerUid) 62 newNotice(buyerUid)
  63 + .withLogPrefix("notice buyer delivery goods to buyer")
62 .withInBox(InboxBusinessTypeEnum.PURCHASE_SENDED, orderCode) 64 .withInBox(InboxBusinessTypeEnum.PURCHASE_SENDED, orderCode)
63 .withSms(InboxBusinessTypeEnum.SMS_SEND, skupTypeText, prdName, sizeName, productCode, orderCode) 65 .withSms(InboxBusinessTypeEnum.SMS_SEND, skupTypeText, prdName, sizeName, productCode, orderCode)
64 .withPush(() -> ufoSendService.platformDeliverBuyer(String.valueOf(buyerUid), String.valueOf(orderCode))) 66 .withPush(() -> ufoSendService.platformDeliverBuyer(String.valueOf(buyerUid), String.valueOf(orderCode)))
@@ -73,6 +73,7 @@ public class InBoxFacade extends BaseNoticeFacade{ @@ -73,6 +73,7 @@ public class InBoxFacade extends BaseNoticeFacade{
73 public void buyerOrderNotPayed(int buyerUid, long orderCode) { 73 public void buyerOrderNotPayed(int buyerUid, long orderCode) {
74 logger.info("notice buyer not paid, buyerUid {}, orderCode {}", buyerUid, orderCode); 74 logger.info("notice buyer not paid, buyerUid {}, orderCode {}", buyerUid, orderCode);
75 newNotice(buyerUid) 75 newNotice(buyerUid)
  76 + .withLogPrefix("notice buyer not paid")
76 .withInBox(InboxBusinessTypeEnum.PURCHASE_UNPAID, orderCode) 77 .withInBox(InboxBusinessTypeEnum.PURCHASE_UNPAID, orderCode)
77 .withPush(() -> ufoSendService.buyerNotPay(String.valueOf(buyerUid), String.valueOf(orderCode))) 78 .withPush(() -> ufoSendService.buyerNotPay(String.valueOf(buyerUid), String.valueOf(orderCode)))
78 .send(); 79 .send();
@@ -88,6 +89,7 @@ public class InBoxFacade extends BaseNoticeFacade{ @@ -88,6 +89,7 @@ public class InBoxFacade extends BaseNoticeFacade{
88 logger.info("notice buyer finish paid, buyerUid {}, orderCode {}", buyerUid, orderCode); 89 logger.info("notice buyer finish paid, buyerUid {}, orderCode {}", buyerUid, orderCode);
89 String timelimitDesc = new StringBuilder().append(deliveryHours).append(unit).toString(); 90 String timelimitDesc = new StringBuilder().append(deliveryHours).append(unit).toString();
90 newNotice(buyerUid) 91 newNotice(buyerUid)
  92 + .withLogPrefix("notice buyer finish paid")
91 .withInBox(InboxBusinessTypeEnum.PURCHASE_NOTIFIED_SELLER, orderCode, timelimitDesc) 93 .withInBox(InboxBusinessTypeEnum.PURCHASE_NOTIFIED_SELLER, orderCode, timelimitDesc)
92 .send(); 94 .send();
93 } 95 }
@@ -103,6 +105,7 @@ public class InBoxFacade extends BaseNoticeFacade{ @@ -103,6 +105,7 @@ public class InBoxFacade extends BaseNoticeFacade{
103 try { 105 try {
104 logger.info("notice buyer seller deliver 2 depot, buyerUid {}, orderCode {}", buyerUid, orderCode); 106 logger.info("notice buyer seller deliver 2 depot, buyerUid {}, orderCode {}", buyerUid, orderCode);
105 newNotice(buyerUid) 107 newNotice(buyerUid)
  108 + .withLogPrefix("notice buyer seller deliver 2 depot")
106 .withInBox(InboxBusinessTypeEnum.NOTICE_BUYER_WHEN_SELLER_DELIVER, orderCode) 109 .withInBox(InboxBusinessTypeEnum.NOTICE_BUYER_WHEN_SELLER_DELIVER, orderCode)
107 .withPush(() -> ufoSendService.sellerDeliverNotice(String.valueOf(buyerUid), String.valueOf(orderCode))) 110 .withPush(() -> ufoSendService.sellerDeliverNotice(String.valueOf(buyerUid), String.valueOf(orderCode)))
108 .withSms(() -> { 111 .withSms(() -> {
@@ -128,6 +131,7 @@ public class InBoxFacade extends BaseNoticeFacade{ @@ -128,6 +131,7 @@ public class InBoxFacade extends BaseNoticeFacade{
128 try { 131 try {
129 logger.info("notice buyer appraise pass, buyerUid {}, orderCode {}", buyerUid, orderCode); 132 logger.info("notice buyer appraise pass, buyerUid {}, orderCode {}", buyerUid, orderCode);
130 newNotice(buyerUid) 133 newNotice(buyerUid)
  134 + .withLogPrefix("notice buyer appraise pass")
131 .withInBox(InboxBusinessTypeEnum.PURCHASE_SENDED, orderCode) 135 .withInBox(InboxBusinessTypeEnum.PURCHASE_SENDED, orderCode)
132 .withSms(() -> { 136 .withSms(() -> {
133 Product product = productMapper.selectByPrimaryKey(psog.getProductId()); 137 Product product = productMapper.selectByPrimaryKey(psog.getProductId());
@@ -147,6 +151,7 @@ public class InBoxFacade extends BaseNoticeFacade{ @@ -147,6 +151,7 @@ public class InBoxFacade extends BaseNoticeFacade{
147 try { 151 try {
148 logger.info("notice seller appraise pass, buyerUid {}, orderCode {}", buyerUid, orderCode); 152 logger.info("notice seller appraise pass, buyerUid {}, orderCode {}", buyerUid, orderCode);
149 newNotice(psog.getUid()) 153 newNotice(psog.getUid())
  154 + .withLogPrefix("notice seller appraise pass")
150 .withInBox(() -> { 155 .withInBox(() -> {
151 String prdName = psog.getProductName(); 156 String prdName = psog.getProductName();
152 Product product = productMapper.selectByPrimaryKey(psog.getProductId()); 157 Product product = productMapper.selectByPrimaryKey(psog.getProductId());