order_user_card.go 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163
  1. package order
  2. import (
  3. "fmt"
  4. "github.com/beego/beego/v2/client/orm"
  5. "time"
  6. )
  7. // 用户持卡表 CygxOrderUserCard 结构体对应 cygx_order_user_card 数据表
  8. type CygxOrderUserCard struct {
  9. UserCardId int `orm:"column(user_card_id);pk";comment:"订单id"`
  10. OrderCode string `comment:"订单编号"`
  11. UserId int `comment:"用户ID"`
  12. Mobile string `comment:"手机号"`
  13. Email string `comment:"邮箱"`
  14. CompanyId int `comment:"公司ID"`
  15. CompanyName string `comment:"公司名称"`
  16. RealName string `comment:"用户实际名称"`
  17. SellerName string `comment:"所属销售"`
  18. CreateTime time.Time `comment:"创建时间"`
  19. ModifyTime time.Time `comment:"修改时间"`
  20. RegisterPlatform int `comment:"来源"`
  21. CardType string `comment:"会员卡类型"`
  22. StartDate time.Time `comment:"开始时间"`
  23. EndDate time.Time `comment:"结束时间"`
  24. IsSuspend int `comment:"是否暂停"`
  25. }
  26. // 用户持卡日志表 CygxOrderUserCardLog 结构体对应 cygx_order_user_card_log 数据表
  27. type CygxOrderUserCardLog struct {
  28. UserCardId int `orm:"column(user_card_log_id);pk";comment:"订单id"`
  29. OrderCode string `comment:"订单编号"`
  30. UserId int `comment:"用户ID"`
  31. Mobile string `comment:"手机号"`
  32. Email string `comment:"邮箱"`
  33. CompanyId int `comment:"公司ID"`
  34. CompanyName string `comment:"公司名称"`
  35. RealName string `comment:"用户实际名称"`
  36. SellerName string `comment:"所属销售"`
  37. CreateTime time.Time `comment:"创建时间"`
  38. ModifyTime time.Time `comment:"修改时间"`
  39. RegisterPlatform int `comment:"来源"`
  40. CardType string `comment:"会员卡类型"`
  41. StartDate time.Time `comment:"开始时间"`
  42. EndDate time.Time `comment:"结束时间"`
  43. IsSuspend int `comment:"是否暂停"`
  44. }
  45. // 添加
  46. func AddCygxOrderUserCard(item *CygxOrderUserCard, itemOrder *CygxOrder) (err error) {
  47. o, err := orm.NewOrm().Begin()
  48. if err != nil {
  49. return
  50. }
  51. defer func() {
  52. fmt.Println(err)
  53. if err == nil {
  54. o.Commit()
  55. } else {
  56. o.Rollback()
  57. }
  58. }()
  59. itemOrderAction := new(CygxOrderAction)
  60. itemOrderAction.Action = "微信小程序已支付"
  61. itemOrderAction.OrderStatus = 2
  62. itemOrderAction.OrderStatusText = "已支付"
  63. itemOrderAction.OrderCode = item.OrderCode
  64. itemOrderAction.UserId = item.UserId
  65. itemOrderAction.Mobile = item.Mobile
  66. itemOrderAction.Email = item.Email
  67. itemOrderAction.CompanyId = item.CompanyId
  68. itemOrderAction.CompanyName = item.CompanyName
  69. itemOrderAction.RealName = item.RealName
  70. itemOrderAction.SellerName = item.SellerName
  71. itemOrderAction.CreateTime = time.Now()
  72. itemOrderAction.ModifyTime = time.Now()
  73. itemOrderAction.RegisterPlatform = item.RegisterPlatform
  74. updateParams := make(map[string]interface{})
  75. updateParams["PayTime"] = itemOrder.PayTime
  76. updateParams["PayMoney"] = itemOrder.PayMoney
  77. updateParams["OrderStatus"] = itemOrder.OrderStatus
  78. updateParams["OutTradeCode"] = itemOrder.OutTradeCode
  79. updateParams["ModifyTime"] = item.ModifyTime
  80. updateParams["StartDate"] = item.StartDate
  81. updateParams["EndDate"] = item.EndDate
  82. updateParams["PaymentType"] = 1
  83. ptrStructOrTableName := "cygx_order"
  84. whereParam := map[string]interface{}{"order_code": itemOrder.OrderCode}
  85. qs := o.QueryTable(ptrStructOrTableName)
  86. for expr, exprV := range whereParam {
  87. qs = qs.Filter(expr, exprV)
  88. }
  89. _, err = qs.Update(updateParams) // 修改订单状态
  90. if err != nil {
  91. return
  92. }
  93. _, err = o.Insert(itemOrderAction) // 写入订单操作信息
  94. if err != nil {
  95. return
  96. }
  97. sql := ` DELETE FROM cygx_order_user_card WHERE mobile=? ` // 删除原有的持卡信息
  98. _, err = o.Raw(sql, item.Mobile).Exec()
  99. if err != nil {
  100. return
  101. }
  102. itemLog := new(CygxOrderUserCardLog)
  103. itemLog.OrderCode = item.OrderCode
  104. itemLog.UserId = item.UserId
  105. itemLog.Mobile = item.Mobile
  106. itemLog.Email = item.Email
  107. itemLog.CompanyId = item.CompanyId
  108. itemLog.CompanyName = item.CompanyName
  109. itemLog.RealName = item.RealName
  110. itemLog.SellerName = item.SellerName
  111. itemLog.CreateTime = time.Now()
  112. itemLog.ModifyTime = time.Now()
  113. itemLog.RegisterPlatform = item.RegisterPlatform
  114. itemLog.CardType = item.CardType
  115. itemLog.StartDate = item.StartDate
  116. itemLog.EndDate = item.EndDate
  117. itemLog.IsSuspend = item.IsSuspend
  118. _, err = o.Insert(item) //写入用户持卡表
  119. if err != nil {
  120. return
  121. }
  122. _, err = o.Insert(itemLog) // 写入用户持卡日志表
  123. if err != nil {
  124. return
  125. }
  126. return
  127. }
  128. // 根据订单编号获取用户持卡详情
  129. func GetCygxOrderUserCardDetailByOrderCode(orderCode string) (item *CygxOrderUserCard, err error) {
  130. o := orm.NewOrm()
  131. sql := `SELECT * FROM cygx_order_user_card WHERE order_code = ? `
  132. err = o.Raw(sql, orderCode).QueryRow(&item)
  133. return
  134. }
  135. // 根据手机号获取用户持卡详情
  136. func GetCygxOrderUserCardDetailByMobile(mobile string) (item *CygxOrderUserCard, err error) {
  137. o := orm.NewOrm()
  138. sql := `SELECT * FROM cygx_order_user_card WHERE mobile = ? AND end_date >= ? `
  139. err = o.Raw(sql, mobile, time.Now()).QueryRow(&item)
  140. return
  141. }
  142. // 获取数量
  143. func GetCygxOrderUserCardCount(condition string, pars []interface{}) (count int, err error) {
  144. o := orm.NewOrm()
  145. sqlCount := ` SELECT COUNT(1) AS count FROM cygx_order_user_card WHERE 1= 1 ` + condition
  146. err = o.Raw(sqlCount, pars).QueryRow(&count)
  147. return
  148. }