order.go 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178
  1. package order
  2. import (
  3. "fmt"
  4. "github.com/beego/beego/v2/client/orm"
  5. "time"
  6. )
  7. type CygxOrderResp struct {
  8. OrderId int `orm:"column(order_id);pk";comment:"订单id"`
  9. OrderCode string `comment:"订单编号"`
  10. OutTradeCode string `comment:"外部交易号"`
  11. PaymentType int `comment:"支付类型。取值范围:1微信支付,2支付宝支付。"`
  12. GoodsName string `comment:"商品名称"`
  13. GoodsId int `comment:"商品ID"`
  14. BuyerInvoice string `comment:"买家发票信息"`
  15. GoodsMoney float64 `comment:"商品总价"`
  16. OrderMoney float64 `comment:"订单总价"`
  17. Point int `comment:"订单消耗积分"`
  18. PointMoney float64 `comment:"订单消耗积分抵多少钱"`
  19. PayMoney float64 `comment:"订单实付金额"`
  20. RefundMoney float64 `comment:"订单退款金额"`
  21. OrderStatus int `comment:"订单状态,0:已取消、1:待支付、2:已支付、3:已退款"`
  22. PayTime time.Time `comment:"订单付款时间"`
  23. SourceId int `comment:"来源ID"`
  24. Source string `comment:"来源\n报告 :article\n活动 :activity"`
  25. SourceTitle string `comment:"来源名称,活动或者报告标题"`
  26. UserId int `comment:"用户ID"`
  27. Mobile string `comment:"手机号"`
  28. Email string `comment:"邮箱"`
  29. CompanyId int `comment:"公司ID"`
  30. CompanyName string `comment:"公司名称"`
  31. RealName string `comment:"用户实际名称"`
  32. SellerName string `comment:"所属销售"`
  33. CreateTime time.Time `comment:"创建时间"`
  34. ModifyTime time.Time `comment:"修改时间"`
  35. RegisterPlatform int `comment:"来源 1小程序,2:网页"`
  36. StartDate time.Time `comment:"开始时间"`
  37. EndDate time.Time `comment:"结束时间"`
  38. }
  39. // 订单表
  40. type CygxOrder struct {
  41. OrderId int `orm:"column(order_id);pk";comment:"订单id"`
  42. OrderCode string `comment:"订单编号"`
  43. OutTradeNo string `comment:"【商户订单号】 商户系统内部订单号"`
  44. OutTradeCode string `comment:"外部交易号"`
  45. PaymentType int `comment:"支付类型。取值范围:1微信支付,2支付宝支付。"`
  46. TradeType string `comment:"交易类型,枚举值:JSAPI:公众号支付 、 NATIVE:扫码支付 、 App:App支付 、 MICROPAY:付款码支付 、 MWEB:H5支付 、 FACEPAY:刷脸支付"`
  47. GoodsName string `comment:"商品名称"`
  48. GoodsId int `comment:"商品ID"`
  49. BuyerInvoice string `comment:"买家发票信息"`
  50. GoodsMoney float64 `comment:"商品总价"`
  51. OrderMoney float64 `comment:"订单总价"`
  52. Point int `comment:"订单消耗积分"`
  53. PointMoney float64 `comment:"订单消耗积分抵多少钱"`
  54. PayMoney float64 `comment:"订单实付金额"`
  55. RefundTime time.Time `comment:"订单退款时间"`
  56. RefundMoney float64 `comment:"订单退款金额"`
  57. OrderStatus int `comment:"订单状态,0:已取消、1:待支付、2:已支付、3:已退款"`
  58. PayTime time.Time `comment:"订单付款时间"`
  59. SourceId int `comment:"来源ID"`
  60. Source string `comment:"来源\n报告 :article\n活动 :activity"`
  61. SourceTitle string `comment:"来源名称,活动或者报告标题"`
  62. UserId int `comment:"用户ID"`
  63. Mobile string `comment:"手机号"`
  64. Email string `comment:"邮箱"`
  65. CompanyId int `comment:"公司ID"`
  66. CompanyName string `comment:"公司名称"`
  67. RealName string `comment:"用户实际名称"`
  68. SellerName string `comment:"所属销售"`
  69. SellerId int `comment:"所属销售Id"`
  70. CreateTime time.Time `comment:"创建时间"`
  71. ModifyTime time.Time `comment:"修改时间"`
  72. RegisterPlatform int `comment:"来源 1小程序,2:网页"`
  73. OrderType int `comment:"订单类型,1:畅读卡订单,2:单场付费订单"`
  74. StartDate time.Time `comment:"开始时间"`
  75. EndDate time.Time `comment:"结束时间"`
  76. }
  77. // 订单操作表
  78. type CygxOrderAction struct {
  79. ActionId int64 `orm:"column(action_id);pk"` // 动作id
  80. Action string // 动作内容
  81. OrderStatus int // 订单状态,0:已取消、1:待支付、2:已支付、3:已退款
  82. OrderStatusText string // 订单状态名称
  83. OrderCode string // 订单编号
  84. UserId int // 用户ID
  85. Mobile string // 手机号
  86. Email string // 邮箱
  87. CompanyId int // 公司ID
  88. CompanyName string // 公司名称
  89. RealName string // 用户实际名称
  90. SellerName string // 所属销售
  91. CreateTime time.Time // 创建时间
  92. ModifyTime time.Time // 修改时间
  93. RegisterPlatform int // 来源 1小程序,2:网页
  94. AdminId int // 管理员ID
  95. AdminName string // 管理员姓名
  96. }
  97. func GetCygxOrderList(condition string, pars []interface{}, startSize, pageSize int) (items []*CygxOrder, err error) {
  98. o := orm.NewOrm()
  99. sql := `SELECT *
  100. FROM
  101. cygx_order
  102. WHERE 1 = 1 ` + condition
  103. sql += ` LIMIT ?,? `
  104. _, err = o.Raw(sql, pars, startSize, pageSize).QueryRows(&items)
  105. return
  106. }
  107. // 自动取消订单
  108. func CancelCygxOrder(item *CygxOrder) (err error) {
  109. o, err := orm.NewOrm().Begin()
  110. if err != nil {
  111. return
  112. }
  113. defer func() {
  114. fmt.Println(err)
  115. if err == nil {
  116. o.Commit()
  117. } else {
  118. o.Rollback()
  119. }
  120. }()
  121. itemOrderAction := new(CygxOrderAction)
  122. itemOrderAction.Action = "超时自动取消订单"
  123. itemOrderAction.OrderStatus = 0
  124. itemOrderAction.OrderStatusText = "已取消"
  125. itemOrderAction.OrderCode = item.OrderCode
  126. itemOrderAction.UserId = item.UserId
  127. itemOrderAction.Mobile = item.Mobile
  128. itemOrderAction.Email = item.Email
  129. itemOrderAction.CompanyId = item.CompanyId
  130. itemOrderAction.CompanyName = item.CompanyName
  131. itemOrderAction.RealName = item.RealName
  132. itemOrderAction.SellerName = item.SellerName
  133. itemOrderAction.CreateTime = time.Now()
  134. itemOrderAction.ModifyTime = time.Now()
  135. itemOrderAction.RegisterPlatform = item.RegisterPlatform
  136. _, err = o.Insert(itemOrderAction) // 写入订单操作信息
  137. if err != nil {
  138. return
  139. }
  140. updateParams := make(map[string]interface{})
  141. updateParams["OrderStatus"] = 0
  142. updateParams["ModifyTime"] = item.ModifyTime
  143. ptrStructOrTableName := "cygx_order"
  144. whereParam := map[string]interface{}{"order_code": item.OrderCode}
  145. qs := o.QueryTable(ptrStructOrTableName)
  146. for expr, exprV := range whereParam {
  147. qs = qs.Filter(expr, exprV)
  148. }
  149. _, err = qs.Update(updateParams)
  150. if err != nil {
  151. return
  152. }
  153. return
  154. }
  155. // 根据订单编号获取订单详情
  156. func GetCygxOrderDetailByOrderCode(orderCode string) (item *CygxOrder, err error) {
  157. o := orm.NewOrm()
  158. sql := `SELECT * FROM cygx_order WHERE order_code = ? `
  159. err = o.Raw(sql, orderCode).QueryRow(&item)
  160. return
  161. }
  162. // 根据订单编号获取订单详情
  163. func GetCygxOrderDetailByOutTradeNo(outTradeNo string) (item *CygxOrder, err error) {
  164. o := orm.NewOrm()
  165. sql := `SELECT * FROM cygx_order WHERE out_trade_no = ? `
  166. err = o.Raw(sql, outTradeNo).QueryRow(&item)
  167. return
  168. }