From 7a3823a2a754cf853e116f1490138be22e8e9d5d Mon Sep 17 00:00:00 2001 From: wolter Date: Fri, 2 Aug 2024 10:41:57 +0800 Subject: [PATCH] =?UTF-8?q?=E6=9B=B4=E6=96=B0=E8=AE=A2=E5=8D=95=E5=92=8C?= =?UTF-8?q?=E8=AE=A2=E5=8D=95=E6=97=A5=E5=BF=97?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- app/data/order_log.go | 8 +- app/data/orders.go | 20 +++- app/http/controllers/backend/order.go | 51 +++++++++ app/http/entities/backend/order.go | 131 ++++++++++++++++++++++ app/http/requestmapping/backend.go | 3 + app/http/routes/admin.go | 5 + app/models/ordersmodel/orders.go | 8 +- app/models/paychannelmodel/pay_channel.go | 19 ++-- app/services/order.go | 64 +++++++++++ 9 files changed, 290 insertions(+), 19 deletions(-) create mode 100644 app/http/controllers/backend/order.go create mode 100644 app/http/entities/backend/order.go create mode 100644 app/services/order.go diff --git a/app/data/order_log.go b/app/data/order_log.go index 42298d3..36db815 100644 --- a/app/data/order_log.go +++ b/app/data/order_log.go @@ -17,7 +17,7 @@ func NewOrderLogRepo(repo xorm.Interface) *OrderLogRepo { } } -func (m *OrderLogRepo) MerchantList(conn builder.Cond, pageFilter entities.PageRequest, orderLogList *[]orderlogmodel.OrderLogModel) (int64, error) { +func (m *OrderLogRepo) OrderLogList(conn builder.Cond, pageFilter entities.PageRequest, orderLogList *[]orderlogmodel.OrderLog) (int64, error) { repo := m.repo.Where(conn) if pageFilter.Page > 0 { repo = repo.Limit(pageFilter.PageSize, pageFilter.PageSize*(pageFilter.Page-1)) @@ -25,15 +25,15 @@ func (m *OrderLogRepo) MerchantList(conn builder.Cond, pageFilter entities.PageR return repo.Desc("create_time").FindAndCount(orderLogList) } -func (m *OrderLogRepo) MerchantInsertOne(orderLog *orderlogmodel.OrderLogModel) (int64, error) { +func (m *OrderLogRepo) OrderLogInsertOne(orderLog *orderlogmodel.OrderLog) (int64, error) { return m.repo.InsertOne(orderLog) } -func (m *OrderLogRepo) MerchantDelete(orderLog *orderlogmodel.OrderLogModel, conn builder.Cond) (int64, error) { +func (m *OrderLogRepo) OrderLogDelete(orderLog *orderlogmodel.OrderLog, conn builder.Cond) (int64, error) { return m.repo.Where(conn).Delete(orderLog) } // columns 参数为要更新的字段 -func (m *OrderLogRepo) MerchantUpdate(orderLog *orderlogmodel.OrderLogModel, conn builder.Cond, columns ...string) (int64, error) { +func (m *OrderLogRepo) OrderLogUpdate(orderLog *orderlogmodel.OrderLog, conn builder.Cond, columns ...string) (int64, error) { return m.repo.Where(conn).MustCols(columns...).Update(orderLog) } diff --git a/app/data/orders.go b/app/data/orders.go index c04d4ee..c9bd442 100644 --- a/app/data/orders.go +++ b/app/data/orders.go @@ -17,7 +17,7 @@ func NewOrderRepo(repo xorm.Interface) *OrderRepo { } } -func (m *OrderRepo) MerchantList(conn builder.Cond, pageFilter entities.PageRequest, orderList *[]ordersmodel.Orders) (int64, error) { +func (m *OrderRepo) OrderList(conn builder.Cond, pageFilter entities.PageRequest, orderList *[]ordersmodel.Orders) (int64, error) { repo := m.repo.Where(conn) if pageFilter.Page > 0 { repo = repo.Limit(pageFilter.PageSize, pageFilter.PageSize*(pageFilter.Page-1)) @@ -25,15 +25,27 @@ func (m *OrderRepo) MerchantList(conn builder.Cond, pageFilter entities.PageRequ return repo.Desc("create_time").FindAndCount(orderList) } -func (m *OrderRepo) MerchantInsertOne(order *ordersmodel.Orders) (int64, error) { +func (m *OrderRepo) OrderInsertOne(order *ordersmodel.Orders) (int64, error) { return m.repo.InsertOne(order) } -func (m *OrderRepo) MerchantDelete(order *ordersmodel.Orders, conn builder.Cond) (int64, error) { +func (m *OrderRepo) OrderDelete(order *ordersmodel.Orders, conn builder.Cond) (int64, error) { return m.repo.Where(conn).Delete(order) } // columns 参数为要更新的字段 -func (m *OrderRepo) MerchantUpdate(order *ordersmodel.Orders, conn builder.Cond, columns ...string) (int64, error) { +func (m *OrderRepo) OrderUpdate(order *ordersmodel.Orders, conn builder.Cond, columns ...string) (int64, error) { return m.repo.Where(conn).MustCols(columns...).Update(order) } + +func (m *OrderRepo) OrdersBackendList(conn builder.Cond, pageFilter entities.PageRequest, orderList *[]ordersmodel.OrdersBackendList) (int64, error) { + repo := m.repo.Select(`orders.*, merchant.name as merchant_name, app.app_name, pay_channel.pay_name`). + Where(conn) + if pageFilter.Page > 0 { + repo = repo.Limit(pageFilter.PageSize, pageFilter.PageSize*(pageFilter.Page-1)) + } + repo = repo.Join("left", "app", "app.id = orders.app_id"). + Join("left", "merchant", "merchant.id = orders.merchant_id"). + Join("left", "pay_channel", "pay_channel.id = orders.pay_id") + return repo.Desc("create_time").FindAndCount(orderList) +} diff --git a/app/http/controllers/backend/order.go b/app/http/controllers/backend/order.go new file mode 100644 index 0000000..5c577a9 --- /dev/null +++ b/app/http/controllers/backend/order.go @@ -0,0 +1,51 @@ +package backend + +import ( + "PaymentCenter/app/constants/errorcode" + "PaymentCenter/app/http/controllers" + "PaymentCenter/app/http/entities" + "PaymentCenter/app/http/entities/backend" + "PaymentCenter/app/models/orderlogmodel" + "PaymentCenter/app/models/ordersmodel" + "PaymentCenter/app/services" + "github.com/ahmetb/go-linq/v3" + "github.com/gin-gonic/gin" +) + +func OrderList(c *gin.Context) { + req, _ := controllers.GetRequest(c).(*backend.OrderListRequest) + filter, err := req.ValidateRequest() + if err != nil { + controllers.Error(c, errorcode.ParamError, err.Error()) + return + } + orderList, total, code := services.OrderList(filter) + + result := make([]backend.OrdersResponse, 0, len(orderList)) + linq.From(orderList).SelectT(func(in ordersmodel.OrdersBackendList) (out backend.OrdersResponse) { + out.ResponseFromDb(in) + return + }).ToSlice(&result) + data := entities.PageRsp{ + Total: total, + Data: result, + } + controllers.HandCodeRes(c, data, code) +} + +func OrderLogsList(c *gin.Context) { + req, _ := controllers.GetRequest(c).(*backend.OrderLogsListRequest) + req.SetDefault() + orderLogList, total, code := services.OrderLogsList(*req) + + result := make([]backend.OrderLogResponse, 0, len(orderLogList)) + linq.From(orderLogList).SelectT(func(in orderlogmodel.OrderLog) (out backend.OrderLogResponse) { + out.ResponseFromDb(in) + return + }).ToSlice(&result) + data := entities.PageRsp{ + Total: total, + Data: result, + } + controllers.HandCodeRes(c, data, code) +} diff --git a/app/http/entities/backend/order.go b/app/http/entities/backend/order.go new file mode 100644 index 0000000..e154ff8 --- /dev/null +++ b/app/http/entities/backend/order.go @@ -0,0 +1,131 @@ +package backend + +import ( + "PaymentCenter/app/http/entities" + "PaymentCenter/app/models/orderlogmodel" + "PaymentCenter/app/models/ordersmodel" + "PaymentCenter/app/utils" + "github.com/pkg/errors" + + "time" +) + +type OrderListRequest struct { + Id int64 `json:"id" form:"id"` + MerchantId int64 `json:"merchant_id" form:"merchant_id"` + PayId int64 `json:"pay_id" form:"pay_id"` + AppId int64 `json:"app_id" form:"app_id"` + MerchantOrderId string `json:"merchant_order_id" form:"merchant_order_id"` + Status int `json:"status" form:"status"` + OrderType int `json:"order_type" form:"order_type"` + StartTime string `json:"start_time" form:"start_time"` + EndTime string `json:"end_time" form:"end_time"` + entities.PageRequest +} +type OrderList struct { + Id int64 `json:"id"` + MerchantId int64 `json:"merchant_id"` + PayId int64 `json:"pay_id"` + AppId int64 `json:"app_id"` + MerchantOrderId string `json:"merchant_order_id"` + Status int `json:"status"` + OrderType int `json:"order_type"` + StartTime time.Time `json:"start_time"` + EndTime time.Time `json:"end_time"` + entities.PageRequest +} + +func (o *OrderListRequest) ValidateRequest() (r OrderList, err error) { + if o.StartTime != "" { + r.StartTime, err = utils.StrToTimeShanghai(o.StartTime) + if err != nil { + err = errors.Wrap(err, "时间格式错误") + return + } + } + if o.EndTime != "" { + r.EndTime, err = utils.StrToTimeShanghai(o.EndTime) + if err != nil { + err = errors.Wrap(err, "时间格式错误") + return + } + } + + r.Id = o.Id + r.MerchantId = o.MerchantId + r.PayId = o.PayId + r.AppId = o.AppId + r.MerchantOrderId = o.MerchantOrderId + r.Status = o.Status + r.OrderType = o.OrderType + r.PageRequest = o.PageRequest + r.SetDefault() + return +} + +type OrdersResponse struct { + Id int64 `json:"id"` + MerchantId int64 `json:"merchant_id"` + PayId int64 `json:"pay_id"` + AppId int64 `json:"app_id"` + MerchantOrderId string `json:"merchant_order_id"` + Status int `json:"status"` + OrderType int `json:"order_type"` + Amount int `json:"amount"` + IpAddress string `json:"ip_address"` + MerchantRequest string `json:"merchant_request"` + MerchantResponse string `json:"merchant_response"` + OrderResponse string `json:"order_response"` + ExtJson string `json:"ext_json"` + CreateTime string `json:"create_time"` + UpdateTime string `json:"update_time"` + MerchantName string `json:"merchant_name"` + PayName string `json:"pay_name"` + AppName string `json:"app_name"` +} + +func (o *OrdersResponse) ResponseFromDb(db ordersmodel.OrdersBackendList) { + o.Id = db.Id + o.MerchantId = db.MerchantId + o.PayId = db.PayId + o.AppId = db.AppId + o.MerchantOrderId = db.MerchantOrderId + o.Status = db.Status + o.OrderType = db.OrderType + o.Amount = db.Amount + o.IpAddress = db.IpAddress + o.MerchantRequest = db.MerchantRequest + o.MerchantResponse = db.MerchantResponse + o.OrderResponse = db.OrderResponse + o.ExtJson = db.ExtJson + o.CreateTime = db.CreateTime.Format("2006-01-02 15:04:05") + o.UpdateTime = db.UpdateTime.Format("2006-01-02 15:04:05") + o.MerchantName = db.MerchantName + o.PayName = db.PayName + o.AppName = db.AppName +} + +type OrderLogsListRequest struct { + OrderId int64 `json:"order_id" form:"order_id"` + entities.PageRequest +} + +type OrderLogResponse struct { + Id int64 `json:"id"` + OrderId int64 `json:"order_id"` + PayCallback string `json:"pay_callback"` + Status int `json:"status"` + MerchantParam string `json:"merchant_param"` + MerchantCallback string `json:"merchant_callback"` + CreateTime string `json:"create_time"` +} + +func (o *OrderLogResponse) ResponseFromDb(db orderlogmodel.OrderLog) { + o.Id = db.Id + o.OrderId = db.OrderId + o.PayCallback = db.PayCallback + o.Status = db.Status + o.MerchantParam = db.MerchantParam + o.MerchantCallback = db.MerchantCallback + o.CreateTime = db.CreateTime.Format("2006-01-02 15:04:05") +} diff --git a/app/http/requestmapping/backend.go b/app/http/requestmapping/backend.go index d5edcf7..5d76b3f 100644 --- a/app/http/requestmapping/backend.go +++ b/app/http/requestmapping/backend.go @@ -24,4 +24,7 @@ var BackendRequestMap = map[string]func() interface{}{ common.ADMIN_V1 + "/app/list": func() interface{} { return new(backend.AppListRequest) }, common.ADMIN_V1 + "/app/delete": func() interface{} { return new(entities.IdRequest) }, common.ADMIN_V1 + "/app/decrypt": func() interface{} { return new(backend.GenerateDecryptKeyRequest) }, + // 订单 + common.ADMIN_V1 + "/order/list": func() interface{} { return new(backend.OrderListRequest) }, + common.ADMIN_V1 + "/order/log/list": func() interface{} { return new(backend.OrderLogsListRequest) }, } diff --git a/app/http/routes/admin.go b/app/http/routes/admin.go index f9461d9..7b531f8 100644 --- a/app/http/routes/admin.go +++ b/app/http/routes/admin.go @@ -46,6 +46,11 @@ func RegisterAdminRoute(router *gin.Engine) { app.DELETE("/delete", backend.AppDelete) // 应用删除 app.GET("/decrypt", backend.GenerateDecrypt) // 生成密钥对 + // 订单 + order := v1.Group("/order") + order.GET("/list", backend.OrderList) // 订单列表 + order.GET("/log/list", backend.OrderLogsList) // 订单日志列表 + } } diff --git a/app/models/ordersmodel/orders.go b/app/models/ordersmodel/orders.go index 48e9aa3..2d8b1c2 100644 --- a/app/models/ordersmodel/orders.go +++ b/app/models/ordersmodel/orders.go @@ -17,7 +17,7 @@ type Orders struct { MerchantId int64 `xorm:"'merchant_id' bigint(20)"` PayId int64 `xorm:"'pay_id' bigint(20)"` AppId int64 `xorm:"'app_id' bigint(20)"` - MerchantOrderId string `xorm:"'merchant_order_id' varchar(128)"` + MerchantOrderId string `xorm:"'merchant_order_id' varchar(32)"` Status int `xorm:"'status' int(11)"` OrderType int `xorm:"'order_type' int(11)"` Amount int `xorm:"'amount' int(11)"` @@ -30,6 +30,12 @@ type Orders struct { UpdateTime time.Time `xorm:"'update_time' timestamp updated"` DeleteTime time.Time `xorm:"'delete_time' timestamp deleted"` } +type OrdersBackendList struct { + Orders `xorm:"extends"` + MerchantName string `xorm:"'merchant_name' varchar(128)"` + PayName string `xorm:"'pay_name' varchar(128)"` + AppName string `xorm:"'app_name' varchar(128)"` +} // 表名 func (m *Orders) TableName() string { diff --git a/app/models/paychannelmodel/pay_channel.go b/app/models/paychannelmodel/pay_channel.go index cede2df..7b65f10 100644 --- a/app/models/paychannelmodel/pay_channel.go +++ b/app/models/paychannelmodel/pay_channel.go @@ -14,16 +14,15 @@ var ( // 实体 type PayChannel struct { Id int64 - PayName string `xorm:"'pay_name' varchar(128)"` - MerchantId int64 `xorm:"'merchant_id' bigint(20)"` - ChannelType int `xorm:"'channel_type' int(11)"` - - AppId string `xorm:"'app_id' varchar(255)"` - ExtJson string `xorm:"'ext_json' JSON"` - ExpireTime time.Time `xorm:"'expire_time' datetime"` - CreateTime time.Time `xorm:"'create_time' datetime created"` - UpdateTime time.Time `xorm:"'update_time' timestamp updated"` - DeleteTime time.Time `xorm:"'delete_time' timestamp deleted"` + PayName string `xorm:"'pay_name' varchar(128)"` + MerchantId int64 `xorm:"'merchant_id' bigint(20)"` + ChannelType int `xorm:"'channel_type' int(11)"` + AppId string `xorm:"'app_id' varchar(255)"` + ExtJson string `xorm:"'ext_json' JSON"` + ExpireTime time.Time `xorm:"'expire_time' datetime"` + CreateTime time.Time `xorm:"'create_time' datetime created"` + UpdateTime time.Time `xorm:"'update_time' timestamp updated"` + DeleteTime time.Time `xorm:"'delete_time' timestamp deleted"` } // 表名 diff --git a/app/services/order.go b/app/services/order.go new file mode 100644 index 0000000..bfc6d8a --- /dev/null +++ b/app/services/order.go @@ -0,0 +1,64 @@ +package services + +import ( + "PaymentCenter/app/data" + "PaymentCenter/app/http/entities/backend" + "PaymentCenter/app/models/orderlogmodel" + "PaymentCenter/app/models/ordersmodel" + "PaymentCenter/app/models/paychannelmodel" + "xorm.io/builder" +) + +// 订单列表,后台查询 +func OrderList(req backend.OrderList) (result []ordersmodel.OrdersBackendList, total int64, code int) { + repo := data.NewOrderRepo(paychannelmodel.GetInstance().GetDb()) + // 拼接查询条件 + conn := builder.NewCond() + if req.Id > 0 { + conn = conn.And(builder.Eq{"id": req.Id}) + } + if req.MerchantId > 0 { + conn = conn.And(builder.Eq{"merchant_id": req.MerchantId}) + } + if req.PayId > 0 { + conn = conn.And(builder.Eq{"pay_id": req.PayId}) + } + if req.AppId > 0 { + conn = conn.And(builder.Eq{"app_id": req.AppId}) + } + if req.MerchantOrderId != "" { + conn = conn.And(builder.Like{"merchant_order_id", req.MerchantOrderId}) + } + if req.Status > 0 { + conn = conn.And(builder.Eq{"status": req.Status}) + } + if req.OrderType > 0 { + conn = conn.And(builder.Eq{"order_type": req.OrderType}) + } + if !req.StartTime.IsZero() { + conn = conn.And(builder.Gte{"start_time": req.StartTime}) + } + if !req.EndTime.IsZero() { + conn = conn.And(builder.Lte{"end_time": req.EndTime}) + } + + // 调用repo + orderList := make([]ordersmodel.OrdersBackendList, 0) + count, err := repo.OrdersBackendList(conn, req.PageRequest, &orderList) + code = handErr(err) + return orderList, count, code +} + +func OrderLogsList(req backend.OrderLogsListRequest) (result []orderlogmodel.OrderLog, total int64, code int) { + repo := data.NewOrderLogRepo(paychannelmodel.GetInstance().GetDb()) + conn := builder.NewCond() + if req.OrderId > 0 { + conn = conn.And(builder.Eq{"order_id": req.OrderId}) + } + // 调用repo + orderLogList := make([]orderlogmodel.OrderLog, 0) + count, err := repo.OrderLogList(conn, req.PageRequest, &orderLogList) + code = handErr(err) + + return orderLogList, count, code +}