refactor: 归并平台业务逻辑目录
This commit is contained in:
170
backend/api/internal/logic/platform/platform.go
Normal file
170
backend/api/internal/logic/platform/platform.go
Normal file
@@ -0,0 +1,170 @@
|
||||
// Package platform 汇聚平台总后台的同步 HTTP 业务逻辑。
|
||||
package platform
|
||||
|
||||
import (
|
||||
"git.apinb.com/bsm-sdk/core/errcode"
|
||||
"git.apinb.com/bsm-sdk/core/infra"
|
||||
"git.apinb.com/bsm-sdk/core/utils"
|
||||
"git.apinb.com/heqiapp/platforms/backend/api/internal/models"
|
||||
"github.com/gin-gonic/gin"
|
||||
)
|
||||
|
||||
// PingHello 返回匿名健康状态。
|
||||
func PingHello(ctx *gin.Context) {
|
||||
infra.Response.Success(ctx, gin.H{"service": "platform-api", "status": "ok"})
|
||||
}
|
||||
|
||||
// DashboardOverview 返回组织与安全运营的首期概览数据。
|
||||
func DashboardOverview(ctx *gin.Context) {
|
||||
overview, err := models.GetDashboardOverview()
|
||||
if err != nil {
|
||||
infra.Response.Error(ctx, err)
|
||||
return
|
||||
}
|
||||
infra.Response.Success(ctx, overview)
|
||||
}
|
||||
|
||||
// CreateOrgGasStationRequest 是创建 org_gas_station 的请求体。
|
||||
type CreateOrgGasStationRequest struct {
|
||||
StationCode string `json:"station_code" binding:"required,max=32"`
|
||||
Name string `json:"name" binding:"required,max=128"`
|
||||
Principal string `json:"principal" binding:"required,max=64"`
|
||||
ServiceArea string `json:"service_area" binding:"required,max=128"`
|
||||
}
|
||||
|
||||
// OrgGasStationListReply 是 org_gas_station 的标准分页响应。
|
||||
type OrgGasStationListReply struct {
|
||||
Total int64 `json:"total"`
|
||||
List []models.OrgGasStation `json:"list"`
|
||||
}
|
||||
|
||||
// CreateOrgGasStation 创建待审核气站并由数据库层保证唯一编码。
|
||||
func CreateOrgGasStation(ctx *gin.Context) {
|
||||
var request CreateOrgGasStationRequest
|
||||
if err := ctx.ShouldBindJSON(&request); err != nil {
|
||||
infra.Response.Error(ctx, errcode.ErrInvalidArgument)
|
||||
return
|
||||
}
|
||||
data := models.OrgGasStation{StationCode: request.StationCode, Name: request.Name, Principal: request.Principal, ServiceArea: request.ServiceArea}
|
||||
data.Identity = models.NewIdentity()
|
||||
data.Status = "draft"
|
||||
data.Version = 1
|
||||
if err := models.CreateOrgGasStation(&data); err != nil {
|
||||
infra.Response.Error(ctx, err)
|
||||
return
|
||||
}
|
||||
infra.Response.Success(ctx, data)
|
||||
}
|
||||
|
||||
// ListOrgGasStation 查询气站分页列表。
|
||||
func ListOrgGasStation(ctx *gin.Context) {
|
||||
page, size := pageSize(ctx)
|
||||
list, total, err := models.ListOrgGasStation(page, size)
|
||||
if err != nil {
|
||||
infra.Response.Error(ctx, err)
|
||||
return
|
||||
}
|
||||
infra.Response.Success(ctx, OrgGasStationListReply{Total: total, List: list})
|
||||
}
|
||||
|
||||
// OrgDeliveryPointListReply 是 org_delivery_point 的标准分页响应。
|
||||
type OrgDeliveryPointListReply struct {
|
||||
Total int64 `json:"total"`
|
||||
List []models.OrgDeliveryPoint `json:"list"`
|
||||
}
|
||||
|
||||
// ListOrgDeliveryPoint 查询配送点分页列表。
|
||||
func ListOrgDeliveryPoint(ctx *gin.Context) {
|
||||
page, size := pageSize(ctx)
|
||||
list, total, err := models.ListOrgDeliveryPoint(page, size)
|
||||
if err != nil {
|
||||
infra.Response.Error(ctx, err)
|
||||
return
|
||||
}
|
||||
infra.Response.Success(ctx, OrgDeliveryPointListReply{Total: total, List: list})
|
||||
}
|
||||
|
||||
// OrgServicePersonListReply 是 org_service_person 的标准分页响应。
|
||||
type OrgServicePersonListReply struct {
|
||||
Total int64 `json:"total"`
|
||||
List []models.OrgServicePerson `json:"list"`
|
||||
}
|
||||
|
||||
// ListOrgServicePerson 查询服务人员分页列表。
|
||||
func ListOrgServicePerson(ctx *gin.Context) {
|
||||
page, size := pageSize(ctx)
|
||||
list, total, err := models.ListOrgServicePerson(page, size)
|
||||
if err != nil {
|
||||
infra.Response.Error(ctx, err)
|
||||
return
|
||||
}
|
||||
infra.Response.Success(ctx, OrgServicePersonListReply{Total: total, List: list})
|
||||
}
|
||||
|
||||
// IdnAccountView 是 idn_account 的最小必要输出,手机号始终脱敏。
|
||||
type IdnAccountView struct {
|
||||
Identity string `json:"identity"`
|
||||
PhoneMasked string `json:"phone_masked"`
|
||||
AccountType string `json:"account_type"`
|
||||
Status string `json:"status"`
|
||||
ServiceArea string `json:"service_area"`
|
||||
}
|
||||
|
||||
// IdnAccountListReply 是 idn_account 的标准分页响应。
|
||||
type IdnAccountListReply struct {
|
||||
Total int64 `json:"total"`
|
||||
List []IdnAccountView `json:"list"`
|
||||
}
|
||||
|
||||
// ListIdnAccount 查询普通用户账户列表。
|
||||
func ListIdnAccount(ctx *gin.Context) {
|
||||
page, size := pageSize(ctx)
|
||||
list, total, err := models.ListIdnAccount(page, size)
|
||||
if err != nil {
|
||||
infra.Response.Error(ctx, err)
|
||||
return
|
||||
}
|
||||
views := make([]IdnAccountView, 0, len(list))
|
||||
for _, item := range list {
|
||||
views = append(views, IdnAccountView{Identity: item.Identity.String(), PhoneMasked: maskPhone(item.Phone), AccountType: item.AccountType, Status: item.Status, ServiceArea: item.ServiceArea})
|
||||
}
|
||||
infra.Response.Success(ctx, IdnAccountListReply{Total: total, List: views})
|
||||
}
|
||||
|
||||
// SafEventListReply 是 saf_event 的标准分页响应。
|
||||
type SafEventListReply struct {
|
||||
Total int64 `json:"total"`
|
||||
List []models.SafEvent `json:"list"`
|
||||
}
|
||||
|
||||
// ListSafEvent 查询安全事件列表。
|
||||
func ListSafEvent(ctx *gin.Context) {
|
||||
page, size := pageSize(ctx)
|
||||
list, total, err := models.ListSafEvent(page, size)
|
||||
if err != nil {
|
||||
infra.Response.Error(ctx, err)
|
||||
return
|
||||
}
|
||||
infra.Response.Success(ctx, SafEventListReply{Total: total, List: list})
|
||||
}
|
||||
|
||||
// pageSize 统一约束分页参数,避免各接口出现不同边界。
|
||||
func pageSize(ctx *gin.Context) (int, int) {
|
||||
page := utils.String2Int(ctx.DefaultQuery("page", "1"))
|
||||
size := utils.String2Int(ctx.DefaultQuery("size", "20"))
|
||||
if page < 1 {
|
||||
page = 1
|
||||
}
|
||||
if size < 1 || size > 100 {
|
||||
size = 20
|
||||
}
|
||||
return page, size
|
||||
}
|
||||
|
||||
// maskPhone 遵循敏感数据最小展示原则。
|
||||
func maskPhone(phone string) string {
|
||||
if len(phone) < 7 {
|
||||
return "***"
|
||||
}
|
||||
return phone[:3] + "****" + phone[len(phone)-4:]
|
||||
}
|
||||
Reference in New Issue
Block a user