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

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.CollectionUtils;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.zanxiang.game.module.base.pojo.enums.DeleteEnum;
import com.zanxiang.game.module.base.pojo.enums.StatusEnum;
import com.zanxiang.game.module.mybatis.entity.GamePayWay;
import com.zanxiang.game.module.mybatis.mapper.GamePayWayMapper;
import com.zanxiang.game.module.sdk.pojo.dto.GamePayWayDTO;
import com.zanxiang.game.module.sdk.service.IGamePayWayService;
import com.zanxiang.module.util.bean.BeanUtil;
import com.zanxiang.module.util.exception.BaseException;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

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

/**
 * @author xufeng
 * @date 2022/6/13 11:46
 */
@Slf4j
@Service
public class GamePayWayServiceImpl extends ServiceImpl<GamePayWayMapper, GamePayWay> implements IGamePayWayService {

    @Override
    public GamePayWayDTO getById(Long id) {
        GamePayWay gamePayWay = super.getById(id);
        if (gamePayWay == null) {
            log.error("参数错误, 游戏支付配置信息不存在, id : {}", id);
            throw new BaseException("参数错误, 游戏支付配置信息不存在");
        }
        return BeanUtil.copy(gamePayWay, GamePayWayDTO.class);
    }

    @Override
    public GamePayWayDTO getGamePayWay(Long gameId, Long payWayId, Long payDeviceId) {
        GamePayWay gamePayWay = getOne(new LambdaQueryWrapper<GamePayWay>()
                .eq(GamePayWay::getGameId, gameId)
                .eq(GamePayWay::getPayWayId, payWayId)
                .eq(GamePayWay::getPayDeviceId, payDeviceId)
                .eq(GamePayWay::getIsDelete, DeleteEnum.NO.getCode())
                .eq(GamePayWay::getStatus, StatusEnum.YES.getCode()));
        if (gamePayWay == null) {
            log.error("参数错误, 游戏支付配置信息不存在, gameId : {}, payWayId : {}, payDeviceId : {}", gameId, payWayId, payDeviceId);
            throw new BaseException("参数错误, 游戏支付配置信息不存在");
        }
        return BeanUtil.copy(gamePayWay, GamePayWayDTO.class);
    }

    @Override
    public GamePayWayDTO closeOrderGetGamePayWay(Long gameId, Long payWayId, Long payDeviceId) {
        GamePayWay gamePayWay = getOne(new LambdaQueryWrapper<GamePayWay>()
                .eq(GamePayWay::getGameId, gameId)
                .eq(GamePayWay::getPayWayId, payWayId)
                .eq(GamePayWay::getPayDeviceId, payDeviceId));
        if (gamePayWay == null) {
            log.error("参数错误, 关闭订单游戏支付配置信息不存在, gameId : {}, payWayId : {}, payDeviceId : {}", gameId, payWayId, payDeviceId);
            throw new BaseException("参数错误, 关闭订单游戏支付配置信息不存在");
        }
        return BeanUtil.copy(gamePayWay, GamePayWayDTO.class);
    }

    @Override
    public List<GamePayWayDTO> listOfGameId(Long gameId) {
        List<GamePayWay> gamePayWayList = list(new LambdaQueryWrapper<GamePayWay>()
                .eq(GamePayWay::getGameId, gameId)
                .eq(GamePayWay::getStatus, StatusEnum.YES)
        );
        if (CollectionUtils.isEmpty(gamePayWayList)) {
            log.error("参数错误, 游戏支付配置信息不存在, gameId : {}", gameId);
            throw new BaseException("参数错误, 游戏支付配置信息不存在");
        }
        return gamePayWayList.stream()
                .map(gamePayWay -> BeanUtil.copy(gamePayWay, GamePayWayDTO.class))
                .collect(Collectors.toList());
    }
}
