package com.zanxiang.game.module.sdk.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.CollectionUtils;
import com.zanxiang.game.back.base.ServerInfo;
import com.zanxiang.game.back.base.pojo.dto.*;
import com.zanxiang.game.back.base.rpc.*;
import com.zanxiang.game.module.base.pojo.enums.AccountTypeEnum;
import com.zanxiang.game.module.base.pojo.enums.GameCategoryEnum;
import com.zanxiang.game.module.mybatis.entity.*;
import com.zanxiang.game.module.sdk.pojo.dto.PlatformOrderDTO;
import com.zanxiang.game.module.sdk.service.*;
import com.zanxiang.module.util.JsonUtil;
import lombok.extern.slf4j.Slf4j;
import org.apache.dubbo.config.annotation.DubboReference;
import org.apache.logging.log4j.util.Strings;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.time.LocalDateTime;
import java.util.Map;
import java.util.Objects;

/**
 * @author : lingfeng
 * @time : 2022-11-10
 * @description : IAP游戏SDK回传方法
 */
@Slf4j
@Service
public class CallBackServiceImpl implements ICallBackService {

    @DubboReference(providedBy = ServerInfo.SERVER_DUBBO_NAME)
    private ITencentAppApiBackRpc tencentAppApiBackRpc;

    @DubboReference(providedBy = ServerInfo.SERVER_DUBBO_NAME)
    private ITencentUserActionBackRpc tencentUserActionBackRpc;

    @DubboReference(providedBy = ServerInfo.SERVER_DUBBO_NAME)
    private ITencentMiniGameBackRpc tencentMiniGameBackRpc;

    @DubboReference(providedBy = ServerInfo.SERVER_DUBBO_NAME)
    private ITtMiniGameBackRpc ttMiniGameBackRpc;

    @DubboReference(providedBy = ServerInfo.SERVER_DUBBO_NAME)
    private ITtAppBackRpc ttAppBackRpc;

    @DubboReference(providedBy = ServerInfo.SERVER_DUBBO_NAME)
    private ITencentAppBackRpc tencentAppBackRpc;

    @Autowired
    private IAgentService agentService;

    @Autowired
    private IGameService gameService;

    @Autowired
    private IGameAppletService gameAppletService;

    @Autowired
    private IUserService userService;

    @Autowired
    private IGameExtService gameExtService;

    @Autowired
    private IGameBackLogService gameBackLogService;

    @Override
    public void userCallBack(User user, Map<String, String> urlParamMap) {
        //判断游戏是否开启广告回传, 未开启, 不回传
        GameExt gameExt = gameExtService.getByGameId(user.getGameId());
        if (!Objects.equals(gameExt.getAdCallBackSwitch(), Boolean.TRUE)) {
            return;
        }
        //自然量, 或者跳转链接参数不存在, 不做回传处理
        if (user.getAgentId() == 0L || CollectionUtils.isEmpty(urlParamMap)) {
            return;
        }
        //查询用户渠道信息
        Agent agent = agentService.getById(user.getAgentId());
        if (agent == null) {
            log.error("用户回传不存在渠道信息, userId : {}", user.getId());
            return;
        }
        //查询小游戏信息或者H5游戏相关公众号信息
        GameApplet gameApplet = gameAppletService.getOne(new LambdaQueryWrapper<GameApplet>()
                .eq(GameApplet::getGameId, agent.getGameId()));
        log.error("用户注册回传, userId : {}", user.getId());
        try {
            //腾讯H5回传
            if (Objects.equals(agent.getAccountType(), AccountTypeEnum.TENCENT_H5.getValue())) {
                TencentUserDTO tencentUserDTO = this.transform(user, agent, gameApplet);
                tencentUserActionBackRpc.backUser(tencentUserDTO);
                gameBackLogService.addLog(user.getId(), null, "腾讯H5用户回传提交", JsonUtil.toString(tencentUserDTO));
            }
            //腾讯小游戏回传
            if (Objects.equals(agent.getAccountType(), AccountTypeEnum.TENCENT_MINI_GAME.getValue())) {
                this.userCallBackTencentMiniGame(user, agent, gameApplet);
            }
            //腾讯APP私域
            if (Objects.equals(agent.getAccountType(), AccountTypeEnum.TENCENT_APP_API.getValue())) {
                TencentUserAppApiRpcDTO tencentUserAppApiRpcDTO = this.transform(user, agent, urlParamMap.get("callbackId"));
                tencentAppApiBackRpc.backUser(tencentUserAppApiRpcDTO);
                gameBackLogService.addLog(user.getId(), null, "腾讯APP私域用户回传提交", JsonUtil.toString(tencentUserAppApiRpcDTO));
            }
            //头条回传
            if (CollectionUtils.isNotEmpty(urlParamMap) && Objects.equals(agent.getAccountType(), AccountTypeEnum.BYTE.getValue())) {
                Game game = gameService.getById(agent.getGameId());
                //判断是微信小游戏
                if (Objects.equals(game.getCategory(), GameCategoryEnum.CATEGORY_WX_APPLET.getId())) {
                    TtUserActiveRpcDTO activeReportRpcDTO = this.transform(user, agent, gameApplet, urlParamMap);
                    ttMiniGameBackRpc.userActiveReport(activeReportRpcDTO);
                    gameBackLogService.addLog(user.getId(), null, "头条用户回传提交", JsonUtil.toString(activeReportRpcDTO));
                }
            }
            //头条APP直投回传
            if (CollectionUtils.isNotEmpty(urlParamMap) && Objects.equals(agent.getAccountType(), AccountTypeEnum.BYTE_APP.getValue())) {
                TtUserActiveAppRpcDTO ttUserActiveAppRpcDTO = this.transform(user, agent);
                ttAppBackRpc.userActiveReport(ttUserActiveAppRpcDTO);
                gameBackLogService.addLog(user.getId(), null, "头条APP用户回传提交", JsonUtil.toString(ttUserActiveAppRpcDTO));
            }
            //腾讯APP直投回传
            if (CollectionUtils.isNotEmpty(urlParamMap) && Objects.equals(agent.getAccountType(), AccountTypeEnum.TENCENT_APP.getValue())) {
                TencentUserAppRpcDTO tencentUserAppRpcDTO = this.transform(agent, user);
                tencentAppBackRpc.backUser(tencentUserAppRpcDTO);
                gameBackLogService.addLog(user.getId(), null, "腾讯APP用户回传提交", JsonUtil.toString(tencentUserAppRpcDTO));
            }
        } catch (Exception e) {
            log.error("用户回传异常, userId : {}, e : {}", user.getId(), e.getMessage());
        }
    }

    private void userCallBackTencentMiniGame(User user, Agent agent, GameApplet gameApplet) {
        //查询游戏信息
        Game game = gameService.getById(agent.getGameId());
        //构造回传参数
        TencentUserDTO tencentUserDTO = this.transform(user, agent, gameApplet);
        //解析设置clickId
        Map<String, String> channelMap = agentService.channelTransform(user.getChannel());
        tencentUserDTO.setClickId(channelMap.get("gdt_vid"));
        //判断API回传开关是否关掉, 未设置关掉, 则执行API回传
        if (game.getApiBackSwitch() == null || game.getApiBackSwitch()) {
            tencentMiniGameBackRpc.backUser(tencentUserDTO);
            gameBackLogService.addLog(user.getId(), null, "腾讯小游戏用户回传提交",
                    JsonUtil.toString(tencentUserDTO));
        }
        //判断是否配置了媒体SDK, 配置了则执行媒体SDK回传
        if (Strings.isNotBlank(game.getAdSdkConfig())) {
            tencentMiniGameBackRpc.callBackUser(tencentUserDTO);
            gameBackLogService.addLog(user.getId(), null, "腾讯小游戏用户媒体SDK回传提交",
                    JsonUtil.toString(tencentUserDTO));
        }
    }

    @Override
    public void roleCallBack(GameUserRole gameUserRole) {
        //判断游戏是否开启广告回传, 未开启, 不回传
        GameExt gameExt = gameExtService.getByGameId(gameUserRole.getGameId());
        if (!Objects.equals(gameExt.getAdCallBackSwitch(), Boolean.TRUE)) {
            return;
        }
        //用户信息
        User user = userService.getById(gameUserRole.getUserId());
        //用户渠道信息
        Agent agent = agentService.getAgentByChannel(user.getChannel());
        if (agent == null) {
            return;
        }
        //查询小游戏信息或者H5游戏相关公众号信息
        GameApplet gameApplet = gameAppletService.getOne(new LambdaQueryWrapper<GameApplet>()
                .eq(GameApplet::getGameId, agent.getGameId()));
        log.error("用户创角回传, userId : {}", user.getId());
        try {
            //腾讯H5回传
            if (Objects.equals(agent.getAccountType(), AccountTypeEnum.TENCENT_H5.getValue())) {
                TencentRoleRegisterRpcDTO tencentRoleRegisterRpcDTO = this.transform(user, agent, gameApplet, gameUserRole);
                tencentUserActionBackRpc.backRoleRegister(tencentRoleRegisterRpcDTO);
                gameBackLogService.addLog(user.getId(), gameUserRole.getRoleId(), "腾讯H5创角回传提交", JsonUtil.toString(tencentRoleRegisterRpcDTO));
            }
            //腾讯小游戏回传
            if (Objects.equals(agent.getAccountType(), AccountTypeEnum.TENCENT_MINI_GAME.getValue())) {
                this.roleCallBackTencentMiniGame(user, agent, gameApplet, gameUserRole);
            }
            //腾讯APP私域
            if (Objects.equals(agent.getAccountType(), AccountTypeEnum.TENCENT_APP_API.getValue())) {
                TencentRoleRegisterAppApiRpcDTO tencentRoleRegisterAppApiRpcDTO = this.transform(gameUserRole, user, agent);
                tencentAppApiBackRpc.backRoleRegister(tencentRoleRegisterAppApiRpcDTO);
                gameBackLogService.addLog(user.getId(), null, "腾讯APP私域创角回传提交", JsonUtil.toString(tencentRoleRegisterAppApiRpcDTO));
            }
            //头条回传
            if (Objects.equals(agent.getAccountType(), AccountTypeEnum.BYTE.getValue())) {
                Game game = gameService.getById(agent.getGameId());
                //判断是微信小游戏
                if (Objects.equals(game.getCategory(), GameCategoryEnum.CATEGORY_WX_APPLET.getId())) {
                    TtRoleRegisterRpcDTO ttRoleRegisterRpcDTO = this.transform(user, agent, gameUserRole, gameApplet);
                    ttMiniGameBackRpc.roleRegisterReport(ttRoleRegisterRpcDTO);
                    gameBackLogService.addLog(user.getId(), gameUserRole.getRoleId(), "头条创角回传提交", JsonUtil.toString(ttRoleRegisterRpcDTO));
                }
            }
            //头条APP直投回传
            if (Objects.equals(agent.getAccountType(), AccountTypeEnum.BYTE_APP.getValue())) {
                TtRoleRegisterAppRpcDTO ttRoleRegisterAppRpcDTO = this.transform(user, agent, gameUserRole);
                ttAppBackRpc.roleRegisterReport(ttRoleRegisterAppRpcDTO);
                gameBackLogService.addLog(user.getId(), gameUserRole.getRoleId(), "头条APP创角回传提交", JsonUtil.toString(ttRoleRegisterAppRpcDTO));
            }
            //腾讯APP直投回传
            if (Objects.equals(agent.getAccountType(), AccountTypeEnum.TENCENT_APP.getValue())) {
                TencentRoleRegisterAppRpcDTO tencentRoleRegisterAppRpcDTO = this.transform(agent, user, gameUserRole);
                tencentAppBackRpc.backRoleRegister(tencentRoleRegisterAppRpcDTO);
                gameBackLogService.addLog(user.getId(), gameUserRole.getRoleId(), "腾讯APP创角回传提交", JsonUtil.toString(tencentRoleRegisterAppRpcDTO));
            }
        } catch (Exception e) {
            log.error("创角回传异常, userId : {}, e : {}", user.getId(), e.getMessage());
        }
    }

    private void roleCallBackTencentMiniGame(User user, Agent agent, GameApplet gameApplet, GameUserRole gameUserRole) {
        //查询游戏信息
        Game game = gameService.getById(agent.getGameId());
        TencentRoleRegisterRpcDTO tencentRoleRegisterRpcDTO = this.transform(user, agent, gameApplet, gameUserRole);
        //判断API回传开关是否关掉, 未设置关掉, 则执行API回传
        if (game.getApiBackSwitch() == null || game.getApiBackSwitch()) {
            tencentMiniGameBackRpc.backRoleRegister(tencentRoleRegisterRpcDTO);
            gameBackLogService.addLog(user.getId(), gameUserRole.getRoleId(), "腾讯小游戏创角回传提交",
                    JsonUtil.toString(tencentRoleRegisterRpcDTO));
        }
        //判断是否配置了媒体SDK, 配置了则执行媒体SDK回传
        if (Strings.isNotBlank(game.getAdSdkConfig())) {
            tencentMiniGameBackRpc.callBackRole(tencentRoleRegisterRpcDTO);
            gameBackLogService.addLog(user.getId(), gameUserRole.getRoleId(), "腾讯小游戏创角媒体SDK回传提交",
                    JsonUtil.toString(tencentRoleRegisterRpcDTO));
        }
    }

    @Override
    public void orderCallBack(PlatformOrderDTO platformOrderDTO) {
        //判断游戏是否开启广告回传, 未开启, 不回传
        GameExt gameExt = gameExtService.getByGameId(platformOrderDTO.getGameId());
        if (!Objects.equals(gameExt.getAdCallBackSwitch(), Boolean.TRUE)) {
            return;
        }
        //用户信息
        User user = userService.getById(platformOrderDTO.getUserId());
        //用户渠道信息
        Agent agent = agentService.getAgentByChannel(user.getChannel());
        if (agent == null) {
            log.error("订单回传不存在渠道信息, orderId : {}", platformOrderDTO.getOrderId());
            return;
        }
        //查询小游戏信息或者H5游戏相关公众号信息
        GameApplet gameApplet = gameAppletService.getOne(new LambdaQueryWrapper<GameApplet>()
                .eq(GameApplet::getGameId, agent.getGameId()));
        log.error("用户订单回传, orderId : {}", platformOrderDTO.getOrderId());
        try {
            //腾讯H5回传
            if (Objects.equals(agent.getAccountType(), AccountTypeEnum.TENCENT_H5.getValue())) {
                TencentOrderDTO tencentOrderDTO = this.transform(platformOrderDTO, user, agent, gameApplet);
                tencentUserActionBackRpc.backOrder(tencentOrderDTO);
                gameBackLogService.addLog(user.getId(), platformOrderDTO.getOrderId(), "腾讯H5订单回传提交", JsonUtil.toString(tencentOrderDTO));
            }
            //腾讯小游戏回传
            if (Objects.equals(agent.getAccountType(), AccountTypeEnum.TENCENT_MINI_GAME.getValue())) {
                this.orderCallBackTencentMiniGame(user, agent, gameApplet, platformOrderDTO);
            }
            //腾讯APP私域
            if (Objects.equals(agent.getAccountType(), AccountTypeEnum.TENCENT_APP_API.getValue())) {
                TencentOrderAppApiRpcDTO tencentOrderAppApiRpcDTO = this.transform(user, agent, platformOrderDTO);
                tencentAppApiBackRpc.backOrder(tencentOrderAppApiRpcDTO);
                gameBackLogService.addLog(user.getId(), platformOrderDTO.getOrderId(), "腾讯APP私域订单回传提交", JsonUtil.toString(tencentOrderAppApiRpcDTO));
            }
            //头条回传
            if (Objects.equals(agent.getAccountType(), AccountTypeEnum.BYTE.getValue())) {
                //判断游戏类型
                Game game = gameService.getById(agent.getGameId());
                if (Objects.equals(game.getCategory(), GameCategoryEnum.CATEGORY_WX_APPLET.getId())) {
                    TtOrderRpcDTO ttOrderRpcDTO = this.transform(platformOrderDTO, user.getOpenId(), agent, gameApplet, user.getCreateTime());
                    ttMiniGameBackRpc.orderReport(ttOrderRpcDTO);
                    gameBackLogService.addLog(user.getId(), platformOrderDTO.getOrderId(), "头条订单回传提交", JsonUtil.toString(ttOrderRpcDTO));
                }
            }
            //头条APP直投回传
            if (Objects.equals(agent.getAccountType(), AccountTypeEnum.BYTE_APP.getValue())) {
                TtOrderAppRpcDTO ttOrderAppRpcDTO = this.transform(platformOrderDTO, user, agent);
                ttAppBackRpc.orderReport(ttOrderAppRpcDTO);
                gameBackLogService.addLog(user.getId(), platformOrderDTO.getOrderId(), "头条APP订单回传提交", JsonUtil.toString(ttOrderAppRpcDTO));
            }
            //腾讯APP直投回传
            if (Objects.equals(agent.getAccountType(), AccountTypeEnum.TENCENT_APP.getValue())) {
                TencentOrderAppRpcDTO tencentOrderAppRpcDTO = this.transform(platformOrderDTO, agent, user);
                tencentAppBackRpc.backOrder(tencentOrderAppRpcDTO);
                gameBackLogService.addLog(user.getId(), platformOrderDTO.getOrderId(), "腾讯APP订单回传提交", JsonUtil.toString(tencentOrderAppRpcDTO));
            }
        } catch (Exception e) {
            log.error("订单回传异常, orderId : {}, e : {}", platformOrderDTO.getOrderId(), e.getMessage());
        }
    }

    private void orderCallBackTencentMiniGame(User user, Agent agent, GameApplet gameApplet, PlatformOrderDTO platformOrderDTO) {
        //查询游戏信息
        Game game = gameService.getById(agent.getGameId());
        TencentOrderDTO tencentOrderDTO = this.transform(platformOrderDTO, user, agent, gameApplet);
        //解析设置clickId
        Map<String, String> channelMap = agentService.channelTransform(user.getChannel());
        tencentOrderDTO.setClickId(channelMap.get("gdt_vid"));
        //判断API回传开关是否关掉, 未设置关掉, 则执行API回传
        if (game.getApiBackSwitch() == null || game.getApiBackSwitch()) {
            tencentMiniGameBackRpc.backOrder(tencentOrderDTO);
            gameBackLogService.addLog(user.getId(), platformOrderDTO.getOrderId(), "腾讯小游戏订单回传提交",
                    JsonUtil.toString(tencentOrderDTO));
        }
        //判断是否配置了媒体SDK, 配置了则执行媒体SDK回传
        if (Strings.isNotBlank(game.getAdSdkConfig())) {
            tencentMiniGameBackRpc.callBackOrder(tencentOrderDTO);
            gameBackLogService.addLog(user.getId(), platformOrderDTO.getOrderId(), "腾讯小游戏订单媒体SDK回传提交",
                    JsonUtil.toString(tencentOrderDTO));
        }
    }

    private TencentRoleRegisterRpcDTO transform(User user, Agent agent, GameApplet gameApplet, GameUserRole gameUserRole) {
        return TencentRoleRegisterRpcDTO.builder()
                .backPolicyId(agent.getBackPolicyId())
                .gameId(agent.getGameId())
                .adAccountId(agent.getAccountId())
                .registerTime(gameUserRole.getCreateTime())
                .channel(agent.getAgentKey())
                .wechatOpenid(user.getOpenId())
                .wechatAppId(gameApplet == null ? null : gameApplet.getAppId())
                .userActionSetId(agent.getUserActionSetId())
                .roleId(gameUserRole.getRoleId())
                .roleName(gameUserRole.getRoleName())
                .roleLevel(gameUserRole.getRoleLevel())
                .build();
    }

    private TtRoleRegisterRpcDTO transform(User user, Agent agent, GameUserRole gameUserRole, GameApplet gameApplet) {
        return TtRoleRegisterRpcDTO.builder()
                .gameId(agent.getGameId())
                .backPolicyId(agent.getBackPolicyId())
                .accountReport(this.transform(agent))
                .wechatAppId(gameApplet == null ? null : gameApplet.getAppId())
                .wechatOpenId(user.getOpenId())
                .agentKey(agent.getAgentKey())
                .roleId(gameUserRole.getRoleId())
                .roleName(gameUserRole.getRoleName())
                .roleLevel(gameUserRole.getRoleLevel())
                .registerTime(gameUserRole.getCreateTime())
                .build();
    }

    private TencentUserDTO transform(User user, Agent agent, GameApplet gameApplet) {
        return TencentUserDTO.builder()
                .backPolicyId(agent.getBackPolicyId())
                .gameId(agent.getGameId())
                .adAccountId(agent.getAccountId())
                .registerTime(user.getCreateTime())
                .channel(agent.getAgentKey())
                .wechatOpenid(user.getOpenId())
                .wechatAppId(gameApplet == null ? null : gameApplet.getAppId())
                .userActionSetId(agent.getUserActionSetId())
                .build();
    }

    private TtUserActiveRpcDTO transform(User user, Agent agent, GameApplet gameApplet, Map<String, String> urlParamMap) {
        return TtUserActiveRpcDTO.builder()
                .gameId(agent.getGameId())
                .wechatAppId(gameApplet == null ? null : gameApplet.getAppId())
                .wechatOpenId(user.getOpenId())
                .accountReport(this.transform(agent))
                .agentKey(agent.getAgentKey())
                .backPolicyId(agent.getBackPolicyId())
                .activeTime(user.getCreateTime())
                .clueToken(urlParamMap.get("clue_token"))
                .projectId(Strings.isBlank(urlParamMap.get("project_id")) ? null : Long.valueOf(urlParamMap.get("project_id")))
                .promotionId(Strings.isBlank(urlParamMap.get("promotion_id")) ? null : Long.valueOf(urlParamMap.get("promotion_id")))
                .advertiserId(Strings.isBlank(urlParamMap.get("advertiser_id")) ? null : Long.valueOf(urlParamMap.get("advertiser_id")))
                .reqId(urlParamMap.get("req_id"))
                .build();
    }

    private TencentOrderDTO transform(PlatformOrderDTO platformOrderDTO, User user, Agent agent, GameApplet gameApplet) {
        return TencentOrderDTO.builder()
                .backPolicyId(agent.getBackPolicyId())
                .gameId(agent.getGameId())
                .adAccountId(agent.getAccountId())
                .registerTime(user.getCreateTime())
                .rechargeMoney(platformOrderDTO.getAmount().longValue() * 100)
                .rechargeTime(platformOrderDTO.getCreateTime())
                .orderId(platformOrderDTO.getOrderId())
                .channel(agent.getAgentKey())
                .wechatOpenid(user.getOpenId())
                .wechatAppId(gameApplet == null ? null : gameApplet.getAppId())
                .orderStatus(platformOrderDTO.getStatus())
                .payTime(platformOrderDTO.getPayTime())
                .userActionSetId(agent.getUserActionSetId())
                .roleId(platformOrderDTO.getRoleId())
                .roleName(platformOrderDTO.getRoleName())
                .build();
    }

    private TtOrderRpcDTO transform(PlatformOrderDTO platformOrderDTO, String openId, Agent agent, GameApplet gameApplet, LocalDateTime regTime) {
        return TtOrderRpcDTO.builder()
                .gameId(agent.getGameId())
                .accountReport(this.transform(agent))
                .wechatAppId(gameApplet == null ? null : gameApplet.getAppId())
                .wechatOpenId(openId)
                .orderId(platformOrderDTO.getOrderId())
                .agentKey(agent.getAgentKey())
                .backPolicyId(agent.getBackPolicyId())
                .rechargeMoney(platformOrderDTO.getAmount().longValue() * 100)
                .orderStatus(platformOrderDTO.getStatus())
                .createTime(platformOrderDTO.getCreateTime())
                .payTime(platformOrderDTO.getPayTime())
                .regTime(regTime)
                .roleId(platformOrderDTO.getRoleId())
                .roleName(platformOrderDTO.getRoleName())
                .build();
    }

    private TtAccountRpcDTO transform(Agent agent) {
        return TtAccountRpcDTO.builder()
                .accountId(agent.getAccountId())
                .reportToken(agent.getReportToken())
                .reportUrl(agent.getReportUrl())
                .build();
    }

    private TtUserActiveAppRpcDTO transform(User user, Agent agent) {
        return TtUserActiveAppRpcDTO.builder()
                .gameId(agent.getGameId())
                .accountReport(this.transform(agent))
                .agentKey(agent.getAgentKey())
                .activeTime(user.getCreateTime())
                .backPolicyId(agent.getBackPolicyId())
                .userId(user.getId().toString())
                .mac(user.getMac())
                .imei(user.getImei())
                .oaid(user.getOaid())
                .androidId(user.getAndroidId())
                .idfa(user.getIdfa())
                .caid(user.getCaid())
                .build();
    }

    private TencentUserAppRpcDTO transform(Agent agent, User user) {
        return TencentUserAppRpcDTO.builder()
                .gameId(agent.getGameId())
                .accountId(agent.getAccountId())
                .agentKey(agent.getAgentKey())
                .activeTime(user.getCreateTime())
                .backPolicyId(agent.getBackPolicyId())
                .userId(user.getId().toString())
                .mac(user.getMac())
                .imei(user.getImei())
                .oaid(user.getOaid())
                .androidId(user.getAndroidId())
                .idfa(user.getIdfa())
                .caid(user.getCaid())
                .regIp(user.getIp())
                .userActionSetId(agent.getUserActionSetId())
                .build();
    }

    private TtOrderAppRpcDTO transform(PlatformOrderDTO platformOrderDTO, User user, Agent agent) {
        return TtOrderAppRpcDTO.builder()
                .gameId(agent.getGameId())
                .accountReport(this.transform(agent))
                .userId(user.getId().toString())
                .orderId(platformOrderDTO.getOrderId())
                .agentKey(agent.getAgentKey())
                .backPolicyId(agent.getBackPolicyId())
                .rechargeMoney(platformOrderDTO.getAmount().multiply(new BigDecimal(100)).longValue())
                .orderStatus(platformOrderDTO.getStatus())
                .regTime(user.getCreateTime())
                .createTime(platformOrderDTO.getCreateTime())
                .payTime(platformOrderDTO.getPayTime())
                .roleId(platformOrderDTO.getRoleId())
                .roleName(platformOrderDTO.getRoleName())
                .mac(user.getMac())
                .imei(user.getImei())
                .oaid(user.getOaid())
                .androidId(user.getAndroidId())
                .idfa(user.getIdfa())
                .caid(user.getCaid())
                .build();
    }

    private TencentOrderAppRpcDTO transform(PlatformOrderDTO platformOrderDTO, Agent agent, User user) {
        return TencentOrderAppRpcDTO.builder()
                .gameId(agent.getGameId())
                .accountId(agent.getAccountId())
                .userId(user.getId().toString())
                .orderId(platformOrderDTO.getOrderId())
                .agentKey(agent.getAgentKey())
                .backPolicyId(agent.getBackPolicyId())
                .rechargeMoney(platformOrderDTO.getAmount().multiply(new BigDecimal(100)).longValue())
                .orderStatus(platformOrderDTO.getStatus())
                .regTime(user.getCreateTime())
                .createTime(platformOrderDTO.getCreateTime())
                .payTime(platformOrderDTO.getPayTime())
                .roleId(platformOrderDTO.getRoleId())
                .roleName(platformOrderDTO.getRoleName())
                .mac(user.getMac())
                .imei(user.getImei())
                .oaid(user.getOaid())
                .androidId(user.getAndroidId())
                .idfa(user.getIdfa())
                .caid(user.getCaid())
                .userActionSetId(agent.getUserActionSetId())
                .build();
    }

    private TtRoleRegisterAppRpcDTO transform(User user, Agent agent, GameUserRole gameUserRole) {
        return TtRoleRegisterAppRpcDTO.builder()
                .gameId(agent.getGameId())
                .userId(user.getId().toString())
                .backPolicyId(agent.getBackPolicyId())
                .accountReport(this.transform(agent))
                .agentKey(agent.getAgentKey())
                .roleId(gameUserRole.getRoleId())
                .roleName(gameUserRole.getRoleName())
                .roleLevel(gameUserRole.getRoleLevel())
                .registerTime(gameUserRole.getCreateTime())
                .mac(user.getMac())
                .imei(user.getImei())
                .oaid(user.getOaid())
                .androidId(user.getAndroidId())
                .idfa(user.getIdfa())
                .caid(user.getCaid())
                .build();
    }

    private TencentRoleRegisterAppRpcDTO transform(Agent agent, User user, GameUserRole gameUserRole) {
        return TencentRoleRegisterAppRpcDTO.builder()
                .gameId(agent.getGameId())
                .userId(user.getId().toString())
                .backPolicyId(agent.getBackPolicyId())
                .accountId(agent.getAccountId())
                .agentKey(agent.getAgentKey())
                .roleId(gameUserRole.getRoleId())
                .roleName(gameUserRole.getRoleName())
                .roleLevel(gameUserRole.getRoleLevel())
                .registerTime(gameUserRole.getCreateTime())
                .mac(user.getMac())
                .imei(user.getImei())
                .oaid(user.getOaid())
                .androidId(user.getAndroidId())
                .idfa(user.getIdfa())
                .caid(user.getCaid())
                .userActionSetId(agent.getUserActionSetId())
                .build();
    }

    private TencentUserAppApiRpcDTO transform(User user, Agent agent, String callbackId) {
        return TencentUserAppApiRpcDTO.builder()
                .backPolicyId(agent.getBackPolicyId())
                .userId(user.getId().toString())
                .gameId(agent.getGameId())
                .adAccountId(agent.getAccountId())
                .registerTime(user.getCreateTime())
                .channel(agent.getAgentKey())
                .mac(user.getMac())
                .imei(user.getImei())
                .oaid(user.getOaid())
                .androidId(user.getAndroidId())
                .idfa(user.getIdfa())
                .caid(user.getCaid())
                .regIp(user.getIp())
                .userActionSetId(agent.getUserActionSetId())
                .callbackId(Strings.isBlank(callbackId) ? null : Long.valueOf(callbackId))
                .build();
    }

    private TencentRoleRegisterAppApiRpcDTO transform(GameUserRole gameUserRole, User user, Agent agent) {
        return TencentRoleRegisterAppApiRpcDTO.builder()
                .backPolicyId(agent.getBackPolicyId())
                .userId(user.getId().toString())
                .gameId(agent.getGameId())
                .adAccountId(agent.getAccountId())
                .registerTime(user.getCreateTime())
                .channel(agent.getAgentKey())
                .roleId(gameUserRole.getRoleId())
                .roleName(gameUserRole.getRoleName())
                .roleLevel(gameUserRole.getRoleLevel())
                .mac(user.getMac())
                .imei(user.getImei())
                .oaid(user.getOaid())
                .androidId(user.getAndroidId())
                .idfa(user.getIdfa())
                .caid(user.getCaid())
                .build();
    }

    private TencentOrderAppApiRpcDTO transform(User user, Agent agent, PlatformOrderDTO platformOrderDTO) {
        return TencentOrderAppApiRpcDTO.builder()
                .backPolicyId(agent.getBackPolicyId())
                .userId(user.getId().toString())
                .gameId(agent.getGameId())
                .adAccountId(agent.getAccountId())
                .registerTime(user.getCreateTime())
                .rechargeMoney(platformOrderDTO.getAmount().multiply(new BigDecimal(100)).longValue())
                .rechargeTime(platformOrderDTO.getCreateTime())
                .orderId(platformOrderDTO.getOrderId())
                .channel(agent.getAgentKey())
                .orderStatus(platformOrderDTO.getStatus())
                .payTime(platformOrderDTO.getPayTime())
                .roleId(platformOrderDTO.getRoleId())
                .roleName(platformOrderDTO.getRoleName())
                .mac(user.getMac())
                .imei(user.getImei())
                .oaid(user.getOaid())
                .androidId(user.getAndroidId())
                .idfa(user.getIdfa())
                .caid(user.getCaid())
                .build();

    }
}
