新增商品编号搜索
This commit is contained in:
@@ -50,6 +50,10 @@ func initialPublishState(reviewRequired bool) (string, string, *time.Time) {
|
||||
func (r *Repository) Create(ownerID uint64, req CreateRequest, reviewRequired bool) (*ListingDTO, error) {
|
||||
var dto *ListingDTO
|
||||
err := r.db.Transaction(func(tx *gorm.DB) error {
|
||||
listingNo, err := r.nextListingNo(tx, time.Now())
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
screenshots, err := marshalScreenshots(req.ScreenshotURLS)
|
||||
if err != nil {
|
||||
return err
|
||||
@@ -78,6 +82,7 @@ func (r *Repository) Create(ownerID uint64, req CreateRequest, reviewRequired bo
|
||||
price := normalizedListingPrice(req)
|
||||
depositAmount := roundMoney(req.DepositAmount)
|
||||
listing := model.RentalListing{
|
||||
ListingNo: listingNo,
|
||||
AccountID: account.ID,
|
||||
OwnerID: ownerID,
|
||||
Price: price,
|
||||
@@ -106,6 +111,10 @@ type externalUploadCreate struct {
|
||||
func (r *Repository) CreateFromExternalUpload(upload externalUploadCreate, req CreateRequest) (*ListingDTO, error) {
|
||||
var dto *ListingDTO
|
||||
err := r.db.Transaction(func(tx *gorm.DB) error {
|
||||
listingNo, err := r.nextListingNo(tx, time.Now())
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
admin, err := r.findActiveUploadAdmin(tx, upload.UploaderName)
|
||||
if err != nil {
|
||||
return err
|
||||
@@ -139,6 +148,7 @@ func (r *Repository) CreateFromExternalUpload(upload externalUploadCreate, req C
|
||||
return err
|
||||
}
|
||||
listing := model.RentalListing{
|
||||
ListingNo: listingNo,
|
||||
AccountID: account.ID,
|
||||
OwnerID: owner.ID,
|
||||
Price: normalizedListingPrice(req),
|
||||
@@ -1009,6 +1019,9 @@ func publicZoneCounts(items []ListingDTO) map[string]int64 {
|
||||
|
||||
func publicSearchText(item ListingDTO) string {
|
||||
parts := []string{
|
||||
item.ListingNo,
|
||||
strconv.FormatUint(item.ID, 10),
|
||||
strconv.FormatUint(item.AccountID, 10),
|
||||
item.Title,
|
||||
item.Description,
|
||||
item.RankLevel,
|
||||
@@ -1303,6 +1316,39 @@ func (r *Repository) findForReviewUpdate(tx *gorm.DB, listingID uint64) (*model.
|
||||
return &listing, &account, nil
|
||||
}
|
||||
|
||||
func (r *Repository) nextListingNo(tx *gorm.DB, now time.Time) (string, error) {
|
||||
bizDate := now.Format("20060102")
|
||||
if tx.Dialector.Name() == "mysql" {
|
||||
if err := tx.Exec(`
|
||||
INSERT INTO listing_no_sequences (biz_date, next_seq)
|
||||
VALUES (?, LAST_INSERT_ID(1))
|
||||
ON DUPLICATE KEY UPDATE next_seq = LAST_INSERT_ID(next_seq + 1)
|
||||
`, bizDate).Error; err != nil {
|
||||
return "", err
|
||||
}
|
||||
var seq int
|
||||
if err := tx.Raw("SELECT LAST_INSERT_ID()").Scan(&seq).Error; err != nil {
|
||||
return "", err
|
||||
}
|
||||
return fmt.Sprintf("%s%04d", bizDate, seq), nil
|
||||
}
|
||||
|
||||
var maxNo string
|
||||
if err := tx.Table("rental_listings").
|
||||
Select("COALESCE(MAX(listing_no), '')").
|
||||
Where("listing_no LIKE ?", bizDate+"%").
|
||||
Scan(&maxNo).Error; err != nil {
|
||||
return "", err
|
||||
}
|
||||
seq := 1
|
||||
if len(maxNo) > len(bizDate) {
|
||||
if parsed, err := strconv.Atoi(maxNo[len(bizDate):]); err == nil {
|
||||
seq = parsed + 1
|
||||
}
|
||||
}
|
||||
return fmt.Sprintf("%s%04d", bizDate, seq), nil
|
||||
}
|
||||
|
||||
type listingRow struct {
|
||||
model.RentalListing
|
||||
Title string
|
||||
@@ -1394,6 +1440,7 @@ func (row listingRow) toDTO() ListingDTO {
|
||||
reviewStatus, reviewReason := normalizedReviewState(row.Status, row.ReviewStatus, row.ReviewReason)
|
||||
return ListingDTO{
|
||||
ID: row.ID,
|
||||
ListingNo: row.ListingNo,
|
||||
AccountID: row.AccountID,
|
||||
OwnerID: row.OwnerID,
|
||||
OwnerPhone: row.OwnerPhone,
|
||||
@@ -1427,6 +1474,7 @@ func toDTO(account model.GameAccount, listing model.RentalListing) *ListingDTO {
|
||||
reviewStatus, reviewReason := normalizedReviewState(listing.Status, listing.ReviewStatus, listing.ReviewReason)
|
||||
return &ListingDTO{
|
||||
ID: listing.ID,
|
||||
ListingNo: listing.ListingNo,
|
||||
AccountID: account.ID,
|
||||
OwnerID: listing.OwnerID,
|
||||
Title: account.Title,
|
||||
|
||||
Reference in New Issue
Block a user