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

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.zanxiang.game.module.mybatis.entity.GameUserRole;
import com.zanxiang.game.module.mybatis.entity.RoleOperate;
import com.zanxiang.game.module.mybatis.entity.User;
import com.zanxiang.game.module.mybatis.mapper.RoleOperateMapper;
import com.zanxiang.game.module.sdk.service.IAgentService;
import com.zanxiang.game.module.sdk.service.IRoleOperateService;
import com.zanxiang.game.module.sdk.service.IUserService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.Map;

/**
 * @author : lingfeng
 * @time : 2023-09-09
 * @description : 角色操作表
 */
@Slf4j
@Service
public class RoleOperateServiceImpl extends ServiceImpl<RoleOperateMapper, RoleOperate> implements IRoleOperateService {

    @Autowired
    private IUserService userService;

    @Autowired
    private IAgentService agentService;

    @Override
    public void systemRoleOperate(GameUserRole gameUserRole) {
        try {
            //查询玩家信息
            User user = userService.getById(gameUserRole.getUserId());
            //解析玩家渠道信息
            Map<String, String> channelMap = agentService.channelTransform(user.getChannel());
            if (!channelMap.containsKey("corpId") || !channelMap.containsKey("corpUserId")
                    || !channelMap.containsKey("externalUserId")) {
                log.error("创角非企微链路, 不做操作! gameId : {}, roleId : {}", gameUserRole.getGameId(), gameUserRole.getRoleId());
                return;
            }
            //查询操作记录
            RoleOperate roleOperate = super.getOne(new LambdaQueryWrapper<RoleOperate>()
                    .eq(RoleOperate::getGameId, gameUserRole.getGameId())
                    .eq(RoleOperate::getServerId, gameUserRole.getServerId())
                    .eq(RoleOperate::getUserId, gameUserRole.getUserId())
                    .eq(RoleOperate::getRoleId, gameUserRole.getRoleId()));
            if (roleOperate == null) {
                roleOperate = this.transform(gameUserRole, channelMap);
            } else {
                roleOperate.setCorpId(channelMap.get("corpId"));
                roleOperate.setExternalUserId(channelMap.get("externalUserId"));
                roleOperate.setAddCorpUserId(channelMap.get("corpUserId"));
                roleOperate.setIsAddCorpWechat(1);
                roleOperate.setUpdateBy(RoleOperate.SYSTEM_OPERATE);
                roleOperate.setUpdateTime(LocalDateTime.now());
            }
            super.saveOrUpdate(roleOperate);
        } catch (Exception e) {
            log.error("创角添加记录异常! gameId : {}, roleId : {}", gameUserRole.getGameId(), gameUserRole.getRoleId());
        }
    }

    private RoleOperate transform(GameUserRole gameUserRole, Map<String, String> channelMap) {
        return RoleOperate.builder()
                .gameId(gameUserRole.getGameId())
                .serverId(gameUserRole.getServerId())
                .userId(gameUserRole.getUserId())
                .roleId(gameUserRole.getRoleId())
                .corpId(channelMap.get("corpId"))
                .externalUserId(channelMap.get("externalUserId"))
                .addCorpUserId(channelMap.get("corpUserId"))
                .isAddCorpWechat(1)
                .createBy(RoleOperate.SYSTEM_OPERATE)
                .createTime(LocalDateTime.now())
                .updateBy(RoleOperate.SYSTEM_OPERATE)
                .updateTime(LocalDateTime.now())
                .build();
    }
}
