12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223 |
- package english_report
- import (
- "encoding/json"
- "eta/eta_api/controllers"
- "eta/eta_api/models"
- "eta/eta_api/models/company"
- "eta/eta_api/models/system"
- "eta/eta_api/services"
- "eta/eta_api/services/alarm_msg"
- "eta/eta_api/utils"
- "fmt"
- "github.com/rdlucklib/rdluck_tools/paging"
- "html"
- "strconv"
- "strings"
- "sync"
- "time"
- )
- // EnglishReportController 研报活动模块
- type EnglishReportController struct {
- controllers.BaseAuthController
- }
- // @Title 新增报告接口
- // @Description 新增报告
- // @Param request body models.AddEnglishReportReq true "type json string"
- // @Success 200 {object} models.AddEnglishReportResp
- // @router /add [post]
- func (this *EnglishReportController) Add() {
- br := new(models.BaseResponse).Init()
- defer func() {
- this.Data["json"] = br
- this.ServeJSON()
- }()
- sysUser := this.SysUser
- if sysUser == nil {
- br.Msg = "请登录"
- br.ErrMsg = "请登录,SysUser Is Empty"
- br.Ret = 408
- return
- }
- var req models.AddEnglishReportReq
- err := json.Unmarshal(this.Ctx.Input.RequestBody, &req)
- if err != nil {
- br.Msg = "参数解析异常!"
- br.ErrMsg = "参数解析失败,Err:" + err.Error()
- return
- }
- if req.Content == "" {
- br.Msg = "请输入内容"
- return
- }
- if req.Overview == "" {
- br.Msg = "请输入overview"
- return
- }
- if req.Abstract == "" {
- br.Msg = "请输入摘要"
- return
- }
- var contentSub string
- if req.Content != "" {
- content, e := services.FilterReportContentBr(req.Content)
- if e != nil {
- br.Msg = "内容去除前后空格失败"
- br.ErrMsg = "内容去除前后空格失败, Err: " + e.Error()
- return
- }
- req.Content = content
- contentSub, err = services.GetReportContentSub(req.Content)
- if err != nil {
- go alarm_msg.SendAlarmMsg("ContentSub 失败,Err:"+err.Error(), 3)
- //utils.SendEmail(utils.APPNAME+"失败提醒", "解析 ContentSub 失败,Err:"+err.Error(), utils.EmailSendToUsers)
- }
- }
- maxStage, err := models.GetEnglishReportStage(req.ClassifyIdFirst, req.ClassifyIdSecond)
- if err != nil {
- br.Msg = "期数获取失败!"
- br.ErrMsg = "期数获取失败,Err:" + err.Error()
- return
- }
- item := new(models.EnglishReport)
- item.AddType = req.AddType
- item.ClassifyIdFirst = req.ClassifyIdFirst
- item.ClassifyNameFirst = req.ClassifyNameFirst
- item.ClassifyIdSecond = req.ClassifyIdSecond
- item.ClassifyNameSecond = req.ClassifyNameSecond
- item.Title = req.Title
- item.Abstract = req.Abstract
- item.Author = req.Author
- item.Frequency = req.Frequency
- item.State = req.State
- item.Content = html.EscapeString(req.Content)
- item.Stage = maxStage + 1
- item.ContentSub = html.EscapeString(contentSub)
- item.CreateTime = req.CreateTime
- item.ModifyTime = time.Now()
- item.Overview = req.Overview
- item.AdminId = sysUser.AdminId
- item.AdminRealName = sysUser.RealName
- newReportId, err := models.AddEnglishReport(item)
- if err != nil {
- br.Msg = "保存失败"
- br.ErrMsg = "保存失败,Err:" + err.Error()
- return
- }
- reportCode := utils.MD5(strconv.Itoa(int(newReportId)))
- //修改唯一编码
- {
- go models.ModifyEnglishReportCode(newReportId, reportCode)
- }
- resp := new(models.AddEnglishReportResp)
- resp.ReportId = newReportId
- resp.ReportCode = reportCode
- br.Ret = 200
- br.Success = true
- br.Msg = "保存成功"
- br.Data = resp
- }
- // @Title 编辑报告接口
- // @Description 编辑报告
- // @Param request body models.EditEnglishReportReq true "type json string"
- // @Success 200 {object} models.EditEnglishReportResp
- // @router /edit [post]
- func (this *EnglishReportController) Edit() {
- br := new(models.BaseResponse).Init()
- defer func() {
- this.Data["json"] = br
- this.ServeJSON()
- }()
- sysUser := this.SysUser
- if sysUser == nil {
- br.Msg = "请登录"
- br.ErrMsg = "请登录,SysUser Is Empty"
- br.Ret = 408
- return
- }
- var req models.EditEnglishReportReq
- err := json.Unmarshal(this.Ctx.Input.RequestBody, &req)
- if err != nil {
- br.Msg = "参数解析异常!"
- br.ErrMsg = "参数解析失败,Err:" + err.Error()
- return
- }
- if req.Content == "" {
- br.Msg = "请输入内容"
- return
- }
- if req.Overview == "" {
- br.Msg = "请输入overview"
- return
- }
- if req.Abstract == "" {
- br.Msg = "请输入摘要"
- return
- }
- markStatus, err := services.UpdateEnReportEditMark(int(req.ReportId), sysUser.AdminId, 1, sysUser.RealName)
- if err != nil {
- br.Msg = err.Error()
- return
- }
- if markStatus.Status == 1 {
- br.Msg = markStatus.Msg
- return
- }
- var contentSub string
- if req.Content != "" {
- content, e := services.FilterReportContentBr(req.Content)
- if e != nil {
- br.Msg = "内容去除前后空格失败"
- br.ErrMsg = "内容去除前后空格失败, Err: " + e.Error()
- return
- }
- req.Content = content
- contentSub, err = services.GetReportContentSub(req.Content)
- if err != nil {
- go alarm_msg.SendAlarmMsg("解析 ContentSub 失败,Err:"+err.Error(), 3)
- //utils.SendEmail(utils.APPNAME+"失败提醒", "解析 ContentSub 失败,Err:"+err.Error(), utils.EmailSendToUsers)
- }
- }
- var stage int
- report, _ := models.GetEnglishReportById(int(req.ReportId))
- if report != nil {
- if report.ClassifyNameFirst != req.ClassifyNameFirst || report.ClassifyNameSecond != req.ClassifyNameSecond {
- maxStage, _ := models.GetEnglishReportStageEdit(req.ClassifyIdFirst, req.ClassifyIdSecond, int(req.ReportId))
- maxStage = maxStage + 1
- stage = maxStage
- } else {
- stage = report.Stage
- }
- }
- item := new(models.EnglishReport)
- item.ClassifyIdFirst = req.ClassifyIdFirst
- item.ClassifyNameFirst = req.ClassifyNameFirst
- item.ClassifyIdSecond = req.ClassifyIdSecond
- item.ClassifyNameSecond = req.ClassifyNameSecond
- item.Title = req.Title
- item.Abstract = req.Abstract
- item.Author = req.Author
- item.Frequency = req.Frequency
- item.State = req.State
- item.Stage = stage
- item.Content = html.EscapeString(req.Content)
- item.ContentSub = html.EscapeString(contentSub)
- item.CreateTime = req.CreateTime
- item.Overview = req.Overview
- err = models.EditEnglishReport(item, req.ReportId)
- if err != nil {
- br.Msg = "保存失败"
- br.ErrMsg = "保存失败,Err:" + err.Error()
- return
- }
- reportCode := utils.MD5(strconv.Itoa(int(req.ReportId)))
- resp := new(models.EditEnglishReportResp)
- resp.ReportId = req.ReportId
- resp.ReportCode = reportCode
- br.Ret = 200
- br.Success = true
- br.Msg = "保存成功"
- br.Data = resp
- }
- // @Title 获取报告详情接口
- // @Description 获取报告详情
- // @Param request body models.ReportDetailReq true "type json string"
- // @Success 200 {object} models.EnglishReportDetailView
- // @router /detail [get]
- func (this *EnglishReportController) Detail() {
- br := new(models.BaseResponse).Init()
- defer func() {
- this.Data["json"] = br
- this.ServeJSON()
- }()
- /*var req models.ReportDetailReq
- err := json.Unmarshal(this.Ctx.Input.RequestBody, &req)
- if err != nil {
- br.Msg = "参数解析异常!"
- br.ErrMsg = "参数解析失败,Err:" + err.Error()
- return
- }
- if req.ReportId <= 0 {
- br.Msg = "参数错误"
- return
- }*/
- reportId, err := this.GetInt("ReportId")
- if err != nil {
- br.Msg = "获取参数失败!"
- br.ErrMsg = "获取参数失败,Err:" + err.Error()
- return
- }
- if reportId <= 0 {
- br.Msg = "参数错误"
- return
- }
- item, err := models.GetEnglishReportById(reportId)
- if err != nil {
- if err.Error() == utils.ErrNoRow() {
- br.Msg = "报告已被删除"
- return
- }
- br.Msg = "获取失败"
- br.ErrMsg = "获取失败,Err:" + err.Error()
- return
- }
- item.Content = html.UnescapeString(item.Content)
- item.ContentSub = html.UnescapeString(item.ContentSub)
- classifyNameMap := make(map[int]*models.EnglishClassifyFullName)
- if item.ClassifyIdSecond > 0 {
- nameList, tErr := models.GetEnglishClassifyFullNameByIds([]int{item.ClassifyIdSecond})
- if tErr != nil {
- br.Msg = "获取分类名称失败"
- br.ErrMsg = "获取分类名称失败, ERR:" + tErr.Error()
- return
- }
- for _, v := range nameList {
- classifyNameMap[v.Id] = v
- }
- //处理分类名
- if n, ok := classifyNameMap[item.ClassifyIdSecond]; ok {
- item.ClassifyNameRoot = n.RootName
- item.ClassifyIdRoot = n.RootId
- }
- }
- br.Ret = 200
- br.Success = true
- br.Msg = "获取成功"
- br.Data = item
- }
- // ListReport
- // @Title 获取报告列表接口
- // @Description 获取报告列表
- // @Param PageSize query int true "每页数据条数"
- // @Param CurrentIndex query int true "当前页页码,从1开始"
- // @Param TimeType query string true "筛选的时间类别:publish_time(发布时间),modify_time(更新时间)"
- // @Param StartDate query string true "开始时间"
- // @Param EndDate query string true "结束时间"
- // @Param Frequency query string true "频度"
- // @Param ClassifyIdFirst query string true "一级分类id"
- // @Param ClassifyIdSecond query string true "二级分类id"
- // @Param State query int true "状态"
- // @Param KeyWord query string true "搜索关键词"
- // @Param PublishSort query string true "desc:降序,asc 升序(预留)"
- // @Param CompanyType query string false "产品类型,枚举值:'ficc','权益';不传默认返回全部"
- // @Success 200 {object} models.ReportListResp
- // @router /list [get]
- func (this *EnglishReportController) ListReport() {
- br := new(models.BaseResponse).Init()
- defer func() {
- this.Data["json"] = br
- this.ServeJSON()
- }()
- sysUser := this.SysUser
- if sysUser == nil {
- br.Msg = "请登录"
- br.ErrMsg = "请登录,SysUser Is Empty"
- br.Ret = 408
- return
- }
- pageSize, _ := this.GetInt("PageSize")
- currentIndex, _ := this.GetInt("CurrentIndex")
- timeType := this.GetString("TimeType")
- startDate := this.GetString("StartDate")
- endDate := this.GetString("EndDate")
- frequency := this.GetString("Frequency")
- classifyIdFirst, _ := this.GetInt("ClassifyIdFirst")
- classifyIdSecond, _ := this.GetInt("ClassifyIdSecond")
- classifyIdRoot, _ := this.GetInt("ClassifyIdRoot")
- state, _ := this.GetInt("State")
- keyWord := this.GetString("KeyWord")
- companyType := this.GetString("CompanyType")
- // 群发邮件状态筛选
- emailState, _ := this.GetInt("EmailState")
- var startSize int
- if pageSize <= 0 {
- pageSize = utils.PageSize20
- }
- if currentIndex <= 0 {
- currentIndex = 1
- }
- startSize = utils.StartIndex(currentIndex, pageSize)
- var condition string
- var pars []interface{}
- if keyWord != "" {
- condition += ` AND (title LIKE '%` + keyWord + `%' OR admin_real_name LIKE '%` + keyWord + `%' ) `
- }
- if timeType == "" {
- timeType = "publish_time"
- }
- if timeType != "publish_time" && timeType != "modify_time" {
- br.Msg = "请选择正确的时间"
- br.ErrMsg = "请选择正确的时间"
- return
- }
- if startDate != "" {
- condition += ` AND ` + timeType + ` >= ? `
- pars = append(pars, startDate)
- }
- if endDate != "" {
- condition += ` AND ` + timeType + ` <= ? `
- pars = append(pars, endDate)
- }
- if frequency != "" {
- condition += ` AND frequency = ? `
- pars = append(pars, frequency)
- }
- if classifyIdFirst != 0 {
- condition += ` AND classify_id_first = ? `
- pars = append(pars, classifyIdFirst)
- }
- if classifyIdSecond != 0 {
- condition += ` AND classify_id_second = ? `
- pars = append(pars, classifyIdSecond)
- }
- if state > 0 {
- condition += ` AND state = ? `
- pars = append(pars, state)
- }
- if classifyIdRoot > 0 && classifyIdFirst == 0 && classifyIdSecond == 0 {
- //查询顶级分类下的所有二级分类ID
- childClassify, err := models.GetEnglishSecondClassifyList([]int{classifyIdRoot})
- if err != nil {
- br.Msg = "查询子分类出错"
- br.ErrMsg = "查询子分类出错, Err:" + err.Error()
- return
- }
- var childClassifyIds []int
- if len(childClassify) > 0 {
- for _, v := range childClassify {
- childClassifyIds = append(childClassifyIds, v.Id)
- }
- condition += ` AND classify_id_first IN (` + utils.GetOrmInReplace(len(childClassifyIds)) + `)`
- pars = append(pars, childClassifyIds)
- } else {
- //一级分类下没有子分类,直接返回空列表
- page := paging.GetPaging(currentIndex, pageSize, 0)
- resp := new(models.EnglishReportListResp)
- resp.Paging = page
- resp.List = make([]*models.EnglishReportList, 0)
- br.Ret = 200
- br.Success = true
- br.Msg = "获取成功"
- br.Data = resp
- return
- }
- }
- // 未群发邮件(包含推送邮件失败的)
- if emailState == 1 {
- failIds, e := models.GetHasFailEmailLogReportIds()
- if e != nil {
- br.Msg = "获取失败"
- br.ErrMsg = "获取存在邮件推送失败记录的英文报告IDs失败, Err:" + e.Error()
- return
- }
- condition += ` AND email_state = 0`
- if len(failIds) > 0 {
- condition += ` OR id IN (` + utils.GetOrmInReplace(len(failIds)) + `)`
- pars = append(pars, failIds)
- }
- }
- // 已群发邮件
- if emailState == 2 {
- successIds, e := models.GetSuccessEmailLogReportIds()
- if e != nil {
- br.Msg = "获取失败"
- br.ErrMsg = "获取邮件推送记录均为成功的英文报告IDs失败, Err:" + e.Error()
- return
- }
- condition += ` AND email_state = 1`
- if len(successIds) > 0 {
- condition += ` AND id IN (` + utils.GetOrmInReplace(len(successIds)) + `)`
- pars = append(pars, successIds)
- }
- }
- var total int
- var errCount, errList, errOther error
- var authOk bool
- list := make([]*models.EnglishReportList, 0)
- failMap := make(map[int]bool, 0) // 有群发失败记录的研报
- adminMap := make(map[int]string, 0) // 编辑中的研究员姓名
- wg := sync.WaitGroup{}
- wg.Add(3)
- // 列表总数
- go func() {
- defer func() {
- wg.Done()
- }()
- t, e := models.GetEnglishReportListCount(condition, pars, companyType)
- if e != nil {
- errCount = fmt.Errorf("获取英文研报Count失败, Err: %s", e.Error())
- return
- }
- total = t
- }()
- // 列表数据
- go func() {
- defer func() {
- wg.Done()
- }()
- // 限制一下富文本字段, 列表用不到
- fieldArr := []string{
- "id", "add_type", "classify_id_first", "classify_name_first", "classify_id_second", "classify_name_second", "title", "abstract", "author",
- "frequency", "create_time", "modify_time", "state", "publish_time", "stage", "msg_is_send", "report_code", "pv", "share_url",
- "pv_email", "email_state", "from_report_id", "key_takeaways", "admin_id", "admin_real_name",
- }
- items, e := models.GetEnglishReportList(condition, pars, companyType, startSize, pageSize, fieldArr)
- if e != nil {
- errList = fmt.Errorf("获取英文研报列表失败, Err: %s", e.Error())
- return
- }
- list = items
- }()
- // 群发权限/失败记录
- go func() {
- defer func() {
- wg.Done()
- }()
- // 获取邮件配置-是否有权限群发
- conf := new(models.EnglishReportEmailConf)
- authKey := "english_report_email_conf"
- confAuth, e := company.GetConfigDetailByCode(authKey)
- if e != nil {
- errOther = fmt.Errorf("获取群发邮件权限失败, Err: %s", e.Error())
- return
- }
- if confAuth.ConfigValue == "" {
- errOther = fmt.Errorf("群发邮件配置为空")
- return
- }
- if e := json.Unmarshal([]byte(confAuth.ConfigValue), &conf); e != nil {
- errOther = fmt.Errorf("群发邮件配置有误")
- return
- }
- authArr := strings.Split(conf.SendAuthGroup, ",")
- if utils.InArrayByStr(authArr, sysUser.RoleTypeCode) {
- authOk = true
- }
- // 是否有群发邮件失败的记录,标记红点
- failList, e := models.GetEnglishReportEmailLogFailList(0)
- if e != nil {
- errOther = fmt.Errorf("获取群发邮件记录失败, Err: %s", e.Error())
- return
- }
- for i := range failList {
- failMap[failList[i].ReportId] = true
- }
- // 获取admin, 用于匹配编辑中的研究员姓名
- admins, e := system.GetSysAdminList("", make([]interface{}, 0), []string{"admin_id", "real_name"}, "")
- if e != nil {
- errOther = fmt.Errorf("获取系统用户列表失败, Err: %s", e.Error())
- return
- }
- for _, a := range admins {
- adminMap[a.AdminId] = a.RealName
- }
- }()
- wg.Wait()
- if errCount != nil {
- br.Msg = "获取失败"
- br.ErrMsg = errCount.Error()
- return
- }
- if errList != nil {
- br.Msg = "获取失败"
- br.ErrMsg = errList.Error()
- return
- }
- if errOther != nil {
- br.Msg = "获取失败"
- br.ErrMsg = errOther.Error()
- return
- }
- // 查询分类信息
- var classifyIdSecondSlice []int
- for _, item := range list {
- classifyIdSecondSlice = append(classifyIdSecondSlice, item.ClassifyIdSecond)
- }
- classifyNameMap := make(map[int]*models.EnglishClassifyFullName)
- if len(classifyIdSecondSlice) > 0 {
- nameList, err := models.GetEnglishClassifyFullNameByIds(classifyIdSecondSlice)
- if err != nil {
- br.Msg = "获取分类名称失败"
- br.ErrMsg = "获取分类名称失败, ERR:" + err.Error()
- return
- }
- for _, v := range nameList {
- classifyNameMap[v.Id] = v
- }
- }
- for _, item := range list {
- if item.State == 2 {
- item.ShareUrl = "https://share.hzinsights.com/reportEn?code=" + item.ReportCode
- }
- item.EmailAuth = authOk
- item.EmailHasFail = failMap[item.Id]
- // 邮箱PV大于0的时候, 不展示最初版本的PV
- if item.PvEmail > 0 {
- item.Pv = 0
- }
- // 报告是否正在编辑中
- var opUser models.MarkReportItem
- key := fmt.Sprint(`crm:enReport:edit:`, item.Id)
- opUserId, e := utils.Rc.RedisInt(key)
- if e != nil {
- str, te := utils.Rc.RedisString(key)
- if te == nil {
- te = json.Unmarshal([]byte(str), &opUser)
- if te == nil {
- opUserId = opUser.AdminId
- }
- }
- }
- var ret models.MarkReportResp
- if opUserId > 0 && opUserId != sysUser.AdminId {
- editor := opUser.Editor
- if editor == "" {
- editor = adminMap[opUserId]
- }
- ret.Status = 1
- ret.Msg = fmt.Sprintf("当前%s正在编辑报告", editor)
- ret.Editor = editor
- }
- if ret.Status == 0 {
- item.CanEdit = true
- } else {
- item.Editor = ret.Editor
- }
- //处理分类名
- if n, ok := classifyNameMap[item.ClassifyIdSecond]; ok {
- if n.RootId == 0 {
- item.FullClassifyName = strings.Join([]string{n.ParentName, n.ClassifyName}, "/")
- } else {
- item.FullClassifyName = strings.Join([]string{n.RootName, n.ParentName, n.ClassifyName}, "/")
- }
- item.ClassifyIdRoot = n.RootId
- item.ClassifyNameRoot = n.RootName
- }
- }
- page := paging.GetPaging(currentIndex, pageSize, total)
- resp := new(models.EnglishReportListResp)
- resp.Paging = page
- resp.List = list
- br.Ret = 200
- br.Success = true
- br.Msg = "获取成功"
- br.Data = resp
- }
- // @Title 发布报告接口
- // @Description 发布报告
- // @Param request body models.PublishReq true "type json string"
- // @Success 200 Ret=200 发布成功
- // @router /publish [post]
- func (this *EnglishReportController) PublishReport() {
- br := new(models.BaseResponse).Init()
- defer func() {
- this.Data["json"] = br
- this.ServeJSON()
- }()
- var req models.PublishReq
- err := json.Unmarshal(this.Ctx.Input.RequestBody, &req)
- if err != nil {
- br.Msg = "参数解析异常!"
- br.ErrMsg = "参数解析失败,Err:" + err.Error()
- return
- }
- reportIds := req.ReportIds
- if reportIds == "" {
- br.Msg = "参数错误"
- br.ErrMsg = "参数错误,报告id不可为空"
- return
- }
- reportArr := strings.Split(reportIds, ",")
- for _, v := range reportArr {
- vint, err := strconv.Atoi(v)
- if err != nil {
- br.Msg = "参数错误"
- br.ErrMsg = "参数错误,Err:" + err.Error()
- return
- }
- report, err := models.GetEnglishReportById(vint)
- if err != nil {
- br.Msg = "获取报告信息失败"
- br.ErrMsg = "获取报告信息失败,Err:" + err.Error()
- return
- }
- if report == nil {
- br.Msg = "报告不存在"
- return
- }
- var tmpErr error
- if report.Content == "" {
- br.Msg = "报告内容为空,不可发布"
- br.ErrMsg = "报告内容为空,不需要生成,report_id:" + strconv.Itoa(report.Id)
- return
- }
- var publishTime string
- if report.PublishTime != "" {
- // 发布时间固定为首次发布时间
- publishTime = report.PublishTime
- } else {
- publishTime = time.Now().Format(utils.FormatDateTime)
- }
- if tmpErr = models.PublishEnglishReportById(report.Id, publishTime); tmpErr != nil {
- br.Msg = "报告发布失败"
- br.ErrMsg = "报告发布失败, Err:" + tmpErr.Error() + ", report_id:" + strconv.Itoa(report.Id)
- return
- }
- go func() {
- _ = services.UpdateEnglishReportEs(report.Id, 2)
- }()
- }
- br.Ret = 200
- br.Success = true
- br.Msg = "发布成功"
- }
- // PrePublishReport
- // @Title 设置定时发布接口
- // @Description 设置定时发布接口
- // @Param request body models.PrePublishReq true "type json string"
- // @Success 200 Ret=200 发布成功
- // @router /pre_publish [post]
- func (this *EnglishReportController) PrePublishReport() {
- br := new(models.BaseResponse).Init()
- defer func() {
- this.Data["json"] = br
- this.ServeJSON()
- }()
- var req models.PrePublishReq
- err := json.Unmarshal(this.Ctx.Input.RequestBody, &req)
- if err != nil {
- br.Msg = "参数解析异常!"
- br.ErrMsg = "参数解析失败,Err:" + err.Error()
- return
- }
- reportId := req.ReportId
- if reportId == 0 {
- br.Msg = "参数错误"
- br.ErrMsg = "参数错误,报告id不可为空"
- return
- }
- if req.PrePublishTime == "" {
- br.Msg = "发布时间不能为空"
- return
- }
- prePublishTime, err := time.ParseInLocation(utils.FormatDateTime, req.PrePublishTime, time.Local)
- if err != nil {
- br.Msg = "发布时间格式错误"
- br.ErrMsg = "发布时间格式错误,Err:" + err.Error()
- return
- }
- if prePublishTime.Before(time.Now()) {
- br.Msg = "发布时间不允许选择过去时间"
- return
- }
- if prePublishTime.Before(time.Now().Add(2 * time.Minute)) {
- br.Msg = "发布时间距离当前时间太近了"
- return
- }
- report, err := models.GetEnglishReportById(reportId)
- if err != nil {
- br.Msg = "获取报告信息失败"
- br.ErrMsg = "获取报告信息失败,Err:" + err.Error()
- return
- }
- if report == nil {
- br.Msg = "报告不存在"
- return
- }
- if report.Content == "" {
- br.Msg = "报告内容为空,不可发布"
- br.ErrMsg = "报告内容为空,不需要生成,report_id:" + strconv.Itoa(report.Id)
- return
- }
- if report.State == 2 {
- br.Msg = "报告已发布,不可设置定时发布"
- return
- }
- var tmpErr error
- if tmpErr = models.SetPrePublishEnglishReportById(report.Id, req.PrePublishTime); tmpErr != nil {
- br.Msg = "设置定时发布失败"
- br.ErrMsg = "设置定时发布失败, Err:" + tmpErr.Error() + ", report_id:" + strconv.Itoa(report.Id)
- return
- }
- br.Ret = 200
- br.Success = true
- br.Msg = "定时发布成功"
- }
- // @Title 取消发布报告接口
- // @Description 取消发布报告
- // @Param request body models.PublishCancelReq true "type json string"
- // @Success 200 Ret=200 取消发布成功
- // @router /publish/cancel [post]
- func (this *EnglishReportController) PublishCancleReport() {
- br := new(models.BaseResponse).Init()
- defer func() {
- this.Data["json"] = br
- this.ServeJSON()
- }()
- var req models.PublishCancelReq
- err := json.Unmarshal(this.Ctx.Input.RequestBody, &req)
- if err != nil {
- br.Msg = "参数解析异常!"
- br.ErrMsg = "参数解析失败,Err:" + err.Error()
- return
- }
- if req.ReportIds <= 0 {
- br.Msg = "参数错误"
- br.ErrMsg = "参数错误,报告id不可为空"
- return
- }
- err = models.PublishCancelEnglishReport(req.ReportIds)
- if err != nil {
- br.Msg = "取消发布失败"
- br.ErrMsg = "取消发布失败,Err:" + err.Error()
- return
- }
- // 更新es里的报告状态
- go func() {
- _ = services.UpdateEnglishReportEs(req.ReportIds, 1)
- }()
- br.Ret = 200
- br.Success = true
- br.Msg = "取消发布成功"
- }
- // @Title 删除报告接口
- // @Description 删除报告
- // @Param request body models.DeleteReq true "type json string"
- // @Success 200 Ret=200 删除成功
- // @router /delete [post]
- func (this *EnglishReportController) Delete() {
- br := new(models.BaseResponse).Init()
- defer func() {
- this.Data["json"] = br
- this.ServeJSON()
- }()
- var req models.DeleteReq
- err := json.Unmarshal(this.Ctx.Input.RequestBody, &req)
- if err != nil {
- br.Msg = "参数解析异常!"
- br.ErrMsg = "参数解析失败,Err:" + err.Error()
- return
- }
- if req.ReportIds <= 0 {
- br.Msg = "参数错误"
- br.ErrMsg = "参数错误,报告id不可为空"
- return
- }
- reportInfo, err := models.GetEnglishReportById(req.ReportIds)
- if err != nil {
- if err.Error() == utils.ErrNoRow() {
- br.Msg = "报告不存在"
- return
- }
- br.Msg = "查询报告信息失败"
- br.ErrMsg = "查询报告信息失败, Err" + err.Error()
- return
- }
- // 更新es里的报告状态
- _ = services.UpdateEnglishReportEs(req.ReportIds, 1)
- if err = models.DeleteEnglishReportAndChapter(reportInfo); err != nil {
- br.Msg = "删除失败"
- br.ErrMsg = "删除报告失败, Err: " + err.Error()
- return
- }
- // 重置PPT关联报告
- go func() {
- _ = services.ResetPPTReport(req.ReportIds, true)
- }()
- go func() {
- _ = services.EnglishPolicyReportSyncCancel(reportInfo)
- }()
- br.Ret = 200
- br.Success = true
- br.Msg = "删除成功"
- }
- // @Title 保存草稿
- // @Description 保存草稿
- // @Param request body models.SaveReportContent true "type json string"
- // @Success 200 {object} models.ReportAuthorResp
- // @router /saveReportContent [post]
- func (this *EnglishReportController) SaveReportContent() {
- br := new(models.BaseResponse).Init()
- br.IsSendEmail = false
- defer func() {
- this.Data["json"] = br
- this.ServeJSON()
- }()
- var req models.SaveReportContent
- err := json.Unmarshal(this.Ctx.Input.RequestBody, &req)
- if err != nil {
- br.Msg = "参数解析异常!"
- br.ErrMsg = "参数解析失败,Err:" + err.Error()
- return
- }
- sysUser := this.SysUser
- if sysUser == nil {
- br.Msg = "请登录"
- br.ErrMsg = "请登录,SysUser Is Empty"
- br.Ret = 408
- return
- }
- reportId := req.ReportId
- noChangeFlag := req.NoChange
- fmt.Println("reportId:", reportId)
- if reportId <= 0 {
- br.Msg = "请输入报告ID"
- return
- }
- markStatus, err := services.UpdateEnReportEditMark(int(req.ReportId), sysUser.AdminId, 1, sysUser.RealName)
- if err != nil {
- br.Msg = err.Error()
- return
- }
- if markStatus.Status == 1 {
- br.Msg = markStatus.Msg
- return
- }
- if noChangeFlag != 1 {
- content := req.Content
- if content == "" {
- content = this.GetString("Content")
- }
- if content != "" {
- contentClean, e := services.FilterReportContentBr(req.Content)
- if e != nil {
- br.Msg = "内容去除前后空格失败"
- br.ErrMsg = "内容去除前后空格失败, Err: " + e.Error()
- return
- }
- content = contentClean
- contentSub, err := services.GetReportContentSub(content)
- if err != nil {
- go alarm_msg.SendAlarmMsg("解析 ContentSub 失败,Err:"+err.Error(), 3)
- //utils.SendEmail(utils.APPNAME+"失败提醒", "解析 ContentSub 失败,Err:"+err.Error(), utils.EmailSendToUsers)
- }
- content = html.EscapeString(content)
- contentSub = html.EscapeString(contentSub)
- err = models.EditEnglishReportContent(reportId, content, contentSub)
- if err != nil {
- br.Msg = "保存失败"
- br.ErrMsg = "保存失败,Err:" + err.Error()
- return
- }
- go models.AddEnglishReportSaveLog(reportId, this.SysUser.AdminId, content, contentSub, this.SysUser.AdminName)
- }
- }
- resp := new(models.SaveReportContentResp)
- resp.ReportId = reportId
- br.Ret = 200
- br.Success = true
- br.Msg = "保存成功成功"
- br.Data = resp
- }
- // @Title 根据分类获取最近一次报告详情接口
- // @Description 根据分类获取最近一次报告详情
- // @Param request body models.ClassifyIdDetailReq true "type json string"
- // @Success 200 {object} models.Report
- // @router /classifyIdDetail [get]
- func (this *EnglishReportController) ClassifyIdDetail() {
- br := new(models.BaseResponse).Init()
- defer func() {
- this.Data["json"] = br
- this.ServeJSON()
- }()
- //classify_id_first=34&classify_id_second=36
- /*var req models.ClassifyIdDetailReq
- err := json.Unmarshal(this.Ctx.Input.RequestBody, &req)
- if err != nil {
- br.Msg = "参数解析异常!"
- br.ErrMsg = "参数解析失败,Err:" + err.Error()
- return
- }*/
- classifyIdFirst, _ := this.GetInt("classify_id_first")
- if classifyIdFirst <= 0 {
- classifyIdFirst, _ = this.GetInt("ClassifyIdFirst")
- }
- classifyIdSecond, _ := this.GetInt("classify_id_second")
- if classifyIdSecond <= 0 {
- classifyIdSecond, _ = this.GetInt("ClassifyIdSecond")
- }
- item, err := models.GetEnglishReportDetailByClassifyId(classifyIdFirst, classifyIdSecond)
- if err != nil && err.Error() != utils.ErrNoRow() {
- br.Msg = "获取失败!"
- br.ErrMsg = "获取失败,Err:" + err.Error()
- return
- }
- if item != nil {
- item.Content = html.UnescapeString(item.Content)
- item.ContentSub = html.UnescapeString(item.ContentSub)
- classifyNameMap := make(map[int]*models.EnglishClassifyFullName)
- if item.ClassifyIdSecond > 0 {
- nameList, tErr := models.GetEnglishClassifyFullNameByIds([]int{item.ClassifyIdSecond})
- if tErr != nil {
- br.Msg = "获取分类名称失败"
- br.ErrMsg = "获取分类名称失败, ERR:" + tErr.Error()
- return
- }
- for _, v := range nameList {
- classifyNameMap[v.Id] = v
- }
- //处理分类名
- if n, ok := classifyNameMap[item.ClassifyIdSecond]; ok {
- item.ClassifyNameRoot = n.RootName
- item.ClassifyIdRoot = n.RootId
- }
- }
- }
- br.Ret = 200
- br.Success = true
- br.Msg = "获取成功"
- br.Data = item
- }
- // Author
- // @Title 获取报告作者接口
- // @Description 获取报告作者
- // @Param AuthorType query int true "来源类型,1:中文,2:英文"
- // @Param Keyword query string true "搜索关键词"
- // @Param CurrentIndex query int true "当前页页码,从1开始"
- // @Param StartDate query string true "开始时间"
- // @Success 200 {object} models.ReportAuthorResp
- // @router /author [get]
- func (this *EnglishReportController) Author() {
- br := new(models.BaseResponse).Init()
- defer func() {
- this.Data["json"] = br
- this.ServeJSON()
- }()
- keyword := this.GetString("Keyword")
- var condition string
- var pars []interface{}
- condition += ` AND author_type = 2 AND enable = 1`
- if keyword != `` {
- condition += ` AND report_author like ? `
- pars = append(pars, "%"+keyword+"%")
- }
- _, items, err := models.GetReportAuthorList(condition, pars, 0, 10000)
- if err != nil {
- br.Msg = "获取失败!"
- br.ErrMsg = "获取失败,Err:" + err.Error()
- return
- }
- resp := models.ReportAuthorResp{
- List: items,
- }
- br.Ret = 200
- br.Success = true
- br.Msg = "获取成功"
- br.Data = resp
- }
- // MarkEditStatus
- // @Title 标记报告编辑状态
- // @Description 标记报告编辑状态接口
- // @Param request body request.MarkEditEnReport true "type json string"
- // @Success 200 标记成功 ;202 标记成功
- // @router /mark [post]
- func (this *EnglishReportController) MarkEditStatus() {
- br := new(models.BaseResponse).Init()
- defer func() {
- this.Data["json"] = br
- this.ServeJSON()
- }()
- sysUser := this.SysUser
- if sysUser == nil {
- br.Msg = "请登录"
- br.ErrMsg = "请登录,SysUser Is Empty"
- br.Ret = 408
- return
- }
- var req models.MarkEditEnReport
- err := json.Unmarshal(this.Ctx.Input.RequestBody, &req)
- if err != nil {
- br.Msg = "参数解析异常!"
- br.ErrMsg = "参数解析失败,Err:" + err.Error()
- return
- }
- if req.ReportId <= 0 {
- br.Msg = "缺少报告Id"
- return
- }
- if req.Status <= 0 {
- br.Msg = "标记状态异常"
- return
- }
- //更新标记key
- data, err := services.UpdateEnReportEditMark(req.ReportId, sysUser.AdminId, req.Status, sysUser.RealName)
- if err != nil {
- br.Msg = err.Error()
- return
- }
- msg := "标记成功"
- br.Ret = 200
- br.Success = true
- br.Msg = msg
- br.Data = data
- }
- // @Title 编辑来自策略报告的报告
- // @Description 编辑来自策略报告的报告
- // @Param request body models.EditEnglishReportFromPolicyReq true "type json string"
- // @Success 200 {object} models.EditEnglishReportResp
- // @router /edit_policy [post]
- func (this *EnglishReportController) EditPolicy() {
- br := new(models.BaseResponse).Init()
- defer func() {
- this.Data["json"] = br
- this.ServeJSON()
- }()
- sysUser := this.SysUser
- if sysUser == nil {
- br.Msg = "请登录"
- br.ErrMsg = "请登录,SysUser Is Empty"
- br.Ret = 408
- return
- }
- var req models.EditEnglishReportFromPolicyReq
- err := json.Unmarshal(this.Ctx.Input.RequestBody, &req)
- if err != nil {
- br.Msg = "参数解析异常!"
- br.ErrMsg = "参数解析失败,Err:" + err.Error()
- return
- }
- if req.ReportId <= 0 {
- br.Msg = "请选择报告ID"
- return
- }
- if req.Title == "" {
- br.Msg = "请输入标题"
- return
- }
- if req.Abstract == "" {
- br.Msg = "请输入摘要"
- return
- }
- if req.Frequency == "" {
- br.Msg = "请输入频度"
- return
- }
- if req.Author == "" {
- br.Msg = "请输入作者"
- return
- }
- if req.CreateTime == "" {
- br.Msg = "请输入创建时间"
- return
- }
- //查询报告是否存在
- reportInfo, err := models.GetEnglishReportItemById(int(req.ReportId))
- if err != nil {
- if err.Error() == utils.ErrNoRow() {
- br.Msg = "报告不存在"
- return
- }
- br.Msg = "查询报告信息失败"
- br.ErrMsg = "查询报告信息失败, Err" + err.Error()
- return
- }
- if reportInfo.State == 2 {
- br.Msg = "报告已发布不允许编辑"
- return
- }
- markStatus, err := services.UpdateEnReportEditMark(int(req.ReportId), sysUser.AdminId, 1, sysUser.RealName)
- if err != nil {
- br.Msg = err.Error()
- return
- }
- if markStatus.Status == 1 {
- br.Msg = markStatus.Msg
- return
- }
- item := reportInfo
- item.Title = req.Title
- item.Abstract = req.Abstract
- item.Author = req.Author
- item.Frequency = req.Frequency
- item.CreateTime = req.CreateTime
- item.ModifyTime = time.Now()
- err = item.Update([]string{"Title", "Abstract", "Author", "Frequency", "CreateTime", "ModifyTime"})
- if err != nil {
- br.Msg = "保存失败"
- br.ErrMsg = "保存失败,Err:" + err.Error()
- return
- }
- reportCode := reportInfo.ReportCode
- resp := new(models.EditEnglishReportResp)
- resp.ReportId = req.ReportId
- resp.ReportCode = reportCode
- br.Ret = 200
- br.Success = true
- br.Msg = "保存成功"
- br.Data = resp
- }
|