package com.zanxiang.game.back.serve.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.zanxiang.game.back.serve.dao.mapper.GameOceanengineAppRoleRegisterLogMapper;
import com.zanxiang.game.back.serve.oceanengine.MiniGameCallback;
import com.zanxiang.game.back.serve.pojo.entity.GameBackPolicy;
import com.zanxiang.game.back.serve.pojo.entity.GameOceanengineAppBackLog;
import com.zanxiang.game.back.serve.pojo.entity.GameOceanengineAppRoleRegisterLog;
import com.zanxiang.game.back.serve.pojo.entity.GameOceanengineAppUserLog;
import com.zanxiang.game.back.serve.pojo.enums.BackStatusEnum;
import com.zanxiang.game.back.serve.service.*;
import com.zanxiang.game.module.base.ServerInfo;
import com.zanxiang.game.module.base.rpc.IAgentRpc;
import lombok.extern.slf4j.Slf4j;
import org.apache.dubbo.config.annotation.DubboReference;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDateTime;
import java.util.Objects;

@Slf4j
@Service
public class GameOceanengineAppRoleRegisterLogServiceImpl extends ServiceImpl<GameOceanengineAppRoleRegisterLogMapper, GameOceanengineAppRoleRegisterLog>
        implements IGameOceanengineAppRoleRegisterLogService {
    @Autowired
    private IGameOceanengineAppUserLogService gameOceanengineAppUserLogService;
    @Autowired
    private IGameOceanengineAppBackLogService gameOceanengineAppBackLogService;
    @DubboReference(providedBy = ServerInfo.SERVER_DUBBO_NAME)
    private IAgentRpc agentRpc;
    @Autowired
    private IGameBackPolicyService gameBackPolicyService;
    @Autowired
    private IGameOceanengineAppCallbackXgltService gameOceanengineAppCallbackXgltService;

    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean callback(GameOceanengineAppRoleRegisterLog roleRegisterLog) {
        BackStatusEnum backStatus = doCallback(roleRegisterLog);

        ///星广联投回传 -> 注册
        if (Objects.equals(backStatus, BackStatusEnum.SUCCESS)) {
            try {
                gameOceanengineAppCallbackXgltService.createRoleBack(roleRegisterLog);
            } catch (Exception ignored) {
            }
        }

        return update(new LambdaUpdateWrapper<GameOceanengineAppRoleRegisterLog>()
                .set(GameOceanengineAppRoleRegisterLog::getBackStatus, backStatus.getBackStatus())
                .eq(GameOceanengineAppRoleRegisterLog::getId, roleRegisterLog.getId())
        );
    }

    private BackStatusEnum doCallback(GameOceanengineAppRoleRegisterLog roleRegisterLog) {
        GameOceanengineAppUserLog userLog = gameOceanengineAppUserLogService.getOne(new LambdaQueryWrapper<GameOceanengineAppUserLog>()
                .eq(GameOceanengineAppUserLog::getGameId, roleRegisterLog.getGameId())
                .eq(GameOceanengineAppUserLog::getUserId, roleRegisterLog.getUserId())
                .eq(GameOceanengineAppUserLog::getAccountId, roleRegisterLog.getAccountId())
                .orderByDesc(GameOceanengineAppUserLog::getCreateTime)
                .last("limit 1")
        );
        if (userLog == null) {
            gameOceanengineAppBackLogService.save(GameOceanengineAppBackLog.builder()
                    .userId(roleRegisterLog.getUserId())
                    .mac(roleRegisterLog.getMac())
                    .imei(roleRegisterLog.getImei())
                    .oaid(roleRegisterLog.getOaid())
                    .androidId(roleRegisterLog.getAndroidId())
                    .idfa(roleRegisterLog.getIdfa())
                    .caid(roleRegisterLog.getCaid())
                    .gameId(roleRegisterLog.getGameId())
                    .accountId(roleRegisterLog.getAccountId())
                    .eventType(String.valueOf(MiniGameCallback.EventType.ACTIVE_REGISTER.getEventType()))
                    .eventLogId(roleRegisterLog.getId())
                    .createTime(LocalDateTime.now())
                    .backStatus(BackStatusEnum.FAILED.getBackStatus())
                    .errMsg("找不到用户注册信息")
                    .build());
            return BackStatusEnum.FAILED;
        }
        //判断创角是否已回传
        if (gameOceanengineAppBackLogService.count(new LambdaQueryWrapper<GameOceanengineAppBackLog>()
                .eq(GameOceanengineAppBackLog::getGameId, userLog.getGameId())
                .eq(GameOceanengineAppBackLog::getUserId, userLog.getUserId())
                .eq(GameOceanengineAppBackLog::getAccountId, userLog.getAccountId())
                .eq(GameOceanengineAppBackLog::getEventType, MiniGameCallback.EventType.ACTIVE_REGISTER.getEventType())
                .eq(GameOceanengineAppBackLog::getBackStatus, BackStatusEnum.SUCCESS.getBackStatus())
        ) > 0) {
            //创角已回传
            return BackStatusEnum.NO;
        }

        if (roleRegisterLog.getBackPolicyId() != null) {
            GameBackPolicy gameBackPolicy = gameBackPolicyService.getById(roleRegisterLog.getBackPolicyId());
            if (gameBackPolicy.getRoleLevelMin() != null && roleRegisterLog.getRoleLevel() != null
                    && roleRegisterLog.getRoleLevel() < gameBackPolicy.getRoleLevelMin()) {
                return BackStatusEnum.NO;
            }
        }
        BackStatusEnum backStatus = BackStatusEnum.SUCCESS;
        String errMsg = null;
        gameOceanengineAppBackLogService.save(GameOceanengineAppBackLog.builder()
                .userId(roleRegisterLog.getUserId())
                .mac(roleRegisterLog.getMac())
                .imei(roleRegisterLog.getImei())
                .oaid(roleRegisterLog.getOaid())
                .androidId(roleRegisterLog.getAndroidId())
                .idfa(roleRegisterLog.getIdfa())
                .caid(roleRegisterLog.getCaid())
                .gameId(roleRegisterLog.getGameId())
                .accountId(roleRegisterLog.getAccountId())
                .eventType(String.valueOf(MiniGameCallback.EventType.ACTIVE_REGISTER.getEventType()))
                .eventLogId(roleRegisterLog.getId())
                .createTime(LocalDateTime.now())
                .backStatus(backStatus.getBackStatus())
                .errMsg(errMsg)
                .build());
        return backStatus;
    }
}
