package com.liquidnet.client.admin.zhengzai.adam.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.liquidnet.client.admin.zhengzai.adam.service.IAdamCaomeiBadgeAdminService;
import com.liquidnet.service.adam.dto.AdamCaomeiBadgeClaimCountDto;
import com.liquidnet.service.adam.dto.AdamCaomeiBadgeClaimUserDto;
import com.liquidnet.service.adam.dto.param.AdamCaomeiBadgeClaimUserSearchParam;
import com.liquidnet.service.adam.dto.param.AdamCaomeiBadgeSearchParam;
import com.liquidnet.service.adam.dto.vo.AdamCaomeiBadgeClaimUserVo;
import com.liquidnet.service.adam.dto.vo.AdamCaomeiBadgeVo;
import com.liquidnet.service.adam.entity.AdamCaomeiBadge;
import com.liquidnet.service.adam.mapper.AdamCaomeiBadgeMapper;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import java.util.Collections;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

@Slf4j
@Service
public class AdamCaomeiBadgeAdminServiceImpl extends ServiceImpl<AdamCaomeiBadgeMapper, AdamCaomeiBadge> implements IAdamCaomeiBadgeAdminService {
    @Override
    public List<AdamCaomeiBadgeVo> listWithClaimedCount(AdamCaomeiBadgeSearchParam param) {
        LambdaQueryWrapper<AdamCaomeiBadge> queryWrapper = Wrappers.lambdaQuery(AdamCaomeiBadge.class);
        if (StringUtils.isNotBlank(param.getName())) {
            queryWrapper.like(AdamCaomeiBadge::getName, param.getName());
        }
        if (param.getType() != null) {
            queryWrapper.eq(AdamCaomeiBadge::getType, param.getType());
        }
        if (param.getDisplayStatus() != null) {
            queryWrapper.eq(AdamCaomeiBadge::getDisplayStatus, param.getDisplayStatus());
        }
        queryWrapper.orderByDesc(AdamCaomeiBadge::getSort, AdamCaomeiBadge::getUpdatedAt);

        List<AdamCaomeiBadge> badges = this.list(queryWrapper);
        if (badges.isEmpty()) {
            return Collections.emptyList();
        }

        List<String> badgeIds = badges.stream()
                .map(AdamCaomeiBadge::getBadgeId)
                .filter(StringUtils::isNotBlank)
                .collect(Collectors.toList());

        // key: 徽章ID，value: 领取人数
        Map<String, Integer> countMap = badgeIds.isEmpty() ? Collections.emptyMap() :
                baseMapper.selectClaimedCountByBadgeIds(badgeIds).stream()
                        .collect(Collectors.toMap(AdamCaomeiBadgeClaimCountDto::getBadgeId, AdamCaomeiBadgeClaimCountDto::getClaimedCount, (a, b) -> a));

        return badges.stream().map(item -> {
            AdamCaomeiBadgeVo vo = new AdamCaomeiBadgeVo();
            BeanUtils.copyProperties(item, vo);
            vo.setClaimedCount(countMap.getOrDefault(item.getBadgeId(), 0));
            return vo;
        }).collect(Collectors.toList());
    }

    @Override
    public List<AdamCaomeiBadgeClaimUserVo> listClaimUsers(AdamCaomeiBadgeClaimUserSearchParam param) {
        if (param == null || StringUtils.isBlank(param.getBadgeId())) {
            return Collections.emptyList();
        }
        List<AdamCaomeiBadgeClaimUserDto> list = baseMapper.selectClaimUsersByBadgeId(param.getBadgeId());
        return list.stream().map(item -> {
            AdamCaomeiBadgeClaimUserVo vo = new AdamCaomeiBadgeClaimUserVo();
            BeanUtils.copyProperties(item, vo);
            return vo;
        }).collect(Collectors.toList());
    }
}
