package models import ( "eta_gn/eta_report/global" "eta_gn/eta_report/utils" "fmt" "strings" "time" ) // 报告状态 const ( ReportStateUnpublished = 1 // 未发布 ReportStatePublished = 2 // 已发布 ReportStateWaitSubmit = 3 // 待提交 ReportStateWaitApprove = 4 // 审批中 ReportStateRefused = 5 // 已驳回 ReportStatePass = 6 // 已通过 ) //type Report struct { // Id int `gorm:"primaryKey;autoIncrement;column:id"` // AddType int `gorm:"column:add_type" description:"新增方式:1:新增报告,2:继承报告"` // ClassifyIdFirst int `gorm:"column:classify_id_first" description:"一级分类id"` // ClassifyNameFirst string `gorm:"column:classify_name_first" description:"一级分类名称"` // ClassifyIdSecond int `gorm:"column:classify_id_second" description:"二级分类id"` // ClassifyNameSecond string `gorm:"column:classify_name_second" description:"二级分类名称"` // Title string `gorm:"column:title" description:"标题"` // Abstract string `gorm:"column:abstract" description:"摘要"` // Author string `gorm:"column:author" description:"作者"` // Frequency string `gorm:"column:frequency" description:"频度"` // CreateTime string `gorm:"column:create_time" description:"创建时间"` // ModifyTime string `gorm:"column:modify_time" description:"修改时间"` // State int `gorm:"column:state" description:"1:未发布,2:已发布"` // PublishTime string `gorm:"column:publish_time" description:"发布时间"` // PrePublishTime string `gorm:"column:pre_publish_time" description:"预发布时间"` // Stage int `gorm:"column:stage" description:"期数"` // MsgIsSend int `gorm:"column:msg_is_send" description:"消息是否已发送,0:否,1:是"` // Content string `gorm:"column:content" description:"内容"` // VideoUrl string `gorm:"column:video_url" description:"音频文件URL"` // VideoName string `gorm:"column:video_name" description:"音频文件名称"` // VideoPlaySeconds string `gorm:"column:video_play_seconds" description:"音频播放时长"` // VideoSize string `gorm:"column:video_size" description:"音频文件大小,单位M"` // ContentSub string `gorm:"column:content_sub" description:"内容前两个章节"` // IsShowNewLabel int `gorm:"column:is_show_new_label" description:"是否显示新标签"` // IsCurrentDate int `gorm:"column:is_current_date" description:"是否当前日期"` // ClassifyName string `gorm:"column:classify_name" description:"分类名称"` // HasChapter int `gorm:"column:has_chapter" description:"是否有章节"` // ContentStruct string `gorm:"column:content_struct" description:"内容组件"` // LastModifyAdminId int `gorm:"column:last_modify_admin_id" description:"最后更新人ID"` // LastModifyAdminName string `gorm:"column:last_modify_admin_name" description:"最后更新人姓名"` // ContentModifyTime string `gorm:"column:content_modify_time" description:"内容更新时间"` // Pv int `gorm:"column:pv" description:"pv"` // Uv int `gorm:"column:uv" description:"uv"` // HeadImg string `gorm:"column:head_img" description:"报告头图地址"` // EndImg string `gorm:"column:end_img" description:"报告尾图地址"` // HeadStyle string `gorm:"column:head_style" description:"版头样式"` // EndStyle string `gorm:"column:end_style" description:"版尾样式"` // CanvasColor string `gorm:"column:canvas_color" description:"画布颜色"` // NeedSplice int `gorm:"column:need_splice" description:"是否拼接版头版位的标记,主要是为了兼容历史报告。0-不需要 1-需要"` // HeadResourceId int `gorm:"column:head_resource_id" description:"版头资源ID"` // EndResourceId int `gorm:"column:end_resource_id" description:"版尾资源ID"` // ClassifyIdThird int `gorm:"column:classify_id_third" description:"三级分类id"` // ClassifyNameThird string `gorm:"column:classify_name_third" description:"三级分类名称"` // CollaborateType int8 `gorm:"column:collaborate_type" description:"协作方式,1:个人,2:多人协作。默认:1"` // ReportLayout int8 `gorm:"column:report_layout" description:"报告布局,1:常规布局,2:智能布局。默认:1"` // IsPublicPublish int8 `gorm:"column:is_public_publish" description:"是否公开发布,1:是,2:否"` // ReportCreateTime string `gorm:"column:report_create_time" description:"报告时间创建时间"` //} type Report struct { Id int `gorm:"column:id;primaryKey;autoIncrement" description:"报告Id"` AddType int `gorm:"column:add_type" description:"新增方式:1:新增报告,2:继承报告"` ClassifyIdFirst int `gorm:"column:classify_id_first" description:"一级分类id"` ClassifyNameFirst string `gorm:"column:classify_name_first" description:"一级分类名称"` ClassifyIdSecond int `gorm:"column:classify_id_second" description:"二级分类id"` ClassifyNameSecond string `gorm:"column:classify_name_second" description:"二级分类名称"` Title string `gorm:"column:title" description:"标题"` Abstract string `gorm:"column:abstract" description:"摘要"` Author string `gorm:"column:author" description:"作者"` Frequency string `gorm:"column:frequency" description:"频度"` CreateTime time.Time `gorm:"column:create_time" description:"创建时间"` ModifyTime time.Time `gorm:"column:modify_time;autoUpdateTime" description:"修改时间"` State int `gorm:"column:state" description:"1:未发布;2:已发布;3-待提交;4-待审批;5-已驳回;6-已通过"` PublishTime time.Time `gorm:"column:publish_time" description:"发布时间"` Stage int `gorm:"column:stage" description:"期数"` MsgIsSend int `gorm:"column:msg_is_send" description:"消息是否已发送,0:否,1:是"` ThsMsgIsSend int `gorm:"column:ths_msg_is_send" description:"客户群消息是否已发送,0:否,1:是"` Content string `gorm:"column:content" description:"内容"` VideoUrl string `gorm:"column:video_url" description:"音频文件URL"` VideoName string `gorm:"column:video_name" description:"音频文件名称"` VideoPlaySeconds string `gorm:"column:video_play_seconds" description:"音频播放时长"` VideoSize string `gorm:"column:video_size" description:"音频文件大小,单位M"` ContentSub string `gorm:"column:content_sub" description:"内容前两个章节"` ReportCode string `gorm:"column:report_code" description:"报告唯一编码"` ReportVersion int `gorm:"column:report_version" description:"1:旧版,2:新版"` HasChapter int `gorm:"column:has_chapter" description:"是否有章节 0-否 1-是"` ChapterType string `gorm:"column:chapter_type" description:"章节类型 day-晨报 week-周报"` OldReportId int `gorm:"column:old_report_id" description:"research_report表ID, 大于0则表示该报告为老后台同步过来的"` MsgSendTime time.Time `gorm:"column:msg_send_time" description:"模版消息发送时间"` AdminId int `gorm:"column:admin_id" description:"创建者账号"` AdminRealName string `gorm:"column:admin_real_name" description:"创建者姓名"` ApproveTime time.Time `gorm:"column:approve_time" description:"审批时间"` ApproveId int `gorm:"column:approve_id" description:"审批ID"` DetailImgUrl string `gorm:"column:detail_img_url" description:"报告详情长图地址"` DetailPdfUrl string `gorm:"column:detail_pdf_url" description:"报告详情PDF地址"` ContentStruct string `gorm:"column:content_struct" description:"内容组件"` LastModifyAdminId int `gorm:"column:last_modify_admin_id" description:"最后更新人ID"` LastModifyAdminName string `gorm:"column:last_modify_admin_name" description:"最后更新人姓名"` ContentModifyTime time.Time `gorm:"column:content_modify_time" description:"内容更新时间"` Pv int `gorm:"column:pv" description:"pv"` Uv int `gorm:"column:uv" description:"uv"` HeadImg string `gorm:"column:head_img" description:"报告头图地址"` EndImg string `gorm:"column:end_img" description:"报告尾图地址"` CanvasColor string `gorm:"column:canvas_color" description:"画布颜色"` NeedSplice int `gorm:"column:need_splice" description:"是否拼接版头版位的标记,主要是为了兼容历史报告。0-不需要 1-需要"` HeadResourceId int `gorm:"column:head_resource_id" description:"版头资源ID"` EndResourceId int `gorm:"column:end_resource_id" description:"版尾资源ID"` ClassifyIdThird int `gorm:"column:classify_id_third" description:"三级分类id"` ClassifyNameThird string `gorm:"column:classify_name_third" description:"三级分类名称"` CollaborateType int `gorm:"column:collaborate_type" description:"协作方式,1:个人,2:多人协作。默认:1"` ReportLayout int `gorm:"column:report_layout" description:"报告布局,1:常规布局,2:智能布局。默认:1"` IsPublicPublish int `gorm:"column:is_public_publish" description:"是否公开发布,1:是,2:否"` ReportCreateTime time.Time `gorm:"column:report_create_time" description:"报告时间创建时间"` InheritReportId int `gorm:"column:inherit_report_id" description:"待继承的报告ID"` VoiceGenerateType int `gorm:"column:voice_generate_type" description:"音频生成方式,0:系统生成,1:人工上传"` ReportSource int `gorm:"column:report_source" description:"报告来源:1-系统内;2-智力共享"` OutReportId string `gorm:"column:out_report_id" description:"外部报告ID(或编码)"` TopicEndTime time.Time `gorm:"column:topic_end_time" description:"课题结束时间"` HeadStyle string `gorm:"-"` EndStyle string `gorm:"-"` } func GetReportByCode(reportCode string) (item *Report, err error) { sql := `SELECT * FROM report WHERE report_code=?` err = global.DEFAULT_DmSQL.Raw(sql, reportCode).Scan(&item).Error return } type ReportShareDetailResp struct { Report *ReportItem `description:"报告"` Disclaimer string `description:"免责声明"` H5ShareName string `description:"研报分享抬头"` H5ReportShareImg string `description:"研报分享图片"` WatermarkChart string `description:"图表是否需要水印"` WatermarkReport string `description:"报告是否需要水印"` Hz int ReportLogo string `description:"报告logo"` } type ReportItem struct { *ReportShow ChapterList []*ReportChapter } // UpdateReportPv // @Description: 研报pv自增 // @author: Roc // @datetime 2024-07-19 15:46:58 // @param reportId int // @return err error func UpdateReportPv(reportId int) (err error) { sql := `UPDATE report SET pv = pv + 1 WHERE id = ?` err = global.DEFAULT_DmSQL.Exec(sql, reportId).Error return } func (m *Report) TableName() string { return "report" } func (m *Report) Update(cols []string) (err error) { err = global.DEFAULT_DmSQL.Select(cols).Updates(m).Error return } func (m *Report) Remove() (err error) { sql := fmt.Sprintf(`DELETE FROM %s WHERE %s = ? LIMIT 1`, m.TableName(), "id") err = global.DEFAULT_DmSQL.Exec(sql, m.Id).Error return } func (m *Report) GetItemById(id int) (item *Report, err error) { sql := fmt.Sprintf(`SELECT * FROM %s WHERE %s = ? LIMIT 1`, m.TableName(), "id") err = global.DEFAULT_DmSQL.Raw(sql, id).First(&item).Error return } func (m *Report) GetItemByCondition(condition string, pars []interface{}, orderRule string) (item *Report, err error) { order := `` if orderRule != "" { order = ` ORDER BY ` + orderRule } sql := fmt.Sprintf(`SELECT * FROM %s WHERE 1=1 %s %s LIMIT 1`, m.TableName(), condition, order) err = global.DEFAULT_DmSQL.Raw(sql, pars...).First(&item).Error return } func (m *Report) GetCountByCondition(condition string, pars []interface{}) (count int, err error) { sql := fmt.Sprintf(`SELECT COUNT(1) FROM report WHERE 1=1 %s`, condition) err = global.DEFAULT_DmSQL.Raw(sql, pars...).Scan(&count).Error return } func (m *Report) GetItemsByCondition(condition string, pars []interface{}, fieldArr []string, orderRule string) (items []*Report, err error) { fields := strings.Join(fieldArr, ",") if len(fieldArr) == 0 { fields = `*` } order := `ORDER BY create_time DESC` if orderRule != "" { order = ` ORDER BY ` + orderRule } sql := fmt.Sprintf(`SELECT %s FROM %s WHERE 1=1 %s %s`, fields, m.TableName(), condition, order) err = global.DEFAULT_DmSQL.Raw(sql, pars...).Find(&items).Error return } // CreateReportAndChapters 新增报告和章节 func (m *Report) CreateReportAndChapters(newReport *Report, newChapters []*ReportChapter, newGrants []*ReportGrant, chapterGrantsMapping map[int][]int) (reportId int, err error) { if newReport == nil { err = fmt.Errorf("report is nil") return } tx := global.DEFAULT_DmSQL.Begin() defer func() { if err != nil { _ = tx.Rollback() return } _ = tx.Commit() }() // 新增报告 if e := tx.Create(newReport).Error; e != nil { err = fmt.Errorf("insert report err: %v", e) return } reportId = newReport.Id // 新增报告授权 if len(newGrants) > 0 { for _, v := range newGrants { v.ReportId = reportId } if e := tx.CreateInBatches(newGrants, utils.MultiAddNum).Error; e != nil { err = fmt.Errorf("insert report grants err: %v", e) return } } // 新增报告章节 originNewChapterId := make(map[int]int) // 继承章节ID-新章节ID if len(newChapters) > 0 { for _, v := range newChapters { var originId int if v.ReportChapterId > 0 { originId = v.ReportChapterId v.ReportChapterId = 0 } v.ReportId = reportId if e := tx.Create(v).Error; e != nil { err = fmt.Errorf("insert report chapter err: %v", e) return } if originId > 0 { originNewChapterId[originId] = v.ReportChapterId } } } //fmt.Printf("originNewChapterId: %v\n", originNewChapterId) // 新增章节授权 if len(chapterGrantsMapping) == 0 { return } for k, v := range chapterGrantsMapping { if k <= 0 || len(v) == 0 { continue } chapterGrants := make([]*ReportChapterGrant, 0) for _, vv := range v { cg := &ReportChapterGrant{ AdminId: vv, ReportChapterId: originNewChapterId[k], CreateTime: time.Now(), } chapterGrants = append(chapterGrants, cg) } if e := tx.Create(chapterGrants).Error; e != nil { err = fmt.Errorf("insert report chapter grants err: %v", e) return } } return } // EditReportAndClearGrant 编辑报告和移除授权 func (m *Report) EditReportAndClearGrant(reportItem *Report, updateCols []string, chapterIds, removeGrants []int) (err error) { if reportItem == nil { err = fmt.Errorf("report is nil") return } tx := global.DEFAULT_DmSQL.Begin() defer func() { if err != nil { _ = tx.Rollback() return } _ = tx.Commit() }() // 更新报告 if e := tx.Select(updateCols).Updates(reportItem).Error; e != nil { err = fmt.Errorf("update report err: %v", e) return } // 移除授权 removeLen := len(removeGrants) if removeLen == 0 { return } sql := fmt.Sprintf(`DELETE FROM report_grant WHERE report_id = ? AND admin_id IN (%s)`, utils.GetOrmInReplace(removeLen)) if e := tx.Exec(sql, reportItem.Id, removeGrants).Error; e != nil { err = fmt.Errorf("remove report grants err: %v", e) return } // 移除章节授权 if len(chapterIds) == 0 { return } sql = fmt.Sprintf(`DELETE FROM report_chapter_grant WHERE report_chapter_id IN (%s) AND admin_id IN (%s)`, utils.GetOrmInReplace(len(chapterIds)), utils.GetOrmInReplace(removeLen)) if e := tx.Exec(sql, chapterIds, removeGrants).Error; e != nil { err = fmt.Errorf("remove chapter grants err: %v", e) return } return } type ReportShow struct { Id int `gorm:"column:id;primaryKey;autoIncrement" description:"报告Id"` AddType int `gorm:"column:add_type" description:"新增方式:1:新增报告,2:继承报告"` ClassifyIdFirst int `gorm:"column:classify_id_first" description:"一级分类id"` ClassifyNameFirst string `gorm:"column:classify_name_first" description:"一级分类名称"` ClassifyIdSecond int `gorm:"column:classify_id_second" description:"二级分类id"` ClassifyNameSecond string `gorm:"column:classify_name_second" description:"二级分类名称"` Title string `gorm:"column:title" description:"标题"` Abstract string `gorm:"column:abstract" description:"摘要"` Author string `gorm:"column:author" description:"作者"` Frequency string `gorm:"column:frequency" description:"频度"` CreateTime string `gorm:"column:create_time" description:"创建时间"` ModifyTime string `gorm:"column:modify_time;autoUpdateTime" description:"修改时间"` State int `gorm:"column:state" description:"1:未发布;2:已发布;3-待提交;4-待审批;5-已驳回;6-已通过"` PublishTime string `gorm:"column:publish_time" description:"发布时间"` Stage int `gorm:"column:stage" description:"期数"` MsgIsSend int `gorm:"column:msg_is_send" description:"消息是否已发送,0:否,1:是"` ThsMsgIsSend int `gorm:"column:ths_msg_is_send" description:"客户群消息是否已发送,0:否,1:是"` Content string `gorm:"column:content" description:"内容"` VideoUrl string `gorm:"column:video_url" description:"音频文件URL"` VideoName string `gorm:"column:video_name" description:"音频文件名称"` VideoPlaySeconds string `gorm:"column:video_play_seconds" description:"音频播放时长"` VideoSize string `gorm:"column:video_size" description:"音频文件大小,单位M"` ContentSub string `gorm:"column:content_sub" description:"内容前两个章节"` ReportCode string `gorm:"column:report_code" description:"报告唯一编码"` ReportVersion int `gorm:"column:report_version" description:"1:旧版,2:新版"` HasChapter int `gorm:"column:has_chapter" description:"是否有章节 0-否 1-是"` ChapterType string `gorm:"column:chapter_type" description:"章节类型 day-晨报 week-周报"` OldReportId int `gorm:"column:old_report_id" description:"research_report表ID, 大于0则表示该报告为老后台同步过来的"` MsgSendTime string `gorm:"column:msg_send_time" description:"模版消息发送时间"` AdminId int `gorm:"column:admin_id" description:"创建者账号"` AdminRealName string `gorm:"column:admin_real_name" description:"创建者姓名"` ApproveTime string `gorm:"column:approve_time" description:"审批时间"` ApproveId int `gorm:"column:approve_id" description:"审批ID"` DetailImgUrl string `gorm:"column:detail_img_url" description:"报告详情长图地址"` DetailPdfUrl string `gorm:"column:detail_pdf_url" description:"报告详情PDF地址"` ContentStruct string `gorm:"column:content_struct" description:"内容组件"` LastModifyAdminId int `gorm:"column:last_modify_admin_id" description:"最后更新人ID"` LastModifyAdminName string `gorm:"column:last_modify_admin_name" description:"最后更新人姓名"` ContentModifyTime string `gorm:"column:content_modify_time" description:"内容更新时间"` Pv int `gorm:"column:pv" description:"pv"` Uv int `gorm:"column:uv" description:"uv"` HeadImg string `gorm:"column:head_img" description:"报告头图地址"` EndImg string `gorm:"column:end_img" description:"报告尾图地址"` CanvasColor string `gorm:"column:canvas_color" description:"画布颜色"` NeedSplice int `gorm:"column:need_splice" description:"是否拼接版头版位的标记,主要是为了兼容历史报告。0-不需要 1-需要"` HeadResourceId int `gorm:"column:head_resource_id" description:"版头资源ID"` EndResourceId int `gorm:"column:end_resource_id" description:"版尾资源ID"` ClassifyIdThird int `gorm:"column:classify_id_third" description:"三级分类id"` ClassifyNameThird string `gorm:"column:classify_name_third" description:"三级分类名称"` CollaborateType int `gorm:"column:collaborate_type" description:"协作方式,1:个人,2:多人协作。默认:1"` ReportLayout int `gorm:"column:report_layout" description:"报告布局,1:常规布局,2:智能布局。默认:1"` IsPublicPublish int `gorm:"column:is_public_publish" description:"是否公开发布,1:是,2:否"` ReportCreateTime string `gorm:"column:report_create_time" description:"报告时间创建时间"` InheritReportId int `gorm:"column:inherit_report_id" description:"待继承的报告ID"` VoiceGenerateType int `gorm:"column:voice_generate_type" description:"音频生成方式,0:系统生成,1:人工上传"` ReportSource int `gorm:"column:report_source" description:"报告来源:1-系统内;2-智力共享"` OutReportId string `gorm:"column:out_report_id" description:"外部报告ID(或编码)"` TopicEndTime string `gorm:"column:topic_end_time" description:"课题结束时间"` HeadStyle string `gorm:"-"` EndStyle string `gorm:"-"` } func (m *Report) Format2Item() (item *ReportShow) { if m == nil { return } item = new(ReportShow) item.Id = m.Id item.AddType = m.AddType item.ClassifyIdFirst = m.ClassifyIdFirst item.ClassifyNameFirst = m.ClassifyNameFirst item.ClassifyIdSecond = m.ClassifyIdSecond item.ClassifyNameSecond = m.ClassifyNameSecond item.Title = m.Title item.Abstract = m.Abstract item.Author = m.Author item.Frequency = m.Frequency item.CreateTime = utils.TimeTransferString(utils.FormatDateTime, m.CreateTime) item.ModifyTime = utils.TimeTransferString(utils.FormatDateTime, m.ModifyTime) item.State = m.State item.PublishTime = utils.TimeTransferString(utils.FormatDateTime, m.PublishTime) item.Stage = m.Stage item.MsgIsSend = m.MsgIsSend item.ThsMsgIsSend = m.ThsMsgIsSend item.Content = m.Content item.VideoUrl = m.VideoUrl item.VideoName = m.VideoName item.VideoPlaySeconds = m.VideoPlaySeconds item.VideoSize = m.VideoSize item.ContentSub = m.ContentSub item.ReportCode = m.ReportCode item.ReportVersion = m.ReportVersion item.HasChapter = m.HasChapter item.ChapterType = m.ChapterType item.MsgSendTime = utils.TimeTransferString(utils.FormatDateTime, m.MsgSendTime) item.AdminId = m.AdminId item.AdminRealName = m.AdminRealName item.ApproveTime = utils.TimeTransferString(utils.FormatDateTime, m.ApproveTime) item.ApproveId = m.ApproveId item.DetailImgUrl = m.DetailImgUrl item.DetailPdfUrl = m.DetailPdfUrl item.ContentStruct = m.ContentStruct item.LastModifyAdminId = m.LastModifyAdminId item.LastModifyAdminName = m.LastModifyAdminName item.ContentModifyTime = utils.TimeTransferString(utils.FormatDateTime, m.ContentModifyTime) item.HeadImg = m.HeadImg item.EndImg = m.EndImg item.CanvasColor = m.CanvasColor item.NeedSplice = m.NeedSplice item.HeadResourceId = m.HeadResourceId item.EndResourceId = m.EndResourceId item.ClassifyIdThird = m.ClassifyIdThird item.ClassifyNameThird = m.ClassifyNameThird item.CollaborateType = m.CollaborateType item.ReportLayout = m.ReportLayout item.IsPublicPublish = m.IsPublicPublish item.ReportCreateTime = utils.TimeTransferString(utils.FormatDateTime, m.ReportCreateTime) item.InheritReportId = m.InheritReportId item.VoiceGenerateType = m.VoiceGenerateType item.ReportSource = m.ReportSource item.OutReportId = m.OutReportId item.TopicEndTime = utils.TimeTransferString(utils.FormatDateTime, m.TopicEndTime) item.HeadStyle = m.HeadStyle item.EndStyle = m.EndStyle return }