package share

import (
	"context"
	"encoding/json"
	"github.com/redis/go-redis/v9"
	"matchmaking-system/internal/biz/structure"
	"matchmaking-system/internal/pkg/logging/applogger"
	"matchmaking-system/internal/pkg/logging/common"
	"time"
)

// ShareBlkTallyCache
// @Description:
type ShareBlkTallyCache struct {
	UserId       int64                // 用户ID
	OrderId      string               // 订单ID
	Symbol       string               // 交易对
	Status       string               // 订单状态
	OpenPrice    string               // 开仓价格
	ClosingPrice string               // 平仓价格
	ClosingTime  time.Time            // 平仓时间
	Order        structure.ShareOrder // 下单信息
}

// ShareBklHashUserOrder
//
//	@Description:
//	@param red
//	@param cacheKey
//	@param order
//	@return error
func ShareBklHashUserOrder(red *redis.Client, cacheKey string, order *ShareBlkTallyCache) error {
	orderStr, err := json.Marshal(order)
	if err != nil {
		applogger.Error("%v ShareBklHashUserOrder.Marshal:%v", common.ErrShareBlk, err)
		return err
	}

	if err = red.HSet(context.Background(), cacheKey, order.OrderId, string(orderStr)).Err(); err != nil {
		applogger.Error("%v ShareBklHashUserOrder.HSet:%v", common.ErrShareBlk, err)
		return err
	}

	return nil
}