user.go 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161
  1. package service
  2. import (
  3. "errors"
  4. "fmt"
  5. "github.com/gin-gonic/gin"
  6. "go.uber.org/zap"
  7. "gorm.io/gorm"
  8. "log"
  9. "sfbase/global"
  10. "sfbase/utils"
  11. "sfis/db"
  12. "sfis/lock"
  13. "sfis/model"
  14. "strconv"
  15. "strings"
  16. "sync"
  17. )
  18. func CreateUserProduct(appId string, productArr []map[string]interface{}) (status int, haveProductId string, errArr []error) {
  19. //取出用户锁
  20. lock.MainLock.Lock()
  21. userLock := lock.UserLockMap[appId]
  22. lock.MainLock.Unlock()
  23. userLock.Lock()
  24. defer userLock.Unlock()
  25. var errs error
  26. haveProductId = "" //已经购买过的产品id
  27. //var errArr = make([]error, 0)
  28. for _, val := range productArr {
  29. productId := utils.IntAll(val["productId"])
  30. costModel := utils.IntAll(val["costModel"])
  31. userProduct := &model.UserProductModel{}
  32. userProduct.AppID = appId
  33. userProduct.CostModel = costModel
  34. userProduct.LeftNum = 0
  35. userProduct.InterfaceStatus = utils.IntAll(val["interfaceStatus"])
  36. userProduct.CallTimesLimitDay = utils.IntAll(val["callTimesLimitDay"])
  37. userProduct.DataNumLimitOneTimes = utils.IntAll(val["dataNumOneTimes"])
  38. userProduct.ProductID = productId
  39. //查询产品是否购买过
  40. userProductInfo := model.UserProduct{}
  41. err := db.GetSFISDB().Where("product_id = ? and app_id = ?", productId, appId).Find(&userProductInfo).Error
  42. if err != nil {
  43. errArr = append(errArr, err)
  44. global.Logger.Error("CreateUserProduct查询user_product表出错:", zap.Any("err:", err))
  45. return 0, haveProductId, errArr
  46. }
  47. //用户第一次购买产品
  48. if userProductInfo.ID == 0 {
  49. err := db.GetSFISDB().Create(userProduct).Error
  50. if err != nil {
  51. errArr = append(errArr, errs)
  52. global.Logger.Info("用户购买产品失败:", zap.Any("appId:", appId), zap.Any("productId:", productId))
  53. continue
  54. }
  55. } else {
  56. haveProductId += "[" + strconv.Itoa(productId) + "]"
  57. global.Logger.Info("用户已购买过该产品", zap.Any("appId:", appId), zap.Any("productId:", productId))
  58. continue
  59. }
  60. }
  61. return 1, haveProductId, errArr
  62. }
  63. func UserProductList(appId string, c *gin.Context) (userProduct []model.UserProduct, err error) {
  64. //userProduct := []model.UserProduct{}
  65. err = db.GetSFISDB().Where("app_id = ? ", appId).Find(&userProduct).Error
  66. if err != nil {
  67. log.Printf("appID:[%s] find into user_product error:[%v]", appId, err)
  68. }
  69. return userProduct, err
  70. }
  71. //创建用户
  72. func CreateUser(user model.User) (model.User, error) {
  73. var tempUser []model.User
  74. // 判断用户名是否重复
  75. db.GetSFISDB().Where("name = ?", user.Name).Find(&tempUser)
  76. if len(tempUser) > 0 {
  77. global.Logger.Error("userCreate Error", zap.Any("user", user), zap.Any("error", "用户名已存在"))
  78. return user, errors.New("用户名已存在")
  79. }
  80. // 判断手机号是否重复
  81. var tempUser_ []model.User
  82. db.GetSFISDB().Where("phone = ?", user.Phone).Find(&tempUser_)
  83. if len(tempUser_) > 0 {
  84. global.Logger.Error("userCreate Error", zap.Any("user", user), zap.Any("error", "手机号已存在"))
  85. return user, errors.New("手机号已存在")
  86. }
  87. errs := db.GetSFISDB().Transaction(func(tx *gorm.DB) error {
  88. // 新增用户
  89. err := tx.Create(&user).Error
  90. if err != nil {
  91. global.Logger.Error("userCreate Error", zap.Any("user", user), zap.Any("error", err))
  92. tx.Rollback()
  93. return err
  94. }
  95. // 初始化用户账户
  96. userAccount := model.UserAccount{AppID: user.AppID, Money: 0}
  97. err = tx.Create(&userAccount).Error
  98. if err != nil {
  99. global.Logger.Error("userAccountInit Error", zap.Any("user", user), zap.Any("userAccount", userAccount), zap.Any("error", err))
  100. tx.Rollback()
  101. return err
  102. }
  103. return nil
  104. })
  105. if errs != nil {
  106. global.Logger.Error("userCreate Error", zap.Any("user", user), zap.Any("error", errs))
  107. return user, errors.New("创建失败")
  108. } else {
  109. global.Logger.Info("userCreate Success", zap.Any("user", user))
  110. // 生全局内存锁
  111. lock.MainLock.Lock()
  112. lock.UserLockMap[user.AppID] = &sync.Mutex{}
  113. lock.MainLock.Unlock()
  114. return user, nil
  115. }
  116. }
  117. // 查询用户
  118. func ListUser(condMap map[string]interface{}, page int, limit int) ([]map[string]interface{}, int64, error) {
  119. // 拼查询sql
  120. var key []string
  121. var param []interface{}
  122. for k, v := range condMap {
  123. if v == "" {
  124. continue
  125. }
  126. var kStr string
  127. kStr = fmt.Sprintf("%s like ?", k)
  128. v = "%" + v.(string) + "%"
  129. key = append(key, kStr)
  130. param = append(param, v)
  131. }
  132. whereSql := strings.Join(key, " and ")
  133. var totalCount int64
  134. var userInfo []map[string]interface{}
  135. err := db.GetSFISDB().Table("user").Where(whereSql, param...).Where("delete_at is null").Count(&totalCount).Error
  136. if err != nil {
  137. return nil, 0, errors.New("查询失败")
  138. }
  139. if totalCount == 0 {
  140. return userInfo, 0, nil
  141. }
  142. if whereSql != "" {
  143. whereSql = " and " + whereSql
  144. }
  145. sql := "select user.app_id,user.name,user.phone,user.secret_key,user.ip_white_list,user.create_at,money from user LEFT JOIN user_account on user.app_id=user_account.app_id where user.delete_at is Null " + whereSql
  146. if limit != 0 && page > 0 {
  147. sql += fmt.Sprintf(" limit %d ,%d", (page-1)*limit, limit)
  148. }
  149. result := db.GetSFISDB().Raw(sql, param...).Scan(&userInfo)
  150. if result.Error != nil {
  151. return nil, 0, errors.New("查询失败")
  152. }
  153. return userInfo, totalCount, nil
  154. }