package biz

import (
	"context"
	"encoding/json"
	"fmt"
	"github.com/go-kratos/kratos/v2/transport/http"
	"time"
	"voucher/internal/biz/do"
)

func (this *VoucherBiz) PushRetryOrderNotice(ctx http.Context, bodyBytes []byte) error {

	_, err := this.rdb.Rdb.RPush(ctx, "retryQueryNotice", string(bodyBytes)).Result()
	if err != nil {
		return fmt.Errorf("添加到队列失败:%v", err)
	}

	return nil
}

func (this *VoucherBiz) PushOrderNotifyRetry(ctx http.Context, req *do.OrderNotifyRetry) error {

	queue := this.bc.RdsMQ.GetOrderNotifyRetry()
	if queue == nil {
		return fmt.Errorf("队列不存在")
	}

	msg, err := json.Marshal(req)
	if err != nil {
		return err
	}

	strMsg := string(msg)

	_, err = this.rdb.Rdb.RPush(ctx, queue.Name, strMsg).Result()
	if err != nil {
		return fmt.Errorf("添加到队列失败:%v", err)
	}

	return nil
}

func (this *VoucherBiz) OrderNotifyRetry(ctx context.Context, msg string) error {

	var req *do.OrderNotifyRetry

	if err := json.Unmarshal([]byte(msg), &req); err != nil {
		return err
	}

	if req.StartTime == "" || req.EndTime == "" {
		return fmt.Errorf("start_time or end_time is empty")
	}

	start, err := time.Parse(time.DateTime, req.StartTime)
	if err != nil {
		return err
	}
	end, err := time.Parse(time.DateTime, req.EndTime)
	if err != nil {
		return err
	}

	return this.timeSliceQuery(ctx, start, end)
}
