155 lines
6.1 KiB
Go
155 lines
6.1 KiB
Go
package staff
|
||
|
||
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/logic/upload"
|
||
"git.apinb.com/heqiapp/platforms/backend/api/internal/models"
|
||
"github.com/gin-gonic/gin"
|
||
)
|
||
|
||
// ListStaff 查询服务人员分页列表。
|
||
func ListStaff(ctx *gin.Context) {
|
||
roleCode := strings.TrimSpace(ctx.Query("role_code"))
|
||
if roleCode == "" {
|
||
common.ListPage[models.StaffAccount](ctx)
|
||
return
|
||
}
|
||
if !validStaffRole(roleCode) {
|
||
infra.Response.Error(ctx, errcode.ErrInvalidArgument)
|
||
return
|
||
}
|
||
page, size := common.PageSize(ctx)
|
||
var list []models.StaffAccount
|
||
var total int64
|
||
query := common.ApplyKeywordFilter(ctx,
|
||
common.ActiveRecords(impl.DBService.Model(&models.StaffAccount{})).Where("role_code = ?", roleCode),
|
||
&models.StaffAccount{})
|
||
if err := query.Count(&total).Error; err != nil {
|
||
infra.Response.Error(ctx, err)
|
||
return
|
||
}
|
||
if err := query.Order("created_at desc").Offset((page - 1) * size).Limit(size).Find(&list).Error; err != nil {
|
||
infra.Response.Error(ctx, err)
|
||
return
|
||
}
|
||
response, err := common.PublicResourceResponse(list)
|
||
if err != nil {
|
||
infra.Response.Error(ctx, err)
|
||
return
|
||
}
|
||
infra.Response.Success(ctx, gin.H{"total": total, "list": common.ProtectPreciseLocation(ctx, &models.StaffAccount{}, response)})
|
||
}
|
||
|
||
// GetStaff 查询一个服务人员档案。
|
||
func GetStaff(ctx *gin.Context) { common.GetByIdentity[models.StaffAccount](ctx) }
|
||
|
||
// GetStaffAvatar 返回已鉴权工作人员资料页使用的头像二进制内容。
|
||
func GetStaffAvatar(ctx *gin.Context) {
|
||
var account models.StaffAccount
|
||
if err := common.ActiveRecords(impl.DBService).Select("avatar").Where("identity = ?", ctx.Param("identity")).First(&account).Error; err != nil {
|
||
common.RespondRecordError(ctx, err)
|
||
return
|
||
}
|
||
upload.ServeAvatar(ctx, account.Avatar)
|
||
}
|
||
|
||
// CreateStaff 创建服务人员档案。
|
||
func CreateStaff(ctx *gin.Context) {
|
||
var request struct {
|
||
Username string `json:"username" binding:"required,max=64"`
|
||
Password string `json:"password" binding:"required"`
|
||
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 ||
|
||
!common.IsValidAccountPassword(request.Password) ||
|
||
!validStaffRole(request.RoleCode) {
|
||
infra.Response.Error(ctx, errcode.ErrInvalidArgument)
|
||
return
|
||
}
|
||
hash, err := common.PasswordHash(request.Password)
|
||
if err != nil {
|
||
infra.Response.Error(ctx, err)
|
||
return
|
||
}
|
||
gasBasicID, err := common.ResolveIdentityID(&models.GasBasic{}, request.GasBasicIdentity, false)
|
||
if err != nil {
|
||
infra.Response.Error(ctx, errcode.ErrInvalidArgument)
|
||
return
|
||
}
|
||
deliveryBasicID, err := common.ResolveIdentityID(&models.DeliveryBasic{}, request.DeliveryBasicIdentity, false)
|
||
if err != nil {
|
||
infra.Response.Error(ctx, errcode.ErrInvalidArgument)
|
||
return
|
||
}
|
||
if !common.ValidateOrganizationIDs(gasBasicID, deliveryBasicID, 0) {
|
||
infra.Response.Error(ctx, errcode.ErrInvalidArgument)
|
||
return
|
||
}
|
||
staff := models.StaffAccount{Entity: common.NewEntity(common.StatusDraft), 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 !validWorkStatus(staff.WorkStatus) {
|
||
infra.Response.Error(ctx, errcode.ErrInvalidArgument)
|
||
return
|
||
}
|
||
if err := impl.DBService.Create(&staff).Error; err != nil {
|
||
infra.Response.Error(ctx, err)
|
||
return
|
||
}
|
||
common.RespondCreatedResource(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:"omitempty,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 || !validWorkStatus(request.WorkStatus) || !validStaffRole(request.RoleCode) {
|
||
infra.Response.Error(ctx, errcode.ErrInvalidArgument)
|
||
return
|
||
}
|
||
gasBasicID, err := common.ResolveIdentityID(&models.GasBasic{}, request.GasBasicIdentity, false)
|
||
if err != nil {
|
||
infra.Response.Error(ctx, errcode.ErrInvalidArgument)
|
||
return
|
||
}
|
||
deliveryBasicID, err := common.ResolveIdentityID(&models.DeliveryBasic{}, request.DeliveryBasicIdentity, false)
|
||
if err != nil {
|
||
infra.Response.Error(ctx, errcode.ErrInvalidArgument)
|
||
return
|
||
}
|
||
if !common.ValidateOrganizationIDs(gasBasicID, deliveryBasicID, 0) {
|
||
infra.Response.Error(ctx, errcode.ErrInvalidArgument)
|
||
return
|
||
}
|
||
values := gin.H{"name": request.Name, "phone": request.Phone, "role_code": request.RoleCode, "gas_basic_id": gasBasicID, "delivery_basic_id": deliveryBasicID, "work_status": request.WorkStatus}
|
||
// 未选择新头像时不提交 avatar,避免普通资料编辑误清空现有头像。
|
||
if request.Avatar != nil {
|
||
values["avatar"] = *request.Avatar
|
||
}
|
||
common.UpdateAllowedByIdentity(ctx, &models.StaffAccount{}, values, []string{"name", "phone", "avatar", "role_code", "gas_basic_id", "delivery_basic_id", "work_status"})
|
||
}
|
||
|
||
func validWorkStatus(status string) bool { return status == "on_duty" || status == "off_duty" }
|
||
func validStaffRole(role string) bool {
|
||
return role == "installer" || role == "delivery" || role == "operations"
|
||
}
|