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

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.core.toolkit.CollectionUtils;
import com.zanxiang.erp.security.util.SecurityUtil;
import com.zanxiang.game.module.manage.pojo.params.RoleOperateParam;
import com.zanxiang.game.module.mybatis.entity.RoleGiftRecord;
import com.zanxiang.game.module.mybatis.entity.RoleOperate;
import com.zanxiang.game.module.mybatis.mapper.RoleOperateMapper;
import com.zanxiang.game.module.manage.service.IRoleOperateService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
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 {

    @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.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.setUpdateBy(sysUserId);
                    roleOperate.setUpdateTime(now);
                    return roleOperate;
                }).collect(Collectors.toList());
        return saveOrUpdateBatch(roleOperateList);
    }
}
