127 lines
3.9 KiB
Go
127 lines
3.9 KiB
Go
package delivery
|
|
|
|
import (
|
|
"net/url"
|
|
"reflect"
|
|
"time"
|
|
|
|
"git.apinb.com/bsm-sdk/core/infra"
|
|
"git.apinb.com/heqiapp/platforms/backend/api/internal/config"
|
|
"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"
|
|
)
|
|
|
|
// deliveryProfileView 表示配送点管理员可查看的本点只读资料。
|
|
type deliveryProfileView struct {
|
|
ID uint64 `json:"id"`
|
|
Identity string `json:"identity"`
|
|
DeliveryCode string `json:"delivery_code"`
|
|
Name string `json:"name"`
|
|
Principal string `json:"principal"`
|
|
Address string `json:"address"`
|
|
GasBasicIdentity string `json:"gas_basic_identity"`
|
|
GasBasicName string `json:"gas_basic_name"`
|
|
Status int `json:"status"`
|
|
CreatedAt time.Time `json:"created_at"`
|
|
UpdatedAt time.Time `json:"updated_at"`
|
|
}
|
|
|
|
func currentScope(ctx *gin.Context) (models.DeliveryBasic, models.GasBasic, bool) {
|
|
_, point, station, ok := CurrentDeliveryAccount(ctx)
|
|
return point, station, ok
|
|
}
|
|
|
|
func ListMenu(ctx *gin.Context) {
|
|
account, _, _, ok := CurrentDeliveryAccount(ctx)
|
|
if !ok {
|
|
return
|
|
}
|
|
menus := MenusForRole(account.RoleCode)
|
|
infra.Response.Success(ctx, gin.H{"total": len(menus), "list": menus})
|
|
}
|
|
|
|
func InvitationQRCode(ctx *gin.Context) {
|
|
_, point, station, ok := CurrentDeliveryAccount(ctx)
|
|
if !ok {
|
|
return
|
|
}
|
|
registerURL, _ := url.Parse(config.Spec.Global.UserRegisterURL)
|
|
query := registerURL.Query()
|
|
query.Set("gas_identity", station.Identity)
|
|
query.Set("delivery_identity", point.Identity)
|
|
registerURL.RawQuery = query.Encode()
|
|
infra.Response.Success(ctx, gin.H{"register_url": registerURL.String()})
|
|
}
|
|
|
|
// ListProfile 返回 JWT 数据范围内唯一配送点的只读资料。
|
|
func ListProfile(ctx *gin.Context) {
|
|
_, point, station, ok := CurrentDeliveryAccount(ctx)
|
|
if !ok {
|
|
return
|
|
}
|
|
infra.Response.Success(ctx, gin.H{
|
|
"total": 1,
|
|
"list": []deliveryProfileView{buildDeliveryProfileView(point, station)},
|
|
})
|
|
}
|
|
|
|
// buildDeliveryProfileView 组合配送点与所属气站的公开展示字段。
|
|
func buildDeliveryProfileView(point models.DeliveryBasic, station models.GasBasic) deliveryProfileView {
|
|
return deliveryProfileView{
|
|
ID: point.ID,
|
|
Identity: point.Identity,
|
|
DeliveryCode: point.DeliveryCode,
|
|
Name: point.Name,
|
|
Principal: point.Principal,
|
|
Address: point.Address,
|
|
GasBasicIdentity: station.Identity,
|
|
GasBasicName: station.Name,
|
|
Status: point.Status,
|
|
CreatedAt: point.CreatedAt,
|
|
UpdatedAt: point.UpdatedAt,
|
|
}
|
|
}
|
|
|
|
func respondList(ctx *gin.Context, list any, total int64) {
|
|
response, err := common.PublicResourceResponse(list)
|
|
if err != nil {
|
|
infra.Response.Error(ctx, err)
|
|
return
|
|
}
|
|
infra.Response.Success(ctx, gin.H{"total": total, "list": response})
|
|
}
|
|
|
|
func listScoped(ctx *gin.Context, model any, query *gorm.DB, order string) {
|
|
page, size := common.PageSize(ctx)
|
|
query = common.ApplyKeywordFilter(ctx, query, model)
|
|
var total int64
|
|
if err := query.Count(&total).Error; err != nil {
|
|
infra.Response.Error(ctx, err)
|
|
return
|
|
}
|
|
list := reflect.New(reflect.SliceOf(reflect.TypeOf(model).Elem()))
|
|
if err := query.Order(order).Offset((page - 1) * size).Limit(size).Find(list.Interface()).Error; err != nil {
|
|
infra.Response.Error(ctx, err)
|
|
return
|
|
}
|
|
respondList(ctx, list.Elem().Interface(), total)
|
|
}
|
|
|
|
func respondRecord(ctx *gin.Context, query *gorm.DB, model any) {
|
|
if err := query.First(model).Error; err != nil {
|
|
common.RespondRecordError(ctx, err)
|
|
return
|
|
}
|
|
response, err := common.PublicResourceResponse(model)
|
|
if err != nil {
|
|
infra.Response.Error(ctx, err)
|
|
return
|
|
}
|
|
infra.Response.Success(ctx, response)
|
|
}
|
|
|
|
func db() *gorm.DB { return impl.DBService }
|