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

262 lines
6.7 KiB

5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
4 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
4 years ago
5 years ago
4 years ago
5 years ago
5 years ago
  1. package models
  2. import (
  3. "errors"
  4. "fmt"
  5. "hudongzhuanjia/utils/define"
  6. "reflect"
  7. "strings"
  8. "time"
  9. "github.com/ouxuanserver/osmanthuswine/src/core"
  10. "github.com/xormplus/xorm"
  11. )
  12. const TableNamePrefix = "ox_"
  13. type Model struct {
  14. Id int `json:"id" xorm:"not null pk autoincr INT(11)"`
  15. IsDelete bool `json:"is_delete" xorm:"default(0)" description:"是否删除"`
  16. CreatedAt time.Time `json:"created_at" xorm:"created" description:"创建时间"`
  17. UpdatedAt time.Time `json:"updated_at" xorm:"updated" description:"更新时间"`
  18. }
  19. func ParseToken(token string) (_type string, id int, err error) {
  20. tokens := strings.SplitN(token, ":", 2)
  21. if len(tokens) < 2 {
  22. err = errors.New("token失效")
  23. return
  24. }
  25. _type = tokens[0]
  26. if tokens[0] == define.TYPE_USER || tokens[0] == define.TYPE_LIVEUSER { // h5用户
  27. user := &User{}
  28. err = user.GetByToken(tokens[1])
  29. if err != nil {
  30. return
  31. }
  32. id = user.Id
  33. } else if tokens[0] == define.TYPE_CUSTOMER { // 客户
  34. customer := &Customer{}
  35. err = customer.GetByToken(tokens[1])
  36. if err != nil {
  37. return
  38. }
  39. id = customer.Id
  40. } else if tokens[0] == define.TYPE_ENTRYPEOPLE { // 录入人员
  41. entry := &OrderEntryPerson{}
  42. err = entry.GetByToken(tokens[1])
  43. if err != nil {
  44. return
  45. }
  46. id = entry.Id
  47. } else {
  48. err = errors.New("token失效")
  49. }
  50. return
  51. }
  52. func (m *Model) TableName(bean interface{}) string {
  53. return core.GetXormAuto().TableName(bean)
  54. }
  55. func (m *Model) Alias(bean interface{}, alias string) string {
  56. tn := m.TableName(bean)
  57. return fmt.Sprintf("%s as %s", tn, alias)
  58. }
  59. func (m *Model) Add(bean interface{}) (int64, error) {
  60. return core.GetXormAuto().InsertOne(bean)
  61. }
  62. func (m *Model) Get(bean interface{}, id interface{}) (bool, error) {
  63. return core.GetXormAuto().NoAutoCondition().Where("is_delete=0 and id=?", id).Get(bean)
  64. }
  65. func (m *Model) Del(bean interface{}, id interface{}) (int64, error) {
  66. return core.GetXormAuto().NoAutoCondition().Where("is_delete=0 and id=?", id).Delete(bean)
  67. }
  68. func (m *Model) Update(bean, id interface{}, field ...string) (int64, error) {
  69. s := core.GetXormAuto().NoAutoCondition().ID(id)
  70. defer s.Close()
  71. if len(field) > 0 {
  72. if field[0] == "*" {
  73. s = s.AllCols()
  74. } else if field[0] == "#" {
  75. s = s.MustCols(field[1:]...)
  76. } else {
  77. s = s.Cols(field...)
  78. }
  79. }
  80. return s.Update(bean)
  81. }
  82. func (m *Model) Exist(bean, id interface{}) (bool, error) {
  83. return core.GetXormAuto().NoAutoCondition().Where("is_delete=0 and id=?", id).Exist(bean)
  84. }
  85. func init() {
  86. err := core.GetXormAuto().Sync2(
  87. new(Activity),
  88. new(ActivityModuleService),
  89. new(AreaStore),
  90. new(AuctionHistory),
  91. new(AuctionResultRecord),
  92. new(AuctionPlayer),
  93. new(BaheTeam),
  94. new(BaheTeamMember),
  95. new(BarrageHistory),
  96. new(BullyScreenHistory),
  97. new(BullyScreenServer),
  98. new(BullyScreenWalletHistory),
  99. new(Customer),
  100. new(CustomerGoods),
  101. new(CustomerOperation),
  102. new(CustomerOrder),
  103. new(DanMuServer),
  104. new(Invitation),
  105. new(InvitationLetter),
  106. new(LotteryDrawActivity),
  107. new(LotteryDrawEnvelopeWallet),
  108. new(LotteryDrawRecord),
  109. new(LotteryDrawRule),
  110. new(LotteryDrawRuleLadder),
  111. new(LotteryDrawWalletHistory),
  112. new(ModuleService),
  113. new(ModuleServiceHistory),
  114. new(ModuleStyle),
  115. new(NewAuctionActivity),
  116. new(NewVoteActivity),
  117. new(NewVoteActivityHistory),
  118. new(NewVoteActivityLadder),
  119. new(OrderDrawActivity),
  120. new(OrderDrawRecord),
  121. new(OrderDrawRule),
  122. new(OrderDrawRuleLadder),
  123. new(OrderDrawStyle),
  124. new(RewardHistory),
  125. new(RewardServer),
  126. new(RewardWallet),
  127. new(RewardWalletHistory),
  128. new(Sensitive),
  129. new(ShakeRedEnvelopeActivity),
  130. new(ShakeRedEnvelopeRecord),
  131. new(ShakeRedEnvelopeRule),
  132. new(ShakeRedEnvelopeRuleLadder),
  133. new(SignHistory),
  134. new(SignUp),
  135. new(TugOfWar),
  136. new(UpperWall),
  137. new(User),
  138. new(UserWalletHistory),
  139. new(NewVoteActivity),
  140. new(NewVoteActivityHistory),
  141. new(OrderEntryPerson),
  142. new(Rehearsal),
  143. new(MsgWallServer),
  144. new(ShakeRedEnvelopeUser),
  145. new(OrderGift),
  146. new(UserPrize),
  147. new(Calorie),
  148. new(CalorieUser),
  149. new(UserOrder),
  150. new(UserRedPack),
  151. new(RealSignList),
  152. //new(RealSignHistory),
  153. new(LiveViewer),
  154. new(LiveConfig),
  155. new(LiveRedEnvelopeRule),
  156. new(UserTransfer),
  157. new(CustomerOrderSub),
  158. )
  159. fmt.Printf("error=======>%v\n\n", err)
  160. }
  161. func Condition(condition map[string]interface{}) *xorm.Session {
  162. session := core.GetXormAuto().NewSession()
  163. for k, v := range condition {
  164. if strings.Contains(k, " not in") {
  165. session = session.NotIn(strings.TrimRight(k, " not in"), v)
  166. } else if strings.Contains(k, " in") {
  167. session = session.In(strings.TrimRight(k, " in"), v)
  168. } else if strings.HasPrefix(strings.ToLower(k), "order by") {
  169. session = session.OrderBy(v.(string))
  170. } else if strings.HasSuffix(k, "<") || strings.HasSuffix(k, ">") ||
  171. strings.HasSuffix(k, "<=") || strings.HasSuffix(k, ">=") ||
  172. strings.HasSuffix(k, "<>") || strings.HasSuffix(k, "=") ||
  173. strings.HasSuffix(k, "!=") {
  174. session = session.Where(fmt.Sprintf("%s?", k), v)
  175. } else {
  176. session = session.Where(fmt.Sprintf("%s=?", k), v)
  177. }
  178. }
  179. return session
  180. }
  181. func Save(condition map[string]interface{}, obj interface{}, filed ...string) error {
  182. session := Condition(condition)
  183. defer session.Close()
  184. if condition != nil && Exist(Condition(condition), reflect.New(reflect.TypeOf(obj).Elem()).Interface()) {
  185. //存在则更新
  186. _, err := session.Cols(filed...).Update(obj)
  187. return err
  188. } else {
  189. //不存在则插入
  190. _, err := core.GetXormAuto().InsertOne(obj)
  191. return err
  192. }
  193. }
  194. func Exist(session *xorm.Session, obj interface{}) (result bool) {
  195. defer session.Close()
  196. result, _ = session.Exist(obj)
  197. return result
  198. }
  199. func GetTableName(bean interface{}) string {
  200. if table, ok := bean.(string); ok {
  201. return table
  202. }
  203. return core.GetXormAuto().TableName(bean)
  204. }
  205. func Alias(bean interface{}, alias string) string {
  206. tn := GetTableName(bean)
  207. return fmt.Sprintf("%s as %s", tn, alias)
  208. }
  209. func Get(bean interface{}, id interface{}) (bool, error) {
  210. return core.GetXormAuto().NoAutoCondition().Where("is_delete=0 and id=?", id).Get(bean)
  211. }
  212. func Del(bean interface{}, id interface{}) (int64, error) {
  213. return core.GetXormAuto().NoAutoCondition().ID(id).Delete(bean)
  214. }
  215. func Add(bean interface{}) (int64, error) {
  216. return core.GetXormAuto().InsertOne(bean)
  217. }
  218. func Update(id, bean interface{}, field ...string) (int64, error) {
  219. if len(field) > 0 {
  220. return core.GetXormAuto().ID(id).NoAutoCondition().Cols(field...).Update(bean)
  221. } else {
  222. return core.GetXormAuto().ID(id).NoAutoCondition().Update(bean)
  223. }
  224. }
  225. func Commit(f func(s *xorm.Session) error) error {
  226. session := core.GetXormAuto().NewSession()
  227. defer session.Close()
  228. err := session.Begin()
  229. if err != nil {
  230. return err
  231. }
  232. err = f(session)
  233. if err != nil {
  234. session.Rollback()
  235. return err
  236. }
  237. return session.Commit()
  238. }