order_virtual_asset.go 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180
  1. package order
  2. //用户虚拟资产表
  3. import (
  4. "fmt"
  5. "github.com/beego/beego/v2/client/orm"
  6. "hongze/hongze_web_mfyx/utils"
  7. "time"
  8. )
  9. type CygxOrderVirtualAsset struct {
  10. OrderVirtualAssetsId int `orm:"column(order_virtual_assets_id);pk";comment:"订单支付ID"`
  11. OrderCode string `comment:"订单编号"`
  12. UserId int `comment:"用户ID"`
  13. Mobile string `comment:"手机号"`
  14. Email string `comment:"邮箱"`
  15. CompanyId int `comment:"公司ID"`
  16. CompanyName string `comment:"公司名称"`
  17. RealName string `comment:"用户实际名称"`
  18. SellerName string `comment:"所属销售"`
  19. CreateTime time.Time `comment:"创建时间"`
  20. ModifyTime time.Time `comment:"修改时间"`
  21. SourceId int `comment:"来源ID"`
  22. Source string `comment:"来源\n报告 :article\n活动 :activity"`
  23. }
  24. // 添加
  25. func AddCygxOrderVirtualAsset(item *CygxOrderVirtualAsset, itemOrder *CygxOrder) (err error) {
  26. o, err := orm.NewOrm().Begin()
  27. if err != nil {
  28. return
  29. }
  30. defer func() {
  31. fmt.Println(err)
  32. if err == nil {
  33. o.Commit()
  34. } else {
  35. o.Rollback()
  36. }
  37. }()
  38. itemOrderAction := new(CygxOrderAction)
  39. itemOrderAction.Action = "微信小程序已支付"
  40. itemOrderAction.OrderStatus = 2
  41. itemOrderAction.OrderStatusText = "已支付"
  42. itemOrderAction.OrderCode = item.OrderCode
  43. itemOrderAction.UserId = item.UserId
  44. itemOrderAction.Mobile = item.Mobile
  45. itemOrderAction.Email = item.Email
  46. itemOrderAction.CompanyId = item.CompanyId
  47. itemOrderAction.CompanyName = item.CompanyName
  48. itemOrderAction.RealName = item.RealName
  49. itemOrderAction.SellerName = item.SellerName
  50. itemOrderAction.CreateTime = time.Now()
  51. itemOrderAction.ModifyTime = time.Now()
  52. itemOrderAction.RegisterPlatform = utils.REGISTER_PLATFORM
  53. updateParams := make(map[string]interface{})
  54. updateParams["PayTime"] = itemOrder.PayTime
  55. updateParams["PayMoney"] = itemOrder.PayMoney
  56. updateParams["OrderStatus"] = itemOrder.OrderStatus
  57. updateParams["OutTradeCode"] = itemOrder.OutTradeCode
  58. updateParams["ModifyTime"] = item.ModifyTime
  59. updateParams["PaymentType"] = 1
  60. ptrStructOrTableName := "cygx_order"
  61. whereParam := map[string]interface{}{"order_code": itemOrder.OrderCode}
  62. qs := o.QueryTable(ptrStructOrTableName)
  63. for expr, exprV := range whereParam {
  64. qs = qs.Filter(expr, exprV)
  65. }
  66. _, err = qs.Update(updateParams) // 修改订单状态
  67. if err != nil {
  68. return
  69. }
  70. _, err = o.Insert(itemOrderAction) // 写入订单操作信息
  71. if err != nil {
  72. return
  73. }
  74. _, err = o.Insert(item) //写入用户用户虚拟资产表
  75. if err != nil {
  76. return
  77. }
  78. return
  79. }
  80. // 获取数量
  81. func GetCygxOrderVirtualAssetdCount(condition string, pars []interface{}) (count int, err error) {
  82. o := orm.NewOrm()
  83. sqlCount := ` SELECT COUNT(1) AS count FROM cygx_order_virtual_asset WHERE 1= 1 ` + condition
  84. err = o.Raw(sqlCount, pars).QueryRow(&count)
  85. return
  86. }
  87. // 获取数量
  88. func GetCygxOrderVirtualAssetdCountByVivo(sourceId, userId int) (count int, err error) {
  89. o := orm.NewOrm()
  90. sqlCount := ` SELECT COUNT(1) AS count FROM cygx_order_virtual_asset WHERE source IN ('activityvideo','activityvoice') AND source_id = ? AND user_id = ? `
  91. err = o.Raw(sqlCount, sourceId, userId).QueryRow(&count)
  92. return
  93. }
  94. // 获取购买的单场活动
  95. func GetCygxOrderVirtualAssetActivityList(sourceIds []int, mobile string) (items []*CygxOrderVirtualAsset, err error) {
  96. lenArr := len(sourceIds)
  97. if lenArr == 0 {
  98. return
  99. }
  100. o := orm.NewOrm()
  101. sql := `SELECT source_id
  102. FROM
  103. cygx_order_virtual_asset
  104. WHERE 1=1 AND source = 'activity' AND mobile= ? AND source_id IN (` + utils.GetOrmInReplace(lenArr) + `) `
  105. _, err = o.Raw(sql, mobile, sourceIds).QueryRows(&items)
  106. return
  107. }
  108. // 移除用户活动权限记录
  109. func RemoveCygxOrderVirtualAsset(item *CygxOrderVirtualAsset, itemOrder *CygxOrder) (err error) {
  110. o, err := orm.NewOrm().Begin()
  111. if err != nil {
  112. return
  113. }
  114. defer func() {
  115. fmt.Println(err)
  116. if err == nil {
  117. o.Commit()
  118. } else {
  119. o.Rollback()
  120. }
  121. }()
  122. itemOrderAction := new(CygxOrderAction)
  123. itemOrderAction.Action = "退款成功"
  124. itemOrderAction.OrderStatus = 3
  125. itemOrderAction.OrderStatusText = "已退款"
  126. itemOrderAction.OrderCode = item.OrderCode
  127. itemOrderAction.UserId = item.UserId
  128. itemOrderAction.Mobile = item.Mobile
  129. itemOrderAction.Email = item.Email
  130. itemOrderAction.CompanyId = item.CompanyId
  131. itemOrderAction.CompanyName = item.CompanyName
  132. itemOrderAction.RealName = item.RealName
  133. itemOrderAction.SellerName = item.SellerName
  134. itemOrderAction.CreateTime = time.Now()
  135. itemOrderAction.ModifyTime = time.Now()
  136. itemOrderAction.RegisterPlatform = utils.REGISTER_PLATFORM
  137. updateParams := make(map[string]interface{})
  138. updateParams["OrderStatus"] = itemOrder.OrderStatus
  139. updateParams["ModifyTime"] = item.ModifyTime
  140. ptrStructOrTableName := "cygx_order"
  141. whereParam := map[string]interface{}{"order_code": itemOrder.OrderCode}
  142. qs := o.QueryTable(ptrStructOrTableName)
  143. for expr, exprV := range whereParam {
  144. qs = qs.Filter(expr, exprV)
  145. }
  146. _, err = qs.Update(updateParams) // 修改订单状态
  147. if err != nil {
  148. return
  149. }
  150. _, err = o.Insert(itemOrderAction) // 写入订单操作信息
  151. if err != nil {
  152. return
  153. }
  154. sql := ` DELETE FROM cygx_order_virtual_asset WHERE order_code =? ` // 删除原有的持卡信息
  155. _, err = o.Raw(sql, item.OrderCode).Exec()
  156. if err != nil {
  157. return
  158. }
  159. return
  160. }