package routers import ( "fmt" "git.apinb.com/bsm-sdk/core/middleware" "git.apinb.com/heqiapp/platforms/backend/api/internal/logic/common" platformbase "git.apinb.com/heqiapp/platforms/backend/api/internal/logic/platform" "git.apinb.com/heqiapp/platforms/backend/api/internal/logic/platform/dashboard" "git.apinb.com/heqiapp/platforms/backend/api/internal/logic/platform/delivery" "git.apinb.com/heqiapp/platforms/backend/api/internal/logic/platform/ec" "git.apinb.com/heqiapp/platforms/backend/api/internal/logic/platform/fin" "git.apinb.com/heqiapp/platforms/backend/api/internal/logic/platform/gas" "git.apinb.com/heqiapp/platforms/backend/api/internal/logic/platform/gasorder" paymentlogic "git.apinb.com/heqiapp/platforms/backend/api/internal/logic/platform/payment" platformlogic "git.apinb.com/heqiapp/platforms/backend/api/internal/logic/platform/platform" "git.apinb.com/heqiapp/platforms/backend/api/internal/logic/platform/product" "git.apinb.com/heqiapp/platforms/backend/api/internal/logic/platform/staff" userlogic "git.apinb.com/heqiapp/platforms/backend/api/internal/logic/platform/user" "git.apinb.com/heqiapp/platforms/backend/api/internal/logic/platform/wallet" "git.apinb.com/heqiapp/platforms/backend/api/internal/models" "github.com/gin-gonic/gin" ) // RegisterPlatform 注册 /heqi/platform/v1 前缀下的平台总后台路由。 func RegisterPlatform(serviceKey string, engine *gin.Engine) { basePath := fmt.Sprintf("/%s/platform/v1", serviceKey) anonymous := engine.Group(basePath) anonymous.GET("/ping/hello", dashboard.PingHello) anonymous.POST("/auth/login", platformbase.Login) protected := engine.Group(basePath) protected.Use(middleware.JwtAuth(true)) protected.Use(platformlogic.RequirePlatformMenuAccess()) protected.GET("/auth/profile", platformbase.CurrentProfile) protected.PUT("/auth/password", platformbase.ChangePassword) protected.GET("/dashboard/overview", dashboard.DashboardOverview) registerGasRoute(protected) registerDeliveryRoute(protected) registerGasorderRoute(protected) registerStaffRoute(protected) registerUserRoute(protected) registerProductRoute(protected) registerWalletRoute(protected) registerCommerceRoute(protected) registerFinanceRoute(protected) registerContentRoute(protected) registerPlatformRoute(protected) } func registerGasRoute(group *gin.RouterGroup) { basic := group.Group("/gas_basic") basic.GET("", gas.ListGasBasic) basic.POST("", gas.CreateGasBasic) basic.GET("/:identity", gas.GetGasBasic) basic.PUT("/:identity", gas.UpdateGasBasic) basic.PATCH("/:identity/status", gas.UpdateGasBasicStatus) basic.DELETE("/:identity", func(ctx *gin.Context) { common.ArchiveRecord(ctx, &models.GasBasic{}) }) registerWritableResource(group, "/gas_account", gas.ListGasAccount, gas.CreateGasAccount, gas.GetGasAccount, gas.UpdateGasAccount, &models.GasAccount{}) } func registerDeliveryRoute(group *gin.RouterGroup) { registerWritableResource(group, "/delivery_basic", delivery.ListDeliveryBasic, delivery.CreateDeliveryBasic, delivery.GetDeliveryBasic, delivery.UpdateDeliveryBasic, &models.DeliveryBasic{}) registerWritableResource(group, "/delivery_account", delivery.ListDeliveryAccount, delivery.CreateDeliveryAccount, delivery.GetDeliveryAccount, delivery.UpdateDeliveryAccount, &models.DeliveryAccount{}) } func registerGasorderRoute(group *gin.RouterGroup) { contract := group.Group("/gasorder_contract") contract.GET("", gasorder.ListGasorderContract) contract.POST("", gasorder.CreateGasorderContract) contract.GET("/:identity", gasorder.GetGasorderContract) contract.PUT("/:identity", gasorder.UpdateGasorderContract) contract.POST("/:identity/activate", gasorder.ActivateGasorderContract) contract.POST("/:identity/renew", gasorder.RenewGasorderContract) contract.POST("/:identity/terminate", gasorder.TerminateGasorderContract) contractProduct := group.Group("/gasorder_contract_product") contractProduct.GET("", gasorder.ListGasorderContractProduct) contractProduct.POST("", gasorder.BindGasorderContractProduct) contractProduct.GET("/:identity", gasorder.GetGasorderContractProduct) contractProduct.POST("/:identity/unbind", gasorder.UnbindGasorderContractProduct) registerReadOnlyHandlers(group, "/gasorder_contract_revision", gasorder.ListGasorderContractRevision, gasorder.GetGasorderContractRevision) order := group.Group("/gasorder_basic") order.GET("", gasorder.ListGasorderBasic) order.POST("", gasorder.CreateGasorderBasic) order.GET("/:identity", gasorder.GetGasorderBasic) order.POST("/:identity/assign", gasorder.AssignGasorderBasic) order.POST("/:identity/filling", gasorder.GasorderStartFilling) order.POST("/:identity/ready", gasorder.GasorderReady) order.POST("/:identity/delivering", gasorder.GasorderStartDelivering) order.POST("/:identity/awaiting-confirmation", gasorder.GasorderAwaitingConfirmation) order.POST("/:identity/complete", gasorder.GasorderComplete) order.POST("/:identity/exception", gasorder.GasorderException) order.POST("/:identity/recover", gasorder.GasorderRecover) order.POST("/:identity/cancel", gasorder.GasorderCancel) registerReadOnlyHandlers(group, "/gasorder_item", gasorder.ListGasorderItem, gasorder.GetGasorderItem) registerReadOnlyHandlers(group, "/gasorder_assign", gasorder.ListGasorderAssign, gasorder.GetGasorderAssign) registerReadOnlyHandlers(group, "/gasorder_status", gasorder.ListGasorderStatus, gasorder.GetGasorderStatus) registerReadOnlyHandlers(group, "/gasorder_track", gasorder.ListGasorderTrack, gasorder.GetGasorderTrack) registerReadOnlyHandlers(group, "/gasorder_track_point", gasorder.ListGasorderTrackPoint, gasorder.GetGasorderTrackPoint) registerReadOnlyHandlers(group, "/gasorder_confirm", gasorder.ListGasorderConfirm, gasorder.GetGasorderConfirm) registerReadOnlyHandlers(group, "/gasorder_payment", gasorder.ListGasorderPayment, gasorder.GetGasorderPayment) } func registerProductRoute(group *gin.RouterGroup) { producer := group.Group("/producer_account") producer.GET("", product.ListProducerAccount) producer.POST("", product.CreateProducerAccount) producer.GET("/:identity", product.GetProducerAccount) producer.PUT("/:identity", product.UpdateProducerAccount) producer.PATCH("/:identity/status", func(ctx *gin.Context) { common.UpdateRecordStatus(ctx, &models.ProducerAccount{}) }) producer.DELETE("/:identity", product.DeleteProducerAccount) registerRestrictedNoDeleteResource(group, "/product_type", &models.ProductType{}, []string{"code", "name"}) registerRestrictedNoDeleteResource(group, "/product_warehouse", &models.ProductWarehouse{}, []string{"code", "name", "address", "manager", "phone"}) infoRelations := []common.ResourceRelation{ requiredRelation("producer_account_identity", "producer_account_id", &models.ProducerAccount{}), requiredRelation("product_type_identity", "product_type_id", &models.ProductType{}), optionalRelation("warehouse_identity", "warehouse_id", &models.ProductWarehouse{}), optionalRelation("gas_basic_identity", "gas_basic_id", &models.GasBasic{}), optionalRelation("delivery_basic_identity", "delivery_basic_id", &models.DeliveryBasic{}), optionalRelation("user_account_identity", "user_account_id", &models.UserAccount{}), } infoList, infoCreate, infoGet, infoUpdate := product.ProductInfoHandlers(infoRelations...) info := group.Group("/product_info") info.GET("", infoList) info.POST("", infoCreate) info.GET("/:identity", infoGet) info.PUT("/:identity", infoUpdate) info.PATCH("/:identity/status", product.UpdateProductInfoRecordStatus) info.PATCH("/:identity/lifecycle", product.UpdateProductInfoLifecycle) repairList, repairCreate, repairGet, repairUpdate := product.ProductRepairHandlers( requiredRelation("product_info_identity", "product_info_id", &models.ProductInfo{}), ) registerNoDeleteResource(group, "/product_repair", repairList, repairCreate, repairGet, repairUpdate, &models.ProductRepair{}) ownerList, ownerGet := product.ProductOwnerHandlers() owner := group.Group("/product_owner") owner.GET("", ownerList) owner.GET("/:identity", ownerGet) } func registerWalletRoute(group *gin.RouterGroup) { basic := group.Group("/wallet_basic") basic.GET("", wallet.ListWalletBasic) basic.GET("/:identity", wallet.GetWalletBasic) basic.PATCH("/:identity/status", wallet.UpdateWalletBasicStatus) basic.POST("/:identity/recharge", wallet.RechargeWalletBasic) basic.POST("/owner/:owner_type/:owner_identity", wallet.GetOrCreateOwnerWallet) bank := group.Group("/wallet_bank") bank.GET("", wallet.ListWalletBank) bank.GET("/:identity", wallet.GetWalletBank) payment := group.Group("/payment_order") payment.GET("", wallet.ListPaymentOrder) payment.GET("/:identity", wallet.GetPaymentOrder) record := group.Group("/wallet_record") record.GET("", wallet.ListWalletRecord) record.GET("/:identity", wallet.GetWalletRecord) applyCash := group.Group("/wallet_apply_cash") applyCash.GET("", wallet.ListWalletApplyCash) applyCash.GET("/:identity", wallet.GetWalletApplyCash) applyCash.POST("/:identity/approve", wallet.ApproveWalletApplyCash) applyCash.POST("/:identity/reject", wallet.RejectWalletApplyCash) applyCash.POST("/:identity/complete", wallet.CompleteWalletApplyCash) } func registerCommerceRoute(group *gin.RouterGroup) { registerWritableResource(group, "/ec_category", ec.ListEcCategory, ec.CreateEcCategory, ec.GetEcCategory, ec.UpdateEcCategory, &models.EcCategory{}) registerRestrictedWritableResource(group, "/ec_product", &models.EcProduct{}, []string{"product_code", "name", "price_amount", "stock_quantity"}, requiredRelation("ec_category_identity", "ec_category_id", &models.EcCategory{})) registerRestrictedWritableResource(group, "/ec_product_attribute", &models.EcProductAttribute{}, []string{"name", "value", "sort_no"}, requiredRelation("ec_product_identity", "ec_product_id", &models.EcProduct{})) registerRestrictedWritableResource(group, "/ec_product_image", &models.EcProductImage{}, []string{"image_uri", "sort_no", "is_cover"}, requiredRelation("ec_product_identity", "ec_product_id", &models.EcProduct{})) registerReadOnlyResource(group, "/ec_cart", &models.EcCart{}) registerReadOnlyHandlers(group, "/ec_order", func(ctx *gin.Context) { common.ListResource(ctx, &models.EcOrder{}) }, ec.GetEcOrder) registerReadOnlyResource(group, "/ec_order_item", &models.EcOrderItem{}) registerReadOnlyResource(group, "/ec_review", &models.EcReview{}) } func registerStaffRoute(group *gin.RouterGroup) { registerWritableResource(group, "/staff_account", staff.ListStaff, staff.CreateStaff, staff.GetStaff, staff.UpdateStaff, &models.StaffAccount{}) registerWritableResource(group, "/staff_credential", staff.ListStaffCredential, staff.CreateStaffCredential, staff.GetStaffCredential, staff.UpdateStaffCredential, &models.StaffCredential{}) } func registerUserRoute(group *gin.RouterGroup) { registerWritableResource(group, "/user_account", userlogic.ListUser, userlogic.CreateUser, userlogic.GetUser, userlogic.UpdateUser, &models.UserAccount{}) registerWritableResource(group, "/user_address", userlogic.ListUserAddress, userlogic.CreateUserAddress, userlogic.GetUserAddress, userlogic.UpdateUserAddress, &models.UserAddress{}) registerWritableResource(group, "/user_service_relation", userlogic.ListUserServiceRelation, userlogic.CreateUserServiceRelation, userlogic.GetUserServiceRelation, userlogic.UpdateUserServiceRelation, &models.UserServiceRelation{}) } func registerPlatformRoute(group *gin.RouterGroup) { account := group.Group("/platform_account") account.GET("", platformlogic.ListPlatformAccount) account.POST("", platformlogic.CreatePlatformAccount) account.GET("/:identity", platformlogic.GetPlatformAccount) account.PUT("/:identity", platformlogic.UpdatePlatformAccount) account.PATCH("/:identity/status", platformlogic.UpdatePlatformAccountStatus) account.DELETE("/:identity", platformlogic.ArchivePlatformAccount) role := group.Group("/platform_role") role.GET("", platformlogic.ListPlatformRole) role.POST("", platformlogic.CreatePlatformRole) role.GET("/:identity", platformlogic.GetPlatformRole) role.PUT("/:identity", platformlogic.UpdatePlatformRole) role.PATCH("/:identity/status", platformlogic.UpdatePlatformRoleStatus) role.DELETE("/:identity", platformlogic.ArchivePlatformRole) role.GET("/:identity/menu", platformlogic.ListPlatformRoleMenuIdentities) role.PUT("/:identity/menu", platformlogic.ReplacePlatformRoleMenus) menu := group.Group("/platform_menu") menu.GET("", platformlogic.ListPlatformMenu) menu.GET("/:identity", platformlogic.GetPlatformMenu) } func registerFinanceRoute(group *gin.RouterGroup) { refund := group.Group("/payment_refund") refund.GET("", paymentlogic.ListRefund) refund.GET("/:identity", paymentlogic.GetRefund) refund.POST("/:identity/approve", paymentlogic.ApproveRefund) refund.POST("/:identity/reject", paymentlogic.RejectRefund) registerReadOnlyResource(group, "/fin_payment", &models.FinPayment{}) settlementList, settlementCreate, settlementGet, settlementUpdate := fin.FinSettlementHandlers() registerWritableResource(group, "/fin_settlement", settlementList, settlementCreate, settlementGet, settlementUpdate, &models.FinSettlement{}) registerReadOnlyResource(group, "/fin_reconciliation", &models.FinReconciliation{}) } func registerContentRoute(group *gin.RouterGroup) { registerRestrictedWritableResource(group, "/cms_content", &models.CmsContent{}, []string{"content_type", "title", "body", "version_no", "publish_status"}) registerRestrictedWritableResource(group, "/cs_ticket", &models.CsTicket{}, []string{"ticket_no", "category", "priority"}, requiredRelation("user_account_identity", "user_account_id", &models.UserAccount{})) } func registerWritableResource(group *gin.RouterGroup, path string, list, create, get, update gin.HandlerFunc, model any) { resource := group.Group(path) resource.GET("", list) resource.POST("", create) resource.GET("/:identity", get) resource.PUT("/:identity", update) resource.PATCH("/:identity/status", func(ctx *gin.Context) { common.UpdateRecordStatus(ctx, model) }) resource.DELETE("/:identity", func(ctx *gin.Context) { common.ArchiveRecord(ctx, model) }) } func registerRestrictedWritableResource(group *gin.RouterGroup, path string, model any, fields []string, relations ...common.ResourceRelation) { list, create, get, update := common.ResourceHandlers(model, fields, fields, relations...) registerWritableResource(group, path, list, create, get, update, model) } func registerNoDeleteResource(group *gin.RouterGroup, path string, list, create, get, update gin.HandlerFunc, model any) { resource := group.Group(path) resource.GET("", list) resource.POST("", create) resource.GET("/:identity", get) resource.PUT("/:identity", update) resource.PATCH("/:identity/status", func(ctx *gin.Context) { common.UpdateRecordStatus(ctx, model) }) } func registerRestrictedNoDeleteResource(group *gin.RouterGroup, path string, model any, fields []string, relations ...common.ResourceRelation) { list, create, get, update := common.ResourceHandlers(model, fields, fields, relations...) registerNoDeleteResource(group, path, list, create, get, update, model) } func registerReadOnlyResource(group *gin.RouterGroup, path string, model any) { list, _, get, _ := common.ResourceHandlers(model, nil, nil) resource := group.Group(path) resource.GET("", list) resource.GET("/:identity", get) } func registerReadOnlyHandlers(group *gin.RouterGroup, path string, list, get gin.HandlerFunc) { resource := group.Group(path) resource.GET("", list) resource.GET("/:identity", get) } func requiredRelation(input, column string, model any) common.ResourceRelation { return common.ResourceRelation{Input: input, Column: column, Model: model, Required: true} } func optionalRelation(input, column string, model any) common.ResourceRelation { return common.ResourceRelation{Input: input, Column: column, Model: model} }