2026-07-30 18:04:34 +08:00
package user
import (
"git.apinb.com/bsm-sdk/core/errcode"
"git.apinb.com/bsm-sdk/core/infra"
"git.apinb.com/heqiapp/platforms/backend/api/internal/impl"
2026-07-31 09:54:17 +08:00
common "git.apinb.com/heqiapp/platforms/backend/api/internal/logic/common"
2026-08-02 23:24:32 +08:00
"git.apinb.com/heqiapp/platforms/backend/api/internal/logic/payment"
2026-07-30 18:04:34 +08:00
"git.apinb.com/heqiapp/platforms/backend/api/internal/models"
"github.com/gin-gonic/gin"
)
// ServiceRelation 返回当前唯一有效服务归属的公开 identity。
func ServiceRelation ( ctx * gin . Context ) {
2026-07-31 09:54:17 +08:00
account , ok := common . UserAccount ( ctx )
2026-07-30 18:04:34 +08:00
if ! ok {
return
}
var relation models . UserServiceRelation
2026-07-31 09:54:17 +08:00
if impl . DBService . Where ( "user_account_id = ? AND status = ?" , account . ID , common . StatusEnable ) . First ( & relation ) . Error != nil {
2026-07-30 18:04:34 +08:00
infra . Response . Success ( ctx , nil )
return
}
response := gin . H { }
if relation . GasBasicID != 0 {
var gas models . GasBasic
if impl . DBService . First ( & gas , relation . GasBasicID ) . Error == nil {
response [ "gas_identity" ] , response [ "gas_name" ] = gas . Identity , gas . Name
}
}
if relation . DeliveryBasicID != 0 {
var delivery models . DeliveryBasic
if impl . DBService . First ( & delivery , relation . DeliveryBasicID ) . Error == nil {
response [ "delivery_identity" ] , response [ "delivery_name" ] = delivery . Identity , delivery . Name
}
}
infra . Response . Success ( ctx , response )
}
2026-08-02 23:24:32 +08:00
// PayGasOrder 为本人未履约供气订单创建统一第三方支付单。
func PayGasOrder ( ctx * gin . Context ) {
account , ok := common . UserAccount ( ctx )
if ! ok {
return
}
var request struct {
RequestNo string ` json:"request_no" binding:"required" `
Channel string ` json:"channel" binding:"required,oneof=alipay wechat" `
PayType string ` json:"pay_type" binding:"required" `
OpenID string ` json:"openid" `
}
if ctx . ShouldBindJSON ( & request ) != nil {
infra . Response . Error ( ctx , errcode . ErrInvalidArgument )
return
}
var order models . GasorderBasic
if err := impl . DBService . Where ( "identity = ? AND user_account_id = ? AND order_status IN ?" , ctx . Param ( "identity" ) , account . ID , [ ] int { common . StatusCreated , common . StatusAssigned } ) . First ( & order ) . Error ; err != nil {
infra . Response . Error ( ctx , err )
return
}
payOrder , err := payment . Create ( ctx , payment . CreateInput { RequestNo : request . RequestNo , BusinessType : "gasorder" , BusinessIdentity : order . Identity ,
UserIdentity : account . Identity , Channel : request . Channel , PayType : request . PayType , Subject : "和气供气订单 " + order . OrderNo , OpenID : request . OpenID , Amount : order . PayableAmount } )
if err != nil {
infra . Response . Error ( ctx , err )
return
}
infra . Response . Success ( ctx , payment . PublicResponse ( payOrder ) )
}
2026-07-30 18:04:34 +08:00
// ListGasContracts 返回用户自己的供气合同。
func ListGasContracts ( ctx * gin . Context ) {
2026-07-31 09:54:17 +08:00
account , ok := common . UserAccount ( ctx )
2026-07-30 18:04:34 +08:00
if ! ok {
return
}
var list [ ] models . GasorderContract
2026-07-31 09:54:17 +08:00
if err := impl . DBService . Where ( "user_account_id = ? AND status <> ?" , account . ID , common . StatusArchived ) . Order ( "created_at desc" ) . Find ( & list ) . Error ; err != nil {
2026-07-30 18:04:34 +08:00
infra . Response . Error ( ctx , err )
return
}
2026-08-02 23:24:32 +08:00
response := make ( [ ] gin . H , 0 , len ( list ) )
for _ , order := range list {
var items [ ] models . GasorderItem
if err := impl . DBService . Where ( "gasorder_basic_id = ?" , order . ID ) . Find ( & items ) . Error ; err != nil {
infra . Response . Error ( ctx , err )
return
}
value := common . ResourceResponse ( order ) . ( map [ string ] any )
value [ "items" ] = common . ResourceResponse ( items )
response = append ( response , value )
}
infra . Response . Success ( ctx , response )
2026-07-30 18:04:34 +08:00
}
// ListGasOrders 返回用户自己的供气订单。
func ListGasOrders ( ctx * gin . Context ) {
2026-07-31 09:54:17 +08:00
account , ok := common . UserAccount ( ctx )
2026-07-30 18:04:34 +08:00
if ! ok {
return
}
var list [ ] models . GasorderBasic
2026-07-31 09:54:17 +08:00
if err := impl . DBService . Where ( "user_account_id = ? AND status <> ?" , account . ID , common . StatusArchived ) . Order ( "created_at desc" ) . Find ( & list ) . Error ; err != nil {
2026-07-30 18:04:34 +08:00
infra . Response . Error ( ctx , err )
return
}
2026-07-31 09:54:17 +08:00
infra . Response . Success ( ctx , common . ResourceResponse ( list ) )
2026-07-30 18:04:34 +08:00
}
// CancelGasOrder 仅允许取消已创建或已分派的本人订单。
func CancelGasOrder ( ctx * gin . Context ) {
2026-07-31 09:54:17 +08:00
account , ok := common . UserAccount ( ctx )
2026-07-30 18:04:34 +08:00
if ! ok {
return
}
result := impl . DBService . Model ( & models . GasorderBasic { } ) .
2026-07-31 09:54:17 +08:00
Where ( "identity = ? AND user_account_id = ? AND order_status IN ?" , ctx . Param ( "identity" ) , account . ID , [ ] int { common . StatusCreated , common . StatusAssigned } ) .
Updates ( map [ string ] any { "order_status" : common . StatusCancelled , "operator_identity" : account . Identity } )
2026-07-30 18:04:34 +08:00
if result . Error != nil {
infra . Response . Error ( ctx , result . Error )
return
}
if result . RowsAffected != 1 {
infra . Response . Error ( ctx , errcode . ErrInvalidArgument )
return
}
infra . Response . Success ( ctx , gin . H { "cancelled" : true } )
}