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

import com.liangjian.dataplatform.member.dao.fourthly.MemDataDictoryRepository;
import com.liangjian.dataplatform.member.entity.po.secondary.FiltersUtils;
import com.liangjian.dataplatform.member.service.IMemberextsService;
import com.liangjian.dataplatform.member.util.QueryBuilderUtil;
import com.liangjian.dataplatform.member.util.StringUtil;
import net.sf.json.JSONObject;
import org.elasticsearch.search.SearchHit;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

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

@Service
public class MemberextsService implements IMemberextsService {



    @Autowired
    private MemDataDictoryRepository memDataDictoryRepository;

    @Override
    public Map<String, Object> memberextsByPhone(String data) {
        Map<String, Object> ret = new HashMap<String, Object>();
        ret.put("items",null);
        ret.put("success",false);
        if(!StringUtil.isEmpty(data)){
            JSONObject jsonObject = net.sf.json.JSONObject.fromObject(data);
            List<FiltersUtils> filrer = new ArrayList<FiltersUtils>();
            FiltersUtils filters = new FiltersUtils();
            filters.setOperate("equal");
            filters.setField("_id");
            filters.setValue(jsonObject.get("query").toString());
            filrer.add(filters);
            String str = null;
            SearchHit[] searchHit = QueryBuilderUtil.search("crm_memberbase_phoneid_v1","_doc",0,0,filrer);
            List<Map<String, Object>> list = new ArrayList<Map<String, Object>>();
            for (int i = 0; i <searchHit.length ; i++) {
                SearchHit searchHit1 = searchHit[i];
                Map<String, Object> source = searchHit1.getSourceAsMap();
                source.put("_id",searchHit1.getId());
                list.add(source);
            }
            ret.put("items",list);
            ret.put("success",true);
        }
        return ret;
    }

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

        Map<String, Object> ret = new HashMap<String, Object>();
        ret.put("items",null);
        ret.put("success",false);
        if(!StringUtil.isEmpty(data)){
            JSONObject jsonObject = net.sf.json.JSONObject.fromObject(data);
            List<FiltersUtils> filrer = new ArrayList<FiltersUtils>();
            FiltersUtils filters = new FiltersUtils();
            filters.setOperate("equal");
            filters.setField("_id");
            filters.setValue(jsonObject.get("MemberPhone").toString());
            filrer.add(filters);
            String str = null;
            SearchHit[] searchHit = QueryBuilderUtil.search("crm_memberbase_phoneid_v1","_doc",0,0,filrer);
            List<Map<String, Object>> list = new ArrayList<Map<String, Object>>();
            for (int i = 0; i <searchHit.length ; i++) {
                SearchHit searchHit1 = searchHit[i];
                Map<String, Object> source = searchHit1.getSourceAsMap();

                String patientPhone = source.get("PatientPhone").toString();
                Map<String, Object> patientPhoneMap  = memDataDictoryRepository.desDecryptFixKey(patientPhone);// 号码解密.
                patientPhone = (String)patientPhoneMap.get("phone");
                if(!StringUtil.isEmpty(patientPhone)){
                    String replaced = patientPhone.substring(3, 7);
                    patientPhone = patientPhone.replace(replaced, "****");
                }

                Map<String, Object> itemsMap = new HashMap<String, Object>();
                itemsMap.put("PatientWeight",source.get("PatientWeight"));
                itemsMap.put("PatientGender",source.get("PatientGender"));
                itemsMap.put("PatientId",source.get("PatientId"));
                itemsMap.put("PatientPhone",patientPhone);
                itemsMap.put("PatientName",source.get("PatientName"));
                itemsMap.put("PatientHeight",source.get("PatientHeight"));
                itemsMap.put("PatientAge",source.get("PatientAge"));
                itemsMap.put("PatientPhoneCr",searchHit1.getId());
                itemsMap.put("_id",searchHit1.getId());
                itemsMap.put("PatientPhoneOr",patientPhone);

                list.add(itemsMap);
            }
            ret.put("items",list);
            ret.put("temsCount",list.size());
            ret.put("success",true);
        }
        return ret;
    }

    @Override
    public Map<String, Object> tagByMemberPhone(String data) {
        Map<String, Object> ret = new HashMap<String, Object>();
        Map<String, Object> itemsMap = new HashMap<String, Object>();
        ret.put("items",itemsMap);
        ret.put("success",true);
        try {
            if(!StringUtil.isEmpty(data)){
                JSONObject jsonObject = net.sf.json.JSONObject.fromObject(data);
                List<FiltersUtils> filrer = new ArrayList<FiltersUtils>();
                FiltersUtils filters = new FiltersUtils();
                filters.setOperate("equal");
                filters.setField("MemberPhone");
                filters.setValue(jsonObject.get("query").toString());
                filrer.add(filters);
                String str = null;
                SearchHit[] memberSearch = QueryBuilderUtil.search("mem_membertag","_doc",0,0,filrer);

                for (int i = 0; i <memberSearch.length ; i++) {
                    SearchHit searchHit1 = memberSearch[i];
                    Map<String, Object> source = searchHit1.getSourceAsMap();
                    String TagTypeName = source.get("TagTypeName").toString();

//                    param.clear();
//                    map.clear();
//                    param.put("MemberPhone",jsonObject.get("query").toString());
//                    param.put("TagTypeName",TagTypeName);
//                    map.put("equals",param);
                    filrer.clear();
                    filters = new FiltersUtils();
                    filters.setOperate("equal");
                    filters.setField("MemberPhone");
                    filters.setValue(jsonObject.get("query").toString());
                    filrer.add(filters);

                    FiltersUtils filters1 = new FiltersUtils();
                    filters1.setOperate("equal");
                    filters1.setField("TagTypeName");
                    filters1.setValue(TagTypeName);
                    filrer.add(filters1);
                    SearchHit[] nameSearch = QueryBuilderUtil.search("mem_membertag","_doc",0,0,filrer);

                    Map<String, Object>[] nameSearchArr = new Map[nameSearch.length];
                    for (int j = 0; j <nameSearch.length ; j++) {
                        SearchHit nameSearch1 = nameSearch[j];
                        Map<String, Object> nameSearchMap = nameSearch1.getSourceAsMap();
                        nameSearchArr[i] = nameSearchMap;
                    }
                    itemsMap.put(TagTypeName,nameSearchArr);
                }
                ret.put("items",itemsMap);
                ret.put("success",true);
            }
        }catch (Exception e){
            e.printStackTrace();
            ret.put("items",null);
            ret.put("success",false);
        }
        return ret;
    }
}
