<commit_before>package resolves

import (
	// "log"
	// "strconv"
	// "strings"
	"time"

	"github.com/hysios/apiai-go"
	"github.com/wanliu/brain_data/database"
	"github.com/wanliu/flow/builtin/ai"
	"github.com/wanliu/flow/builtin/config"
	. "github.com/wanliu/flow/context"
)

type PatchOrderResolve struct {
	OrderResolve
	Origin          *OrderResolve
	OriginUpdatedAt time.Time
}

func NewPatchOrderResolve(ctx Context) *PatchOrderResolve {
	resolve := new(PatchOrderResolve)

	aiResult := ctx.Value("Result").(apiai.Result)

	resolve.AiParams = ai.ApiAiOrder{AiResult: aiResult}
	resolve.ExtractFromParams()

	syncQueue := ctx.Value(config.CtxKeySyncQueue)
	if syncQueue != nil {
		resolve.OrderSyncQueue = syncQueue.(string)
	}

	if viewer := ctx.CtxValue("Viewer"); viewer != nil {
		user := viewer.(*database.User)
		resolve.User = user
	}

	return resolve
}

func (r *PatchOrderResolve) Patch(orderResolve *OrderResolve) {
	r.Origin = orderResolve

	orderResolve.Products.Patch(r.Products)
	orderResolve.Gifts.Patch(r.Gifts)

	if r.Address != "" && r.Origin.Address == "" {
		r.Origin.Address = r.Address
	}

	if r.Customer != "" && r.Origin.Customer == "" {
		r.Origin.Customer = r.Customer
	}

	if r.OrderSyncQueue != "" {
		r.Origin.OrderSyncQueue = r.OrderSyncQueue
	}

	r.OriginUpdatedAt = r.Origin.UpdatedAt
	// r.Origin.Touch()
}

// 新增 2 种产品, 《伊利畅轻450原味》 已 10 件,
// 《伊利燕麦有机 》已 19 件
// func (r PatchOrderResolve) Answer() string {
// 	if nil == r.Origin {
// 		return "ERROR"
// 	}

// 	shtMns := config.PatchShortMinutes

// 	if r.Origin.Fulfiled() {
// 		return r.LongAnswer()
// 	} else if r.WithinShortMinute(shtMns) {
// 		return r.ShortAnswer()
// 	} else {
// 		return r.LongAnswer()
// 	}
// }

// func (r PatchOrderResolve) WithinShortMinute(mins int) bool {
// 	return r.OriginUpdatedAt.Add(time.Duration(mins)*time.Minute).UnixNano() > time.Now().UnixNano()<commit_msg>debug sync queue<commit_after>package resolves

import (
	"log"
	// "strconv"
	// "strings"
	"time"

	"github.com/hysios/apiai-go"
	"github.com/wanliu/brain_data/database"
	"github.com/wanliu/flow/builtin/ai"
	"github.com/wanliu/flow/builtin/config"
	. "github.com/wanliu/flow/context"
)

type PatchOrderResolve struct {
	OrderResolve
	Origin          *OrderResolve
	OriginUpdatedAt time.Time
}

func NewPatchOrderResolve(ctx Context) *PatchOrderResolve {
	resolve := new(PatchOrderResolve)

	aiResult := ctx.Value("Result").(apiai.Result)

	resolve.AiParams = ai.ApiAiOrder{AiResult: aiResult}
	resolve.ExtractFromParams()

	syncQueue := ctx.Value(config.CtxKeySyncQueue)
	if syncQueue != nil {
		resolve.OrderSyncQueue = syncQueue.(string)
	}

	if viewer := ctx.CtxValue("Viewer"); viewer != nil {
		user := viewer.(*database.User)
		resolve.User = user
	}

	return resolve
}

func (r *PatchOrderResolve) Patch(orderResolve *OrderResolve) {
	r.Origin = orderResolve

	orderResolve.Products.Patch(r.Products)
	orderResolve.Gifts.Patch(r.Gifts)

	if r.Address != "" && r.Origin.Address == "" {
		r.Origin.Address = r.Address
	}

	if r.Customer != "" && r.Origin.Customer == "" {
		r.Origin.Customer = r.Customer
	}

	log.Printf("New Synce Queue: %v, orign Synce Queue: %v", r.OrderSyncQueue, r.Origin.OrderSyncQueue)
	if r.OrderSyncQueue != "" {
		r.Origin.OrderSyncQueue = r.OrderSyncQueue
	}

	r.OriginUpdatedAt = r.Origin.UpdatedAt
	// r.Origin.Touch()
}

// 新增 2 种产品, 《伊利畅轻450原味》 已 10 件,
// 《伊利燕麦有机 》已 19 件
// func (r PatchOrderResolve) Answer() string {
// 	if nil == r.Origin {
// 		return "ERROR"
// 	}

// 	shtMns := config.PatchShortMinutes

// 	if r.Origin.Fulfiled() {
// 		return r.LongAnswer()
// 	} else if r.WithinShortMinute(shtMns) {
// 		return r.ShortAnswer()
// 	} else {
// 		return r.LongAnswer()
// 	}
// }

// func (r PatchOrderResolve) WithinShortMinute(mins int) bool {
// 	return r.OriginUpdatedAt.Add(time.Duration(mins)*time.Minute).UnixNano() > time.Now().UnixNano()
{'fraction_non_alphanumeric': 0.1585710614237985, 'fraction_numerical': 0.004112053456694937, 'mean_word_length': 3.638855780691299, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 5, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1858614', 'n_tokens_mistral': 1581, 'n_tokens_neox': 1553, 'n_words': 251}