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

264 lines
6.7 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
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 {
  50. err = errors.New("token失效")
  51. }
  52. return
  53. }
  54. func (m *Model) TableName(bean interface{}) string {
  55. return core.GetXormAuto().TableName(bean)
  56. }
  57. func (m *Model) Alias(bean interface{}, alias string) string {
  58. tn := m.TableName(bean)
  59. return fmt.Sprintf("%s as %s", tn, alias)
  60. }
  61. func (m *Model) Add(bean interface{}) (int64, error) {
  62. return core.GetXormAuto().InsertOne(bean)
  63. }
  64. func (m *Model) Get(bean interface{}, id interface{}) (bool, error) {
  65. return core.GetXormAuto().NoAutoCondition().Where("is_delete=0 and id=?", id).Get(bean)
  66. }
  67. func (m *Model) Del(bean interface{}, id interface{}) (int64, error) {
  68. return core.GetXormAuto().NoAutoCondition().Where("is_delete=0 and id=?", id).Delete(bean)
  69. }
  70. func (m *Model) Update(bean, id interface{}, field ...string) (int64, error) {
  71. s := core.GetXormAuto().NoAutoCondition().ID(id)
  72. defer s.Close()
  73. if len(field) > 0 {
  74. if field[0] == "*" {
  75. s = s.AllCols()
  76. } else if field[0] == "#" {
  77. s = s.MustCols(field[1:]...)
  78. } else {
  79. s = s.Cols(field...)
  80. }
  81. }
  82. return s.Update(bean)
  83. }
  84. func (m *Model) Exist(bean, id interface{}) (bool, error) {
  85. return core.GetXormAuto().NoAutoCondition().Where("is_delete=0 and id=?", id).Exist(bean)
  86. }
  87. func init() {
  88. err := core.GetXormAuto().Sync2(
  89. new(Activity),
  90. new(ActivityModuleService),
  91. new(AreaStore),
  92. new(AuctionHistory),
  93. new(AuctionResultRecord),
  94. new(AuctionPlayer),
  95. new(BaheTeam),
  96. new(BaheTeamMember),
  97. new(BarrageHistory),
  98. new(BullyScreenHistory),
  99. new(BullyScreenServer),
  100. new(BullyScreenWalletHistory),
  101. new(Customer),
  102. new(CustomerGoods),
  103. new(CustomerOperation),
  104. new(CustomerOrder),
  105. new(DanMuServer),
  106. new(Invitation),
  107. new(InvitationLetter),
  108. new(LotteryDrawActivity),
  109. new(LotteryDrawEnvelopeWallet),
  110. new(LotteryDrawRecord),
  111. new(LotteryDrawRule),
  112. new(LotteryDrawRuleLadder),
  113. new(LotteryDrawWalletHistory),
  114. new(ModuleService),
  115. new(ModuleServiceHistory),
  116. new(ModuleStyle),
  117. new(NewAuctionActivity),
  118. new(NewVoteActivity),
  119. new(NewVoteActivityHistory),
  120. new(NewVoteActivityLadder),
  121. new(OrderDrawActivity),
  122. new(OrderDrawRecord),
  123. new(OrderDrawRule),
  124. new(OrderDrawRuleLadder),
  125. new(OrderDrawStyle),
  126. new(RewardHistory),
  127. new(RewardServer),
  128. new(RewardWallet),
  129. new(RewardWalletHistory),
  130. new(Sensitive),
  131. new(ShakeRedEnvelopeActivity),
  132. new(ShakeRedEnvelopeRecord),
  133. new(ShakeRedEnvelopeRule),
  134. new(ShakeRedEnvelopeRuleLadder),
  135. new(SignHistory),
  136. new(SignUp),
  137. new(TugOfWar),
  138. new(UpperWall),
  139. new(User),
  140. new(UserWalletHistory),
  141. new(NewVoteActivity),
  142. new(NewVoteActivityHistory),
  143. new(OrderEntryPerson),
  144. new(Rehearsal),
  145. new(MsgWallServer),
  146. new(ShakeRedEnvelopeUser),
  147. new(OrderGift),
  148. new(UserPrize),
  149. new(Calorie),
  150. new(CalorieUser),
  151. new(UserOrder),
  152. new(UserRedPack),
  153. new(RealSignList),
  154. //new(RealSignHistory),
  155. new(LiveViewer),
  156. new(LiveConfig),
  157. new(LiveRedEnvelopeRule),
  158. new(UserTransfer),
  159. new(CustomerOrderSub),
  160. )
  161. fmt.Printf("error=======>%v\n\n", err)
  162. }
  163. func Condition(condition map[string]interface{}) *xorm.Session {
  164. session := core.GetXormAuto().NewSession()
  165. for k, v := range condition {
  166. if strings.Contains(k, " not in") {
  167. session = session.NotIn(strings.TrimRight(k, " not in"), v)
  168. } else if strings.Contains(k, " in") {
  169. session = session.In(strings.TrimRight(k, " in"), v)
  170. } else if strings.HasPrefix(strings.ToLower(k), "order by") {
  171. session = session.OrderBy(v.(string))
  172. } else if strings.HasSuffix(k, "<") || strings.HasSuffix(k, ">") ||
  173. strings.HasSuffix(k, "<=") || strings.HasSuffix(k, ">=") ||
  174. strings.HasSuffix(k, "<>") || strings.HasSuffix(k, "=") ||
  175. strings.HasSuffix(k, "!=") {
  176. session = session.Where(fmt.Sprintf("%s?", k), v)
  177. } else {
  178. session = session.Where(fmt.Sprintf("%s=?", k), v)
  179. }
  180. }
  181. return session
  182. }
  183. func Save(condition map[string]interface{}, obj interface{}, filed ...string) error {
  184. session := Condition(condition)
  185. defer session.Close()
  186. if condition != nil && Exist(Condition(condition), reflect.New(reflect.TypeOf(obj).Elem()).Interface()) {
  187. //存在则更新
  188. _, err := session.Cols(filed...).Update(obj)
  189. return err
  190. } else {
  191. //不存在则插入
  192. _, err := core.GetXormAuto().InsertOne(obj)
  193. return err
  194. }
  195. }
  196. func Exist(session *xorm.Session, obj interface{}) (result bool) {
  197. defer session.Close()
  198. result, _ = session.Exist(obj)
  199. return result
  200. }
  201. func GetTableName(bean interface{}) string {
  202. if table, ok := bean.(string); ok {
  203. return table
  204. }
  205. return core.GetXormAuto().TableName(bean)
  206. }
  207. func Alias(bean interface{}, alias string) string {
  208. tn := GetTableName(bean)
  209. return fmt.Sprintf("%s as %s", tn, alias)
  210. }
  211. func Get(bean interface{}, id interface{}) (bool, error) {
  212. return core.GetXormAuto().NoAutoCondition().Where("is_delete=0 and id=?", id).Get(bean)
  213. }
  214. func Del(bean interface{}, id interface{}) (int64, error) {
  215. return core.GetXormAuto().NoAutoCondition().ID(id).Delete(bean)
  216. }
  217. func Add(bean interface{}) (int64, error) {
  218. return core.GetXormAuto().InsertOne(bean)
  219. }
  220. func Update(id, bean interface{}, field ...string) (int64, error) {
  221. if len(field) > 0 {
  222. return core.GetXormAuto().ID(id).NoAutoCondition().Cols(field...).Update(bean)
  223. } else {
  224. return core.GetXormAuto().ID(id).NoAutoCondition().Update(bean)
  225. }
  226. }
  227. func Commit(f func(s *xorm.Session) error) error {
  228. session := core.GetXormAuto().NewSession()
  229. defer session.Close()
  230. err := session.Begin()
  231. if err != nil {
  232. return err
  233. }
  234. err = f(session)
  235. if err != nil {
  236. session.Rollback()
  237. return err
  238. }
  239. return session.Commit()
  240. }