user.go 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193
  1. package service
  2. import (
  3. "log"
  4. "sfis/db"
  5. "sfis/lock"
  6. "sfis/model"
  7. "sfis/model/response"
  8. "strconv"
  9. "strings"
  10. "time"
  11. "github.com/gin-gonic/gin"
  12. "gorm.io/gorm"
  13. )
  14. func UserProduct(productIds, appId, startTime, endTime string, leftNum, costModel, interfaceStatus, callTimesLimitDay, dataNumOneTimes, tradeMoney, buyType int, c *gin.Context) {
  15. //取出用户锁
  16. lock.MainLock.Lock()
  17. userLock := lock.UserLockMap[appId]
  18. lock.MainLock.Unlock()
  19. userLock.Lock()
  20. defer userLock.Unlock()
  21. productIdsArr := strings.Split(productIds, ",")
  22. if len(productIdsArr) > 0 {
  23. var errs error
  24. for _, v := range productIdsArr {
  25. productId, _ := strconv.Atoi(v)
  26. userProduct := &model.UserProduct{}
  27. userProduct.AppID = appId
  28. userProduct.StartAt, _ = time.ParseInLocation("2006-01-02 15:04:05", startTime, time.Local)
  29. userProduct.EndAt, _ = time.ParseInLocation("2006-01-02 15:04:05", endTime, time.Local)
  30. userProduct.LeftNum = leftNum
  31. userProduct.CostModel = costModel
  32. userProduct.InterfaceStatus = interfaceStatus
  33. userProduct.CallTimesLimitDay = callTimesLimitDay
  34. userProduct.DataNumLimitOneTimes = dataNumOneTimes
  35. userProduct.ProductID = productId
  36. userProductInfo := model.UserProduct{}
  37. product := model.Product{}
  38. //查询产品信息,获取购买时候产品单价、试用次数
  39. err := db.GetSFISDB().Where("id = ?", productId).Find(&product).Error
  40. if err != nil {
  41. response.FailWithMessage("查询产品信息出错", c)
  42. return
  43. }
  44. historyUnitPrice := product.UnitPrice
  45. err = db.GetSFISDB().Where("product_id = ? and app_id = ?", productId, appId).Find(&userProductInfo).Error
  46. if err != nil {
  47. response.FailWithMessage("查询用户产品信息出错", c)
  48. return
  49. }
  50. //已选择过该产品--更新用户产品信息
  51. if userProductInfo.ID != 0 {
  52. before := userProductInfo.LeftNum
  53. after := userProductInfo.LeftNum + leftNum
  54. userProductId := userProductInfo.ID
  55. errs = db.GetSFISDB().Transaction(func(tx *gorm.DB) error {
  56. //更新用户产品信息
  57. userProduct.LeftNum = userProductInfo.LeftNum + leftNum
  58. userProduct.UpdateAt = time.Now()
  59. log.Println(userProduct.UpdateAt, "++++++++++++++++++++")
  60. err := tx.Exec("update user_product set left_num = ?, start_at = ?,end_at = ?,cost_model = ?,interface_status = ?,call_times_limit_day=?,data_num_limit_one_times=? where product_id = ? and app_id = ?", after, startTime, endTime, costModel, interfaceStatus, callTimesLimitDay, dataNumOneTimes, productId, appId).Error
  61. if err != nil {
  62. log.Printf("appID:[%s],productId:[%d] update user_product error:[%v]", appId, productId, err)
  63. tx.Rollback()
  64. return err
  65. }
  66. //生成购买产品记录
  67. //扣费类型为扣余额,user_buy_record中tradeMoney金额为0
  68. rechargeAmount := 0
  69. if costModel == 0 {
  70. rechargeAmount = tradeMoney
  71. }
  72. err = tx.Exec("insert into user_buy_record (`app_id`,`product_id`,`user_product_id`,`before`,`after`,`trade_money`,`buy_type`,`history_unit_price`) values (?,?,?,?,?,?,?,?)", appId, productId, userProductId, before, after, rechargeAmount, buyType, historyUnitPrice).Error
  73. if err != nil {
  74. log.Printf("appID:[%s],productId[%d],trade_money:[%d] execute insert into user_buy_record error:[%v]", appId, productId, tradeMoney, err)
  75. tx.Rollback()
  76. return err
  77. }
  78. //扣费类型是扣余额,充值用户余额
  79. if costModel == 1 {
  80. userAccount := model.UserAccount{}
  81. err = tx.First(&userAccount, model.UserAccount{AppID: appId}).Error
  82. if err != nil {
  83. log.Printf("appID:[%s],productId[%d],trade_money:[%d] execute find user_account error:[%v]", appId, productId, tradeMoney, err)
  84. tx.Rollback()
  85. return err
  86. }
  87. moneyBefore := userAccount.Money
  88. moneyAfter := userAccount.Money + tradeMoney
  89. //充值
  90. err := tx.Exec("update user_account set money = ? WHERE `app_id` = ?", moneyAfter, appId).Error
  91. if err != nil {
  92. log.Printf("appID:[%s],money:[%d] execute cost user_account error:[%v]", appId, moneyAfter, err)
  93. tx.Rollback()
  94. return err
  95. }
  96. //生充值记录
  97. err = tx.Exec("insert into user_money_record (app_id,`before`,`after`,trade_money) values (?,?,?,?)", appId, moneyBefore, moneyAfter, tradeMoney).Error
  98. if err != nil {
  99. log.Printf("appID:[%s],trade_money:[%d] execute insert into user_money_record error:[%v]", appId, tradeMoney, err)
  100. tx.Rollback()
  101. return err
  102. }
  103. }
  104. return nil
  105. })
  106. } else { //用户没有购买过改产品
  107. errs = db.GetSFISDB().Transaction(func(tx *gorm.DB) error {
  108. //扣费类型为扣余额,user_buy_record中tradeMoney金额为0
  109. rechargeAmount := 0
  110. //第一次购买产品赠送试用量
  111. testNum := product.TestNum
  112. remark := ""
  113. if costModel == 0 {
  114. rechargeAmount = tradeMoney
  115. leftNum += testNum
  116. } else if costModel == 1 {
  117. //扣余额,把赠送量转化成金额
  118. freeMoney := testNum * product.UnitPrice
  119. tradeMoney += freeMoney
  120. remark = "充值金额为:" + strconv.Itoa(tradeMoney) + "赠送金额为:" + strconv.Itoa(freeMoney)
  121. }
  122. userProduct.LeftNum = leftNum
  123. //生用户产品
  124. err := tx.Create(userProduct).Error
  125. if err != nil {
  126. log.Printf("appID:[%s],productId:[%d] insert user_product error:[%v]", appId, productId, err)
  127. tx.Rollback()
  128. return tx.Error
  129. }
  130. userProductId := userProduct.ID
  131. //生成购买产品记录
  132. err = tx.Exec("insert into user_buy_record (`app_id`,`product_id`,`user_product_id`,`before`,`after`,`trade_money`,`buy_type`,`history_unit_price`) values (?,?,?,?,?,?,?,?)", appId, productId, userProductId, 0, leftNum, rechargeAmount, buyType, historyUnitPrice).Error
  133. if err != nil {
  134. log.Printf("appID:[%s],productId[%d],trade_money:[%d] execute insert into user_buy_record error:[%v]", appId, productId, tradeMoney, err)
  135. tx.Rollback()
  136. return err
  137. }
  138. //扣费类型是扣余额,充值用户余额
  139. if costModel == 1 {
  140. //第一次购买产品赠送免费次数
  141. userAccount := model.UserAccount{}
  142. err = tx.First(&userAccount, model.UserAccount{AppID: appId}).Error
  143. if err != nil {
  144. log.Printf("appID:[%s],productId[%d],trade_money:[%d] execute find user_account error:[%v]", appId, productId, tradeMoney, err)
  145. tx.Rollback()
  146. return err
  147. }
  148. moneyBefore := userAccount.Money
  149. moneyAfter := userAccount.Money + tradeMoney
  150. //充值
  151. err := tx.Exec("update user_account set money = ? WHERE `app_id` = ?", moneyAfter, appId).Error
  152. if err != nil {
  153. log.Printf("appID:[%s],money:[%d] execute cost user_account error:[%v]", appId, moneyAfter, err)
  154. tx.Rollback()
  155. return err
  156. }
  157. //生充值记录
  158. err = tx.Exec("insert into user_money_record (app_id,`before`,`after`,trade_money,remark) values (?,?,?,?,?)", appId, moneyBefore, moneyAfter, tradeMoney, remark).Error
  159. if err != nil {
  160. log.Printf("appID:[%s],trade_money:[%d] execute insert into user_money_record error:[%v]", appId, tradeMoney, err)
  161. tx.Rollback()
  162. return err
  163. }
  164. }
  165. return nil
  166. })
  167. }
  168. }
  169. if errs == nil {
  170. response.Ok(c)
  171. return
  172. } else {
  173. response.FailWithMessage("购买产品失败", c)
  174. return
  175. }
  176. }
  177. response.FailWithMessage("缺少参数", c)
  178. }
  179. func UserProductList(appId string, c *gin.Context) {
  180. userProduct := []model.UserProduct{}
  181. err := db.GetSFISDB().Where("app_id = ? and ", appId).Find(&userProduct).Error
  182. if err != nil {
  183. log.Printf("appID:[%s] find into user_product error:[%v]", appId, err)
  184. response.FailWithMessage("查询出错", c)
  185. } else {
  186. response.OkWithData(userProduct, c)
  187. }
  188. }