user.go 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  1. package service
  2. import (
  3. "github.com/gin-gonic/gin"
  4. "gorm.io/gorm"
  5. "log"
  6. "sfis/db"
  7. "sfis/model"
  8. "sfis/model/response"
  9. "strconv"
  10. "strings"
  11. "time"
  12. )
  13. func UserProject(projectIds, appId, startTime, endTime string, leftNum, costModel, interfaceStatus, callTimesLimitDay, dataNumOneTimes, tradeMoney, buyType, historyUnitPrice int, c *gin.Context) {
  14. projectIdsArr := strings.Split(projectIds, ",")
  15. if len(projectIdsArr) > 0 {
  16. var errs error
  17. for _, v := range projectIdsArr {
  18. userProject := &model.UserProduct{}
  19. userProject.AppID = appId
  20. userProject.StartAt, _ = time.ParseInLocation("2006-01-02 15:04:05", startTime, time.Local)
  21. userProject.EndAt, _ = time.ParseInLocation("2006-01-02 15:04:05", endTime, time.Local)
  22. userProject.LeftNum = leftNum
  23. userProject.CostModel = costModel
  24. userProject.InterfaceStatus = interfaceStatus
  25. userProject.CallTimesLimitDay = callTimesLimitDay
  26. userProject.DataNumLimitOneTimes = dataNumOneTimes
  27. id, _ := strconv.Atoi(v)
  28. userProject.ProductID = id
  29. userProjectInfo := []model.UserProduct{}
  30. err := db.GetSFISDB().Where("product_id = ? and app_id = ?", id, appId).Find(&userProjectInfo).Error
  31. if err != nil {
  32. response.FailWithMessage("查询用户产品信息出错", c)
  33. return
  34. }
  35. if len(userProjectInfo) > 0 {
  36. before := userProjectInfo[0].LeftNum
  37. after := userProjectInfo[0].LeftNum + leftNum
  38. userProjectId := userProjectInfo[0].ID
  39. errs = db.GetSFISDB().Transaction(func(tx *gorm.DB) error {
  40. //更新用户产品信息
  41. userProject.LeftNum = userProjectInfo[0].LeftNum + leftNum
  42. err := tx.Exec("update user_product set left_num = 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 = ?", leftNum, startTime, endTime, costModel, interfaceStatus, callTimesLimitDay, dataNumOneTimes, id, appId).Error
  43. if err != nil {
  44. log.Printf("appID:[%s],projectId:[%d] execute cost user_account error:[%v]", appId, id, err)
  45. tx.Rollback()
  46. return err
  47. }
  48. //生成购买产品记录
  49. 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, id, userProjectId, before, after, tradeMoney, buyType, historyUnitPrice).Error
  50. if err != nil {
  51. log.Printf("appID:[%s],projectId[%d],trade_money:[%d] execute insert into user_buy_record error:[%v]", appId, id, tradeMoney, err)
  52. tx.Rollback()
  53. return err
  54. }
  55. return nil
  56. })
  57. } else {
  58. errs = db.GetSFISDB().Transaction(func(tx *gorm.DB) error {
  59. //生用户产品
  60. err := tx.Create(userProject).Error
  61. if err != nil {
  62. log.Printf("appID:[%s],projectId:[%d] insert user_project error:[%v]", appId, id, err)
  63. tx.Rollback()
  64. return tx.Error
  65. }
  66. userProjectId := userProject.ID
  67. //生成购买产品记录
  68. 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, id, userProjectId, 0, leftNum, tradeMoney, buyType, historyUnitPrice).Error
  69. if err != nil {
  70. log.Printf("appID:[%s],projectId[%d],trade_money:[%d] execute insert into user_buy_record error:[%v]", appId, id, tradeMoney, err)
  71. tx.Rollback()
  72. return err
  73. }
  74. return nil
  75. })
  76. }
  77. }
  78. if errs == nil {
  79. response.Ok(c)
  80. return
  81. } else {
  82. response.FailWithMessage("购买产品失败", c)
  83. return
  84. }
  85. }
  86. response.FailWithMessage("缺少参数", c)
  87. }
  88. func UserProjectList(appId string, c *gin.Context) {
  89. userProject := []model.UserProduct{}
  90. err := db.GetSFISDB().Where("app_id = ? and ", appId).Find(&userProject).Error
  91. if err != nil {
  92. log.Printf("appID:[%s] find into user_project error:[%v]", appId, err)
  93. response.FailWithMessage("查询出错", c)
  94. } else {
  95. response.OkWithData(userProject, c)
  96. }
  97. }