package gas import ( "git.apinb.com/bsm-sdk/core/errcode" "git.apinb.com/bsm-sdk/core/infra" "git.apinb.com/heqiapp/platforms/backend/api/internal/impl" "git.apinb.com/heqiapp/platforms/backend/api/internal/logic/common" "git.apinb.com/heqiapp/platforms/backend/api/internal/models" "github.com/gin-gonic/gin" "gorm.io/gorm" ) func scopedDeliveryAccounts(databaseService *gorm.DB, gasBasicID uint64) *gorm.DB { return databaseService.Model(&models.DeliveryAccount{}). Joins("JOIN delivery_basic ON delivery_basic.id = delivery_account.delivery_basic_id"). Where("delivery_account.status <> ? AND delivery_basic.gas_basic_id = ?", common.StatusArchived, gasBasicID) } func ListDeliveryAccount(ctx *gin.Context) { station, ok := currentGas(ctx) if !ok { return } page, size := common.PageSize(ctx) var list []models.DeliveryAccount var total int64 query := common.ApplyKeywordFilter(ctx, scopedDeliveryAccounts(impl.DBService, station.ID), &models.DeliveryAccount{}) if err := query.Count(&total).Error; err != nil { infra.Response.Error(ctx, err) return } if err := query.Order("delivery_account.created_at desc").Offset((page - 1) * size).Limit(size).Find(&list).Error; err != nil { infra.Response.Error(ctx, err) return } respondList(ctx, list, total) } func GetDeliveryAccount(ctx *gin.Context) { station, ok := currentGas(ctx) if !ok { return } query := scopedDeliveryAccounts(impl.DBService, station.ID). Where("delivery_account.identity = ?", ctx.Param("identity")) respondScopedRecord(ctx, query, &models.DeliveryAccount{}) } func CreateDeliveryAccount(ctx *gin.Context) { station, ok := currentGas(ctx) if !ok { return } var request struct { DeliveryBasicIdentity string `json:"delivery_basic_identity" binding:"required"` Username string `json:"username" binding:"required,max=64"` Password string `json:"password" binding:"required"` DisplayName string `json:"display_name" binding:"max=64"` } if err := ctx.ShouldBindJSON(&request); err != nil || !common.IsValidAccountPassword(request.Password) { infra.Response.Error(ctx, errcode.ErrInvalidArgument) return } delivery, ok := requireDelivery(ctx, request.DeliveryBasicIdentity, station.ID) if !ok { return } hash, err := common.PasswordHash(request.Password) if err != nil { infra.Response.Error(ctx, err) return } account := models.DeliveryAccount{ Entity: common.NewEntity(common.StatusEnable), DeliveryBasicID: delivery.ID, Username: request.Username, PasswordHash: hash, DisplayName: request.DisplayName, RoleCode: "admin", } if err := impl.DBService.Create(&account).Error; err != nil { infra.Response.Error(ctx, err) return } common.RespondCreatedResource(ctx, account) } func UpdateDeliveryAccount(ctx *gin.Context) { station, ok := currentGas(ctx) if !ok { return } var account models.DeliveryAccount if err := scopedDeliveryAccounts(impl.DBService, station.ID). Select("delivery_account.*"). Where("delivery_account.identity = ?", ctx.Param("identity")). First(&account).Error; err != nil { infra.Response.Error(ctx, errcode.ErrInvalidArgument) return } var request struct { DisplayName string `json:"display_name" binding:"max=64"` } if err := ctx.ShouldBindJSON(&request); err != nil { infra.Response.Error(ctx, errcode.ErrInvalidArgument) return } common.UpdateAllowedByIdentity(ctx, &models.DeliveryAccount{}, gin.H{"display_name": request.DisplayName}, []string{"display_name"}) } func UpdateDeliveryAccountStatus(ctx *gin.Context) { station, ok := currentGas(ctx) if !ok { return } var account models.DeliveryAccount if err := scopedDeliveryAccounts(impl.DBService, station.ID). Select("delivery_account.*"). Where("delivery_account.identity = ?", ctx.Param("identity")). First(&account).Error; err != nil { infra.Response.Error(ctx, errcode.ErrInvalidArgument) return } common.UpdateRecordStatus(ctx, &models.DeliveryAccount{}) } func ArchiveDeliveryAccount(ctx *gin.Context) { UpdateDeliveryAccountStatusWithValue(ctx, common.StatusArchived) } func UpdateDeliveryAccountStatusWithValue(ctx *gin.Context, status int) { station, ok := currentGas(ctx) if !ok { return } result := impl.DBService.Model(&models.DeliveryAccount{}). Where("identity = ? AND status <> ? AND delivery_basic_id IN (SELECT id FROM delivery_basic WHERE gas_basic_id = ?)", ctx.Param("identity"), common.StatusArchived, station.ID). Update("status", status) if result.Error != nil || result.RowsAffected != 1 { infra.Response.Error(ctx, errcode.ErrInvalidArgument) return } infra.Response.Success(ctx, gin.H{"updated": true}) }