merchant_user.go 2.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  1. package shanghu
  2. import (
  3. orm "duoduo/database"
  4. "time"
  5. )
  6. // 商户账号
  7. type MerchantUser struct {
  8. ID int64 `gorm:"column:id;type:bigint(20);primary_key;AUTO_INCREMENT" json:"id"` // 主键
  9. Code string `gorm:"column:code;type:varchar(255)" json:"code"`
  10. OpenID string `gorm:"column:open_id;type:varchar(255)" json:"open_id"` // open_id 唯一索引
  11. Phone string `gorm:"column:phone;type:varchar(24)" json:"phone"` // 手机号
  12. NickName string `gorm:"column:nick_name;type:varchar(255)" json:"nick_name"` // 微信用户名
  13. AvatarUrl string `gorm:"column:avatar_url;type:varchar(255)" json:"avatar_url"` // 头像url
  14. Admin int `gorm:"column:admin;type:int(11)" json:"admin"` // 1-管理员
  15. CreateBy int64 `gorm:"column:create_by;type:bigint(20)" json:"create_by"` // 创建者
  16. UpdateBy int64 `gorm:"column:update_by;type:bigint(20)" json:"update_by"` // 更新者
  17. CreatedAt time.Time `gorm:"column:created_at;type:datetime(3)" json:"created_at"` // 创建时间
  18. UpdatedAt time.Time `gorm:"column:updated_at;type:datetime(3)" json:"updated_at"` // 最后更新时间
  19. DeletedAt time.Time `gorm:"column:deleted_at;type:datetime(3);default:null" json:"deleted_at"` // 删除时间
  20. }
  21. func (m *MerchantUser) TableName() string {
  22. return "merchant_user"
  23. }
  24. func (m *MerchantUser) GetNum() int {
  25. var count int
  26. tableCount := orm.ShMysql.Table(m.TableName()).Where("open_id = ? ", m.OpenID)
  27. tableCount.Count(&count)
  28. return count
  29. }
  30. func (m *MerchantUser) GetCodeNum() int {
  31. var count int
  32. tableCount := orm.ShMysql.Table(m.TableName()).Where("code = ? ", m.Code)
  33. tableCount.Count(&count)
  34. return count
  35. }
  36. func (m *MerchantUser) GetUserInfo() (MerchantUser, error) {
  37. var doc MerchantUser
  38. table := orm.ShMysql.Table(m.TableName())
  39. table = table.Where("open_id = ? ", m.OpenID)
  40. if err := table.Select("*").First(&doc).Error; err != nil {
  41. return doc, err
  42. }
  43. return doc, nil
  44. }
  45. func (m *MerchantUser) GetUserInfoByCode() (MerchantUser, error) {
  46. var doc MerchantUser
  47. table := orm.ShMysql.Table(m.TableName())
  48. table = table.Where("code = ? ", m.Code)
  49. if err := table.Select("open_id").First(&doc).Error; err != nil {
  50. return doc, err
  51. }
  52. return doc, nil
  53. }
  54. func (u *MerchantUser) Create() (MerchantUser, error) {
  55. var doc MerchantUser
  56. var err error
  57. doc = *u
  58. err = orm.ShMysql.Table(u.TableName()).Create(&doc).Error
  59. if err != nil {
  60. return doc, err
  61. }
  62. return doc, nil
  63. }