classify.go 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425
  1. package report
  2. import (
  3. "errors"
  4. "fmt"
  5. "hongze/hongze_yb/global"
  6. "hongze/hongze_yb/models/response"
  7. "hongze/hongze_yb/models/tables/chart_permission_search_key_word_mapping"
  8. "hongze/hongze_yb/models/tables/company_product"
  9. "hongze/hongze_yb/models/tables/rddp/classify"
  10. "hongze/hongze_yb/models/tables/rddp/report"
  11. "hongze/hongze_yb/services/company"
  12. "hongze/hongze_yb/services/user"
  13. "hongze/hongze_yb/utils"
  14. "sort"
  15. "strings"
  16. )
  17. // GetClassListByClassifyId 专栏详情:查询二级分类列表
  18. func GetClassListByClassifyId(user user.UserInfo, classifyIdFirst int) (list []*response.ClassifyListItem, err error) {
  19. var errMsg string
  20. defer func() {
  21. if err != nil {
  22. global.LOG.Critical(fmt.Sprintf("GetClassListByClassifyName: userId=%d, err:%s, errMsg:%s", user.UserID, err.Error(), errMsg))
  23. }
  24. }()
  25. classifyInfo, err := classify.GetByClassifyId(classifyIdFirst)
  26. if err != nil {
  27. errMsg = err.Error()
  28. err = errors.New("分类查询出错")
  29. return
  30. }
  31. if classifyInfo.Id == 0 {
  32. err = errors.New("分类不存在")
  33. return
  34. }
  35. if classifyInfo.ParentId != 0 {
  36. err = errors.New("不允许查询二级分类")
  37. return
  38. }
  39. classifyList, err := classify.GetListByPid(classifyInfo.Id)
  40. if err != nil {
  41. errMsg = err.Error()
  42. err = errors.New("二级分类查询出错")
  43. return
  44. }
  45. var secondIds []int
  46. for _, v := range classifyList {
  47. secondIds = append(secondIds, v.Id)
  48. }
  49. //查询二级分类中最新的报告ID
  50. reportList, err := report.GetLatestReportsByClassifyIdFirst(classifyIdFirst, secondIds)
  51. if err != nil && err != utils.ErrNoRow {
  52. errMsg = err.Error()
  53. err = errors.New("报告查询出错")
  54. return
  55. }
  56. reportMap := make(map[int]*report.Report)
  57. for _, v := range reportList {
  58. reportMap[v.ClassifyIdSecond] = v
  59. }
  60. for _, item := range classifyList {
  61. temp := new(response.ClassifyListItem)
  62. temp.ClassifyIdSecond = item.Id
  63. temp.ParentId = item.ParentId
  64. temp.AuthorDescript = item.AuthorDescript
  65. temp.Abstract = item.Abstract
  66. temp.ReportAuthor = item.ReportAuthor
  67. temp.HomeImgUrl = item.AvatarImgUrl // 此处图片用二级分类的"头像"
  68. temp.ClassifyNameSecond = item.ClassifyName
  69. temp.VipTitle = item.VipTitle
  70. if classifyInfo.ClassifyName == "权益研报" {
  71. temp.ProductName = "权益"
  72. } else {
  73. temp.ProductName = "FICC"
  74. }
  75. // 专栏下无报告则不展示
  76. if _, ok := reportMap[item.Id]; ok {
  77. list = append(list, temp)
  78. temp.Stage = reportMap[item.Id].Stage
  79. }
  80. }
  81. return
  82. }
  83. func GetClassifyDetail(user user.UserInfo, classifyIdSecond int) (detail *response.ClassifyDetail, err error) {
  84. var errMsg string
  85. defer func() {
  86. if err != nil {
  87. global.LOG.Critical(fmt.Sprintf("GetClassifyDetail: userId=%d, err:%s, errMsg:%s", user.UserID, err.Error(), errMsg))
  88. }
  89. }()
  90. classifyInfo, err := classify.GetByClassifyId(classifyIdSecond)
  91. if err != nil {
  92. errMsg = err.Error()
  93. err = errors.New("分类查询出错")
  94. return
  95. }
  96. if classifyInfo.Id == 0 {
  97. err = errors.New("分类不存在")
  98. return
  99. }
  100. if classifyInfo.ParentId == 0 {
  101. err = errors.New("只允许查询二级分类")
  102. return
  103. }
  104. //查询权限, 查询当前分类下的最新的报告,
  105. permissionIds, err := chart_permission_search_key_word_mapping.GetChartPermissionIdsByKeyWord(classifyIdSecond)
  106. if err != nil {
  107. errMsg = err.Error()
  108. err = errors.New("分类权限查询出错")
  109. return
  110. }
  111. authOk, permissionCheckInfo, _, err := company.GetCheckPermission(user.CompanyID, int(user.UserID), permissionIds)
  112. if err != nil && err != utils.ErrNoRow {
  113. errMsg = err.Error()
  114. err = errors.New("权限查询出错")
  115. return
  116. }
  117. detail = new(response.ClassifyDetail)
  118. detail.ClassifyIdSecond = classifyInfo.Id
  119. detail.ParentId = classifyInfo.ParentId
  120. detail.AuthorDescript = classifyInfo.AuthorDescript
  121. detail.ReportAuthor = classifyInfo.ReportAuthor
  122. detail.AvatarImgUrl = classifyInfo.AvatarImgUrl
  123. detail.ClassifyNameSecond = classifyInfo.ClassifyName
  124. detail.Abstract = classifyInfo.Abstract
  125. detail.Descript = classifyInfo.Descript
  126. detail.VipTitle = classifyInfo.VipTitle
  127. detail.PermissionCheck = &permissionCheckInfo
  128. detail.AuthOk = authOk
  129. return
  130. }
  131. // GetClassifyReportList 查询二级分类下的报告列表
  132. func GetClassifyReportList(user user.UserInfo, classifyIdSecond int, pageIndex, pageSize int) (list *response.ClassReportList, err error) {
  133. var errMsg string
  134. defer func() {
  135. if err != nil {
  136. global.LOG.Critical(fmt.Sprintf("GetClassifyDetail: userId=%d, err:%s, errMsg:%s", user.UserID, err.Error(), errMsg))
  137. }
  138. }()
  139. // 判断用户状态是否是正常和永续和试用
  140. var productAuthOk bool
  141. companyProduct, err := company_product.GetByCompany2ProductId(user.CompanyID, 1)
  142. if err == utils.ErrNoRow {
  143. err = nil
  144. }
  145. if err != nil {
  146. errMsg = err.Error()
  147. err = errors.New("查询用户购买产品出错")
  148. return
  149. }
  150. if companyProduct != nil {
  151. // 无FICC权限的客户不可见
  152. if companyProduct.CompanyProductID > 0 {
  153. // 已购或者试用用户可见
  154. if strings.Contains("永续,正式", companyProduct.Status) || (companyProduct.Status == "试用" && companyProduct.IsSuspend != 1) {
  155. productAuthOk = true
  156. }
  157. }
  158. }
  159. classifyInfo, err := classify.GetByClassifyId(classifyIdSecond)
  160. if err != nil {
  161. errMsg = err.Error()
  162. err = errors.New("分类查询出错")
  163. return
  164. }
  165. if classifyInfo.Id == 0 {
  166. err = errors.New("分类不存在")
  167. return
  168. }
  169. if classifyInfo.ParentId == 0 {
  170. err = errors.New("只允许查询二级分类")
  171. return
  172. }
  173. //查询权限, 查询当前分类下的最新的报告,
  174. var permissionIds []int
  175. if productAuthOk {
  176. permissionIds, err = chart_permission_search_key_word_mapping.GetChartPermissionIdsByKeyWord(classifyIdSecond)
  177. if err != nil {
  178. errMsg = err.Error()
  179. err = errors.New("分类权限查询出错")
  180. return
  181. }
  182. }
  183. authOk, permissionCheckInfo, _, err := company.GetCheckPermission(user.CompanyID, int(user.UserID), permissionIds)
  184. if err != nil && err != utils.ErrNoRow {
  185. errMsg = err.Error()
  186. err = errors.New("权限查询出错")
  187. return
  188. }
  189. //查询 report列表
  190. offset := (pageIndex - 1) * pageSize
  191. reportList, err := report.GetListByClassifyIdSecond(classifyIdSecond, offset, pageSize)
  192. if err != nil {
  193. errMsg = err.Error()
  194. err = errors.New("查询报告列表出错")
  195. return
  196. }
  197. total, err := report.GetListCountByClassifyIdSecond(classifyIdSecond)
  198. if err != nil {
  199. errMsg = err.Error()
  200. err = errors.New("查询报告总数出错")
  201. return
  202. }
  203. var newList []*response.ClassReportListItem
  204. if len(reportList) > 0 {
  205. for _, reportInfo := range reportList {
  206. reportItem := new(response.ClassReportListItem)
  207. reportItem.ReportId = reportInfo.Id
  208. reportItem.Title = reportInfo.Title
  209. reportItem.PublishTime = reportInfo.PublishTime
  210. reportItem.ClassifyNameFirst = reportInfo.ClassifyNameFirst
  211. reportItem.ClassifyIdFirst = reportInfo.ClassifyIdFirst
  212. reportItem.ClassifyIdSecond = reportInfo.ClassifyIdSecond
  213. reportItem.ClassifyNameSecond = reportInfo.ClassifyNameSecond
  214. reportItem.Stage = reportInfo.Stage
  215. reportItem.Abstract = reportInfo.Abstract
  216. if reportInfo.Abstract != "" {
  217. reportItem.Abstract = "<div style=\"overflow: hidden;white-space: nowrap;text-overflow: ellipsis;\">" + reportInfo.Abstract + "</div>"
  218. }
  219. reportItem.Author = classifyInfo.ReportAuthor
  220. reportItem.ReportImgUrl = classifyInfo.AvatarImgUrl // 此处图片用二级分类的"头像"
  221. reportItem.VideoName = reportInfo.VideoName
  222. reportItem.VideoPlaySeconds = reportInfo.VideoPlaySeconds
  223. if reportInfo.VideoUrl != "" && reportInfo.VideoName == "" {
  224. reportItem.VideoName = reportInfo.Title
  225. }
  226. if authOk {
  227. reportItem.VideoUrl = reportInfo.VideoUrl
  228. }
  229. newList = append(newList, reportItem)
  230. }
  231. }
  232. list = new(response.ClassReportList)
  233. list.Paging = response.GetPaging(pageIndex, pageSize, int(total))
  234. list.List = newList
  235. list.PermissionCheck = &permissionCheckInfo
  236. list.AuthOk = authOk
  237. return
  238. }
  239. // GetClassifySecondSimpleList 获取二级分类简写名称列表
  240. func GetClassifySecondSimpleList(user user.UserInfo, classifyIdFirst int) (list []*response.ClassifySimpleListItem, err error) {
  241. var errMsg string
  242. defer func() {
  243. if err != nil {
  244. global.LOG.Critical(fmt.Sprintf("GetClassifySecondSimpleList: userId=%d, err:%s, errMsg:%s", user.UserID, err.Error(), errMsg))
  245. }
  246. }()
  247. classifyParent, err := classify.GetByClassifyId(classifyIdFirst)
  248. if err != nil {
  249. errMsg = err.Error()
  250. err = errors.New("分类查询出错")
  251. return
  252. }
  253. if classifyParent.Id == 0 {
  254. err = errors.New("该分类不存在")
  255. return
  256. }
  257. simpleList, err := classify.GetListByPid(classifyIdFirst)
  258. if err != nil {
  259. errMsg = err.Error()
  260. err = errors.New("二级分类查询出错")
  261. return
  262. }
  263. if len(simpleList) > 0 {
  264. for _, item := range simpleList {
  265. if item.ClassifyName == classifyParent.ClassifyName && len(simpleList) == 1 {
  266. break
  267. }
  268. temp := new(response.ClassifySimpleListItem)
  269. temp.ClassifyIdSecond = item.Id
  270. temp.ClassifyNameSecond = item.ClassifyName
  271. if index := strings.Index(item.ClassifyName, "双周报"); index > 0 {
  272. temp.ClassifySecondNameSimple = item.ClassifyName[0:index]
  273. } else if item.ClassifyName == "纺织服装与商品价格" {
  274. temp.ClassifySecondNameSimple = "纺织服装"
  275. } else {
  276. temp.ClassifySecondNameSimple = item.ClassifyName
  277. }
  278. list = append(list, temp)
  279. }
  280. }
  281. return
  282. }
  283. // GetClassifyFirstList 获取一级分类列表
  284. func GetClassifyFirstList(user user.UserInfo) (resp response.ClassifyFirstList, err error) {
  285. var errMsg string
  286. defer func() {
  287. if err != nil {
  288. global.LOG.Critical(fmt.Sprintf("GetClassifyFirstList: userId=%d, err:%s, errMsg:%s", user.UserID, err.Error(), errMsg))
  289. }
  290. }()
  291. classifyParents, err := classify.GetParentList()
  292. if err != nil {
  293. errMsg = err.Error()
  294. err = errors.New("分类查询出错")
  295. return
  296. }
  297. if len(classifyParents) == 0 {
  298. err = errors.New("分类不存在")
  299. return
  300. }
  301. classifyIconMap := make(map[string]string)
  302. classifySortMap := make(map[string]int)
  303. for _, v := range classifyParents {
  304. classifyIconMap[v.ClassifyName] = v.YbFiccIcon
  305. classifySortMap[v.ClassifyName] = v.YbFiccSort
  306. }
  307. var list response.ClassifyFirstList
  308. for _, item := range classifyParents {
  309. temp := new(response.ClassifyFirstListItem)
  310. temp.ClassifyIdFirst = item.Id
  311. temp.ClassifyNameFirst = item.ClassifyName
  312. temp.IconImgUrl = classifyIconMap[item.ClassifyName]
  313. temp.Sort = classifySortMap[item.ClassifyName]
  314. //if temp.Sort == 0 {
  315. // continue
  316. //}
  317. // ShowType展示类型:1-列表 2-专栏 3-品种; RedirectType跳转类型:1-专栏列表 2-报告列表 3-专栏详情 4-品种类型列表
  318. temp.RedirectType = 2
  319. if item.ShowType == 2 {
  320. temp.RedirectType = 1
  321. classifyChild, _ := classify.GetChildByPid(item.Id)
  322. if classifyChild != nil && len(classifyChild) == 1 {
  323. // 存在二级分类且仅有一个时直接跳转专栏详情
  324. temp.ClassifyIdSecond = classifyChild[0].Id
  325. temp.ClassifyNameSecond = classifyChild[0].ClassifyName
  326. temp.RedirectType = 3
  327. }
  328. } else if item.ShowType == 3 {
  329. temp.RedirectType = 4
  330. }
  331. list = append(list, temp)
  332. }
  333. // 判断用户状态是否是正常和永续
  334. var productAuthOk bool
  335. companyProduct, err := company_product.GetByCompany2ProductId(user.CompanyID, 1)
  336. if err == utils.ErrNoRow {
  337. err = nil
  338. }
  339. if err != nil && err != utils.ErrNoRow {
  340. errMsg = err.Error()
  341. err = errors.New("查询用户购买产品出错")
  342. return
  343. }
  344. if companyProduct != nil {
  345. // 无FICC权限的客户不可见
  346. if companyProduct.CompanyProductID > 0 {
  347. // 已购或者试用用户可见
  348. if strings.Contains("永续,正式", companyProduct.Status) || (companyProduct.Status == "试用" && companyProduct.IsSuspend != 1) {
  349. productAuthOk = true
  350. }
  351. }
  352. }
  353. // 获取有效的权限id列表
  354. var validPermissionIdList []int
  355. //var allClassifySecondIds []int
  356. if productAuthOk {
  357. validPermissionIdList, err = company.GetValidPermissionIdListByCompany2ProductId(user.CompanyID, 1)
  358. if err != nil && err != utils.ErrNoRow {
  359. errMsg = err.Error()
  360. err = errors.New("查询分类出错")
  361. return
  362. }
  363. }
  364. //classifyNames, err := chart_permission_search_key_word_mapping.GetKeyWordsByChartPermissionByIds(validPermissionIdList)
  365. //if err != nil {
  366. // errMsg = err.Error()
  367. // err = errors.New("查询权限对应的分类出错")
  368. // return
  369. //}
  370. //
  371. //if len(classifyNames) > 0 {
  372. // allClassifySecondIds, err = classify.GetSecondIdsByClassifyNames(classifyNames)
  373. // if err != nil {
  374. // errMsg = err.Error()
  375. // err = errors.New("查询分类出错")
  376. // return
  377. // }
  378. //}
  379. for _, item := range list {
  380. //无权限用户关闭品种类别入口
  381. if item.RedirectType == 4 && !productAuthOk {
  382. continue
  383. }
  384. //只有宏观经济权限的用户关闭品种类别入口
  385. if len(validPermissionIdList) == 1 && validPermissionIdList[0] == 1 && item.RedirectType == 4 {
  386. continue
  387. }
  388. resp = append(resp, item)
  389. }
  390. if len(resp) > 0 {
  391. sort.Sort(resp)
  392. }
  393. return
  394. }