order_user_card.go 8.1 KB

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