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-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 )
}
// 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-07-31 09:54:17 +08:00
infra . Response . Success ( ctx , common . ResourceResponse ( list ) )
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 } )
}