package data

import (
	"context"
	"encoding/json"
	"fmt"
	"github.com/olivere/elastic/v7"
	"hongze/hz_crm_api/models/data_manage"
	"hongze/hz_crm_api/utils"
	"log"
	"os"
	"strconv"
	"strings"
)

const (
	ES_URL      = "http://es-cn-nif227b580019rgw6.public.elasticsearch.aliyuncs.com:9200" //<1>
	ES_USERNAME = "elastic"                                                               //<2>
	ES_PASSWORD = "hongze@2021"                                                           //<3>
)

func NewClient() (client *elastic.Client, err error) {
	errorlog := log.New(os.Stdout, "APP", log.LstdFlags)
	file := ""
	if utils.RunMode == "release" {
		file = `./rdlucklog/eslog.log`
	} else {
		file = `./rdlucklog/eslog.log`
	}
	logFile, _ := os.OpenFile(file, os.O_RDWR|os.O_CREATE|os.O_APPEND, 0766)
	client, err = elastic.NewClient(
		elastic.SetURL(ES_URL),
		elastic.SetBasicAuth(ES_USERNAME, ES_PASSWORD),
		elastic.SetTraceLog(log.New(logFile, "ES-TRACE: ", 0)),
		elastic.SetSniff(false), elastic.SetErrorLog(errorlog))
	return
}

func CreateEdbInfoIndex() {
	indexName := utils.DATA_INDEX_NAME
	mappingJson := `{
 "mappings": {
   "dynamic": true,
   "properties": {
     "EdbInfoId": {
       "type": "integer"
     },
     "SourceName": {
       "type": "text",
       "term_vector": "with_positions_offsets",
		"analyzer": "ik_smart"
     },
    "EdbCode": {
       "type": "text",
       "term_vector": "with_positions_offsets",
		"analyzer": "ik_smart"
     },
     "EdbName": {
       "type": "text",
       "term_vector": "with_positions_offsets",
		"analyzer": "ik_smart"
     },
      "Frequency": {
       "type": "text",
       "term_vector": "with_positions_offsets",
		"analyzer": "ik_smart"
     },
     "UniqueCode": {
       "type": "text",
       "term_vector": "with_positions_offsets"
     },
     "Unit": {
       "type": "text",
       "term_vector": "with_positions_offsets"
     }
   }
 }
}`
	EsCreateIndex(indexName, mappingJson)
}

// indexName:索引名称
// mappingJson:表结构
func EsCreateIndex(indexName, mappingJson string) (err error) {
	client, err := NewClient()
	if err != nil {
		return
	}
	//定义表结构
	exists, err := client.IndexExists(indexName).Do(context.Background()) //<5>
	if err != nil {
		return
	}
	if !exists {
		resp, err := client.CreateIndex(indexName).BodyJson(mappingJson).Do(context.Background())
		//BodyJson(bodyJson).Do(context.Background())
		if err != nil {
			fmt.Println("CreateIndex Err:" + err.Error())
			return err
		}
		fmt.Println(resp.Index, resp.ShardsAcknowledged, resp.Acknowledged)
	} else {
		fmt.Println(indexName + " 已存在")
	}
	return
}

func AddAllEdbInfo() {
	allList, err := data_manage.GetEdbInfoAllList()
	if err != nil {
		fmt.Println("GetArticleAll Err:", err.Error())
		return
	}
	indexName := "hz_data_lib_v1"
	for _, v := range allList {
		EsAddOrEditEdbInfo(indexName, strconv.Itoa(v.EdbInfoId), v)
		fmt.Println(v.EdbInfoId)
	}
}

// 新增和修改数据
func EsAddOrEditEdbInfo(indexName, docId string, item *data_manage.EdbInfo) (err error) {
	defer func() {
		if err != nil {
			fmt.Println("EsAddOrEditEdbInfo Err:", err.Error())
		}
	}()
	client, err := NewClient()
	if err != nil {
		return
	}
	searchById, err := client.Get().Index(indexName).Id(docId).Do(context.Background())
	if err != nil && !strings.Contains(err.Error(), "404") {
		fmt.Println("Get Err" + err.Error())
		return
	}
	if searchById != nil && searchById.Found {
		resp, err := client.Update().Index(indexName).Id(docId).Doc(map[string]interface{}{
			"EdbInfoId":  item.EdbInfoId,
			"EdbCode":    item.EdbCode,
			"EdbName":    item.EdbName,
			"Frequency":  item.Frequency,
			"SourceName": item.SourceName,
			"UniqueCode": item.UniqueCode,
			"Unit":       item.Unit,
		}).Do(context.Background())
		if err != nil {
			return err
		}
		fmt.Println(resp.Status, resp.Result)
		if resp.Status == 0 {
			fmt.Println("修改成功")
		} else {
			fmt.Println("EditData", resp.Status, resp.Result)
		}
	} else {
		resp, err := client.Index().Index(indexName).Id(docId).BodyJson(item).Do(context.Background())
		if err != nil {
			fmt.Println("新增失败:", err.Error())
			return err
		}
		if resp.Status == 0 && resp.Result == "created" {
			fmt.Println("新增成功")
			err = nil
		} else {
			fmt.Println("AddData", resp.Status, resp.Result)
		}
	}
	return
}

// 删除数据
func EsDeleteEdbInfo(docId int) (err error) {
	indexName := utils.DATA_INDEX_NAME
	client, err := NewClient()
	if err != nil {
		return
	}
	resp, err := client.Delete().Index(indexName).Id(strconv.Itoa(docId)).Do(context.Background())
	fmt.Println(resp)
	if err != nil {
		return
	}
	if resp.Status == 0 {
		fmt.Println("删除成功")
	} else {
		fmt.Println("AddData", resp.Status, resp.Result)
	}
	return
}

func MappingModify(indexName, mappingJson string) {
	client, err := NewClient()
	if err != nil {
		return
	}
	result, err := client.PutMapping().Index(indexName).BodyString(mappingJson).Do(context.Background())
	fmt.Println(err)
	fmt.Println(result)
	return
}

// 搜索指标信息
func EsSearchEdbInfo(indexName, keyWord string, filterSource int) (result []*data_manage.EdbInfoList, err error) {
	client, err := NewClient()
	if keyWord != "" {
		keyWordArr := strings.Split(keyWord, " ")
		existMap := make(map[int]string)
		for _, searchKey := range keyWordArr {
			matchArr := make([]elastic.Query, 0)
			boolquery := elastic.NewBoolQuery()

			multiMatch := elastic.NewMultiMatchQuery(searchKey, "EdbName", "EdbCode").Analyzer("ik_smart")
			matchArr = append(matchArr, multiMatch)

			if filterSource == 2 {
				matchPhrase := elastic.NewMatchPhraseQuery("Frequency", "月度")
				matchArr = append(matchArr, matchPhrase)
			}

			boolquery.Must(matchArr...)

			//request := client.Search(indexName).Sort("CreateTime", false).Query(boolquery)
			request := client.Search(indexName).Query(boolquery)

			searchByMatch, err := request.Do(context.Background())
			if err != nil {
				return nil, err
			}
			if searchByMatch != nil {
				if searchByMatch.Hits != nil {
					for _, v := range searchByMatch.Hits.Hits {
						edbInfoJson, err := v.Source.MarshalJSON()
						if err != nil {
							return nil, err
						}
						edbInfo := new(data_manage.EdbInfoList)
						err = json.Unmarshal(edbInfoJson, &edbInfo)
						if err != nil {
							return nil, err
						}
						if _, ok := existMap[edbInfo.EdbInfoId]; !ok {
							result = append(result, edbInfo)
						}
						existMap[edbInfo.EdbInfoId] = edbInfo.EdbCode
					}
				}
			}
		}
	}
	return
}
