package com.zanxiang.game.module.sdk.listener;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.zanxiang.game.module.mybatis.entity.GameUserRole;
import com.zanxiang.game.module.sdk.service.IGameUserRoleService;
import com.zanxiang.game.module.sdk.service.IRoleOperateService;
import com.zanxiang.module.util.JsonUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.event.EventListener;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Component;

/**
 * @author : lingfeng
 * @time : 2024-06-05
 * @description : 游戏角色创建监听
 */
@Slf4j
@Component
public class GameRoleCreateListener {

    @Autowired
    private IRoleOperateService roleOperateService;

    @Autowired
    private IGameUserRoleService gameUserRoleService;

    @Async
    @EventListener
    public void onApplicationEvent(GameRoleCreateEvent event) {
        try {
            //查询角色信息
            GameUserRole gameUserRole = gameUserRoleService.getOne(new LambdaQueryWrapper<GameUserRole>()
                    .eq(GameUserRole::getGameId, event.getGameId())
                    .eq(GameUserRole::getRoleId, event.getRoleId())
                    .eq(GameUserRole::getUserId, event.getUserId()));
            if (gameUserRole == null) {
                log.error("角色创建事 : 查询角色信息为空, event : {}", JsonUtil.toString(event));
                return;
            }
            log.error("创角事件消费 gameId : {}, roleId : {}", gameUserRole.getGameId(), gameUserRole.getRoleId());
            //创角成功, 添加角色操作记录
            roleOperateService.systemRoleOperate(gameUserRole);
        } catch (Exception e) {
            log.error("创角事件消费异常, event : {}, e : {}", JsonUtil.toString(event), e.getMessage(), e);
        }
    }
}
