package com.czg.service.account.service.impl;

import cn.hutool.core.util.StrUtil;
import com.czg.account.entity.BkContactList;
import com.czg.account.service.BkContactListService;
import com.czg.account.service.ShopInfoService;
import com.czg.service.account.mapper.BkContactListMapper;
import com.mybatisflex.core.query.QueryWrapper;
import com.mybatisflex.spring.service.impl.ServiceImpl;
import jakarta.annotation.Resource;
import org.springframework.stereotype.Service;

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

/**
 * 通讯录联系人列表 服务层实现。
 *
 * @author ww
 * @since 2025-11-27
 */
@Service
public class BkContactListServiceImpl extends ServiceImpl<BkContactListMapper, BkContactList> implements BkContactListService {
    @Resource
    private ShopInfoService shopInfoService;

    @Override
    public List<BkContactList> getUserList(Long shopId, Set<String> phones) {
        if (phones == null || phones.isEmpty()) {
            return new ArrayList<>();
        }

        remove(QueryWrapper.create().eq(BkContactList::getShopId, shopId));
        List<BkContactList> collect = phones.stream()
                .filter(StrUtil::isNotEmpty)
                .map(phone -> {
                    BkContactList bkContact = new BkContactList();
                    bkContact.setShopId(shopId);
                    bkContact.setPhone(phone.trim());
                    return bkContact;
                })
                .collect(Collectors.toList());
        mapper.insertBatch(collect, 50);
        Long mainShopId = shopInfoService.getMainIdByShopId(shopId);
        return mapper.getUserList(shopId, mainShopId);
    }
}
