互动
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.

171 lines
4.2 KiB

5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
  1. package models
  2. import (
  3. "fmt"
  4. "github.com/ouxuanserver/osmanthuswine/src/core"
  5. "github.com/xormplus/xorm"
  6. "reflect"
  7. "strings"
  8. )
  9. const TableNamePrefix = "ox_"
  10. func init() {
  11. err := core.GetXormAuto().Sync2(
  12. new(Activity),
  13. new(ActivityModuleService),
  14. new(AreaStore),
  15. new(AuctionHistory),
  16. new(AuctionResultRecord),
  17. new(BaheTeam),
  18. new(BaheTeamMember),
  19. new(BarrageHistory),
  20. new(BullyScreenHistory),
  21. new(BullyScreenServer),
  22. new(BullyScreenWalletHistory),
  23. new(Customer),
  24. new(CustomerGoods),
  25. new(CustomerOperation),
  26. new(CustomerOrder),
  27. new(DanMuServer),
  28. new(Invitation),
  29. new(InvitationLetter),
  30. new(LotteryDrawActivity),
  31. new(LotteryDrawEnvelopeWallet),
  32. new(LotteryDrawRecord),
  33. new(LotteryDrawRule),
  34. new(LotteryDrawRuleLadder),
  35. new(LotteryDrawWalletHistory),
  36. new(ModuleService),
  37. new(ModuleServiceHistory),
  38. new(ModuleStyle),
  39. new(NewAuctionActivity),
  40. new(NewVoteActivity),
  41. new(NewVoteActivityHistory),
  42. new(NewVoteActivityLadder),
  43. new(OrderDrawActivity),
  44. new(OrderDrawRecord),
  45. new(OrderDrawRule),
  46. new(OrderDrawRuleLadder),
  47. new(OrderDrawStyle),
  48. new(RewardHistory),
  49. new(RewardServer),
  50. new(RewardWallet),
  51. new(RewardWalletHistory),
  52. new(Sensitive),
  53. new(ShakeRedEnvelopeActivity),
  54. new(ShakeRedEnvelopeRecord),
  55. new(ShakeRedEnvelopeRule),
  56. new(ShakeRedEnvelopeRuleLadder),
  57. new(SignHistory),
  58. new(SignUp),
  59. new(TugOfWar),
  60. new(UpperWall),
  61. new(User),
  62. new(UserWalletHistory),
  63. new(NewVoteActivity),
  64. new(NewVoteActivityHistory),
  65. new(OrderEntryPerson),
  66. new(Rehearsal),
  67. new(MsgWallServer),
  68. new(ShakeRedEnvelopeUser),
  69. new(OrderGift),
  70. new(UserPrize),
  71. new(Calorie),
  72. new(CalorieUser),
  73. new(UserOrder),
  74. new(UserRedPack),
  75. new(RealSignList),
  76. //new(RealSignHistory),
  77. new(LiveViewer),
  78. new(LiveConfig),
  79. new(LiveRedEnvelopeRule),
  80. new(UserTransfer),
  81. new(CustomerOrderSub),
  82. )
  83. fmt.Printf("error=======>%v\n\n", err)
  84. }
  85. func Condition(condition map[string]interface{}) *xorm.Session {
  86. session := core.GetXormAuto().NewSession()
  87. for k, v := range condition {
  88. if strings.Contains(k, " not in") {
  89. session = session.NotIn(strings.TrimRight(k, " not in"), v)
  90. } else if strings.Contains(k, " in") {
  91. session = session.In(strings.TrimRight(k, " in"), v)
  92. } else if strings.HasPrefix(strings.ToLower(k), "order by") {
  93. session = session.OrderBy(v.(string))
  94. } else if strings.HasSuffix(k, "<") || strings.HasSuffix(k, ">") ||
  95. strings.HasSuffix(k, "<=") || strings.HasSuffix(k, ">=") ||
  96. strings.HasSuffix(k, "<>") || strings.HasSuffix(k, "=") ||
  97. strings.HasSuffix(k, "!=") {
  98. session = session.Where(fmt.Sprintf("%s?", k), v)
  99. } else {
  100. session = session.Where(fmt.Sprintf("%s=?", k), v)
  101. }
  102. }
  103. return session
  104. }
  105. func Save(condition map[string]interface{}, obj interface{}, filed ...string) error {
  106. session := Condition(condition)
  107. defer session.Close()
  108. if condition != nil && Exist(Condition(condition), reflect.New(reflect.TypeOf(obj).Elem()).Interface()) {
  109. //存在则更新
  110. _, err := session.Cols(filed...).Update(obj)
  111. return err
  112. } else {
  113. //不存在则插入
  114. _, err := core.GetXormAuto().InsertOne(obj)
  115. return err
  116. }
  117. }
  118. func Exist(session *xorm.Session, obj interface{}) (result bool) {
  119. defer session.Close()
  120. result, _ = session.Exist(obj)
  121. return result
  122. }
  123. func GetTableName(bean interface{}) string {
  124. if table, ok := bean.(string); ok {
  125. return table
  126. }
  127. return core.GetXormAuto().TableName(bean)
  128. }
  129. func AliasTableName(bean interface{}, alias string) string {
  130. tn := GetTableName(bean)
  131. return fmt.Sprintf("%s as %s", tn, alias)
  132. }
  133. func Get(bean interface{}, id int64) (bool, error) {
  134. return core.GetXormAuto().Where("is_delete=0 and id=?", id).Get(bean)
  135. }
  136. func Add(bean interface{}) (int64, error) {
  137. return core.GetXormAuto().InsertOne(bean)
  138. }
  139. func Update(id, bean interface{}, field ...string) (int64, error) {
  140. if len(field) > 0 {
  141. return core.GetXormAuto().ID(id).Cols(field...).Update(bean)
  142. } else {
  143. return core.GetXormAuto().ID(id).Update(bean)
  144. }
  145. }
  146. func Commit(f func(s *xorm.Session) error) error {
  147. session := core.GetXormAuto().NewSession()
  148. defer session.Close()
  149. err := session.Begin()
  150. if err != nil {
  151. return err
  152. }
  153. err = f(session)
  154. if err != nil {
  155. session.Rollback()
  156. return err
  157. }
  158. return session.Commit()
  159. }