ficc_report.go 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544
  1. package services
  2. //FICC研报
  3. import (
  4. "errors"
  5. "fmt"
  6. "hongze/hongze_cygx/models"
  7. "hongze/hongze_cygx/models/ficc_report"
  8. "hongze/hongze_cygx/utils"
  9. "html"
  10. "strconv"
  11. "strings"
  12. "time"
  13. )
  14. // GetMinClassify
  15. // @Description: 获取最小分类ID
  16. // @author: Roc
  17. // @datetime 2024-06-20 09:23:19
  18. // @param reportInfo *models.Report
  19. // @return minClassifyId int
  20. // @return minClassifyName string
  21. // @return err error
  22. func GetMinClassify(reportInfo *ficc_report.Report) (minClassifyId int, minClassifyName string, err error) {
  23. defer func() {
  24. if err != nil {
  25. go utils.SendAlarmMsg(fmt.Sprint("获取最小分类ID失败,报告ID:%d,Err:%s", reportInfo.Id, err.Error()), 2)
  26. }
  27. }()
  28. minClassifyId = reportInfo.ClassifyIdThird
  29. minClassifyName = reportInfo.ClassifyNameThird
  30. if minClassifyId <= 0 {
  31. minClassifyId = reportInfo.ClassifyIdSecond
  32. minClassifyName = reportInfo.ClassifyNameSecond
  33. }
  34. if minClassifyId <= 0 {
  35. minClassifyId = reportInfo.ClassifyIdFirst
  36. minClassifyName = reportInfo.ClassifyNameFirst
  37. }
  38. if minClassifyId <= 0 {
  39. err = errors.New("分类异常")
  40. }
  41. return
  42. }
  43. // 获取报告详情
  44. // 获取报告详情
  45. func GetReportDetail(userinfo *models.WxUserItem, reportId int) (reportDetail ficc_report.ReportDetail, err error) {
  46. //var errMsg string
  47. defer func() {
  48. if err != nil {
  49. fmt.Println(err)
  50. go utils.SendAlarmMsg(fmt.Sprint("获取研报详情失败 GetFiccYbDetailByApi ,err:", err.Error(), "ReportId:", reportId), 2)
  51. }
  52. }()
  53. detailArticle, e := models.GetArticleDetailByReportId(reportId)
  54. if e != nil {
  55. err = errors.New("报告查询出错" + e.Error())
  56. return
  57. }
  58. reportInfo, e := ficc_report.GetByReportId(reportId)
  59. if e != nil {
  60. //errMsg = err.Error()
  61. err = errors.New("报告查询出错" + e.Error())
  62. return
  63. }
  64. if reportInfo.Id == 0 {
  65. err = errors.New("报告不存在")
  66. return
  67. }
  68. if reportInfo.State != 2 && reportInfo.State != 6 {
  69. err = errors.New("报告未发布")
  70. return
  71. }
  72. // 获取最小分类
  73. minClassifyId, _, err := GetMinClassify(reportInfo)
  74. // 判断报告是否属于专栏报告
  75. firstClassify, e := ficc_report.GetByClassifyId(reportInfo.ClassifyIdFirst)
  76. if e != nil {
  77. err = errors.New("报告一级分类有误")
  78. return
  79. }
  80. // 最小分类
  81. var minClassify *ficc_report.Classify
  82. if reportInfo.ClassifyIdFirst == minClassifyId {
  83. minClassify = firstClassify
  84. } else {
  85. minClassify, e = ficc_report.GetByClassifyId(minClassifyId)
  86. if e != nil {
  87. err = errors.New("报告最小层级分类有误")
  88. return
  89. }
  90. }
  91. //判断是否已经申请过
  92. applyCount, e := models.GetApplyRecordCount(userinfo.UserId)
  93. if e != nil {
  94. err = errors.New("判断是否已申请过试用失败")
  95. return
  96. }
  97. var hasPermission int
  98. if userinfo.CompanyId > 1 {
  99. companyPermission, e := models.GetCompanyPermission(userinfo.CompanyId)
  100. if e != nil {
  101. err = errors.New("获取用户权限信息失败")
  102. return
  103. }
  104. if companyPermission == "" {
  105. if applyCount > 0 {
  106. hasPermission = 5
  107. } else {
  108. hasPermission = 2
  109. }
  110. } else {
  111. var hasPersion bool
  112. slice := strings.Split(companyPermission, ",")
  113. if utils.InArrayByStr(slice, "周期") {
  114. hasPersion = true
  115. }
  116. if hasPersion {
  117. hasPermission = 1
  118. } else { //无该行业权限
  119. companyDetail, e := models.GetCompanyDetailById(userinfo.CompanyId)
  120. if e == nil && companyDetail.ProductId == 1 {
  121. hasPermission = 2
  122. } else {
  123. hasPermission = 3
  124. }
  125. }
  126. }
  127. //获取销售手机号
  128. sellerItem, e := models.GetSellerByCompanyId(userinfo.CompanyId)
  129. if e != nil && e.Error() != utils.ErrNoRow() {
  130. err = errors.New("获取销售数据失败")
  131. return
  132. }
  133. if sellerItem != nil {
  134. reportDetail.SellerMobile = sellerItem.Mobile
  135. reportDetail.SellerName = sellerItem.RealName
  136. }
  137. } else { //潜在客户
  138. if applyCount > 0 {
  139. hasPermission = 5
  140. } else {
  141. hasPermission = 4
  142. }
  143. }
  144. reportDetail.HasPermission = hasPermission
  145. //判断权限
  146. var authOk bool
  147. var reportChapterIdList []int
  148. if reportInfo.HasChapter == 1 {
  149. if reportInfo.ClassifyNameFirst == "晨报" {
  150. //authOk, permissionCheckInfo, err = CheckDayReportPermission(userinfo, productAuthOk)
  151. } else {
  152. _, _, _, reportChapterIdList, err = CheckWeekReportPermission(userinfo, reportId, true)
  153. }
  154. }
  155. if hasPermission == 1 {
  156. authOk = true
  157. }
  158. reportItem := new(ficc_report.ReportItem)
  159. reportItem.ReportId = reportInfo.Id
  160. reportItem.Title = reportInfo.Title
  161. reportItem.PublishTime = reportInfo.PublishTime
  162. reportItem.ClassifyNameFirst = reportInfo.ClassifyNameFirst
  163. reportItem.ClassifyNameSecond = reportInfo.ClassifyNameSecond
  164. reportItem.Stage = reportInfo.Stage
  165. reportItem.Abstract = reportInfo.Abstract
  166. reportItem.ContentSub = html.UnescapeString(reportInfo.ContentSub)
  167. reportItem.Frequency = reportInfo.Frequency
  168. reportItem.VideoName = reportInfo.VideoName
  169. reportItem.HasChapter = reportInfo.HasChapter
  170. reportItem.ReportLayout = reportInfo.ReportLayout
  171. reportItem.HeadImg = reportInfo.HeadImg
  172. reportItem.EndImg = reportInfo.EndImg
  173. reportItem.CanvasColor = reportInfo.CanvasColor
  174. reportItem.ArticleId = detailArticle.ArticleId
  175. //版头版尾样式
  176. {
  177. if reportInfo.HeadResourceId > 0 {
  178. headResource, tmpErr := ficc_report.GetResourceItemById(reportInfo.HeadResourceId)
  179. if tmpErr != nil {
  180. err = tmpErr
  181. return
  182. }
  183. reportItem.HeadImg = headResource.ImgURL
  184. reportItem.HeadStyle = headResource.Style
  185. }
  186. if reportInfo.EndResourceId > 0 {
  187. endResource, tmpErr := ficc_report.GetResourceItemById(reportInfo.EndResourceId)
  188. if tmpErr != nil {
  189. err = tmpErr
  190. return
  191. }
  192. reportItem.EndImg = endResource.ImgURL
  193. reportItem.EndStyle = endResource.Style
  194. }
  195. }
  196. if reportInfo.VideoName == "" && reportInfo.VideoUrl != "" {
  197. reportItem.VideoName = reportInfo.Title
  198. }
  199. reportItem.VideoSize = reportInfo.VideoSize
  200. reportItem.VideoPlaySeconds = reportInfo.VideoPlaySeconds
  201. reportItem.Author = reportInfo.Author
  202. // 分享背景图取二级分类配图, 二级没有配图时使用一级配图, 一级也没有使用默认图
  203. reportItem.ShareBgImg = utils.DEFAULT_REPORT_SHARE_BG_IMG
  204. secondClassify, e := ficc_report.GetByClassifyId(reportInfo.ClassifyIdSecond)
  205. if e != nil {
  206. err = errors.New("报告二级分类有误")
  207. return
  208. }
  209. if secondClassify.YbShareBgImg != "" {
  210. reportItem.ShareBgImg = secondClassify.YbShareBgImg
  211. } else {
  212. if firstClassify.YbShareBgImg != "" {
  213. reportItem.ShareBgImg = firstClassify.YbShareBgImg
  214. }
  215. }
  216. var reportTypeList []*ficc_report.ReportChapterListItem
  217. if reportInfo.HasChapter == 1 {
  218. //(晨报和周报的banner图)
  219. if reportInfo.ClassifyNameFirst == "晨报" {
  220. reportItem.BannerUrl = utils.ALIYUN_YBIMG_HOST + "report_banner_day.jpg"
  221. } else {
  222. reportItem.BannerUrl = utils.ALIYUN_YBIMG_HOST + "report_banner_week.jpg"
  223. }
  224. // 如果还没有配置banner图,则取晨报的
  225. if reportItem.BannerUrl == `` {
  226. reportItem.BannerUrl = utils.ALIYUN_YBIMG_HOST + "report_banner_day.jpg"
  227. }
  228. if authOk {
  229. reportTypeList, err = GetChapterListByReportChapterIdList(reportInfo.ClassifyNameFirst, reportInfo.Id, reportChapterIdList)
  230. if err != nil {
  231. return
  232. }
  233. }
  234. } else {
  235. // 音频播放条图片用分类图片
  236. //reportItem.VideoImg = utils.HZ_DEFAULT_AVATAR
  237. //permissionIds, tmpErr := chart_permission_search_key_word_mapping.GetChartPermissionIdsByKeyWord(reportInfo.ClassifyIdSecond)
  238. //if tmpErr != nil {
  239. // errMsg = tmpErr.Error()
  240. // err = errors.New("查询报告权限失败")
  241. // return
  242. //}
  243. //if len(permissionIds) > 0 {
  244. // chartPermission, tmpErr := chart_permission.GetListByIds(permissionIds)
  245. // if tmpErr != nil {
  246. // errMsg = tmpErr.Error()
  247. // err = errors.New("查询品种信息失败")
  248. // return
  249. // }
  250. // lenChart := len(chartPermission)
  251. // for i := 0; i < lenChart; i++ {
  252. // if chartPermission[i].YbImgUrl != "" {
  253. // reportItem.VideoImg = utils.ALIYUN_YBIMG_HOST + chartPermission[i].YbImgUrl
  254. // break
  255. // }
  256. // }
  257. //}
  258. }
  259. //如果有权限则展示content
  260. if authOk {
  261. reportItem.Content = html.UnescapeString(reportInfo.Content)
  262. reportItem.VideoUrl = reportInfo.VideoUrl
  263. }
  264. reportDetail.ReportInfo = reportItem
  265. reportDetail.ReportChapterList = reportTypeList
  266. //reportDetail.PermissionCheck = &permissionCheckInfo
  267. reportDetail.AuthOk = authOk
  268. //reportDetail.LikeNum = likeNum
  269. //reportDetail.LikeEnabled = likeEnabled
  270. reportDetail.ReportShowType = int(firstClassify.ShowType)
  271. reportDetail.ReportDetailShowType = int(minClassify.ReportDetailShowType)
  272. // 如果分类配置是列表展示,那么就移除content内容
  273. if minClassify.ReportDetailShowType == 2 {
  274. for _, v := range reportTypeList {
  275. v.Content = ``
  276. }
  277. } else {
  278. for _, v := range reportTypeList {
  279. v.Content = html.UnescapeString(v.Content)
  280. }
  281. }
  282. return
  283. }
  284. // CheckWeekReportPermission
  285. // @Description: 验证周报的权限(并获取拥有权限的章节id列表)
  286. // @author: Roc
  287. // @datetime 2024-06-24 11:06:52
  288. // @param userInfo user.UserInfo
  289. // @param reportId int
  290. // @param productAuthOk bool
  291. // @return authOk bool
  292. // @return permissionCheckInfo response.PermissionCheckInfo
  293. // @return validTypeIds []int 分类关联的章节类型ID列表
  294. // @return reportChapterIdList []int 并获取拥有权限的章节id列表
  295. // @return err error
  296. func CheckWeekReportPermission(userInfo *models.WxUserItem, reportId int, productAuthOk bool) (authOk bool, permissionCheckInfo ficc_report.PermissionCheckInfo, validTypeIds, reportChapterIdList []int, err error) {
  297. var permissionIds []int
  298. //var validPermissionIds []int //最后允许显示的章节
  299. // 当前报告的品种与章节列表的map
  300. permissionChapterList := make(map[int][]int)
  301. permissionIdMap := make(map[int]bool)
  302. typeIdMap := make(map[int]bool)
  303. reportChapterIdMap := make(map[int]bool)
  304. if productAuthOk {
  305. reportChapterMappingList, e := ficc_report.GetReportChapterPermissionMappingItemListByReportId(reportId)
  306. if e != nil && e.Error() != utils.ErrNoRow() {
  307. err = errors.New(e.Error())
  308. return
  309. }
  310. for _, v := range reportChapterMappingList {
  311. if _, ok := permissionIdMap[v.ChartPermissionId]; !ok {
  312. permissionIdMap[v.ChartPermissionId] = true
  313. permissionIds = append(permissionIds, v.ChartPermissionId)
  314. }
  315. if _, ok := typeIdMap[v.TypeId]; !ok {
  316. typeIdMap[v.TypeId] = true
  317. validTypeIds = append(validTypeIds, v.TypeId)
  318. }
  319. tmpList, ok := permissionChapterList[v.ChartPermissionId]
  320. if !ok {
  321. tmpList = make([]int, 0)
  322. }
  323. permissionChapterList[v.ChartPermissionId] = append(tmpList, v.ReportChapterId)
  324. if _, ok := reportChapterIdMap[v.ReportChapterId]; !ok {
  325. reportChapterIdMap[v.ReportChapterId] = true
  326. reportChapterIdList = append(reportChapterIdList, v.ReportChapterId) //走权益的校验权限,到这里的权限都可以用
  327. }
  328. }
  329. }
  330. return
  331. }
  332. // GetChapterListByReportChapterIdList
  333. // @Description: 根据报告获取章节列表
  334. // @author: Roc
  335. // @datetime 2024-06-24 11:23:36
  336. // @param classifyNameFirst string
  337. // @param reportId int
  338. // @param reportChapterIdList []int
  339. // @param reportCreateTime time.Time
  340. // @return reportTypeList response.ReportChapterList
  341. // @return err error
  342. func GetChapterListByReportChapterIdList(classifyNameFirst string, reportId int, reportChapterIdList []int) (reportTypeList ficc_report.ReportChapterList, err error) {
  343. var errMsg string
  344. defer func() {
  345. if err != nil {
  346. go utils.SendAlarmMsg(fmt.Sprintf("GetChapterListByReport: err:%s, errMsg:%s", err.Error(), errMsg), 2)
  347. }
  348. }()
  349. //查询有效的章节
  350. typeList, e := ficc_report.GetEffectTypes()
  351. if e != nil {
  352. err = errors.New("章节类型查询出错" + e.Error())
  353. return
  354. }
  355. if len(typeList) == 0 {
  356. err = errors.New("无有效的章节")
  357. return
  358. }
  359. typeMap := make(map[uint64]*ficc_report.ReportChapterType)
  360. for _, v := range typeList {
  361. typeMap[v.ReportChapterTypeId] = v
  362. }
  363. var chapterList []*ficc_report.ReportChapter
  364. if len(reportChapterIdList) > 0 {
  365. //获取所有当前研报有权限的章节
  366. chapterList, e = ficc_report.GetListByChapterIds(reportChapterIdList)
  367. } else {
  368. // 获取所有报告章节
  369. chapterList, e = ficc_report.GetListByReportId(reportId, classifyNameFirst)
  370. }
  371. if e != nil && e.Error() != utils.ErrNoRow() {
  372. err = errors.New("章节查询出错" + e.Error())
  373. return
  374. }
  375. if len(chapterList) == 0 {
  376. err = errors.New("无有效章节")
  377. return
  378. }
  379. for _, item := range chapterList {
  380. typeItem, ok1 := typeMap[uint64(item.TypeId)]
  381. // 如果是配置的章节,那么就需要判断是否禁用,如果禁用,则不展示
  382. if item.TypeId > 0 && !ok1 {
  383. continue
  384. }
  385. temp := new(ficc_report.ReportChapterListItem)
  386. temp.ReportChapterId = item.ReportChapterId
  387. temp.TypeId = item.TypeId
  388. temp.TypeName = item.TypeName
  389. temp.Title = item.Title
  390. temp.Trend = item.Trend
  391. temp.ReportId = item.ReportId
  392. temp.Sort = item.Sort
  393. temp.PublishTime = item.PublishTime
  394. temp.VideoUrl = item.VideoUrl
  395. temp.VideoName = item.VideoName
  396. temp.VideoPlaySeconds = item.VideoPlaySeconds
  397. temp.VideoSize = item.VideoSize
  398. temp.Content = item.Content
  399. // 系统配置的参数,只有配置的章节类型,才能赋值
  400. if typeItem != nil {
  401. temp.ReportChapterTypeKey = typeItem.ReportChapterTypeKey
  402. temp.ReportChapterTypeName = typeItem.ReportChapterTypeName
  403. temp.ReportChapterTypeThumb = typeItem.YbIconUrl
  404. }
  405. reportTypeList = append(reportTypeList, temp)
  406. }
  407. //if len(reportTypeList) > 0 {
  408. // sort.Sort(reportTypeList)
  409. //}
  410. return
  411. }
  412. //func init() {
  413. // //GetFiccRreportToCygxArticle()
  414. // UpdateFICCReportResourceData(5542)
  415. //}
  416. // 获取FICC研报到查研观向数据库中
  417. func GetFiccRreportToCygxArticle() (err error) {
  418. defer func() {
  419. if err != nil {
  420. fmt.Println(err)
  421. go utils.SendAlarmMsg(fmt.Sprint("自动取消十分钟之内没有付款的的订单 CancelCygxOrder ,err:", err.Error()), 2)
  422. }
  423. }()
  424. var condition string
  425. var pars []interface{}
  426. //modifyTime := time.Now().Add(-10 * time.Minute)
  427. //condition = ` AND modify_time >= ? `
  428. //pars = append(pars, modifyTime)
  429. reportList, e := ficc_report.GetFiccRreportToCygxArticle(condition, pars)
  430. if e != nil {
  431. err = errors.New("GetFiccRreportToCygxArticle, Err: " + e.Error())
  432. return
  433. }
  434. fmt.Println(len(reportList))
  435. if len(reportList) == 0 {
  436. return
  437. }
  438. var reportIds []int
  439. for _, v := range reportList {
  440. reportIds = append(reportIds, v.Id)
  441. }
  442. listaFiccReport, e := models.GetCygxCygxArticleListByReportIds(reportIds)
  443. if e != nil {
  444. err = errors.New("GetCygxCygxArticleListByReportIds, Err: " + e.Error())
  445. return
  446. }
  447. mapFiccReport := make(map[int]bool)
  448. for _, v := range listaFiccReport {
  449. mapFiccReport[v.IsReport] = true
  450. }
  451. //量不大,先这么写吧
  452. for _, v := range reportList {
  453. time.Sleep(1 * time.Millisecond) // 延时一秒
  454. item := new(models.CygxArticle)
  455. item.ReportId = v.Id
  456. item.PublishDate = v.PublishTime.Format(utils.FormatDateTime)
  457. item.Title = v.Title
  458. item.Body = v.Content
  459. item.Abstract = v.Abstract
  460. item.PublishStatus = 1
  461. if (v.ClassifyNameFirst == "宏观报告" && v.ClassifyNameSecond == "大类资产周报") || (v.ClassifyNameFirst == "晨会纪要" && v.ClassifyNameSecond == "晨会纪要") {
  462. item.SubCategoryName = "产业跟踪"
  463. item.MatchTypeName = "产业跟踪"
  464. if utils.RunMode == "release" {
  465. item.CategoryId = 10091
  466. item.CategoryIdTwo = 10091
  467. } else {
  468. //item.CategoryId = 10091
  469. //item.CategoryIdTwo = 10091
  470. }
  471. } else if v.ClassifyNameFirst == "调研纪要" && v.ClassifyNameSecond == "调研纪要" {
  472. item.SubCategoryName = "调研纪要"
  473. item.MatchTypeName = "调研纪要"
  474. if utils.RunMode == "release" {
  475. item.CategoryId = 10090
  476. item.CategoryIdTwo = 10090
  477. } else {
  478. //item.CategoryId = 10091
  479. //item.CategoryIdTwo = 10091
  480. }
  481. } else {
  482. continue
  483. }
  484. var articleIdMax int
  485. maxArticleIdArticleInfo, e := models.GetMaxArticleIdInfo()
  486. if e != nil {
  487. err = errors.New("GetMaxArticleIdInfo, Err: " + e.Error())
  488. return
  489. }
  490. articleIdMax = maxArticleIdArticleInfo.ArticleId + 1
  491. item.ArticleIdMd5 = utils.MD5(strconv.Itoa(articleIdMax))
  492. item.ArticleId = articleIdMax
  493. //如果不存在就新增
  494. if !mapFiccReport[v.Id] {
  495. _, e = models.AddCygxArticles(item)
  496. if e != nil {
  497. err = errors.New("AddCygxArticles, Err: " + e.Error())
  498. return
  499. }
  500. }
  501. UpdateFICCReportResourceData(v.Id)
  502. fmt.Println(v)
  503. }
  504. return
  505. }