123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448 |
- package services
- import (
- "errors"
- "fmt"
- "hongze/hongze_clpt/models"
- "hongze/hongze_clpt/models/ficc_report"
- "hongze/hongze_clpt/utils"
- "html"
- "strings"
- )
- // GetMinClassify
- // @Description: 获取最小分类ID
- // @author: Roc
- // @datetime 2024-06-20 09:23:19
- // @param reportInfo *models.Report
- // @return minClassifyId int
- // @return minClassifyName string
- // @return err error
- func GetMinClassify(reportInfo *ficc_report.Report) (minClassifyId int, minClassifyName string, err error) {
- defer func() {
- if err != nil {
- go utils.SendAlarmMsg(fmt.Sprint("获取最小分类ID失败,报告ID:%d,Err:%s", reportInfo.Id, err.Error()), 2)
- }
- }()
- minClassifyId = reportInfo.ClassifyIdThird
- minClassifyName = reportInfo.ClassifyNameThird
- if minClassifyId <= 0 {
- minClassifyId = reportInfo.ClassifyIdSecond
- minClassifyName = reportInfo.ClassifyNameSecond
- }
- if minClassifyId <= 0 {
- minClassifyId = reportInfo.ClassifyIdFirst
- minClassifyName = reportInfo.ClassifyNameFirst
- }
- if minClassifyId <= 0 {
- err = errors.New("分类异常")
- }
- return
- }
- // CheckWeekReportPermission
- // @Description: 验证周报的权限(并获取拥有权限的章节id列表)
- // @author: Roc
- // @datetime 2024-06-24 11:06:52
- // @param userInfo user.UserInfo
- // @param reportId int
- // @param productAuthOk bool
- // @return authOk bool
- // @return permissionCheckInfo response.PermissionCheckInfo
- // @return validTypeIds []int 分类关联的章节类型ID列表
- // @return reportChapterIdList []int 并获取拥有权限的章节id列表
- // @return err error
- func CheckWeekReportPermission(userInfo *models.WxUserItem, reportId int, productAuthOk bool) (authOk bool, permissionCheckInfo ficc_report.PermissionCheckInfo, validTypeIds, reportChapterIdList []int, err error) {
- var permissionIds []int
- //var validPermissionIds []int //最后允许显示的章节
- // 当前报告的品种与章节列表的map
- permissionChapterList := make(map[int][]int)
- permissionIdMap := make(map[int]bool)
- typeIdMap := make(map[int]bool)
- reportChapterIdMap := make(map[int]bool)
- if productAuthOk {
- reportChapterMappingList, e := ficc_report.GetReportChapterPermissionMappingItemListByReportId(reportId)
- if e != nil && e.Error() != utils.ErrNoRow() {
- err = errors.New(e.Error())
- return
- }
- for _, v := range reportChapterMappingList {
- if _, ok := permissionIdMap[v.ChartPermissionId]; !ok {
- permissionIdMap[v.ChartPermissionId] = true
- permissionIds = append(permissionIds, v.ChartPermissionId)
- }
- if _, ok := typeIdMap[v.TypeId]; !ok {
- typeIdMap[v.TypeId] = true
- validTypeIds = append(validTypeIds, v.TypeId)
- }
- tmpList, ok := permissionChapterList[v.ChartPermissionId]
- if !ok {
- tmpList = make([]int, 0)
- }
- permissionChapterList[v.ChartPermissionId] = append(tmpList, v.ReportChapterId)
- if _, ok := reportChapterIdMap[v.ReportChapterId]; !ok {
- reportChapterIdMap[v.ReportChapterId] = true
- reportChapterIdList = append(reportChapterIdList, v.ReportChapterId) //走权益的校验权限,到这里的权限都可以用
- }
- }
- }
- return
- }
- // GetChapterListByReportChapterIdList
- // @Description: 根据报告获取章节列表
- // @author: Roc
- // @datetime 2024-06-24 11:23:36
- // @param classifyNameFirst string
- // @param reportId int
- // @param reportChapterIdList []int
- // @param reportCreateTime time.Time
- // @return reportTypeList response.ReportChapterList
- // @return err error
- func GetChapterListByReportChapterIdList(classifyNameFirst string, reportId int, reportChapterIdList []int) (reportTypeList ficc_report.ReportChapterList, err error) {
- var errMsg string
- defer func() {
- if err != nil {
- go utils.SendAlarmMsg(fmt.Sprintf("GetChapterListByReport: err:%s, errMsg:%s", err.Error(), errMsg), 2)
- }
- }()
- //查询有效的章节
- typeList, e := ficc_report.GetEffectTypes()
- if e != nil {
- err = errors.New("章节类型查询出错" + e.Error())
- return
- }
- if len(typeList) == 0 {
- err = errors.New("无有效的章节")
- return
- }
- typeMap := make(map[uint64]*ficc_report.ReportChapterType)
- for _, v := range typeList {
- typeMap[v.ReportChapterTypeId] = v
- }
- var chapterList []*ficc_report.ReportChapter
- if len(reportChapterIdList) > 0 {
- //获取所有当前研报有权限的章节
- chapterList, e = ficc_report.GetListByChapterIds(reportChapterIdList)
- } else {
- // 获取所有报告章节
- chapterList, e = ficc_report.GetListByReportId(reportId, classifyNameFirst)
- }
- if e != nil && e.Error() != utils.ErrNoRow() {
- err = errors.New("章节查询出错" + e.Error())
- return
- }
- if len(chapterList) == 0 {
- err = errors.New("无有效章节")
- return
- }
- for _, item := range chapterList {
- typeItem, ok1 := typeMap[uint64(item.TypeId)]
- // 如果是配置的章节,那么就需要判断是否禁用,如果禁用,则不展示
- if item.TypeId > 0 && !ok1 {
- continue
- }
- temp := new(ficc_report.ReportChapterListItem)
- temp.ReportChapterId = item.ReportChapterId
- temp.TypeId = item.TypeId
- temp.TypeName = item.TypeName
- temp.Title = item.Title
- temp.Trend = item.Trend
- temp.ReportId = item.ReportId
- temp.Sort = item.Sort
- temp.PublishTime = item.PublishTime
- temp.VideoUrl = item.VideoUrl
- temp.VideoName = item.VideoName
- temp.VideoPlaySeconds = item.VideoPlaySeconds
- temp.VideoSize = item.VideoSize
- temp.Content = item.Content
- // 系统配置的参数,只有配置的章节类型,才能赋值
- if typeItem != nil {
- temp.ReportChapterTypeKey = typeItem.ReportChapterTypeKey
- temp.ReportChapterTypeName = typeItem.ReportChapterTypeName
- temp.ReportChapterTypeThumb = typeItem.YbIconUrl
- }
- reportTypeList = append(reportTypeList, temp)
- }
- //if len(reportTypeList) > 0 {
- // sort.Sort(reportTypeList)
- //}
- return
- }
- // 获取报告详情
- func GetReportDetail(userinfo *models.WxUserItem, reportId int) (reportDetail ficc_report.ReportDetail, err error) {
- //var errMsg string
- defer func() {
- if err != nil {
- fmt.Println(err)
- go utils.SendAlarmMsg(fmt.Sprint("获取研报详情失败 GetFiccYbDetailByApi ,err:", err.Error(), "ReportId:", reportId), 2)
- }
- }()
- detailArticle, e := models.GetArticleDetailByReportId(reportId)
- if e != nil {
- err = errors.New("报告查询出错" + e.Error())
- return
- }
- reportInfo, e := ficc_report.GetByReportId(reportId)
- if e != nil {
- //errMsg = err.Error()
- err = errors.New("报告查询出错" + e.Error())
- return
- }
- if reportInfo.Id == 0 {
- err = errors.New("报告不存在")
- return
- }
- if reportInfo.State != 2 && reportInfo.State != 6 {
- err = errors.New("报告未发布")
- return
- }
- // 获取最小分类
- minClassifyId, _, err := GetMinClassify(reportInfo)
- // 判断报告是否属于专栏报告
- firstClassify, e := ficc_report.GetByClassifyId(reportInfo.ClassifyIdFirst)
- if e != nil {
- err = errors.New("报告一级分类有误")
- return
- }
- // 最小分类
- var minClassify *ficc_report.Classify
- if reportInfo.ClassifyIdFirst == minClassifyId {
- minClassify = firstClassify
- } else {
- minClassify, e = ficc_report.GetByClassifyId(minClassifyId)
- if e != nil {
- err = errors.New("报告最小层级分类有误")
- return
- }
- }
- var hasPermission int
- var hasPower bool
- if userinfo.CompanyId > 1 {
- companyPermission, e := models.GetCompanyPermission(userinfo.CompanyId)
- if e != nil {
- err = errors.New("GetCompanyPermission")
- return
- }
- if companyPermission != "" {
- slice := strings.Split(companyPermission, ",")
- if utils.InArrayByStr(slice, "周期") {
- hasPower = true
- }
- }
- }
- if hasPower {
- hasPermission = 1
- } else {
- //hasPermission, e = GetUserPermissionCode(userinfo.UserId, userinfo.CompanyId)
- //if e != nil {
- // err = errors.New("GetUserPermissionCode")
- // return
- //}
- reportDetail.SellerName, reportDetail.SellerMobile, _ = GetSellerName(userinfo)
- applyCount, e := models.GetApplyRecordCount(userinfo.UserId)
- if e != nil {
- err = errors.New("GetApplyRecordCount")
- return
- }
- if userinfo.CompanyId > 1 {
- companyPermission, e := models.GetCompanyPermission(userinfo.CompanyId)
- if e != nil {
- err = errors.New("GetCompanyPermission")
- return
- }
- if companyPermission == "" {
- if applyCount > 0 {
- hasPermission = 6
- } else {
- hasPermission = 2
- }
- } else {
- if applyCount == 0 {
- hasPermission = 4
- } else {
- hasPermission = 3
- }
- }
- } else { //潜在客户
- if applyCount > 0 {
- hasPermission = 6
- } else {
- hasPermission = 5
- }
- }
- }
- reportDetail.HasPermission = hasPermission
- //判断权限
- var authOk bool
- var reportChapterIdList []int
- if reportInfo.HasChapter == 1 {
- if reportInfo.ClassifyNameFirst == "晨报" {
- //authOk, permissionCheckInfo, err = CheckDayReportPermission(userinfo, productAuthOk)
- } else {
- _, _, _, reportChapterIdList, err = CheckWeekReportPermission(userinfo, reportId, true)
- }
- }
- if hasPermission == 1 {
- authOk = true
- }
- reportItem := new(ficc_report.ReportItem)
- reportItem.ReportId = reportInfo.Id
- reportItem.Title = reportInfo.Title
- reportItem.PublishTime = reportInfo.PublishTime
- reportItem.ClassifyNameFirst = reportInfo.ClassifyNameFirst
- reportItem.ClassifyNameSecond = reportInfo.ClassifyNameSecond
- reportItem.Stage = reportInfo.Stage
- reportItem.Abstract = reportInfo.Abstract
- reportItem.ContentSub = html.UnescapeString(reportInfo.ContentSub)
- reportItem.Frequency = reportInfo.Frequency
- reportItem.VideoName = reportInfo.VideoName
- reportItem.HasChapter = reportInfo.HasChapter
- reportItem.ReportLayout = reportInfo.ReportLayout
- reportItem.HeadImg = reportInfo.HeadImg
- reportItem.EndImg = reportInfo.EndImg
- reportItem.CanvasColor = reportInfo.CanvasColor
- reportItem.ArticleId = detailArticle.ArticleId
- reportItem.Disclaimer = GetConfigCodeDisclaimer()
- if reportInfo.ClassifyNameFirst == "晨会纪要" && reportInfo.ClassifyNameSecond == "晨会纪要" {
- reportItem.Title = "FICC/周期品晨会纪要"
- }
- //版头版尾样式
- {
- if reportInfo.HeadResourceId > 0 {
- headResource, tmpErr := ficc_report.GetResourceItemById(reportInfo.HeadResourceId)
- if tmpErr != nil {
- err = tmpErr
- return
- }
- reportItem.HeadImg = headResource.ImgURL
- reportItem.HeadStyle = headResource.Style
- }
- if reportInfo.EndResourceId > 0 {
- endResource, tmpErr := ficc_report.GetResourceItemById(reportInfo.EndResourceId)
- if tmpErr != nil {
- err = tmpErr
- return
- }
- reportItem.EndImg = endResource.ImgURL
- reportItem.EndStyle = endResource.Style
- }
- }
- if reportInfo.VideoName == "" && reportInfo.VideoUrl != "" {
- reportItem.VideoName = reportInfo.Title
- }
- reportItem.VideoSize = reportInfo.VideoSize
- reportItem.VideoPlaySeconds = reportInfo.VideoPlaySeconds
- reportItem.Author = reportInfo.Author
- // 分享背景图取二级分类配图, 二级没有配图时使用一级配图, 一级也没有使用默认图
- reportItem.ShareBgImg = utils.DEFAULT_REPORT_SHARE_BG_IMG
- secondClassify, e := ficc_report.GetByClassifyId(reportInfo.ClassifyIdSecond)
- if e != nil {
- err = errors.New("报告二级分类有误")
- return
- }
- if secondClassify.YbShareBgImg != "" {
- reportItem.ShareBgImg = secondClassify.YbShareBgImg
- } else {
- if firstClassify.YbShareBgImg != "" {
- reportItem.ShareBgImg = firstClassify.YbShareBgImg
- }
- }
- var reportTypeList []*ficc_report.ReportChapterListItem
- if reportInfo.HasChapter == 1 {
- //(晨报和周报的banner图)
- if reportInfo.ClassifyNameFirst == "晨报" {
- reportItem.BannerUrl = utils.ALIYUN_YBIMG_HOST + "report_banner_day.jpg"
- } else {
- reportItem.BannerUrl = utils.ALIYUN_YBIMG_HOST + "report_banner_week.jpg"
- }
- // 如果还没有配置banner图,则取晨报的
- if reportItem.BannerUrl == `` {
- reportItem.BannerUrl = utils.ALIYUN_YBIMG_HOST + "report_banner_day.jpg"
- }
- if authOk {
- reportTypeList, err = GetChapterListByReportChapterIdList(reportInfo.ClassifyNameFirst, reportInfo.Id, reportChapterIdList)
- if err != nil {
- return
- }
- }
- } else {
- // 音频播放条图片用分类图片
- //reportItem.VideoImg = utils.HZ_DEFAULT_AVATAR
- //permissionIds, tmpErr := chart_permission_search_key_word_mapping.GetChartPermissionIdsByKeyWord(reportInfo.ClassifyIdSecond)
- //if tmpErr != nil {
- // errMsg = tmpErr.Error()
- // err = errors.New("查询报告权限失败")
- // return
- //}
- //if len(permissionIds) > 0 {
- // chartPermission, tmpErr := chart_permission.GetListByIds(permissionIds)
- // if tmpErr != nil {
- // errMsg = tmpErr.Error()
- // err = errors.New("查询品种信息失败")
- // return
- // }
- // lenChart := len(chartPermission)
- // for i := 0; i < lenChart; i++ {
- // if chartPermission[i].YbImgUrl != "" {
- // reportItem.VideoImg = utils.ALIYUN_YBIMG_HOST + chartPermission[i].YbImgUrl
- // break
- // }
- // }
- //}
- }
- //如果有权限则展示content
- if authOk {
- reportItem.Content = html.UnescapeString(reportInfo.Content)
- reportItem.VideoUrl = reportInfo.VideoUrl
- }
- reportDetail.ReportInfo = reportItem
- reportDetail.ReportChapterList = reportTypeList
- //reportDetail.PermissionCheck = &permissionCheckInfo
- reportDetail.AuthOk = authOk
- //reportDetail.LikeNum = likeNum
- //reportDetail.LikeEnabled = likeEnabled
- reportDetail.ReportShowType = int(firstClassify.ShowType)
- reportDetail.ReportDetailShowType = int(minClassify.ReportDetailShowType)
- // 如果分类配置是列表展示,那么就移除content内容
- if minClassify.ReportDetailShowType == 2 {
- for _, v := range reportTypeList {
- v.Content = ``
- }
- } else {
- for _, v := range reportTypeList {
- v.Content = html.UnescapeString(v.Content)
- }
- }
- return
- }
|