cyy_saas_user.go 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194
  1. package models
  2. import (
  3. "errors"
  4. "fmt"
  5. "reflect"
  6. "strings"
  7. "github.com/beego/beego/v2/client/orm"
  8. )
  9. type CyySaasUser struct {
  10. Id int `orm:"column(id);auto"`
  11. Uid int `orm:"column(uid);null" description:"用户uid"`
  12. Mobile string `orm:"column(mobile);size(11)" description:"手机号"`
  13. ParentId int `orm:"column(parent_id);null" description:"上级id"`
  14. CreatedAt int `orm:"column(created_at);null" description:"创建时间"`
  15. UpdatedAt int `orm:"column(updated_at);null" description:"更新时间"`
  16. IsDelete int8 `orm:"column(is_delete);null" description:"是否删除"`
  17. Price float64 `orm:"column(price);null;digits(10);decimals(2)" description:"可提现佣金"`
  18. TotalPrice float64 `orm:"column(total_price);null;digits(10);decimals(2)" description:"累计佣金"`
  19. StoreId int `orm:"column(store_id)" description:"商城ID,表示该用户是哪个商城带来的"`
  20. PlatformOpenId string `orm:"column(platform_open_id);size(255)" description:"平台openid"`
  21. PlatformOpenIdMerchant string `orm:"column(platform_open_id_merchant);size(255);null" description:"批发端openid"`
  22. ShareProfit float64 `orm:"column(share_profit);digits(10);decimals(2)" description:"联盟佣金"`
  23. Name string `orm:"column(name);size(255)" description:"昵称"`
  24. Gender int8 `orm:"column(gender);null" description:"性别,0: 未设置,1:男,2:女"`
  25. Avatar string `orm:"column(avatar);size(255)" description:"头像"`
  26. AccessToken string `orm:"column(access_token);size(255)" description:"token"`
  27. TotalIntegral int `orm:"column(total_integral);null"`
  28. Integral float64 `orm:"column(integral);digits(10);decimals(2)" description:"积分"`
  29. BytedanceOpenId string `orm:"column(bytedance_open_id);size(255)" description:"bytedance open id"`
  30. WithdrawMethod string `orm:"column(withdraw_method);null" description:"提现方式"`
  31. IsSalesman int8 `orm:"column(is_salesman)" description:"是否是业务员"`
  32. AliUserId string `orm:"column(ali_user_id);size(50)" description:"支付宝用户ID 18位"`
  33. LeaguePrice float64 `orm:"column(league_price);null;digits(10);decimals(2)"`
  34. SaasIntegral int `orm:"column(saas_integral);null" description:"联盟用户积分"`
  35. Money float64 `orm:"column(money);null;digits(10);decimals(2)" description:"余额"`
  36. CircleBg string `orm:"column(circle_bg);size(255);null" description:"朋友圈背景图"`
  37. AliOpenId string `orm:"column(ali_openId);size(255);null" description:"支付宝openid"`
  38. CloudInventoryBalance float64 `orm:"column(cloud_inventory_balance);null;digits(10);decimals(2)" description:"可提现云库存余额"`
  39. CloudInventoryFreezeBalance float64 `orm:"column(cloud_inventory_freeze_balance);null;digits(10);decimals(2)" description:"冻结云库存余额"`
  40. CloudInventoryTotalBalance float64 `orm:"column(cloud_inventory_total_balance);null;digits(10);decimals(2)" description:"累计云库存余额"`
  41. PurchaseMoney float64 `orm:"column(purchase_money);null;digits(10);decimals(2)" description:"采购金"`
  42. IsCloudInventory int8 `orm:"column(is_cloud_inventory);null" description:"是否云库存用户"`
  43. CloudInventoryLevel int8 `orm:"column(cloud_inventory_level);null" description:"云库存会员等级"`
  44. CanOpenStore int `orm:"column(can_open_store)" description:"是否有开店权限"`
  45. AiCloudInventory int8 `orm:"column(ai_cloud_inventory);null" description:"开启AI云库存 0关闭 1开启"`
  46. AutoExchange int8 `orm:"column(auto_exchange);null" description:"自动兑换采购金"`
  47. AiShopping int8 `orm:"column(ai_shopping);null" description:"AI带货"`
  48. PlatformOpenIdNew string `orm:"column(platform_open_id_new);size(255);null" description:"串码联名openid"`
  49. WechatUnionId string `orm:"column(wechat_union_id);size(255);null"`
  50. SaasUnionId string `orm:"column(saas_union_id);size(255);null"`
  51. IsPublicSphere int8 `orm:"column(is_public_sphere);null" description:"是否公域用户"`
  52. TotalAmount float64 `orm:"column(total_amount);null;digits(10);decimals(6)" description:"可用倍数"`
  53. WaitAmount float64 `orm:"column(wait_amount);null;digits(10);decimals(6)" description:"待领取红包"`
  54. LeijiTotalAmount float64 `orm:"column(leiji_total_amount);null;digits(10);decimals(6)" description:"累计倍数"`
  55. UnionId string `orm:"column(union_id);size(255);null"`
  56. }
  57. func (t *CyySaasUser) TableName() string {
  58. return "cyy_saas_user"
  59. }
  60. func init() {
  61. orm.RegisterModel(new(CyySaasUser))
  62. }
  63. // AddCyySaasUser insert a new CyySaasUser into database and returns
  64. // last inserted Id on success.
  65. func AddCyySaasUser(m *CyySaasUser) (id int64, err error) {
  66. o := orm.NewOrm()
  67. id, err = o.Insert(m)
  68. return
  69. }
  70. // GetCyySaasUserById retrieves CyySaasUser by Id. Returns error if
  71. // Id doesn't exist
  72. func GetCyySaasUserById(id int) (v *CyySaasUser, err error) {
  73. o := orm.NewOrm()
  74. v = &CyySaasUser{Id: id}
  75. if err = o.Read(v); err == nil {
  76. return v, nil
  77. }
  78. return nil, err
  79. }
  80. // GetAllCyySaasUser retrieves all CyySaasUser matches certain condition. Returns empty list if
  81. // no records exist
  82. func GetAllCyySaasUser(query map[string]string, fields []string, sortby []string, order []string,
  83. offset int64, limit int64) (ml []interface{}, err error) {
  84. o := orm.NewOrm()
  85. qs := o.QueryTable(new(CyySaasUser))
  86. // query k=v
  87. for k, v := range query {
  88. // rewrite dot-notation to Object__Attribute
  89. k = strings.Replace(k, ".", "__", -1)
  90. if strings.Contains(k, "isnull") {
  91. qs = qs.Filter(k, (v == "true" || v == "1"))
  92. } else {
  93. qs = qs.Filter(k, v)
  94. }
  95. }
  96. // order by:
  97. var sortFields []string
  98. if len(sortby) != 0 {
  99. if len(sortby) == len(order) {
  100. // 1) for each sort field, there is an associated order
  101. for i, v := range sortby {
  102. orderby := ""
  103. if order[i] == "desc" {
  104. orderby = "-" + v
  105. } else if order[i] == "asc" {
  106. orderby = v
  107. } else {
  108. return nil, errors.New("Error: Invalid order. Must be either [asc|desc]")
  109. }
  110. sortFields = append(sortFields, orderby)
  111. }
  112. qs = qs.OrderBy(sortFields...)
  113. } else if len(sortby) != len(order) && len(order) == 1 {
  114. // 2) there is exactly one order, all the sorted fields will be sorted by this order
  115. for _, v := range sortby {
  116. orderby := ""
  117. if order[0] == "desc" {
  118. orderby = "-" + v
  119. } else if order[0] == "asc" {
  120. orderby = v
  121. } else {
  122. return nil, errors.New("Error: Invalid order. Must be either [asc|desc]")
  123. }
  124. sortFields = append(sortFields, orderby)
  125. }
  126. } else if len(sortby) != len(order) && len(order) != 1 {
  127. return nil, errors.New("Error: 'sortby', 'order' sizes mismatch or 'order' size is not 1")
  128. }
  129. } else {
  130. if len(order) != 0 {
  131. return nil, errors.New("Error: unused 'order' fields")
  132. }
  133. }
  134. var l []CyySaasUser
  135. qs = qs.OrderBy(sortFields...)
  136. if _, err = qs.Limit(limit, offset).All(&l, fields...); err == nil {
  137. if len(fields) == 0 {
  138. for _, v := range l {
  139. ml = append(ml, v)
  140. }
  141. } else {
  142. // trim unused fields
  143. for _, v := range l {
  144. m := make(map[string]interface{})
  145. val := reflect.ValueOf(v)
  146. for _, fname := range fields {
  147. m[fname] = val.FieldByName(fname).Interface()
  148. }
  149. ml = append(ml, m)
  150. }
  151. }
  152. return ml, nil
  153. }
  154. return nil, err
  155. }
  156. // UpdateCyySaasUser updates CyySaasUser by Id and returns error if
  157. // the record to be updated doesn't exist
  158. func UpdateCyySaasUserById(m *CyySaasUser) (err error) {
  159. o := orm.NewOrm()
  160. v := CyySaasUser{Id: m.Id}
  161. // ascertain id exists in the database
  162. if err = o.Read(&v); err == nil {
  163. var num int64
  164. if num, err = o.Update(m); err == nil {
  165. fmt.Println("Number of records updated in database:", num)
  166. }
  167. }
  168. return
  169. }
  170. // DeleteCyySaasUser deletes CyySaasUser by Id and returns error if
  171. // the record to be deleted doesn't exist
  172. func DeleteCyySaasUser(id int) (err error) {
  173. o := orm.NewOrm()
  174. v := CyySaasUser{Id: id}
  175. // ascertain id exists in the database
  176. if err = o.Read(&v); err == nil {
  177. var num int64
  178. if num, err = o.Delete(&CyySaasUser{Id: id}); err == nil {
  179. fmt.Println("Number of records deleted in database:", num)
  180. }
  181. }
  182. return
  183. }