product.go 9.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331
  1. package services
  2. import (
  3. "errors"
  4. "eta/eta_mini_crm_ht/models"
  5. "eta/eta_mini_crm_ht/utils"
  6. "fmt"
  7. "strconv"
  8. "strings"
  9. "time"
  10. )
  11. var (
  12. tranMediaMap = map[models.MediaType]string{
  13. models.Video: "视频",
  14. models.Audio: "音频",
  15. }
  16. )
  17. func GetUnsetProductCountByCondition(productType string, permissionIds []int, condition string, pars []interface{}) (total int, ids []int, err error) {
  18. setIds, err := models.GetProductSourceIdsByProductType(productType)
  19. if err != nil {
  20. return
  21. }
  22. switch productType {
  23. case "report":
  24. return getReportIdsByCondition(setIds, permissionIds, condition, pars)
  25. case "media":
  26. return getMediaIdsByCondition(setIds, permissionIds, condition, pars)
  27. default:
  28. err = errors.New("不支持的产品类型查询")
  29. return
  30. }
  31. }
  32. func getReportIdsByCondition(setIds []int, permissionIds []int, condition string, pars []interface{}) (total int, filterIds []int, err error) {
  33. permissionNames, err := models.GetPermissionNamesWithRiskLevel(permissionIds)
  34. var conditionHT string
  35. var conditionETA string
  36. if err != nil {
  37. utils.FileLog.Error("查询品种名称失败", err.Error)
  38. return
  39. }
  40. if len(permissionNames) > 0 {
  41. conditionHT = "(source ='HT' and plate_name in (" + utils.GetOrmReplaceHolder(len(permissionNames)) + "))"
  42. }
  43. filterPermissionIds, err := models.FilterPermissionIdsWithRiskLevel(permissionIds)
  44. if err != nil {
  45. utils.FileLog.Error("过滤品种Id失败", err.Error)
  46. return
  47. }
  48. classifyCondition := utils.GetOrmReplaceHolder(len(filterPermissionIds))
  49. var classifyPars []interface{}
  50. classifyPars = append(classifyPars, filterPermissionIds)
  51. classifyIds, err := models.GetClassifyIdsByPermissionIds(classifyCondition, classifyPars)
  52. if len(classifyIds) > 0 {
  53. conditionETA = "(source ='ETA' and classify_id in (" + utils.GetOrmReplaceHolder(len(classifyIds)) + "))"
  54. }
  55. if conditionHT != "" {
  56. if conditionETA != "" {
  57. condition += "and ( " + conditionHT + " or " + conditionETA + ")"
  58. pars = append(pars, permissionNames)
  59. pars = append(pars, classifyIds)
  60. } else {
  61. condition += "and " + conditionHT
  62. pars = append(pars, permissionNames)
  63. }
  64. } else {
  65. if conditionETA != "" {
  66. condition += "and " + conditionETA
  67. pars = append(pars, classifyIds)
  68. }
  69. }
  70. var ids []int
  71. ids, err = models.GetReportIdsByCondition(condition, pars)
  72. if err != nil {
  73. utils.FileLog.Error("查询报告Id失败", err.Error)
  74. return
  75. }
  76. filterMap := make(map[int]bool)
  77. for _, filterId := range setIds {
  78. filterMap[filterId] = true
  79. }
  80. for _, id := range ids {
  81. if !filterMap[id] {
  82. filterIds = append(filterIds, id)
  83. total++
  84. }
  85. }
  86. return
  87. }
  88. func getMediaIdsByCondition(setIds []int, permissionIds []int, condition string, pars []interface{}) (total int, filterIds []int, err error) {
  89. filterPermissionIds, err := models.FilterPermissionIdsWithRiskLevel(permissionIds)
  90. if err != nil {
  91. utils.FileLog.Error("过滤品种id失败", err.Error)
  92. return
  93. }
  94. mediaIds, err := models.GetMediaIdIdsByPermissionId(filterPermissionIds)
  95. if err != nil {
  96. utils.FileLog.Error("过滤媒体Id失败", err.Error)
  97. return
  98. }
  99. if len(mediaIds) > 0 {
  100. condition += "and id in (" + utils.GetOrmReplaceHolder(len(mediaIds)) + ")"
  101. pars = append(pars, mediaIds)
  102. mediaIds, err = models.GetMediaIdsByCondition(condition, pars)
  103. }
  104. filterMap := make(map[int]bool)
  105. for _, filterId := range setIds {
  106. filterMap[filterId] = true
  107. }
  108. for _, id := range mediaIds {
  109. if !filterMap[id] {
  110. total++
  111. filterIds = append(filterIds, id)
  112. }
  113. }
  114. return
  115. }
  116. func GetUnsetProductByCondition(productType string, ids []int, sortCondition string, startSize int, pageSize int) (products []*ProductView, err error) {
  117. switch productType {
  118. case "report":
  119. var reports []models.Report
  120. reports, err = models.GetReportByIds(sortCondition, ids, startSize, pageSize)
  121. if err != nil {
  122. utils.FileLog.Error("查询报告产品失败", err.Error)
  123. return
  124. }
  125. for _, report := range reports {
  126. products = append(products, &ProductView{
  127. ProductName: report.Title,
  128. SourceId: report.Id,
  129. ProductType: "report",
  130. PublishedTime: report.PublishedTime,
  131. })
  132. }
  133. return
  134. case "media":
  135. var medias []models.Media
  136. medias, err = models.GetMediasById(ids, sortCondition, startSize, pageSize)
  137. if err != nil {
  138. utils.FileLog.Error("查询报告产品失败", err.Error)
  139. return
  140. }
  141. for _, media := range medias {
  142. products = append(products, &ProductView{
  143. ProductName: media.MediaName,
  144. SourceId: media.Id,
  145. ProductType: string(media.MediaType),
  146. PublishedTime: media.PublishedTime.Format(time.DateTime),
  147. })
  148. }
  149. return
  150. default:
  151. err = errors.New("不支持的产品类型查询")
  152. return
  153. }
  154. }
  155. type ProductView struct {
  156. Id int
  157. ProductName string
  158. SourceId int
  159. ProductType string
  160. RiskLevel string
  161. PublishedTime string
  162. PermissionNames string
  163. UpdatedTime string
  164. Price string
  165. IsPermanent bool
  166. ValidDays int
  167. CoverSrc string
  168. SaleStatus string
  169. Creator string
  170. Description string
  171. }
  172. func GetRiskLevel(productType string, id int) (riskLevel string, productName string, permissionName string, err error) {
  173. switch productType {
  174. case "report":
  175. var report *models.Report
  176. report, err = models.GetReportById(id)
  177. if err != nil {
  178. utils.FileLog.Error("获取报告失败,无法确认风险等级", err.Error())
  179. return
  180. }
  181. switch report.Source {
  182. case models.SourceHT:
  183. var permission models.Permission
  184. permission, err = models.GetPermissionByName(report.PlateName)
  185. if err != nil {
  186. utils.FileLog.Error("获取品种信息,无法确认风险等级", err.Error())
  187. return
  188. }
  189. return permission.RiskLevel, report.Title, permission.Name, nil
  190. case models.SourceETA:
  191. var permissionIds []int
  192. permissionIds, err = models.GetByPermissionIdsByClassifyId(report.ClassifyId)
  193. if err != nil {
  194. utils.FileLog.Error("获取品种信息,无法确认风险等级", err.Error())
  195. return
  196. }
  197. var permissions []models.Permission
  198. var permissionNameArr []string
  199. permissions, err = models.GetPermissionsWithRiskLevel(permissionIds)
  200. for _, permission := range permissions {
  201. permissionNameArr = append(permissionNameArr, permission.Name)
  202. }
  203. permissionName = strings.Join(permissionNameArr, ",")
  204. if err != nil {
  205. utils.FileLog.Error("获取品种信息,无法确认风险等级", err.Error())
  206. return
  207. }
  208. var riskNum int
  209. for _, permission := range permissions {
  210. if permission.RiskLevel == "" {
  211. continue
  212. }
  213. var parseErr error
  214. currentNum, parseErr := strconv.Atoi(permission.RiskLevel[1:])
  215. if parseErr != nil {
  216. continue
  217. }
  218. if currentNum > riskNum {
  219. riskNum = currentNum
  220. }
  221. }
  222. if riskNum > 0 {
  223. return fmt.Sprintf("R%d", riskNum), report.Title, permissionName, nil
  224. }
  225. return
  226. }
  227. case "media":
  228. var media *models.Media
  229. media, err = models.GetMediaById("", id)
  230. if err != nil {
  231. utils.FileLog.Error("获取媒体信息,无法确认风险等级", err.Error())
  232. return
  233. }
  234. var permissionsIds []int
  235. permissionsIds, err = models.GetPermissionIdsByMediaId(media.MediaType, id)
  236. if err != nil {
  237. utils.FileLog.Error("获取媒体信息,无法确认风险等级", err.Error())
  238. return
  239. }
  240. var permissions []models.Permission
  241. var permissionNameArr []string
  242. permissions, err = models.GetPermissionsWithRiskLevel(permissionsIds)
  243. for _, permission := range permissions {
  244. permissionNameArr = append(permissionNameArr, permission.Name)
  245. }
  246. permissionName = strings.Join(permissionNameArr, ",")
  247. if err != nil {
  248. utils.FileLog.Error("获取媒体信息,无法确认风险等级", err.Error())
  249. return
  250. }
  251. var riskNum int
  252. for _, permission := range permissions {
  253. if permission.RiskLevel == "" {
  254. continue
  255. }
  256. var parseErr error
  257. currentNum, parseErr := strconv.Atoi(permission.RiskLevel[1:])
  258. if parseErr != nil {
  259. continue
  260. }
  261. if currentNum > riskNum {
  262. riskNum = currentNum
  263. }
  264. }
  265. if riskNum > 0 {
  266. return fmt.Sprintf("R%d", riskNum), media.MediaName, permissionName, nil
  267. }
  268. return
  269. case "package":
  270. var permission *models.Permission
  271. permission, err = models.GetPermissionByPermissionId(id)
  272. if err != nil {
  273. utils.FileLog.Error("获取套餐品种信息,无法确认风险等级", err.Error())
  274. return
  275. }
  276. if permission.RiskLevel == "" {
  277. return "", "", "", errors.New("当前品种未设置风测等级")
  278. }
  279. return permission.RiskLevel, permission.Name, "", nil
  280. default:
  281. var media *models.Media
  282. media, err = models.GetMediaById(models.MediaType(productType), id)
  283. if err != nil {
  284. utils.FileLog.Error("获取媒体信息,无法确认风险等级", err.Error())
  285. return
  286. }
  287. var permissionsIds []int
  288. permissionsIds, err = models.GetPermissionIdsByMediaId(media.MediaType, id)
  289. if err != nil {
  290. utils.FileLog.Error("获取媒体信息,无法确认风险等级", err.Error())
  291. return
  292. }
  293. var permissions []models.Permission
  294. permissions, err = models.GetPermissionsWithRiskLevel(permissionsIds)
  295. if err != nil {
  296. utils.FileLog.Error("获取媒体信息,无法确认风险等级", err.Error())
  297. return
  298. }
  299. var riskNum int
  300. var permissionNameArr []string
  301. for _, permission := range permissions {
  302. permissionNameArr = append(permissionNameArr, permission.Name)
  303. if permission.RiskLevel == "" {
  304. continue
  305. }
  306. var parseErr error
  307. currentNum, parseErr := strconv.Atoi(permission.RiskLevel[1:])
  308. if parseErr != nil {
  309. continue
  310. }
  311. if currentNum > riskNum {
  312. riskNum = currentNum
  313. }
  314. }
  315. permissionName = strings.Join(permissionNameArr, ",")
  316. if riskNum > 0 {
  317. return fmt.Sprintf("R%d", riskNum), media.MediaName, permissionName, nil
  318. }
  319. return
  320. }
  321. return
  322. }