package org.jeecg.modules.adweb.userCountry.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import jakarta.annotation.Resource;
import org.apache.commons.collections4.CollectionUtils;
import org.jeecg.modules.adweb.userCountry.entity.AdwebCountry;
import org.jeecg.modules.adweb.userCountry.entity.AdwebUserCountry;
import org.jeecg.modules.adweb.userCountry.mapper.AdwebUserCountryMapper;
import org.jeecg.modules.adweb.userCountry.service.IAdwebCountryService;
import org.jeecg.modules.adweb.userCountry.service.IAdwebUserCountryService;
import org.springframework.stereotype.Service;

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

/**
 * @Description: adweb_user_country
 * @Author: jeecg-boot
 * @Date:   2022-08-19
 * @Version: V1.0
 */
@Service
public class AdwebUserCountryServiceImpl extends ServiceImpl<AdwebUserCountryMapper, AdwebUserCountry> implements IAdwebUserCountryService {

    @Resource
    private IAdwebCountryService adwebCountryService;

    /**
     * 根据子账户的id获取分配给其负责的国家列表
     *
     * @param id 子账户id
     * @return 国家id列表
     */
    @Override
    public List<Integer> getCountryIdsBySubId(String id) {
        QueryWrapper<AdwebUserCountry> queryWrapper = new QueryWrapper<AdwebUserCountry>()
                .select("country_id")
                .eq("uid", id)
                .eq("status", 1);
        List<AdwebUserCountry> list = this.list(queryWrapper);
        if (CollectionUtils.isEmpty(list)) {
            return new ArrayList<>();
        }
        return list.stream().map(AdwebUserCountry::getCountryId).collect(Collectors.toList());
    }

    /**
     * 根据子账户的id获取分配给其负责的国家列表
     *
     * @param id     子账户id
     * @param siteId 站点id
     * @return 国家code列表
     */
    @Override
    public List<String> getCountryCodesBySubId(String id, Integer siteId) {
        List<Integer> countryIds = this.getCountryIdsBySubId(id);
        if (CollectionUtils.isEmpty(countryIds)) {
            return new ArrayList<>();
        }
        QueryWrapper<AdwebCountry> queryWrapper = new QueryWrapper<>();
        queryWrapper.in("geoname_id", countryIds);
        List<AdwebCountry> countries = adwebCountryService.list(queryWrapper);
        return countries.stream().map(AdwebCountry::getCountryIsoCode).collect(Collectors.toList());
    }

}
