91 lines
3.2 KiB
Go
91 lines
3.2 KiB
Go
package gas
|
|
|
|
import (
|
|
"strings"
|
|
|
|
"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"
|
|
)
|
|
|
|
type accountRequest struct {
|
|
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"`
|
|
}
|
|
|
|
type accountUpdateRequest struct {
|
|
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"`
|
|
}
|
|
|
|
func ListGasAccount(ctx *gin.Context) {
|
|
identities := splitOwnerIdentities(ctx.Query("gas_basic_identities"))
|
|
if len(identities) > 100 {
|
|
infra.Response.Error(ctx, errcode.ErrInvalidArgument)
|
|
return
|
|
}
|
|
common.ListPageFiltered[models.GasAccount](ctx, func(query *gorm.DB) *gorm.DB {
|
|
if len(identities) == 0 {
|
|
return query
|
|
}
|
|
return query.Where("gas_basic_id IN (SELECT id FROM gas_basic WHERE identity IN ? AND status <> ?)", identities, common.StatusArchived)
|
|
})
|
|
}
|
|
func GetGasAccount(ctx *gin.Context) { common.GetByIdentity[models.GasAccount](ctx) }
|
|
|
|
func CreateGasAccount(ctx *gin.Context) {
|
|
var request accountRequest
|
|
if err := ctx.ShouldBindJSON(&request); err != nil {
|
|
infra.Response.Error(ctx, errcode.ErrInvalidArgument)
|
|
return
|
|
}
|
|
gasBasicID, err := common.ResolveIdentityID(&models.GasBasic{}, request.GasBasicIdentity, true)
|
|
if err != nil {
|
|
infra.Response.Error(ctx, errcode.ErrInvalidArgument)
|
|
return
|
|
}
|
|
hash, err := common.PasswordHash(request.Password)
|
|
if err != nil {
|
|
infra.Response.Error(ctx, err)
|
|
return
|
|
}
|
|
account := models.GasAccount{Entity: common.NewEntity(common.StatusEnable), GasBasicID: gasBasicID, Username: request.Username, DisplayName: request.DisplayName, PasswordHash: hash, RoleCode: request.RoleCode}
|
|
if err := impl.DBService.Create(&account).Error; err != nil {
|
|
infra.Response.Error(ctx, err)
|
|
return
|
|
}
|
|
common.RespondCreatedResource(ctx, account)
|
|
}
|
|
|
|
func UpdateGasAccount(ctx *gin.Context) {
|
|
var request accountUpdateRequest
|
|
if err := ctx.ShouldBindJSON(&request); err != nil {
|
|
infra.Response.Error(ctx, errcode.ErrInvalidArgument)
|
|
return
|
|
}
|
|
gasBasicID, err := common.ResolveIdentityID(&models.GasBasic{}, request.GasBasicIdentity, true)
|
|
if err != nil {
|
|
infra.Response.Error(ctx, errcode.ErrInvalidArgument)
|
|
return
|
|
}
|
|
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"})
|
|
}
|
|
|
|
func splitOwnerIdentities(value string) []string {
|
|
parts := strings.Split(strings.TrimSpace(value), ",")
|
|
if len(parts) == 1 && parts[0] == "" {
|
|
return nil
|
|
}
|
|
return parts
|
|
}
|