128 lines
4.5 KiB
Go
128 lines
4.5 KiB
Go
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"
|
|
common "git.apinb.com/heqiapp/platforms/backend/api/internal/logic/common"
|
|
"git.apinb.com/heqiapp/platforms/backend/api/internal/logic/payment"
|
|
"git.apinb.com/heqiapp/platforms/backend/api/internal/models"
|
|
"github.com/gin-gonic/gin"
|
|
)
|
|
|
|
// ServiceRelation 返回当前唯一有效服务归属的公开 identity。
|
|
func ServiceRelation(ctx *gin.Context) {
|
|
account, ok := common.UserAccount(ctx)
|
|
if !ok {
|
|
return
|
|
}
|
|
var relation models.UserServiceRelation
|
|
if impl.DBService.Where("user_account_id = ? AND status = ?", account.ID, common.StatusEnable).First(&relation).Error != nil {
|
|
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)
|
|
}
|
|
|
|
// 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))
|
|
}
|
|
|
|
// ListGasContracts 返回用户自己的供气合同。
|
|
func ListGasContracts(ctx *gin.Context) {
|
|
account, ok := common.UserAccount(ctx)
|
|
if !ok {
|
|
return
|
|
}
|
|
var list []models.GasorderContract
|
|
if err := impl.DBService.Where("user_account_id = ? AND status <> ?", account.ID, common.StatusArchived).Order("created_at desc").Find(&list).Error; err != nil {
|
|
infra.Response.Error(ctx, err)
|
|
return
|
|
}
|
|
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)
|
|
}
|
|
|
|
// ListGasOrders 返回用户自己的供气订单。
|
|
func ListGasOrders(ctx *gin.Context) {
|
|
account, ok := common.UserAccount(ctx)
|
|
if !ok {
|
|
return
|
|
}
|
|
var list []models.GasorderBasic
|
|
if err := impl.DBService.Where("user_account_id = ? AND status <> ?", account.ID, common.StatusArchived).Order("created_at desc").Find(&list).Error; err != nil {
|
|
infra.Response.Error(ctx, err)
|
|
return
|
|
}
|
|
infra.Response.Success(ctx, common.ResourceResponse(list))
|
|
}
|
|
|
|
// CancelGasOrder 仅允许取消已创建或已分派的本人订单。
|
|
func CancelGasOrder(ctx *gin.Context) {
|
|
account, ok := common.UserAccount(ctx)
|
|
if !ok {
|
|
return
|
|
}
|
|
result := impl.DBService.Model(&models.GasorderBasic{}).
|
|
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})
|
|
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})
|
|
}
|