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

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.zanxiang.erp.security.util.SecurityUtil;
import com.zanxiang.game.module.manage.pojo.params.RoleOperateParam;
import com.zanxiang.game.module.manage.service.IRoleOperateService;
import com.zanxiang.game.module.mybatis.entity.GameGiftPackLinkLog;
import com.zanxiang.game.module.mybatis.entity.RoleOperate;
import com.zanxiang.game.module.mybatis.mapper.RoleOperateMapper;
import org.apache.commons.lang3.StringUtils;
import org.apache.logging.log4j.util.Strings;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDateTime;
import java.util.List;
import java.util.stream.Collectors;

/**
 * <p>
 * 角色操作表 服务实现类
 * </p>
 *
 * @author auto
 * @since 2023-09-09
 */
@Service
public class RoleOperateServiceImpl extends ServiceImpl<RoleOperateMapper, RoleOperate> implements IRoleOperateService {

    @Async
    @Override
    public void roleOperateUpdate(String userPhone, GameGiftPackLinkLog gameGiftPackLinkLog) {
        RoleOperate roleOperate = super.getOne(new LambdaQueryWrapper<RoleOperate>()
                .eq(RoleOperate::getGameId, gameGiftPackLinkLog.getGameId())
                .eq(RoleOperate::getServerId, gameGiftPackLinkLog.getServerId())
                .eq(RoleOperate::getUserId, gameGiftPackLinkLog.getUserId())
                .eq(RoleOperate::getRoleId, gameGiftPackLinkLog.getRoleId()));
        //不存在数据, 直接保存
        if (roleOperate == null) {
            super.save(this.transform(userPhone, gameGiftPackLinkLog));
            return;
        }
        //更新玩家企微信息
        if (StringUtils.isNoneBlank(gameGiftPackLinkLog.getCorpId(), gameGiftPackLinkLog.getExternalUserId(), gameGiftPackLinkLog.getCorpUserId())
                && StringUtils.isAllBlank(roleOperate.getCorpId(), roleOperate.getExternalUserId(), roleOperate.getAddCorpUserId())) {
            roleOperate.setCorpId(gameGiftPackLinkLog.getCorpId());
            roleOperate.setExternalUserId(gameGiftPackLinkLog.getExternalUserId());
            roleOperate.setAddCorpUserId(gameGiftPackLinkLog.getCorpUserId());
            roleOperate.setIsAddCorpWechat(1);
        }
        //更新玩家手机号
        if (Strings.isNotBlank(userPhone) && Strings.isBlank(roleOperate.getUserPhone())) {
            roleOperate.setUserPhone(userPhone);
        }
        roleOperate.setUpdateBy(RoleOperate.SYSTEM_OPERATE);
        roleOperate.setUpdateTime(LocalDateTime.now());
        super.updateById(roleOperate);
    }

    private RoleOperate transform(String userPhone, GameGiftPackLinkLog gameGiftPackLinkLog) {
        RoleOperate roleOperate = RoleOperate.builder()
                .gameId(gameGiftPackLinkLog.getGameId())
                .serverId(gameGiftPackLinkLog.getServerId())
                .userId(gameGiftPackLinkLog.getUserId())
                .roleId(gameGiftPackLinkLog.getRoleId())
                .corpId(gameGiftPackLinkLog.getCorpId())
                .externalUserId(gameGiftPackLinkLog.getExternalUserId())
                .addCorpUserId(gameGiftPackLinkLog.getCorpUserId())
                .userPhone(userPhone)
                .createBy(RoleOperate.SYSTEM_OPERATE)
                .createTime(LocalDateTime.now())
                .updateBy(RoleOperate.SYSTEM_OPERATE)
                .updateTime(LocalDateTime.now())
                .build();
        if (StringUtils.isNoneBlank(roleOperate.getCorpId(), roleOperate.getExternalUserId(), roleOperate.getAddCorpUserId())) {
            roleOperate.setIsAddCorpWechat(1);
        }
        return roleOperate;
    }

    @Override
    public boolean userAddCorpWechat(Long gameId, Long userId) {
        return super.count(new LambdaQueryWrapper<RoleOperate>()
                .eq(RoleOperate::getGameId, gameId)
                .eq(RoleOperate::getUserId, userId)
                .eq(RoleOperate::getIsAddCorpWechat, 1)
        ) > 0;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean modify(RoleOperateParam param) {
        Long sysUserId = SecurityUtil.getUserId();
        LocalDateTime now = LocalDateTime.now();
        List<RoleOperate> roleOperateList = param.getRoleInfoList().stream()
                .map(item -> {
                    RoleOperate roleOperate = RoleOperate.builder().build();
                    RoleOperate queryOne = getOne(new LambdaQueryWrapper<RoleOperate>()
                            .eq(RoleOperate::getGameId, item.getGameId())
                            .eq(RoleOperate::getServerId, item.getServerId())
                            .eq(RoleOperate::getUserId, item.getUserId())
                            .eq(RoleOperate::getRoleId, item.getRoleId())
                            .eq(RoleOperate::getIsDelete, 0));
                    if (null == queryOne) {
                        roleOperate.setCreateBy(sysUserId);
                        roleOperate.setCreateTime(now);
                    } else {
                        roleOperate = queryOne;
                    }
                    roleOperate.setGameId(item.getGameId());
                    roleOperate.setServerId(item.getServerId());
                    roleOperate.setUserId(item.getUserId());
                    roleOperate.setRoleId(item.getRoleId());
                    roleOperate.setIsChangeGameType(param.getIsChangeGameType());
                    roleOperate.setIsAddCorpWechat(param.getIsAddCorpWechat());
                    roleOperate.setCorpId(param.getCorpId());
                    roleOperate.setExternalUserId(param.getExternalUserId());
                    roleOperate.setAddCorpUserId(param.getAddCorpUserId());
                    roleOperate.setUserWechat(param.getUserWechat());
                    roleOperate.setUserPhone(param.getUserPhone());
                    roleOperate.setIsRemoveGame(param.getIsRemoveGame());
                    roleOperate.setIsWakeUp(param.getIsWakeUp());
                    roleOperate.setRemark(param.getRemark());
                    roleOperate.setPutUserId(param.getPutUserId());
                    roleOperate.setGsAccount(param.getGsAccount());
                    roleOperate.setUpdateBy(sysUserId);
                    roleOperate.setUpdateTime(now);
                    return roleOperate;
                }).collect(Collectors.toList());
        return saveOrUpdateBatch(roleOperateList);
    }
}
