package logic

import (
	"app.yhyue.com/moapp/jybase/common"
	"context"
	"jyBXSubscribe/rpc/model"

	"jyBXSubscribe/rpc/internal/svc"
	"jyBXSubscribe/rpc/type/bxsubscribe"

	"github.com/zeromicro/go-zero/core/logx"
)

type GetSubScribeInfoLogic struct {
	ctx    context.Context
	svcCtx *svc.ServiceContext
	logx.Logger
}

func NewGetSubScribeInfoLogic(ctx context.Context, svcCtx *svc.ServiceContext) *GetSubScribeInfoLogic {
	return &GetSubScribeInfoLogic{
		ctx:    ctx,
		svcCtx: svcCtx,
		Logger: logx.WithContext(ctx),
	}
}

// 订阅设置获取
func (l *GetSubScribeInfoLogic) GetSubScribeInfo(in *bxsubscribe.UserReq) (*bxsubscribe.UserResq, error) {
	// todo: add your logic here and delete this line
	//data := model.NewSubscribePush(in.Types).GetSubScribeInfo(in)
	data := &map[string]interface{}{}
	items, ok := (*data)["items"].([]map[string]interface{})
	if !ok {
		inter_items, _ := (*data)["items"].([]interface{})
		items = common.ObjArrToMapArr(inter_items)
	}
	buyerclass, _ := (*data)["buyerclass"].([]interface{})
	infotype, _ := (*data)["infotype"].([]interface{})
	keytip := false
	if in.Types == model.MemberFlag && in.PositionType == 0 {
		keytip = (*data)["b_keytip"].(bool)
	}
	return &bxsubscribe.UserResq{
		ErrorCode: 0,
		ErrorMsg:  "",
		Data: &bxsubscribe.Subscribe{
			Area:         l.formatM(common.ObjToMap((*data)["area"])),
			Buyerclass:   common.ObjArrToStringArr(buyerclass),
			Infotype:     common.ObjArrToStringArr(infotype),
			Matchway:     common.Int64All((*data)["matchway"]),
			Projectmatch: common.Int64All((*data)["projectmatch"]),
			Items:        l.formatItems(items),
			Keytip:       keytip,
		},
	}, nil
}

//格式转化 map<string,list>转化
func (l *GetSubScribeInfoLogic) formatM(m *map[string]interface{}) map[string]*bxsubscribe.List {
	retM := map[string]*bxsubscribe.List{}
	if len(*m) == 0 {
		return retM
	}
	for k, v := range *m {
		s_v, ok := v.([]string)
		if !ok {
			interf, _ := v.([]interface{})
			s_v = common.ObjArrToStringArr(interf)
		}
		retM[k] = &bxsubscribe.List{
			Value: s_v,
		}
	}
	return retM
}

func (l *GetSubScribeInfoLogic) formatItems(m []map[string]interface{}) []*bxsubscribe.Items {
	items := make([]*bxsubscribe.Items, len(m))
	for k, v := range m {
		items[k] = &bxsubscribe.Items{
			SItem:      common.ObjToString(v["s_item"]),
			UpdateTime: common.Int64All(v["updatetime"]),
		}
		akey, _ := v["a_key"].([]map[string]interface{})
		pbKey := make([]*bxsubscribe.Keys, len(akey))
		for kk, vv := range akey {
			key, ok := vv["key"].([]string)
			if !ok {
				inter_vv, _ := vv["key"].([]interface{})
				key = common.ObjArrToStringArr(inter_vv)
			}
			notkey, ok := vv["notkey"].([]string)
			if !ok {
				inter_vv, _ := vv["notkey"].([]interface{})
				notkey = common.ObjArrToStringArr(inter_vv)
			}
			pbKey[kk] = &bxsubscribe.Keys{
				Key:        key,
				Notkey:     notkey,
				UpdateTime: common.If(vv["updatetime"] == nil, int64(0), common.Int64All(vv["updatetime"])).(int64),
			}
		}
		items[k].AKey = pbKey
	}
	return items
}
