Home > Backend Development > Golang > Golang implementation of bargaining logic

Golang implementation of bargaining logic

WBOY
Release: 2023-05-10 09:45:06
Original
470 people have browsed it

Bargaining activities are a form of promotion popular in e-commerce and social platforms. Participants can obtain preferential prices for goods through haggling within a certain period of time. However, the implementation of price bargaining logic is not simple, and issues such as the relationship between participants and price control need to be taken into consideration.

This article will introduce how to use Golang to implement price bargaining logic.

1. The basic logic of bargaining

The basic logic of bargaining can be summarized as follows:

  1. Create a bargaining activity: The initiator of the activity selects a product and sets Set the original price and bargaining period, and invite others to participate in bargaining.
  2. Participate in bargaining: Event participants lower the price of goods by initiating bargaining. The bargaining range is randomly determined by the system, but will not be lower than a minimum value.
  3. Share bargaining: Participants can invite more people to participate in bargaining by sharing bargaining links and increase their own bargaining opportunities.
  4. Successful bargaining: When the price of the product drops to a certain level, the bargaining is considered successful, and the user can obtain corresponding discounts.

2. Golang implements bargaining logic

Let’s introduce how to use Golang to implement bargaining logic. First we need to define some data structures:

  1. Product information

type Product struct {

ID          int     // 商品ID
Name        string  // 商品名称
OriginalPrice   float32 // 商品原价
CurrentPrice    float32 // 当前价格
MinPriceDelta   float32 // 最小砍价幅度
MinPrice    float32 // 最低价格
Participants    map[int]*Participant // 参与者列表
ChoppedLogs map[int]float32 // 砍价日志
StartTime   time.Time // 开始时间
EndTime     time.Time // 结束时间
Copy after login

}

Among them, Participants represents The list of participants, ChoppedLogs records the extent of each price bargain made by the user, and StartTime and EndTime represent the price bargaining period.

  1. Participant information

type Participant struct {

ID      int     // 参与者ID
Name    string  // 参与者名称
AmountChopped   float32 // 已砍金额
JoinedTime  time.Time // 加入时间
InviterID   int     // 邀请者ID
ProductID   int     // 商品ID
Invited     []*Participant // 被邀请人列表
Copy after login

}

In participant information, AmountChopped indicates that the participant is The amount that has been cut off from the current product, InviterID records the ID of the inviter, and Invited records the list of invitees.

  1. Hagging log

type ChoppedLog struct {

ParticipantID   int     // 砍价者ID
ChoppedAmount   float32 // 砍价金额
ProductID   int     // 商品ID
CreatedTime time.Time // 砍价时间
Copy after login

}

In the bargaining log, the bargainer is recorded ID, bargaining amount, product ID and bargaining time.

Through the above definition, we can write the following bargaining logic:

  1. Create a bargaining activity

func NewProduct(name string, originalPrice, minPriceDelta, minPrice float32, startTime, endTime time.Time) *Product {

return &Product{
    Name:           name,
    OriginalPrice:  originalPrice,
    CurrentPrice:   originalPrice,
    MinPriceDelta:  minPriceDelta,
    MinPrice:       minPrice,
    Participants:   make(map[int]*Participant),
    ChoppedLogs:    make(map[int]float32),
    StartTime:      startTime,
    EndTime:        endTime,
}
Copy after login

}

  1. Participate in price bargaining

func (p Product) Join(participant Participant) error {

if participant.JoinedTime.Before(p.StartTime) || participant.JoinedTime.After(p.EndTime) {
    return fmt.Errorf("参与时间错误")
}
if p.CurrentPrice <= p.MinPrice {
    return fmt.Errorf("价格已经到达最低价,不能再砍价了。")
}
id := len(p.Participants) + 1
participant.ID = id
participant.ProductID = p.ID
p.Participants[id] = participant
return nil
Copy after login

}

  1. Share bargain

func (p *Product) Invite(participantID , invitedID int) error {

if _, ok := p.Participants[participantID]; !ok {
    return fmt.Errorf("该用户未参加本次砍价活动")
}
if _, ok := p.Participants[invitedID]; !ok {
    return fmt.Errorf("该用户未在砍价活动中")
}
if participantID == invitedID {
    return fmt.Errorf("不允许自己邀请自己")
}
p.Participants[participantID].Invited = append(p.Participants[participantID].Invited, p.Participants[invitedID])
p.Participants[invitedID].InviterID = participantID
return nil
Copy after login

}

  1. Bargaining successful

func (p *Product) Chop(participantID int) error {

if _, ok := p.Participants[participantID]; !ok {
    return fmt.Errorf("该用户未参加本次砍价活动")
}
if p.CurrentPrice <= p.MinPrice {
    return fmt.Errorf("提前到达底价,不能再砍价了。")
}
num := rand.Intn(10) // 随机砍价幅度
chopAmount := p.MinPriceDelta + float32(num)
if chopAmount >= p.CurrentPrice-p.MinPrice {
    chopAmount = p.CurrentPrice - p.MinPrice
}
p.CurrentPrice -= chopAmount
p.Participants[participantID].AmountChopped += chopAmount
p.ChoppedLogs[participantID] = chopAmount
if p.CurrentPrice <= p.MinPrice {
    p.CurrentPrice = p.MinPrice
}
return nil
Copy after login

}

Through the above code, we can implement basic bargaining logic, including basic operations such as creating bargaining activities, participating in bargaining, sharing bargaining, and bargaining success. However, these codes are far from meeting the needs of practical applications, because we still need to consider the following issues:

  1. How to prevent some users from obtaining the lowest price of goods through malicious bargaining?
  2. How to control the bargaining range so that the price fluctuates within a reasonable range?
  3. How to design bargaining rules and invitation mechanisms so that bargaining activities can attract more users to participate?

We also need to deal with the above issues according to specific business needs.

3. Summary

Implementing bargaining logic through Golang can allow us to better understand the implementation principles of bargaining activities. However, in actual development, we also need to consider other issues, such as concurrent processing, preventing fraudulent orders, etc. These issues also require us to deal with specific business scenarios. I believe that with continuous practice, we can gradually master the skills of implementing bargaining activities and make greater contributions to the development of e-commerce and social platforms.

The above is the detailed content of Golang implementation of bargaining logic. For more information, please follow other related articles on the PHP Chinese website!

source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template