package logic

import (
	"context"
	"errors"
	"fusenapi/model/gmodel"
	"fusenapi/utils/auth"
	"fusenapi/utils/basic"
	"fusenapi/utils/format"
	"fusenapi/utils/image"
	"gorm.io/gorm"
	"strings"

	"fusenapi/server/product/internal/svc"
	"fusenapi/server/product/internal/types"

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

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

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

func (l *OtherProductListLogic) OtherProductList(req *types.OtherProductListReq, userinfo *auth.UserInfo) (resp *basic.Response) {
	if req.Num <= 0 || req.Num > 100 {
		req.Num = 4
	}
	if req.Size > 0 {
		req.Size = image.GetCurrentSize(req.Size)
	}
	//获取用户信息
	user, err := l.svcCtx.AllModels.FsUser.FindUserById(l.ctx, userinfo.UserId)
	if err != nil && !errors.Is(err, gorm.ErrRecordNotFound) {
		logx.Error(err)
		return resp.SetStatusWithMessage(basic.CodeDbSqlErr, "failed to get user info")
	}
	tagInfo, err := l.svcCtx.AllModels.FsTags.FindOne(l.ctx, req.Cid)
	if err != nil {
		if errors.Is(err, gorm.ErrRecordNotFound) {
			return resp.SetStatusWithMessage(basic.CodeDbRecordNotFoundErr, "the tag info is not exists")
		}
		logx.Error(err)
		return resp.SetStatusWithMessage(basic.CodeDbSqlErr, "failed to get tage info")
	}
	if *tagInfo.Status != 1 {
		return resp.SetStatusWithMessage(basic.CodeDbSqlErr, "tage info is not available")
	}
	if tagInfo.RecommendProduct == nil || *tagInfo.RecommendProduct == "" {
		return resp.SetStatusWithMessage(basic.CodeOK, "success")
	}
	pids, err := format.StrSlicToInt64Slice(strings.Split(*tagInfo.RecommendProduct, ","))
	if err != nil {
		logx.Error(err)
		return resp.SetStatusWithMessage(basic.CodeServiceErr, "failed to parse recommend product")
	}
	list, err := l.getRandom(pids, int(req.Num))
	if err != nil {
		logx.Error(err)
		return resp.SetStatusWithMessage(basic.CodeDbSqlErr, "failed to get random list")
	}
	if req.Size <= 0 {
		return resp.SetStatusWithMessage(basic.CodeOK, "success,", list)
	}
	//千人前面处理
	for _, v := range list {
		r := &image.ThousandFaceImageFormatReq{
			Size:           int(req.Size),
			IsThousandFace: 0,
			Cover:          v.Cover,
			CoverImg:       v.CoverImg,
			CoverDefault:   "",
			ProductId:      v.Id,
			UserId:         user.Id,
		}
		if user.Id > 0 {
			r.IsThousandFace = int(*user.IsThousandFace)
		}
		image.ThousandFaceImageFormat(r)
		v.Cover = r.Cover
		v.CoverImg = r.CoverImg
		v.CoverDefault = r.CoverDefault
	}
	return resp.SetStatusWithMessage(basic.CodeOK, "success,", list)
}
func (l *OtherProductListLogic) getRandom(productIds []int64, limit int) (result []types.OtherProductListRsp, err error) {
	if len(productIds) == 0 {
		return
	}
	//获取推荐产品信息
	recommendProductList, err := l.svcCtx.AllModels.FsProduct.GetRandomProductListInIds(l.ctx, productIds, limit)
	if err != nil {
		return nil, err
	}
	if len(recommendProductList) == 0 {
		return
	}
	mapProduct := make(map[int64]int)
	newProductIds := make([]int64, 0, len(recommendProductList))
	for k, v := range recommendProductList {
		newProductIds = append(newProductIds, v.Id)
		mapProduct[v.Id] = k
	}
	//查询最新的sku
	reqStatus := int64(1)
	productTemplateIds, err := l.svcCtx.AllModels.FsProductTemplateV2.GetProductTemplateListByParams(l.ctx, gmodel.GetProductTemplateListByParamsReq{
		ProductIds: newProductIds,
		GroupBy:    "product_id",
		OrderBy:    "id DESC,sort DESC",
		Fields:     "product_id,max(id) as id",
		Status:     &reqStatus,
	})
	if err != nil {
		return nil, err
	}
	for _, v := range productTemplateIds {
		productIndex, ok := mapProduct[*v.ProductId]
		if !ok {
			continue
		}
		productInfo := recommendProductList[productIndex]
		result = append(result, types.OtherProductListRsp{
			Title:    *productInfo.Title,
			Cover:    *productInfo.Cover,
			CoverImg: *productInfo.CoverImg,
			Sn:       *productInfo.Sn,
			Id:       *v.ProductId,
			SkuId:    v.Id,
		})
	}
	return result, nil
}