order_user_card.go 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258
  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. switch itemOrder.TradeType {
  61. case "JSAPI":
  62. itemOrderAction.Action = "公众号支付"
  63. case "NATIVE":
  64. itemOrderAction.Action = "扫码支付"
  65. case "App":
  66. itemOrderAction.Action = "App支付"
  67. case "MICROPAY":
  68. itemOrderAction.Action = "付款码支付"
  69. case "MWEB":
  70. itemOrderAction.Action = "H5支付"
  71. case "FACEPAY":
  72. itemOrderAction.Action = "刷脸支付"
  73. }
  74. itemOrderAction.OrderStatus = 2
  75. itemOrderAction.OrderStatusText = "已支付"
  76. itemOrderAction.OrderCode = item.OrderCode
  77. itemOrderAction.UserId = item.UserId
  78. itemOrderAction.Mobile = item.Mobile
  79. itemOrderAction.Email = item.Email
  80. itemOrderAction.CompanyId = item.CompanyId
  81. itemOrderAction.CompanyName = item.CompanyName
  82. itemOrderAction.RealName = item.RealName
  83. itemOrderAction.SellerName = item.SellerName
  84. itemOrderAction.CreateTime = time.Now()
  85. itemOrderAction.ModifyTime = time.Now()
  86. itemOrderAction.RegisterPlatform = item.RegisterPlatform
  87. updateParams := make(map[string]interface{})
  88. updateParams["PayTime"] = itemOrder.PayTime
  89. updateParams["PayMoney"] = itemOrder.PayMoney
  90. updateParams["OrderStatus"] = itemOrder.OrderStatus
  91. updateParams["OutTradeCode"] = itemOrder.OutTradeCode
  92. updateParams["ModifyTime"] = item.ModifyTime
  93. updateParams["StartDate"] = item.StartDate
  94. updateParams["EndDate"] = item.EndDate
  95. updateParams["PaymentType"] = 1
  96. updateParams["TradeType"] = itemOrder.TradeType
  97. ptrStructOrTableName := "cygx_order"
  98. whereParam := map[string]interface{}{"order_code": itemOrder.OrderCode}
  99. qs := o.QueryTable(ptrStructOrTableName)
  100. for expr, exprV := range whereParam {
  101. qs = qs.Filter(expr, exprV)
  102. }
  103. _, err = qs.Update(updateParams) // 修改订单状态
  104. if err != nil {
  105. return
  106. }
  107. _, err = o.Insert(itemOrderAction) // 写入订单操作信息
  108. if err != nil {
  109. return
  110. }
  111. sql := ` DELETE FROM cygx_order_user_card WHERE mobile=? ` // 删除原有的持卡信息
  112. _, err = o.Raw(sql, item.Mobile).Exec()
  113. if err != nil {
  114. return
  115. }
  116. itemLog := new(CygxOrderUserCardLog)
  117. itemLog.OrderCode = item.OrderCode
  118. itemLog.UserId = item.UserId
  119. itemLog.Mobile = item.Mobile
  120. itemLog.Email = item.Email
  121. itemLog.CompanyId = item.CompanyId
  122. itemLog.CompanyName = item.CompanyName
  123. itemLog.RealName = item.RealName
  124. itemLog.SellerName = item.SellerName
  125. itemLog.CreateTime = time.Now()
  126. itemLog.ModifyTime = time.Now()
  127. itemLog.RegisterPlatform = item.RegisterPlatform
  128. itemLog.CardType = item.CardType
  129. itemLog.StartDate = item.StartDate
  130. itemLog.EndDate = item.EndDate
  131. itemLog.IsSuspend = item.IsSuspend
  132. _, err = o.Insert(item) //写入用户持卡表
  133. if err != nil {
  134. return
  135. }
  136. _, err = o.Insert(itemLog) // 写入用户持卡日志表
  137. if err != nil {
  138. return
  139. }
  140. return
  141. }
  142. // 根据订单编号获取用户持卡详情
  143. func GetCygxOrderUserCardDetailByOrderCode(orderCode string) (item *CygxOrderUserCard, err error) {
  144. o := orm.NewOrm()
  145. sql := `SELECT * FROM cygx_order_user_card WHERE order_code = ? `
  146. err = o.Raw(sql, orderCode).QueryRow(&item)
  147. return
  148. }
  149. // 根据手机号获取用户持卡详情
  150. func GetCygxOrderUserCardDetailByMobile(mobile string) (item *CygxOrderUserCard, err error) {
  151. o := orm.NewOrm()
  152. sql := `SELECT * FROM cygx_order_user_card WHERE mobile = ? `
  153. err = o.Raw(sql, mobile).QueryRow(&item)
  154. return
  155. }
  156. // 获取数量
  157. func GetCygxOrderUserCardCount(condition string, pars []interface{}) (count int, err error) {
  158. o := orm.NewOrm()
  159. sqlCount := ` SELECT COUNT(1) AS count FROM cygx_order_user_card WHERE 1= 1 ` + condition
  160. err = o.Raw(sqlCount, pars).QueryRow(&count)
  161. return
  162. }
  163. // 删除
  164. func RemoveCygxOrderUserCard(item *CygxOrderUserCard, itemOrder *CygxOrder) (err error) {
  165. o, err := orm.NewOrm().Begin()
  166. if err != nil {
  167. return
  168. }
  169. defer func() {
  170. fmt.Println(err)
  171. if err == nil {
  172. o.Commit()
  173. } else {
  174. o.Rollback()
  175. }
  176. }()
  177. itemOrderAction := new(CygxOrderAction)
  178. itemOrderAction.Action = "退款成功"
  179. itemOrderAction.OrderStatus = 3
  180. itemOrderAction.OrderStatusText = "已退款"
  181. itemOrderAction.OrderCode = item.OrderCode
  182. itemOrderAction.UserId = item.UserId
  183. itemOrderAction.Mobile = item.Mobile
  184. itemOrderAction.Email = item.Email
  185. itemOrderAction.CompanyId = item.CompanyId
  186. itemOrderAction.CompanyName = item.CompanyName
  187. itemOrderAction.RealName = item.RealName
  188. itemOrderAction.SellerName = item.SellerName
  189. itemOrderAction.CreateTime = time.Now()
  190. itemOrderAction.ModifyTime = time.Now()
  191. itemOrderAction.RegisterPlatform = item.RegisterPlatform
  192. updateParams := make(map[string]interface{})
  193. updateParams["OrderStatus"] = itemOrder.OrderStatus
  194. updateParams["ModifyTime"] = item.ModifyTime
  195. ptrStructOrTableName := "cygx_order"
  196. whereParam := map[string]interface{}{"order_code": itemOrder.OrderCode}
  197. qs := o.QueryTable(ptrStructOrTableName)
  198. for expr, exprV := range whereParam {
  199. qs = qs.Filter(expr, exprV)
  200. }
  201. _, err = qs.Update(updateParams) // 修改订单状态
  202. if err != nil {
  203. return
  204. }
  205. _, err = o.Insert(itemOrderAction) // 写入订单操作信息
  206. if err != nil {
  207. return
  208. }
  209. //sql := ` DELETE FROM cygx_order_user_card WHERE mobile=? ` // 删除原有的持卡信息 (退款之后不删除相关权益权限)
  210. //_, err = o.Raw(sql, item.Mobile).Exec()
  211. //if err != nil {
  212. // return
  213. //}
  214. itemLog := new(CygxOrderUserCardLog)
  215. itemLog.OrderCode = item.OrderCode
  216. itemLog.UserId = item.UserId
  217. itemLog.Mobile = item.Mobile
  218. itemLog.Email = item.Email
  219. itemLog.CompanyId = item.CompanyId
  220. itemLog.CompanyName = item.CompanyName
  221. itemLog.RealName = item.RealName
  222. itemLog.SellerName = item.SellerName
  223. itemLog.CreateTime = time.Now()
  224. itemLog.ModifyTime = time.Now()
  225. itemLog.RegisterPlatform = item.RegisterPlatform
  226. itemLog.CardType = item.CardType
  227. itemLog.StartDate = item.StartDate
  228. itemLog.EndDate = item.EndDate
  229. itemLog.IsSuspend = 1
  230. _, err = o.Insert(itemLog) // 写入用户持卡日志表
  231. if err != nil {
  232. return
  233. }
  234. return
  235. }