package com.liquidnet.client.admin.web.controller.zhengzai.goblin.artist;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.liquidnet.service.goblin.entity.GoblinStoreInfo;
import com.liquidnet.service.goblin.mapper.GoblinStoreInfoMapper;
import com.liquidnet.service.kylin.entity.KylinArtist;
import com.liquidnet.service.kylin.mapper.KylinArtistMapper;
import org.apache.commons.lang3.StringUtils;

import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.*;
import java.util.function.Function;
import java.util.stream.Collectors;

/**
 * Admin 艺人模块共享工具方法
 */
final class ArtistAdminHelper {

    private ArtistAdminHelper() {}

    static final DateTimeFormatter FMT = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");

    /** 按店铺名模糊查店铺ID，无输入返回 null */
    static List<String> findStoreIds(GoblinStoreInfoMapper mapper, String storeName) {
        if (StringUtils.isBlank(storeName)) return null;
        return mapper.selectList(
                        new LambdaQueryWrapper<GoblinStoreInfo>()
                                .like(GoblinStoreInfo::getStoreName, storeName)
                                .select(GoblinStoreInfo::getStoreId))
                .stream().map(GoblinStoreInfo::getStoreId).collect(Collectors.toList());
    }

    /** 按艺人名模糊查艺人ID，无输入返回 null */
    static List<String> findArtistIds(KylinArtistMapper mapper, String artistName) {
        if (StringUtils.isBlank(artistName)) return null;
        return mapper.selectList(
                        new LambdaQueryWrapper<KylinArtist>()
                                .like(KylinArtist::getArtistName, artistName)
                                .select(KylinArtist::getArtistId))
                .stream().map(KylinArtist::getArtistId).collect(Collectors.toList());
    }

    /** 批量查店铺名 */
    static Map<String, String> storeNameMap(GoblinStoreInfoMapper mapper, Set<String> ids) {
        if (ids.isEmpty()) return Collections.emptyMap();
        return mapper.selectList(new LambdaQueryWrapper<GoblinStoreInfo>()
                        .in(GoblinStoreInfo::getStoreId, ids))
                .stream().collect(Collectors.toMap(GoblinStoreInfo::getStoreId, GoblinStoreInfo::getStoreName, (a, b) -> a));
    }

    /** 批量查艺人名 */
    static Map<String, String> artistNameMap(KylinArtistMapper mapper, Set<String> ids) {
        if (ids.isEmpty()) return Collections.emptyMap();
        return mapper.selectList(new LambdaQueryWrapper<KylinArtist>()
                        .in(KylinArtist::getArtistId, ids))
                .stream().collect(Collectors.toMap(KylinArtist::getArtistId, KylinArtist::getArtistName, (a, b) -> a));
    }

    /** 从实体列表中提取 ID 集合 */
    static <T> Set<String> idsOf(List<T> list, Function<T, String> getter) {
        return list.stream().map(getter).filter(Objects::nonNull).collect(Collectors.toSet());
    }

    /** 时间格式化为 yyyy-MM-dd HH:mm:ss */
    static String formatTime(LocalDateTime time) {
        return time != null ? time.format(FMT) : null;
    }
}
