作者 陈志颖

fix:调整错误

  1 +package command
  2 +
  3 +import (
  4 + "fmt"
  5 + "time"
  6 +
  7 + "github.com/beego/beego/v2/core/validation"
  8 +)
  9 +
  10 +type CancelCommand struct {
  11 + // 订单编号
  12 + OrderNo string `json:"orderNo" valid:"Required"`
  13 + // 公司id
  14 + CompanyId int64 `json:"companyId" valid:"Required"`
  15 + // 是否是公司负责人
  16 + IsPrincipal bool `json:"isPrincipal" valid:"Required"`
  17 + // 统一用户id
  18 + Uid int64 `json:"uid" valid:"Required"`
  19 + // 用户账号
  20 + UserAccount string `json:"userAccount" valid:"Required"`
  21 + // 用户头像URL
  22 + UserAvatarUrl string `json:"userAvatarUrl" valid:"Required"`
  23 + // 用户名称
  24 + UserName string `json:"userName" valid:"Required"`
  25 + // 邮件地址
  26 + Email string `json:"email" valid:"Required"`
  27 + // 性别
  28 + Gender int `json:"gender" valid:"Required"`
  29 + // 入职时间
  30 + EntryTime time.Time `json:"entryTime" valid:"Required"`
  31 + // 分机
  32 + Extension string `json:"extension" valid:"Required"`
  33 + // 工作地
  34 + Workplace string `json:"workplace" valid:"Required"`
  35 + // 私人电话
  36 + PrivateNumber string `json:"privateNumber" valid:"Required"`
  37 + // 工号
  38 + JobNumber string `json:"jobNumber" valid:"Required"`
  39 +}
  40 +
  41 +func (cancelCommand *CancelCommand) Valid(validation *validation.Validation) {
  42 + validation.SetError("CustomValid", "未实现的自定义认证")
  43 +}
  44 +
  45 +func (cancelCommand *CancelCommand) ValidateCommand() error {
  46 + valid := validation.Validation{}
  47 + b, err := valid.Valid(cancelCommand)
  48 + if err != nil {
  49 + return err
  50 + }
  51 + if !b {
  52 + for _, validErr := range valid.Errors {
  53 + return fmt.Errorf("%s %s", validErr.Key, validErr.Message)
  54 + }
  55 + }
  56 + return nil
  57 +}
  1 +package command
  2 +
  3 +import (
  4 + "fmt"
  5 +
  6 + "github.com/beego/beego/v2/core/validation"
  7 +)
  8 +
  9 +type CreateOrderCommand struct {
  10 + // 订单编号
  11 + OrderNo string `json:"orderNo" valid:"Required"`
  12 + // 公司Id
  13 + CompanyId int64 `json:"companyId" valid:"Required"`
  14 + // 买家名称
  15 + BuyerName string `json:"buyerName,omitempty"`
  16 + // 联系信息
  17 + ContactInfo string `json:"contactInfo,omitempty"`
  18 + // 发货地址
  19 + ShippingAddress string `json:"shippingAddress,omitempty"`
  20 + // 订单商品总数
  21 + OrderNum int64 `json:"orderNum" valid:"Required"`
  22 + // 订单总价
  23 + TotalPrice float64 `json:"totalPrice" valid:"Required"`
  24 +}
  25 +
  26 +func (createOrderCommand *CreateOrderCommand) Valid(validation *validation.Validation) {
  27 + validation.SetError("CustomValid", "未实现的自定义认证")
  28 +}
  29 +
  30 +func (createOrderCommand *CreateOrderCommand) ValidateCommand() error {
  31 + valid := validation.Validation{}
  32 + b, err := valid.Valid(createOrderCommand)
  33 + if err != nil {
  34 + return err
  35 + }
  36 + if !b {
  37 + for _, validErr := range valid.Errors {
  38 + return fmt.Errorf("%s %s", validErr.Key, validErr.Message)
  39 + }
  40 + }
  41 + return nil
  42 +}
  1 +package command
  2 +
  3 +import (
  4 + "fmt"
  5 +
  6 + "github.com/beego/beego/v2/core/validation"
  7 +)
  8 +
  9 +type DrawDividendsCommand struct {
  10 + // 参与分红的合伙人
  11 + PartnerId int64 `json:"partnerId" valid:"Required"`
  12 + // 进行分红的订单编号
  13 + OrderNo string `json:"orderNo" valid:"Required"`
  14 +}
  15 +
  16 +func (drawDividendsCommand *DrawDividendsCommand) Valid(validation *validation.Validation) {
  17 + validation.SetError("CustomValid", "未实现的自定义认证")
  18 +}
  19 +
  20 +func (drawDividendsCommand *DrawDividendsCommand) ValidateCommand() error {
  21 + valid := validation.Validation{}
  22 + b, err := valid.Valid(drawDividendsCommand)
  23 + if err != nil {
  24 + return err
  25 + }
  26 + if !b {
  27 + for _, validErr := range valid.Errors {
  28 + return fmt.Errorf("%s %s", validErr.Key, validErr.Message)
  29 + }
  30 + }
  31 + return nil
  32 +}
  1 +package command
  2 +
  3 +import (
  4 + "fmt"
  5 + "time"
  6 +
  7 + "github.com/beego/beego/v2/core/validation"
  8 +)
  9 +
  10 +type ReceivingCommand struct {
  11 + // 订单号
  12 + OrderNo string `json:"orderNo" valid:"Required"`
  13 + // 是否是公司负责人
  14 + IsPrincipal bool `json:"isPrincipal" valid:"Required"`
  15 + // 统一用户id
  16 + Uid int64 `json:"uid" valid:"Required"`
  17 + // 用户账号
  18 + UserAccount string `json:"userAccount" valid:"Required"`
  19 + // 用户头像URL
  20 + UserAvatarUrl string `json:"userAvatarUrl" valid:"Required"`
  21 + // 用户名称
  22 + UserName string `json:"userName" valid:"Required"`
  23 + // 邮件地址
  24 + Email string `json:"email" valid:"Required"`
  25 + // 性别
  26 + Gender int `json:"gender" valid:"Required"`
  27 + // 入职时间
  28 + EntryTime time.Time `json:"entryTime" valid:"Required"`
  29 + // 分机
  30 + Extension string `json:"extension" valid:"Required"`
  31 + // 工作地
  32 + Workplace string `json:"workplace" valid:"Required"`
  33 + // 私人电话
  34 + PrivateNumber string `json:"privateNumber" valid:"Required"`
  35 + // 工号
  36 + JobNumber string `json:"jobNumber" valid:"Required"`
  37 + // 公司id
  38 + CompanyId int64 `json:"companyId" valid:"Required"`
  39 +}
  40 +
  41 +func (receivingCommand *ReceivingCommand) Valid(validation *validation.Validation) {
  42 + validation.SetError("CustomValid", "未实现的自定义认证")
  43 +}
  44 +
  45 +func (receivingCommand *ReceivingCommand) ValidateCommand() error {
  46 + valid := validation.Validation{}
  47 + b, err := valid.Valid(receivingCommand)
  48 + if err != nil {
  49 + return err
  50 + }
  51 + if !b {
  52 + for _, validErr := range valid.Errors {
  53 + return fmt.Errorf("%s %s", validErr.Key, validErr.Message)
  54 + }
  55 + }
  56 + return nil
  57 +}
  1 +package command
  2 +
  3 +import (
  4 + "fmt"
  5 +
  6 + "github.com/beego/beego/v2/core/validation"
  7 +)
  8 +
  9 +type RemoveOrderCommand struct {
  10 + // 订单id
  11 + OrderId int64 `json:"orderId" valid:"Required"`
  12 +}
  13 +
  14 +func (removeOrderCommand *RemoveOrderCommand) Valid(validation *validation.Validation) {
  15 + validation.SetError("CustomValid", "未实现的自定义认证")
  16 +}
  17 +
  18 +func (removeOrderCommand *RemoveOrderCommand) ValidateCommand() error {
  19 + valid := validation.Validation{}
  20 + b, err := valid.Valid(removeOrderCommand)
  21 + if err != nil {
  22 + return err
  23 + }
  24 + if !b {
  25 + for _, validErr := range valid.Errors {
  26 + return fmt.Errorf("%s %s", validErr.Key, validErr.Message)
  27 + }
  28 + }
  29 + return nil
  30 +}
  1 +package command
  2 +
  3 +import (
  4 + "fmt"
  5 + "time"
  6 +
  7 + "github.com/beego/beego/v2/core/validation"
  8 +)
  9 +
  10 +type ReturnCommand struct {
  11 + // 订单号
  12 + OrderNo string `json:"orderNo" valid:"Required"`
  13 + // 是否是公司负责人
  14 + IsPrincipal bool `json:"isPrincipal" valid:"Required"`
  15 + // 统一用户id
  16 + Uid int64 `json:"uid" valid:"Required"`
  17 + // 用户账号
  18 + UserAccount string `json:"userAccount" valid:"Required"`
  19 + // 用户头像URL
  20 + UserAvatarUrl string `json:"userAvatarUrl" valid:"Required"`
  21 + // 用户名称
  22 + UserName string `json:"userName" valid:"Required"`
  23 + // 邮件地址
  24 + Email string `json:"email" valid:"Required"`
  25 + // 性别
  26 + Gender int `json:"gender" valid:"Required"`
  27 + // 入职时间
  28 + EntryTime time.Time `json:"entryTime" valid:"Required"`
  29 + // 分机
  30 + Extension string `json:"extension" valid:"Required"`
  31 + // 工作地
  32 + Workplace string `json:"workplace" valid:"Required"`
  33 + // 私人电话
  34 + PrivateNumber string `json:"privateNumber" valid:"Required"`
  35 + // 工号
  36 + JobNumber string `json:"jobNumber" valid:"Required"`
  37 + // 公司id
  38 + CompanyId int64 `json:"companyId" valid:"Required"`
  39 +}
  40 +
  41 +func (returnCommand *ReturnCommand) Valid(validation *validation.Validation) {
  42 + validation.SetError("CustomValid", "未实现的自定义认证")
  43 +}
  44 +
  45 +func (returnCommand *ReturnCommand) ValidateCommand() error {
  46 + valid := validation.Validation{}
  47 + b, err := valid.Valid(returnCommand)
  48 + if err != nil {
  49 + return err
  50 + }
  51 + if !b {
  52 + for _, validErr := range valid.Errors {
  53 + return fmt.Errorf("%s %s", validErr.Key, validErr.Message)
  54 + }
  55 + }
  56 + return nil
  57 +}
  1 +package command
  2 +
  3 +import (
  4 + "fmt"
  5 +
  6 + "github.com/beego/beego/v2/core/validation"
  7 +)
  8 +
  9 +type ShippingCommand struct {
  10 + // 发货单号
  11 + DeliveryCode string `json:"deliveryCode" valid:"Required"`
  12 + // 订单编号
  13 + OrderNo string `json:"orderNo" valid:"Required"`
  14 + // 订单数量
  15 + OrderNum int64 `json:"orderNum" valid:"Required"`
  16 +}
  17 +
  18 +func (shippingCommand *ShippingCommand) Valid(validation *validation.Validation) {
  19 + validation.SetError("CustomValid", "未实现的自定义认证")
  20 +}
  21 +
  22 +func (shippingCommand *ShippingCommand) ValidateCommand() error {
  23 + valid := validation.Validation{}
  24 + b, err := valid.Valid(shippingCommand)
  25 + if err != nil {
  26 + return err
  27 + }
  28 + if !b {
  29 + for _, validErr := range valid.Errors {
  30 + return fmt.Errorf("%s %s", validErr.Key, validErr.Message)
  31 + }
  32 + }
  33 + return nil
  34 +}
  1 +package command
  2 +
  3 +import (
  4 + "fmt"
  5 +
  6 + "github.com/beego/beego/v2/core/validation"
  7 +)
  8 +
  9 +type UpdateOrderCommand struct {
  10 + // 订单id
  11 + OrderId int64 `json:"orderId" valid:"Required"`
  12 +}
  13 +
  14 +func (updateOrderCommand *UpdateOrderCommand) Valid(validation *validation.Validation) {
  15 + validation.SetError("CustomValid", "未实现的自定义认证")
  16 +}
  17 +
  18 +func (updateOrderCommand *UpdateOrderCommand) ValidateCommand() error {
  19 + valid := validation.Validation{}
  20 + b, err := valid.Valid(updateOrderCommand)
  21 + if err != nil {
  22 + return err
  23 + }
  24 + if !b {
  25 + for _, validErr := range valid.Errors {
  26 + return fmt.Errorf("%s %s", validErr.Key, validErr.Message)
  27 + }
  28 + }
  29 + return nil
  30 +}
  1 +package query
  2 +
  3 +import (
  4 + "fmt"
  5 +
  6 + "github.com/beego/beego/v2/core/validation"
  7 +)
  8 +
  9 +type GetOrderQuery struct {
  10 + // 订单id
  11 + OrderId int64 `json:"orderId" valid:"Required"`
  12 +}
  13 +
  14 +func (getOrderQuery *GetOrderQuery) Valid(validation *validation.Validation) {
  15 + validation.SetError("CustomValid", "未实现的自定义认证")
  16 +}
  17 +
  18 +func (getOrderQuery *GetOrderQuery) ValidateQuery() error {
  19 + valid := validation.Validation{}
  20 + b, err := valid.Valid(getOrderQuery)
  21 + if err != nil {
  22 + return err
  23 + }
  24 + if !b {
  25 + for _, validErr := range valid.Errors {
  26 + return fmt.Errorf("%s %s", validErr.Key, validErr.Message)
  27 + }
  28 + }
  29 + return nil
  30 +}
  1 +package query
  2 +
  3 +import (
  4 + "fmt"
  5 +
  6 + "github.com/beego/beego/v2/core/validation"
  7 +)
  8 +
  9 +type ListOrderQuery struct {
  10 + // 查询偏离量
  11 + Offset int `json:"offset" valid:"Required"`
  12 + // 查询限制
  13 + Limit int `json:"limit" valid:"Required"`
  14 +}
  15 +
  16 +func (listOrderQuery *ListOrderQuery) Valid(validation *validation.Validation) {
  17 + validation.SetError("CustomValid", "未实现的自定义认证")
  18 +}
  19 +
  20 +func (listOrderQuery *ListOrderQuery) ValidateQuery() error {
  21 + valid := validation.Validation{}
  22 + b, err := valid.Valid(listOrderQuery)
  23 + if err != nil {
  24 + return err
  25 + }
  26 + if !b {
  27 + for _, validErr := range valid.Errors {
  28 + return fmt.Errorf("%s %s", validErr.Key, validErr.Message)
  29 + }
  30 + }
  31 + return nil
  32 +}
  1 +package service
  2 +
  3 +import (
  4 + "fmt"
  5 + "github.com/linmadan/egglib-go/core/application"
  6 + "github.com/linmadan/egglib-go/utils/tool_funs"
  7 + "gitlab.fjmaimaimai.com/mmm-go-pp/partner01/pkg/application/factory"
  8 + "gitlab.fjmaimaimai.com/mmm-go-pp/partner01/pkg/application/order/command"
  9 + "gitlab.fjmaimaimai.com/mmm-go-pp/partner01/pkg/application/order/query"
  10 + "gitlab.fjmaimaimai.com/mmm-go-pp/partner01/pkg/domain"
  11 +)
  12 +
  13 +// 订单基础服务
  14 +type OrderService struct {
  15 +}
  16 +
  17 +// 取消订单服务
  18 +func (orderService *OrderService) Cancel(cancelCommand *command.CancelCommand) (interface{}, error) {
  19 + if err := cancelCommand.ValidateCommand(); err != nil {
  20 + return nil, application.ThrowError(application.ARG_ERROR, err.Error())
  21 + }
  22 + transactionContext, err := factory.CreateTransactionContext(nil)
  23 + if err != nil {
  24 + return nil, application.ThrowError(application.TRANSACTION_ERROR, err.Error())
  25 + }
  26 + if err := transactionContext.StartTransaction(); err != nil {
  27 + return nil, application.ThrowError(application.TRANSACTION_ERROR, err.Error())
  28 + }
  29 + defer func() {
  30 + _ = transactionContext.RollbackTransaction()
  31 + }()
  32 + if err := transactionContext.CommitTransaction(); err != nil {
  33 + return nil, application.ThrowError(application.TRANSACTION_ERROR, err.Error())
  34 + }
  35 + return nil, nil
  36 +}
  37 +
  38 +// 创建订单增删改查
  39 +func (orderService *OrderService) CreateOrder(createOrderCommand *command.CreateOrderCommand) (interface{}, error) {
  40 + if err := createOrderCommand.ValidateCommand(); err != nil {
  41 + return nil, application.ThrowError(application.ARG_ERROR, err.Error())
  42 + }
  43 + transactionContext, err := factory.CreateTransactionContext(nil)
  44 + if err != nil {
  45 + return nil, application.ThrowError(application.TRANSACTION_ERROR, err.Error())
  46 + }
  47 + if err := transactionContext.StartTransaction(); err != nil {
  48 + return nil, application.ThrowError(application.TRANSACTION_ERROR, err.Error())
  49 + }
  50 + defer func() {
  51 + _ = transactionContext.RollbackTransaction()
  52 + }()
  53 + newOrder := &domain.Order{
  54 + OrderNo: createOrderCommand.OrderNo,
  55 + CompanyId: createOrderCommand.CompanyId,
  56 + Buyer: &domain.BuyerInfo{
  57 + BuyerName: createOrderCommand.BuyerName,
  58 + },
  59 + TotalPrice: createOrderCommand.TotalPrice,
  60 + }
  61 + var orderRepository domain.OrderRepository
  62 + if value, err := factory.CreateOrderRepository(map[string]interface{}{
  63 + "transactionContext": transactionContext,
  64 + }); err != nil {
  65 + return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, err.Error())
  66 + } else {
  67 + orderRepository = value
  68 + }
  69 + if order, err := orderRepository.Save(newOrder); err != nil {
  70 + return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, err.Error())
  71 + } else {
  72 + if err := transactionContext.CommitTransaction(); err != nil {
  73 + return nil, application.ThrowError(application.TRANSACTION_ERROR, err.Error())
  74 + }
  75 + return order, nil
  76 + }
  77 +}
  78 +
  79 +// 订单分红服务
  80 +func (orderService *OrderService) DarwDividends(darwDividendsCommand *command.DrawDividendsCommand) (interface{}, error) {
  81 + if err := darwDividendsCommand.ValidateCommand(); err != nil {
  82 + return nil, application.ThrowError(application.ARG_ERROR, err.Error())
  83 + }
  84 + transactionContext, err := factory.CreateTransactionContext(nil)
  85 + if err != nil {
  86 + return nil, application.ThrowError(application.TRANSACTION_ERROR, err.Error())
  87 + }
  88 + if err := transactionContext.StartTransaction(); err != nil {
  89 + return nil, application.ThrowError(application.TRANSACTION_ERROR, err.Error())
  90 + }
  91 + defer func() {
  92 + transactionContext.RollbackTransaction()
  93 + }()
  94 + if err := transactionContext.CommitTransaction(); err != nil {
  95 + return nil, application.ThrowError(application.TRANSACTION_ERROR, err.Error())
  96 + }
  97 + return nil, nil
  98 +}
  99 +
  100 +// 返回订单增删改查
  101 +func (orderService *OrderService) GetOrder(getOrderQuery *query.GetOrderQuery) (interface{}, error) {
  102 + if err := getOrderQuery.ValidateQuery(); err != nil {
  103 + return nil, application.ThrowError(application.ARG_ERROR, err.Error())
  104 + }
  105 + transactionContext, err := factory.CreateTransactionContext(nil)
  106 + if err != nil {
  107 + return nil, application.ThrowError(application.TRANSACTION_ERROR, err.Error())
  108 + }
  109 + if err := transactionContext.StartTransaction(); err != nil {
  110 + return nil, application.ThrowError(application.TRANSACTION_ERROR, err.Error())
  111 + }
  112 + defer func() {
  113 + transactionContext.RollbackTransaction()
  114 + }()
  115 + var orderRepository domain.OrderRepository
  116 + if value, err := factory.CreateOrderRepository(map[string]interface{}{
  117 + "transactionContext": transactionContext,
  118 + }); err != nil {
  119 + return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, err.Error())
  120 + } else {
  121 + orderRepository = value
  122 + }
  123 + order, err := orderRepository.FindOne(map[string]interface{}{"orderId": getOrderQuery.OrderId})
  124 + if err != nil {
  125 + return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, err.Error())
  126 + }
  127 + if order == nil {
  128 + return nil, application.ThrowError(application.RES_NO_FIND_ERROR, fmt.Sprintf("%s", string(getOrderQuery.OrderId)))
  129 + } else {
  130 + if err := transactionContext.CommitTransaction(); err != nil {
  131 + return nil, application.ThrowError(application.TRANSACTION_ERROR, err.Error())
  132 + }
  133 + return order, nil
  134 + }
  135 +}
  136 +
  137 +// 返回订单增删改查列表
  138 +func (orderService *OrderService) ListOrder(listOrderQuery *query.ListOrderQuery) (interface{}, error) {
  139 + if err := listOrderQuery.ValidateQuery(); err != nil {
  140 + return nil, application.ThrowError(application.ARG_ERROR, err.Error())
  141 + }
  142 + transactionContext, err := factory.CreateTransactionContext(nil)
  143 + if err != nil {
  144 + return nil, application.ThrowError(application.TRANSACTION_ERROR, err.Error())
  145 + }
  146 + if err := transactionContext.StartTransaction(); err != nil {
  147 + return nil, application.ThrowError(application.TRANSACTION_ERROR, err.Error())
  148 + }
  149 + defer func() {
  150 + transactionContext.RollbackTransaction()
  151 + }()
  152 + var orderRepository domain.OrderRepository
  153 + if value, err := factory.CreateOrderRepository(map[string]interface{}{
  154 + "transactionContext": transactionContext,
  155 + }); err != nil {
  156 + return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, err.Error())
  157 + } else {
  158 + orderRepository = value
  159 + }
  160 + if count, orders, err := orderRepository.Find(tool_funs.SimpleStructToMap(listOrderQuery)); err != nil {
  161 + return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, err.Error())
  162 + } else {
  163 + if err := transactionContext.CommitTransaction(); err != nil {
  164 + return nil, application.ThrowError(application.TRANSACTION_ERROR, err.Error())
  165 + }
  166 + return map[string]interface{}{
  167 + "count": count,
  168 + "orders": orders,
  169 + }, nil
  170 + }
  171 +}
  172 +
  173 +// 订单收货服务
  174 +func (orderService *OrderService) Receiving(receivingCommand *command.ReceivingCommand) (interface{}, error) {
  175 + if err := receivingCommand.ValidateCommand(); err != nil {
  176 + return nil, application.ThrowError(application.ARG_ERROR, err.Error())
  177 + }
  178 + transactionContext, err := factory.CreateTransactionContext(nil)
  179 + if err != nil {
  180 + return nil, application.ThrowError(application.TRANSACTION_ERROR, err.Error())
  181 + }
  182 + if err := transactionContext.StartTransaction(); err != nil {
  183 + return nil, application.ThrowError(application.TRANSACTION_ERROR, err.Error())
  184 + }
  185 + defer func() {
  186 + transactionContext.RollbackTransaction()
  187 + }()
  188 + if err := transactionContext.CommitTransaction(); err != nil {
  189 + return nil, application.ThrowError(application.TRANSACTION_ERROR, err.Error())
  190 + }
  191 + return nil, nil
  192 +}
  193 +
  194 +// 移除订单增删改查
  195 +func (orderService *OrderService) RemoveOrder(removeOrderCommand *command.RemoveOrderCommand) (interface{}, error) {
  196 + if err := removeOrderCommand.ValidateCommand(); err != nil {
  197 + return nil, application.ThrowError(application.ARG_ERROR, err.Error())
  198 + }
  199 + transactionContext, err := factory.CreateTransactionContext(nil)
  200 + if err != nil {
  201 + return nil, application.ThrowError(application.TRANSACTION_ERROR, err.Error())
  202 + }
  203 + if err := transactionContext.StartTransaction(); err != nil {
  204 + return nil, application.ThrowError(application.TRANSACTION_ERROR, err.Error())
  205 + }
  206 + defer func() {
  207 + transactionContext.RollbackTransaction()
  208 + }()
  209 + var orderRepository domain.OrderRepository
  210 + if value, err := factory.CreateOrderRepository(map[string]interface{}{
  211 + "transactionContext": transactionContext,
  212 + }); err != nil {
  213 + return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, err.Error())
  214 + } else {
  215 + orderRepository = value
  216 + }
  217 + order, err := orderRepository.FindOne(map[string]interface{}{"orderId": removeOrderCommand.OrderId})
  218 + if err != nil {
  219 + return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, err.Error())
  220 + }
  221 + if order == nil {
  222 + return nil, application.ThrowError(application.RES_NO_FIND_ERROR, fmt.Sprintf("%s", string(removeOrderCommand.OrderId)))
  223 + }
  224 + if order, err := orderRepository.Remove(order); err != nil {
  225 + return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, err.Error())
  226 + } else {
  227 + if err := transactionContext.CommitTransaction(); err != nil {
  228 + return nil, application.ThrowError(application.TRANSACTION_ERROR, err.Error())
  229 + }
  230 + return order, nil
  231 + }
  232 +}
  233 +
  234 +// 订单退货服务
  235 +func (orderService *OrderService) Return(returnCommand *command.ReturnCommand) (interface{}, error) {
  236 + if err := returnCommand.ValidateCommand(); err != nil {
  237 + return nil, application.ThrowError(application.ARG_ERROR, err.Error())
  238 + }
  239 + transactionContext, err := factory.CreateTransactionContext(nil)
  240 + if err != nil {
  241 + return nil, application.ThrowError(application.TRANSACTION_ERROR, err.Error())
  242 + }
  243 + if err := transactionContext.StartTransaction(); err != nil {
  244 + return nil, application.ThrowError(application.TRANSACTION_ERROR, err.Error())
  245 + }
  246 + defer func() {
  247 + transactionContext.RollbackTransaction()
  248 + }()
  249 + if err := transactionContext.CommitTransaction(); err != nil {
  250 + return nil, application.ThrowError(application.TRANSACTION_ERROR, err.Error())
  251 + }
  252 + return nil, nil
  253 +}
  254 +
  255 +// 订单发货服务
  256 +func (orderService *OrderService) Shipping(shippingCommand *command.ShippingCommand) (interface{}, error) {
  257 + if err := shippingCommand.ValidateCommand(); err != nil {
  258 + return nil, application.ThrowError(application.ARG_ERROR, err.Error())
  259 + }
  260 + transactionContext, err := factory.CreateTransactionContext(nil)
  261 + if err != nil {
  262 + return nil, application.ThrowError(application.TRANSACTION_ERROR, err.Error())
  263 + }
  264 + if err := transactionContext.StartTransaction(); err != nil {
  265 + return nil, application.ThrowError(application.TRANSACTION_ERROR, err.Error())
  266 + }
  267 + defer func() {
  268 + transactionContext.RollbackTransaction()
  269 + }()
  270 + if err := transactionContext.CommitTransaction(); err != nil {
  271 + return nil, application.ThrowError(application.TRANSACTION_ERROR, err.Error())
  272 + }
  273 + return nil, nil
  274 +}
  275 +
  276 +// 更新订单增删改查
  277 +func (orderService *OrderService) UpdateOrder(updateOrderCommand *command.UpdateOrderCommand) (interface{}, error) {
  278 + if err := updateOrderCommand.ValidateCommand(); err != nil {
  279 + return nil, application.ThrowError(application.ARG_ERROR, err.Error())
  280 + }
  281 + transactionContext, err := factory.CreateTransactionContext(nil)
  282 + if err != nil {
  283 + return nil, application.ThrowError(application.TRANSACTION_ERROR, err.Error())
  284 + }
  285 + if err := transactionContext.StartTransaction(); err != nil {
  286 + return nil, application.ThrowError(application.TRANSACTION_ERROR, err.Error())
  287 + }
  288 + defer func() {
  289 + _ = transactionContext.RollbackTransaction()
  290 + }()
  291 + var orderRepository domain.OrderRepository
  292 + if value, err := factory.CreateOrderRepository(map[string]interface{}{
  293 + "transactionContext": transactionContext,
  294 + }); err != nil {
  295 + return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, err.Error())
  296 + } else {
  297 + orderRepository = value
  298 + }
  299 + order, err := orderRepository.FindOne(map[string]interface{}{"orderId": updateOrderCommand.OrderId})
  300 + if err != nil {
  301 + return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, err.Error())
  302 + }
  303 + if order == nil {
  304 + return nil, application.ThrowError(application.RES_NO_FIND_ERROR, fmt.Sprintf("%s", string(updateOrderCommand.OrderId)))
  305 + }
  306 + if err := order.Update(tool_funs.SimpleStructToMap(updateOrderCommand)); err != nil {
  307 + return nil, application.ThrowError(application.BUSINESS_ERROR, err.Error())
  308 + }
  309 + if order, err := orderRepository.Save(order); err != nil {
  310 + return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, err.Error())
  311 + } else {
  312 + if err := transactionContext.CommitTransaction(); err != nil {
  313 + return nil, application.ThrowError(application.TRANSACTION_ERROR, err.Error())
  314 + }
  315 + return order, nil
  316 + }
  317 +}
  318 +
  319 +func NewOrderService(options map[string]interface{}) *OrderService {
  320 + newOrderService := &OrderService{}
  321 + return newOrderService
  322 +}
  1 +package controllers
  2 +
  3 +import (
  4 + "github.com/linmadan/egglib-go/web/beego"
  5 + "gitlab.fjmaimaimai.com/mmm-go-pp/partner01/pkg/application/order/command"
  6 + "gitlab.fjmaimaimai.com/mmm-go-pp/partner01/pkg/application/order/query"
  7 + "gitlab.fjmaimaimai.com/mmm-go-pp/partner01/pkg/application/order/service"
  8 +)
  9 +
  10 +type OrderController struct {
  11 + beego.BaseController
  12 +}
  13 +
  14 +func (controller *OrderController) CreateOrder() {
  15 + orderService := service.NewOrderService(nil)
  16 + createOrderCommand := &command.CreateOrderCommand{}
  17 + controller.Unmarshal(createOrderCommand)
  18 + data, err := orderService.CreateOrder(createOrderCommand)
  19 + controller.Response(data, err)
  20 +}
  21 +
  22 +func (controller *OrderController) UpdateOrder() {
  23 + orderService := service.NewOrderService(nil)
  24 + updateOrderCommand := &command.UpdateOrderCommand{}
  25 + controller.Unmarshal(updateOrderCommand)
  26 + orderId, _ := controller.GetInt64(":orderId")
  27 + updateOrderCommand.OrderId = orderId
  28 + data, err := orderService.UpdateOrder(updateOrderCommand)
  29 + controller.Response(data, err)
  30 +}
  31 +
  32 +func (controller *OrderController) GetOrder() {
  33 + orderService := service.NewOrderService(nil)
  34 + getOrderQuery := &query.GetOrderQuery{}
  35 + orderId, _ := controller.GetInt64(":orderId")
  36 + getOrderQuery.OrderId = orderId
  37 + data, err := orderService.GetOrder(getOrderQuery)
  38 + controller.Response(data, err)
  39 +}
  40 +
  41 +func (controller *OrderController) RemoveOrder() {
  42 + orderService := service.NewOrderService(nil)
  43 + removeOrderCommand := &command.RemoveOrderCommand{}
  44 + controller.Unmarshal(removeOrderCommand)
  45 + orderId, _ := controller.GetInt64(":orderId")
  46 + removeOrderCommand.OrderId = orderId
  47 + data, err := orderService.RemoveOrder(removeOrderCommand)
  48 + controller.Response(data, err)
  49 +}
  50 +
  51 +func (controller *OrderController) ListOrder() {
  52 + orderService := service.NewOrderService(nil)
  53 + listOrderQuery := &query.ListOrderQuery{}
  54 + offset, _ := controller.GetInt("offset")
  55 + listOrderQuery.Offset = offset
  56 + limit, _ := controller.GetInt("limit")
  57 + listOrderQuery.Limit = limit
  58 + data, err := orderService.ListOrder(listOrderQuery)
  59 + controller.Response(data, err)
  60 +}
  1 +package routers
  2 +
  3 +import (
  4 + "github.com/beego/beego/v2/server/web"
  5 + "gitlab.fjmaimaimai.com/mmm-go-pp/partner01/pkg/port/beego/controllers"
  6 +)
  7 +
  8 +func init() {
  9 + web.Router("/orders/", &controllers.OrderController{}, "Post:CreateOrder")
  10 + web.Router("/orders/:orderId", &controllers.OrderController{}, "Put:UpdateOrder")
  11 + web.Router("/orders/:orderId", &controllers.OrderController{}, "Get:GetOrder")
  12 + web.Router("/orders/:orderId", &controllers.OrderController{}, "Delete:RemoveOrder")
  13 + web.Router("/orders/", &controllers.OrderController{}, "Get:ListOrder")
  14 +}
  1 +package order
  2 +
  3 +import (
  4 + "net/http"
  5 +
  6 + "github.com/gavv/httpexpect"
  7 + . "github.com/onsi/ginkgo"
  8 + . "github.com/onsi/gomega"
  9 + pG "gitlab.fjmaimaimai.com/mmm-go-pp/partner01/pkg/infrastructure/pg"
  10 +)
  11 +
  12 +var _ = Describe("创建订单增删改查", func() {
  13 + Describe("提交数据创建订单增删改查", func() {
  14 + Context("提交正确的新订单实体数据", func() {
  15 + It("返回订单实体数据", func() {
  16 + httpExpect := httpexpect.New(GinkgoT(), server.URL)
  17 + body := map[string]interface{}{
  18 + "orderNo": "string",
  19 + "companyId": "int64",
  20 + "buyerName": "string",
  21 + "contactInfo": "string",
  22 + "shippingAddress": "string",
  23 + "orderNum": "int64",
  24 + "totalPrice": "float64",
  25 + }
  26 + httpExpect.POST("/orders/").
  27 + WithJSON(body).
  28 + Expect().
  29 + Status(http.StatusOK).
  30 + JSON().
  31 + Object().
  32 + ContainsKey("code").ValueEqual("code", 0).
  33 + ContainsKey("msg").ValueEqual("msg", "ok").
  34 + ContainsKey("data").Value("data").Object().
  35 + ContainsKey("orderId").ValueNotEqual("orderId", BeZero())
  36 + })
  37 + })
  38 + })
  39 + AfterEach(func() {
  40 + _, err := pG.DB.Exec("DELETE FROM orders WHERE true")
  41 + Expect(err).NotTo(HaveOccurred())
  42 + })
  43 +})
  1 +package order
  2 +
  3 +import (
  4 + "net/http"
  5 +
  6 + "github.com/gavv/httpexpect"
  7 + . "github.com/onsi/ginkgo"
  8 + . "github.com/onsi/gomega"
  9 + pG "gitlab.fjmaimaimai.com/mmm-go-pp/partner01/pkg/infrastructure/pg"
  10 +)
  11 +
  12 +var _ = Describe("返回订单增删改查", func() {
  13 + var orderId int64
  14 + BeforeEach(func() {
  15 + _, err := pG.DB.QueryOne(
  16 + pg.Scan(&orderId),
  17 + "INSERT INTO orders (order_id, buyer, company_id, partner_id, delivery_code, is_disable, order_no, order_detail, order_dividend, order_dividend_status, order_goods, order_source, order_type, order_status, total_price, region_info, remarks, create_at, update_at, delete_at) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?) RETURNING order_id",
  18 + "testOrderId", "testBuyer", "testCompanyId", "testPartnerId", "testDeliveryCode", "testIsDisable", "testOrderNo", "testOrderDetail", "testOrderDividend", "testOrderDividendStatus", "testOrderGoods", "testOrderSource", "testOrderType", "testOrderStatus", "testTotalPrice", "testRegionInfo", "testRemarks", "testCreateAt", "testUpdateAt", "testDeleteAt")
  19 + Expect(err).NotTo(HaveOccurred())
  20 + })
  21 + Describe("根据orderId参数返回订单实体", func() {
  22 + Context("传入有效的orderId", func() {
  23 + It("返回订单实体数据", func() {
  24 + httpExpect := httpexpect.New(GinkgoT(), server.URL)
  25 + httpExpect.GET("/orders/{orderId}").
  26 + Expect().
  27 + Status(http.StatusOK).
  28 + JSON().
  29 + Object().
  30 + ContainsKey("code").ValueEqual("code", 0).
  31 + ContainsKey("msg").ValueEqual("msg", "ok").
  32 + ContainsKey("data").Value("data").Object()
  33 + })
  34 + })
  35 + })
  36 + AfterEach(func() {
  37 + _, err := pG.DB.Exec("DELETE FROM orders WHERE true")
  38 + Expect(err).NotTo(HaveOccurred())
  39 + })
  40 +})
  1 +package order
  2 +
  3 +import (
  4 + "net/http"
  5 +
  6 + "github.com/gavv/httpexpect"
  7 + . "github.com/onsi/ginkgo"
  8 + . "github.com/onsi/gomega"
  9 + pG "gitlab.fjmaimaimai.com/mmm-go-pp/partner01/pkg/infrastructure/pg"
  10 +)
  11 +
  12 +var _ = Describe("返回订单增删改查列表", func() {
  13 + var orderId int64
  14 + BeforeEach(func() {
  15 + _, err := pG.DB.QueryOne(
  16 + pg.Scan(&orderId),
  17 + "INSERT INTO orders (order_id, buyer, company_id, partner_id, delivery_code, is_disable, order_no, order_detail, order_dividend, order_dividend_status, order_goods, order_source, order_type, order_status, total_price, region_info, remarks, create_at, update_at, delete_at) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?) RETURNING order_id",
  18 + "testOrderId", "testBuyer", "testCompanyId", "testPartnerId", "testDeliveryCode", "testIsDisable", "testOrderNo", "testOrderDetail", "testOrderDividend", "testOrderDividendStatus", "testOrderGoods", "testOrderSource", "testOrderType", "testOrderStatus", "testTotalPrice", "testRegionInfo", "testRemarks", "testCreateAt", "testUpdateAt", "testDeleteAt")
  19 + Expect(err).NotTo(HaveOccurred())
  20 + })
  21 + Describe("根据参数返回订单实体列表", func() {
  22 + Context("传入有效的参数", func() {
  23 + It("返回订单实体数据列表", func() {
  24 + httpExpect := httpexpect.New(GinkgoT(), server.URL)
  25 + httpExpect.GET("/orders/").
  26 + WithQuery("offset", "int").
  27 + WithQuery("limit", "int").
  28 + Expect().
  29 + Status(http.StatusOK).
  30 + JSON().
  31 + Object().
  32 + ContainsKey("code").ValueEqual("code", 0).
  33 + ContainsKey("msg").ValueEqual("msg", "ok").
  34 + ContainsKey("data").Value("data").Object().
  35 + ContainsKey("count").ValueEqual("count", 1).
  36 + ContainsKey("orders").Value("orders").Array()
  37 + })
  38 + })
  39 + })
  40 + AfterEach(func() {
  41 + _, err := pG.DB.Exec("DELETE FROM orders WHERE true")
  42 + Expect(err).NotTo(HaveOccurred())
  43 + })
  44 +})
  1 +package order
  2 +
  3 +import (
  4 + "net/http"
  5 + "net/http/httptest"
  6 + "testing"
  7 +
  8 + "github.com/beego/beego/v2/server/web"
  9 + . "github.com/onsi/ginkgo"
  10 + . "github.com/onsi/gomega"
  11 + _ "gitlab.fjmaimaimai.com/mmm-go-pp/partner01/pkg/infrastructure/pg"
  12 + _ "gitlab.fjmaimaimai.com/mmm-go-pp/partner01/pkg/port/beego"
  13 +)
  14 +
  15 +func TestOrder(t *testing.T) {
  16 + RegisterFailHandler(Fail)
  17 + RunSpecs(t, "Beego Port Order Correlations Test Case Suite")
  18 +}
  19 +
  20 +var handler http.Handler
  21 +var server *httptest.Server
  22 +
  23 +var _ = BeforeSuite(func() {
  24 + handler = web.BeeApp.Handlers
  25 + server = httptest.NewServer(handler)
  26 +})
  27 +
  28 +var _ = AfterSuite(func() {
  29 + server.Close()
  30 +})
  1 +package order
  2 +
  3 +import (
  4 + "net/http"
  5 +
  6 + "github.com/gavv/httpexpect"
  7 + . "github.com/onsi/ginkgo"
  8 + . "github.com/onsi/gomega"
  9 + pG "gitlab.fjmaimaimai.com/mmm-go-pp/partner01/pkg/infrastructure/pg"
  10 +)
  11 +
  12 +var _ = Describe("移除订单增删改查", func() {
  13 + var orderId int64
  14 + BeforeEach(func() {
  15 + _, err := pG.DB.QueryOne(
  16 + pg.Scan(&orderId),
  17 + "INSERT INTO orders (order_id, buyer, company_id, partner_id, delivery_code, is_disable, order_no, order_detail, order_dividend, order_dividend_status, order_goods, order_source, order_type, order_status, total_price, region_info, remarks, create_at, update_at, delete_at) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?) RETURNING order_id",
  18 + "testOrderId", "testBuyer", "testCompanyId", "testPartnerId", "testDeliveryCode", "testIsDisable", "testOrderNo", "testOrderDetail", "testOrderDividend", "testOrderDividendStatus", "testOrderGoods", "testOrderSource", "testOrderType", "testOrderStatus", "testTotalPrice", "testRegionInfo", "testRemarks", "testCreateAt", "testUpdateAt", "testDeleteAt")
  19 + Expect(err).NotTo(HaveOccurred())
  20 + })
  21 + Describe("根据参数移除订单增删改查", func() {
  22 + Context("传入有效的orderId", func() {
  23 + It("返回被移除订单实体的数据", func() {
  24 + httpExpect := httpexpect.New(GinkgoT(), server.URL)
  25 + httpExpect.DELETE("/orders/{orderId}").
  26 + Expect().
  27 + Status(http.StatusOK).
  28 + JSON().
  29 + Object().
  30 + ContainsKey("code").ValueEqual("code", 0).
  31 + ContainsKey("msg").ValueEqual("msg", "ok").
  32 + ContainsKey("data").Value("data").Object()
  33 + })
  34 + })
  35 + })
  36 + AfterEach(func() {
  37 + _, err := pG.DB.Exec("DELETE FROM orders WHERE true")
  38 + Expect(err).NotTo(HaveOccurred())
  39 + })
  40 +})
  1 +package order
  2 +
  3 +import (
  4 + "net/http"
  5 +
  6 + "github.com/gavv/httpexpect"
  7 + . "github.com/onsi/ginkgo"
  8 + . "github.com/onsi/gomega"
  9 + pG "gitlab.fjmaimaimai.com/mmm-go-pp/partner01/pkg/infrastructure/pg"
  10 +)
  11 +
  12 +var _ = Describe("更新订单增删改查", func() {
  13 + var orderId int64
  14 + BeforeEach(func() {
  15 + _, err := pG.DB.QueryOne(
  16 + pg.Scan(&orderId),
  17 + "INSERT INTO orders (order_id, buyer, company_id, partner_id, delivery_code, is_disable, order_no, order_detail, order_dividend, order_dividend_status, order_goods, order_source, order_type, order_status, total_price, region_info, remarks, create_at, update_at, delete_at) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?) RETURNING order_id",
  18 + "testOrderId", "testBuyer", "testCompanyId", "testPartnerId", "testDeliveryCode", "testIsDisable", "testOrderNo", "testOrderDetail", "testOrderDividend", "testOrderDividendStatus", "testOrderGoods", "testOrderSource", "testOrderType", "testOrderStatus", "testTotalPrice", "testRegionInfo", "testRemarks", "testCreateAt", "testUpdateAt", "testDeleteAt")
  19 + Expect(err).NotTo(HaveOccurred())
  20 + })
  21 + Describe("提交数据更新订单增删改查", func() {
  22 + Context("提交正确的订单实体数据", func() {
  23 + It("返回更新后的订单实体数据", func() {
  24 + httpExpect := httpexpect.New(GinkgoT(), server.URL)
  25 + body := map[string]interface{}{}
  26 + httpExpect.PUT("/orders/{orderId}").
  27 + WithJSON(body).
  28 + Expect().
  29 + Status(http.StatusOK).
  30 + JSON().
  31 + Object().
  32 + ContainsKey("code").ValueEqual("code", 0).
  33 + ContainsKey("msg").ValueEqual("msg", "ok").
  34 + ContainsKey("data").Value("data").Object().
  35 + ContainsKey("orderId").ValueEqual("orderId", orderId)
  36 + })
  37 + })
  38 + })
  39 + AfterEach(func() {
  40 + _, err := pG.DB.Exec("DELETE FROM orders WHERE true")
  41 + Expect(err).NotTo(HaveOccurred())
  42 + })
  43 +})