user_access_service.go 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  1. package merchant
  2. import (
  3. logger "eta/eta_mini_ht_api/common/component/log"
  4. "eta/eta_mini_ht_api/common/utils/page"
  5. "eta/eta_mini_ht_api/domian/user"
  6. "eta/eta_mini_ht_api/models/merchant"
  7. subscribeDao "eta/eta_mini_ht_api/models/merchant"
  8. "sync"
  9. "time"
  10. )
  11. type UserAccessDTO struct {
  12. ID int `json:"id"`
  13. TemplateUserId int `json:"templateUserId"`
  14. ProductID int `json:"productID"`
  15. ProductType string `json:"productType"`
  16. BeginDate string `json:"beginDate"`
  17. EndDate string `json:"endDate"`
  18. Status string `json:"status"`
  19. }
  20. var (
  21. productMap = map[string]merchant.MerchantProductType{
  22. "package": merchant.Package,
  23. "video": merchant.Video,
  24. "audio": merchant.Audio,
  25. "report": merchant.Report,
  26. }
  27. )
  28. func convertToUserAccessDTO(userSubscriptionAccessList merchant.UserSubscriptionAccessList) UserAccessDTO {
  29. return UserAccessDTO{
  30. ID: userSubscriptionAccessList.ID,
  31. TemplateUserId: userSubscriptionAccessList.TemplateUserId,
  32. ProductID: userSubscriptionAccessList.ProductID,
  33. ProductType: string(userSubscriptionAccessList.ProductType),
  34. BeginDate: userSubscriptionAccessList.BeginDate.Format(time.DateOnly),
  35. EndDate: userSubscriptionAccessList.EndDate.Format(time.DateOnly),
  36. Status: string(userSubscriptionAccessList.Status),
  37. }
  38. }
  39. func OpenProduct(templateUserId int, productOrderNo string, product MerchantProductDTO) error {
  40. userAccess := merchant.UserSubscriptionAccessList{
  41. TemplateUserId: templateUserId,
  42. ProductID: product.Id,
  43. ProductName: product.Title,
  44. ProductType: productMap[product.Type],
  45. BeginDate: time.Now(),
  46. Status: merchant.SubscribeValid,
  47. ProductOrderNo: productOrderNo,
  48. }
  49. if !product.IsPermanent {
  50. userAccess.EndDate = time.Now().Add(time.Duration(product.ValidDays) * 24 * time.Hour)
  51. }
  52. return merchant.InsertOrUpdateUserSubscribe(userAccess)
  53. }
  54. func ExpireProduct(deadLine string) (err error) {
  55. total := user.GetNeedExpiredAccessCount(deadLine)
  56. if total == 0 {
  57. logger.Info("没有需要过期的产品")
  58. return
  59. }
  60. for total > 0 {
  61. list, _ := user.GetExpiredAccessList(100, deadLine)
  62. if len(list) > 0 {
  63. var wg sync.WaitGroup
  64. wg.Add(len(list))
  65. for _, access := range list {
  66. go func(access subscribeDao.UserSubscriptionAccessList) {
  67. defer wg.Done()
  68. access.Status = subscribeDao.SubscribeExpired
  69. expireErr := subscribeDao.ExpireUserAccess(access)
  70. if expireErr != nil {
  71. logger.Error("更新产品过期状态失败:%v,用户:%d,产品:%d", expireErr, access.TemplateUserId, access.ProductID)
  72. }
  73. }(access)
  74. }
  75. wg.Wait()
  76. }
  77. total = user.GetNeedExpiredAccessCount(deadLine)
  78. }
  79. //if err != nil {
  80. // logger.Error("获取产品订单信息失败:%v", err)
  81. // return
  82. //}
  83. //product, err := merchantService.GetMerchantProductById(productOrder.ProductID)
  84. //if err != nil {
  85. // logger.Error("获取产品信息失败:%v", err)
  86. // return
  87. //}
  88. //return merchantService.CloseProduct(productOrderNo, product)
  89. return
  90. }
  91. func CloseProduct(templateUserId int, productOrderNo string, product MerchantProductDTO) error {
  92. userAccess := merchant.UserSubscriptionAccessList{
  93. TemplateUserId: templateUserId,
  94. ProductID: product.Id,
  95. ProductName: product.Title,
  96. ProductType: productMap[product.Type],
  97. Status: merchant.SubscribeClose,
  98. ProductOrderNo: productOrderNo,
  99. }
  100. return merchant.CloseUserAccess(userAccess)
  101. }
  102. func SubscribeList(templateUserId int, productType string, pageInfo page.PageInfo) (list []UserAccessDTO, err error) {
  103. offset := page.StartIndex(pageInfo.Current, pageInfo.PageSize)
  104. var subscribeList []merchant.UserSubscriptionAccessList
  105. subscribeList, err = merchant.SubscribeList(templateUserId, productType, pageInfo.LatestId, offset, pageInfo.PageSize)
  106. if err != nil {
  107. return
  108. }
  109. for _, userSubscriptionAccessList := range subscribeList {
  110. list = append(list, convertToUserAccessDTO(userSubscriptionAccessList))
  111. }
  112. return
  113. }
  114. func GetUserSubscribe(templateUserId int, productId int) (dto UserAccessDTO, err error) {
  115. var subscribe merchant.UserSubscriptionAccessList
  116. subscribe, err = merchant.GetUserSubscribeById(templateUserId, productId)
  117. if err != nil {
  118. return
  119. }
  120. dto = convertToUserAccessDTO(subscribe)
  121. return
  122. }
  123. func GetTotalUserPageCountByProductType(productType string, id int) (int64, int64) {
  124. return merchant.GetTotalUserPageCountByProductType(productType, id)
  125. }