2026-07-28 13:55:40 +08:00
|
|
|
package gas
|
2026-07-27 02:03:26 +08:00
|
|
|
|
|
|
|
|
import (
|
|
|
|
|
"git.apinb.com/bsm-sdk/core/errcode"
|
|
|
|
|
"git.apinb.com/bsm-sdk/core/infra"
|
|
|
|
|
"git.apinb.com/heqiapp/platforms/backend/api/internal/impl"
|
2026-07-28 10:42:26 +08:00
|
|
|
"git.apinb.com/heqiapp/platforms/backend/api/internal/logic/common"
|
2026-07-27 02:03:26 +08:00
|
|
|
"git.apinb.com/heqiapp/platforms/backend/api/internal/models"
|
|
|
|
|
"github.com/gin-gonic/gin"
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
type accountRequest struct {
|
2026-07-27 10:11:07 +08:00
|
|
|
Username string `json:"username" binding:"required,max=64"`
|
|
|
|
|
Password string `json:"password" binding:"required,min=8,max=128"`
|
|
|
|
|
DisplayName string `json:"display_name" binding:"max=64"`
|
|
|
|
|
RoleCode string `json:"role_code" binding:"max=64"`
|
|
|
|
|
GasBasicIdentity string `json:"gas_basic_identity"`
|
|
|
|
|
DeliveryBasicIdentity string `json:"delivery_basic_identity"`
|
2026-07-27 02:03:26 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
type accountUpdateRequest struct {
|
2026-07-27 10:11:07 +08:00
|
|
|
DisplayName string `json:"display_name" binding:"max=64"`
|
|
|
|
|
RoleCode string `json:"role_code" binding:"max=64"`
|
|
|
|
|
GasBasicIdentity string `json:"gas_basic_identity"`
|
|
|
|
|
DeliveryBasicIdentity string `json:"delivery_basic_identity"`
|
2026-07-27 02:03:26 +08:00
|
|
|
}
|
|
|
|
|
|
2026-07-28 10:42:26 +08:00
|
|
|
func ListGasAccount(ctx *gin.Context) { common.ListPage[models.GasAccount](ctx) }
|
|
|
|
|
func GetGasAccount(ctx *gin.Context) { common.GetByIdentity[models.GasAccount](ctx) }
|
2026-07-27 02:03:26 +08:00
|
|
|
|
|
|
|
|
func CreateGasAccount(ctx *gin.Context) {
|
|
|
|
|
var request accountRequest
|
2026-07-27 10:11:07 +08:00
|
|
|
if err := ctx.ShouldBindJSON(&request); err != nil {
|
|
|
|
|
infra.Response.Error(ctx, errcode.ErrInvalidArgument)
|
|
|
|
|
return
|
|
|
|
|
}
|
2026-07-28 10:42:26 +08:00
|
|
|
gasBasicID, err := common.ResolveIdentityID(&models.GasBasic{}, request.GasBasicIdentity, true)
|
2026-07-27 10:11:07 +08:00
|
|
|
if err != nil {
|
2026-07-27 02:03:26 +08:00
|
|
|
infra.Response.Error(ctx, errcode.ErrInvalidArgument)
|
|
|
|
|
return
|
|
|
|
|
}
|
2026-07-28 13:55:40 +08:00
|
|
|
hash, err := common.PasswordHash(request.Password)
|
2026-07-27 02:03:26 +08:00
|
|
|
if err != nil {
|
|
|
|
|
infra.Response.Error(ctx, err)
|
|
|
|
|
return
|
|
|
|
|
}
|
2026-07-28 10:42:26 +08:00
|
|
|
account := models.GasAccount{Entity: common.NewEntity("enabled"), GasBasicID: gasBasicID, Username: request.Username, DisplayName: request.DisplayName, PasswordHash: hash, RoleCode: request.RoleCode}
|
2026-07-27 02:03:26 +08:00
|
|
|
if err := impl.DBService.Create(&account).Error; err != nil {
|
|
|
|
|
infra.Response.Error(ctx, err)
|
|
|
|
|
return
|
|
|
|
|
}
|
2026-07-28 10:42:26 +08:00
|
|
|
common.RespondCreatedResource(ctx, account)
|
2026-07-27 02:03:26 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func UpdateGasAccount(ctx *gin.Context) {
|
|
|
|
|
var request accountUpdateRequest
|
|
|
|
|
if err := ctx.ShouldBindJSON(&request); err != nil {
|
|
|
|
|
infra.Response.Error(ctx, errcode.ErrInvalidArgument)
|
|
|
|
|
return
|
|
|
|
|
}
|
2026-07-28 10:42:26 +08:00
|
|
|
gasBasicID, err := common.ResolveIdentityID(&models.GasBasic{}, request.GasBasicIdentity, true)
|
2026-07-27 10:11:07 +08:00
|
|
|
if err != nil {
|
|
|
|
|
infra.Response.Error(ctx, errcode.ErrInvalidArgument)
|
|
|
|
|
return
|
|
|
|
|
}
|
2026-07-28 10:42:26 +08:00
|
|
|
common.UpdateAllowedByIdentity(ctx, &models.GasAccount{}, gin.H{"gas_basic_id": gasBasicID, "display_name": request.DisplayName, "role_code": request.RoleCode}, []string{"gas_basic_id", "display_name", "role_code"})
|
2026-07-27 02:03:26 +08:00
|
|
|
}
|