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.GameTencentAppRoleRegisterMapper;
import com.zanxiang.game.back.serve.oceanengine.MiniGameCallback;
import com.zanxiang.game.back.serve.pojo.entity.GameTencentAppBackLog;
import com.zanxiang.game.back.serve.pojo.entity.GameTencentAppRoleRegister;
import com.zanxiang.game.back.serve.pojo.entity.GameTencentAppUser;
import com.zanxiang.game.back.serve.pojo.enums.BackStatusEnum;
import com.zanxiang.game.back.serve.service.IGameTencentAppBackLogService;
import com.zanxiang.game.back.serve.service.IGameTencentAppRoleRegisterService;
import com.zanxiang.game.back.serve.service.IGameTencentAppUserService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDateTime;

@Slf4j
@Service
public class GameTencentAppRoleRegisterServiceImpl extends ServiceImpl<GameTencentAppRoleRegisterMapper, GameTencentAppRoleRegister>
        implements IGameTencentAppRoleRegisterService {

    @Autowired
    private IGameTencentAppUserService gameTencentAppUserService;

    @Autowired
    private IGameTencentAppBackLogService gameTencentAppBackLogService;

    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean callback(GameTencentAppRoleRegister roleRegisterLog) {
        BackStatusEnum backStatus = doCallback(roleRegisterLog);
        return update(new LambdaUpdateWrapper<GameTencentAppRoleRegister>()
                .set(GameTencentAppRoleRegister::getBackStatus, backStatus.getBackStatus())
                .eq(GameTencentAppRoleRegister::getId, roleRegisterLog.getId())
        );
    }

    private BackStatusEnum doCallback(GameTencentAppRoleRegister roleRegisterLog) {
        GameTencentAppUser userLog = gameTencentAppUserService.getOne(new LambdaQueryWrapper<GameTencentAppUser>()
                .eq(GameTencentAppUser::getGameId, roleRegisterLog.getGameId())
                .eq(GameTencentAppUser::getUserId, roleRegisterLog.getUserId())
                .eq(GameTencentAppUser::getAccountId, roleRegisterLog.getAccountId())
                .orderByDesc(GameTencentAppUser::getCreateTime)
                .last("limit 1")
        );
        if (userLog == null) {
            gameTencentAppBackLogService.save(GameTencentAppBackLog.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 (gameTencentAppBackLogService.count(new LambdaQueryWrapper<GameTencentAppBackLog>()
                .eq(GameTencentAppBackLog::getGameId, userLog.getGameId())
                .eq(GameTencentAppBackLog::getUserId, userLog.getUserId())
                .eq(GameTencentAppBackLog::getAccountId, userLog.getAccountId())
                .eq(GameTencentAppBackLog::getEventType, MiniGameCallback.EventType.ACTIVE_REGISTER.getEventType())
                .eq(GameTencentAppBackLog::getBackStatus, BackStatusEnum.SUCCESS.getBackStatus())
        ) > 0) {
            //创角已回传
            return BackStatusEnum.NO;
        }
        BackStatusEnum backStatus = BackStatusEnum.SUCCESS;
        String errMsg = null;
        gameTencentAppBackLogService.save(GameTencentAppBackLog.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;
    }
}
