package logic

import (
	"context"
	"encoding/json"
	"errors"
	"fmt"
	"fusenapi/constants"
	"fusenapi/model/gmodel"
	"fusenapi/utils/auth"
	"fusenapi/utils/basic"
	"fusenapi/utils/format"
	"gorm.io/gorm"
	"strings"

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

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

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

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

// 处理进入前逻辑w,r
// func (l *GetProductStepPriceLogic) BeforeLogic(w http.ResponseWriter, r *http.Request) {
// }

func (l *GetProductStepPriceLogic) GetProductStepPrice(req *types.GetProductStepPriceReq, userinfo *auth.UserInfo) (resp *basic.Response) {
	if req.ProductId <= 0 {
		return resp.SetStatusWithMessage(basic.CodeRequestParamsErr, "err param:product id")
	}
	//获取产品信息(只是获取id)
	_, err := l.svcCtx.AllModels.FsProduct.FindOne(l.ctx, req.ProductId, "id")
	if err != nil {
		if errors.Is(err, gorm.ErrRecordNotFound) {
			return resp.SetStatusWithMessage(basic.CodeDbRecordNotFoundErr, "the product is not exists")
		}
		logx.Error(err)
		return resp.SetStatusWithMessage(basic.CodeDbSqlErr, "failed to get product info")
	}
	//查询产品价格
	modelPriceList, err := l.svcCtx.AllModels.FsProductModel3d.GetAllByProductIdTag(l.ctx, req.ProductId, constants.TAG_MODEL, "id,size_id,part_list,part_id,step_price,packed_unit")
	if err != nil {
		logx.Error(err)
		return resp.SetStatusWithMessage(basic.CodeDbSqlErr, "failed to get model price list")
	}
	fittingIds := make([]int64, 0, len(modelPriceList))
	for _, v := range modelPriceList {
		*v.PartList = strings.Trim(*v.PartList, ",")
		if *v.PartList != "" {
			tmpPartIds, err := format.StrSlicToInt64Slice(strings.Split(*v.PartList, ","))
			if err != nil {
				logx.Error(err)
				return resp.SetStatusWithMessage(basic.CodeServiceErr, "failed to parse model part list ")
			}
			fittingIds = append(fittingIds, tmpPartIds...)
		}
	}
	//查询配件价格列表
	fittingPriceList, err := l.svcCtx.AllModels.FsProductModel3d.GetAllByIdsTag(l.ctx, fittingIds, constants.TAG_PARTS, "id,price,packed_unit")
	if err != nil {
		logx.Error(err)
		return resp.SetStatusWithMessage(basic.CodeDbSqlErr, "failed to get fitting price list")
	}
	mapFitting := make(map[int64]gmodel.FsProductModel3d)
	for _, v := range fittingPriceList {
		mapFitting[v.Id] = v
	}
	//遍历处理模型价格
	mapRsp := make(map[string]interface{})
	for _, modelInfo := range modelPriceList {
		var stepPrice gmodel.StepPriceJsonStruct
		//没有设置阶梯价格
		if modelInfo.StepPrice == nil || len(*modelInfo.StepPrice) == 0 {
			continue
		}
		if err = json.Unmarshal(*modelInfo.StepPrice, &stepPrice); err != nil {
			logx.Error(err)
			return resp.SetStatusWithMessage(basic.CodeJsonErr, "failed to parse step price json")
		}
		*modelInfo.PartList = strings.Trim(*modelInfo.PartList, ",")
		mapFittingUnit := make(map[string]interface{})
		if *modelInfo.PartList != "" {
			tmpPartIds, err := format.StrSlicToInt64Slice(strings.Split(*modelInfo.PartList, ","))
			if err != nil {
				logx.Error(err)
				return resp.SetStatusWithMessage(basic.CodeServiceErr, "failed to parse model part list!! ")
			}
			for _, partId := range tmpPartIds {
				fittingInfo, ok := mapFitting[*modelInfo.PartId]
				if !ok {
					return resp.SetStatusWithMessage(basic.CodeServiceErr, fmt.Sprintf("fitting price is not exists:%d", *modelInfo.PartId))
				}
				mapFittingUnit[fmt.Sprintf("_%d", partId)] = map[string]interface{}{
					"packed_unit": *fittingInfo.PackedUnit, //装箱个数
				}
			}
		}
		mapRsp[fmt.Sprintf("_%d", *modelInfo.SizeId)] = map[string]interface{}{
			"min_buy_units_quantity": stepPrice.MinBuyUnitsNum, //起购数量
			"main_packed_unit":       *modelInfo.PackedUnit,    //主体装箱数
			"fitting":                mapFittingUnit,
		}
	}
	return resp.SetStatusWithMessage(basic.CodeOK, "success", mapRsp)
}

// 处理逻辑后 w,r 如:重定向, resp 必须重新处理
// func (l *GetProductStepPriceLogic) AfterLogic(w http.ResponseWriter, r *http.Request, resp *basic.Response) {
// // httpx.OkJsonCtx(r.Context(), w, resp)
// }