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.mybatis.entity.PayMerchant;
import com.zanxiang.game.module.mybatis.mapper.PayMerchantMapper;
import com.zanxiang.game.module.sdk.pojo.dto.PayMerchantDTO;
import com.zanxiang.game.module.sdk.service.IPayMerchantService;
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-06-16 19:04
 */
@Slf4j
@Service
public class PayMerchantServiceImpl extends ServiceImpl<PayMerchantMapper, PayMerchant> implements IPayMerchantService {

    @Override
    public PayMerchantDTO getByMerchantNo(String merchantNo) {
        PayMerchant payMerchant = super.getOne(new LambdaQueryWrapper<PayMerchant>()
                .eq(PayMerchant::getMerchantNo, merchantNo));
        if (payMerchant == null) {
            log.error("参数错误, 商户信息不存在, merchantNo : {}", merchantNo);
            throw new BaseException("参数错误, 商户信息不存在");
        }
        return BeanUtil.copy(payMerchant, PayMerchantDTO.class);
    }

    @Override
    public List<PayMerchantDTO> getByMerchantNos(List<String> merchantNoList) {
        if (CollectionUtils.isEmpty(merchantNoList)) {
            log.error("参数错误, 商户号列表不存在");
            throw new BaseException("参数错误, 商户id列表不存在");
        }
        List<PayMerchant> payMerchantList = super.list(new LambdaQueryWrapper<PayMerchant>()
                .in(PayMerchant::getMerchantNo, merchantNoList)
                .eq(PayMerchant::getMaxPayLock, 0));
        if (CollectionUtils.isEmpty(payMerchantList)) {
            log.error("参数错误, 符合条件的商户不存在, ids : {}", merchantNoList);
            throw new BaseException("参数错误, 符合条件的商户不存在");
        }
        return payMerchantList.stream().map(payMerchant -> BeanUtil.copy(payMerchant, PayMerchantDTO.class))
                .collect(Collectors.toList());
    }
}
