order.go 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188
  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. func GetCygxOrderDetailList(condition string, pars []interface{}) (items []*CygxOrder, err error) {
  108. o := orm.NewOrm()
  109. sql := `SELECT *
  110. FROM
  111. cygx_order
  112. WHERE 1 = 1 ` + condition
  113. _, err = o.Raw(sql, pars).QueryRows(&items)
  114. return
  115. }
  116. // 自动取消订单
  117. func CancelCygxOrder(item *CygxOrder) (err error) {
  118. o, err := orm.NewOrm().Begin()
  119. if err != nil {
  120. return
  121. }
  122. defer func() {
  123. fmt.Println(err)
  124. if err == nil {
  125. o.Commit()
  126. } else {
  127. o.Rollback()
  128. }
  129. }()
  130. itemOrderAction := new(CygxOrderAction)
  131. itemOrderAction.Action = "超时自动取消订单"
  132. itemOrderAction.OrderStatus = 0
  133. itemOrderAction.OrderStatusText = "已取消"
  134. itemOrderAction.OrderCode = item.OrderCode
  135. itemOrderAction.UserId = item.UserId
  136. itemOrderAction.Mobile = item.Mobile
  137. itemOrderAction.Email = item.Email
  138. itemOrderAction.CompanyId = item.CompanyId
  139. itemOrderAction.CompanyName = item.CompanyName
  140. itemOrderAction.RealName = item.RealName
  141. itemOrderAction.SellerName = item.SellerName
  142. itemOrderAction.CreateTime = time.Now()
  143. itemOrderAction.ModifyTime = time.Now()
  144. itemOrderAction.RegisterPlatform = item.RegisterPlatform
  145. _, err = o.Insert(itemOrderAction) // 写入订单操作信息
  146. if err != nil {
  147. return
  148. }
  149. updateParams := make(map[string]interface{})
  150. updateParams["OrderStatus"] = 0
  151. updateParams["ModifyTime"] = item.ModifyTime
  152. ptrStructOrTableName := "cygx_order"
  153. whereParam := map[string]interface{}{"order_code": item.OrderCode}
  154. qs := o.QueryTable(ptrStructOrTableName)
  155. for expr, exprV := range whereParam {
  156. qs = qs.Filter(expr, exprV)
  157. }
  158. _, err = qs.Update(updateParams)
  159. if err != nil {
  160. return
  161. }
  162. return
  163. }
  164. // 根据订单编号获取订单详情
  165. func GetCygxOrderDetailByOrderCode(orderCode string) (item *CygxOrder, err error) {
  166. o := orm.NewOrm()
  167. sql := `SELECT * FROM cygx_order WHERE order_code = ? `
  168. err = o.Raw(sql, orderCode).QueryRow(&item)
  169. return
  170. }
  171. // 根据订单编号获取订单详情
  172. func GetCygxOrderDetailByOutTradeNo(outTradeNo string) (item *CygxOrder, err error) {
  173. o := orm.NewOrm()
  174. sql := `SELECT * FROM cygx_order WHERE out_trade_no = ? `
  175. err = o.Raw(sql, outTradeNo).QueryRow(&item)
  176. return
  177. }