package com.gic.haoban.manage.service.service.impl;

import com.alibaba.fastjson.JSONObject;
import com.gic.haoban.manage.service.service.MemberQueryService;
import com.gic.member.api.dto.es.MemberDataDTO;
import com.gic.search.engine.api.constant.OperateEnum;
import com.gic.search.engine.api.dto.ESResponseQueryBatch;
import com.gic.search.engine.api.dto.ESResponseQuerySingle;
import com.gic.search.engine.api.dto.dynamic.DynamicSearchDTO;
import com.gic.search.engine.api.filter.NewNodeAddUtil;
import com.gic.search.engine.api.service.dynamic.ESDataDynamicOperationApiService;
import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.lang3.StringUtils;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

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

@Service("memberQueryService")
public class MemberQueryServiceImpl implements MemberQueryService {

    private static final Logger logger = LogManager.getLogger(MemberQueryService.class);
    @Autowired
    private ESDataDynamicOperationApiService esApiService;

    @Override
    public JSONObject queryDataSingle(String enterpriseId, String memberId, String returnFields) {
        JSONObject searchRes = null;
        try {
            JSONObject searchJson = new JSONObject();
            DynamicSearchDTO search = new DynamicSearchDTO();
            searchJson.put("id", memberId);
            search.setSearchJson(searchJson);
            search.setEnterpriseId(enterpriseId);
            search.setColumnCategoryCode("member");
            if (StringUtils.isNotBlank(returnFields)) {
                search.setReturnFileds(returnFields);
            }
            ESResponseQuerySingle single = esApiService.queryDataSingle(search);
            searchRes = single.getRes();
        } catch (Exception e) {
            logger.warn("从搜索引擎搜索会员数据异常memberId: {}", memberId, e);
        }
        return searchRes;
    }

    /**
     * 批量查询会员信息
     *
     * @param enterpriseId
     * @param memberIds
     * MemberDataEnum
     * @param returnFields
     * @return
     */
    @Override
    public List<MemberDataDTO> queryMemberBatch(String enterpriseId, List<String> memberIds, String returnFields) {

        if (CollectionUtils.isEmpty(memberIds)) {
            return Collections.emptyList();
        }
        DynamicSearchDTO search = new DynamicSearchDTO();
        JSONObject searchJson = new JSONObject();
        searchJson = NewNodeAddUtil.addNewNodeForAnd("memberId", OperateEnum.OPERATE_CONTAIN.getValue(), StringUtils.join(memberIds, " "),
                searchJson);
        search.setSearchJson(searchJson);
        search.setColumnCategoryCode("member");
        search.setEnterpriseId(enterpriseId);
        if (StringUtils.isNotBlank(returnFields)) {
            search.setReturnFileds(returnFields);
        }
        ESResponseQueryBatch queryBatch = esApiService.queryDataBatch(search, false, null);
        if (CollectionUtils.isEmpty(queryBatch.getRes())) {
            return Collections.emptyList();

        }
        return queryBatch.getRes()
                .stream()
                .map(item -> item.toJavaObject(MemberDataDTO.class))
                .collect(Collectors.toList());
    }
}
