package logic

import (
	"app.yhyue.com/moapp/jybase/common"
	"context"
	"github.com/zeromicro/go-zero/core/logx"
	"jyBXBase/rpc/bxbase"
	IC "jyBXBase/rpc/init"
	"jyBXBase/rpc/internal/svc"
	"jyBXBase/rpc/util"
)

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

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

// 获取收藏列表
func (l *ListLogic) List(in *bxbase.ListReq) (*bxbase.ListRes, error) {
	var ldata bxbase.ColList
	pagesize_max := 50
	isPay, _ := util.Power(in.UserId)
	if !isPay {
		pagesize_max = 100
	}
	if in.Pagesize != 0 {
		pagesize_max = int(in.Pagesize)
	}
	sql := util.CollListSql(in, isPay, in.UserId)
	logx.Info(sql)
	data := IC.MainMysql.SelectBySql(sql)
	count := 0
	result := []map[string]interface{}{}
	if data != nil && len(*data) > 0 {
		if in.Pagenum <= 0 {
			in.Pagenum = 1
		}
		start := (int(in.Pagenum) - 1) * pagesize_max
		end := int(in.Pagenum) * pagesize_max
		if end > len(*data) {
			end = len(*data)
		}
		if start < len(*data) {
			result = (*data)[start:end]
		}
		count = len(*data)
	}
	haveNextPage := len(result) >= pagesize_max
	ldata.Count = int64(count)
	ldata.HaveNextPage = haveNextPage
	ldata.Pagesize = int64(pagesize_max)
	var res []*bxbase.ColData
	mres := util.GetInfoById(IC.MgoBidding, "collection", "bidding_back", result)
	for _, v := range mres {
		var r bxbase.ColData
		r.Id = v.Id
		r.Title = v.Title
		r.Buyerclass = v.Buyerclass
		r.Buyer = v.Buyer
		r.Type = v.Type
		r.Budget = common.InterfaceToStr(v.Budget)
		r.Area = v.Area
		r.Bidamount = common.InterfaceToStr(v.Bidamount)
		r.Bidopentime = v.Bidopentime
		r.Publishtime = v.Publishtime
		r.SSubscopeclass = v.S_subscopeclass
		r.SWinner = v.S_winner
		res = append(res, &r)
	}
	ldata.Res = res

	return &bxbase.ListRes{
		Ldata: &ldata,
	}, nil
}
