package renewchange import ( "context" "strings" "github.com/gogf/gf/v2/database/gdb" "github.com/gogf/gf/v2/errors/gerror" "github.com/gogf/gf/v2/frame/g" "github.com/gogf/gf/v2/os/gtime" "github.com/gogf/gf/v2/util/gconv" ) // OrderDO mirrors the production t_order table. It is repository-local // because Worker does not generate DAO/DO/entity code for the order domain. type OrderDO struct { g.Meta `orm:"table:t_order, do:true"` Id any `orm:"id"` OrderSn any `orm:"order_sn"` ParentOrderId any `orm:"parent_order_id"` ParentOrderSn any `orm:"parent_order_sn"` CustomerType any `orm:"customer_type"` CustomerAgentId any `orm:"customer_agent_id"` EndUserId any `orm:"end_user_id"` AgentId any `orm:"agent_id"` OrderType any `orm:"order_type"` OrderStatus any `orm:"order_status"` ShippingStatus any `orm:"shipping_status"` ReturnStatus any `orm:"return_status"` Consignee any `orm:"consignee"` Mobile any `orm:"mobile"` Email any `orm:"email"` Address any `orm:"address"` AreaCode any `orm:"area_code"` BuyerNote any `orm:"buyer_note"` AdminNote any `orm:"admin_note"` Remark any `orm:"remark"` ShippingMethod any `orm:"shipping_method"` LogisticsId any `orm:"logistics_id"` LogisticsName any `orm:"logistics_name"` ShippingTypeId any `orm:"shipping_type_id"` ShippingTypeName any `orm:"shipping_type_name"` TrackingNo any `orm:"tracking_no"` ShippingTime *gtime.Time `orm:"shipping_time"` ReceivedTime *gtime.Time `orm:"received_time"` PayType any `orm:"pay_type"` PayStatus any `orm:"pay_status"` PayOrderSn any `orm:"pay_order_sn"` PayTime *gtime.Time `orm:"pay_time"` TotalAmount any `orm:"total_amount"` ProductAmount any `orm:"product_amount"` ShippingFee any `orm:"shipping_fee"` ServiceFee any `orm:"service_fee"` InvoiceFee any `orm:"invoice_fee"` CouponAmount any `orm:"coupon_amount"` DiscountAmount any `orm:"discount_amount"` Balance any `orm:"balance"` PaidAmount any `orm:"paid_amount"` OnlinePaidAmount any `orm:"online_paid_amount"` OfflinePaidAmount any `orm:"offline_paid_amount"` UnpaidAmount any `orm:"unpaid_amount"` UnrefundAmount any `orm:"unrefund_amount"` IsNeedCommission any `orm:"is_need_commission"` DistributionStatus any `orm:"distribution_status"` TenantId any `orm:"tenant_id"` CreatedDept any `orm:"created_dept"` CreatedBy any `orm:"created_by"` CreatedAt *gtime.Time `orm:"created_at"` UpdatedBy any `orm:"updated_by"` UpdatedAt *gtime.Time `orm:"updated_at"` } // OrderItemDO mirrors the production t_order_item table. type OrderItemDO struct { g.Meta `orm:"table:t_order_item, do:true"` ItemId any `orm:"item_id"` OrderId any `orm:"order_id"` OrderSn any `orm:"order_sn"` CustomerType any `orm:"customer_type"` CustomerAgentId any `orm:"customer_agent_id"` EndUserId any `orm:"end_user_id"` Price any `orm:"price"` Quantity any `orm:"quantity"` ProductId any `orm:"product_id"` ProductName any `orm:"product_name"` ProductSn any `orm:"product_sn"` PicThumb any `orm:"pic_thumb"` SkuId any `orm:"sku_id"` SkuData any `orm:"sku_data"` DeliveryQuantity any `orm:"delivery_quantity"` ProductType any `orm:"product_type"` IsGift any `orm:"is_gift"` AgentId any `orm:"agent_id"` PrepayPrice any `orm:"prepay_price"` Commission any `orm:"commission"` OriginPrice any `orm:"origin_price"` PromotionData any `orm:"promotion_data"` ExtraSkuData any `orm:"extra_sku_data"` PayMode any `orm:"pay_mode"` ServiceBeginTime *gtime.Time `orm:"service_begin_time"` ServiceEndTime *gtime.Time `orm:"service_end_time"` TenantId any `orm:"tenant_id"` CreatedDept any `orm:"created_dept"` CreatedBy any `orm:"created_by"` CreatedAt *gtime.Time `orm:"created_at"` UpdatedBy any `orm:"updated_by"` UpdatedAt *gtime.Time `orm:"updated_at"` } // InsertContractOrder inserts a fully built order and returns its primary key. // The order number must exist before the INSERT is issued. func (r *Repository) InsertContractOrder(ctx context.Context, tx gdb.TX, order *OrderDO) (int64, error) { if err := ValidateOrderDO(order); err != nil { return 0, err } id, err := tx.Ctx(ctx).Model(TableOrder).Data(order).InsertAndGetId() if err != nil { return 0, gerror.Wrap(err, "insert order failed") } return id, nil } // InsertContractOrderItem inserts the aggregate order item for an order. func (r *Repository) InsertContractOrderItem(ctx context.Context, tx gdb.TX, item *OrderItemDO) error { if err := ValidateOrderItemDO(item); err != nil { return err } _, err := tx.Ctx(ctx).Model(TableOrderItem).Data(item).Insert() return gerror.Wrap(err, "insert order item failed") } func ValidateOrderDO(order *OrderDO) error { if order == nil { return gerror.New("order cannot be nil") } if strings.TrimSpace(gconv.String(order.OrderSn)) == "" { return gerror.New("order number cannot be empty") } if gconv.Int64(order.CustomerAgentId) <= 0 { return gerror.New("order customer agent id must be greater than zero") } if gconv.Int64(order.AgentId) <= 0 { return gerror.New("order seller agent id must be greater than zero") } if strings.TrimSpace(gconv.String(order.TenantId)) == "" { return gerror.New("order tenant id cannot be empty") } if gconv.Int64(order.CreatedDept) <= 0 || gconv.Int64(order.CreatedBy) <= 0 || gconv.Int64(order.UpdatedBy) <= 0 { return gerror.New("order creator organization is incomplete") } if order.CreatedAt == nil || order.UpdatedAt == nil { return gerror.New("order audit time cannot be empty") } return nil } func ValidateOrderItemDO(item *OrderItemDO) error { if item == nil { return gerror.New("order item cannot be nil") } if gconv.Int64(item.OrderId) <= 0 { return gerror.New("order item order id must be greater than zero") } if strings.TrimSpace(gconv.String(item.OrderSn)) == "" { return gerror.New("order item order number cannot be empty") } if gconv.Int64(item.CustomerAgentId) <= 0 { return gerror.New("order item customer agent id must be greater than zero") } if gconv.Int64(item.AgentId) <= 0 { return gerror.New("order item seller agent id must be greater than zero") } if gconv.Int(item.Quantity) <= 0 { return gerror.New("order item quantity must be greater than zero") } if gconv.Int64(item.ProductId) <= 0 { return gerror.New("order item product id must be greater than zero") } if strings.TrimSpace(gconv.String(item.ProductName)) == "" || strings.TrimSpace(gconv.String(item.ProductSn)) == "" { return gerror.New("order item product contract is incomplete") } if item.ServiceBeginTime == nil || item.ServiceEndTime == nil || !item.ServiceEndTime.After(item.ServiceBeginTime) { return gerror.New("order item service period is invalid") } if strings.TrimSpace(gconv.String(item.TenantId)) == "" { return gerror.New("order item tenant id cannot be empty") } if gconv.Int64(item.CreatedDept) <= 0 || gconv.Int64(item.CreatedBy) <= 0 || gconv.Int64(item.UpdatedBy) <= 0 { return gerror.New("order item creator organization is incomplete") } if item.CreatedAt == nil || item.UpdatedAt == nil { return gerror.New("order item audit time cannot be empty") } return nil }