report_v2.go 47 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686
  1. package controllers
  2. import (
  3. "encoding/json"
  4. "eta_gn/eta_api/models"
  5. "eta_gn/eta_api/models/report"
  6. "eta_gn/eta_api/models/report_approve"
  7. "eta_gn/eta_api/models/smart_report"
  8. "eta_gn/eta_api/models/system"
  9. "eta_gn/eta_api/services"
  10. "eta_gn/eta_api/services/alarm_msg"
  11. "eta_gn/eta_api/services/data"
  12. "eta_gn/eta_api/utils"
  13. "fmt"
  14. "github.com/rdlucklib/rdluck_tools/paging"
  15. "html"
  16. "io"
  17. "os"
  18. "strconv"
  19. "strings"
  20. "time"
  21. )
  22. // ListReport
  23. // @Title 获取报告列表接口
  24. // @Description 获取报告列表
  25. // @Param PageSize query int true "每页数据条数"
  26. // @Param CurrentIndex query int true "当前页页码,从1开始"
  27. // @Param TimeType query string true "筛选的时间类别:publish_time(发布时间),modify_time(更新时间);approve_time(审批时间)"
  28. // @Param StartDate query string true "开始时间"
  29. // @Param EndDate query string true "结束时间"
  30. // @Param Frequency query string true "频度"
  31. // @Param ClassifyIdFirst query int true "一级分类id"
  32. // @Param ClassifyIdSecond query int true "二级分类id"
  33. // @Param ClassifyIdThird query int true "三级分类id"
  34. // @Param State query int true "状态"
  35. // @Param KeyWord query string true "搜索关键词"
  36. // @Param PublishSort query string true "desc:降序,asc 升序(预留)"
  37. // @Param FilterReportType query string true "筛选报告类型,1:公共研报,2:共享研报,3:我的研报"
  38. // @Success 200 {object} models.ReportListResp
  39. // @router /list [get]
  40. func (this *ReportController) ListReport() {
  41. br := new(models.BaseResponse).Init()
  42. defer func() {
  43. this.Data["json"] = br
  44. this.ServeJSON()
  45. }()
  46. pageSize, _ := this.GetInt("PageSize")
  47. currentIndex, _ := this.GetInt("CurrentIndex")
  48. timeType := this.GetString("TimeType")
  49. startDate := this.GetString("StartDate")
  50. endDate := this.GetString("EndDate")
  51. frequency := this.GetString("Frequency")
  52. classifyIdFirst, _ := this.GetInt("ClassifyIdFirst", 0)
  53. classifyIdSecond, _ := this.GetInt("ClassifyIdSecond", 0)
  54. classifyIdThird, _ := this.GetInt("ClassifyIdThird", 0)
  55. state, _ := this.GetInt("State")
  56. keyWord := this.GetString("KeyWord")
  57. msgIsSend, _ := this.GetInt("MsgIsSend")
  58. filterReportType, _ := this.GetInt("FilterReportType", 1)
  59. var startSize int
  60. if pageSize <= 0 {
  61. pageSize = utils.PageSize20
  62. }
  63. if currentIndex <= 0 {
  64. currentIndex = 1
  65. }
  66. startSize = utils.StartIndex(currentIndex, pageSize)
  67. if timeType == "" {
  68. timeType = "publish_time"
  69. }
  70. if timeType != "publish_time" && timeType != "modify_time" && timeType != "approve_time" {
  71. br.Msg = "请选择正确的时间"
  72. br.ErrMsg = "请选择正确的时间"
  73. return
  74. }
  75. var condition string
  76. var pars []interface{}
  77. if keyWord != "" {
  78. condition += ` AND (a.title LIKE ? OR a.admin_real_name LIKE ? ) `
  79. pars = utils.GetLikeKeywordPars(pars, keyWord, 2)
  80. }
  81. if startDate != "" {
  82. condition += ` AND a.` + timeType + ` >= ? `
  83. pars = append(pars, startDate)
  84. }
  85. if endDate != "" {
  86. condition += ` AND a.` + timeType + ` <= ? `
  87. pars = append(pars, endDate)
  88. }
  89. if frequency != "" {
  90. condition += ` AND a.frequency = ? `
  91. pars = append(pars, frequency)
  92. }
  93. if classifyIdFirst > 0 {
  94. condition += ` AND a.classify_id_first = ? `
  95. pars = append(pars, classifyIdFirst)
  96. }
  97. if classifyIdSecond > 0 {
  98. condition += ` AND a.classify_id_second = ? `
  99. pars = append(pars, classifyIdSecond)
  100. }
  101. if classifyIdThird > 0 {
  102. condition += ` AND a.classify_id_third = ? `
  103. pars = append(pars, classifyIdThird)
  104. }
  105. if state > 0 {
  106. condition += ` AND a.state = ? `
  107. pars = append(pars, state)
  108. }
  109. // 消息是否已推送 1-未推送; 2-已推送
  110. if msgIsSend == 1 {
  111. condition += ` AND (a.msg_is_send = 0 OR a.ths_msg_is_send = 0) `
  112. }
  113. if msgIsSend == 2 {
  114. condition += ` AND a.msg_is_send = 1 AND a.ths_msg_is_send = 1 `
  115. }
  116. var err error
  117. var total int
  118. var list []*models.ReportList
  119. switch filterReportType {
  120. // 筛选报告类型,1:公共研报,2:共享研报,3:我的研报
  121. case 1:
  122. condition += ` AND a.is_public_publish = ? `
  123. pars = append(pars, 1)
  124. condition += ` AND a.state in (2,6) `
  125. case 3:
  126. condition += ` AND a.admin_id = ? `
  127. pars = append(pars, this.SysUser.AdminId)
  128. case 2:
  129. condition += ` AND (a.admin_id = ? or b.admin_id = ?) `
  130. pars = append(pars, this.SysUser.AdminId, this.SysUser.AdminId)
  131. }
  132. // 共享报告需要连表查询,所以需要单独写
  133. if filterReportType == 2 {
  134. total, err = models.GetReportListCountByGrant(condition, pars)
  135. if err != nil {
  136. br.Msg = "获取失败"
  137. br.ErrMsg = "获取失败,Err:" + err.Error()
  138. return
  139. }
  140. list, err = models.GetReportListByGrant(condition, pars, startSize, pageSize)
  141. if err != nil {
  142. br.Msg = "获取失败"
  143. br.ErrMsg = "获取失败,Err:" + err.Error()
  144. return
  145. }
  146. } else {
  147. total, err = models.GetReportListCountV1(condition, pars)
  148. if err != nil {
  149. br.Msg = "获取失败"
  150. br.ErrMsg = "获取失败,Err:" + err.Error()
  151. return
  152. }
  153. list, err = models.GetReportListV1(condition, pars, startSize, pageSize)
  154. if err != nil {
  155. br.Msg = "获取失败"
  156. br.ErrMsg = "获取失败,Err:" + err.Error()
  157. return
  158. }
  159. }
  160. listLen := len(list)
  161. if listLen > 0 {
  162. pvMap := make(map[int]int)
  163. uvMap := make(map[int]int)
  164. reportIdArr := make([]int, 0)
  165. syncReportIdArr := make([]string, 0) // 同步过来的报告IDs
  166. oldAndNewReportIdMap := make(map[int]int) // 旧报告和新报告的id对应关系
  167. for i := 0; i < listLen; i++ {
  168. reportIdArr = append(reportIdArr, list[i].Id)
  169. if list[i].OldReportId > 0 && list[i].ReportLayout == 1 {
  170. syncReportIdArr = append(syncReportIdArr, strconv.Itoa(list[i].OldReportId))
  171. oldAndNewReportIdMap[list[i].OldReportId] = list[i].Id
  172. }
  173. pvMap[list[i].Id] = list[i].Pv
  174. uvMap[list[i].Id] = list[i].Uv
  175. }
  176. // 当下报告的pv,uv
  177. if len(reportIdArr) > 0 {
  178. pvList, e := models.GetReportPvUvByReportIdList(reportIdArr)
  179. if e != nil {
  180. br.Msg = "获取失败"
  181. br.ErrMsg = "获取同步报告对应的PV、UV失败, Err: " + e.Error()
  182. return
  183. }
  184. for _, v := range pvList {
  185. pv := pvMap[v.ReportId]
  186. uv := uvMap[v.ReportId]
  187. pvMap[v.ReportId] = v.PvTotal + pv
  188. uvMap[v.ReportId] = v.UvTotal + uv
  189. }
  190. }
  191. //reportIds := strings.Join(reportIdArr, ",")
  192. //syncReportIds := strings.Join(syncReportIdArr, ",")
  193. // 查询同步过来的报告对应的老报告PV+UV
  194. if len(syncReportIdArr) > 0 {
  195. puvList, e := models.GetPUVByResearchReportIds(syncReportIdArr)
  196. if e != nil {
  197. br.Msg = "获取失败"
  198. br.ErrMsg = "获取同步报告对应的PV、UV失败, Err: " + e.Error()
  199. return
  200. }
  201. puvLen := len(puvList)
  202. for i := 0; i < puvLen; i++ {
  203. newReportId, ok := oldAndNewReportIdMap[puvList[i].ResearchReportId]
  204. if ok {
  205. pv := pvMap[newReportId]
  206. uv := uvMap[newReportId]
  207. pvMap[newReportId] = puvList[i].Pv + pv
  208. uvMap[newReportId] = puvList[i].Uv + uv
  209. }
  210. }
  211. }
  212. // 晨周报音频列表
  213. videoList, err := models.GetReportChapterVideoListByReportIds(reportIdArr)
  214. if err != nil {
  215. br.Msg = "获取失败"
  216. br.ErrMsg = "获取报告音频文件失败,Err:" + err.Error()
  217. return
  218. }
  219. for i := 0; i < listLen; i++ {
  220. list[i].Content = html.UnescapeString(list[i].Content)
  221. list[i].ContentSub = html.UnescapeString(list[i].ContentSub)
  222. // 除周报外其余报告均可推送客群
  223. list[i].NeedThsMsg = 1
  224. //if list[i].HasChapter == 1 && list[i].ChapterType == utils.REPORT_TYPE_WEEK {
  225. // list[i].NeedThsMsg = 0
  226. //}
  227. chapterList := make([]*models.ReportChapterVideoList, 0)
  228. for ii := 0; ii < len(videoList); ii++ {
  229. if list[i].Id == videoList[ii].ReportId {
  230. chapterList = append(chapterList, videoList[ii])
  231. }
  232. }
  233. list[i].ChapterVideoList = chapterList
  234. list[i].Pv = pvMap[list[i].Id]
  235. list[i].Uv = uvMap[list[i].Id]
  236. }
  237. // 多人协作的协作报告,需要判断是否可编辑
  238. {
  239. grantObj := report.ReportGrant{}
  240. grantList, err := grantObj.GetGrantListByIdList(reportIdArr)
  241. if err != nil {
  242. br.Msg = "获取失败"
  243. br.ErrMsg = "获取报告授权失败,Err:" + err.Error()
  244. return
  245. }
  246. //grantMap := make(map[报告id]map[用户id]bool)
  247. grantMap := make(map[int]map[int]bool)
  248. for _, v := range grantList {
  249. grantUserMap, ok := grantMap[v.ReportId]
  250. if !ok {
  251. grantUserMap = make(map[int]bool)
  252. }
  253. grantUserMap[v.AdminId] = true
  254. grantMap[v.ReportId] = grantUserMap
  255. }
  256. for i, item := range list {
  257. if item.AdminId == this.SysUser.AdminId {
  258. list[i].HasAuth = true
  259. continue
  260. }
  261. // 查找授权
  262. var hasAuth bool
  263. grantUserMap, ok := grantMap[item.Id]
  264. // 如果报告根本没有授权用户,说明没有授权当前用户
  265. if !ok {
  266. continue
  267. }
  268. _, ok = grantUserMap[this.SysUser.AdminId]
  269. list[i].HasAuth = hasAuth
  270. // 如果报告关联用户找到,说明有授权当前用户
  271. if ok {
  272. list[i].HasAuth = true
  273. }
  274. }
  275. }
  276. }
  277. for _, item := range list {
  278. /*key := fmt.Sprint(`crm:report:edit:`, item.Id)
  279. opUserId, _ := utils.Rc.RedisInt(key)
  280. //如果当前没有人操作,获取当前操作人是本人,那么编辑按钮可用
  281. if opUserId <= 0 || (opUserId == this.SysUser.AdminId) || item.ClassifyNameFirst == "周报" || item.ClassifyNameFirst == "晨报" {
  282. item.CanEdit = true
  283. } else {
  284. adminInfo, errAdmin := system.GetSysUserById(opUserId)
  285. if errAdmin != nil {
  286. br.Msg = "获取失败"
  287. br.ErrMsg = "获取失败,Err:" + errAdmin.Error()
  288. return
  289. }
  290. item.Editor = adminInfo.RealName
  291. }*/
  292. if item.HasChapter == 1 {
  293. item.CanEdit = true
  294. continue
  295. }
  296. markStatus, err := services.UpdateReportEditMark(item.Id, 0, this.SysUser.AdminId, 2, this.SysUser.RealName, this.Lang)
  297. if err != nil {
  298. br.Msg = "查询标记状态失败"
  299. br.ErrMsg = "查询标记状态失败,Err:" + err.Error()
  300. return
  301. }
  302. if markStatus.Status == 0 {
  303. item.CanEdit = true
  304. } else {
  305. item.Editor = markStatus.Editor
  306. }
  307. }
  308. page := paging.GetPaging(currentIndex, pageSize, total)
  309. resp := new(models.ReportListResp)
  310. resp.Paging = page
  311. resp.List = list
  312. br.Ret = 200
  313. br.Success = true
  314. br.Msg = "获取成功"
  315. br.Data = resp
  316. }
  317. // Add
  318. // @Title 新增报告接口
  319. // @Description 新增报告(不区分报告类型)
  320. // @Param request body models.AddReq true "type json string"
  321. // @Success 200 {object} models.AddResp
  322. // @router /add [post]
  323. func (this *ReportController) Add() {
  324. br := new(models.BaseResponse).Init()
  325. defer func() {
  326. this.Data["json"] = br
  327. this.ServeJSON()
  328. }()
  329. sysUser := this.SysUser
  330. if sysUser == nil {
  331. br.Msg = "请登录"
  332. br.ErrMsg = "请登录,SysUser Is Empty"
  333. br.Ret = 408
  334. return
  335. }
  336. var req models.AddReq
  337. err := json.Unmarshal(this.Ctx.Input.RequestBody, &req)
  338. if err != nil {
  339. br.Msg = "参数解析异常!"
  340. br.ErrMsg = "参数解析失败,Err:" + err.Error()
  341. return
  342. }
  343. if req.Title == `` {
  344. br.Msg = "标题不能为空"
  345. br.ErrMsg = "标题不能为空"
  346. br.IsSendEmail = false
  347. return
  348. }
  349. if req.ClassifyIdFirst <= 0 {
  350. br.Msg = "分类必填"
  351. br.ErrMsg = "分类必填"
  352. br.IsSendEmail = false
  353. return
  354. }
  355. var contentSub string
  356. if req.Content != "" {
  357. e := utils.ContentXssCheck(req.Content)
  358. if e != nil {
  359. br.Msg = "存在非法标签"
  360. br.ErrMsg = "存在非法标签, Err: " + e.Error()
  361. return
  362. }
  363. content, e := services.FilterReportContentBr(req.Content)
  364. if e != nil {
  365. br.Msg = "内容去除前后空格失败"
  366. br.ErrMsg = "内容去除前后空格失败, Err: " + e.Error()
  367. return
  368. }
  369. req.Content = content
  370. contentSub, err = services.GetReportContentSub(req.Content)
  371. if err != nil {
  372. go alarm_msg.SendAlarmMsg("ContentSub 失败,Err:"+err.Error(), 3)
  373. //utils.SendEmail(utils.APPNAME+"失败提醒", "解析 ContentSub 失败,Err:"+err.Error(), utils.EmailSendToUsers)
  374. }
  375. }
  376. // 报告期数
  377. maxStage, err := models.GetReportStage(req.ClassifyIdFirst, req.ClassifyIdSecond, req.ClassifyIdThird)
  378. if err != nil {
  379. br.Msg = "期数获取失败!"
  380. br.ErrMsg = "期数获取失败,Err:" + err.Error()
  381. return
  382. }
  383. // 根据审批开关及审批流判断当前报告状态
  384. state, e := services.CheckReportCurrState(report_approve.FlowReportTypeChinese, req.ClassifyIdFirst, req.ClassifyIdSecond, req.ClassifyIdThird, models.ReportOperateAdd)
  385. if e != nil {
  386. br.Msg = "操作失败"
  387. br.ErrMsg = "校验报告当前状态失败, Err: " + e.Error()
  388. return
  389. }
  390. // 协作方式,1:个人,2:多人协作。默认:1
  391. if req.CollaborateType == 0 {
  392. req.CollaborateType = 1
  393. }
  394. // 报告布局,1:常规布局,2:智能布局。默认:1
  395. if req.ReportLayout == 0 {
  396. req.ReportLayout = 1
  397. }
  398. // 是否公开发布,1:是,2:否
  399. if req.IsPublicPublish == 0 {
  400. req.IsPublicPublish = 1
  401. }
  402. classifyItemList, err := models.GetClassifyListByIdList([]int{req.ClassifyIdFirst, req.ClassifyIdSecond, req.ClassifyIdThird})
  403. classifyMap := make(map[int]string)
  404. for _, v := range classifyItemList {
  405. classifyMap[v.Id] = v.ClassifyName
  406. }
  407. item := new(models.Report)
  408. item.AddType = req.AddType
  409. item.ReportVersion = 2
  410. item.ClassifyIdFirst = req.ClassifyIdFirst
  411. item.ClassifyNameFirst = classifyMap[req.ClassifyIdFirst]
  412. item.ClassifyIdSecond = req.ClassifyIdSecond
  413. item.ClassifyNameSecond = classifyMap[req.ClassifyIdSecond]
  414. item.Title = req.Title
  415. item.Abstract = req.Abstract
  416. item.Author = req.Author
  417. item.Frequency = req.Frequency
  418. item.State = state
  419. item.Content = html.EscapeString(req.Content)
  420. item.Stage = maxStage + 1
  421. item.ContentSub = html.EscapeString(contentSub)
  422. if req.CreateTime != `` {
  423. formatDate := utils.FormatDateTime
  424. if !strings.Contains(req.CreateTime, ":") {
  425. formatDate = utils.FormatDate
  426. }
  427. createTime, tmpErr := time.ParseInLocation(formatDate, req.CreateTime, time.Local)
  428. if tmpErr != nil {
  429. br.Msg = "时间解析异常"
  430. br.ErrMsg = "时间解析异常,Err:" + err.Error()
  431. return
  432. }
  433. item.CreateTime = createTime
  434. }
  435. item.ModifyTime = time.Now()
  436. item.ReportVersion = req.ReportVersion
  437. item.AdminId = sysUser.AdminId
  438. item.AdminRealName = sysUser.RealName
  439. item.ClassifyIdThird = req.ClassifyIdThird
  440. item.ClassifyNameThird = classifyMap[req.ClassifyIdThird]
  441. // 产品要求,如果是多人协作,那么就是章节类型的报告
  442. if req.CollaborateType == 2 {
  443. item.HasChapter = 1
  444. item.ChapterType = ""
  445. }
  446. item.LastModifyAdminId = sysUser.AdminId
  447. item.LastModifyAdminName = sysUser.RealName
  448. item.ContentModifyTime = time.Now()
  449. item.NeedSplice = 1
  450. item.ContentStruct = html.EscapeString(req.ContentStruct)
  451. item.HeadImg = req.HeadImg
  452. item.EndImg = req.EndImg
  453. item.CanvasColor = req.CanvasColor
  454. item.HeadResourceId = req.HeadResourceId
  455. item.EndResourceId = req.EndResourceId
  456. item.CollaborateType = req.CollaborateType
  457. item.ReportLayout = req.ReportLayout
  458. item.IsPublicPublish = req.IsPublicPublish
  459. item.ReportCreateTime = time.Now()
  460. err, errMsg := services.AddReportAndChapter(item, req.InheritReportId, req.GrantAdminIdList)
  461. if err != nil {
  462. br.Msg = "保存失败"
  463. if errMsg != "" {
  464. br.Msg = errMsg
  465. }
  466. br.ErrMsg = "保存失败,Err:" + err.Error()
  467. return
  468. }
  469. recordItem := &models.ReportStateRecord{
  470. ReportId: item.Id,
  471. ReportType: 1,
  472. State: 1,
  473. AdminId: this.SysUser.AdminId,
  474. AdminName: this.SysUser.AdminName,
  475. CreateTime: time.Now(),
  476. }
  477. go func() {
  478. _, _ = models.AddReportStateRecord(recordItem)
  479. }()
  480. resp := new(models.AddResp)
  481. resp.ReportId = int64(item.Id)
  482. resp.ReportCode = item.ReportCode
  483. br.Ret = 200
  484. br.Success = true
  485. br.Msg = "保存成功"
  486. br.Data = resp
  487. }
  488. // Edit
  489. // @Title 编辑报告基础信息接口
  490. // @Description 编辑报告基础信息(不区分报告类型)
  491. // @Param request body models.EditReq true "type json string"
  492. // @Success 200 {object} models.EditResp
  493. // @router /edit [post]
  494. func (this *ReportController) Edit() {
  495. br := new(models.BaseResponse).Init()
  496. defer func() {
  497. this.Data["json"] = br
  498. this.ServeJSON()
  499. }()
  500. sysUser := this.SysUser
  501. if sysUser == nil {
  502. br.Msg = "请登录"
  503. br.ErrMsg = "请登录,SysUser Is Empty"
  504. br.Ret = 408
  505. return
  506. }
  507. var req models.EditReq
  508. err := json.Unmarshal(this.Ctx.Input.RequestBody, &req)
  509. if err != nil {
  510. br.Msg = "参数解析异常!"
  511. br.ErrMsg = "参数解析失败,Err:" + err.Error()
  512. return
  513. }
  514. //if req.Content == "" {
  515. // br.Msg = "报告内容不能为空"
  516. // return
  517. //}
  518. //更新标记key
  519. markStatus, err := services.UpdateReportEditMark(int(req.ReportId), 0, sysUser.AdminId, 1, sysUser.RealName, this.Lang)
  520. if err != nil {
  521. br.Msg = err.Error()
  522. return
  523. }
  524. if markStatus.Status == 1 {
  525. br.Msg = markStatus.Msg
  526. //br.Ret = 202 //202 服务器已接受请求,但尚未处理。
  527. return
  528. }
  529. reportInfo, e := models.GetReportByReportId(int(req.ReportId))
  530. if e != nil {
  531. if utils.IsErrNoRow(e) {
  532. br.Msg = "报告已被删除, 请刷新页面"
  533. return
  534. }
  535. br.Msg = "操作失败"
  536. br.ErrMsg = "获取报告失败, Err: " + e.Error()
  537. return
  538. }
  539. if reportInfo.State == models.ReportStatePublished || reportInfo.State == models.ReportStatePass {
  540. br.Msg = "该报告已发布,不允许编辑"
  541. br.ErrMsg = "该报告已发布,不允许编辑"
  542. br.IsSendEmail = false
  543. return
  544. }
  545. // 编辑报告信息
  546. err, errMsg := services.EditReport(reportInfo, req, sysUser)
  547. if err != nil {
  548. br.Msg = "保存失败"
  549. if errMsg != "" {
  550. br.Msg = errMsg
  551. }
  552. br.ErrMsg = "保存失败,Err:" + err.Error()
  553. return
  554. }
  555. reportCode := utils.MD5(strconv.Itoa(int(req.ReportId)))
  556. resp := new(models.EditResp)
  557. resp.ReportId = req.ReportId
  558. resp.ReportCode = reportCode
  559. br.Ret = 200
  560. br.Success = true
  561. br.Msg = "保存成功"
  562. br.Data = resp
  563. }
  564. // Detail
  565. // @Title 获取报告详情接口
  566. // @Description 获取报告详情
  567. // @Param request body models.ReportDetailReq true "type json string"
  568. // @Success 200 {object} models.Report
  569. // @router /detail [get]
  570. func (this *ReportController) Detail() {
  571. br := new(models.BaseResponse).Init()
  572. defer func() {
  573. this.Data["json"] = br
  574. this.ServeJSON()
  575. }()
  576. /*var req models.ReportDetailReq
  577. err := json.Unmarshal(this.Ctx.Input.RequestBody, &req)
  578. if err != nil {
  579. br.Msg = "参数解析异常!"
  580. br.ErrMsg = "参数解析失败,Err:" + err.Error()
  581. return
  582. }
  583. if req.ReportId <= 0 {
  584. br.Msg = "参数错误"
  585. return
  586. }*/
  587. reportId, err := this.GetInt("ReportId")
  588. if err != nil {
  589. br.Msg = "获取参数失败!"
  590. br.ErrMsg = "获取参数失败,Err:" + err.Error()
  591. return
  592. }
  593. if reportId <= 0 {
  594. br.Msg = "参数错误"
  595. return
  596. }
  597. item, err := models.GetReportById(reportId)
  598. if err != nil {
  599. if utils.IsErrNoRow(err) {
  600. br.Msg = "报告已被删除"
  601. return
  602. }
  603. br.Msg = "获取失败"
  604. br.ErrMsg = "获取失败,Err:" + err.Error()
  605. return
  606. }
  607. chapterList := make([]*models.ReportChapter, 0)
  608. if item.HasChapter == 1 {
  609. // 获取章节内容
  610. tmpChapterList, err := models.GetPublishedChapterListByReportId(item.Id)
  611. if err != nil {
  612. br.Msg = "获取失败"
  613. br.ErrMsg = "获取晨/周报章节列表失败, Err: " + err.Error()
  614. return
  615. }
  616. if len(tmpChapterList) > 0 {
  617. // 章节类型的字段赋值
  618. for _, item := range tmpChapterList {
  619. item.Content = html.UnescapeString(item.Content)
  620. item.ContentSub = html.UnescapeString(item.ContentSub)
  621. chapterList = append(chapterList, item)
  622. }
  623. }
  624. //item.Abstract = item.Title
  625. }
  626. item.Content = html.UnescapeString(item.Content)
  627. item.ContentSub = html.UnescapeString(item.ContentSub)
  628. item.ContentStruct = html.UnescapeString(item.ContentStruct)
  629. if item.HeadResourceId > 0 {
  630. headResource, err := smart_report.GetResourceItemById(item.HeadResourceId)
  631. if err != nil {
  632. br.Msg = "操作失败"
  633. br.ErrMsg = "获取资源库版头失败, Err: " + err.Error()
  634. return
  635. }
  636. item.HeadImg = headResource.ImgUrl
  637. item.HeadStyle = headResource.Style
  638. }
  639. if item.EndResourceId > 0 {
  640. endResource, err := smart_report.GetResourceItemById(item.EndResourceId)
  641. if err != nil {
  642. br.Msg = "操作失败"
  643. br.ErrMsg = "获取资源库版头失败, Err: " + err.Error()
  644. return
  645. }
  646. item.EndImg = endResource.ImgUrl
  647. item.EndStyle = endResource.Style
  648. }
  649. resp := &models.ReportDetailView{
  650. ReportDetail: item,
  651. ChapterList: chapterList,
  652. }
  653. br.Ret = 200
  654. br.Success = true
  655. br.Msg = "获取成功"
  656. br.Data = resp
  657. }
  658. // SaveReportContent
  659. // @Title 保存草稿
  660. // @Description 保存草稿
  661. // @Param request body models.SaveReportContent true "type json string"
  662. // @Success 200 {object} models.ReportAuthorResp
  663. // @router /saveReportContent [post]
  664. func (this *ReportController) SaveReportContent() {
  665. br := new(models.BaseResponse).Init()
  666. br.IsSendEmail = false
  667. defer func() {
  668. this.Data["json"] = br
  669. this.ServeJSON()
  670. }()
  671. sysUser := this.SysUser
  672. if sysUser == nil {
  673. br.Msg = "请登录"
  674. br.ErrMsg = "请登录,SysUser Is Empty"
  675. br.Ret = 408
  676. return
  677. }
  678. var req models.SaveReportContent
  679. err := json.Unmarshal(this.Ctx.Input.RequestBody, &req)
  680. if err != nil {
  681. br.Msg = "参数解析异常!"
  682. br.ErrMsg = "参数解析失败,Err:" + err.Error()
  683. return
  684. }
  685. reportId := req.ReportId
  686. noChangeFlag := req.NoChange
  687. if reportId <= 0 {
  688. resp := new(models.SaveReportContentResp)
  689. resp.ReportId = reportId
  690. br.Ret = 200
  691. br.Success = true
  692. br.Msg = "保存成功"
  693. br.Data = resp
  694. return
  695. }
  696. // 获取报告详情
  697. reportInfo, _ := models.GetReportByReportId(req.ReportId)
  698. if reportInfo != nil && reportInfo.State == 2 {
  699. br.Msg = "该报告已发布,不允许编辑"
  700. br.ErrMsg = "该报告已发布,不允许编辑"
  701. br.IsSendEmail = false
  702. return
  703. }
  704. // 标记更新中
  705. {
  706. markStatus, err := services.UpdateReportEditMark(req.ReportId, 0, sysUser.AdminId, 1, sysUser.RealName, this.Lang)
  707. if err != nil {
  708. br.Msg = err.Error()
  709. return
  710. }
  711. if markStatus.Status == 1 {
  712. br.Msg = markStatus.Msg
  713. return
  714. }
  715. }
  716. // 内容有过修改的话,那么逻辑处理
  717. if noChangeFlag != 1 {
  718. content := req.Content
  719. if content == "" {
  720. content = this.GetString("Content")
  721. }
  722. if content != "" {
  723. e := utils.ContentXssCheck(req.Content)
  724. if e != nil {
  725. br.Msg = "存在非法标签"
  726. br.ErrMsg = "存在非法标签, Err: " + e.Error()
  727. return
  728. }
  729. contentClean, e := services.FilterReportContentBr(req.Content)
  730. if e != nil {
  731. br.Msg = "内容去除前后空格失败"
  732. br.ErrMsg = "内容去除前后空格失败, Err: " + e.Error()
  733. return
  734. }
  735. content = contentClean
  736. contentSub, err := services.GetReportContentSub(content)
  737. if err != nil {
  738. go alarm_msg.SendAlarmMsg("解析 ContentSub 失败,Err:"+err.Error(), 3)
  739. //utils.SendEmail(utils.APPNAME+"失败提醒", "解析 ContentSub 失败,Err:"+err.Error(), utils.EmailSendToUsers)
  740. }
  741. reportInfo.Content = html.EscapeString(content)
  742. reportInfo.ContentSub = html.EscapeString(contentSub)
  743. reportInfo.ContentStruct = html.EscapeString(req.ContentStruct)
  744. reportInfo.HeadImg = req.HeadImg
  745. reportInfo.EndImg = req.EndImg
  746. reportInfo.CanvasColor = req.CanvasColor
  747. reportInfo.HeadResourceId = req.HeadResourceId
  748. reportInfo.EndResourceId = req.EndResourceId
  749. reportInfo.ModifyTime = time.Now()
  750. reportInfo.ContentModifyTime = time.Now()
  751. updateCols := []string{"Content", "ContentSub", "ContentStruct", "HeadImg", "EndImg", "CanvasColor", "HeadResourceId", "EndResourceId", "ModifyTime", "ContentModifyTime"}
  752. err = reportInfo.UpdateReport(updateCols)
  753. if err != nil {
  754. br.Msg = "保存失败"
  755. br.ErrMsg = "保存失败,Err:" + err.Error()
  756. return
  757. }
  758. go models.AddReportSaveLog(reportId, this.SysUser.AdminId, reportInfo.Content, reportInfo.ContentSub, reportInfo.ContentStruct, reportInfo.CanvasColor, this.SysUser.AdminName, reportInfo.HeadResourceId, reportInfo.EndResourceId)
  759. }
  760. }
  761. resp := new(models.SaveReportContentResp)
  762. resp.ReportId = reportId
  763. br.Ret = 200
  764. br.Success = true
  765. br.Msg = "保存成功"
  766. br.Data = resp
  767. }
  768. // AuthorizedListReport
  769. // @Title 获取有权限的报告列表接口
  770. // @Description 获取有权限的报告列表接口
  771. // @Param PageSize query int true "每页数据条数"
  772. // @Param CurrentIndex query int true "当前页页码,从1开始"
  773. // @Param Keyword query string true "搜索关键词"
  774. // @Param ClassifyIdFirst query int true "一级分类id"
  775. // @Param ClassifyIdSecond query int true "二级分类id"
  776. // @Param ClassifyIdThird query int true "三级分类id"
  777. // @Success 200 {object} models.ReportListResp
  778. // @router /list/authorized [get]
  779. func (this *ReportController) AuthorizedListReport() {
  780. br := new(models.BaseResponse).Init()
  781. defer func() {
  782. this.Data["json"] = br
  783. this.ServeJSON()
  784. }()
  785. pageSize, _ := this.GetInt("PageSize")
  786. currentIndex, _ := this.GetInt("CurrentIndex")
  787. keyword := this.GetString("Keyword")
  788. classifyIdFirst, _ := this.GetInt("ClassifyIdFirst", 0)
  789. classifyIdSecond, _ := this.GetInt("ClassifyIdSecond", 0)
  790. classifyIdThird, _ := this.GetInt("ClassifyIdThird", 0)
  791. var startSize int
  792. if pageSize <= 0 {
  793. pageSize = utils.PageSize20
  794. }
  795. if currentIndex <= 0 {
  796. currentIndex = 1
  797. }
  798. startSize = utils.StartIndex(currentIndex, pageSize)
  799. var list []*models.ReportList
  800. // 没有输入信息,那就不展示
  801. if keyword == `` && classifyIdFirst <= 0 {
  802. page := paging.GetPaging(currentIndex, pageSize, 0)
  803. resp := new(models.ReportListResp)
  804. resp.Paging = page
  805. resp.List = list
  806. br.Ret = 200
  807. br.Success = true
  808. br.Msg = "获取成功"
  809. br.Data = resp
  810. return
  811. }
  812. // 当前用户有权限的报告id列表
  813. grantReportIdList := make([]int, 0)
  814. {
  815. obj := report.ReportGrant{}
  816. grantList, err := obj.GetGrantListByAdminId(this.SysUser.AdminId)
  817. if err != nil {
  818. br.Msg = "获取失败"
  819. br.ErrMsg = "获取授权报告id失败,Err:" + err.Error()
  820. return
  821. }
  822. for _, v := range grantList {
  823. grantReportIdList = append(grantReportIdList, v.ReportId)
  824. }
  825. }
  826. var condition string
  827. var pars []interface{}
  828. if classifyIdFirst > 0 {
  829. condition += ` AND a.classify_id_first = ? `
  830. pars = append(pars, classifyIdFirst)
  831. }
  832. if classifyIdSecond > 0 {
  833. condition += ` AND a.classify_id_second = ? `
  834. pars = append(pars, classifyIdSecond)
  835. }
  836. if classifyIdThird > 0 {
  837. condition += ` AND a.classify_id_third = ? `
  838. pars = append(pars, classifyIdThird)
  839. }
  840. if keyword != `` {
  841. condition += ` AND a.title LIKE ? `
  842. pars = utils.GetLikeKeywordPars(pars, keyword, 1)
  843. }
  844. var err error
  845. var total int
  846. orCondition := `AND ( (a.is_public_publish = ? AND a.state in (2,6)) or a.admin_id = ? `
  847. pars = append(pars, 1, this.SysUser.AdminId)
  848. // 当前用户有权限的报告id列表
  849. num := len(grantReportIdList)
  850. if num > 0 {
  851. orCondition += ` OR a.id in (` + utils.GetOrmInReplace(num) + `)`
  852. pars = append(pars, grantReportIdList)
  853. }
  854. orCondition += ` ) `
  855. condition += orCondition
  856. total, err = models.GetReportListCountByAuthorized(condition, pars)
  857. if err != nil {
  858. br.Msg = "获取失败"
  859. br.ErrMsg = "获取失败,Err:" + err.Error()
  860. return
  861. }
  862. list, err = models.GetReportListByAuthorized(condition, pars, startSize, pageSize)
  863. if err != nil {
  864. br.Msg = "获取失败"
  865. br.ErrMsg = "获取失败,Err:" + err.Error()
  866. return
  867. }
  868. //for _, item := range list {
  869. // if item.HasChapter == 1 {
  870. // item.CanEdit = true
  871. // } else {
  872. // markStatus, err := services.UpdateReportEditMark(item.Id, this.SysUser.AdminId, 2, this.SysUser.RealName, this.Lang)
  873. // if err != nil {
  874. // br.Msg = "查询标记状态失败"
  875. // br.ErrMsg = "查询标记状态失败,Err:" + err.Error()
  876. // return
  877. // }
  878. // if markStatus.Status == 0 {
  879. // item.CanEdit = true
  880. // } else {
  881. // item.Editor = markStatus.Editor
  882. // }
  883. // }
  884. //}
  885. page := paging.GetPaging(currentIndex, pageSize, total)
  886. resp := new(models.ReportListResp)
  887. resp.Paging = page
  888. resp.List = list
  889. br.Ret = 200
  890. br.Success = true
  891. br.Msg = "获取成功"
  892. br.Data = resp
  893. }
  894. // BaseDetail
  895. // @Title 获取报告基础信息详情接口
  896. // @Description 获取报告基础信息详情接口
  897. // @Param request body models.ReportDetailReq true "type json string"
  898. // @Success 200 {object} models.Report
  899. // @router /detail/base [get]
  900. func (this *ReportController) BaseDetail() {
  901. br := new(models.BaseResponse).Init()
  902. defer func() {
  903. this.Data["json"] = br
  904. this.ServeJSON()
  905. }()
  906. /*var req models.ReportDetailReq
  907. err := json.Unmarshal(this.Ctx.Input.RequestBody, &req)
  908. if err != nil {
  909. br.Msg = "参数解析异常!"
  910. br.ErrMsg = "参数解析失败,Err:" + err.Error()
  911. return
  912. }
  913. if req.ReportId <= 0 {
  914. br.Msg = "参数错误"
  915. return
  916. }*/
  917. reportId, err := this.GetInt("ReportId")
  918. if err != nil {
  919. br.Msg = "获取参数失败!"
  920. br.ErrMsg = "获取参数失败,Err:" + err.Error()
  921. return
  922. }
  923. if reportId <= 0 {
  924. br.Msg = "参数错误"
  925. return
  926. }
  927. reportInfo, err := models.GetReportById(reportId)
  928. if err != nil {
  929. if utils.IsErrNoRow(err) {
  930. br.Msg = "报告已被删除"
  931. return
  932. }
  933. br.Msg = "获取失败"
  934. br.ErrMsg = "获取失败,Err:" + err.Error()
  935. return
  936. }
  937. fmt.Println(reportInfo)
  938. // 基础信息就不获取章节信息了
  939. chapterList := make([]*models.ReportChapter, 0)
  940. reportInfo.Content = html.UnescapeString(reportInfo.Content)
  941. reportInfo.ContentSub = html.UnescapeString(reportInfo.ContentSub)
  942. grandAdminList := make([]models.ReportDetailViewAdmin, 0)
  943. permissionList := make([]models.ReportDetailViewPermission, 0)
  944. // 处理报告授权用户列表
  945. {
  946. obj := report.ReportGrant{}
  947. grantList, tmpErr := obj.GetGrantListById(reportId)
  948. if tmpErr != nil {
  949. br.Msg = "获取章节id授权用户列表失败"
  950. br.ErrMsg = "获取章节id授权用户列表失败, Err: " + tmpErr.Error()
  951. return
  952. }
  953. if len(grantList) > 0 {
  954. grandAdminIdList := make([]int, 0)
  955. for _, v := range grantList {
  956. grandAdminIdList = append(grandAdminIdList, v.AdminId)
  957. }
  958. adminList, tmpErr := system.GetAdminListByIdList(grandAdminIdList)
  959. if tmpErr != nil {
  960. br.Msg = "获取章节id授权用户列表失败"
  961. br.ErrMsg = "获取章节id授权用户列表失败, Err: " + tmpErr.Error()
  962. return
  963. }
  964. for _, v := range adminList {
  965. grandAdminList = append(grandAdminList, models.ReportDetailViewAdmin{
  966. AdminId: v.AdminId,
  967. AdminName: v.RealName,
  968. })
  969. }
  970. }
  971. }
  972. // 处理章节id关联品种id列表
  973. {
  974. minClassifyId := reportInfo.ClassifyIdThird
  975. if minClassifyId <= 0 {
  976. minClassifyId = reportInfo.ClassifyIdSecond
  977. }
  978. if minClassifyId <= 0 {
  979. minClassifyId = reportInfo.ClassifyIdFirst
  980. }
  981. if minClassifyId <= 0 {
  982. br.Msg = "分类异常"
  983. br.ErrMsg = "分类异常"
  984. return
  985. }
  986. // 获取分类关联的品种id
  987. classifyPermissionList, tmpErr := models.GetPermission(minClassifyId)
  988. if tmpErr != nil {
  989. br.Msg = "获取分类信息失败"
  990. br.ErrMsg = "获取失败,Err:" + tmpErr.Error()
  991. return
  992. }
  993. if len(classifyPermissionList) > 0 {
  994. permissionIdList := make([]int, 0)
  995. for _, v := range classifyPermissionList {
  996. permissionIdList = append(permissionIdList, v.ChartPermissionId)
  997. }
  998. adminList, tmpErr := models.GetChartPermissionByIdList(permissionIdList)
  999. if tmpErr != nil {
  1000. br.Msg = "获取章节id授权用户列表失败"
  1001. br.ErrMsg = "获取章节id授权用户列表失败, Err: " + tmpErr.Error()
  1002. return
  1003. }
  1004. for _, v := range adminList {
  1005. permissionList = append(permissionList, models.ReportDetailViewPermission{
  1006. PermissionId: v.ChartPermissionId,
  1007. PermissionName: v.PermissionName,
  1008. })
  1009. }
  1010. }
  1011. }
  1012. resp := &models.ReportDetailView{
  1013. ReportDetail: reportInfo,
  1014. ChapterList: chapterList,
  1015. GrandAdminList: grandAdminList,
  1016. PermissionList: permissionList,
  1017. }
  1018. br.Ret = 200
  1019. br.Success = true
  1020. br.Msg = "获取成功"
  1021. br.Data = resp
  1022. }
  1023. // EditLayoutImg
  1024. // @Title 版图设置接口
  1025. // @Description 版图设置接口
  1026. // @Param request body models.EditLayoutImgReq true "type json string"
  1027. // @Success 200 {object} models.EditResp
  1028. // @router /layout_img/edit [post]
  1029. func (this *ReportController) EditLayoutImg() {
  1030. br := new(models.BaseResponse).Init()
  1031. defer func() {
  1032. this.Data["json"] = br
  1033. this.ServeJSON()
  1034. }()
  1035. sysUser := this.SysUser
  1036. if sysUser == nil {
  1037. br.Msg = "请登录"
  1038. br.ErrMsg = "请登录,SysUser Is Empty"
  1039. br.Ret = 408
  1040. return
  1041. }
  1042. var req models.EditLayoutImgReq
  1043. err := json.Unmarshal(this.Ctx.Input.RequestBody, &req)
  1044. if err != nil {
  1045. br.Msg = "参数解析异常!"
  1046. br.ErrMsg = "参数解析失败,Err:" + err.Error()
  1047. return
  1048. }
  1049. //if req.Content == "" {
  1050. // br.Msg = "报告内容不能为空"
  1051. // return
  1052. //}
  1053. //更新标记key
  1054. markStatus, err := services.UpdateReportEditMark(int(req.ReportId), 0, sysUser.AdminId, 1, sysUser.RealName, this.Lang)
  1055. if err != nil {
  1056. br.Msg = err.Error()
  1057. return
  1058. }
  1059. if markStatus.Status == 1 {
  1060. br.Msg = markStatus.Msg
  1061. //br.Ret = 202 //202 服务器已接受请求,但尚未处理。
  1062. return
  1063. }
  1064. reportInfo, e := models.GetReportByReportId(int(req.ReportId))
  1065. if e != nil {
  1066. if utils.IsErrNoRow(e) {
  1067. br.Msg = "报告已被删除, 请刷新页面"
  1068. return
  1069. }
  1070. br.Msg = "操作失败"
  1071. br.ErrMsg = "获取报告失败, Err: " + e.Error()
  1072. return
  1073. }
  1074. if reportInfo.State == models.ReportStatePublished || reportInfo.State == models.ReportStatePass {
  1075. br.Msg = "该报告已发布,不允许编辑"
  1076. br.ErrMsg = "该报告已发布,不允许编辑"
  1077. br.IsSendEmail = false
  1078. return
  1079. }
  1080. // 编辑报告信息
  1081. err, errMsg := services.EditReportLayoutImg(reportInfo, req, sysUser)
  1082. if err != nil {
  1083. br.Msg = "保存失败"
  1084. if errMsg != "" {
  1085. br.Msg = errMsg
  1086. }
  1087. br.ErrMsg = "保存失败,Err:" + err.Error()
  1088. return
  1089. }
  1090. reportCode := utils.MD5(strconv.Itoa(int(req.ReportId)))
  1091. resp := new(models.EditResp)
  1092. resp.ReportId = req.ReportId
  1093. resp.ReportCode = reportCode
  1094. br.Ret = 200
  1095. br.Success = true
  1096. br.Msg = "保存成功"
  1097. br.Data = resp
  1098. }
  1099. // PublishReport
  1100. // @Title 发布报告接口
  1101. // @Description 发布报告
  1102. // @Param request body models.PublishReq true "type json string"
  1103. // @Success 200 Ret=200 发布成功
  1104. // @router /publish [post]
  1105. func (this *ReportController) PublishReport() {
  1106. br := new(models.BaseResponse).Init()
  1107. defer func() {
  1108. this.Data["json"] = br
  1109. this.ServeJSON()
  1110. }()
  1111. var req models.PublishReq
  1112. err := json.Unmarshal(this.Ctx.Input.RequestBody, &req)
  1113. if err != nil {
  1114. br.Msg = "参数解析异常!"
  1115. br.ErrMsg = "参数解析失败,Err:" + err.Error()
  1116. return
  1117. }
  1118. reportIds := req.ReportIds
  1119. if reportIds == "" {
  1120. br.Msg = "参数错误"
  1121. br.ErrMsg = "参数错误,报告id不可为空"
  1122. return
  1123. }
  1124. // 这里实际上不会批量发布了...
  1125. reportArr := strings.Split(reportIds, ",")
  1126. tips := ""
  1127. for _, v := range reportArr {
  1128. vint, err := strconv.Atoi(v)
  1129. if err != nil {
  1130. br.Msg = "参数错误"
  1131. br.ErrMsg = "参数错误,Err:" + err.Error()
  1132. return
  1133. }
  1134. // 报告的图表刷新状态校验
  1135. refreshResult := data.CheckBatchChartRefreshResult("report", vint, 0)
  1136. if !refreshResult {
  1137. br.Msg = "图表刷新未完成,请稍后操作"
  1138. br.ErrMsg = "图表刷新未完成,请稍后操作"
  1139. br.IsSendEmail = false
  1140. return
  1141. }
  1142. // 报告发布
  1143. tmpTips, err, errMsg := services.PublishReport(vint, req.ReportUrl, this.SysUser)
  1144. if err != nil {
  1145. br.Msg = errMsg
  1146. br.ErrMsg = "报告发布失败,Err:" + err.Error()
  1147. return
  1148. }
  1149. tips = tmpTips
  1150. }
  1151. // 发布晨周报部分章节未发布的提示
  1152. if tips != "" {
  1153. br.Data = tips
  1154. }
  1155. br.Ret = 200
  1156. br.Success = true
  1157. br.Msg = "发布成功"
  1158. }
  1159. // PublishCancelReport
  1160. // @Title 取消发布报告接口
  1161. // @Description 取消发布报告
  1162. // @Param request body models.PublishCancelReq true "type json string"
  1163. // @Success 200 Ret=200 取消发布成功
  1164. // @router /publish/cancle [post]
  1165. func (this *ReportController) PublishCancelReport() {
  1166. br := new(models.BaseResponse).Init()
  1167. defer func() {
  1168. this.Data["json"] = br
  1169. this.ServeJSON()
  1170. }()
  1171. var req models.PublishCancelReq
  1172. err := json.Unmarshal(this.Ctx.Input.RequestBody, &req)
  1173. if err != nil {
  1174. br.Msg = "参数解析异常!"
  1175. br.ErrMsg = "参数解析失败,Err:" + err.Error()
  1176. return
  1177. }
  1178. if req.ReportIds <= 0 {
  1179. br.Msg = "参数错误"
  1180. br.ErrMsg = "参数错误,报告id不可为空"
  1181. return
  1182. }
  1183. publishTimeNullFlag := true
  1184. reportInfo, err := models.GetReportById(req.ReportIds)
  1185. if err != nil {
  1186. br.Msg = "获取报告信息失败"
  1187. br.ErrMsg = "获取报告信息失败,Err:" + err.Error()
  1188. return
  1189. }
  1190. if reportInfo.MsgIsSend == 1 {
  1191. publishTimeNullFlag = false
  1192. }
  1193. // 根据审批开关及审批流判断当前报告状态
  1194. state, e := services.CheckReportCurrState(report_approve.FlowReportTypeChinese, reportInfo.ClassifyIdFirst, reportInfo.ClassifyIdSecond, reportInfo.ClassifyIdThird, models.ReportOperateCancelPublish)
  1195. if e != nil {
  1196. br.Msg = "操作失败"
  1197. br.ErrMsg = "校验报告当前状态失败, Err: " + e.Error()
  1198. return
  1199. }
  1200. err = models.PublishCancelReport(req.ReportIds, state, publishTimeNullFlag, this.SysUser.AdminId, this.SysUser.RealName)
  1201. if err != nil {
  1202. br.Msg = "取消发布失败"
  1203. br.ErrMsg = "取消发布失败,Err:" + err.Error()
  1204. return
  1205. }
  1206. // 更新ES禁用
  1207. {
  1208. go services.UpdateReportEs(req.ReportIds, 1)
  1209. }
  1210. //// 获取审批流设置
  1211. //confKey := "approval_flow"
  1212. //confTmp, e := company.GetConfigDetailByCode(confKey)
  1213. //if e != nil {
  1214. // br.Msg = "获取审批流配置失败"
  1215. // br.ErrMsg = "获取审批流配置失败, Err: " + e.Error()
  1216. // return
  1217. //}
  1218. //if confTmp.ConfigValue == "1" || confTmp.ConfigValue == "2" || confTmp.ConfigValue == "3" {
  1219. // br.Msg = "撤销成功"
  1220. //} else {
  1221. // br.Msg = "取消发布成功"
  1222. //}
  1223. recordItem := &models.ReportStateRecord{
  1224. ReportId: req.ReportIds,
  1225. ReportType: 1,
  1226. State: state,
  1227. AdminId: this.SysUser.AdminId,
  1228. AdminName: this.SysUser.AdminName,
  1229. CreateTime: time.Now(),
  1230. }
  1231. go func() {
  1232. _, _ = models.AddReportStateRecord(recordItem)
  1233. }()
  1234. br.Ret = 200
  1235. br.Success = true
  1236. }
  1237. // PrePublishReport
  1238. // @Title 设置定时发布接口
  1239. // @Description 设置定时发布接口
  1240. // @Param request body models.PrePublishReq true "type json string"
  1241. // @Success 200 Ret=200 发布成功
  1242. // @router /pre_publish [post]
  1243. func (this *ReportController) PrePublishReport() {
  1244. br := new(models.BaseResponse).Init()
  1245. defer func() {
  1246. this.Data["json"] = br
  1247. this.ServeJSON()
  1248. }()
  1249. var req models.PrePublishReq
  1250. err := json.Unmarshal(this.Ctx.Input.RequestBody, &req)
  1251. if err != nil {
  1252. br.Msg = "参数解析异常!"
  1253. br.ErrMsg = "参数解析失败,Err:" + err.Error()
  1254. return
  1255. }
  1256. reportId := req.ReportId
  1257. if reportId == 0 {
  1258. br.Msg = "参数错误"
  1259. br.ErrMsg = "参数错误,报告id不可为空"
  1260. return
  1261. }
  1262. if req.PrePublishTime == "" {
  1263. br.Msg = "发布时间不能为空"
  1264. return
  1265. }
  1266. if req.PreMsgSend != 0 && req.PreMsgSend != 1 {
  1267. br.Msg = "参数错误"
  1268. br.ErrMsg = "是否发送模版消息标识错误"
  1269. return
  1270. }
  1271. prePublishTime, err := time.ParseInLocation(utils.FormatDateTime, req.PrePublishTime, time.Local)
  1272. if err != nil {
  1273. br.Msg = "发布时间格式错误"
  1274. br.ErrMsg = "发布时间格式错误,Err:" + err.Error()
  1275. return
  1276. }
  1277. if prePublishTime.Before(time.Now()) {
  1278. br.Msg = "发布时间不允许选择过去时间"
  1279. return
  1280. }
  1281. if prePublishTime.Before(time.Now().Add(2 * time.Minute)) {
  1282. br.Msg = "发布时间距离当前时间太近了"
  1283. return
  1284. }
  1285. reportDetail, err := models.GetReportById(reportId)
  1286. if err != nil {
  1287. br.Msg = "获取报告信息失败"
  1288. br.ErrMsg = "获取报告信息失败,Err:" + err.Error()
  1289. return
  1290. }
  1291. if reportDetail == nil {
  1292. br.Msg = "报告不存在"
  1293. return
  1294. }
  1295. // 如果是章节类型的报告,那么需要确认所有章节已发布
  1296. if reportDetail.HasChapter == 1 {
  1297. chapterList, err := models.GetChapterListByReportId(reportId)
  1298. if err != nil {
  1299. return
  1300. }
  1301. for _, chapter := range chapterList {
  1302. if chapter.PublishState == 1 {
  1303. br.Msg = "还存在未发布的章节"
  1304. br.ErrMsg = "还存在未发布的章节"
  1305. return
  1306. }
  1307. }
  1308. } else {
  1309. if reportDetail.Content == "" {
  1310. br.Msg = "报告内容为空,不可设置定时发布"
  1311. br.ErrMsg = "报告内容为空,不可设置定时发布,report_id:" + strconv.Itoa(reportDetail.Id)
  1312. return
  1313. }
  1314. }
  1315. if reportDetail.State == 2 {
  1316. br.Msg = "报告已发布,不可设置定时发布"
  1317. return
  1318. }
  1319. // 校验是否开启了审批流
  1320. opening, e := services.CheckReportOpenApprove(report_approve.FlowReportTypeChinese, reportDetail.ClassifyIdFirst, reportDetail.ClassifyIdSecond, reportDetail.ClassifyIdThird)
  1321. if e != nil {
  1322. br.Msg = "操作失败"
  1323. br.ErrMsg = "校验报告是否开启审批流失败, Err: " + e.Error()
  1324. return
  1325. }
  1326. if opening {
  1327. br.Msg = "报告已开启审批流, 不可设置定时发布"
  1328. return
  1329. }
  1330. var tmpErr error
  1331. if tmpErr = models.SetPrePublishReportById(reportDetail.Id, req.PrePublishTime, req.PreMsgSend); tmpErr != nil {
  1332. br.Msg = "设置定时发布失败"
  1333. br.ErrMsg = "设置定时发布失败, Err:" + tmpErr.Error() + ", report_id:" + strconv.Itoa(reportDetail.Id)
  1334. return
  1335. }
  1336. // 生成报告pdf和长图
  1337. {
  1338. reportPdfUrl := services.GetGeneralPdfUrl(reportDetail.ReportCode, reportDetail.ClassifyNameFirst, reportDetail.ReportLayout)
  1339. go services.Report2pdfAndJpeg(reportPdfUrl, reportDetail.Id, 1)
  1340. }
  1341. br.Ret = 200
  1342. br.Success = true
  1343. br.Msg = "定时发布成功"
  1344. }
  1345. // SubmitApprove
  1346. // @Title 提交审批
  1347. // @Description 提交审批接口
  1348. // @Param request body models.ReportSubmitApproveReq true "type json string"
  1349. // @Success 200 Ret=200 操作成功
  1350. // @router /approve/submit [post]
  1351. func (this *ReportController) SubmitApprove() {
  1352. br := new(models.BaseResponse).Init()
  1353. defer func() {
  1354. if br.ErrMsg == "" {
  1355. br.IsSendEmail = false
  1356. }
  1357. this.Data["json"] = br
  1358. this.ServeJSON()
  1359. }()
  1360. sysUser := this.SysUser
  1361. if sysUser == nil {
  1362. br.Msg = "请登录"
  1363. br.ErrMsg = "请登录,SysUser Is Empty"
  1364. return
  1365. }
  1366. var req models.ReportSubmitApproveReq
  1367. if e := json.Unmarshal(this.Ctx.Input.RequestBody, &req); e != nil {
  1368. br.Msg = "参数有误"
  1369. br.ErrMsg = "参数解析失败, Err: " + e.Error()
  1370. return
  1371. }
  1372. reportId := req.ReportId
  1373. if reportId <= 0 {
  1374. br.Msg = "参数有误"
  1375. br.ErrMsg = fmt.Sprintf("参数有误, ReportId: %d", req.ReportId)
  1376. return
  1377. }
  1378. reportOb := new(models.Report)
  1379. reportItem, e := reportOb.GetItemById(reportId)
  1380. if e != nil {
  1381. if utils.IsErrNoRow(e) {
  1382. br.Msg = "报告已被删除, 请刷新页面"
  1383. return
  1384. }
  1385. br.Msg = "操作失败"
  1386. br.ErrMsg = "获取报告失败, Err: " + e.Error()
  1387. return
  1388. }
  1389. // 如果是章节类型的报告,那么需要确认所有章节已发布
  1390. if reportItem.HasChapter == 1 {
  1391. chapterList, err := models.GetChapterListByReportId(reportId)
  1392. if err != nil {
  1393. return
  1394. }
  1395. for _, chapter := range chapterList {
  1396. if chapter.PublishState == 1 {
  1397. br.Msg = "还存在未发布的章节"
  1398. br.ErrMsg = "还存在未发布的章节"
  1399. return
  1400. }
  1401. }
  1402. } else {
  1403. if reportItem.Content == "" {
  1404. br.Msg = "报告内容为空,不可提交"
  1405. br.ErrMsg = "报告内容为空,不可提交,report_id:" + strconv.Itoa(reportItem.Id)
  1406. return
  1407. }
  1408. }
  1409. // 校验当前审批配置, 返回下一个状态
  1410. state, e := services.CheckReportCurrState(report_approve.FlowReportTypeChinese, reportItem.ClassifyIdFirst, reportItem.ClassifyIdSecond, reportItem.ClassifyIdThird, models.ReportOperateSubmitApprove)
  1411. if e != nil {
  1412. br.Msg = "操作失败"
  1413. br.ErrMsg = "校验报告状态失败, Err: " + e.Error()
  1414. return
  1415. }
  1416. // 下一个状态不为待审批时, 仅更新状态
  1417. if state != models.ReportStateWaitApprove {
  1418. reportItem.State = state
  1419. e = reportItem.UpdateReport([]string{"State"})
  1420. if e != nil {
  1421. br.Msg = "操作失败"
  1422. br.ErrMsg = "更新报告状态失败, Err: " + e.Error()
  1423. return
  1424. }
  1425. br.Ret = 200
  1426. br.Success = true
  1427. br.Msg = "操作成功"
  1428. return
  1429. }
  1430. // 提交审批
  1431. approveId, e := services.SubmitReportApprove(report_approve.FlowReportTypeChinese, reportItem.Id, reportItem.Title, reportItem.ClassifyIdFirst, reportItem.ClassifyIdSecond, reportItem.ClassifyIdThird, sysUser.AdminId, sysUser.RealName)
  1432. if e != nil {
  1433. br.Msg = "操作失败"
  1434. br.ErrMsg = "提交审批失败, Err: " + e.Error()
  1435. return
  1436. }
  1437. reportItem.ApproveId = approveId
  1438. reportItem.State = models.ReportStateWaitApprove
  1439. reportItem.ModifyTime = time.Now().Local()
  1440. e = reportItem.UpdateReport([]string{"ApproveId", "State", "ModifyTime"})
  1441. if e != nil {
  1442. br.Msg = "操作失败"
  1443. br.ErrMsg = "更新报告状态失败, Err: " + e.Error()
  1444. return
  1445. }
  1446. // 报告权限处理
  1447. {
  1448. go services.HandleReportPermission(reportItem)
  1449. }
  1450. br.Ret = 200
  1451. br.Success = true
  1452. br.Msg = "操作成功"
  1453. }
  1454. // CancelApprove
  1455. // @Title 撤销审批
  1456. // @Description 撤销审批
  1457. // @Param request body models.ReportCancelApproveReq true "type json string"
  1458. // @Success 200 Ret=200 操作成功
  1459. // @router /approve/cancel [post]
  1460. func (this *ReportController) CancelApprove() {
  1461. br := new(models.BaseResponse).Init()
  1462. defer func() {
  1463. if br.ErrMsg == "" {
  1464. br.IsSendEmail = false
  1465. }
  1466. this.Data["json"] = br
  1467. this.ServeJSON()
  1468. }()
  1469. sysUser := this.SysUser
  1470. if sysUser == nil {
  1471. br.Msg = "请登录"
  1472. br.ErrMsg = "请登录,SysUser Is Empty"
  1473. return
  1474. }
  1475. var req models.ReportCancelApproveReq
  1476. if e := json.Unmarshal(this.Ctx.Input.RequestBody, &req); e != nil {
  1477. br.Msg = "参数有误"
  1478. br.ErrMsg = "参数解析失败, Err: " + e.Error()
  1479. return
  1480. }
  1481. reportId := req.ReportId
  1482. if reportId <= 0 {
  1483. br.Msg = "参数有误"
  1484. br.ErrMsg = fmt.Sprintf("参数有误, ReportId: %d", req.ReportId)
  1485. return
  1486. }
  1487. reportOb := new(models.Report)
  1488. reportItem, e := reportOb.GetItemById(reportId)
  1489. if e != nil {
  1490. if utils.IsErrNoRow(e) {
  1491. br.Msg = "报告已被删除, 请刷新页面"
  1492. return
  1493. }
  1494. br.Msg = "操作失败"
  1495. br.ErrMsg = "获取报告失败, Err: " + e.Error()
  1496. return
  1497. }
  1498. // 校验当前审批配置, 返回下一个状态
  1499. state, e := services.CheckReportCurrState(report_approve.FlowReportTypeChinese, reportItem.ClassifyIdFirst, reportItem.ClassifyIdSecond, reportItem.ClassifyIdThird, models.ReportOperateCancelApprove)
  1500. if e != nil {
  1501. br.Msg = "操作失败"
  1502. br.ErrMsg = "校验报告状态失败, Err: " + e.Error()
  1503. return
  1504. }
  1505. // 下一个状态不为待提交时, 仅更新状态
  1506. if state != models.ReportStateWaitSubmit {
  1507. reportItem.State = state
  1508. e = reportItem.UpdateReport([]string{"State"})
  1509. if e != nil {
  1510. br.Msg = "操作失败"
  1511. br.ErrMsg = "更新报告状态失败, Err: " + e.Error()
  1512. return
  1513. }
  1514. br.Ret = 200
  1515. br.Success = true
  1516. br.Msg = "操作成功"
  1517. return
  1518. }
  1519. //if reportItem.ApproveId <= 0 {
  1520. // br.Msg = "报告审批不存在"
  1521. // br.ErrMsg = fmt.Sprintf("报告审批不存在, ApproveId: %d", reportItem.ApproveId)
  1522. // return
  1523. //}
  1524. // 撤销审批
  1525. e = services.CancelReportApprove(report_approve.FlowReportTypeChinese, reportItem.Id, reportItem.ApproveId, sysUser.AdminId, sysUser.RealName)
  1526. if e != nil {
  1527. br.Msg = "操作失败"
  1528. br.ErrMsg = "撤销审批失败, Err: " + e.Error()
  1529. return
  1530. }
  1531. //reportItem.ApproveId = 0
  1532. //reportItem.State = models.ReportStateWaitSubmit
  1533. //reportItem.ModifyTime = time.Now().Local()
  1534. //e = reportItem.UpdateReport([]string{"ApproveId", "State", "ModifyTime"})
  1535. //if e != nil {
  1536. // br.Msg = "操作失败"
  1537. // br.ErrMsg = "更新报告状态失败, Err: " + e.Error()
  1538. // return
  1539. //}
  1540. br.Ret = 200
  1541. br.Success = true
  1542. br.Msg = "操作成功"
  1543. }
  1544. func initPdf() {
  1545. inFile := "anNNgk3Bbi4LRULwcJgNOPrREYh5.pdf"
  1546. f2, err := services.GeneralWaterMarkPdf(inFile, "颜鹏 - 18170239278")
  1547. //f2, err := services.GeneralWaterMarkPdf(inFile, "上周美国馏分油库存累库95万桶,馏分油表需环比下降(-25.6万桶/日)。本期馏分油产量继续抬升,在供增需减的环比变动下库存持续累库。馏分油供应的增加我们认为可能和进口的油种有关,今年以来美国进口的中重质原油占比不断走高,尤其是5")
  1548. if err != nil {
  1549. fmt.Println("生成失败,ERR:", err)
  1550. return
  1551. }
  1552. // 创建一个新的文件
  1553. newPdf, err := os.Create("new0555.pdf")
  1554. if err != nil {
  1555. fmt.Println("创建临时文件失败,Err:", err)
  1556. return
  1557. }
  1558. defer func() {
  1559. _ = newPdf.Close()
  1560. }()
  1561. _, _ = io.Copy(newPdf, f2)
  1562. }