package com.bawei.mall.product.runer;

import com.bawei.common.core.domain.R;
import com.bawei.es.remote.EsDocRemoteService;
import com.bawei.es.remote.EsIndexRemoteService;
import com.bawei.mall.product.domain.MallProductInfo;
import com.bawei.mall.product.mapper.MallProductInfoMapper;
import com.fasterxml.jackson.databind.ObjectMapper;
import lombok.extern.log4j.Log4j2;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.ApplicationArguments;
import org.springframework.boot.ApplicationRunner;
import org.springframework.stereotype.Component;

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

/**
 * @program: mall_cloud
 * @ClassName: EsRunnerImpl
 * @author: Gyc
 * @create: 2024-04-27 15:36
 **/
@Component
@Log4j2
public class EsRunnerImpl implements ApplicationRunner {
    @Autowired
    private EsIndexRemoteService esIndexRemoteService;
    @Autowired
    private EsDocRemoteService esDocRemoteService;
    @Autowired
    private MallProductInfoMapper mallProductInfoMapper;
    @Override
    public void run(ApplicationArguments args) throws Exception {
        List<MallProductInfo> mallProductInfos = mallProductInfoMapper.selectMallProductInfoList(new MallProductInfo());
        ObjectMapper objectMapper = new ObjectMapper();
        Map<String, Object> productMap = mallProductInfos.stream()
                .collect(Collectors.toMap(
                        productInfo -> String.valueOf(productInfo.getId()),
                        productInfo -> objectMapper.convertValue(productInfo, Map.class)
                ));

        R shop = esDocRemoteService.batchAddDoc("shop", productMap);
        log.info(shop);


    }

}
