package model

import (
	"app.yhyue.com/moapp/jybase/redis"
	"encoding/json"
	"fmt"
	"jyBXBase/rpc/bxbase"
	IC "jyBXBase/rpc/init"
	"log"
	"strings"
	"time"

	MC "app.yhyue.com/moapp/jybase/common"
	ME "app.yhyue.com/moapp/jybase/encrypt"
	elastic "app.yhyue.com/moapp/jybase/es"
	"app.yhyue.com/moapp/jybase/mongodb"
	"app.yhyue.com/moapp/jybase/mysql"
	"github.com/zeromicro/go-zero/core/logx"
	"go.mongodb.org/mongo-driver/bson/primitive"
)

const (
	search_index        = "bidding"
	search_type         = "bidding"
	mongodb_fields      = `{"_id":1,"area":1,"publishtime":1,"s_subscopeclass":1,"subtype":1,"title":1,"toptype":1,"type":1, "buyerclass":1,"budget":1,"bidamount":1,"s_winner":1,"bidopentime":1,"buyer":1,"projectname":1,"spidercode":1,"site":1}`
	query               = `{"query":{"terms":{"_id":["%s"]}},"_source":["_id","area", "publishtime", "s_subscopeclass", "subtype", "title", "toptype", "type", "buyerclass","bidamount","budget","projectname","buyer","bidopentime","s_winner","filetext","spidercode","site"],"from":0,"size":%d}`
	multi_match         = `{"multi_match": {"query": %s,"type": "phrase", "fields": ["title"]}}`
	query_bool_must     = `{"terms":{"%s":[%s]}}`
	query_bool_must_and = `{"bool":{"must":[%s],"must_not":[%s]}}`
	search_field        = `"_id","area", "publishtime", "s_subscopeclass", "subtype", "title", "toptype", "type", "buyerclass","bidamount","budget","projectname","buyer","bidopentime","s_winner","filetext","isValidFile","spidercode","site"`
	query_city_hkeys    = `{"query":{"bool":{"must":[%s],"should":[%s],"minimum_should_match":%d}},"highlight": {"pre_tags": ["<a>"],"post_tags": ["</a>"],"fields": {"title": {"fragment_size": 0,"number_of_fragments": 1}}},"_source":[` + search_field + `],"sort":[{"publishtime":"desc"},{"budget":"desc"}],"from":0,"size":%d}`
	Pushbidding         = "global_common_data.dws_f_bid_baseinfo"
	Province            = "province"
	StatusNoLogin       = 1 // 1-未登录用户
	StatusLoginUser     = 2 // 2-用户自己的key
	StatusLogin         = 3 // 3-登录用户最新标讯
	StatusCache         = 0 //   0 -不存缓存 本来查的就是缓存的数据
)

type NewestInfo struct {
	TableName    string
	UserId       string
	MysqlDb      *mysql.Mysql
	NewUserId    int64
	PushMysql    *mysql.Mysql
	NewsLimitNum int64 // 最新标讯数量条数限制
}

var mysqlTables = map[string]string{
	"f": "push.pushsubscribe",
	"v": "push.pushsubscribe",
	"m": "push.pushmember",
	"e": "push.pushentniche",
}

func GetRoleNewestInfoService(AppId, MgoUserId string, NewUserId, AccountId, EntId, EntUserId, PositionType, PositionId int64) (roleNewestInfo *NewestInfo, flag string) {
	powerCheck := IC.Middleground.PowerCheckCenter.Check(AppId, MgoUserId, NewUserId, AccountId, EntId, PositionType, PositionId)
	thisUserId := MC.If(PositionType == 1, MC.InterfaceToStr(EntUserId), MC.InterfaceToStr(NewUserId)).(string)
	thisNewUserId := MC.If(PositionType == 1, EntUserId, NewUserId).(int64)
	if powerCheck.Member.Status > 0 {
		// 大会员
		flag = "m"
	} else if powerCheck.Entniche.Status > 0 && powerCheck.Entniche.PowerSource != 1 && powerCheck.Entniche.IsEntPower == 1 {
		// 商机管理
		flag = "e"
	} else if powerCheck.Vip.Status > 0 {
		// 超级订阅
		flag = "v"
	} else {
		// 普通用户
		flag = "f"
	}
	thisUserType := MC.If(PositionType == 1, "e", flag).(string)
	return GetNewestInfo(thisUserId, thisUserType, thisNewUserId), flag
}
func GetNewestInfo(userId, userType string, newUserId int64) *NewestInfo {
	nt := &NewestInfo{
		UserId:       userId,
		TableName:    mysqlTables[userType],
		MysqlDb:      IC.BaseServiceMysql,
		NewUserId:    newUserId,
		NewsLimitNum: IC.C.NewsLimitNum,
	}
	return nt
}

// GetPushHistoryCount 缓存code  配置最新的redis code；可以感觉redis内存大小 调节redis存储；
// 根据GetPushHistoryCount 返回值 作为列表缓存key中一个参数
func (n *NewestInfo) GetPushHistoryCount() int64 {
	countKey := fmt.Sprintf(IC.C.NewsCache.Count.Key, n.TableName, n.UserId)
	if b := redis.GetInt("new", countKey); b != 0 {
		return int64(b)
	}
	countSql := "SELECT COUNT(1) FROM (SELECT 1 FROM %s WHERE userid = %d LIMIT 1) AS ph"
	//countSql := "select count(1) from %s  a  where a.userid=%d order by a.id desc"
	countSql = fmt.Sprintf(countSql, n.TableName, n.NewUserId)
	timeOut := IC.C.NewsCache.Count.Timeout
	c := n.MysqlDb.CountBySql(countSql)
	if c > 0 {
		timeOut = timeOut * 4
	} else {
		c = -1
	}
	redis.Put("new", countKey, c, IC.C.NewsCache.Count.Timeout) //过期时间走配置，比最新标讯列表缓存时间 要短1/10 尽量不超过两分钟
	return c
}

func (n *NewestInfo) GetPushHistory() (res []*bxbase.NewestList) {
	findSQL := "select a.infoid,REPLACE(a.matchkeys,'+',' ') as matchkeys,a.attachment_count,a.budget,a.bidamount from %s  a  where a.userid=%d   order by a.id desc limit ?"
	findSQL = fmt.Sprintf(findSQL, n.TableName, n.NewUserId)
	logx.Info(n.TableName, "-------", n.NewUserId, ",findSQL:", findSQL)
	list := n.MysqlDb.SelectBySql(findSQL, n.NewsLimitNum)
	if len(*list) > 0 && list != nil {
		m := map[string]bool{}
		es_ids := []string{}
		infos := map[string]*bxbase.NewestList{}
		for _, v := range *list {
			infoId := MC.ObjToString(v["infoid"])
			if m[infoId] {
				continue
			}
			es_ids = append(es_ids, infoId)
			m[MC.ObjToString(v["infoid"])] = true
			//
			infos[infoId] = &bxbase.NewestList{
				Id:         ME.EncodeArticleId2ByCheck(MC.ObjToString(v["infoid"])),
				Matchkeys:  strings.Split(MC.ObjToString(v["matchkeys"]), " "),
				Budget:     MC.Int64All(v["budget"]),
				Bidamount:  MC.Int64All(v["bidamount"]),
				FileExists: MC.Int64All(v["attachment_count"]) > 0,
			}
		}
		if len(es_ids) > 0 {
			list := elastic.Get(search_index, search_type, fmt.Sprintf(query, strings.Join(es_ids, `","`), len(es_ids)))
			if list != nil {
				for _, v := range *list {
					_id := MC.ObjToString(v["_id"])
					bn := infos[_id]
					bn.Title = MC.ObjToString(v["title"])
					bn.PublishTime = MC.Int64All(v["publishtime"])
					bn.Subtype = MC.If(v["subtype"] != nil, MC.ObjToString(v["subtype"]), MC.ObjToString(v["toptype"])).(string)
					bn.Area = MC.If(MC.ObjToString(v["area"]) == "A", "全国", MC.ObjToString(v["area"])).(string)
					bn.Buyerclass = MC.ObjToString(v["buyerclass"])
					bn.City = MC.ObjToString(v["city"])
					bn.Industry = MC.If(MC.ObjToString(v["s_subscopeclass"]) != "", strings.Split(strings.Split(MC.ObjToString(v["s_subscopeclass"]), ",")[0], "_")[0], "").(string)
					bn.SpiderCode = MC.ObjToString(v["spidercode"])
					bn.Site = MC.ObjToString(v["site"])
				}
			}
		}
		//mongodb bidding
		mgo_ids := []primitive.ObjectID{}
		for _, v := range es_ids {
			if infos[v].Title == "" {
				mgo_ids = append(mgo_ids, mongodb.StringTOBsonId(v))
			}
		}
		if len(mgo_ids) > 0 {
			list, ok := IC.MgoBidding.Find("bidding", map[string]interface{}{"_id": map[string]interface{}{"$in": mgo_ids}}, nil, mongodb_fields, false, -1, -1)
			if ok && *list != nil {
				for _, v := range *list {
					_id := mongodb.BsonIdToSId(v["_id"])
					bn := infos[_id]
					bn.Title = MC.ObjToString(v["title"])
					bn.PublishTime = MC.Int64All(v["publishtime"])
					bn.Subtype = MC.If(v["subtype"] != nil, MC.ObjToString(v["subtype"]), MC.ObjToString(v["toptype"])).(string)
					bn.Area = MC.If(MC.ObjToString(v["area"]) == "A", "全国", MC.ObjToString(v["area"])).(string)
					bn.Buyerclass = MC.ObjToString(v["buyerclass"])
					bn.City = MC.ObjToString(v["city"])
					bn.Industry = MC.If(MC.ObjToString(v["s_subscopeclass"]) != "", strings.Split(strings.Split(MC.ObjToString(v["s_subscopeclass"]), ",")[0], "_")[0], "").(string)
					bn.SpiderCode = MC.ObjToString(v["spidercode"])
					bn.Site = MC.ObjToString(v["site"])
				}
			}
		}
		//mongodb bidding_back
		mgo_back_ids := []primitive.ObjectID{}
		for _, v := range mgo_ids {
			if infos[mongodb.BsonIdToSId(v)].Title == "" {
				mgo_back_ids = append(mgo_back_ids, v)
			}
		}
		if len(mgo_back_ids) > 0 {
			list, ok := IC.MgoBidding.Find("bidding_back", map[string]interface{}{"_id": map[string]interface{}{"$in": mgo_back_ids}}, nil, mongodb_fields, false, -1, -1)
			if ok && *list != nil {
				for _, v := range *list {
					_id := mongodb.BsonIdToSId(v["_id"])
					bn := infos[_id]
					bn.Title = MC.ObjToString(v["title"])
					bn.PublishTime = MC.Int64All(v["publishtime"])
					bn.Subtype = MC.If(v["subtype"] != nil, MC.ObjToString(v["subtype"]), MC.ObjToString(v["toptype"])).(string)
					bn.Area = MC.If(MC.ObjToString(v["area"]) == "A", "全国", MC.ObjToString(v["area"])).(string)
					bn.Buyerclass = MC.ObjToString(v["buyerclass"])
					bn.City = MC.ObjToString(v["city"])
					bn.Industry = MC.If(MC.ObjToString(v["s_subscopeclass"]) != "", strings.Split(strings.Split(MC.ObjToString(v["s_subscopeclass"]), ",")[0], "_")[0], "").(string)
					bn.SpiderCode = MC.ObjToString(v["spidercode"])
					bn.Site = MC.ObjToString(v["site"])
				}
			}
		}
		//
		for _, v := range infos {
			res = append(res, v)
		}
	}
	return
}

// 根据定位或者搜索历史 查es
func NewestQuery(city, keys, subtype string) (str string) {
	var musts, bools []string
	if keys != "" {
		for _, v := range strings.Split(keys, ",") {
			keys := strings.Split(v, " ") //历史搜索 空格划分
			must_tmp := []string{}
			for _, key := range keys {
				must_tmp = append(must_tmp, fmt.Sprintf(multi_match, "\""+key+"\""))
			}
			bools = append(bools, fmt.Sprintf(query_bool_must_and, strings.Join(must_tmp, ","), ""))
		}

	}
	if city != "" {
		musts = append(musts, fmt.Sprintf(query_bool_must, "city", `"`+city+`"`))
	}
	//未登录首页推送数据限制
	if subtype != "" {
		musts = append(musts, fmt.Sprintf(query_bool_must, "subtype", subtype))
	}
	minimum_should_match := 0
	if len(bools) > 0 {
		minimum_should_match = 1
	}
	str = fmt.Sprintf(query_city_hkeys, strings.Join(musts, ","), strings.Join(bools, ","), minimum_should_match, IC.C.NewsLimitNum)
	logx.Info("str:", str)
	return
}

// es查询
func NewestES(doSearchStr string) (res []*bxbase.NewestList) {
	list := elastic.Get(search_index, search_type, doSearchStr)
	if list != nil && len(*list) > 0 {
		for _, v := range *list {
			_id := mongodb.BsonIdToSId(v["_id"])
			isValidFile, _ := v["isValidFile"].(bool)
			res = append(res, &bxbase.NewestList{
				Id:          ME.EncodeArticleId2ByCheck(_id),
				Title:       MC.ObjToString(v["title"]),
				Subtype:     MC.If(v["subtype"] != nil, MC.ObjToString(v["subtype"]), MC.ObjToString(v["toptype"])).(string),
				Area:        MC.If(MC.ObjToString(v["area"]) == "A", "全国", MC.ObjToString(v["area"])).(string),
				Buyerclass:  MC.ObjToString(v["buyerclass"]),
				City:        MC.ObjToString(v["city"]),
				Industry:    MC.If(MC.ObjToString(v["s_subscopeclass"]) != "", strings.Split(strings.Split(MC.ObjToString(v["s_subscopeclass"]), ",")[0], "_")[0], "").(string),
				Budget:      MC.Int64All(v["budget"]),
				Bidamount:   MC.Int64All(v["bidamount"]),
				FileExists:  isValidFile, //附件
				PublishTime: MC.Int64All(v["publishtime"]),
				Site:        MC.ObjToString(v["site"]),
				SpiderCode:  MC.ObjToString(v["spidercode"]),
			})
		}
	}
	return
}

// GetRedisKeyTimeout 获取缓存的key 和超时时间
func GetRedisKeyTimeout(status int, positionId int64) (redisKey string, timeout int) {
	switch status {
	case StatusNoLogin:
		redisKey = IC.C.NewsCache.NoLogin.Key
		redisKey = fmt.Sprintf(redisKey, time.Now().Year(), time.Now().Month(), time.Now().Day())
		timeout = IC.C.NewsCache.NoLogin.Timeout
	case StatusLoginUser:
		redisKey = IC.C.NewsCache.LoginUser.Key // 登录用户使用的缓存key
		redisKey = fmt.Sprintf(redisKey, positionId, time.Now().Year(), time.Now().Month(), time.Now().Day())
		timeout = IC.C.NewsCache.LoginUser.Timeout
	case StatusLogin:
		redisKey = IC.C.NewsCache.Login.Key // 登录用户使用的最新标讯缓存key
		redisKey = fmt.Sprintf(redisKey, time.Now().Year(), time.Now().Month(), time.Now().Day())
		timeout = IC.C.NewsCache.Login.Timeout
	}
	return
}

// PutNewsCache 存缓存
func PutNewsCache(redisKey string, redisTimeout int, list []*bxbase.NewestList) {
	b, err := json.Marshal(list)
	if err != nil {
		log.Printf("保存缓存 序列化异常,data:%s,err:%s\n", list, err.Error())
		return
	}
	if err = redis.PutBytes("new", redisKey, &b, redisTimeout); err != nil {
		log.Printf("保存缓存 redis 异常,key:%s,err:%s\n", redisKey, err.Error())
	}

}

// GetNewsCache 取缓存
func GetNewsCache(redisKey string) (list []*bxbase.NewestList, err error) {
	redisByte, err := redis.GetBytes("new", redisKey)
	if err != nil || redisByte == nil || len(*redisByte) == 0 {
		return list, err
	}
	err = json.Unmarshal(*redisByte, &list)
	if err != nil {
		logx.Info(fmt.Sprintf("读取缓存 序列化异常,err:%s", err.Error()))
		return nil, err
	}
	return list, nil
}
