package com.red.circle.order.infra.database.rds.service.pay.impl;

import com.google.api.client.util.Lists;
import com.google.common.collect.Maps;
import com.red.circle.framework.core.asserts.ResponseAssert;
import com.red.circle.framework.core.response.CommonErrorCode;
import com.red.circle.framework.dto.PageResult;
import com.red.circle.framework.mybatis.service.impl.BaseServiceImpl;
import com.red.circle.order.infra.database.rds.dao.pay.PayChannelDAO;
import com.red.circle.order.infra.database.rds.entity.pay.PayChannel;
import com.red.circle.order.infra.database.rds.query.SysPayChannelQry;
import com.red.circle.order.infra.database.rds.service.pay.PayChannelService;
import com.red.circle.tool.core.collection.CollectionUtils;
import com.red.circle.tool.core.text.StringUtils;
import java.util.List;
import java.util.Map;
import java.util.Optional;
import java.util.Set;
import java.util.stream.Collectors;
import lombok.RequiredArgsConstructor;
import org.springframework.dao.DuplicateKeyException;
import org.springframework.stereotype.Service;

/**
 * <p>
 * 支付渠道 服务实现类.
 * </p>
 *
 * @author pengshigang
 * @since 2021-12-08
 */
@Service
@RequiredArgsConstructor
public class PayChannelServiceImpl extends BaseServiceImpl<PayChannelDAO, PayChannel> implements
    PayChannelService {


  @Override
  public List<PayChannel> listByChannelCodes(Set<String> codes) {
    if (CollectionUtils.isEmpty(codes)) {
      return CollectionUtils.newArrayList();
    }
    return query()
        .in(PayChannel::getChannelCode, codes)
        .list();
  }

  @Override
  public PageResult<PayChannel> pageChannel(SysPayChannelQry query) {
    return query()
        .eq(StringUtils.isNotBlank(query.getChannelCode()), PayChannel::getChannelCode,
            query.getChannelCode())
        .eq(StringUtils.isNotBlank(query.getChannelType()), PayChannel::getChannelType,
            query.getChannelType())
        .like(StringUtils.isNotBlank(query.getChannelName()), PayChannel::getChannelName,
            query.getChannelName())
        .orderByDesc(PayChannel::getCreateTime)
        .page(query.getPageQuery());
  }

  @Override
  public List<PayChannel> listChannel() {
    return query()
        .orderByDesc(PayChannel::getCreateTime)
        .list();
  }

  @Override
  public void addChannel(PayChannel param) {
    try {
      save(param);
    } catch (DuplicateKeyException ignore) {
      ResponseAssert.failure(CommonErrorCode.INFORMATION_EXISTS);
    }
  }

  @Override
  public void updateChannel(PayChannel param) {
    updateSelectiveById(param);
  }

  @Override
  public Map<String, PayChannel> mapByCodes(Set<String> codes) {
    if (CollectionUtils.isEmpty(codes)) {
      return Maps.newHashMap();
    }
    return Optional.ofNullable(listByCodes(codes))
        .map(sysPayChannels -> sysPayChannels.stream()
            .collect(Collectors.toMap(PayChannel::getChannelCode, v -> v)))
        .orElseGet(Maps::newHashMap);
  }

  @Override
  public List<PayChannel> listByCodes(Set<String> codes) {
    if (CollectionUtils.isEmpty(codes)) {
      return Lists.newArrayList();
    }
    return Optional.ofNullable(query()
            .in(PayChannel::getChannelCode, codes)
            .orderByDesc(PayChannel::getCreateTime)
            .list())
        .orElseGet(Lists::newArrayList);
  }
}
