|  | @@ -1,13 +1,22 @@
 | 
	
		
			
				|  |  |  package com.zanxiang.game.back.serve.service.impl;
 | 
	
		
			
				|  |  |  
 | 
	
		
			
				|  |  | +import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
 | 
	
		
			
				|  |  |  import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
 | 
	
		
			
				|  |  |  import com.zanxiang.game.back.serve.dao.mapper.GameTencentMiniGameCallbackMapper;
 | 
	
		
			
				|  |  |  import com.zanxiang.game.back.serve.pojo.dto.GameTencentCallbackDTO;
 | 
	
		
			
				|  |  | +import com.zanxiang.game.back.serve.pojo.entity.GameBackPolicy;
 | 
	
		
			
				|  |  | +import com.zanxiang.game.back.serve.pojo.entity.GameTencentMiniGameBackLog;
 | 
	
		
			
				|  |  |  import com.zanxiang.game.back.serve.pojo.entity.GameTencentMiniGameCallback;
 | 
	
		
			
				|  |  | +import com.zanxiang.game.back.serve.pojo.enums.ActionTypeEnum;
 | 
	
		
			
				|  |  | +import com.zanxiang.game.back.serve.pojo.enums.BackStatusEnum;
 | 
	
		
			
				|  |  | +import com.zanxiang.game.back.serve.pojo.enums.BackTypeEnum;
 | 
	
		
			
				|  |  | +import com.zanxiang.game.back.serve.service.IGameBackPolicyService;
 | 
	
		
			
				|  |  | +import com.zanxiang.game.back.serve.service.IGameTencentMiniGameBackLogService;
 | 
	
		
			
				|  |  |  import com.zanxiang.game.back.serve.service.IGameTencentMiniGameCallbackService;
 | 
	
		
			
				|  |  |  import com.zanxiang.game.module.base.ServerInfo;
 | 
	
		
			
				|  |  |  import com.zanxiang.game.module.base.pojo.vo.AgentRpcVO;
 | 
	
		
			
				|  |  |  import com.zanxiang.game.module.base.rpc.IAgentRpc;
 | 
	
		
			
				|  |  | +import com.zanxiang.module.redis.service.IDistributedLockComponent;
 | 
	
		
			
				|  |  |  import com.zanxiang.module.util.DateUtil;
 | 
	
		
			
				|  |  |  import com.zanxiang.module.util.JsonUtil;
 | 
	
		
			
				|  |  |  import com.zanxiang.module.util.bean.BeanUtil;
 | 
	
	
		
			
				|  | @@ -15,11 +24,14 @@ import lombok.extern.slf4j.Slf4j;
 | 
	
		
			
				|  |  |  import org.apache.dubbo.config.annotation.DubboReference;
 | 
	
		
			
				|  |  |  import org.apache.kafka.clients.producer.KafkaProducer;
 | 
	
		
			
				|  |  |  import org.apache.kafka.clients.producer.ProducerRecord;
 | 
	
		
			
				|  |  | +import org.apache.logging.log4j.util.Strings;
 | 
	
		
			
				|  |  |  import org.springframework.beans.factory.annotation.Autowired;
 | 
	
		
			
				|  |  |  import org.springframework.beans.factory.annotation.Value;
 | 
	
		
			
				|  |  |  import org.springframework.stereotype.Service;
 | 
	
		
			
				|  |  |  
 | 
	
		
			
				|  |  |  import java.time.LocalDate;
 | 
	
		
			
				|  |  | +import java.util.Objects;
 | 
	
		
			
				|  |  | +import java.util.concurrent.TimeUnit;
 | 
	
		
			
				|  |  |  
 | 
	
		
			
				|  |  |  @Slf4j
 | 
	
		
			
				|  |  |  @Service
 | 
	
	
		
			
				|  | @@ -35,6 +47,15 @@ public class GameTencentMiniGameCallbackServiceImpl extends ServiceImpl<GameTenc
 | 
	
		
			
				|  |  |      @Autowired
 | 
	
		
			
				|  |  |      private KafkaProducer<String, String> kafkaProducer;
 | 
	
		
			
				|  |  |  
 | 
	
		
			
				|  |  | +    @Autowired
 | 
	
		
			
				|  |  | +    private IDistributedLockComponent distributedLockComponent;
 | 
	
		
			
				|  |  | +
 | 
	
		
			
				|  |  | +    @Autowired
 | 
	
		
			
				|  |  | +    private IGameBackPolicyService gameBackPolicyService;
 | 
	
		
			
				|  |  | +
 | 
	
		
			
				|  |  | +    @Autowired
 | 
	
		
			
				|  |  | +    private IGameTencentMiniGameBackLogService gameTencentMiniGameBackLogService;
 | 
	
		
			
				|  |  | +
 | 
	
		
			
				|  |  |      @Override
 | 
	
		
			
				|  |  |      public boolean callback(GameTencentCallbackDTO dto) {
 | 
	
		
			
				|  |  |          AgentRpcVO agent = agentRpc.getByTencentAccountId(dto.getAccountId()).getData();
 | 
	
	
		
			
				|  | @@ -45,17 +66,64 @@ public class GameTencentMiniGameCallbackServiceImpl extends ServiceImpl<GameTenc
 | 
	
		
			
				|  |  |          if (agent != null) {
 | 
	
		
			
				|  |  |              callback.setAgentKey(agent.getAgentKey());
 | 
	
		
			
				|  |  |              callback.setGameId(agent.getGameId());
 | 
	
		
			
				|  |  | +            callback.setWechatAppId(agent.getAppId());
 | 
	
		
			
				|  |  |          } else {
 | 
	
		
			
				|  |  |              callback.setAgentKey("-");
 | 
	
		
			
				|  |  |              callback.setGameId(-1L);
 | 
	
		
			
				|  |  |          }
 | 
	
		
			
				|  |  |          callback.setDay(dto.getClickTime() == null || dto.getClickTime() < 1000 ? LocalDate.now() : DateUtil.secondToLocalDate(dto.getClickTime()));
 | 
	
		
			
				|  |  |          save(callback);
 | 
	
		
			
				|  |  | +        //信息发送到卡夫卡
 | 
	
		
			
				|  |  |          try {
 | 
	
		
			
				|  |  |              kafkaProducer.send(new ProducerRecord<>(tencentCallbackTopic, dto.getAccountId().toString(), JsonUtil.toString(callback)));
 | 
	
		
			
				|  |  |          } catch (Exception e) {
 | 
	
		
			
				|  |  |              log.error(e.getMessage(), e);
 | 
	
		
			
				|  |  |          }
 | 
	
		
			
				|  |  | +        //监测链接执行注册回传
 | 
	
		
			
				|  |  | +        try {
 | 
	
		
			
				|  |  | +            this.linkCallBack(callback, agent);
 | 
	
		
			
				|  |  | +        }catch (Exception e){
 | 
	
		
			
				|  |  | +            log.error(e.getMessage(), e);
 | 
	
		
			
				|  |  | +        }
 | 
	
		
			
				|  |  |          return true;
 | 
	
		
			
				|  |  |      }
 | 
	
		
			
				|  |  | +
 | 
	
		
			
				|  |  | +    private void linkCallBack(GameTencentMiniGameCallback callback, AgentRpcVO agent) {
 | 
	
		
			
				|  |  | +        //渠道参数判断
 | 
	
		
			
				|  |  | +        if (agent == null || agent.getBackPolicyId() == null){
 | 
	
		
			
				|  |  | +            return;
 | 
	
		
			
				|  |  | +        }
 | 
	
		
			
				|  |  | +        //查询策略, 判断是否开启监测链接回传
 | 
	
		
			
				|  |  | +        GameBackPolicy gameBackPolicy = gameBackPolicyService.getById(agent.getBackPolicyId());
 | 
	
		
			
				|  |  | +        if (!Objects.equals(gameBackPolicy.getLinkCallBack(), Boolean.TRUE)){
 | 
	
		
			
				|  |  | +            return;
 | 
	
		
			
				|  |  | +        }
 | 
	
		
			
				|  |  | +        //参数校验, 判断是否符合回传条件
 | 
	
		
			
				|  |  | +        if (callback.getGameId() == null || Strings.isBlank(callback.getWechatAppId())
 | 
	
		
			
				|  |  | +                || callback.getAccountId() == null || Strings.isBlank(callback.getWechatOpenid())) {
 | 
	
		
			
				|  |  | +            log.error("监测链接回传参数不全, id : {}", callback.getId());
 | 
	
		
			
				|  |  | +            return;
 | 
	
		
			
				|  |  | +        }
 | 
	
		
			
				|  |  | +        //线程锁, 防止重复请求
 | 
	
		
			
				|  |  | +        String lockKey = com.zanxiang.game.back.base.ServerInfo.SERVER_NAME + ":tencentMiniLinkCallBack:"
 | 
	
		
			
				|  |  | +                + callback.getGameId() + ":" + callback.getAccountId() + ":"
 | 
	
		
			
				|  |  | +                + callback.getWechatAppId() + ":" + callback.getWechatOpenid();
 | 
	
		
			
				|  |  | +        if (!distributedLockComponent.doLock(lockKey, 0L, 3L, TimeUnit.MINUTES)) {
 | 
	
		
			
				|  |  | +            return;
 | 
	
		
			
				|  |  | +        }
 | 
	
		
			
				|  |  | +        //注册已回传判断
 | 
	
		
			
				|  |  | +        if (gameTencentMiniGameBackLogService.count(new LambdaQueryWrapper<GameTencentMiniGameBackLog>()
 | 
	
		
			
				|  |  | +                .eq(GameTencentMiniGameBackLog::getGameId, callback.getGameId())
 | 
	
		
			
				|  |  | +                .eq(GameTencentMiniGameBackLog::getWechatAppId, callback.getWechatAppId())
 | 
	
		
			
				|  |  | +                .eq(GameTencentMiniGameBackLog::getWechatOpenid, callback.getWechatOpenid())
 | 
	
		
			
				|  |  | +                .eq(GameTencentMiniGameBackLog::getAdAccountId, callback.getAccountId())
 | 
	
		
			
				|  |  | +                .eq(GameTencentMiniGameBackLog::getActionType, ActionTypeEnum.REGISTER.getActionType())
 | 
	
		
			
				|  |  | +                .eq(GameTencentMiniGameBackLog::getBackStatus, BackStatusEnum.SUCCESS.getBackStatus())
 | 
	
		
			
				|  |  | +                .eq(GameTencentMiniGameBackLog::getBackType, BackTypeEnum.BACK_API.getBackType())
 | 
	
		
			
				|  |  | +        ) > 0) {
 | 
	
		
			
				|  |  | +            return;
 | 
	
		
			
				|  |  | +        }
 | 
	
		
			
				|  |  | +        //注册回传
 | 
	
		
			
				|  |  | +        gameTencentMiniGameBackLogService.linkCallBack(callback);
 | 
	
		
			
				|  |  | +    }
 | 
	
		
			
				|  |  |  }
 |