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

273 lines
6.9 KiB

5 years ago
5 years ago
4 years ago
5 years ago
5 years ago
4 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
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
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. "git.ouxuan.net/tommy/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. new(PadSigner),
  168. new(PadSigning),
  169. )
  170. fmt.Printf("error=======>%v\n\n", err)
  171. }
  172. func Condition(condition map[string]interface{}) *xorm.Session {
  173. session := core.GetXormAuto().NewSession()
  174. for k, v := range condition {
  175. if strings.Contains(k, " not in") {
  176. session = session.NotIn(strings.TrimRight(k, " not in"), v)
  177. } else if strings.Contains(k, " in") {
  178. session = session.In(strings.TrimRight(k, " in"), v)
  179. } else if strings.HasPrefix(strings.ToLower(k), "order by") {
  180. session = session.OrderBy(v.(string))
  181. } else if strings.HasSuffix(k, "<") || strings.HasSuffix(k, ">") ||
  182. strings.HasSuffix(k, "<=") || strings.HasSuffix(k, ">=") ||
  183. strings.HasSuffix(k, "<>") || strings.HasSuffix(k, "=") ||
  184. strings.HasSuffix(k, "!=") {
  185. session = session.Where(fmt.Sprintf("%s?", k), v)
  186. } else {
  187. session = session.Where(fmt.Sprintf("%s=?", k), v)
  188. }
  189. }
  190. return session
  191. }
  192. func Save(condition map[string]interface{}, obj interface{}, filed ...string) error {
  193. session := Condition(condition)
  194. defer session.Close()
  195. if condition != nil && Exist(Condition(condition), reflect.New(reflect.TypeOf(obj).Elem()).Interface()) {
  196. //存在则更新
  197. _, err := session.Cols(filed...).Update(obj)
  198. return err
  199. } else {
  200. //不存在则插入
  201. _, err := core.GetXormAuto().InsertOne(obj)
  202. return err
  203. }
  204. }
  205. func Exist(session *xorm.Session, obj interface{}) (result bool) {
  206. defer session.Close()
  207. result, _ = session.Exist(obj)
  208. return result
  209. }
  210. func GetTableName(bean interface{}) string {
  211. if table, ok := bean.(string); ok {
  212. return table
  213. }
  214. return core.GetXormAuto().TableName(bean)
  215. }
  216. func Alias(bean interface{}, alias string) string {
  217. tn := GetTableName(bean)
  218. return fmt.Sprintf("%s as %s", tn, alias)
  219. }
  220. func Get(bean interface{}, id interface{}) (bool, error) {
  221. return core.GetXormAuto().NoAutoCondition().Where("is_delete=0 and id=?", id).Get(bean)
  222. }
  223. func Del(bean interface{}, id interface{}) (int64, error) {
  224. return core.GetXormAuto().NoAutoCondition().ID(id).Delete(bean)
  225. }
  226. func Add(bean interface{}) (int64, error) {
  227. return core.GetXormAuto().InsertOne(bean)
  228. }
  229. func Update(id, bean interface{}, field ...string) (int64, error) {
  230. if len(field) > 0 {
  231. return core.GetXormAuto().ID(id).NoAutoCondition().Cols(field...).Update(bean)
  232. } else {
  233. return core.GetXormAuto().ID(id).NoAutoCondition().Update(bean)
  234. }
  235. }
  236. func Commit(f func(s *xorm.Session) error) error {
  237. session := core.GetXormAuto().NewSession()
  238. defer session.Close()
  239. err := session.Begin()
  240. if err != nil {
  241. return err
  242. }
  243. err = f(session)
  244. if err != nil {
  245. session.Rollback()
  246. return err
  247. }
  248. return session.Commit()
  249. }