package com.liangjian.dataplatform.member.service.impl;

import com.liangjian.dataplatform.member.entity.po.secondary.FiltersData;
import com.liangjian.dataplatform.member.entity.po.secondary.FiltersUtils;
import com.liangjian.dataplatform.member.jobUtil.EsUtils;
import com.liangjian.dataplatform.member.service.IMemFriendsService;
import net.sf.json.JSONObject;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class MemFriendsService implements IMemFriendsService {


    @Override
    public Map<String, Object> memberFriends(FiltersData data) {

        Map<String, Object> ret = new HashMap<>();
        int start = 0;
        Object query = null;
        String filter = "";
        String MemberPhone = "";
        if (data != null){
            List<FiltersUtils> filters = data.getFilters();
            filter = EsUtils.getFilterString(filters);
            MemberPhone = filters.get(0).getValue();
            query = "select * from mem_memberfriends where  1=1 " +
                        filter
                        + " order by MemberFriendsPhone " +
                        " limit " + data.getPageSize();
        }
        String sql = "{\"size\":10,\"query\":{\"bool\":{\"must\":[{\"term\":{\"MemUsualPhone\":{\"value\":\"{@MemUsualPhone}\",\"boost\":1}}},{\"nested\":{\"query\":{\"bool\":{\"must_not\":[{\"term\":{\"Friends.MemberFriendsPhone\":{\"value\":\"{@MemUsualPhone}\",\"boost\":1}}}],\"adjust_pure_negative\":true,\"boost\":1}},\"path\":\"Friends\",\"ignore_unmapped\":false,\"score_mode\":\"none\",\"boost\":1,\"inner_hits\":{\"ignore_unmapped\":false,\"from\":{@from},\"size\":{@size},\"version\":false,\"seq_no_primary_term\":false,\"explain\":false,\"track_scores\":false,\"_source\":{\"includes\":[\"Friends.*\"],\"excludes\":[]}}}}],\"adjust_pure_negative\":true,\"boost\":1}},\"_source\":false,\"stored_fields\":\"_none_\",\"docvalue_fields\":[{\"field\":\"MemUsualPhone\"},{\"field\":\"MemberNickName\"}],\"sort\":[{\"_doc\":{\"order\":\"asc\"}}]}";

        sql = sql.replace("{@from}", "" + start).replace("{@size}", "" + data.getPageSize())
                .replace("{@MemUsualPhone}", MemberPhone);

        String result = EsUtils.query(sql, "mem_memberbase_p/_search?format=json", false);

        JSONObject jsonObject = JSONObject.fromObject(result);
        List<Object> list = (List<Object>) jsonObject.getJSONObject("hits").get("hits");

        List<Object> objectList = new ArrayList<>();
        int totalCount = 0;
        if(list != null && list.size() > 0){


        }
        ret.put("items",objectList);
        ret.put("totalCount",totalCount);
        return ret;
    }
}
