package com.red.circle.other.app.command.sys.query;

import cn.hutool.core.collection.CollectionUtil;
import com.google.common.collect.Lists;
import com.red.circle.other.app.dto.clientobject.sys.CountryCodeCO;
import com.red.circle.other.infra.database.rds.entity.sys.SysCountryCode;
import com.red.circle.other.infra.database.rds.service.sys.SysCountryCodeService;

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

import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Component;

/**
 * 获取开放国家列表置顶top6.
 *
 * @author pengliang on 2021/3/10
 */
@Component
@RequiredArgsConstructor
public class SysCountryOpenTopQryExe {

  private final SysCountryCodeService sysCountryCodeService;

  public List<CountryCodeCO> execute() {
    List<SysCountryCode> list = sysCountryCodeService.listOpenCountryTop(7);
    if (CollectionUtil.isEmpty(list)) {
      return Lists.newArrayList();
    }
    return list.stream().map(this::sysCountryCodeToCountryCodeCO)
        .filter(Objects::nonNull).collect(Collectors.toList());
  }

  private CountryCodeCO sysCountryCodeToCountryCodeCO(SysCountryCode sysCountryCode) {
    if (Objects.isNull(sysCountryCode)) {
      return null;
    }
    return new CountryCodeCO()
        .setId(sysCountryCode.getId())
        .setAlphaTwo(sysCountryCode.getAlphaTwo())
        .setAlphaThree(sysCountryCode.getAlphaThree())
        .setCountryName(sysCountryCode.getCountryName())
        .setAliasName(sysCountryCode.getAliasName())
        .setNationalFlag(sysCountryCode.getNationalFlag())
        .setPhonePrefix(sysCountryCode.getPhonePrefix())
        .setOpen(sysCountryCode.getOpen())
        .setTop(sysCountryCode.getTop());
  }

}
