user_source_click_flow.go 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153
  1. package services
  2. import (
  3. "eta/eta_mini_crm_ht/models"
  4. "eta/eta_mini_crm_ht/utils"
  5. "strings"
  6. )
  7. func GetUserSourceClickFlowListCountByUserId(userId int, condition string, pars []interface{}, permissionIds []int, product models.SourceType) (total int, conditionNew string, parsNew []interface{}, err error) {
  8. if len(permissionIds) == 0 {
  9. total, err = models.GetUserSourceClickFlowListCountByUserId(userId, condition, pars)
  10. conditionNew = condition
  11. parsNew = pars
  12. return
  13. }
  14. if product == models.AudioSourceType || product == models.VideoSourceType {
  15. mediaType := transMediaType(product)
  16. videoIds, queryErr := GetMediaIdsByPermissionIds(permissionIds, mediaType)
  17. if queryErr != nil || len(videoIds) == 0 {
  18. return
  19. }
  20. condition = condition + " and source_id in (" + utils.GetOrmReplaceHolder(len(videoIds)) + ") "
  21. pars = append(pars, videoIds)
  22. } else if product == models.ReportSourceType {
  23. reportIds, queryErr := GetReportIdsByPermissionIds(permissionIds)
  24. if queryErr != nil || len(reportIds) == 0 {
  25. return
  26. }
  27. condition = condition + " and source_id in (" + utils.GetOrmReplaceHolder(len(reportIds)) + ") "
  28. pars = append(pars, reportIds)
  29. } else {
  30. reportIds, reportErr := GetReportIdsByPermissionIds(permissionIds)
  31. addReport := false
  32. addVideo := false
  33. addAudio := false
  34. var permissionCondition string
  35. var permissionPars []interface{}
  36. if reportErr == nil && len(reportIds) > 0 {
  37. permissionCondition = permissionCondition + " (source_id in (" + utils.GetOrmReplaceHolder(len(reportIds)) + ") and source_type=?)"
  38. permissionPars = append(permissionPars, reportIds)
  39. permissionPars = append(permissionPars, models.ReportSourceType)
  40. addReport = true
  41. }
  42. videoIds, videoErr := GetMediaIdsByPermissionIds(permissionIds, models.Video)
  43. if videoErr == nil && len(videoIds) > 0 {
  44. if addReport {
  45. permissionCondition = permissionCondition + "or (source_id in (" + utils.GetOrmReplaceHolder(len(videoIds)) + ") and source_type=?)"
  46. } else {
  47. permissionCondition = permissionCondition + " (source_id in (" + utils.GetOrmReplaceHolder(len(videoIds)) + ") and source_type=?)"
  48. }
  49. permissionPars = append(permissionPars, videoIds)
  50. permissionPars = append(permissionPars, models.VideoSourceType)
  51. addVideo = true
  52. }
  53. audioIds, audioErr := GetMediaIdsByPermissionIds(permissionIds, models.Audio)
  54. if audioErr == nil && len(audioIds) > 0 {
  55. if addReport || addVideo {
  56. permissionCondition = permissionCondition + "or (source_id in (" + utils.GetOrmReplaceHolder(len(audioIds)) + ") and source_type=?)"
  57. } else {
  58. permissionCondition = permissionCondition + " (source_id in (" + utils.GetOrmReplaceHolder(len(audioIds)) + ") and source_type=?)"
  59. }
  60. permissionPars = append(permissionPars, audioIds)
  61. permissionPars = append(permissionPars, models.AudioSourceType)
  62. addAudio = true
  63. }
  64. if addReport || addVideo || addAudio {
  65. condition = condition + " and ( " + permissionCondition + " ) "
  66. pars = append(pars, permissionPars...)
  67. }
  68. }
  69. if condition == "" {
  70. utils.FileLog.Warn("没有符合要求的阅读记录")
  71. return
  72. }
  73. total, err = models.GetUserSourceClickFlowListCountByUserId(userId, condition, pars)
  74. conditionNew = condition
  75. parsNew = pars
  76. return
  77. }
  78. func GetUserSourceClickFlowListByUserId(userId int, condition string, pars []interface{}, startSize, pageSize int) (items []models.UserSourceClickFlowView, err error) {
  79. list, err := models.GetUserSourceClickFlowListByUserId(userId, condition, pars, startSize, pageSize)
  80. if err != nil {
  81. return
  82. }
  83. for _, item := range list {
  84. viewItem := item.ToView()
  85. switch item.SourceType {
  86. case models.ReportSourceType:
  87. viewItem.PermissionNames = getReportPermissionNames(item.SourceId)
  88. case models.AudioSourceType, models.VideoSourceType:
  89. mediaType := transMediaType(item.SourceType)
  90. viewItem.PermissionNames = getMediaPermissionNames(item.SourceId, mediaType)
  91. }
  92. items = append(items, viewItem)
  93. }
  94. return
  95. }
  96. func transMediaType(sourceType models.SourceType) models.MediaType {
  97. switch sourceType {
  98. case models.AudioSourceType:
  99. return models.Audio
  100. case models.VideoSourceType:
  101. return models.Video
  102. default:
  103. return ""
  104. }
  105. }
  106. func getReportPermissionNames(reportId int) (permissionNames string) {
  107. report, err := models.GetReportById(reportId)
  108. if err != nil {
  109. return
  110. }
  111. if report.Source == models.SourceETA {
  112. permissionIds, queryErr := models.GetByPermissionIdsByClassifyId(report.ClassifyId)
  113. if queryErr != nil {
  114. return
  115. }
  116. permissionNameList, nameErr := models.GetPermissionNames(permissionIds)
  117. if nameErr != nil {
  118. utils.FileLog.Error("获取品种名称列表失败", err.Error())
  119. return
  120. }
  121. if len(permissionNameList) > 0 {
  122. permissionNames = strings.Join(permissionNameList, ",")
  123. }
  124. return
  125. }
  126. if report.Source == models.SourceHT {
  127. return report.PlateName
  128. }
  129. return
  130. }
  131. func getMediaPermissionNames(mediaId int, mediaType models.MediaType) (permissionNames string) {
  132. media, err := models.GetMediaById(mediaType, mediaId)
  133. if err != nil {
  134. utils.FileLog.Error("获取媒体失败,Err:" + err.Error())
  135. return
  136. }
  137. permissionIds, queryErr := models.GetPermissionIdsByMediaId(media.MediaType, media.Id)
  138. if queryErr != nil {
  139. utils.FileLog.Error("获取媒体权限失败,Err:" + queryErr.Error())
  140. return
  141. }
  142. permissionNameList, nameErr := models.GetPermissionNames(permissionIds)
  143. if nameErr != nil {
  144. utils.FileLog.Error("获取品种名称列表信息失败", nameErr.Error())
  145. return
  146. }
  147. permissionNames = strings.Join(permissionNameList, ",")
  148. return
  149. }