提交 e5c0a9c9 作者: 沈振路

Merge branch 'customer_service_SZlu'

...@@ -224,10 +224,10 @@ ...@@ -224,10 +224,10 @@
<artifactId>spring-cloud-starter-netflix-hystrix</artifactId> <artifactId>spring-cloud-starter-netflix-hystrix</artifactId>
</dependency> </dependency>
<!-- <dependency> <dependency>
<groupId>org.springframework.boot</groupId> <groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-amqp</artifactId> <artifactId>spring-boot-starter-amqp</artifactId>
</dependency>--> </dependency>
<dependency> <dependency>
......
...@@ -8,9 +8,11 @@ import cn.hutool.json.JSONUtil; ...@@ -8,9 +8,11 @@ import cn.hutool.json.JSONUtil;
import com.alibaba.fastjson.JSON; import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject; import com.alibaba.fastjson.JSONObject;
import com.alibaba.fastjson.TypeReference; import com.alibaba.fastjson.TypeReference;
import com.sun.org.apache.xpath.internal.operations.Bool;
import com.yaoyaozw.customer.common.R; import com.yaoyaozw.customer.common.R;
import com.yaoyaozw.customer.constants.ApiResultConstant; import com.yaoyaozw.customer.constants.ApiResultConstant;
import com.yaoyaozw.customer.constants.CustomerCommonConstant; import com.yaoyaozw.customer.constants.CustomerCommonConstant;
import com.yaoyaozw.customer.constants.RabbitCommonNameConstant;
import com.yaoyaozw.customer.entity.*; import com.yaoyaozw.customer.entity.*;
import com.yaoyaozw.customer.enums.CustomerStoreTemplateEnum; import com.yaoyaozw.customer.enums.CustomerStoreTemplateEnum;
import com.yaoyaozw.customer.feigns.ReferralFeignClient; import com.yaoyaozw.customer.feigns.ReferralFeignClient;
...@@ -28,6 +30,8 @@ import com.yaoyaozw.customer.vo.customer.CrowdPackageUserVO; ...@@ -28,6 +30,8 @@ import com.yaoyaozw.customer.vo.customer.CrowdPackageUserVO;
import org.apache.commons.lang3.StringUtils; import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger; import org.slf4j.Logger;
import org.slf4j.LoggerFactory; import org.slf4j.LoggerFactory;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate; import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.scheduling.annotation.Async; import org.springframework.scheduling.annotation.Async;
...@@ -69,6 +73,8 @@ public class CustomerServiceCommonAsyncComponent { ...@@ -69,6 +73,8 @@ public class CustomerServiceCommonAsyncComponent {
private KanbanCommonMapper kanbanCommonMapper; private KanbanCommonMapper kanbanCommonMapper;
@Autowired @Autowired
private CustomerGraphicsDelayMapper customerGraphicsDelayMapper; private CustomerGraphicsDelayMapper customerGraphicsDelayMapper;
@Autowired
private RabbitTemplate rabbitTemplate;
@Async("myExecutor") @Async("myExecutor")
...@@ -542,6 +548,7 @@ public class CustomerServiceCommonAsyncComponent { ...@@ -542,6 +548,7 @@ public class CustomerServiceCommonAsyncComponent {
* 以下是公有方法 * 以下是公有方法
*/ */
public void getCopyReferral(String dateStr, AuthInfoVO authInfoVo, ReferralEntity referralEntity) { public void getCopyReferral(String dateStr, AuthInfoVO authInfoVo, ReferralEntity referralEntity) {
// ygAccessLimit(true, authInfoVo.getStoreType());
referralEntity.setStoreType(authInfoVo.getStoreType()); referralEntity.setStoreType(authInfoVo.getStoreType());
// 非常用链接类型的name需要处理 // 非常用链接类型的name需要处理
String name = referralEntity.getName(); String name = referralEntity.getName();
...@@ -600,7 +607,50 @@ public class CustomerServiceCommonAsyncComponent { ...@@ -600,7 +607,50 @@ public class CustomerServiceCommonAsyncComponent {
return userEntityList; return userEntityList;
} }
private void ygAccessLimit(Boolean checkStoreType, String storeType) {
if (checkStoreType && !CustomerCommonConstant.STORE_NAME_YANG_GUANG.equals(storeType)) {
// 需要校验书城但是不是阳光书城,直接返回
return;
}
boolean go;
synchronized (rabbitTemplate) {
// 从redis查询计数
Object count = redisTemplate.opsForValue().get(CustomerCommonConstant.YANG_GUANG_ACCESS_LIMIT_REDIS_KEY);
if (ObjectUtil.isNull(count)) {
// redis中没有,设置0
redisTemplate.opsForValue().set(CustomerCommonConstant.YANG_GUANG_ACCESS_LIMIT_REDIS_KEY, 0);
count = 0;
}
LOCAL_LOG.info("当前count: {}", count);
long stamp = System.currentTimeMillis();
// 判断计数是否达到上限
go = (Integer)count < 80;
if (go) {
// 没有达到上限,发送消息并且计数加1
rabbitTemplate.convertAndSend(RabbitCommonNameConstant.OPERATE_COMMON_EXCHANGE, RabbitCommonNameConstant.YG_LIMIT_TTL_ROUTE_KEY, stamp);
redisTemplate.opsForValue().increment(CustomerCommonConstant.YANG_GUANG_ACCESS_LIMIT_REDIS_KEY);
}
}
if (!go) {
// 计数达到上限了,休眠30秒,递归调用
try {
LOCAL_LOG.info("达到上限, 休眠15秒");
Thread.sleep(15000);
// 再次调用不需要校验书城
ygAccessLimit(false, "");
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
@RabbitListener(queues = RabbitCommonNameConstant.YG_LIMIT_DEATH_QUEUE)
public void listener() {
redisTemplate.opsForValue().decrement(CustomerCommonConstant.YANG_GUANG_ACCESS_LIMIT_REDIS_KEY);
}
} }
package com.yaoyaozw.customer.configs; package com.yaoyaozw.customer.configs;
import com.yaoyaozw.customer.constants.RabbitCommonNameConstant;
import org.springframework.amqp.core.Binding;
import org.springframework.amqp.core.BindingBuilder;
import org.springframework.amqp.core.DirectExchange;
import org.springframework.amqp.core.Queue;
import org.springframework.amqp.support.converter.Jackson2JsonMessageConverter;
import org.springframework.amqp.support.converter.MessageConverter;
import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration; import org.springframework.context.annotation.Configuration;
...@@ -10,42 +17,48 @@ import java.util.Map; ...@@ -10,42 +17,48 @@ import java.util.Map;
* @author darker * @author darker
* @date 2022/10/21 16:04 * @date 2022/10/21 16:04
*/ */
//@Configuration @Configuration
public class RabbitConfig { public class RabbitConfig {
/* @Bean /* @Bean
public MessageConverter messageConverter(){
return new Jackson2JsonMessageConverter();
}*/
@Bean
public Queue directQueue(){ public Queue directQueue(){
Map<String, Object> map = new HashMap<>(4); Map<String, Object> map = new HashMap<>(4);
map.put("x-dead-letter-exchange", "DEATH_EXCHANGE"); map.put("x-dead-letter-exchange", RabbitCommonNameConstant.DEATH_EXCHANGE);
map.put("x-dead-letter-routing-key", "death"); map.put("x-dead-letter-routing-key", RabbitCommonNameConstant.YG_LIMIT_DEATH_ROUTE_KEY);
map.put("x-message-ttl", 60000); map.put("x-message-ttl", 60000);
return new Queue("YG_ACCESS_LIMIT", true, false, false, map); return new Queue(RabbitCommonNameConstant.YG_LIMIT_TTL_QUEUE, true, false, false, map);
} }
@Bean @Bean
public DirectExchange directExchange(){ public DirectExchange directExchange(){
return new DirectExchange("TTL_EXCHANGE"); return new DirectExchange(RabbitCommonNameConstant.OPERATE_COMMON_EXCHANGE);
} }
@Bean @Bean
public Queue deathQueue(){ public Queue deathQueue(){
return new Queue("DEATH_QUEUE"); return new Queue(RabbitCommonNameConstant.YG_LIMIT_DEATH_QUEUE);
} }
@Bean @Bean
public DirectExchange deathExchange(){ public DirectExchange deathExchange(){
return new DirectExchange("DEATH_EXCHANGE"); return new DirectExchange(RabbitCommonNameConstant.DEATH_EXCHANGE);
} }
@Bean @Bean
public Binding bindingDirect(){ public Binding bindingDirect(){
return BindingBuilder.bind(directQueue()).to(directExchange()).with("yg_limit"); return BindingBuilder.bind(directQueue()).to(directExchange()).with(RabbitCommonNameConstant.YG_LIMIT_TTL_ROUTE_KEY);
} }
@Bean @Bean
public Binding bindingDeath(){ public Binding bindingDeath(){
return BindingBuilder.bind(deathQueue()).to(deathExchange()).with("death"); return BindingBuilder.bind(deathQueue()).to(deathExchange()).with(RabbitCommonNameConstant.YG_LIMIT_DEATH_ROUTE_KEY);
}*/ }
} }
package com.yaoyaozw.customer.constants;
/**
* @author darker
* @date 2022/10/24 14:40
*/
public class RabbitCommonNameConstant {
/**
* 1、运营系统交换机
*/
public static final String OPERATE_COMMON_EXCHANGE = "OPERATE_COMMON_EXCHANGE";
/**
* 阳光访问频率控制
*/
public static final String YG_LIMIT_TTL_QUEUE = "YG_LIMIT_TTL_QUEUE";
public static final String YG_LIMIT_TTL_ROUTE_KEY = "YG_LIMIT";
/**
* 2、死信交换机 - 死信队列 - routeKey
*/
public static final String DEATH_EXCHANGE = "DEATH_EXCHANGE";
/**
* 阳光访问频率死信
*/
public static final String YG_LIMIT_DEATH_QUEUE = "YG_LIMIT_DEATH_QUEUE";
public static final String YG_LIMIT_DEATH_ROUTE_KEY = "YG_LIMIT_DEATH";
}
package com.yaoyaozw.customer.controller; package com.yaoyaozw.customer.controller;
import cn.hutool.core.util.ObjectUtil;
import com.rabbitmq.client.AMQP;
import com.rabbitmq.client.Channel;
import com.yaoyaozw.customer.constants.CustomerCommonConstant;
import com.yaoyaozw.customer.constants.RabbitCommonNameConstant;
import com.yaoyaozw.customer.dto.integration.IntegrationRequestDTO; import com.yaoyaozw.customer.dto.integration.IntegrationRequestDTO;
import com.yaoyaozw.customer.service.AccountOrderService; import com.yaoyaozw.customer.service.AccountOrderService;
import com.yaoyaozw.customer.service.CustomerDelayPublishService; import com.yaoyaozw.customer.service.CustomerDelayPublishService;
import com.yaoyaozw.customer.service.CustomerGraphicsService; import com.yaoyaozw.customer.service.CustomerGraphicsService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.amqp.rabbit.core.ChannelCallback;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController; import org.springframework.web.bind.annotation.RestController;
...@@ -15,6 +25,7 @@ import java.util.Calendar; ...@@ -15,6 +25,7 @@ import java.util.Calendar;
* @author wgh * @author wgh
* @date 2022/10/10 20:04 * @date 2022/10/10 20:04
*/ */
@Slf4j
@RestController @RestController
@RequestMapping("/customer-service/test") @RequestMapping("/customer-service/test")
public class TestController { public class TestController {
...@@ -25,6 +36,10 @@ public class TestController { ...@@ -25,6 +36,10 @@ public class TestController {
private AccountOrderService accountOrderService; private AccountOrderService accountOrderService;
@Autowired @Autowired
private CustomerDelayPublishService customerDelayPublishService; private CustomerDelayPublishService customerDelayPublishService;
@Autowired
private RabbitTemplate rabbitTemplate;
@Autowired
private RedisTemplate<String, Object> redisTemplate;
@GetMapping("/CustomerTest") @GetMapping("/CustomerTest")
public void test(){ public void test(){
...@@ -69,4 +84,53 @@ public class TestController { ...@@ -69,4 +84,53 @@ public class TestController {
integrationRequestDTO.setRequestDate(calendar.getTime()); integrationRequestDTO.setRequestDate(calendar.getTime());
accountOrderService.activeUserByOrder(integrationRequestDTO); accountOrderService.activeUserByOrder(integrationRequestDTO);
} }
@GetMapping("/ygLimit")
public void ygLimit(){
limit();
}
private void limit() {
boolean go;
synchronized (rabbitTemplate) {
// 从redis查询计数
Object count = redisTemplate.opsForValue().get(CustomerCommonConstant.YANG_GUANG_ACCESS_LIMIT_REDIS_KEY);
if (ObjectUtil.isNull(count)) {
// redis中没有,设置0
redisTemplate.opsForValue().set(CustomerCommonConstant.YANG_GUANG_ACCESS_LIMIT_REDIS_KEY, 0);
count = 0;
}
log.info("当前count: {}", count);
long stamp = System.currentTimeMillis();
// 判断计数是否达到上限
go = (Integer)count < 40;
if (go) {
// 没有达到上限,发送消息并且计数加1
rabbitTemplate.convertAndSend(RabbitCommonNameConstant.OPERATE_COMMON_EXCHANGE, RabbitCommonNameConstant.YG_LIMIT_TTL_ROUTE_KEY, stamp);
redisTemplate.opsForValue().increment(CustomerCommonConstant.YANG_GUANG_ACCESS_LIMIT_REDIS_KEY);
}
}
if (!go) {
// 计数达到上限了,休眠30秒,递归调用
try {
log.info("达到上限, 休眠15秒");
Thread.sleep(15000);
limit();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
@RabbitListener(queues = RabbitCommonNameConstant.YG_LIMIT_DEATH_QUEUE)
public void listener() {
log.info("消费");
redisTemplate.opsForValue().decrement(CustomerCommonConstant.YANG_GUANG_ACCESS_LIMIT_REDIS_KEY);
}
} }
...@@ -129,39 +129,6 @@ public class SchedulingTask { ...@@ -129,39 +129,6 @@ public class SchedulingTask {
*/ */
// @Scheduled(cron = "0 0/15 * * * ?") // @Scheduled(cron = "0 0/15 * * * ?")
public void updateCrowdPackageNumFromRedis() { public void updateCrowdPackageNumFromRedis() {
localLog.info("开始同步redis中人群包人数至数据库"); crowdPackageService.updateCrowdPackageNumFromRedis();
HashMap<String, Integer> entries = (HashMap<String, Integer>) redisTemplate.boundHashOps(CustomerCommonConstant.CROWD_HUMAN_NUN_REDIS_KEY).entries();
if (CollectionUtil.isEmpty(entries)) {
return;
}
localLog.info("redis中获取到人群包人数数据: {}对", entries.size());
Date now = new Date();
List<CrowdPackage> list = crowdPackageService.list();
localLog.info("数据库中获取人群包: {}条", list.size());
List<CrowdPackage> resultList = list.stream().filter(item -> entries.containsKey(item.getId().toString()))
.peek(item -> {
item.setLastCountTime(now);
Integer num = entries.get(item.getId().toString());
item.setCrowdNum(num);
}).collect(Collectors.toList());
if (entries.size() != list.size()) {
localLog.info("删除redis在数据库中不存在的人群包");
List<String> idList = list.stream().map(item -> String.valueOf(item.getId())).collect(Collectors.toList());
for (String key : entries.keySet()) {
if (!idList.contains(key)) {
localLog.info("redis中人群包id: {} 在数据库中不存在, 删除", key);
redisTemplate.opsForHash().delete(CustomerCommonConstant.CROWD_HUMAN_NUN_REDIS_KEY, key);
}
}
}
localLog.info("要更新人群包: {} 条", resultList.size());
if (CollectionUtil.isNotEmpty(resultList)) {
crowdPackageService.updateBatchById(resultList);
}
localLog.info("删除创建时的临时数据");
crowdPackageService.remove(new QueryWrapper<CrowdPackage>().isNull("package_name"));
} }
} }
...@@ -99,4 +99,10 @@ public interface CrowdPackageService extends IService<CrowdPackage> { ...@@ -99,4 +99,10 @@ public interface CrowdPackageService extends IService<CrowdPackage> {
* @return {@link BaseResult} * @return {@link BaseResult}
*/ */
BaseResult updateUserPackageBatch(List<String> openIdList); BaseResult updateUserPackageBatch(List<String> openIdList);
/**
* 更新包num人群复述
*/
void updateCrowdPackageNumFromRedis();
} }
...@@ -11,6 +11,7 @@ import com.yaoyaozw.customer.common.GenericsResult; ...@@ -11,6 +11,7 @@ import com.yaoyaozw.customer.common.GenericsResult;
import com.yaoyaozw.customer.components.CustomerServiceCommonAsyncComponent; import com.yaoyaozw.customer.components.CustomerServiceCommonAsyncComponent;
import com.yaoyaozw.customer.components.TokenManager; import com.yaoyaozw.customer.components.TokenManager;
import com.yaoyaozw.customer.constants.CrowdPackageCommonConstant; import com.yaoyaozw.customer.constants.CrowdPackageCommonConstant;
import com.yaoyaozw.customer.constants.CustomerCommonConstant;
import com.yaoyaozw.customer.dto.crowd.CrowdPackageQueryDTO; import com.yaoyaozw.customer.dto.crowd.CrowdPackageQueryDTO;
import com.yaoyaozw.customer.dto.crowd.CrowdPackageConditionDTO; import com.yaoyaozw.customer.dto.crowd.CrowdPackageConditionDTO;
import com.yaoyaozw.customer.entity.CrowdPackage; import com.yaoyaozw.customer.entity.CrowdPackage;
...@@ -34,6 +35,7 @@ import org.springframework.data.redis.core.RedisTemplate; ...@@ -34,6 +35,7 @@ import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service; import org.springframework.stereotype.Service;
import java.util.Date; import java.util.Date;
import java.util.HashMap;
import java.util.List; import java.util.List;
import java.util.stream.Collectors; import java.util.stream.Collectors;
...@@ -234,6 +236,44 @@ public class CrowdPackageServiceImpl extends ServiceImpl<MaterialCrowdPackageMap ...@@ -234,6 +236,44 @@ public class CrowdPackageServiceImpl extends ServiceImpl<MaterialCrowdPackageMap
return new BaseResult().success(); return new BaseResult().success();
} }
@Override
public void updateCrowdPackageNumFromRedis() {
LOCAL_LOG.info("开始同步redis中人群包人数至数据库");
HashMap<String, Integer> entries = (HashMap<String, Integer>) redisTemplate.boundHashOps(CustomerCommonConstant.CROWD_HUMAN_NUN_REDIS_KEY).entries();
if (CollectionUtil.isEmpty(entries)) {
return;
}
LOCAL_LOG.info("redis中获取到人群包人数数据: {}对", entries.size());
Date now = new Date();
List<CrowdPackage> list = super.list();
LOCAL_LOG.info("数据库中获取人群包: {}条", list.size());
List<CrowdPackage> resultList = list.stream().filter(item -> entries.containsKey(item.getId().toString()))
.peek(item -> {
item.setLastCountTime(now);
Integer num = entries.get(item.getId().toString());
item.setCrowdNum(num);
}).collect(Collectors.toList());
if (entries.size() != list.size()) {
LOCAL_LOG.info("删除redis在数据库中不存在的人群包");
List<String> idList = list.stream().map(item -> String.valueOf(item.getId())).collect(Collectors.toList());
for (String key : entries.keySet()) {
if (!idList.contains(key)) {
LOCAL_LOG.info("redis中人群包id: {} 在数据库中不存在, 删除", key);
redisTemplate.opsForHash().delete(CustomerCommonConstant.CROWD_HUMAN_NUN_REDIS_KEY, key);
}
}
}
LOCAL_LOG.info("要更新人群包: {} 条", resultList.size());
if (CollectionUtil.isNotEmpty(resultList)) {
super.updateBatchById(resultList);
}
LOCAL_LOG.info("删除创建时的临时数据");
super.remove(new QueryWrapper<CrowdPackage>().isNull("package_name"));
}
/** /**
* 构造操作符 * 构造操作符
* *
......
...@@ -169,13 +169,15 @@ public class CustomerGraphicsDelayServiceImpl extends ServiceImpl<CustomerGraphi ...@@ -169,13 +169,15 @@ public class CustomerGraphicsDelayServiceImpl extends ServiceImpl<CustomerGraphi
// 查询链接数据 // 查询链接数据
ReferralEntity referralEntity = referralEntityService.getOne(new QueryWrapper<ReferralEntity>().eq("material_graphics_id", id)); ReferralEntity referralEntity = referralEntityService.getOne(new QueryWrapper<ReferralEntity>().eq("material_graphics_id", id));
if (ObjectUtil.isNull(referralEntity)) { if (ObjectUtil.isNotNull(referralEntity)) {
return new GenericsResult<>(false, "找不到链接数据");
}
CustomerReferralDTO customerReferralDto = new CustomerReferralDTO(); CustomerReferralDTO customerReferralDto = new CustomerReferralDTO();
BeanUtil.copyProperties(referralEntity, customerReferralDto); BeanUtil.copyProperties(referralEntity, customerReferralDto);
customerDelayGraphicsDetailVO.setCustomerReferralDto(customerReferralDto); customerDelayGraphicsDetailVO.setCustomerReferralDto(customerReferralDto);
} else {
customerDelayGraphicsDetailVO.setCustomerReferralDto(new CustomerReferralDTO());
}
AuthInfoVO authInfoVO = super.baseMapper.getCustomerDelayAuthInfo(id); AuthInfoVO authInfoVO = super.baseMapper.getCustomerDelayAuthInfo(id);
customerDelayGraphicsDetailVO.setAuthInfoVo(authInfoVO); customerDelayGraphicsDetailVO.setAuthInfoVo(authInfoVO);
......
...@@ -59,6 +59,7 @@ ...@@ -59,6 +59,7 @@
on ae.store_id = se.id on ae.store_id = se.id
where ae.app_id = #{appId} where ae.app_id = #{appId}
and is_active = 1
</select> </select>
</mapper> </mapper>
\ No newline at end of file
Markdown 格式
0%
您添加了 0 到此讨论。请谨慎行事。
请先完成此评论的编辑!
注册 或者 后发表评论