| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278 |
- package admin
- import (
- "app/commons/constant"
- "app/commons/model/entity"
- "app/commons/services"
- "github.com/gin-gonic/gin"
- "github.com/shopspring/decimal"
- )
- // CreateNodeOrder 创建节点订单
- func (s *Server) CreateNodeOrder(ctx *gin.Context) {
- c := s.FromContext(ctx)
- type Request struct {
- UserId int64 `json:"userId" binding:"required"`
- Uid string `json:"uid" binding:"required"`
- ProductId int64 `json:"productId" binding:"required"`
- ProductName string `json:"productName"`
- UsdPrice decimal.Decimal `json:"usdPrice"`
- Quantity decimal.Decimal `json:"quantity"`
- UsdAmount decimal.Decimal `json:"usdAmount"`
- PayState int `json:"payState"`
- State int `json:"state"`
- Source int `json:"source"`
- }
- req := new(Request)
- if err := c.ShouldBindJSON(&req); err != nil {
- c.Fail(constant.ErrorParams)
- return
- }
- order := &entity.NodeOrder{
- UserId: req.UserId,
- Uid: req.Uid,
- ProductId: req.ProductId,
- ProductName: req.ProductName,
- UsdPrice: req.UsdPrice,
- Quantity: req.Quantity,
- UsdAmount: req.UsdAmount,
- PayState: req.PayState,
- State: req.State,
- Source: req.Source,
- }
- if err := s.DB().Create(order).Error; err != nil {
- c.Fail(err.Error())
- return
- }
- c.Resp(order)
- }
- // UpdateNodeOrder 更新节点订单
- func (s *Server) UpdateNodeOrder(ctx *gin.Context) {
- c := s.FromContext(ctx)
- type Request struct {
- Id int64 `json:"id" binding:"required"`
- UserId *int64 `json:"userId"`
- Uid *string `json:"uid"`
- ProductId *int64 `json:"productId"`
- ProductName *string `json:"productName"`
- UsdPrice *decimal.Decimal `json:"usdPrice"`
- Quantity *decimal.Decimal `json:"quantity"`
- UsdAmount *decimal.Decimal `json:"usdAmount"`
- PayState *int `json:"payState"`
- State *int `json:"state"`
- Source *int `json:"source"`
- }
- req := new(Request)
- if err := c.ShouldBindJSON(&req); err != nil {
- c.Fail(constant.ErrorParams)
- return
- }
- order := &entity.NodeOrder{}
- if err := s.DB().First(order, req.Id).Error; err != nil {
- c.Fail(constant.ErrorInfo)
- return
- }
- updates := make(map[string]interface{})
- if req.UserId != nil {
- updates["user_id"] = *req.UserId
- }
- if req.Uid != nil {
- updates["uid"] = *req.Uid
- }
- if req.ProductId != nil {
- updates["product_id"] = *req.ProductId
- }
- if req.ProductName != nil {
- updates["product_name"] = *req.ProductName
- }
- if req.UsdPrice != nil {
- updates["usd_price"] = *req.UsdPrice
- }
- if req.Quantity != nil {
- updates["quantity"] = *req.Quantity
- }
- if req.UsdAmount != nil {
- updates["usd_amount"] = *req.UsdAmount
- }
- if req.PayState != nil {
- updates["pay_state"] = *req.PayState
- }
- if req.State != nil {
- updates["state"] = *req.State
- }
- if req.Source != nil {
- updates["source"] = *req.Source
- }
- if err := s.DB().Model(order).Updates(updates).Error; err != nil {
- c.Fail(err.Error())
- return
- }
- c.Resp(order)
- }
- // DeleteNodeOrder 删除节点订单
- func (s *Server) DeleteNodeOrder(ctx *gin.Context) {
- c := s.FromContext(ctx)
- type Request struct {
- Id int64 `json:"id" binding:"required"`
- }
- req := new(Request)
- if err := c.ShouldBindJSON(&req); err != nil {
- c.Fail(constant.ErrorParams)
- return
- }
- if err := s.DB().Delete(&entity.NodeOrder{}, req.Id).Error; err != nil {
- c.Fail(err.Error())
- return
- }
- c.Resp(nil)
- }
- // GetNodeOrder 获取节点订单详情
- func (s *Server) GetNodeOrder(ctx *gin.Context) {
- c := s.FromContext(ctx)
- type Request struct {
- Id int64 `json:"id" form:"id" binding:"required"`
- }
- req := new(Request)
- if err := c.ShouldBind(&req); err != nil {
- c.Fail(constant.ErrorParams)
- return
- }
- order := &entity.NodeOrder{}
- if err := s.DB().First(order, req.Id).Error; err != nil {
- c.Fail(constant.ErrorInfo)
- return
- }
- c.Resp(order)
- }
- // FindNodeOrder 分页查询节点订单
- func (s *Server) FindNodeOrder(ctx *gin.Context) {
- c := s.FromContext(ctx)
- type Request struct {
- services.Pagination
- RecordId string `json:"recordId" form:"recordId"`
- UserId int64 `json:"userId" form:"userId"`
- Uid string `json:"uid" form:"uid"`
- ProductId int64 `json:"productId" form:"productId"`
- ProductName string `json:"productName" form:"productName"`
- PayState *int `json:"payState" form:"payState"`
- State *int `json:"state" form:"state"`
- Source *int `json:"source" form:"source"`
- BeginTime int64 `json:"beginTime" form:"beginTime"`
- EndTime int64 `json:"endTime" form:"endTime"`
- Order string `json:"order" form:"order"`
- IsExport bool `json:"isExport" form:"isExport"`
- Fields string `json:"fields" form:"fields"`
- }
- req := new(Request)
- if err := c.ShouldBind(&req); err != nil {
- c.Fail(constant.ErrorParams)
- return
- }
- db := s.DB().Model(&entity.NodeOrder{})
- // 条件查询
- if req.RecordId != "" {
- db = db.Where("record_id = ?", req.RecordId)
- }
- if req.UserId > 0 {
- db = db.Where("user_id = ?", req.UserId)
- }
- if req.Uid != "" {
- db = db.Where("uid = ?", req.Uid)
- }
- if req.ProductId > 0 {
- db = db.Where("product_id = ?", req.ProductId)
- }
- if req.ProductName != "" {
- db = db.Where("product_name LIKE ?", "%"+req.ProductName+"%")
- }
- if req.PayState != nil {
- db = db.Where("pay_state = ?", *req.PayState)
- }
- if req.State != nil {
- db = db.Where("state = ?", *req.State)
- }
- if req.Source != nil {
- db = db.Where("source = ?", *req.Source)
- }
- if req.BeginTime > 0 {
- db = db.Where("created_at >= ?", req.BeginTime)
- }
- if req.EndTime > 0 {
- db = db.Where("created_at <= ?", req.EndTime)
- }
- // 排序
- if req.Order == "" {
- req.Order = "id desc"
- }
- db = db.Order(req.Order)
- // 分页查询
- list, err := s.PageNodeOrder(db, &req.Pagination)
- if err != nil {
- c.Fail(err.Error())
- return
- }
- // 状态映射
- payStateMap := map[int]string{
- entity.NodeOrderPayStateWaiting: "待支付",
- entity.NodeOrderPayStateSuccess: "支付成功",
- entity.NodeOrderPayStateFail: "支付失败",
- }
- stateMap := map[int]string{
- entity.NodeOrderStateUnknown: "未知",
- entity.NodeOrderStateEffective: "有效",
- entity.NodeOrderStateExpired: "过期失效",
- entity.NodeOrderStateFailInvalid: "主动失效",
- }
- sourceMap := map[int]string{
- entity.NodeOrderSourceFromUser: "用户购买",
- entity.NodeOrderSourceFromAdminBuy: "管理后台购买",
- }
- // 可导出字段列表
- cols := []map[string]string{
- {"field": "id", "label": "ID"},
- {"field": "recordId", "label": "订单ID"},
- {"field": "userId", "label": "用户ID"},
- {"field": "uid", "label": "用户UID"},
- {"field": "productId", "label": "产品ID"},
- {"field": "productName", "label": "产品名称"},
- {"field": "usdPrice", "label": "单价(USD)"},
- {"field": "quantity", "label": "数量"},
- {"field": "usdAmount", "label": "总价值(USD)"},
- {"field": "payState", "label": "支付状态"},
- {"field": "state", "label": "订单状态"},
- {"field": "source", "label": "来源"},
- {"field": "createdAt", "label": "创建时间"},
- }
- c.Resp(gin.H{
- "list": list,
- "pagination": req.Pagination,
- "payStateMap": payStateMap,
- "stateMap": stateMap,
- "sourceMap": sourceMap,
- "cols": cols,
- })
- }
|