report.go 28 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104
  1. package controllers
  2. import (
  3. "encoding/json"
  4. "eta_gn/eta_api/models"
  5. "eta_gn/eta_api/services"
  6. "eta_gn/eta_api/services/alarm_msg"
  7. "eta_gn/eta_api/utils"
  8. "fmt"
  9. "github.com/beego/beego/v2/server/web"
  10. "github.com/h2non/filetype"
  11. "github.com/tealeg/xlsx"
  12. "html"
  13. "io"
  14. "os"
  15. "path"
  16. "path/filepath"
  17. "strconv"
  18. "strings"
  19. "time"
  20. )
  21. // ReportController 报告
  22. type ReportController struct {
  23. BaseAuthController
  24. }
  25. // ReportCommonController 报告
  26. type ReportCommonController struct {
  27. BaseCommonController
  28. }
  29. // ReportUploadCommonController 报告上传
  30. type ReportUploadCommonController struct {
  31. web.Controller
  32. }
  33. // Delete
  34. // @Title 删除报告接口
  35. // @Description 删除报告
  36. // @Param request body models.DeleteReq true "type json string"
  37. // @Success 200 Ret=200 删除成功
  38. // @router /delete [post]
  39. func (this *ReportController) Delete() {
  40. br := new(models.BaseResponse).Init()
  41. defer func() {
  42. this.Data["json"] = br
  43. this.ServeJSON()
  44. }()
  45. var req models.DeleteReq
  46. err := json.Unmarshal(this.Ctx.Input.RequestBody, &req)
  47. if err != nil {
  48. br.Msg = "参数解析异常!"
  49. br.ErrMsg = "参数解析失败,Err:" + err.Error()
  50. return
  51. }
  52. if req.ReportIds <= 0 {
  53. br.Msg = "参数错误"
  54. br.ErrMsg = "参数错误,报告id不可为空"
  55. return
  56. }
  57. if err := services.DeleteReportAndChapter(req.ReportIds); err != nil {
  58. br.Msg = "删除失败"
  59. br.ErrMsg = "删除报告失败, Err: " + err.Error()
  60. return
  61. }
  62. br.Ret = 200
  63. br.Success = true
  64. br.Msg = "删除成功"
  65. }
  66. // Upload
  67. // @Title 图片上传
  68. // @Description 图片上传接口
  69. // @Param file query file true "文件"
  70. // @Success 200 新增成功
  71. // @router /upload [post]
  72. func (this *ReportController) Upload() {
  73. br := new(models.BaseResponse).Init()
  74. defer func() {
  75. this.Data["json"] = br
  76. this.ServeJSON()
  77. }()
  78. f, h, err := this.GetFile("file")
  79. if err != nil {
  80. br.Msg = "获取资源信息失败"
  81. br.ErrMsg = "获取资源信息失败,Err:" + err.Error()
  82. return
  83. }
  84. fileData, e := io.ReadAll(f)
  85. if e != nil {
  86. br.Msg = "上传失败"
  87. br.ErrMsg = "读取文件失败, Err: " + e.Error()
  88. return
  89. }
  90. pass := filetype.IsImage(fileData)
  91. if !pass {
  92. br.Msg = "文件格式有误"
  93. br.ErrMsg = "文件格式有误"
  94. return
  95. }
  96. ext := path.Ext(h.Filename)
  97. dateDir := time.Now().Format("20060102")
  98. uploadDir := utils.STATIC_DIR + "hongze/" + dateDir
  99. err = os.MkdirAll(uploadDir, utils.DIR_MOD)
  100. if err != nil {
  101. br.Msg = "存储目录创建失败"
  102. br.ErrMsg = "存储目录创建失败,Err:" + err.Error()
  103. return
  104. }
  105. randStr := utils.GetRandStringNoSpecialChar(28)
  106. fileName := randStr + ext
  107. fpath := uploadDir + "/" + fileName
  108. defer f.Close() //关闭上传文件
  109. err = this.SaveToFile("file", fpath)
  110. if err != nil {
  111. br.Msg = "文件上传失败"
  112. br.ErrMsg = "文件上传失败,Err:" + err.Error()
  113. return
  114. }
  115. resourceUrl := ``
  116. //上传到阿里云 和 minio
  117. //if utils.ObjectStorageClient == "minio" {
  118. // resourceUrl, err = services.UploadImgToMinIo(fileName, fpath)
  119. // if err != nil {
  120. // br.Msg = "文件上传失败"
  121. // br.ErrMsg = "文件上传失败,Err:" + err.Error()
  122. // return
  123. // }
  124. //} else {
  125. // resourceUrl, err = services.UploadAliyunV2(fileName, fpath)
  126. // if err != nil {
  127. // br.Msg = "文件上传失败"
  128. // br.ErrMsg = "文件上传失败,Err:" + err.Error()
  129. // return
  130. // }
  131. //}
  132. ossClient := services.NewOssClient()
  133. if ossClient == nil {
  134. br.Msg = "上传失败"
  135. br.ErrMsg = "初始化OSS服务失败"
  136. return
  137. }
  138. resourceUrl, err = ossClient.UploadFile(fileName, fpath, "")
  139. if err != nil {
  140. br.Msg = "文件上传失败"
  141. br.ErrMsg = "文件上传失败,Err:" + err.Error()
  142. return
  143. }
  144. defer func() {
  145. os.Remove(fpath)
  146. }()
  147. item := new(models.Resource)
  148. item.ResourceUrl = resourceUrl
  149. item.ResourceType = 1
  150. item.CreateTime = time.Now()
  151. newId, err := models.AddResource(item)
  152. if err != nil {
  153. br.Msg = "资源上传失败"
  154. br.ErrMsg = "资源上传失败,Err:" + err.Error()
  155. return
  156. }
  157. resp := new(models.ResourceResp)
  158. resp.Id = newId
  159. resp.ResourceUrl = resourceUrl
  160. br.Msg = "上传成功"
  161. br.Ret = 200
  162. br.Success = true
  163. br.Data = resp
  164. return
  165. }
  166. // ClassifyIdDetail
  167. // @Title 根据分类获取最近一次报告详情接口
  168. // @Description 根据分类获取最近一次报告详情
  169. // @Param request body models.ClassifyIdDetailReq true "type json string"
  170. // @Success 200 {object} models.Report
  171. // @router /classifyIdDetail [get]
  172. func (this *ReportController) ClassifyIdDetail() {
  173. br := new(models.BaseResponse).Init()
  174. defer func() {
  175. this.Data["json"] = br
  176. this.ServeJSON()
  177. }()
  178. //classify_id_first=34&classify_id_second=36
  179. /*var req models.ClassifyIdDetailReq
  180. err := json.Unmarshal(this.Ctx.Input.RequestBody, &req)
  181. if err != nil {
  182. br.Msg = "参数解析异常!"
  183. br.ErrMsg = "参数解析失败,Err:" + err.Error()
  184. return
  185. }*/
  186. classifyIdFirst, _ := this.GetInt("classify_id_first")
  187. if classifyIdFirst <= 0 {
  188. classifyIdFirst, _ = this.GetInt("ClassifyIdFirst")
  189. }
  190. classifyIdSecond, _ := this.GetInt("classify_id_second")
  191. if classifyIdSecond <= 0 {
  192. classifyIdSecond, _ = this.GetInt("ClassifyIdSecond")
  193. }
  194. item, err := models.GetReportDetailByClassifyId(classifyIdFirst, classifyIdSecond)
  195. if err != nil && !utils.IsErrNoRow(err) {
  196. br.Msg = "获取失败!"
  197. br.ErrMsg = "获取失败,Err:" + err.Error()
  198. return
  199. }
  200. if item != nil {
  201. item.Content = html.UnescapeString(item.Content)
  202. item.ContentSub = html.UnescapeString(item.ContentSub)
  203. }
  204. br.Ret = 200
  205. br.Success = true
  206. br.Msg = "获取成功"
  207. br.Data = item
  208. }
  209. // SendTemplateMsg
  210. // @Title 模板消息推送接口
  211. // @Description 模板消息推送
  212. // @Param request body models.SendTemplateMsgReq true "type json string"
  213. // @Success 200 Ret=200 推送成功
  214. // @router /sendTemplateMsg [post]
  215. func (this *ReportController) SendTemplateMsg() {
  216. br := new(models.BaseResponse).Init()
  217. defer func() {
  218. this.Data["json"] = br
  219. this.ServeJSON()
  220. }()
  221. br.Ret = 200
  222. br.Success = true
  223. br.Msg = "操作成功"
  224. return
  225. }
  226. // Author
  227. // @Title 获取报告作者接口
  228. // @Description 获取报告作者
  229. // @Param AuthorType query int true "来源类型,1:中文,2:英文"
  230. // @Param StartDate query string true "开始时间"
  231. // @Success 200 {object} models.ReportAuthorResp
  232. // @router /author [get]
  233. func (this *ReportController) Author() {
  234. br := new(models.BaseResponse).Init()
  235. defer func() {
  236. this.Data["json"] = br
  237. this.ServeJSON()
  238. }()
  239. keyword := this.GetString("Keyword")
  240. var condition string
  241. var pars []interface{}
  242. condition += ` AND author_type = 1 AND enable = 1`
  243. if keyword != `` {
  244. condition += ` AND report_author like ? `
  245. pars = append(pars, utils.GetLikeKeyword(keyword))
  246. }
  247. _, items, err := models.GetReportAuthorList(condition, pars, 0, 10000)
  248. if err != nil {
  249. br.Msg = "获取失败!"
  250. br.ErrMsg = "获取失败,Err:" + err.Error()
  251. return
  252. }
  253. resp := models.ReportAuthorResp{
  254. List: items,
  255. }
  256. br.Ret = 200
  257. br.Success = true
  258. br.Msg = "获取成功"
  259. br.Data = resp
  260. }
  261. // UploadImg
  262. // @Title 图片上传
  263. // @Description 图片上传接口
  264. // @Param File query file true "文件"
  265. // @Success 200 上传成功
  266. // @router /uploadImg [post]
  267. func (this *ReportUploadCommonController) UploadImg() {
  268. var err error
  269. defer func() {
  270. if err != nil {
  271. fmt.Println("文件上传失败:", err.Error())
  272. go alarm_msg.SendAlarmMsg("URI:"+this.Ctx.Input.URI()+" 文件上传失败:"+err.Error(), 3)
  273. //go utils.SendEmail(utils.APPNAME+"失败提醒", "URI:"+this.Ctx.Input.URI()+" 文件上传失败:"+err.Error(), utils.EmailSendToUsers)
  274. }
  275. }()
  276. f, h, err := this.GetFile("file")
  277. if err != nil {
  278. return
  279. }
  280. defer f.Close() //关闭上传文件
  281. // 不依赖于文件扩展名检查文件格式
  282. fileData, e := io.ReadAll(f)
  283. if e != nil {
  284. err = fmt.Errorf("读取文件失败, Err: %s", e.Error())
  285. return
  286. }
  287. pass := filetype.IsImage(fileData)
  288. if !pass {
  289. kind, _ := filetype.Match(fileData)
  290. if kind.Extension != "pdf" {
  291. err = fmt.Errorf("文件格式有误")
  292. return
  293. }
  294. fmt.Printf("File type: %s. MIME: %s\n", kind.Extension, kind.MIME.Value)
  295. }
  296. ext := path.Ext(h.Filename)
  297. dateDir := time.Now().Format("20060102")
  298. uploadDir := utils.STATIC_DIR + "hongze/" + dateDir
  299. err = os.MkdirAll(uploadDir, utils.DIR_MOD)
  300. if err != nil {
  301. return
  302. }
  303. randStr := utils.GetRandStringNoSpecialChar(28)
  304. fileName := randStr + ext
  305. fpath := uploadDir + "/" + fileName
  306. err = this.SaveToFile("file", fpath)
  307. if err != nil {
  308. return
  309. }
  310. defer func() {
  311. os.Remove(fpath)
  312. }()
  313. // 上传至对象存储
  314. resourceUrl := ``
  315. ossClient := services.NewOssClient()
  316. if ossClient == nil {
  317. err = fmt.Errorf("初始化OSS服务失败")
  318. return
  319. }
  320. resourceUrl, err = ossClient.UploadFile(fileName, fpath, "")
  321. if err != nil {
  322. err = fmt.Errorf("文件上传失败, Err: %s", err.Error())
  323. return
  324. }
  325. item := new(models.Resource)
  326. item.ResourceUrl = resourceUrl
  327. item.ResourceType = 1
  328. item.CreateTime = time.Now()
  329. newId, err := models.AddResource(item)
  330. if err != nil {
  331. return
  332. }
  333. resp := new(models.ResourceResp)
  334. resp.Id = newId
  335. resp.ResourceUrl = resourceUrl
  336. this.Data["json"] = map[string]string{"link": resourceUrl}
  337. this.ServeJSON()
  338. }
  339. // Export
  340. // @Title 研报浏览数据导出
  341. // @Description 研报浏览数据导出接口
  342. // @Param ReportIds query string true "报告id,多个报告用英文,隔开"
  343. // @Success 200 {object} company.CompanyListResp
  344. // @router /report_view_record/export [get]
  345. func (this *ReportController) Export() {
  346. br := new(models.BaseResponse).Init()
  347. defer func() {
  348. this.Data["json"] = br
  349. this.ServeJSON()
  350. }()
  351. sysUser := this.SysUser
  352. if sysUser == nil {
  353. br.Msg = "请登录"
  354. br.ErrMsg = "请登录,SysUser Is Empty"
  355. br.Ret = 408
  356. return
  357. }
  358. reportIds := this.GetString("ReportIds")
  359. if reportIds == "" {
  360. br.Msg = "请选择需要下载的报告"
  361. br.ErrMsg = "请选择需要下载的报告"
  362. return
  363. }
  364. //生成excel文件
  365. dir, err := os.Executable()
  366. exPath := filepath.Dir(dir)
  367. downLoadnFilePath := exPath + "/" + time.Now().Format(utils.FormatDateTimeUnSpace) + ".xlsx"
  368. xlsxFile := xlsx.NewFile()
  369. if err != nil {
  370. br.Msg = "生成文件失败"
  371. br.ErrMsg = "生成文件失败"
  372. return
  373. }
  374. //只有权益研究员,以及超管可以下载
  375. if sysUser.RoleTypeCode != utils.ROLE_TYPE_CODE_RAI_RESEARCHR && sysUser.RoleTypeCode != utils.ROLE_TYPE_CODE_ADMIN {
  376. br.Msg = "没有下载权限"
  377. br.ErrMsg = "没有下载权限"
  378. return
  379. }
  380. //普通样式
  381. style := xlsx.NewStyle()
  382. alignment := xlsx.Alignment{
  383. Horizontal: "center",
  384. Vertical: "center",
  385. WrapText: true,
  386. }
  387. //设置默认字体和文字大小
  388. xlsx.SetDefaultFont(12, "宋体")
  389. style.Alignment = alignment
  390. style.ApplyAlignment = true
  391. //标题样式
  392. titleStyle := xlsx.NewStyle()
  393. titleFont := xlsx.NewFont(20, "宋体")
  394. titleFont.Bold = true
  395. titleStyle.Font = *titleFont
  396. titleStyle.Alignment = alignment
  397. //titleStyle.ApplyAlignment = true
  398. //表头
  399. headerStyle := xlsx.NewStyle()
  400. headerFont := xlsx.NewFont(12, "宋体")
  401. headerFont.Bold = true
  402. headerStyle.Font = *headerFont
  403. headerStyle.Alignment = alignment
  404. headerStyle.ApplyAlignment = true
  405. defer func() {
  406. os.Remove(downLoadnFilePath)
  407. }()
  408. //记录已经命名了的表单名称
  409. sheetNameMap := make(map[string]int)
  410. reportSlice := strings.Split(reportIds, ",")
  411. for _, reportIdStr := range reportSlice {
  412. reportId, convErr := strconv.Atoi(reportIdStr)
  413. if convErr != nil {
  414. br.Msg = "报告传参异常"
  415. br.ErrMsg = "报告传参异常"
  416. return
  417. }
  418. report, reportErr := models.GetReportById(reportId)
  419. if reportErr != nil {
  420. br.Msg = "查询报告异常"
  421. br.ErrMsg = "查询报告异常:" + reportErr.Error()
  422. return
  423. }
  424. //查询浏览记录
  425. _, viewList, err := models.GetViewListByReportId(reportId)
  426. //fmt.Println(total)
  427. //fmt.Println(viewList)
  428. //fmt.Println(err)
  429. if err != nil {
  430. br.Msg = "查询报告浏览记录异常"
  431. br.ErrMsg = "查询报告浏览记录异常:" + err.Error()
  432. return
  433. }
  434. //导入报表
  435. //报表名称
  436. createTime, _ := time.Parse(utils.FormatDateTime, report.CreateTime)
  437. timeStr := createTime.Format("0102")
  438. reportName := report.Title + "(" + timeStr + ")"
  439. //表单名称
  440. sheetName := reportName
  441. //判断当前sheet表单map中是否已经存在该表单名称,如果存在,那么需要添加后缀,并给sheet表单map中赋值次数加1处理;不存在,那么还是使用当前名称,并给sheet表单map中赋值次数为1
  442. if n, ok := sheetNameMap[sheetName]; ok == true {
  443. n++
  444. sheetName = sheetName + "_" + strconv.Itoa(n)
  445. sheetNameMap[sheetName] = n
  446. } else {
  447. sheetNameMap[sheetName] = 1
  448. }
  449. sheel, err := xlsxFile.AddSheet(sheetName)
  450. if err != nil {
  451. br.Msg = "新增Sheet失败"
  452. br.ErrMsg = "新增Sheet失败,Err:" + err.Error()
  453. return
  454. }
  455. //设置列宽
  456. sheel.SetColWidth(0, 0, 28)
  457. sheel.SetColWidth(1, 1, 18)
  458. sheel.SetColWidth(2, 2, 18)
  459. sheel.SetColWidth(3, 3, 18)
  460. sheel.SetColWidth(4, 4, 40)
  461. //标题行
  462. titleRow := sheel.AddRow()
  463. titleRow.SetHeight(40)
  464. //标题列
  465. titleCell := titleRow.AddCell()
  466. titleCell.HMerge = 4 //向右合并列数,不包括自身列
  467. //报表标题名称
  468. titleCell.SetValue(reportName)
  469. titleCell.SetStyle(titleStyle)
  470. //表头
  471. headerRow := sheel.AddRow()
  472. headerRow.SetHeight(18)
  473. timeHeaderCell := headerRow.AddCell()
  474. timeHeaderCell.SetValue("浏览时间")
  475. timeHeaderCell.SetStyle(headerStyle)
  476. nameHeaderCell := headerRow.AddCell()
  477. nameHeaderCell.SetValue("用户名称")
  478. nameHeaderCell.SetStyle(headerStyle)
  479. mobileHeaderCell := headerRow.AddCell()
  480. mobileHeaderCell.SetValue("手机号")
  481. mobileHeaderCell.SetStyle(headerStyle)
  482. emailHeaderCell := headerRow.AddCell()
  483. emailHeaderCell.SetValue("邮箱")
  484. emailHeaderCell.SetStyle(headerStyle)
  485. companyHeaderCell := headerRow.AddCell()
  486. companyHeaderCell.SetValue("所属企业客户名称")
  487. companyHeaderCell.SetStyle(headerStyle)
  488. for _, v := range viewList {
  489. dataRow := sheel.AddRow()
  490. dataRow.SetHeight(18)
  491. timeCell := dataRow.AddCell()
  492. timeCell.SetString(v.CreateTime.Format(utils.FormatDateTime))
  493. timeCell.SetStyle(style)
  494. nameCell := dataRow.AddCell()
  495. nameCell.SetString(v.RealName)
  496. nameCell.SetStyle(style)
  497. mobileCell := dataRow.AddCell()
  498. mobileCell.SetString(v.Mobile)
  499. mobileCell.SetStyle(style)
  500. emailCell := dataRow.AddCell()
  501. emailCell.SetString(v.Email)
  502. emailCell.SetStyle(style)
  503. companyCell := dataRow.AddCell()
  504. companyCell.SetString(v.CompanyName)
  505. companyCell.SetStyle(style)
  506. }
  507. }
  508. err = xlsxFile.Save(downLoadnFilePath)
  509. if err != nil {
  510. br.Msg = "保存文件失败"
  511. br.ErrMsg = "保存文件失败"
  512. return
  513. }
  514. randStr := time.Now().Format(utils.FormatDateTimeUnSpace)
  515. downloadFileName := "研报浏览记录" + randStr + ".xlsx"
  516. this.Ctx.Output.Download(downLoadnFilePath, downloadFileName)
  517. br.Ret = 200
  518. br.Success = true
  519. br.Msg = "导出成功"
  520. }
  521. // ThsSendTemplateMsg
  522. // @Title 客群消息推送接口
  523. // @Description 客群消息推送接口
  524. // @Param request body models.SendTemplateMsgReq true "type json string"
  525. // @Success 200 Ret=200 推送成功
  526. // @router /ths/sendTemplateMsg [post]
  527. func (this *ReportController) ThsSendTemplateMsg() {
  528. br := new(models.BaseResponse).Init()
  529. defer func() {
  530. this.Data["json"] = br
  531. this.ServeJSON()
  532. }()
  533. var req models.ThsSendTemplateMsgReq
  534. err := json.Unmarshal(this.Ctx.Input.RequestBody, &req)
  535. if err != nil {
  536. br.Msg = "参数解析异常!"
  537. br.ErrMsg = "参数解析失败,Err:" + err.Error()
  538. return
  539. }
  540. for _, v := range req.ReportId {
  541. if v <= 0 {
  542. br.Msg = "参数错误"
  543. br.ErrMsg = "参数错误"
  544. return
  545. }
  546. //加锁,避免重复点击造成的多次推送
  547. {
  548. redisKey := fmt.Sprint("report:send:ths:", v)
  549. ok := utils.Rc.SetNX(redisKey, 1, time.Second*300)
  550. if !ok {
  551. br.Msg = "报告已推送客群,请勿重复推送"
  552. return
  553. }
  554. defer func() {
  555. utils.Rc.Delete(redisKey)
  556. }()
  557. }
  558. report, err := models.GetReportById(v)
  559. if err != nil {
  560. if utils.IsErrNoRow(err) {
  561. br.Msg = "报告已经被删除,请刷新页面"
  562. br.ErrMsg = "报告已经被删除,请刷新页面,req:" + string(this.Ctx.Input.RequestBody)
  563. return
  564. }
  565. br.Msg = "获取报告信息失败"
  566. br.ErrMsg = "获取报告信息失败,req:" + string(this.Ctx.Input.RequestBody)
  567. return
  568. }
  569. if report.ThsMsgIsSend == 1 {
  570. br.Msg = "消息已推送,请勿重复操作"
  571. br.ErrMsg = "模板消息已推送,请勿重复操作1"
  572. return
  573. }
  574. err = models.ModifyReportThsMsgIsSend(report)
  575. if err != nil {
  576. br.Msg = "发送失败"
  577. br.ErrMsg = "发送失败,Err:" + err.Error()
  578. return
  579. }
  580. }
  581. br.Ret = 200
  582. br.Success = true
  583. br.Msg = "发送成功"
  584. }
  585. // GetDayWeekReportChapterTypeList
  586. // @Title 获取晨报周报章节类型列表
  587. // @Description 获取晨报周报章节类型列表
  588. // @Param StopType query string true "停更类型 stop; disable;"
  589. // @Success 200 {object} models.ReportListResp
  590. // @router /getDayWeekReportChapterTypeList [get]
  591. func (this *ReportController) GetDayWeekReportChapterTypeList() {
  592. br := new(models.BaseResponse).Init()
  593. defer func() {
  594. this.Data["json"] = br
  595. this.ServeJSON()
  596. }()
  597. sysUser := this.SysUser
  598. if sysUser == nil {
  599. br.Msg = "请登录"
  600. br.ErrMsg = "请登录,SysUser Is Empty"
  601. br.Ret = 408
  602. return
  603. }
  604. br.Ret = 200
  605. br.Success = true
  606. br.Msg = "操作成功"
  607. return
  608. }
  609. // GetDayWeekReportPauseTime
  610. // @Title 获取晨报周报的更新暂停时间
  611. // @Description 获取晨报周报的更新暂停时间
  612. // @Success 200 {object} models.ReportListResp
  613. // @router /getDayWeekReportPauseTime [get]
  614. func (this *ReportController) GetDayWeekReportPauseTime() {
  615. br := new(models.BaseResponse).Init()
  616. defer func() {
  617. this.Data["json"] = br
  618. this.ServeJSON()
  619. }()
  620. sysUser := this.SysUser
  621. if sysUser == nil {
  622. br.Msg = "请登录"
  623. br.ErrMsg = "请登录,SysUser Is Empty"
  624. br.Ret = 408
  625. return
  626. }
  627. br.Ret = 200
  628. br.Success = true
  629. br.Msg = "操作成功"
  630. return
  631. }
  632. // EditDayWeekReport
  633. // @Title 编辑晨周报
  634. // @Description 编辑晨周报
  635. // @Param request body models.SaveDayWeekReportReq true "type json string"
  636. // @Success 200 Ret=200 保存成功
  637. // @router /editDayWeekReport [post]
  638. func (this *ReportController) EditDayWeekReport() {
  639. br := new(models.BaseResponse).Init()
  640. defer func() {
  641. this.Data["json"] = br
  642. this.ServeJSON()
  643. }()
  644. sysUser := this.SysUser
  645. if sysUser == nil {
  646. br.Msg = "请登录"
  647. br.ErrMsg = "请登录,SysUser Is Empty"
  648. br.Ret = 408
  649. return
  650. }
  651. br.Ret = 200
  652. br.Success = true
  653. br.Msg = "操作成功"
  654. return
  655. }
  656. // GetLastDayWeekReportChapter
  657. // @Title 获取上一篇已发表的晨周报章节内容
  658. // @Description 获取上一篇已发表的晨周报章节内容
  659. // @Param TypeId query int true "品种ID"
  660. // @Param ReportType query string true "报告类型 day-晨报; week-周报; "
  661. // @Success 200 {object} company.CompanyListResp
  662. // @router /getLastDayWeekReportChapter [get]
  663. func (this *ReportController) GetLastDayWeekReportChapter() {
  664. br := new(models.BaseResponse).Init()
  665. defer func() {
  666. this.Data["json"] = br
  667. this.ServeJSON()
  668. }()
  669. sysUser := this.SysUser
  670. if sysUser == nil {
  671. br.Msg = "请登录"
  672. br.ErrMsg = "请登录,SysUser Is Empty"
  673. br.Ret = 408
  674. return
  675. }
  676. br.Ret = 200
  677. br.Success = true
  678. br.Msg = "操作成功"
  679. return
  680. }
  681. // GetChapterTrendTag
  682. // @Title 获取章节趋势标签列表
  683. // @Description 获取章节趋势标签列表
  684. // @Success 200 Ret=200 获取成功
  685. // @router /getChapterTrendTag [get]
  686. func (this *ReportController) GetChapterTrendTag() {
  687. br := new(models.BaseResponse).Init()
  688. defer func() {
  689. this.Data["json"] = br
  690. this.ServeJSON()
  691. }()
  692. sysUser := this.SysUser
  693. if sysUser == nil {
  694. br.Msg = "请登录"
  695. br.ErrMsg = "请登录,SysUser Is Empty"
  696. br.Ret = 408
  697. return
  698. }
  699. list, err := models.GetKeyWordListByFrom("trend")
  700. if err != nil {
  701. br.Msg = "获取章节趋势标签列表失败"
  702. br.ErrMsg = "获取章节趋势标签列表失败, Err: " + err.Error()
  703. return
  704. }
  705. br.Ret = 200
  706. br.Success = true
  707. br.Msg = "获取成功"
  708. br.Data = list
  709. }
  710. // GetDayReportTickerList
  711. // @Title 获取晨报数据指标列表
  712. // @Description 获取晨报数据指标列表
  713. // @Param ReportChapterId query int true "章节ID"
  714. // @Success 200 Ret=200 保存成功
  715. // @router /getDayReportTickerList [get]
  716. func (this *ReportController) GetDayReportTickerList() {
  717. br := new(models.BaseResponse).Init()
  718. defer func() {
  719. this.Data["json"] = br
  720. this.ServeJSON()
  721. }()
  722. sysUser := this.SysUser
  723. if sysUser == nil {
  724. br.Msg = "请登录"
  725. br.ErrMsg = "请登录,SysUser Is Empty"
  726. br.Ret = 408
  727. return
  728. }
  729. br.Ret = 200
  730. br.Success = true
  731. br.Msg = "操作成功"
  732. return
  733. }
  734. // GetDayWeekReportVideoList
  735. // @Title 获取晨周报音视频列表
  736. // @Description 获取晨周报音视频列表
  737. // @Param ReportId query int true "报告ID"
  738. // @Success 200 Ret=200 保存成功
  739. // @router /getDayWeekReportVideoList [get]
  740. func (this *ReportController) GetDayWeekReportVideoList() {
  741. br := new(models.BaseResponse).Init()
  742. defer func() {
  743. this.Data["json"] = br
  744. this.ServeJSON()
  745. }()
  746. sysUser := this.SysUser
  747. if sysUser == nil {
  748. br.Msg = "请登录"
  749. br.ErrMsg = "请登录,SysUser Is Empty"
  750. br.Ret = 408
  751. return
  752. }
  753. reportId, _ := this.GetInt("ReportId")
  754. if reportId <= 0 {
  755. br.Msg = "参数有误"
  756. return
  757. }
  758. list, err := models.GetReportChapterVideoList(reportId)
  759. if err != nil {
  760. br.Msg = "获取失败"
  761. br.ErrMsg = "获取报告音频列表失败, Err: " + err.Error()
  762. return
  763. }
  764. br.Ret = 200
  765. br.Success = true
  766. br.Msg = "获取成功"
  767. br.Data = list
  768. }
  769. // IsLastDayWeekReportChapter
  770. // @Title 是否为晨周报本期最后一个发布的报告
  771. // @Description 是否为晨周报本期最后一个发布的报告
  772. // @Param ReportChapterId query int true "报告章节ID"
  773. // @Success 200 Ret=200 获取成功
  774. // @router /isLastDayWeekReportChapter [get]
  775. func (this *ReportController) IsLastDayWeekReportChapter() {
  776. br := new(models.BaseResponse).Init()
  777. defer func() {
  778. this.Data["json"] = br
  779. this.ServeJSON()
  780. }()
  781. sysUser := this.SysUser
  782. if sysUser == nil {
  783. br.Msg = "请登录"
  784. br.ErrMsg = "请登录,SysUser Is Empty"
  785. br.Ret = 408
  786. return
  787. }
  788. br.Ret = 200
  789. br.Success = true
  790. br.Msg = "操作成功"
  791. return
  792. }
  793. // PublishDayWeekReport
  794. // @Title 发布晨周报
  795. // @Description 发布晨周报
  796. // @Param request body models.PublishDayWeekReportReq true "type json string"
  797. // @Success 200 Ret=200 操作成功
  798. // @router /publishDayWeekReport [post]
  799. func (this *ReportController) PublishDayWeekReport() {
  800. br := new(models.BaseResponse).Init()
  801. defer func() {
  802. this.Data["json"] = br
  803. this.ServeJSON()
  804. }()
  805. sysUser := this.SysUser
  806. if sysUser == nil {
  807. br.Msg = "请登录"
  808. br.ErrMsg = "请登录,SysUser Is Empty"
  809. br.Ret = 408
  810. return
  811. }
  812. br.Ret = 200
  813. br.Success = true
  814. br.Msg = "操作成功"
  815. return
  816. }
  817. // GetStopDayWeekReportChapterTypeList
  818. // @Title 获取暂停更新的晨报周报章节类型列表
  819. // @Description 获取暂停更新的晨报周报章节类型列表
  820. // @Success 200 {object} models.ReportListResp
  821. // @router /getStopDayWeekReportChapterTypeList [get]
  822. func (this *ReportController) GetStopDayWeekReportChapterTypeList() {
  823. br := new(models.BaseResponse).Init()
  824. defer func() {
  825. this.Data["json"] = br
  826. this.ServeJSON()
  827. }()
  828. sysUser := this.SysUser
  829. if sysUser == nil {
  830. br.Msg = "请登录"
  831. br.ErrMsg = "请登录,SysUser Is Empty"
  832. br.Ret = 408
  833. return
  834. }
  835. br.Ret = 200
  836. br.Success = true
  837. br.Msg = "获取成功"
  838. return
  839. }
  840. // MarkEditStatus
  841. // @Title 标记报告编辑状态
  842. // @Description 标记报告编辑状态接口
  843. // @Param request body request.MarkEditEnReport true "type json string"
  844. // @Success 200 标记成功 ;202 标记成功
  845. // @router /mark [post]
  846. func (this *ReportController) MarkEditStatus() {
  847. br := new(models.BaseResponse).Init()
  848. defer func() {
  849. this.Data["json"] = br
  850. this.ServeJSON()
  851. }()
  852. sysUser := this.SysUser
  853. if sysUser == nil {
  854. br.Msg = "请登录"
  855. br.ErrMsg = "请登录,SysUser Is Empty"
  856. br.Ret = 408
  857. return
  858. }
  859. var req models.MarkEditReport
  860. err := json.Unmarshal(this.Ctx.Input.RequestBody, &req)
  861. if err != nil {
  862. br.Msg = "参数解析异常!"
  863. br.ErrMsg = "参数解析失败,Err:" + err.Error()
  864. return
  865. }
  866. if req.ReportId <= 0 {
  867. br.Msg = "缺少报告Id"
  868. return
  869. }
  870. if req.Status < 0 {
  871. br.Msg = "标记状态异常"
  872. return
  873. }
  874. //更新标记key
  875. data, err := services.UpdateReportEditMark(req.ReportId, req.ReportChapterId, sysUser.AdminId, req.Status, sysUser.RealName, this.Lang)
  876. if err != nil {
  877. br.Msg = err.Error()
  878. return
  879. }
  880. msg := "标记成功"
  881. br.Ret = 200
  882. br.Success = true
  883. br.Msg = msg
  884. br.Data = data
  885. }
  886. // SendMsg
  887. // @Title 模板消息/客群消息推送接口
  888. // @Description 模板消息/客群消息推送接口
  889. // @Param request body models.SendTemplateMsgReq true "type json string"
  890. // @Success 200 {object} models.SendTemplateMsgResp
  891. // @router /sendMsg [post]
  892. func (this *ReportController) SendMsg() {
  893. br := new(models.BaseResponse).Init()
  894. defer func() {
  895. this.Data["json"] = br
  896. this.ServeJSON()
  897. }()
  898. var req models.SendTemplateMsgReq
  899. err := json.Unmarshal(this.Ctx.Input.RequestBody, &req)
  900. if err != nil {
  901. br.Msg = "参数解析异常!"
  902. br.ErrMsg = "参数解析失败,Err:" + err.Error()
  903. return
  904. }
  905. if req.ReportId <= 0 {
  906. br.Msg = "参数错误"
  907. br.ErrMsg = "参数错误"
  908. return
  909. }
  910. // 加锁,避免重复点击造成的多次推送
  911. {
  912. redisKey := fmt.Sprint("report:send:ths:", req.ReportId)
  913. ok := utils.Rc.SetNX(redisKey, 1, time.Second*300)
  914. if !ok {
  915. br.Msg = "报告已推送, 请勿重复推送"
  916. return
  917. }
  918. defer func() {
  919. _ = utils.Rc.Delete(redisKey)
  920. }()
  921. }
  922. reportInfo, e := models.GetReportByReportId(req.ReportId)
  923. if e != nil {
  924. br.Msg = "报告不存在或已被删除, 请刷新页面"
  925. br.ErrMsg = "获取报告失败, Err: " + e.Error()
  926. return
  927. }
  928. if reportInfo.MsgIsSend == 1 && reportInfo.ThsMsgIsSend == 1 {
  929. br.Msg = "消息已推送,请勿重复操作"
  930. br.ErrMsg = "消息已推送,请勿重复操作"
  931. return
  932. }
  933. // 更新报告发布时间
  934. videoNameDate := `(` + time.Now().Format("0102") + `)`
  935. if err = models.UpdateReportPublishTime(req.ReportId, videoNameDate); err != nil {
  936. br.Msg = "修改报告发布时间失败"
  937. br.ErrMsg = "修改发布时间失败,Err:" + err.Error()
  938. return
  939. }
  940. if err = models.UpdateReportChapterPublishTime(req.ReportId, videoNameDate); err != nil {
  941. br.Msg = "修改章节发布时间失败"
  942. br.ErrMsg = "修改发布时间失败,Err:" + err.Error()
  943. return
  944. }
  945. // 可能存在历史数据两个只推了一个所以此处加个判断
  946. // 推送模板消息
  947. if reportInfo.MsgIsSend == 0 {
  948. go services.SendMiniProgramReportWxMsg(reportInfo.Id)
  949. }
  950. // 更新推送消息状态
  951. reportInfo.MsgIsSend = 1
  952. reportInfo.ThsMsgIsSend = 1
  953. reportInfo.MsgSendTime = time.Now()
  954. reportInfo.ModifyTime = time.Now()
  955. err = reportInfo.UpdateReport([]string{"MsgIsSend", "ThsMsgIsSend", "MsgSendTime", "ModifyTime"})
  956. if err != nil {
  957. br.Msg = "推送失败"
  958. br.ErrMsg = "更新报告消息状态失败, Err:" + err.Error()
  959. return
  960. }
  961. br.Ret = 200
  962. br.Success = true
  963. br.Msg = "推送成功"
  964. br.IsAddLog = true
  965. br.Data = models.SendTemplateMsgResp{
  966. ReportId: reportInfo.Id,
  967. MsgSendTime: reportInfo.MsgSendTime.Format(utils.FormatDateTime),
  968. }
  969. }
  970. // CheckDayWeekReportChapterVideo
  971. // @Title 获取暂停更新的晨报周报章节类型列表
  972. // @Description 获取暂停更新的晨报周报章节类型列表
  973. // @Param ReportId query string true "报告ID"
  974. // @Success 200 {object} models.ReportListResp
  975. // @router /CheckDayWeekReportChapterVideo [get]
  976. func (this *ReportController) CheckDayWeekReportChapterVideo() {
  977. br := new(models.BaseResponse).Init()
  978. defer func() {
  979. this.Data["json"] = br
  980. this.ServeJSON()
  981. }()
  982. sysUser := this.SysUser
  983. if sysUser == nil {
  984. br.Msg = "请登录"
  985. br.ErrMsg = "请登录,SysUser Is Empty"
  986. br.Ret = 408
  987. return
  988. }
  989. reqReportId := this.GetString("ReportId")
  990. reportId, _ := strconv.Atoi(reqReportId)
  991. if reportId <= 0 {
  992. br.Msg = "报告ID有误"
  993. return
  994. }
  995. // 获取章节列表
  996. chapterList, err := models.GetChapterListByReportId(reportId)
  997. if err != nil {
  998. br.Msg = "获取章节列表失败"
  999. br.ErrMsg = "获取章节列表失败, Err: " + err.Error()
  1000. return
  1001. }
  1002. typeNameArr := make([]string, 0)
  1003. for _, v := range chapterList {
  1004. if v.VideoUrl == "" || (v.VideoUrl != "" && v.VideoKind == 2) {
  1005. typeNameArr = append(typeNameArr, v.TypeName)
  1006. }
  1007. }
  1008. br.Ret = 200
  1009. br.Success = true
  1010. br.Msg = "保存成功"
  1011. br.Data = typeNameArr
  1012. }