From 8bf48d053e644bb11125deef45a56c1bf3650014 Mon Sep 17 00:00:00 2001 From: yangkai Date: Wed, 12 Sep 2018 16:30:36 +0800 Subject: [PATCH] =?UTF-8?q?=E6=94=AF=E4=BB=98=E5=AE=8C=E6=88=90=E5=90=8E?= =?UTF-8?q?=E6=B7=BB=E5=8A=A0=E6=8A=BD=E5=A5=96=E6=AC=A1=E6=95=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../CustomerLotteryCountServiceImpl.java | 85 +++++++++++++++++++ 1 file changed, 85 insertions(+) create mode 100644 src/main/java/au/com/royalpay/payment/manage/customers/core/impls/CustomerLotteryCountServiceImpl.java diff --git a/src/main/java/au/com/royalpay/payment/manage/customers/core/impls/CustomerLotteryCountServiceImpl.java b/src/main/java/au/com/royalpay/payment/manage/customers/core/impls/CustomerLotteryCountServiceImpl.java new file mode 100644 index 000000000..dff151e40 --- /dev/null +++ b/src/main/java/au/com/royalpay/payment/manage/customers/core/impls/CustomerLotteryCountServiceImpl.java @@ -0,0 +1,85 @@ +package au.com.royalpay.payment.manage.customers.core.impls; + +import au.com.royalpay.payment.core.events.AfterPaymentFinishEvent; +import au.com.royalpay.payment.manage.mappers.system.CustomerMapper; +import au.com.royalpay.payment.tools.threadpool.RoyalThreadPoolExecutor; +import cn.yixblog.platform.http.HttpRequestGenerator; +import cn.yixblog.platform.http.HttpRequestResult; +import com.alibaba.fastjson.JSONObject; +import org.apache.commons.codec.digest.DigestUtils; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; +import org.springframework.beans.factory.annotation.Value; +import org.springframework.context.ApplicationListener; +import org.springframework.stereotype.Service; +import org.springframework.web.bind.annotation.RequestMethod; +import org.springframework.web.util.UriComponentsBuilder; + +import javax.annotation.Resource; +import java.math.BigDecimal; + +@Service +public class CustomerLotteryCountServiceImpl implements ApplicationListener { + private Logger logger = LoggerFactory.getLogger(getClass()); + + @Value("${app.customer.host}") + private String CUSTOMER_HOST; + + @Value("${customer.app.appid}") + private String CUSTOMER_APP_ID; + + @Value("${customer.app.auth-code}") + private String CUSTOMER_AUTH_CODE; + + @Resource + private RoyalThreadPoolExecutor royalThreadPoolExecutor; + @Resource + private CustomerMapper customerMapper; + + @Override + public void onApplicationEvent(AfterPaymentFinishEvent event) { + final JSONObject order = event.getFinishedEvent().getOrder(); + BigDecimal clearAmount = event.getFinishedEvent().getAudFee(); + String channel = order.getString("channel"); + if ("Wechat".equals(channel)) { + JSONObject customerRelation = customerMapper.findCustomerByOpenId(order.getString("customer_id")); + if (customerRelation.getString("kanga_openid") == null) { + return; + } + royalThreadPoolExecutor.execute(() -> afterPaymentAddLotteryCount(customerRelation.getString("kanga_openid"), + order.getString("order_id"), channel, clearAmount)); + } + if ("Alipay".equals(channel)) { + royalThreadPoolExecutor.execute(() -> afterPaymentAddLotteryCount(order.getString("customer_id"), + order.getString("order_id"), channel, clearAmount)); + } + } + + private void afterPaymentAddLotteryCount(String id, String orderId, String channel, BigDecimal clearAmount) { + String timestamp = System.currentTimeMillis() + ""; + String base = CUSTOMER_APP_ID + timestamp + CUSTOMER_AUTH_CODE; + String sign = DigestUtils.sha256Hex(base).toLowerCase(); + String uri = UriComponentsBuilder.fromHttpUrl(CUSTOMER_HOST + "activity/luck_draw/payment/lottery/count") + .queryParam("appid", CUSTOMER_APP_ID) + .queryParam("timestamp", timestamp) + .queryParam("sign", sign) + .queryParam("id", id) + .queryParam("orderId", orderId) + .queryParam("channel", channel) + .queryParam("amount", clearAmount) + .toUriString(); + HttpRequestGenerator gen = new HttpRequestGenerator(uri, RequestMethod.POST); + try { + HttpRequestResult result = gen.execute(); + if (result.isSuccess()) { + int statusCode = result.getStatusCode(); + if (statusCode == 200) { + logger.info("抽奖次数添加成功"); + } + } + + } catch (Exception ignored) { + ignored.printStackTrace(); + } + } +}