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.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.zanxiang.erp.base.ErpServer;
import com.zanxiang.erp.base.rpc.ISysUserRpc;
import com.zanxiang.erp.security.util.SecurityUtil;
import com.zanxiang.game.module.manage.pojo.dto.GameDTO;
import com.zanxiang.game.module.manage.pojo.params.GameGiftAddParam;
import com.zanxiang.game.module.manage.pojo.params.GameGiftListParam;
import com.zanxiang.game.module.manage.pojo.vo.GameGiftListVO;
import com.zanxiang.game.module.manage.service.IGameGiftService;
import com.zanxiang.game.module.manage.service.IGameService;
import com.zanxiang.game.module.manage.service.IGameSupperService;
import com.zanxiang.game.module.mybatis.entity.GameGift;
import com.zanxiang.game.module.mybatis.entity.GameSupper;
import com.zanxiang.game.module.mybatis.mapper.GameGiftMapper;
import com.zanxiang.module.util.exception.BaseException;
import org.apache.dubbo.config.annotation.DubboReference;
import org.springframework.beans.factory.annotation.Autowired;
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 GameGiftServiceImpl extends ServiceImpl<GameGiftMapper, GameGift> implements IGameGiftService {
    @Autowired
    private IGameService gameService;
    @Autowired
    private IGameSupperService gameSupperService;
    @DubboReference(providedBy = ErpServer.SERVER_DUBBO_NAME)
    private ISysUserRpc sysUserRpc;

    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean addOrUpdate(GameGiftAddParam param) {
        Long sysUserId = SecurityUtil.getUserId();
        LocalDateTime now = LocalDateTime.now();
        if (null == param.getId()) {
            long count = count(new LambdaQueryWrapper<GameGift>()
                    .eq(GameGift::getParentGameId, param.getParentGameId())
                    .eq(GameGift::getSuperGameId, param.getSuperGameId())
                    .eq(GameGift::getGiftName, param.getGiftName()));
            if (count > 1) {
                throw new BaseException("此游戏礼包名称重复");
            }
            GameGift gameGift = GameGift.builder()
                    .parentGameId(param.getParentGameId())
                    .superGameId(param.getSuperGameId())
                    .giftName(param.getGiftName())
                    .createBy(sysUserId)
                    .createTime(now)
                    .updateBy(sysUserId)
                    .updateTime(now)
                    .build();
            return save(gameGift);
        } else {
            long count = count(new LambdaQueryWrapper<GameGift>()
                    .ne(GameGift::getId, param.getId())
                    .eq(GameGift::getParentGameId, param.getParentGameId())
                    .eq(GameGift::getSuperGameId, param.getSuperGameId())
                    .eq(GameGift::getGiftName, param.getGiftName()));
            if (count > 1) {
                throw new BaseException("此游戏礼包名称重复");
            }
            GameGift gameGift = GameGift.builder()
                    .id(param.getId())
                    .parentGameId(param.getParentGameId())
                    .superGameId(param.getSuperGameId())
                    .giftName(param.getGiftName())
                    .updateBy(sysUserId)
                    .updateTime(now)
                    .build();
            return updateById(gameGift);
        }
    }

    @Override
    public List<GameGiftListVO> queryAll(GameGiftListParam param) {
        return list(new LambdaQueryWrapper<GameGift>()
                .eq(null != param.getSuperGameId(), GameGift::getSuperGameId, param.getSuperGameId())
                .eq(null != param.getParentGameId(), GameGift::getParentGameId, param.getParentGameId())
                .like(StringUtils.isNotBlank(param.getGiftName()), GameGift::getGiftName, param.getGiftName())
                .eq(GameGift::getIsDelete, 0))
                .stream()
                .map(item -> {
                    GameDTO game = gameService.getById(item.getParentGameId());
                    GameSupper gameSupper = gameSupperService.getById(item.getSuperGameId());
                    return GameGiftListVO.builder()
                            .id(item.getId())
                            .superGameId(item.getSuperGameId())
                            .superGameName(null == gameSupper ? null : gameSupper.getName())
                            .parentGameId(item.getParentGameId())
                            .parentGameName(null == game ? null : game.getName())
                            .giftName(item.getGiftName())
                            .createBy(item.getCreateBy())
                            .createName(sysUserRpc.getById(item.getCreateBy()).getData().getNickname())
                            .createTime(item.getCreateTime())
                            .updateBy(item.getUpdateBy())
                            .updateName(sysUserRpc.getById(item.getUpdateBy()).getData().getNickname())
                            .updateTime(item.getUpdateTime())
                            .build();
                }).collect(Collectors.toList());
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean deleteByIds(Long ids) {
        Long sysUserId = SecurityUtil.getUserId();
        return update(new LambdaUpdateWrapper<GameGift>()
                .set(GameGift::getIsDelete, 1)
                .set(GameGift::getUpdateBy, sysUserId)
                .set(GameGift::getUpdateTime, LocalDateTime.now())
                .in(GameGift::getId, ids));
    }

    @Override
    public IPage<GameGiftListVO> listOfPage(GameGiftListParam param) {
        return page(param.toPage(), new LambdaQueryWrapper<GameGift>()
                .eq(null != param.getSuperGameId(), GameGift::getSuperGameId, param.getSuperGameId())
                .eq(null != param.getParentGameId(), GameGift::getParentGameId, param.getParentGameId())
                .like(StringUtils.isNotBlank(param.getGiftName()), GameGift::getGiftName, param.getGiftName())
                .eq(GameGift::getIsDelete, 0))
                .convert(item -> {
                    GameDTO game = gameService.getById(item.getParentGameId());
                    GameSupper gameSupper = gameSupperService.getById(item.getSuperGameId());
                    return GameGiftListVO.builder()
                            .id(item.getId())
                            .superGameId(item.getSuperGameId())
                            .superGameName(null == gameSupper ? null : gameSupper.getName())
                            .parentGameId(item.getParentGameId())
                            .parentGameName(null == game ? null : game.getName())
                            .giftName(item.getGiftName())
                            .createBy(item.getCreateBy())
                            .createName(sysUserRpc.getById(item.getCreateBy()).getData().getNickname())
                            .createTime(item.getCreateTime())
                            .updateBy(item.getUpdateBy())
                            .updateName(sysUserRpc.getById(item.getUpdateBy()).getData().getNickname())
                            .updateTime(item.getUpdateTime())
                            .build();
                });
    }
}
