order_virtual_asset.go 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172
  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 GetCygxOrderVirtualAssetActivityList(sourceIds []int, mobile string) (items []*CygxOrderVirtualAsset, err error) {
  89. lenArr := len(sourceIds)
  90. if lenArr == 0 {
  91. return
  92. }
  93. o := orm.NewOrm()
  94. sql := `SELECT source_id
  95. FROM
  96. cygx_order_virtual_asset
  97. WHERE 1=1 AND source = 'activity' AND mobile= ? AND source_id IN (` + utils.GetOrmInReplace(lenArr) + `) `
  98. _, err = o.Raw(sql, mobile, sourceIds).QueryRows(&items)
  99. return
  100. }
  101. // 移除用户活动权限记录
  102. func RemoveCygxOrderVirtualAsset(item *CygxOrderVirtualAsset, itemOrder *CygxOrder) (err error) {
  103. o, err := orm.NewOrm().Begin()
  104. if err != nil {
  105. return
  106. }
  107. defer func() {
  108. fmt.Println(err)
  109. if err == nil {
  110. o.Commit()
  111. } else {
  112. o.Rollback()
  113. }
  114. }()
  115. itemOrderAction := new(CygxOrderAction)
  116. itemOrderAction.Action = "退款成功"
  117. itemOrderAction.OrderStatus = 3
  118. itemOrderAction.OrderStatusText = "已退款"
  119. itemOrderAction.OrderCode = item.OrderCode
  120. itemOrderAction.UserId = item.UserId
  121. itemOrderAction.Mobile = item.Mobile
  122. itemOrderAction.Email = item.Email
  123. itemOrderAction.CompanyId = item.CompanyId
  124. itemOrderAction.CompanyName = item.CompanyName
  125. itemOrderAction.RealName = item.RealName
  126. itemOrderAction.SellerName = item.SellerName
  127. itemOrderAction.CreateTime = time.Now()
  128. itemOrderAction.ModifyTime = time.Now()
  129. itemOrderAction.RegisterPlatform = utils.REGISTER_PLATFORM
  130. updateParams := make(map[string]interface{})
  131. updateParams["OrderStatus"] = itemOrder.OrderStatus
  132. updateParams["ModifyTime"] = item.ModifyTime
  133. ptrStructOrTableName := "cygx_order"
  134. whereParam := map[string]interface{}{"order_code": itemOrder.OrderCode}
  135. qs := o.QueryTable(ptrStructOrTableName)
  136. for expr, exprV := range whereParam {
  137. qs = qs.Filter(expr, exprV)
  138. }
  139. _, err = qs.Update(updateParams) // 修改订单状态
  140. if err != nil {
  141. return
  142. }
  143. _, err = o.Insert(itemOrderAction) // 写入订单操作信息
  144. if err != nil {
  145. return
  146. }
  147. sql := ` DELETE FROM cygx_order_virtual_asset WHERE order_code =? ` // 删除原有的持卡信息
  148. _, err = o.Raw(sql, item.OrderCode).Exec()
  149. if err != nil {
  150. return
  151. }
  152. return
  153. }