user_access_service.go 2.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  1. package merchant
  2. import (
  3. "eta/eta_mini_ht_api/common/utils/page"
  4. "eta/eta_mini_ht_api/models/merchant"
  5. "time"
  6. )
  7. type UserAccessDTO struct {
  8. ID int
  9. TemplateUserId int
  10. ProductID int
  11. ProductType string
  12. BeginDate string
  13. EndDate string
  14. }
  15. var (
  16. productMap = map[string]merchant.MerchantProductType{
  17. "package": merchant.Package,
  18. "video": merchant.Video,
  19. "audio": merchant.Audio,
  20. "report": merchant.Report,
  21. }
  22. )
  23. func convertToUserAccessDTO(userSubscriptionAccessList merchant.UserSubscriptionAccessList) UserAccessDTO {
  24. return UserAccessDTO{
  25. ID: userSubscriptionAccessList.ID,
  26. TemplateUserId: userSubscriptionAccessList.TemplateUserId,
  27. ProductID: userSubscriptionAccessList.ProductID,
  28. ProductType: string(userSubscriptionAccessList.ProductType),
  29. BeginDate: userSubscriptionAccessList.BeginDate.Format(time.DateOnly),
  30. EndDate: userSubscriptionAccessList.EndDate.Format(time.DateOnly),
  31. }
  32. }
  33. func OpenProduct(templateUserId int, productOrderNo string, product MerchantProductDTO) error {
  34. userAccess := merchant.UserSubscriptionAccessList{
  35. TemplateUserId: templateUserId,
  36. ProductID: product.Id,
  37. ProductName: product.Title,
  38. ProductType: productMap[product.Type],
  39. BeginDate: time.Now(),
  40. Status: merchant.SubscribeValid,
  41. ProductOrderNo: productOrderNo,
  42. }
  43. if !product.IsPermanent {
  44. userAccess.EndDate = time.Now().Add(time.Duration(product.ValidDays) * 24 * time.Hour)
  45. }
  46. return merchant.InsertOrUpdateUserSubscribe(userAccess)
  47. }
  48. func CloseProduct(templateUserId int, productOrderNo string, product MerchantProductDTO) error {
  49. userAccess := merchant.UserSubscriptionAccessList{
  50. TemplateUserId: templateUserId,
  51. ProductID: product.Id,
  52. ProductName: product.Title,
  53. ProductType: productMap[product.Type],
  54. Status: merchant.SubscribeClose,
  55. ProductOrderNo: productOrderNo,
  56. }
  57. return merchant.CloseUserAccess(userAccess)
  58. }
  59. func SubscribeList(templateUserId int, productType string, pageInfo page.PageInfo) (list []UserAccessDTO, err error) {
  60. offset := page.StartIndex(pageInfo.Current, pageInfo.PageSize)
  61. var subscribeList []merchant.UserSubscriptionAccessList
  62. subscribeList, err = merchant.SubscribeList(templateUserId, productType, pageInfo.LatestId, offset, pageInfo.PageSize)
  63. if err != nil {
  64. return
  65. }
  66. for _, userSubscriptionAccessList := range subscribeList {
  67. list = append(list, convertToUserAccessDTO(userSubscriptionAccessList))
  68. }
  69. return
  70. }
  71. func GetUserSubscribe(templateUserId int, productId int) (dto UserAccessDTO, err error) {
  72. var subscribe merchant.UserSubscriptionAccessList
  73. subscribe, err = merchant.GetUserSubscribeById(templateUserId, productId)
  74. if err != nil {
  75. return
  76. }
  77. dto = convertToUserAccessDTO(subscribe)
  78. return
  79. }
  80. func GetTotalUserPageCountByProductType(productType string, id int) (int64, int64) {
  81. return merchant.GetTotalUserPageCountByProductType(productType, id)
  82. }