community_list.go 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160
  1. package models
  2. import (
  3. "errors"
  4. "fmt"
  5. "reflect"
  6. "strings"
  7. "github.com/beego/beego/v2/client/orm"
  8. )
  9. type CommunityList struct {
  10. CommunityId int `orm:"column(community_id);auto" description:"社区图谱"`
  11. ParentId string `orm:"column(parent_id);size(225);null" description:"上级id"`
  12. UserId string `orm:"column(user_id);size(225);null"`
  13. Username string `orm:"column(username);size(255);null"`
  14. PromoteName string `orm:"column(promote_name);size(255);null" description:"级别"`
  15. Sales string `orm:"column(sales);size(255);null" description:"入仓金额"`
  16. TeamSales string `orm:"column(team_sales);size(255);null" description:"社区业绩"`
  17. TodayTeamSales string `orm:"column(today_team_sales);size(255);null" description:"今天业绩"`
  18. UserBv string `orm:"column(user_bv);size(255);null"`
  19. SubStatus string `orm:"column(sub_status);size(255);null"`
  20. CTime int64 `orm:"column(c_time);null"`
  21. IsYes int `orm:"column(is_yes);null" description:"默认0:待抓取"`
  22. }
  23. func (t *CommunityList) TableName() string {
  24. return "community_list"
  25. }
  26. func init() {
  27. orm.RegisterModel(new(CommunityList))
  28. }
  29. // AddCommunityList insert a new CommunityList into database and returns
  30. // last inserted Id on success.
  31. func AddCommunityList(m *CommunityList) (id int64, err error) {
  32. o := orm.NewOrm()
  33. id, err = o.Insert(m)
  34. return
  35. }
  36. // GetCommunityListById retrieves CommunityList by Id. Returns error if
  37. // Id doesn't exist
  38. func GetCommunityListById(id int) (v *CommunityList, err error) {
  39. o := orm.NewOrm()
  40. v = &CommunityList{CommunityId: id}
  41. if err = o.Read(v); err == nil {
  42. return v, nil
  43. }
  44. return nil, err
  45. }
  46. // GetAllCommunityList retrieves all CommunityList matches certain condition. Returns empty list if
  47. // no records exist
  48. func GetAllCommunityList(query map[string]string, fields []string, sortby []string, order []string,
  49. offset int64, limit int64) (ml []interface{}, err error) {
  50. o := orm.NewOrm()
  51. qs := o.QueryTable(new(CommunityList))
  52. // query k=v
  53. for k, v := range query {
  54. // rewrite dot-notation to Object__Attribute
  55. k = strings.Replace(k, ".", "__", -1)
  56. if strings.Contains(k, "isnull") {
  57. qs = qs.Filter(k, (v == "true" || v == "1"))
  58. } else {
  59. qs = qs.Filter(k, v)
  60. }
  61. }
  62. // order by:
  63. var sortFields []string
  64. if len(sortby) != 0 {
  65. if len(sortby) == len(order) {
  66. // 1) for each sort field, there is an associated order
  67. for i, v := range sortby {
  68. orderby := ""
  69. if order[i] == "desc" {
  70. orderby = "-" + v
  71. } else if order[i] == "asc" {
  72. orderby = v
  73. } else {
  74. return nil, errors.New("Error: Invalid order. Must be either [asc|desc]")
  75. }
  76. sortFields = append(sortFields, orderby)
  77. }
  78. qs = qs.OrderBy(sortFields...)
  79. } else if len(sortby) != len(order) && len(order) == 1 {
  80. // 2) there is exactly one order, all the sorted fields will be sorted by this order
  81. for _, v := range sortby {
  82. orderby := ""
  83. if order[0] == "desc" {
  84. orderby = "-" + v
  85. } else if order[0] == "asc" {
  86. orderby = v
  87. } else {
  88. return nil, errors.New("Error: Invalid order. Must be either [asc|desc]")
  89. }
  90. sortFields = append(sortFields, orderby)
  91. }
  92. } else if len(sortby) != len(order) && len(order) != 1 {
  93. return nil, errors.New("Error: 'sortby', 'order' sizes mismatch or 'order' size is not 1")
  94. }
  95. } else {
  96. if len(order) != 0 {
  97. return nil, errors.New("Error: unused 'order' fields")
  98. }
  99. }
  100. var l []CommunityList
  101. qs = qs.OrderBy(sortFields...)
  102. if _, err = qs.Limit(limit, offset).All(&l, fields...); err == nil {
  103. if len(fields) == 0 {
  104. for _, v := range l {
  105. ml = append(ml, v)
  106. }
  107. } else {
  108. // trim unused fields
  109. for _, v := range l {
  110. m := make(map[string]interface{})
  111. val := reflect.ValueOf(v)
  112. for _, fname := range fields {
  113. m[fname] = val.FieldByName(fname).Interface()
  114. }
  115. ml = append(ml, m)
  116. }
  117. }
  118. return ml, nil
  119. }
  120. return nil, err
  121. }
  122. // UpdateCommunityList updates CommunityList by Id and returns error if
  123. // the record to be updated doesn't exist
  124. func UpdateCommunityListById(m *CommunityList) (err error) {
  125. o := orm.NewOrm()
  126. v := CommunityList{CommunityId: m.CommunityId}
  127. // ascertain id exists in the database
  128. if err = o.Read(&v); err == nil {
  129. var num int64
  130. if num, err = o.Update(m); err == nil {
  131. fmt.Println("Number of records updated in database:", num)
  132. }
  133. }
  134. return
  135. }
  136. // DeleteCommunityList deletes CommunityList by Id and returns error if
  137. // the record to be deleted doesn't exist
  138. func DeleteCommunityList(id int) (err error) {
  139. o := orm.NewOrm()
  140. v := CommunityList{CommunityId: id}
  141. // ascertain id exists in the database
  142. if err = o.Read(&v); err == nil {
  143. var num int64
  144. if num, err = o.Delete(&CommunityList{CommunityId: id}); err == nil {
  145. fmt.Println("Number of records deleted in database:", num)
  146. }
  147. }
  148. return
  149. }