Merge branch 'develop' of gitee.com:fusenpack/fusenapi into develop

This commit is contained in:
laodaming 2023-10-25 12:30:43 +08:00
commit 5855ba8dde
3 changed files with 109 additions and 48 deletions

View File

@ -27,24 +27,34 @@ type PreLogoSearchResult struct {
// 搜索
func (p *FsPreprocessLogoModel) PreLogoSearch(ctx context.Context, zipcode string, keywordsStr string, count int) (resp []PreLogoSearchResult, err error) {
keywords := regexp.MustCompile(`\s+`).Split(keywordsStr, -1)
for i, v := range keywords {
keywords[i] = "+" + v + "*"
}
sqlstr := fmt.Sprintf("SELECT id,restaurant_name,resource_url,address,zip_code,phone,website,is_branch FROM fs_preprocess_logo WHERE MATCH(restaurant_name) AGAINST(? IN BOOLEAN MODE) and zip_code = ? limit %d;", count)
tx := p.db.WithContext(ctx).Model(&FsPreprocessLogo{}).Raw(sqlstr, strings.Join(keywords, " "), zipcode)
err = tx.Scan(&resp).Error
if err != nil {
logx.Error(err)
return nil, err
}
for i := range resp {
if resp[i].RestaurantType == nil {
resp[i].RestaurantType = FsString("")
keywordsList := SplitSearchKeywords(keywordsStr)
var keywords []string
for _, v := range keywordsList {
if len(v) > 1 {
keywords = append(keywords, "+"+v+"*")
}
}
if len(keywords) != 0 {
sqlstr := fmt.Sprintf("SELECT id,restaurant_name,resource_url,address,zip_code,phone,website,is_branch FROM fs_preprocess_logo WHERE MATCH(restaurant_name) AGAINST(? IN BOOLEAN MODE) and zip_code = ? limit %d;", count)
tx := p.db.WithContext(ctx).Model(&FsPreprocessLogo{}).Raw(sqlstr, strings.Join(keywords, " "), zipcode)
err = tx.Scan(&resp).Error
if err != nil {
logx.Error(err)
return nil, err
}
for i := range resp {
if resp[i].RestaurantType == nil {
resp[i].RestaurantType = FsString("")
}
if len(*resp[i].ResourceUrl) < 10 {
resp[i].ResourceUrl = FsString(testData[rand.Uint64()%uint64(len(testData))])
}
}
resp[i].ResourceUrl = FsString(testData[rand.Uint64()%uint64(len(testData))])
}
if resp == nil {
@ -54,26 +64,42 @@ func (p *FsPreprocessLogoModel) PreLogoSearch(ctx context.Context, zipcode strin
return resp, nil
}
func SplitSearchKeywords(keywordsStr string) []string {
return regexp.MustCompile(`\s+|\++`).Split(keywordsStr, -1)
}
// 搜索建议
func (p *FsPreprocessLogoModel) PreLogoSearchSuggestions(ctx context.Context, keywordsStr string, count int) (resp []PreLogoSearchResult, err error) {
keywords := regexp.MustCompile(`\s+`).Split(keywordsStr, -1)
for i, v := range keywords {
keywords[i] = "+" + v + "*"
}
sqlstr := fmt.Sprintf("SELECT id,restaurant_name,resource_url,address,zip_code,phone,website,is_branch FROM fs_preprocess_logo WHERE MATCH(restaurant_name) AGAINST(? IN BOOLEAN MODE) limit %d;", count)
tx := p.db.WithContext(ctx).Model(&FsPreprocessLogo{}).Raw(sqlstr, strings.Join(keywords, " "))
err = tx.Scan(&resp).Error
if err != nil {
logx.Error(err)
return nil, err
}
for i := range resp {
if resp[i].RestaurantType == nil {
resp[i].RestaurantType = FsString("")
// keywordsList := regexp.MustCompile(`\s+|\++`).Split(keywordsStr, -1)
keywordsList := SplitSearchKeywords(keywordsStr)
var keywords []string
for _, v := range keywordsList {
if len(v) > 1 {
keywords = append(keywords, "+"+v+"*")
}
resp[i].ResourceUrl = FsString(testData[rand.Uint64()%uint64(len(testData))])
}
if len(keywords) != 0 {
sqlstr := fmt.Sprintf("SELECT id,restaurant_name,resource_url,address,zip_code,phone,website,is_branch FROM fs_preprocess_logo WHERE MATCH(restaurant_name) AGAINST(? IN BOOLEAN MODE) limit %d;", count)
tx := p.db.WithContext(ctx).Model(&FsPreprocessLogo{}).Raw(sqlstr, strings.Join(keywords, " "))
err = tx.Scan(&resp).Error
if err != nil {
logx.Error(err)
return nil, err
}
for i := range resp {
if resp[i].RestaurantType == nil {
resp[i].RestaurantType = FsString("")
}
if len(*resp[i].ResourceUrl) < 10 {
resp[i].ResourceUrl = FsString(testData[rand.Uint64()%uint64(len(testData))])
}
}
}
if resp == nil {

View File

@ -9,6 +9,7 @@ import (
"fusenapi/model/gmodel"
"fusenapi/utils/basic"
"fusenapi/utils/file"
"fusenapi/utils/format"
"fusenapi/utils/handlers"
"fusenapi/utils/hash"
"fusenapi/utils/order"
@ -16,6 +17,7 @@ import (
"fusenapi/utils/pdf"
"fusenapi/utils/queue"
"math"
"strconv"
"time"
"github.com/aws/aws-sdk-go/aws/session"
@ -374,19 +376,49 @@ func (d *defaultOrder) Invoice(ctx context.Context, in *InvoiceReq) (res *Invoic
state = ress.OrderDetail.DeliveryAddress.State
suite = ress.OrderDetail.DeliveryAddress.Suite
zipCode = ress.OrderDetail.DeliveryAddress.ZipCode
} else {
// 邮箱
modelUser := d.MysqlConn.Model(&gmodel.FsUser{})
if in.UserId != 0 {
modelUser = modelUser.Where("id = ?", in.UserId)
}
var userInfo = &gmodel.FsUser{}
result1 := modelUser.Debug().Take(&userInfo)
if result1.Error != nil {
if errors.Is(result1.Error, gorm.ErrRecordNotFound) {
errorCode = *basic.CodeErrOrderCreatePrePaymentInfoNoFound
} else {
errorCode = *basic.CodeServiceErr
}
logc.Errorf(ctx, "order invoice trade failed, err: %v", err)
return &InvoiceRes{
ErrorCode: errorCode,
}, result1.Error
}
if userInfo.Email != nil {
name = *userInfo.Email
}
}
var products string
for _, orderProduct := range ress.OrderDetail.OrderProduct {
var model00301 = constants.INVOICE_TEMPLATE_0301
var price = fmt.Sprintf("%s%s", constants.OrderCurrencyMessage[constants.Currency(orderProduct.ItemPrice.Current.CurrentCurrency)], orderProduct.ItemPrice.Current.CurrentAmount.(string))
var priceTotal = fmt.Sprintf("%s%s", constants.OrderCurrencyMessage[constants.Currency(orderProduct.TotalPrice.Current.CurrentCurrency)], orderProduct.TotalPrice.Current.CurrentAmount.(string))
var productsInfo = fmt.Sprintf(model00301, orderProduct.ProductName, price, orderProduct.PurchaseQuantity.Current, priceTotal)
var priceStr = format.NumToStringWithThousandthPercentile(orderProduct.ItemPrice.Current.CurrentAmount.(string))
var price = fmt.Sprintf("%s%s", constants.OrderCurrencyMessage[constants.Currency(orderProduct.ItemPrice.Current.CurrentCurrency)], priceStr)
var priceTotalStr = format.NumToStringWithThousandthPercentile(orderProduct.TotalPrice.Current.CurrentAmount.(string))
var priceTotal = fmt.Sprintf("%s%s", constants.OrderCurrencyMessage[constants.Currency(orderProduct.TotalPrice.Current.CurrentCurrency)], priceTotalStr)
productNum := strconv.FormatFloat(orderProduct.PurchaseQuantity.Current.(float64), 'f', -1, 64)
var productNumStr = format.NumToStringWithThousandthPercentile(productNum)
var productsInfo = fmt.Sprintf(model00301, orderProduct.ProductName, price, productNumStr, priceTotal)
products = products + productsInfo
}
model003 = fmt.Sprintf(constants.INVOICE_TEMPLATE_03, products)
var subtotal = fmt.Sprintf("%s%s", constants.OrderCurrencyMessage[constants.Currency(ress.OrderDetail.OrderAmount.Subtotal.Current.CurrentCurrency)], ress.OrderDetail.OrderAmount.Subtotal.Current.CurrentAmount.(string))
var subtotalStr = format.NumToStringWithThousandthPercentile(ress.OrderDetail.OrderAmount.Subtotal.Current.CurrentAmount.(string))
var subtotal = fmt.Sprintf("%s%s", constants.OrderCurrencyMessage[constants.Currency(ress.OrderDetail.OrderAmount.Subtotal.Current.CurrentCurrency)], subtotalStr)
var taxStr = "0.00"
if ress.OrderDetail.OrderAmount.Tax.Current.CurrentAmount != nil {
taxStr = ress.OrderDetail.OrderAmount.Tax.Current.CurrentAmount.(string)
@ -396,14 +428,16 @@ func (d *defaultOrder) Invoice(ctx context.Context, in *InvoiceReq) (res *Invoic
taxCurrency = constants.OrderCurrencyMessage[constants.Currency(ress.OrderDetail.OrderAmount.Total.Current.CurrentCurrency)]
}
var tax = fmt.Sprintf("%s%s", taxCurrency, taxStr)
var total = fmt.Sprintf("%s%s", constants.OrderCurrencyMessage[constants.Currency(ress.OrderDetail.OrderAmount.Total.Current.CurrentCurrency)], ress.OrderDetail.OrderAmount.Total.Current.CurrentAmount.(string))
var totalStr = format.NumToStringWithThousandthPercentile(ress.OrderDetail.OrderAmount.Total.Current.CurrentAmount.(string))
var total = fmt.Sprintf("%s%s", constants.OrderCurrencyMessage[constants.Currency(ress.OrderDetail.OrderAmount.Total.Current.CurrentCurrency)], totalStr)
// 生成收据发票--首款
if receiptSnsDeposit != "" {
model002 = fmt.Sprintf(constants.INVOICE_TEMPLATE_02, receiptSnsDeposit, name, ctimeDate, street+" "+suite, city, state+zipCode)
var payAmountStr01 = format.NumToStringWithThousandthPercentile(ress.OrderDetail.OrderAmount.Deposit.PayAmount.Current.CurrentAmount.(string))
v7 := "Deposit Requested"
v8 := fmt.Sprintf("%s%s", constants.OrderCurrencyMessage[constants.Currency(ress.OrderDetail.OrderAmount.Deposit.PayAmount.Current.CurrentCurrency)], ress.OrderDetail.OrderAmount.Deposit.PayAmount.Current.CurrentAmount.(string))
v8 := fmt.Sprintf("%s%s", constants.OrderCurrencyMessage[constants.Currency(ress.OrderDetail.OrderAmount.Deposit.PayAmount.Current.CurrentCurrency)], payAmountStr01)
v9 := "Deposit Due"
v10 := v8
model004 = fmt.Sprintf(constants.INVOICE_TEMPLATE_04, subtotal, tax, total, v7, v8, v9, v10)
@ -413,7 +447,7 @@ func (d *defaultOrder) Invoice(ctx context.Context, in *InvoiceReq) (res *Invoic
var content = model001 + model002 + model003 + model004 + model005 + model006
base64, err := pdf.HtmlToPdfBase64(content, "html")
base64, err := pdf.HtmlToPdfBase64(content, "html", "Letter")
if err != nil {
logc.Errorf(ctx, "order invoice HtmlToPdfBase64 failed, err: %v", err)
errorCode = *basic.CodeServiceErr
@ -455,9 +489,9 @@ func (d *defaultOrder) Invoice(ctx context.Context, in *InvoiceReq) (res *Invoic
// 生成收据发票--尾款
if receiptSnsFinal != "" {
model002 = fmt.Sprintf(constants.INVOICE_TEMPLATE_02, receiptSnsDeposit, name, ctimeDate, street+" "+suite, city, state+zipCode)
var payAmountStr02 = format.NumToStringWithThousandthPercentile(ress.OrderDetail.OrderAmount.RemainingBalance.PayAmount.Current.CurrentAmount.(string))
v7 := "Balance Requested"
v8 := fmt.Sprintf("%s%s", constants.OrderCurrencyMessage[constants.Currency(ress.OrderDetail.OrderAmount.RemainingBalance.PayAmount.Current.CurrentCurrency)], ress.OrderDetail.OrderAmount.RemainingBalance.PayAmount.Current.CurrentAmount.(string))
v8 := fmt.Sprintf("%s%s", constants.OrderCurrencyMessage[constants.Currency(ress.OrderDetail.OrderAmount.RemainingBalance.PayAmount.Current.CurrentCurrency)], payAmountStr02)
v9 := "Balance Due"
v10 := v8
model004 = fmt.Sprintf(constants.INVOICE_TEMPLATE_04, subtotal, tax, total, v7, v8, v9, v10)
@ -465,7 +499,7 @@ func (d *defaultOrder) Invoice(ctx context.Context, in *InvoiceReq) (res *Invoic
cardSn := "****" + *orderTradeFinal.CardSn
model005 = fmt.Sprintf(constants.INVOICE_TEMPLATE_05, *orderTradeDeposit.CardBrand, cardSn)
var content = model001 + model002 + model003 + model004 + model005 + model006
base64, err := pdf.HtmlToPdfBase64(content, "html")
base64, err := pdf.HtmlToPdfBase64(content, "html", "Letter")
if err != nil {
logc.Errorf(ctx, "order invoice HtmlToPdfBase64 failed, err: %v", err)
errorCode = *basic.CodeServiceErr

View File

@ -3,15 +3,16 @@ package pdf
import (
"encoding/base64"
"errors"
"github.com/SebastiaanKlippert/go-wkhtmltopdf"
"strings"
"github.com/SebastiaanKlippert/go-wkhtmltopdf"
)
/*
html转 Pdf
outFile为空则不保存(使用该方法需要安装工具 sudo apt-get install wkhtmltopdf)
*/
func HtmlToPdfBase64(content string, dataType string, outFile ...string) (string, error) {
func HtmlToPdfBase64(content string, dataType string, pageSize string, outFile ...string) (string, error) {
pdfg, err := wkhtmltopdf.NewPDFGenerator()
if err != nil {
return "", err
@ -27,7 +28,7 @@ func HtmlToPdfBase64(content string, dataType string, outFile ...string) (string
//模式
pdfg.Orientation.Set(wkhtmltopdf.OrientationPortrait)
//pdf尺寸默认A4纸
pdfg.PageSize.Set(wkhtmltopdf.PageSizeA4)
pdfg.PageSize.Set(pageSize)
// Create PDF document in memory
if err = pdfg.Create(); err != nil {
return "", err