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

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 com.alibaba.fastjson.JSONObject;
import com.gic.haoban.manage.service.service.MemberQueryService;
import com.gic.search.engine.api.dto.ESResponseQuerySingle;
import com.gic.search.engine.api.dto.dynamic.DynamicSearchDTO;
import com.gic.search.engine.api.service.dynamic.ESDataDynamicOperationApiService;

@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;
    }

}
