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

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