面包店
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 

146 lines
4.2 KiB

  1. package db
  2. import (
  3. "applet/app/db/model"
  4. "applet/app/utils/logx"
  5. "time"
  6. "xorm.io/xorm"
  7. )
  8. // 获取对应等级未过期的付费订单
  9. func UserLevelOrderListByUIDByLevelTypeByExpire(Db *xorm.Engine, uid, levelID interface{}) ([]*model.UserLevelOrd, error) {
  10. var m []*model.UserLevelOrd
  11. err := Db.Where("uid = ? AND level_id = ? AND state = ? ", uid, levelID, 1).Desc("expire_at").Find(&m)
  12. if err != nil {
  13. return nil, err
  14. }
  15. return m, nil
  16. }
  17. // OrderListByPvdAndNoSettledWithPage is 查询未结算订单
  18. func UserLevelOrderListByUIDByNoSettledWithPage(Db *xorm.Engine, page int) ([]*model.UserLevelOrd, error) {
  19. perPage := 100
  20. startPlace := (page - 1) * perPage
  21. var o []*model.UserLevelOrd
  22. err := Db.Where("settle_at=?", 0).In("state", 1).Limit(perPage, startPlace).Find(&o)
  23. if err != nil {
  24. return nil, err
  25. }
  26. return o, nil
  27. }
  28. // 获取一个订单
  29. func UserLevelOrderById(Db *xorm.Engine, id string) (*model.UserLevelOrd, error) {
  30. var m *model.UserLevelOrd
  31. has, err := Db.Where("id = ? ", id).Get(&m)
  32. if err != nil || has == false {
  33. return nil, err
  34. }
  35. return m, nil
  36. }
  37. //UserLevelOrderListByUIDByLevelByDateTypeByExpire is 获取某用户某等级的订单,按照过期时间降序排序
  38. func UserLevelOrderListByUIDByLevelByDateTypeByExpire(Db *xorm.Engine, uid, levelID, t interface{}) ([]*model.UserLevelOrd, error) {
  39. var m []*model.UserLevelOrd
  40. err := Db.Where("uid = ? AND level_id = ? AND date_type = ? AND state = ? ", uid, levelID, t, 1).Desc("expire_at").Find(&m)
  41. if err != nil {
  42. return nil, err
  43. }
  44. return m, nil
  45. }
  46. //UserLevelOrderOneByUIDByLevelByDateTypeByExpire is 获取某用户某等级的订单,按照过期时间降序排序
  47. func UserLevelOrderOneByUIDByLevelByDateTypeByExpire(Db *xorm.Engine, uid, levelID interface{}) ([]*model.UserLevelOrd, error) {
  48. var m []*model.UserLevelOrd
  49. err := Db.Where("uid = ? AND level_id = ? and state = 1", uid, levelID).Desc("expire_at").Find(&m)
  50. if err != nil {
  51. return nil, err
  52. }
  53. return m, nil
  54. }
  55. //UserLevelOrdInsertOne is 插入一个 等级付费升级的订单
  56. func UserLevelOrdInsertOne(Db *xorm.Engine, order *model.UserLevelOrd) (int64, error) {
  57. affect, err := Db.InsertOne(order)
  58. if err != nil {
  59. return 0, err
  60. }
  61. return affect, nil
  62. }
  63. //UserLevelOrdUpdateOne is 更新付费升级订单
  64. func UserLevelOrdUpdateOne(Db *xorm.Engine, order *model.UserLevelOrd) (int64, error) {
  65. affect, err := Db.ID(order.Id).Update(order)
  66. if err != nil {
  67. return 0, err
  68. }
  69. return affect, nil
  70. }
  71. func UserLevelOrdUpdateOneWithSession(Db *xorm.Session, order *model.UserLevelOrd) (int64, error) {
  72. affect, err := Db.ID(order.Id).Update(order)
  73. if err != nil {
  74. return 0, err
  75. }
  76. return affect, nil
  77. }
  78. func UserLevelOrdFindOneByOid(Db *xorm.Engine, id int64) (*model.UserLevelOrd, error) {
  79. var m model.UserLevelOrd
  80. has, err := Db.Where("id=? AND state=?", id, 1).Get(&m)
  81. if err != nil || !has {
  82. return nil, err
  83. }
  84. return &m, nil
  85. }
  86. func UserLevelOrdFindByUidStateLevelAndNotExpire(Db *xorm.Engine, uid int, state int, level string) (*model.UserLevelOrd, error) {
  87. var m model.UserLevelOrd
  88. //过期时间还没到 或者是永久的订单
  89. if has, err := Db.Where("uid = ? AND level_id = ? AND state = ? AND (expire_at>= ? OR date_type=4)", uid, level, state, time.Now().Unix()).Get(&m); !has || err != nil {
  90. return nil, logx.Warn(err)
  91. }
  92. return &m, nil
  93. }
  94. // 查询订单状态
  95. func UserLevelOrdFindByID(Db *xorm.Engine, id int) (*model.UserLevelOrd, error) {
  96. var m model.UserLevelOrd
  97. if has, err := Db.Where("id = ?", id).Get(&m); !has || err != nil {
  98. return nil, logx.Warn(err)
  99. }
  100. return &m, nil
  101. }
  102. // 获取对应id订单
  103. func UserLevelOrdFindByIDs(Db *xorm.Engine, ids []int) ([]*model.UserLevelOrd, error) {
  104. var m []*model.UserLevelOrd
  105. err := Db.In("id", ids).Find(&m)
  106. if err != nil {
  107. return nil, err
  108. }
  109. return m, nil
  110. }
  111. //session事务修改订单
  112. func UserLvUpOrderUpdateWithSession(session *xorm.Session, ordId int64, order *model.UserLevelOrd, forceCols ...string) (int64, error) {
  113. var (
  114. affected int64
  115. err error
  116. )
  117. if forceCols != nil {
  118. affected, err = session.Where("id=?", ordId).Cols(forceCols...).Update(order)
  119. } else {
  120. affected, err = session.Where("id=?", ordId).Update(order)
  121. }
  122. if err != nil {
  123. return 0, logx.Warn(err)
  124. }
  125. return affected, nil
  126. }