87 lines
3.6 KiB
Go
87 lines
3.6 KiB
Go
package platform
|
|
|
|
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/models"
|
|
"github.com/gin-gonic/gin"
|
|
)
|
|
|
|
// ListStaff 查询服务人员分页列表。
|
|
func ListStaff(ctx *gin.Context) { listPage[models.StaffAccount](ctx) }
|
|
|
|
// GetStaff 查询一个服务人员档案。
|
|
func GetStaff(ctx *gin.Context) { getByIdentity[models.StaffAccount](ctx) }
|
|
|
|
// CreateStaff 创建服务人员档案。
|
|
func CreateStaff(ctx *gin.Context) {
|
|
var request struct {
|
|
Username string `json:"username" binding:"required,max=64"`
|
|
Password string `json:"password" binding:"required,min=8,max=128"`
|
|
Name string `json:"name" binding:"required,max=64"`
|
|
Phone string `json:"phone" binding:"max=32"`
|
|
Avatar string `json:"avatar" binding:"max=512"`
|
|
RoleCode string `json:"role_code" binding:"max=64"`
|
|
GasBasicIdentity string `json:"gas_basic_identity"`
|
|
DeliveryBasicIdentity string `json:"delivery_basic_identity"`
|
|
WorkStatus string `json:"work_status" binding:"max=32"`
|
|
}
|
|
if err := ctx.ShouldBindJSON(&request); err != nil {
|
|
infra.Response.Error(ctx, errcode.ErrInvalidArgument)
|
|
return
|
|
}
|
|
hash, err := passwordHash(request.Password)
|
|
if err != nil {
|
|
infra.Response.Error(ctx, err)
|
|
return
|
|
}
|
|
gasBasicID, err := resolveIdentityID(&models.GasBasic{}, request.GasBasicIdentity, false)
|
|
if err != nil {
|
|
infra.Response.Error(ctx, errcode.ErrInvalidArgument)
|
|
return
|
|
}
|
|
deliveryBasicID, err := resolveIdentityID(&models.DeliveryBasic{}, request.DeliveryBasicIdentity, false)
|
|
if err != nil {
|
|
infra.Response.Error(ctx, errcode.ErrInvalidArgument)
|
|
return
|
|
}
|
|
staff := models.StaffAccount{Entity: newEntity("draft"), Username: request.Username, PasswordHash: hash, Name: request.Name, Phone: request.Phone, Avatar: request.Avatar, RoleCode: request.RoleCode, GasBasicID: gasBasicID, DeliveryBasicID: deliveryBasicID, WorkStatus: request.WorkStatus}
|
|
if staff.WorkStatus == "" {
|
|
staff.WorkStatus = "off_duty"
|
|
}
|
|
if err := impl.DBService.Create(&staff).Error; err != nil {
|
|
infra.Response.Error(ctx, err)
|
|
return
|
|
}
|
|
infra.Response.Success(ctx, staff)
|
|
}
|
|
|
|
// UpdateStaff 更新服务人员档案。
|
|
func UpdateStaff(ctx *gin.Context) {
|
|
var request struct {
|
|
Name string `json:"name" binding:"required,max=64"`
|
|
Phone string `json:"phone" binding:"max=32"`
|
|
Avatar string `json:"avatar" binding:"max=512"`
|
|
RoleCode string `json:"role_code" binding:"max=64"`
|
|
GasBasicIdentity string `json:"gas_basic_identity"`
|
|
DeliveryBasicIdentity string `json:"delivery_basic_identity"`
|
|
WorkStatus string `json:"work_status" binding:"max=32"`
|
|
}
|
|
if err := ctx.ShouldBindJSON(&request); err != nil {
|
|
infra.Response.Error(ctx, errcode.ErrInvalidArgument)
|
|
return
|
|
}
|
|
gasBasicID, err := resolveIdentityID(&models.GasBasic{}, request.GasBasicIdentity, false)
|
|
if err != nil {
|
|
infra.Response.Error(ctx, errcode.ErrInvalidArgument)
|
|
return
|
|
}
|
|
deliveryBasicID, err := resolveIdentityID(&models.DeliveryBasic{}, request.DeliveryBasicIdentity, false)
|
|
if err != nil {
|
|
infra.Response.Error(ctx, errcode.ErrInvalidArgument)
|
|
return
|
|
}
|
|
updateAllowedByIdentity(ctx, &models.StaffAccount{}, gin.H{"name": request.Name, "phone": request.Phone, "avatar": request.Avatar, "role_code": request.RoleCode, "gas_basic_id": gasBasicID, "delivery_basic_id": deliveryBasicID, "work_status": request.WorkStatus}, []string{"name", "phone", "avatar", "role_code", "gas_basic_id", "delivery_basic_id", "work_status"})
|
|
}
|