Non puoi selezionare più di 25 argomenti Gli argomenti devono iniziare con una lettera o un numero, possono includere trattini ('-') e possono essere lunghi fino a 35 caratteri.
 
 
 

65 righe
1.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. now := common.GetTimestamp()
  33. binding := UserAssetChannel{
  34. UserId: userId,
  35. ChannelType: channelType,
  36. Group: group,
  37. ChannelId: channelId,
  38. CreatedAt: now,
  39. UpdatedAt: now,
  40. }
  41. return DB.Clauses(clause.OnConflict{
  42. Columns: []clause.Column{
  43. {Name: "user_id"},
  44. {Name: "channel_type"},
  45. {Name: "group"},
  46. },
  47. DoUpdates: clause.Assignments(map[string]interface{}{
  48. "channel_id": channelId,
  49. "updated_at": now,
  50. }),
  51. }).Create(&binding).Error
  52. }
  53. func UnbindUserAssetChannel(userId int, channelType int, group string) error {
  54. return DB.Where("user_id = ? AND channel_type = ? AND "+commonGroupCol+" = ?", userId, channelType, group).
  55. Delete(&UserAssetChannel{}).Error
  56. }