Вы не можете выбрать более 25 тем Темы должны начинаться с буквы или цифры, могут содержать дефисы(-) и должны содержать не более 35 символов.
 
 
 

88 строки
2.9 KiB

  1. package model
  2. import (
  3. "errors"
  4. "github.com/QuantumNous/new-api/common"
  5. "gorm.io/gorm"
  6. "gorm.io/gorm/clause"
  7. )
  8. type UserAssetChannel struct {
  9. Id int `json:"id" gorm:"primaryKey"`
  10. UserId int `json:"user_id" gorm:"not null;uniqueIndex:idx_user_asset_channel,priority:1"`
  11. ChannelType int `json:"channel_type" gorm:"not null;uniqueIndex:idx_user_asset_channel,priority:2"`
  12. Group string `json:"group" gorm:"column:group;type:varchar(64);not null;uniqueIndex:idx_user_asset_channel,priority:3"`
  13. ChannelId int `json:"channel_id" gorm:"not null"`
  14. CreatedAt int64 `json:"created_at"`
  15. UpdatedAt int64 `json:"updated_at"`
  16. }
  17. func (UserAssetChannel) TableName() string {
  18. return "user_asset_channels"
  19. }
  20. func GetUserAssetChannel(userId int, channelType int, group string) (*UserAssetChannel, error) {
  21. var binding UserAssetChannel
  22. err := DB.Where("user_id = ? AND channel_type = ? AND "+commonGroupCol+" = ?", userId, channelType, group).First(&binding).Error
  23. if errors.Is(err, gorm.ErrRecordNotFound) {
  24. return nil, nil
  25. }
  26. if err != nil {
  27. return nil, err
  28. }
  29. return &binding, nil
  30. }
  31. func BindUserAssetChannel(userId int, channelType int, group string, channelId int) error {
  32. return BindUserAssetChannelWithTx(DB, userId, channelType, group, channelId)
  33. }
  34. func BindUserAssetChannelWithTx(tx *gorm.DB, userId int, channelType int, group string, channelId int) error {
  35. now := common.GetTimestamp()
  36. binding := UserAssetChannel{
  37. UserId: userId,
  38. ChannelType: channelType,
  39. Group: group,
  40. ChannelId: channelId,
  41. CreatedAt: now,
  42. UpdatedAt: now,
  43. }
  44. return tx.Session(&gorm.Session{SkipDefaultTransaction: true}).Clauses(clause.OnConflict{
  45. Columns: []clause.Column{
  46. {Name: "user_id"},
  47. {Name: "channel_type"},
  48. {Name: "group"},
  49. },
  50. DoUpdates: clause.Assignments(map[string]interface{}{
  51. "channel_id": channelId,
  52. "updated_at": now,
  53. }),
  54. }).Create(&binding).Error
  55. }
  56. func GetUserAssetChannelsByTypes(userId int, channelTypes []int, group string) ([]UserAssetChannel, error) {
  57. if len(channelTypes) == 0 {
  58. return nil, nil
  59. }
  60. var bindings []UserAssetChannel
  61. err := DB.Where("user_id = ? AND channel_type IN ? AND "+commonGroupCol+" = ?", userId, channelTypes, group).
  62. Order("updated_at DESC, id DESC").
  63. Find(&bindings).Error
  64. return bindings, err
  65. }
  66. func DeleteUserAssetChannelsByTypesWithTx(tx *gorm.DB, userId int, channelTypes []int, group string) error {
  67. if len(channelTypes) == 0 {
  68. return nil
  69. }
  70. return tx.Session(&gorm.Session{SkipDefaultTransaction: true}).Where("user_id = ? AND channel_type IN ? AND "+commonGroupCol+" = ?", userId, channelTypes, group).
  71. Delete(&UserAssetChannel{}).Error
  72. }
  73. func UnbindUserAssetChannel(userId int, channelType int, group string) error {
  74. return DB.Where("user_id = ? AND channel_type = ? AND "+commonGroupCol+" = ?", userId, channelType, group).
  75. Delete(&UserAssetChannel{}).Error
  76. }