fix(listing): 外部上传归属分销站点
This commit is contained in:
@@ -164,17 +164,22 @@ func (r *Repository) ensureCreateCooldown(tx *gorm.DB, ownerID uint64, cooldown
|
||||
}
|
||||
|
||||
type externalUploadCreate struct {
|
||||
UploaderName string
|
||||
SourceChannel string
|
||||
ClientUploadTime *time.Time
|
||||
ClientIP string
|
||||
RawPayload []byte
|
||||
ParsedPayload []byte
|
||||
UploaderName string
|
||||
SourceChannel string
|
||||
DistributionSiteCode string
|
||||
ClientUploadTime *time.Time
|
||||
ClientIP string
|
||||
RawPayload []byte
|
||||
ParsedPayload []byte
|
||||
}
|
||||
|
||||
func (r *Repository) CreateFromExternalUpload(ctx context.Context, upload externalUploadCreate, req CreateRequest) (*ListingDTO, error) {
|
||||
var dto *ListingDTO
|
||||
err := r.db.WithContext(ctx).Transaction(func(tx *gorm.DB) error {
|
||||
site, distributionSiteID, err := resolveExternalUploadSite(tx, upload.DistributionSiteCode)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
listingNo, err := r.nextListingNo(tx, time.Now())
|
||||
if err != nil {
|
||||
return err
|
||||
@@ -183,7 +188,7 @@ func (r *Repository) CreateFromExternalUpload(ctx context.Context, upload extern
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
owner, err := r.ensureUploadOwnerUser(tx, admin)
|
||||
owner, err := r.ensureUploadOwnerUser(tx, admin, distributionSiteID)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
@@ -214,22 +219,33 @@ func (r *Repository) CreateFromExternalUpload(ctx context.Context, upload extern
|
||||
}
|
||||
priceCent := normalizedListingPriceCent(req)
|
||||
listing := model.RentalListing{
|
||||
SourceTenantID: owner.TenantID,
|
||||
ListingNo: listingNo,
|
||||
AccountID: account.ID,
|
||||
OwnerID: owner.ID,
|
||||
PriceCent: priceCent,
|
||||
DepositAmountCent: req.DepositAmountCent,
|
||||
Status: "draft",
|
||||
ReviewStatus: "pending",
|
||||
HandoffMode: "platform",
|
||||
SettlementMode: "platform_managed",
|
||||
ManagedAdminID: &admin.ID,
|
||||
SourceChannel: upload.SourceChannel,
|
||||
SourceTenantID: owner.TenantID,
|
||||
ListingNo: listingNo,
|
||||
AccountID: account.ID,
|
||||
OwnerID: owner.ID,
|
||||
PriceCent: priceCent,
|
||||
DepositAmountCent: req.DepositAmountCent,
|
||||
Status: "draft",
|
||||
ReviewStatus: "pending",
|
||||
HandoffMode: "platform",
|
||||
SettlementMode: "platform_managed",
|
||||
ManagedAdminID: &admin.ID,
|
||||
DistributionSiteID: distributionSiteID,
|
||||
SourceChannel: upload.SourceChannel,
|
||||
}
|
||||
if err := tx.Create(&listing).Error; err != nil {
|
||||
return err
|
||||
}
|
||||
if distributionSiteID != nil {
|
||||
if err := tx.Create(&model.DistributionSiteAccount{
|
||||
SiteID: *distributionSiteID,
|
||||
AccountID: account.ID,
|
||||
ListingID: listing.ID,
|
||||
DisplayStatus: "enabled",
|
||||
}).Error; err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
var conversationID uint64
|
||||
if r.chatCreator != nil {
|
||||
convID, err := r.chatCreator.EnsureListingConversation(tx, listing, admin.ID)
|
||||
@@ -257,12 +273,32 @@ func (r *Repository) CreateFromExternalUpload(ctx context.Context, upload extern
|
||||
return err
|
||||
}
|
||||
dto = toDTO(account, listing)
|
||||
if distributionSiteID != nil {
|
||||
dto.DistributionSiteID = distributionSiteID
|
||||
dto.DistributionSiteCode = site.SiteCode
|
||||
dto.DistributionSiteName = site.SiteName
|
||||
}
|
||||
dto.ListingGroupConversationID = conversationID
|
||||
return nil
|
||||
})
|
||||
return dto, err
|
||||
}
|
||||
|
||||
func resolveExternalUploadSite(tx *gorm.DB, siteCode string) (*model.DistributionSite, *uint64, error) {
|
||||
siteCode = strings.TrimSpace(siteCode)
|
||||
if siteCode == "" {
|
||||
return nil, nil, nil
|
||||
}
|
||||
var site model.DistributionSite
|
||||
if err := tx.Where("site_code = ? AND status = ?", siteCode, "active").First(&site).Error; err != nil {
|
||||
if errors.Is(err, gorm.ErrRecordNotFound) {
|
||||
return nil, nil, ErrDistributionSiteInvalid
|
||||
}
|
||||
return nil, nil, err
|
||||
}
|
||||
return &site, &site.ID, nil
|
||||
}
|
||||
|
||||
func (r *Repository) Update(ctx context.Context, ownerID uint64, listingID uint64, req UpdateRequest, reviewRequired bool) (*ListingDTO, error) {
|
||||
var dto *ListingDTO
|
||||
err := r.db.WithContext(ctx).Transaction(func(tx *gorm.DB) error {
|
||||
@@ -383,7 +419,7 @@ func (r *Repository) findActiveUploadAdmin(tx *gorm.DB, uploaderName string) (*m
|
||||
}
|
||||
}
|
||||
|
||||
func (r *Repository) ensureUploadOwnerUser(tx *gorm.DB, admin *model.AdminUser) (*model.User, error) {
|
||||
func (r *Repository) ensureUploadOwnerUser(tx *gorm.DB, admin *model.AdminUser, distributionSiteID *uint64) (*model.User, error) {
|
||||
phone := fmt.Sprintf("admin:%d", admin.ID)
|
||||
nickname := strings.TrimSpace(admin.Nickname)
|
||||
if nickname == "" {
|
||||
@@ -391,7 +427,11 @@ func (r *Repository) ensureUploadOwnerUser(tx *gorm.DB, admin *model.AdminUser)
|
||||
}
|
||||
var user model.User
|
||||
tenantID := userTenantID(tx.Statement.Context, tx, 0)
|
||||
userQuery := tx.Where("phone = ?", phone)
|
||||
siteID := uint64(0)
|
||||
if distributionSiteID != nil {
|
||||
siteID = *distributionSiteID
|
||||
}
|
||||
userQuery := tx.Where("phone = ? AND distribution_site_id = ?", phone, siteID)
|
||||
if tenantID > 0 {
|
||||
userQuery = userQuery.Where("tenant_id = ?", tenantID)
|
||||
}
|
||||
@@ -414,13 +454,14 @@ func (r *Repository) ensureUploadOwnerUser(tx *gorm.DB, admin *model.AdminUser)
|
||||
return nil, err
|
||||
}
|
||||
user = model.User{
|
||||
TenantID: tenantID,
|
||||
Phone: phone,
|
||||
Nickname: nickname,
|
||||
RealnameStatus: "verified",
|
||||
RiskStatus: "normal",
|
||||
CreditScore: 100,
|
||||
Status: "active",
|
||||
TenantID: tenantID,
|
||||
DistributionSiteID: siteID,
|
||||
Phone: phone,
|
||||
Nickname: nickname,
|
||||
RealnameStatus: "verified",
|
||||
RiskStatus: "normal",
|
||||
CreditScore: 100,
|
||||
Status: "active",
|
||||
}
|
||||
if err := tx.Create(&user).Error; err != nil {
|
||||
return nil, err
|
||||
|
||||
Reference in New Issue
Block a user