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

271 lines
6.9 KiB

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