From 9d9b5d252711d3689c7c20b256531aba99fc55db Mon Sep 17 00:00:00 2001 From: wangning <164851225@qq.com> Date: Thu, 18 Jan 2018 15:26:43 +0800 Subject: [PATCH 1/5] customer impression --- src/db/modify.sql | 4 +- .../apps/core/CustomerImpressionService.java | 4 +- .../impls/CustomerImpressionServiceImpl.java | 178 ++++++++++++------ .../listeners/AfterPaymentFinishListener.java | 28 +++ .../manage/apps/events/package-info.java | 4 + .../web/CustomerImpressionController.java | 6 - .../mappers/client/ClientCustomersMapper.java | 2 + .../manage/task/CustomerImpressionTask.java | 9 +- .../mappers/client/ClientCustomersMapper.xml | 12 ++ .../impls/CustomerImpressionImplTest.java | 53 +++--- 10 files changed, 198 insertions(+), 102 deletions(-) create mode 100644 src/main/java/au/com/royalpay/payment/manage/apps/events/listeners/AfterPaymentFinishListener.java create mode 100644 src/main/java/au/com/royalpay/payment/manage/apps/events/package-info.java diff --git a/src/db/modify.sql b/src/db/modify.sql index b76c689d8..4bbd1d4c2 100644 --- a/src/db/modify.sql +++ b/src/db/modify.sql @@ -206,4 +206,6 @@ VALUES ( 'android_update' , 'true' - ); \ No newline at end of file + ); + +ALTER table sys_clients_customers drop column update_time; \ No newline at end of file diff --git a/src/main/java/au/com/royalpay/payment/manage/apps/core/CustomerImpressionService.java b/src/main/java/au/com/royalpay/payment/manage/apps/core/CustomerImpressionService.java index 2f15ef700..36e3c92f5 100644 --- a/src/main/java/au/com/royalpay/payment/manage/apps/core/CustomerImpressionService.java +++ b/src/main/java/au/com/royalpay/payment/manage/apps/core/CustomerImpressionService.java @@ -13,7 +13,9 @@ public interface CustomerImpressionService { JSONObject findOne(int client_id,String customer_id); - void generate(int client_id); + void generateTag(int client_id); + + void generateInfo(); void modifyNameRemark(int client_id, String customer_id,String name_remark); } diff --git a/src/main/java/au/com/royalpay/payment/manage/apps/core/impls/CustomerImpressionServiceImpl.java b/src/main/java/au/com/royalpay/payment/manage/apps/core/impls/CustomerImpressionServiceImpl.java index a7dcc4f49..47a479195 100644 --- a/src/main/java/au/com/royalpay/payment/manage/apps/core/impls/CustomerImpressionServiceImpl.java +++ b/src/main/java/au/com/royalpay/payment/manage/apps/core/impls/CustomerImpressionServiceImpl.java @@ -1,8 +1,6 @@ package au.com.royalpay.payment.manage.apps.core.impls; -import java.math.BigDecimal; import java.util.ArrayList; -import java.util.Date; import java.util.List; import javax.annotation.Resource; @@ -10,6 +8,10 @@ import javax.annotation.Resource; import org.apache.commons.lang3.StringUtils; import org.apache.commons.lang3.time.DateFormatUtils; import org.joda.time.DateTime; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; +import org.springframework.data.redis.core.BoundListOperations; +import org.springframework.data.redis.core.StringRedisTemplate; import org.springframework.stereotype.Service; import org.springframework.transaction.annotation.Transactional; import org.springframework.util.CollectionUtils; @@ -45,6 +47,9 @@ public class CustomerImpressionServiceImpl implements CustomerImpressionService private CustomerRelationAlipayMapper customerRelationAlipayMapper; @Resource private CustomerMapper customerMapper; + @Resource + private StringRedisTemplate stringRedisTemplate; + Logger logger = LoggerFactory.getLogger(CustomerImpressionServiceImpl.class); @Override public JSONObject listPageble(CustomerImpressionQuery customerImpressionQuery) { @@ -91,66 +96,72 @@ public class CustomerImpressionServiceImpl implements CustomerImpressionService return customer; } + // @Override + // @Transactional + // public void generate(int client_id) { + // JSONObject params = new JSONObject(); + // params.put("client_id", client_id); + // params.put("include_success_status", 3); + // PageList lastRecord = clientCustomersMapper.listCustomerInfo(params, new PageBounds(1, 1, + // Order.formString("update_time.desc"))); + // if (!CollectionUtils.isEmpty(lastRecord)) { + // params.put("confirm_time", lastRecord.get(0).getDate("update_time")); + // } + // List orders = orderMapper.listAnalysisClientCustomer(params); + // Date now = new Date(); + // for (JSONObject order : orders) { + // if (StringUtils.isEmpty(order.getString("channel"))) { + // continue; + // } + // JSONObject userInfo = customerMapper.findCustomerByOpenId(order.getString("customer_id")); + // if (userInfo == null) { + // userInfo = customerRelationAlipayMapper.findCustomerByUserId(order.getString("customer_id")); + // } + // + // JSONObject clientCustomerInfo = clientCustomersMapper.getClientCustomerWithNull(client_id, + // order.getString("customer_id")); + // if (clientCustomerInfo == null) { + // JSONObject client_customer = new JSONObject(); + // client_customer.put("client_id", order.getIntValue("client_id")); + // client_customer.put("customer_id", order.getString("customer_id")); + // client_customer.put("channel", order.getString("channel")); + // client_customer.put("tag", "不活跃用户"); + // if (userInfo != null) { + // client_customer.put("headimg", userInfo.getString("headimg")); + // client_customer.put("nick_name", userInfo.getString("nickname")); + // } + // client_customer.put("payment_times", order.getIntValue("payment_times")); + // client_customer.put("total_amount", order.getBigDecimal("total_amount")); + // client_customer.put("update_time", now); + // client_customer.put("last_payment_time", order.getDate("confirm_time")); + // clientCustomersMapper.insert(client_customer); + // } else { + // clientCustomerInfo.put("payment_times", clientCustomerInfo.getIntValue("payment_times") + + // order.getIntValue("payment_times")); + // BigDecimal total_amount = clientCustomerInfo.getBigDecimal("total_amount"); + // if (total_amount != null) { + // clientCustomerInfo.put("total_amount", + // clientCustomerInfo.getBigDecimal("total_amount").add(order.getBigDecimal("total_amount"))); + // } else { + // clientCustomerInfo.put("total_amount", order.getBigDecimal("total_amount")); + // } + // if (userInfo != null) { + // clientCustomerInfo.put("headimg", userInfo.getString("headimg")); + // clientCustomerInfo.put("nick_name", userInfo.getString("nickname")); + // } + // clientCustomerInfo.put("channel", order.getString("channel")); + // clientCustomerInfo.put("tag", "不活跃用户"); + // clientCustomerInfo.put("update_time", now); + // clientCustomerInfo.put("last_payment_time", order.getDate("confirm_time")); + // clientCustomersMapper.update(clientCustomerInfo); + // } + // } + // generateTag(client_id); + // } + @Override @Transactional - public void generate(int client_id) { - JSONObject params = new JSONObject(); - params.put("client_id", client_id); - params.put("include_success_status", 3); - PageList lastRecord = clientCustomersMapper.listCustomerInfo(params, new PageBounds(1, 1, Order.formString("update_time.desc"))); - if (!CollectionUtils.isEmpty(lastRecord)) { - params.put("confirm_time", lastRecord.get(0).getDate("update_time")); - } - List orders = orderMapper.listAnalysisClientCustomer(params); - Date now = new Date(); - for (JSONObject order : orders) { - if (StringUtils.isEmpty(order.getString("channel"))) { - continue; - } - JSONObject userInfo = customerMapper.findCustomerByOpenId(order.getString("customer_id")); - if (userInfo == null) { - userInfo = customerRelationAlipayMapper.findCustomerByUserId(order.getString("customer_id")); - } - - JSONObject clientCustomerInfo = clientCustomersMapper.getClientCustomerWithNull(client_id, order.getString("customer_id")); - if (clientCustomerInfo == null) { - JSONObject client_customer = new JSONObject(); - client_customer.put("client_id", order.getIntValue("client_id")); - client_customer.put("customer_id", order.getString("customer_id")); - client_customer.put("channel", order.getString("channel")); - client_customer.put("tag", "不活跃用户"); - if (userInfo != null) { - client_customer.put("headimg", userInfo.getString("headimg")); - client_customer.put("nick_name", userInfo.getString("nickname")); - } - client_customer.put("payment_times", order.getIntValue("payment_times")); - client_customer.put("total_amount", order.getBigDecimal("total_amount")); - client_customer.put("update_time", now); - client_customer.put("last_payment_time", order.getDate("confirm_time")); - clientCustomersMapper.insert(client_customer); - } else { - clientCustomerInfo.put("payment_times", clientCustomerInfo.getIntValue("payment_times") + order.getIntValue("payment_times")); - BigDecimal total_amount = clientCustomerInfo.getBigDecimal("total_amount"); - if (total_amount != null) { - clientCustomerInfo.put("total_amount", clientCustomerInfo.getBigDecimal("total_amount").add(order.getBigDecimal("total_amount"))); - } else { - clientCustomerInfo.put("total_amount", order.getBigDecimal("total_amount")); - } - if (userInfo != null) { - clientCustomerInfo.put("headimg", userInfo.getString("headimg")); - clientCustomerInfo.put("nick_name", userInfo.getString("nickname")); - } - clientCustomerInfo.put("channel", order.getString("channel")); - clientCustomerInfo.put("tag", "不活跃用户"); - clientCustomerInfo.put("update_time", now); - clientCustomerInfo.put("last_payment_time", order.getDate("confirm_time")); - clientCustomersMapper.update(clientCustomerInfo); - } - } - generateTag(client_id); - } - - private void generateTag(int client_id) { + public void generateTag(int client_id) { JSONObject params = new JSONObject(); DateTime dt = new DateTime(); dt = dt.minusMonths(5); @@ -176,6 +187,53 @@ public class CustomerImpressionServiceImpl implements CustomerImpressionService } + @Override + public void generateInfo() { + BoundListOperations ops = stringRedisTemplate.boundListOps("customer_impression"); + String redisValue = ops.leftPop(); + + while (!StringUtils.isEmpty(redisValue)) { + try { + JSONObject order = JSONObject.parseObject(redisValue); + redisValue = ops.leftPop(); + if (StringUtils.isEmpty(order.getString("channel"))) { + continue; + } + JSONObject userInfo = customerMapper.findCustomerByOpenId(order.getString("customer_id")); + if (userInfo == null) { + userInfo = customerRelationAlipayMapper.findCustomerByUserId(order.getString("customer_id")); + } + JSONObject clientCustomerInfo = clientCustomersMapper.getClientCustomerWithNull(order.getIntValue("client_id"), order.getString("customer_id")); + if (clientCustomerInfo == null) { + JSONObject client_customer = new JSONObject(); + client_customer.put("client_id", order.getIntValue("client_id")); + client_customer.put("customer_id", order.getString("customer_id")); + client_customer.put("channel", order.getString("channel")); + client_customer.put("tag", "不活跃用户"); + if (userInfo != null) { + client_customer.put("headimg", userInfo.getString("headimg")); + client_customer.put("nick_name", userInfo.getString("nickname")); + } + client_customer.put("payment_times", 1); + client_customer.put("total_amount", order.getBigDecimal("total_amount")); + client_customer.put("last_payment_time", order.getDate("confirm_time")); + clientCustomersMapper.insert(client_customer); + } else { + clientCustomerInfo.put("payment_times", 1); + clientCustomerInfo.put("total_amount", order.getBigDecimal("total_amount")); + if (userInfo != null) { + clientCustomerInfo.put("headimg", userInfo.getString("headimg")); + clientCustomerInfo.put("nick_name", userInfo.getString("nickname")); + } + clientCustomerInfo.put("last_payment_time", order.getDate("confirm_time")); + clientCustomersMapper.updateAfterPaymentFinish(clientCustomerInfo); + } + } catch (Exception e) { + logger.debug("Reduce Customer Impression Error Redis Value =" + redisValue, e); + } + } + } + @Override public void modifyNameRemark(int client_id, String customer_id, String name_remark) { JSONObject clientCustomerInfo = clientCustomersMapper.getClientCustomer(client_id, customer_id); diff --git a/src/main/java/au/com/royalpay/payment/manage/apps/events/listeners/AfterPaymentFinishListener.java b/src/main/java/au/com/royalpay/payment/manage/apps/events/listeners/AfterPaymentFinishListener.java new file mode 100644 index 000000000..30f21ef73 --- /dev/null +++ b/src/main/java/au/com/royalpay/payment/manage/apps/events/listeners/AfterPaymentFinishListener.java @@ -0,0 +1,28 @@ +package au.com.royalpay.payment.manage.apps.events.listeners; + +import au.com.royalpay.payment.core.events.AfterPaymentFinishEvent; + +import com.alibaba.fastjson.JSONObject; + +import org.springframework.context.ApplicationListener; +import org.springframework.data.redis.core.BoundListOperations; +import org.springframework.data.redis.core.StringRedisTemplate; +import org.springframework.stereotype.Service; + +import javax.annotation.Resource; + +/** + * Created by wangning on 17/01/2018. + */ +@Service +public class AfterPaymentFinishListener implements ApplicationListener { + @Resource + private StringRedisTemplate stringRedisTemplate; + + @Override + public void onApplicationEvent(AfterPaymentFinishEvent event) { + JSONObject order = event.getFinishedEvent().getOrder(); + BoundListOperations ops = stringRedisTemplate.boundListOps("customer_impression"); + ops.rightPush(order.toJSONString()); + } +} \ No newline at end of file diff --git a/src/main/java/au/com/royalpay/payment/manage/apps/events/package-info.java b/src/main/java/au/com/royalpay/payment/manage/apps/events/package-info.java new file mode 100644 index 000000000..99acb1d8e --- /dev/null +++ b/src/main/java/au/com/royalpay/payment/manage/apps/events/package-info.java @@ -0,0 +1,4 @@ +/** + * Created by wangning on 17/01/2018. + */ +package au.com.royalpay.payment.manage.apps.events; \ No newline at end of file diff --git a/src/main/java/au/com/royalpay/payment/manage/apps/web/CustomerImpressionController.java b/src/main/java/au/com/royalpay/payment/manage/apps/web/CustomerImpressionController.java index d19dbe8b6..24d062285 100644 --- a/src/main/java/au/com/royalpay/payment/manage/apps/web/CustomerImpressionController.java +++ b/src/main/java/au/com/royalpay/payment/manage/apps/web/CustomerImpressionController.java @@ -41,10 +41,4 @@ public class CustomerImpressionController { @ModelAttribute(CommonConsts.RETAIL_DEVICE) JSONObject app) { customerImpressionService.modifyNameRemark(app.getIntValue("client_id"), customer_id, customerInfo.getString("name_remark")); } - - @RequestMapping(value = "/gengrate", method = RequestMethod.PUT) - @ResponseBody - public void generate(@ModelAttribute(CommonConsts.RETAIL_DEVICE) JSONObject app) { - customerImpressionService.generate(app.getIntValue("client_id")); - } } diff --git a/src/main/java/au/com/royalpay/payment/manage/mappers/client/ClientCustomersMapper.java b/src/main/java/au/com/royalpay/payment/manage/mappers/client/ClientCustomersMapper.java index 52257fdfe..b15607431 100644 --- a/src/main/java/au/com/royalpay/payment/manage/mappers/client/ClientCustomersMapper.java +++ b/src/main/java/au/com/royalpay/payment/manage/mappers/client/ClientCustomersMapper.java @@ -26,6 +26,8 @@ public interface ClientCustomersMapper { @AutoSql(type = SqlType.UPDATE) public void update(JSONObject obj); + public void updateAfterPaymentFinish(JSONObject obj); + PageList listCustomerInfo(JSONObject params, PageBounds pageBounds); } diff --git a/src/main/java/au/com/royalpay/payment/manage/task/CustomerImpressionTask.java b/src/main/java/au/com/royalpay/payment/manage/task/CustomerImpressionTask.java index d54ccbf23..ec3dd6246 100644 --- a/src/main/java/au/com/royalpay/payment/manage/task/CustomerImpressionTask.java +++ b/src/main/java/au/com/royalpay/payment/manage/task/CustomerImpressionTask.java @@ -31,7 +31,7 @@ public class CustomerImpressionTask { private ThreadPoolExecutor generatePool = new ThreadPoolExecutor(10, 30, 5, TimeUnit.SECONDS, new LinkedBlockingQueue()); @Scheduled(cron = "0 0 3 * * ?") - public void generate() { + public void generateTags() { List clients = clientMapper.listValidClient(); List> splitList = new ArrayList<>(); for (int i = 0; i < clients.size(); i+=200) { @@ -45,9 +45,14 @@ public class CustomerImpressionTask { for (List splitClients : splitList) { Runnable task = () -> splitClients.forEach((p)->{ System.out.println("当前执行到"+p.getIntValue("client_id")); - customerImpressionService.generate(p.getIntValue("client_id")); + customerImpressionService.generateTag(p.getIntValue("client_id")); }); generatePool.execute(task); } } + + @Scheduled(cron = "0/1 * * * * ?") + public void generateInfo() { + customerImpressionService.generateInfo(); + } } diff --git a/src/main/resources/au/com/royalpay/payment/manage/mappers/client/ClientCustomersMapper.xml b/src/main/resources/au/com/royalpay/payment/manage/mappers/client/ClientCustomersMapper.xml index 780110af8..ae1d2d552 100644 --- a/src/main/resources/au/com/royalpay/payment/manage/mappers/client/ClientCustomersMapper.xml +++ b/src/main/resources/au/com/royalpay/payment/manage/mappers/client/ClientCustomersMapper.xml @@ -31,4 +31,16 @@ and customer_id = #{customer_id} + + + update sys_clients_customers + set total_amount = total_amount + #{total_amount}, + payment_times = payment_times + #{payment_times}, + last_payment_time = #{last_payment_time}, + nick_name = #{nick_name}, + headimg = #{headimg} + where client_id= #{client_id} + and customer_id = #{customer_id} + + \ No newline at end of file diff --git a/src/test/java/au/com/royalpay/payment/manage/apps/core/impls/CustomerImpressionImplTest.java b/src/test/java/au/com/royalpay/payment/manage/apps/core/impls/CustomerImpressionImplTest.java index df64a02c2..d62d26edf 100644 --- a/src/test/java/au/com/royalpay/payment/manage/apps/core/impls/CustomerImpressionImplTest.java +++ b/src/test/java/au/com/royalpay/payment/manage/apps/core/impls/CustomerImpressionImplTest.java @@ -11,58 +11,47 @@ import javax.annotation.Resource; import org.junit.Test; import org.junit.runner.RunWith; import org.springframework.boot.test.context.SpringBootTest; +import org.springframework.data.redis.core.BoundListOperations; +import org.springframework.data.redis.core.StringRedisTemplate; import org.springframework.test.context.ActiveProfiles; import org.springframework.test.context.junit4.SpringRunner; +import org.springframework.util.CollectionUtils; import com.alibaba.fastjson.JSONObject; import au.com.royalpay.payment.manage.apps.core.CustomerImpressionService; +import au.com.royalpay.payment.manage.mappers.payment.OrderMapper; import au.com.royalpay.payment.manage.mappers.system.ClientMapper; +import au.com.royalpay.payment.manage.mappers.system.CustomerRelationAlipayMapper; /** * Created by wangning on 05/01/2018. */ @SpringBootTest -@ActiveProfiles({"production","alipay","wechat","jd","bestpay"}) +@ActiveProfiles({ "local", "alipay", "wechat", "jd", "bestpay" }) @RunWith(SpringRunner.class) public class CustomerImpressionImplTest { @Resource - private CustomerImpressionService customerImpression; - @Resource - private ClientMapper clientMapper; - - private ThreadPoolExecutor generatePool = new ThreadPoolExecutor(10, 30, 5, TimeUnit.SECONDS, new LinkedBlockingQueue()); + private OrderMapper orderMapper; - @Test - public void generate() throws Exception { - customerImpression.generate(9); - } + @Resource + private StringRedisTemplate stringRedisTemplate; @Test - public void generateAll(){ - List clients = clientMapper.listValidClient(); - List> splitList = new ArrayList<>(); - for (int i = 0; i < clients.size(); i+=200) { - if(i+200>clients.size()){ - splitList.add(clients.subList(i,clients.size())); - }else { - splitList.add(clients.subList(i,i+200)); + public void redisQueue() { + BoundListOperations ops = stringRedisTemplate.boundListOps("customer_impression"); + JSONObject order = orderMapper.find("00009201711300930013961422"); + for (int i = 0; i < 10000; i++) { + synchronized (lock) { + ops.rightPush(order.toJSONString()); } } - for (List splitClients : splitList) { - Runnable task = () -> splitClients.forEach((p)->{ - System.out.println("当前执行到"+p.getIntValue("client_id")); - customerImpression.generate(p.getIntValue("client_id")); - }); - generatePool.execute(task); - } - generatePool.shutdown(); - try { - generatePool.awaitTermination(5, TimeUnit.HOURS); - } catch (InterruptedException e) { - e.printStackTrace(); - } + System.out.println("asdsada"); + System.out.println("asdsada"); + System.out.println("asdsada"); + System.out.println("asdsada"); + System.out.println("asdsada"); + System.out.println("asdsada"); } - } \ No newline at end of file From 9c567a6e985c08fc15dc2088f10c10cd52b8f855 Mon Sep 17 00:00:00 2001 From: wangning <164851225@qq.com> Date: Thu, 18 Jan 2018 15:27:53 +0800 Subject: [PATCH 2/5] update --- .../impls/CustomerImpressionImplTest.java | 20 +------------------ 1 file changed, 1 insertion(+), 19 deletions(-) diff --git a/src/test/java/au/com/royalpay/payment/manage/apps/core/impls/CustomerImpressionImplTest.java b/src/test/java/au/com/royalpay/payment/manage/apps/core/impls/CustomerImpressionImplTest.java index d62d26edf..d40bbe97c 100644 --- a/src/test/java/au/com/royalpay/payment/manage/apps/core/impls/CustomerImpressionImplTest.java +++ b/src/test/java/au/com/royalpay/payment/manage/apps/core/impls/CustomerImpressionImplTest.java @@ -1,11 +1,5 @@ package au.com.royalpay.payment.manage.apps.core.impls; -import java.util.ArrayList; -import java.util.List; -import java.util.concurrent.LinkedBlockingQueue; -import java.util.concurrent.ThreadPoolExecutor; -import java.util.concurrent.TimeUnit; - import javax.annotation.Resource; import org.junit.Test; @@ -15,14 +9,10 @@ import org.springframework.data.redis.core.BoundListOperations; import org.springframework.data.redis.core.StringRedisTemplate; import org.springframework.test.context.ActiveProfiles; import org.springframework.test.context.junit4.SpringRunner; -import org.springframework.util.CollectionUtils; import com.alibaba.fastjson.JSONObject; -import au.com.royalpay.payment.manage.apps.core.CustomerImpressionService; import au.com.royalpay.payment.manage.mappers.payment.OrderMapper; -import au.com.royalpay.payment.manage.mappers.system.ClientMapper; -import au.com.royalpay.payment.manage.mappers.system.CustomerRelationAlipayMapper; /** * Created by wangning on 05/01/2018. @@ -42,16 +32,8 @@ public class CustomerImpressionImplTest { BoundListOperations ops = stringRedisTemplate.boundListOps("customer_impression"); JSONObject order = orderMapper.find("00009201711300930013961422"); for (int i = 0; i < 10000; i++) { - synchronized (lock) { - ops.rightPush(order.toJSONString()); - } + ops.rightPush(order.toJSONString()); } - System.out.println("asdsada"); - System.out.println("asdsada"); - System.out.println("asdsada"); - System.out.println("asdsada"); - System.out.println("asdsada"); - System.out.println("asdsada"); } } \ No newline at end of file From 28537f578380614de304c3d2bac70f9672978756 Mon Sep 17 00:00:00 2001 From: wangning <164851225@qq.com> Date: Thu, 18 Jan 2018 17:02:38 +0800 Subject: [PATCH 3/5] tmp --- .../apps/core/CustomerImpressionService.java | 1 + .../impls/CustomerImpressionServiceImpl.java | 127 +++++++++--------- .../manage/task/CustomerImpressionTask.java | 21 +++ 3 files changed, 86 insertions(+), 63 deletions(-) diff --git a/src/main/java/au/com/royalpay/payment/manage/apps/core/CustomerImpressionService.java b/src/main/java/au/com/royalpay/payment/manage/apps/core/CustomerImpressionService.java index 36e3c92f5..5ea8c6f61 100644 --- a/src/main/java/au/com/royalpay/payment/manage/apps/core/CustomerImpressionService.java +++ b/src/main/java/au/com/royalpay/payment/manage/apps/core/CustomerImpressionService.java @@ -14,6 +14,7 @@ public interface CustomerImpressionService { JSONObject findOne(int client_id,String customer_id); void generateTag(int client_id); + void generate(int client_id); void generateInfo(); diff --git a/src/main/java/au/com/royalpay/payment/manage/apps/core/impls/CustomerImpressionServiceImpl.java b/src/main/java/au/com/royalpay/payment/manage/apps/core/impls/CustomerImpressionServiceImpl.java index 47a479195..b56bef18c 100644 --- a/src/main/java/au/com/royalpay/payment/manage/apps/core/impls/CustomerImpressionServiceImpl.java +++ b/src/main/java/au/com/royalpay/payment/manage/apps/core/impls/CustomerImpressionServiceImpl.java @@ -1,6 +1,8 @@ package au.com.royalpay.payment.manage.apps.core.impls; +import java.math.BigDecimal; import java.util.ArrayList; +import java.util.Date; import java.util.List; import javax.annotation.Resource; @@ -96,68 +98,62 @@ public class CustomerImpressionServiceImpl implements CustomerImpressionService return customer; } - // @Override - // @Transactional - // public void generate(int client_id) { - // JSONObject params = new JSONObject(); - // params.put("client_id", client_id); - // params.put("include_success_status", 3); - // PageList lastRecord = clientCustomersMapper.listCustomerInfo(params, new PageBounds(1, 1, - // Order.formString("update_time.desc"))); - // if (!CollectionUtils.isEmpty(lastRecord)) { - // params.put("confirm_time", lastRecord.get(0).getDate("update_time")); - // } - // List orders = orderMapper.listAnalysisClientCustomer(params); - // Date now = new Date(); - // for (JSONObject order : orders) { - // if (StringUtils.isEmpty(order.getString("channel"))) { - // continue; - // } - // JSONObject userInfo = customerMapper.findCustomerByOpenId(order.getString("customer_id")); - // if (userInfo == null) { - // userInfo = customerRelationAlipayMapper.findCustomerByUserId(order.getString("customer_id")); - // } - // - // JSONObject clientCustomerInfo = clientCustomersMapper.getClientCustomerWithNull(client_id, - // order.getString("customer_id")); - // if (clientCustomerInfo == null) { - // JSONObject client_customer = new JSONObject(); - // client_customer.put("client_id", order.getIntValue("client_id")); - // client_customer.put("customer_id", order.getString("customer_id")); - // client_customer.put("channel", order.getString("channel")); - // client_customer.put("tag", "不活跃用户"); - // if (userInfo != null) { - // client_customer.put("headimg", userInfo.getString("headimg")); - // client_customer.put("nick_name", userInfo.getString("nickname")); - // } - // client_customer.put("payment_times", order.getIntValue("payment_times")); - // client_customer.put("total_amount", order.getBigDecimal("total_amount")); - // client_customer.put("update_time", now); - // client_customer.put("last_payment_time", order.getDate("confirm_time")); - // clientCustomersMapper.insert(client_customer); - // } else { - // clientCustomerInfo.put("payment_times", clientCustomerInfo.getIntValue("payment_times") + - // order.getIntValue("payment_times")); - // BigDecimal total_amount = clientCustomerInfo.getBigDecimal("total_amount"); - // if (total_amount != null) { - // clientCustomerInfo.put("total_amount", - // clientCustomerInfo.getBigDecimal("total_amount").add(order.getBigDecimal("total_amount"))); - // } else { - // clientCustomerInfo.put("total_amount", order.getBigDecimal("total_amount")); - // } - // if (userInfo != null) { - // clientCustomerInfo.put("headimg", userInfo.getString("headimg")); - // clientCustomerInfo.put("nick_name", userInfo.getString("nickname")); - // } - // clientCustomerInfo.put("channel", order.getString("channel")); - // clientCustomerInfo.put("tag", "不活跃用户"); - // clientCustomerInfo.put("update_time", now); - // clientCustomerInfo.put("last_payment_time", order.getDate("confirm_time")); - // clientCustomersMapper.update(clientCustomerInfo); - // } - // } - // generateTag(client_id); - // } + @Override + public void generate(int client_id) { + JSONObject params = new JSONObject(); + params.put("client_id", client_id); + params.put("include_success_status", 3); + PageList lastRecord = clientCustomersMapper.listCustomerInfo(params, new PageBounds(1, 1, Order.formString("update_time.desc"))); + if (!CollectionUtils.isEmpty(lastRecord)) { + params.put("confirm_time", lastRecord.get(0).getDate("update_time")); + } + List orders = orderMapper.listAnalysisClientCustomer(params); + Date now = new Date(); + for (JSONObject order : orders) { + if (StringUtils.isEmpty(order.getString("channel"))) { + continue; + } + JSONObject userInfo = customerMapper.findCustomerByOpenId(order.getString("customer_id")); + if (userInfo == null) { + userInfo = customerRelationAlipayMapper.findCustomerByUserId(order.getString("customer_id")); + } + + JSONObject clientCustomerInfo = clientCustomersMapper.getClientCustomerWithNull(client_id, order.getString("customer_id")); + if (clientCustomerInfo == null) { + JSONObject client_customer = new JSONObject(); + client_customer.put("client_id", order.getIntValue("client_id")); + client_customer.put("customer_id", order.getString("customer_id")); + client_customer.put("channel", order.getString("channel")); + client_customer.put("tag", "不活跃用户"); + if (userInfo != null) { + client_customer.put("headimg", userInfo.getString("headimg")); + client_customer.put("nick_name", userInfo.getString("nickname")); + } + client_customer.put("payment_times", order.getIntValue("payment_times")); + client_customer.put("total_amount", order.getBigDecimal("total_amount")); + client_customer.put("update_time", now); + client_customer.put("last_payment_time", order.getDate("confirm_time")); + clientCustomersMapper.insert(client_customer); + } else { + clientCustomerInfo.put("payment_times", clientCustomerInfo.getIntValue("payment_times") + order.getIntValue("payment_times")); + BigDecimal total_amount = clientCustomerInfo.getBigDecimal("total_amount"); + if (total_amount != null) { + clientCustomerInfo.put("total_amount", clientCustomerInfo.getBigDecimal("total_amount").add(order.getBigDecimal("total_amount"))); + } else { + clientCustomerInfo.put("total_amount", order.getBigDecimal("total_amount")); + } + if (userInfo != null) { + clientCustomerInfo.put("headimg", userInfo.getString("headimg")); + clientCustomerInfo.put("nick_name", userInfo.getString("nickname")); + } + clientCustomerInfo.put("channel", order.getString("channel")); + clientCustomerInfo.put("tag", "不活跃用户"); + clientCustomerInfo.put("update_time", now); + clientCustomerInfo.put("last_payment_time", order.getDate("confirm_time")); + clientCustomersMapper.update(clientCustomerInfo); + } + } + } @Override @Transactional @@ -189,6 +185,11 @@ public class CustomerImpressionServiceImpl implements CustomerImpressionService @Override public void generateInfo() { + Date now = new Date(); + DateTime dt = new DateTime(2018,1,19,3,0); + if(dt.toDate().compareTo(now)>0){ + return; + } BoundListOperations ops = stringRedisTemplate.boundListOps("customer_impression"); String redisValue = ops.leftPop(); @@ -197,7 +198,7 @@ public class CustomerImpressionServiceImpl implements CustomerImpressionService JSONObject order = JSONObject.parseObject(redisValue); redisValue = ops.leftPop(); if (StringUtils.isEmpty(order.getString("channel"))) { - continue; + continue; } JSONObject userInfo = customerMapper.findCustomerByOpenId(order.getString("customer_id")); if (userInfo == null) { diff --git a/src/main/java/au/com/royalpay/payment/manage/task/CustomerImpressionTask.java b/src/main/java/au/com/royalpay/payment/manage/task/CustomerImpressionTask.java index ec3dd6246..a562dd076 100644 --- a/src/main/java/au/com/royalpay/payment/manage/task/CustomerImpressionTask.java +++ b/src/main/java/au/com/royalpay/payment/manage/task/CustomerImpressionTask.java @@ -51,6 +51,27 @@ public class CustomerImpressionTask { } } + @Scheduled(cron = "0 0 3 * * ?") + public void generate() { + List clients = clientMapper.listValidClient(); + List> splitList = new ArrayList<>(); + for (int i = 0; i < clients.size(); i+=200) { + if(i+200>clients.size()){ + splitList.add(clients.subList(i,clients.size())); + }else { + splitList.add(clients.subList(i,i+200)); + } + + } + for (List splitClients : splitList) { + Runnable task = () -> splitClients.forEach((p)->{ + System.out.println("当前执行到"+p.getIntValue("client_id")); + customerImpressionService.generate(p.getIntValue("client_id")); + }); + generatePool.execute(task); + } + } + @Scheduled(cron = "0/1 * * * * ?") public void generateInfo() { customerImpressionService.generateInfo(); From 58aba0f91be48dfec61ccf60b33d3e3bb31464e0 Mon Sep 17 00:00:00 2001 From: wangning <164851225@qq.com> Date: Thu, 18 Jan 2018 18:06:35 +0800 Subject: [PATCH 4/5] update --- .../impls/CustomerImpressionServiceImpl.java | 110 ++++++++++-------- 1 file changed, 64 insertions(+), 46 deletions(-) diff --git a/src/main/java/au/com/royalpay/payment/manage/apps/core/impls/CustomerImpressionServiceImpl.java b/src/main/java/au/com/royalpay/payment/manage/apps/core/impls/CustomerImpressionServiceImpl.java index b56bef18c..9767623a3 100644 --- a/src/main/java/au/com/royalpay/payment/manage/apps/core/impls/CustomerImpressionServiceImpl.java +++ b/src/main/java/au/com/royalpay/payment/manage/apps/core/impls/CustomerImpressionServiceImpl.java @@ -32,6 +32,8 @@ import au.com.royalpay.payment.manage.mappers.system.CustomerMapper; import au.com.royalpay.payment.manage.mappers.system.CustomerRelationAlipayMapper; import au.com.royalpay.payment.tools.exceptions.BadRequestException; import au.com.royalpay.payment.tools.exceptions.NotFoundException; +import au.com.royalpay.payment.tools.lock.Locker; +import au.com.royalpay.payment.tools.lock.LockerRedisImpl; import au.com.royalpay.payment.tools.utils.PageListUtils; /** @@ -50,7 +52,11 @@ public class CustomerImpressionServiceImpl implements CustomerImpressionService @Resource private CustomerMapper customerMapper; @Resource + private Locker locker; + @Resource private StringRedisTemplate stringRedisTemplate; + + private static String CUSTOMER_IMPRESSION_PREFIX = "Customer_impression_insert"; Logger logger = LoggerFactory.getLogger(CustomerImpressionServiceImpl.class); @Override @@ -100,6 +106,11 @@ public class CustomerImpressionServiceImpl implements CustomerImpressionService @Override public void generate(int client_id) { + Date now = new Date(); + DateTime dt = new DateTime(2018, 1, 20, 0, 0); + if (now.compareTo(dt.toDate()) > 0) { + return; + } JSONObject params = new JSONObject(); params.put("client_id", client_id); params.put("include_success_status", 3); @@ -108,49 +119,49 @@ public class CustomerImpressionServiceImpl implements CustomerImpressionService params.put("confirm_time", lastRecord.get(0).getDate("update_time")); } List orders = orderMapper.listAnalysisClientCustomer(params); - Date now = new Date(); for (JSONObject order : orders) { if (StringUtils.isEmpty(order.getString("channel"))) { continue; } - JSONObject userInfo = customerMapper.findCustomerByOpenId(order.getString("customer_id")); + String customer_id = order.getString("customer_id"); + JSONObject userInfo = customerMapper.findCustomerByOpenId(customer_id); if (userInfo == null) { - userInfo = customerRelationAlipayMapper.findCustomerByUserId(order.getString("customer_id")); + userInfo = customerRelationAlipayMapper.findCustomerByUserId(customer_id); } - JSONObject clientCustomerInfo = clientCustomersMapper.getClientCustomerWithNull(client_id, order.getString("customer_id")); + JSONObject clientCustomerInfo = clientCustomersMapper.getClientCustomerWithNull(client_id, customer_id); if (clientCustomerInfo == null) { - JSONObject client_customer = new JSONObject(); - client_customer.put("client_id", order.getIntValue("client_id")); - client_customer.put("customer_id", order.getString("customer_id")); - client_customer.put("channel", order.getString("channel")); - client_customer.put("tag", "不活跃用户"); - if (userInfo != null) { - client_customer.put("headimg", userInfo.getString("headimg")); - client_customer.put("nick_name", userInfo.getString("nickname")); + locker.lock(CUSTOMER_IMPRESSION_PREFIX+customer_id, 30_000, 30_000); + try { + JSONObject client_customer = new JSONObject(); + client_customer.put("client_id", order.getIntValue("client_id")); + client_customer.put("customer_id", order.getString("customer_id")); + client_customer.put("channel", order.getString("channel")); + client_customer.put("tag", "不活跃用户"); + if (userInfo != null) { + client_customer.put("headimg", userInfo.getString("headimg")); + client_customer.put("nick_name", userInfo.getString("nickname")); + } + client_customer.put("payment_times", order.getIntValue("payment_times")); + client_customer.put("total_amount", order.getBigDecimal("total_amount")); + client_customer.put("update_time", now); + client_customer.put("last_payment_time", order.getDate("confirm_time")); + clientCustomersMapper.insert(client_customer); + } finally { + locker.unlock(customer_id); } - client_customer.put("payment_times", order.getIntValue("payment_times")); - client_customer.put("total_amount", order.getBigDecimal("total_amount")); - client_customer.put("update_time", now); - client_customer.put("last_payment_time", order.getDate("confirm_time")); - clientCustomersMapper.insert(client_customer); } else { clientCustomerInfo.put("payment_times", clientCustomerInfo.getIntValue("payment_times") + order.getIntValue("payment_times")); - BigDecimal total_amount = clientCustomerInfo.getBigDecimal("total_amount"); - if (total_amount != null) { - clientCustomerInfo.put("total_amount", clientCustomerInfo.getBigDecimal("total_amount").add(order.getBigDecimal("total_amount"))); - } else { - clientCustomerInfo.put("total_amount", order.getBigDecimal("total_amount")); - } + clientCustomerInfo.put("payment_times", order.getIntValue("payment_times")); + clientCustomerInfo.put("client_id", order.getIntValue("client_id")); + clientCustomerInfo.put("customer_id", order.getString("customer_id")); + clientCustomerInfo.put("total_amount", order.getBigDecimal("total_amount")); if (userInfo != null) { clientCustomerInfo.put("headimg", userInfo.getString("headimg")); clientCustomerInfo.put("nick_name", userInfo.getString("nickname")); } - clientCustomerInfo.put("channel", order.getString("channel")); - clientCustomerInfo.put("tag", "不活跃用户"); - clientCustomerInfo.put("update_time", now); clientCustomerInfo.put("last_payment_time", order.getDate("confirm_time")); - clientCustomersMapper.update(clientCustomerInfo); + clientCustomersMapper.updateAfterPaymentFinish(clientCustomerInfo); } } } @@ -160,7 +171,7 @@ public class CustomerImpressionServiceImpl implements CustomerImpressionService public void generateTag(int client_id) { JSONObject params = new JSONObject(); DateTime dt = new DateTime(); - dt = dt.minusMonths(5); + dt = dt.minusMonths(1); params.put("confirm_time", dt.toDate()); params.put("client_id", client_id); List orders = orderMapper.listAnalysisClientCustomer(params); @@ -186,8 +197,8 @@ public class CustomerImpressionServiceImpl implements CustomerImpressionService @Override public void generateInfo() { Date now = new Date(); - DateTime dt = new DateTime(2018,1,19,3,0); - if(dt.toDate().compareTo(now)>0){ + DateTime dt = new DateTime(2018, 1, 19, 3, 0); + if (dt.toDate().compareTo(now) > 0) { return; } BoundListOperations ops = stringRedisTemplate.boundListOps("customer_impression"); @@ -200,27 +211,35 @@ public class CustomerImpressionServiceImpl implements CustomerImpressionService if (StringUtils.isEmpty(order.getString("channel"))) { continue; } - JSONObject userInfo = customerMapper.findCustomerByOpenId(order.getString("customer_id")); + String customer_id = order.getString("customer_id"); + JSONObject userInfo = customerMapper.findCustomerByOpenId(customer_id); if (userInfo == null) { - userInfo = customerRelationAlipayMapper.findCustomerByUserId(order.getString("customer_id")); + userInfo = customerRelationAlipayMapper.findCustomerByUserId(customer_id); } - JSONObject clientCustomerInfo = clientCustomersMapper.getClientCustomerWithNull(order.getIntValue("client_id"), order.getString("customer_id")); + JSONObject clientCustomerInfo = clientCustomersMapper.getClientCustomerWithNull(order.getIntValue("client_id"), customer_id); if (clientCustomerInfo == null) { - JSONObject client_customer = new JSONObject(); - client_customer.put("client_id", order.getIntValue("client_id")); - client_customer.put("customer_id", order.getString("customer_id")); - client_customer.put("channel", order.getString("channel")); - client_customer.put("tag", "不活跃用户"); - if (userInfo != null) { - client_customer.put("headimg", userInfo.getString("headimg")); - client_customer.put("nick_name", userInfo.getString("nickname")); + locker.lock(CUSTOMER_IMPRESSION_PREFIX+customer_id, 30_000, 30_000); + try { + JSONObject client_customer = new JSONObject(); + client_customer.put("client_id", order.getIntValue("client_id")); + client_customer.put("customer_id", order.getString("customer_id")); + client_customer.put("channel", order.getString("channel")); + client_customer.put("tag", "不活跃用户"); + if (userInfo != null) { + client_customer.put("headimg", userInfo.getString("headimg")); + client_customer.put("nick_name", userInfo.getString("nickname")); + } + client_customer.put("payment_times", 1); + client_customer.put("total_amount", order.getBigDecimal("total_amount")); + client_customer.put("last_payment_time", order.getDate("confirm_time")); + clientCustomersMapper.insert(client_customer); + } finally { + locker.unlock(customer_id); } - client_customer.put("payment_times", 1); - client_customer.put("total_amount", order.getBigDecimal("total_amount")); - client_customer.put("last_payment_time", order.getDate("confirm_time")); - clientCustomersMapper.insert(client_customer); } else { clientCustomerInfo.put("payment_times", 1); + clientCustomerInfo.put("client_id", order.getIntValue("client_id")); + clientCustomerInfo.put("customer_id", order.getString("customer_id")); clientCustomerInfo.put("total_amount", order.getBigDecimal("total_amount")); if (userInfo != null) { clientCustomerInfo.put("headimg", userInfo.getString("headimg")); @@ -244,5 +263,4 @@ public class CustomerImpressionServiceImpl implements CustomerImpressionService clientCustomerInfo.put("name_remak", name_remark); clientCustomersMapper.update(clientCustomerInfo); } - } \ No newline at end of file From 9ddcea613f33e770e85d5e1cf70e247e215057a3 Mon Sep 17 00:00:00 2001 From: wangning <164851225@qq.com> Date: Thu, 18 Jan 2018 18:14:41 +0800 Subject: [PATCH 5/5] update --- src/db/modify.sql | 4 +--- .../manage/apps/core/impls/CustomerImpressionServiceImpl.java | 1 + 2 files changed, 2 insertions(+), 3 deletions(-) diff --git a/src/db/modify.sql b/src/db/modify.sql index 4bbd1d4c2..b76c689d8 100644 --- a/src/db/modify.sql +++ b/src/db/modify.sql @@ -206,6 +206,4 @@ VALUES ( 'android_update' , 'true' - ); - -ALTER table sys_clients_customers drop column update_time; \ No newline at end of file + ); \ No newline at end of file diff --git a/src/main/java/au/com/royalpay/payment/manage/apps/core/impls/CustomerImpressionServiceImpl.java b/src/main/java/au/com/royalpay/payment/manage/apps/core/impls/CustomerImpressionServiceImpl.java index 9767623a3..20aaec0b0 100644 --- a/src/main/java/au/com/royalpay/payment/manage/apps/core/impls/CustomerImpressionServiceImpl.java +++ b/src/main/java/au/com/royalpay/payment/manage/apps/core/impls/CustomerImpressionServiceImpl.java @@ -230,6 +230,7 @@ public class CustomerImpressionServiceImpl implements CustomerImpressionService client_customer.put("nick_name", userInfo.getString("nickname")); } client_customer.put("payment_times", 1); + client_customer.put("update_time", now); client_customer.put("total_amount", order.getBigDecimal("total_amount")); client_customer.put("last_payment_time", order.getDate("confirm_time")); clientCustomersMapper.insert(client_customer);