2026-07-28 13:55:40 +08:00
|
|
|
package delivery
|
2026-07-27 02:03:26 +08:00
|
|
|
|
|
|
|
|
import (
|
2026-07-29 15:54:20 +08:00
|
|
|
"strings"
|
|
|
|
|
|
2026-07-27 02:03:26 +08:00
|
|
|
"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"
|
2026-07-29 15:54:20 +08:00
|
|
|
"gorm.io/gorm"
|
2026-07-27 02:03:26 +08:00
|
|
|
)
|
|
|
|
|
|
2026-08-10 21:06:12 +08:00
|
|
|
// deliveryAdminRoleCode 是配送点管理后台当前唯一允许的登录角色编码。
|
|
|
|
|
const deliveryAdminRoleCode = "admin"
|
|
|
|
|
|
|
|
|
|
// newDeliveryAccount 构造平台创建的配送点登录账号,并固定其权限身份。
|
|
|
|
|
func newDeliveryAccount(deliveryBasicID uint64, request accountRequest, passwordHash string) models.DeliveryAccount {
|
|
|
|
|
return models.DeliveryAccount{
|
|
|
|
|
Entity: common.NewEntity(common.StatusEnable),
|
|
|
|
|
DeliveryBasicID: deliveryBasicID,
|
|
|
|
|
Username: request.Username,
|
|
|
|
|
DisplayName: request.DisplayName,
|
|
|
|
|
PasswordHash: passwordHash,
|
|
|
|
|
RoleCode: deliveryAdminRoleCode,
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2026-07-27 02:03:26 +08:00
|
|
|
type accountRequest struct {
|
2026-07-27 10:11:07 +08:00
|
|
|
Username string `json:"username" binding:"required,max=64"`
|
2026-07-30 10:01:19 +08:00
|
|
|
Password string `json:"password" binding:"required"`
|
2026-07-27 10:11:07 +08:00
|
|
|
DisplayName string `json:"display_name" binding:"max=64"`
|
|
|
|
|
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"`
|
|
|
|
|
DeliveryBasicIdentity string `json:"delivery_basic_identity"`
|
2026-07-27 02:03:26 +08:00
|
|
|
}
|
|
|
|
|
|
2026-07-29 15:54:20 +08:00
|
|
|
func ListDeliveryAccount(ctx *gin.Context) {
|
|
|
|
|
identities := strings.Split(strings.TrimSpace(ctx.Query("delivery_basic_identities")), ",")
|
|
|
|
|
if len(identities) == 1 && identities[0] == "" {
|
|
|
|
|
identities = nil
|
|
|
|
|
}
|
|
|
|
|
if len(identities) > 100 {
|
|
|
|
|
infra.Response.Error(ctx, errcode.ErrInvalidArgument)
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
common.ListPageFiltered[models.DeliveryAccount](ctx, func(query *gorm.DB) *gorm.DB {
|
|
|
|
|
if len(identities) == 0 {
|
|
|
|
|
return query
|
|
|
|
|
}
|
|
|
|
|
return query.Where("delivery_basic_id IN (SELECT id FROM delivery_basic WHERE identity IN ? AND status <> ?)", identities, common.StatusArchived)
|
|
|
|
|
})
|
|
|
|
|
}
|
|
|
|
|
func GetDeliveryAccount(ctx *gin.Context) { common.GetByIdentity[models.DeliveryAccount](ctx) }
|
2026-07-27 02:03:26 +08:00
|
|
|
|
|
|
|
|
func CreateDeliveryAccount(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-30 10:01:19 +08:00
|
|
|
if !common.IsValidAccountPassword(request.Password) {
|
|
|
|
|
infra.Response.Error(ctx, errcode.ErrInvalidArgument)
|
|
|
|
|
return
|
|
|
|
|
}
|
2026-07-28 10:42:26 +08:00
|
|
|
deliveryBasicID, err := common.ResolveIdentityID(&models.DeliveryBasic{}, request.DeliveryBasicIdentity, 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-08-10 21:06:12 +08:00
|
|
|
account := newDeliveryAccount(deliveryBasicID, request, hash)
|
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 UpdateDeliveryAccount(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
|
|
|
deliveryBasicID, err := common.ResolveIdentityID(&models.DeliveryBasic{}, request.DeliveryBasicIdentity, true)
|
2026-07-27 10:11:07 +08:00
|
|
|
if err != nil {
|
|
|
|
|
infra.Response.Error(ctx, errcode.ErrInvalidArgument)
|
|
|
|
|
return
|
|
|
|
|
}
|
2026-08-10 21:06:12 +08:00
|
|
|
common.UpdateAllowedByIdentity(ctx, &models.DeliveryAccount{}, gin.H{"delivery_basic_id": deliveryBasicID, "display_name": request.DisplayName}, []string{"delivery_basic_id", "display_name"})
|
2026-07-27 02:03:26 +08:00
|
|
|
}
|