package logic

import (
	"app.yhyue.com/moapp/jybase/common"
	se "app.yhyue.com/moapp/jybase/encrypt"
	"app.yhyue.com/moapp/jybase/redis"
	"context"
	"fmt"
	IC "jyBXBase/rpc/init"
	util2 "jyBXBase/rpc/util"
	"strconv"
	"strings"

	"jyBXBase/rpc/bxbase"
	"jyBXBase/rpc/internal/svc"

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

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

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

// 招标信息是否被收藏
func (l *IsCollActionLogic) IsCollAction(in *bxbase.IsCollActionReq) (*bxbase.IsCollActionRes, error) {
	var idata bxbase.IData
	if in.Label != "" {
		bid := util2.DecodeId(strings.Split(in.Bids, ",")[0])
		if bdinfos := IC.MainMysql.SelectBySql(fmt.Sprintf("SELECT labelid FROM %s WHERE userid = ? AND bid = ?", "bdcollection"), in.UserId, bid); bdinfos != nil && len(*bdinfos) > 0 {
			bdinfo := (*bdinfos)[0]
			label_ids := common.ObjToString(bdinfo["labelid"])
			//var labArr = []map[string]interface{}{}
			var labArr []*bxbase.Labels
			if label_ids != "" {
				label_ids_inter := []interface{}{}
				var instatus = "?"
				for k, id := range strings.Split(label_ids, ",") {
					if k > 0 {
						instatus += ",?"
					}
					label_ids_inter = append(label_ids_inter, id)
				}
				if labinfos := IC.MainMysql.SelectBySql(fmt.Sprintf("SELECT * FROM %s WHERE id IN (%s)", "bdlabel", instatus), label_ids_inter...); labinfos != nil && len(*labinfos) > 0 {
					for _, v := range *labinfos {
						lid := strconv.FormatInt(v["id"].(int64), 10)
						lid_str := se.SE.EncodeString(lid)
						labArr = append(labArr, &bxbase.Labels{
							Labelname: common.ObjToString(v["labelname"]),
							Id:        lid_str,
						})
					}
				}
			}
			idata.Iscoll = true
			idata.Labels = labArr
		}
		return &bxbase.IsCollActionRes{
			Data: &idata,
		}, nil
	}
	res := []string{}
	collBidMap := map[string]bool{}
	isCollkey := fmt.Sprintf("isColl_%s", in.UserId)
	collStatus := redis.GetInt("other", isCollkey)
	if collStatus == 0 {
		if count := int(IC.MainMysql.CountBySql(fmt.Sprintf(`select count(1) 
		from %s where userid = ?`, "bdcollection"), in.UserId)); count > 100 {
			collStatus = 2
		} else if count == 0 {
			collStatus = 0
		} else {
			collStatus = 1
		}
		redis.Put("other", isCollkey, collStatus, 259200)
	}
	if collStatus == 1 { //100条内 取redis
		list := util2.GetCollRedis(in.UserId, collStatus)
		for _, v := range list {
			collBidMap[v] = true
		}
	} else if collStatus == 2 { //大于100条 取mysql
		if labArr := IC.MainMysql.SelectBySql(fmt.Sprintf("select bid from %s where  userid = ?", "bdcollection"), in.UserId); labArr != nil && len(*labArr) > 0 {
			for _, v := range *labArr {
				bid_id := common.ObjToString(v["bid"])
				collBidMap[bid_id] = true
			}
		}
	} else { //0条
		idata.Bids = res
		return &bxbase.IsCollActionRes{
			Data: &idata,
		}, nil
	}

	if in.Bids == "" {
		for k := range collBidMap {
			res = append(res, k)
		}
	} else {
		for _, v := range strings.Split(in.Bids, ",") {
			//招标信息解密
			bid := util2.DecodeId(v)
			if collBidMap[bid] {
				// url.QueryEscape(v)
				res = append(res, v)
			}
		}
	}

	return &bxbase.IsCollActionRes{
		Data: &idata,
	}, nil
}
