report_open.go 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846
  1. package controllers
  2. import (
  3. "encoding/json"
  4. "eta_gn/eta_report/models"
  5. "eta_gn/eta_report/services"
  6. "eta_gn/eta_report/services/knowledge"
  7. "eta_gn/eta_report/utils"
  8. "fmt"
  9. "strconv"
  10. "strings"
  11. "time"
  12. )
  13. // ReportOpenController 报告开放接口
  14. type ReportOpenController struct {
  15. BaseOpenController
  16. }
  17. // ClassifyTree
  18. // @Title 报告分类
  19. // @Description 报告分类接口
  20. // @Param ClassifyType query int false "分类类型:0-全部(不传默认为0);1-研报;2-PPT"
  21. // @Param OutId query string false "用户工号Id"
  22. // @Success 200 {object} models.ClassifyTreeItem
  23. // @router /report/classify [get]
  24. func (this *ReportOpenController) ClassifyTree() {
  25. br := new(models.BaseResponse).Init()
  26. defer func() {
  27. if br.ErrMsg == "" {
  28. br.IsSendEmail = false
  29. }
  30. this.Data["json"] = br
  31. this.ServeJSON()
  32. }()
  33. classifyType, _ := this.GetInt("ClassifyType", 0)
  34. outId := this.GetString("OutId")
  35. if classifyType < 0 || classifyType > 2 {
  36. classifyType = 0
  37. }
  38. // 获取分类列表, 格式化分类树
  39. classifyOb := new(models.Classify)
  40. cond := ``
  41. pars := make([]interface{}, 0)
  42. if classifyType > 0 {
  43. cond += ` AND classify_type = ?`
  44. pars = append(pars, classifyType)
  45. }
  46. classifyList, e := classifyOb.GetItemsByCondition(cond, pars, []string{}, "parent_id ASC, sort ASC, create_time ASC")
  47. if e != nil {
  48. br.Msg = "获取失败"
  49. br.ErrMsg = fmt.Sprintf("获取分类列表失败,%v", e)
  50. return
  51. }
  52. if outId == "" {
  53. br.Msg = "工号不能为空"
  54. return
  55. }
  56. list := make([]*models.Classify, 0)
  57. classifyObj := new(models.ClassifyVisible)
  58. visibleUsers, err := classifyObj.GetClassifyVisibleAll()
  59. if err != nil {
  60. br.Msg = "获取失败"
  61. br.ErrMsg = fmt.Sprintf("获取可见用户失败,%v", err)
  62. return
  63. }
  64. adminObj := new(models.Admin)
  65. admin, e := adminObj.GetAdminByOutId(outId)
  66. if e != nil {
  67. if utils.IsErrNoRow(e) {
  68. br.Msg = "工号不存在"
  69. return
  70. }
  71. br.Msg = "获取失败"
  72. br.ErrMsg = fmt.Sprintf("获取管理员信息失败,%v", e)
  73. return
  74. }
  75. visibleUsersMap := make(map[int][]int)
  76. for _, v := range visibleUsers {
  77. visibleUsersMap[v.ClassifyId] = append(visibleUsersMap[v.ClassifyId], v.AdminId)
  78. }
  79. for _, v := range classifyList {
  80. if visibleUser, ok := visibleUsersMap[v.Id]; ok {
  81. if !utils.InArrayByInt(visibleUser, admin.AdminId) {
  82. continue
  83. }
  84. }
  85. list = append(list, v)
  86. }
  87. resp := services.GetReportClassifyTreeRecursive(list, 0)
  88. resp = services.RecursiveFilterNoChildTreeClassify(resp)
  89. br.Data = resp
  90. br.Ret = 200
  91. br.Success = true
  92. br.Msg = "获取成功"
  93. }
  94. // ReportCreate
  95. // @Title 生成报告
  96. // @Description 生成报告接口
  97. // @Param request body models.ReportCreateReq true "type json string"
  98. // @Success 200 string "操作成功"
  99. // @router /report/create [post]
  100. func (this *ReportOpenController) ReportCreate() {
  101. br := new(models.BaseResponse).Init()
  102. defer func() {
  103. if br.ErrMsg == "" {
  104. br.IsSendEmail = false
  105. }
  106. this.Data["json"] = br
  107. this.ServeJSON()
  108. }()
  109. var req models.ReportCreateReq
  110. if e := json.Unmarshal(this.Ctx.Input.RequestBody, &req); e != nil {
  111. br.Msg = "参数解析异常"
  112. br.ErrMsg = fmt.Sprintf("参数解析失败,%v", e)
  113. return
  114. }
  115. if req.ReportId <= 0 {
  116. br.Msg = "报告ID有误"
  117. br.ErrMsg = fmt.Sprintf("智力共享报告ID有误, %d", req.ReportId)
  118. return
  119. }
  120. if req.ClassifyId <= 0 {
  121. br.Msg = "请选择分类"
  122. return
  123. }
  124. req.Title = strings.TrimSpace(req.Title)
  125. if req.Title == "" {
  126. br.Msg = "请输入报告标题"
  127. return
  128. }
  129. req.EndTime = strings.TrimSpace(req.EndTime)
  130. if req.EndTime == "" {
  131. br.Msg = "请输入课题结束时间"
  132. return
  133. }
  134. topicTime, e := time.ParseInLocation(utils.FormatDateTime, req.EndTime, time.Local)
  135. if e != nil {
  136. br.Msg = "课题结束时间格式有误"
  137. return
  138. }
  139. req.Creator = strings.TrimSpace(req.Creator)
  140. if req.Creator == "" {
  141. br.Msg = "请输入创建人工号"
  142. return
  143. }
  144. // 查询分类,根据分类创建对应类型的研报
  145. classifyOb := new(models.Classify)
  146. classify, e := classifyOb.GetItemById(req.ClassifyId)
  147. if e != nil {
  148. if utils.IsErrNoRow(e) {
  149. br.Msg = "分类不存在"
  150. return
  151. }
  152. br.Msg = "操作失败"
  153. br.ErrMsg = fmt.Sprintf("获取分类失败, %v", e)
  154. return
  155. }
  156. // 校验报告ID是否已存在, 已存在返回已有的报告ID
  157. reportExist, pptExist := services.GetReportByOutReportId(req.ReportId)
  158. if reportExist != nil && reportExist.Id > 0 {
  159. resp := new(models.ReportCreateResp)
  160. resp.ReportId = reportExist.Id
  161. br.Data = resp
  162. br.Ret = 200
  163. br.Success = true
  164. br.Msg = "操作成功"
  165. return
  166. }
  167. if pptExist != nil && pptExist.PptId > 0 {
  168. resp := new(models.ReportCreateResp)
  169. resp.ReportId = pptExist.PptId
  170. br.Data = resp
  171. br.Ret = 200
  172. br.Success = true
  173. br.Msg = "操作成功"
  174. return
  175. }
  176. // 研报类型
  177. var reportId int
  178. if classify.ClassifyType == utils.ReportTypeDefault {
  179. reportId, e = services.CreateReport(req.ReportId, req.ClassifyId, req.Title, topicTime, req.Creator, req.Authors)
  180. if e != nil {
  181. br.Msg = "操作失败"
  182. br.ErrMsg = fmt.Sprintf("新增报告失败, %v", e)
  183. return
  184. }
  185. }
  186. // PPT类型
  187. if classify.ClassifyType == utils.ReportTypePPT {
  188. reportId, e = services.CreatePptReport(req.ReportId, req.ClassifyId, req.Title, topicTime, req.Creator, req.Authors)
  189. if e != nil {
  190. br.Msg = "操作失败"
  191. br.ErrMsg = fmt.Sprintf("新增PPT报告失败, %v", e)
  192. return
  193. }
  194. }
  195. resp := new(models.ReportCreateResp)
  196. resp.ReportId = reportId
  197. //resp.ReportCode = reportCode
  198. br.Data = resp
  199. br.Ret = 200
  200. br.Success = true
  201. br.Msg = "操作成功"
  202. }
  203. // ReportModify
  204. // @Title 编辑报告
  205. // @Description 编辑报告接口
  206. // @Param request body models.ReportModifyReq true "type json string"
  207. // @Success 200 string "操作成功"
  208. // @router /report/modify [post]
  209. func (this *ReportOpenController) ReportModify() {
  210. br := new(models.BaseResponse).Init()
  211. defer func() {
  212. if br.ErrMsg == "" {
  213. br.IsSendEmail = false
  214. }
  215. this.Data["json"] = br
  216. this.ServeJSON()
  217. }()
  218. var req models.ReportModifyReq
  219. if e := json.Unmarshal(this.Ctx.Input.RequestBody, &req); e != nil {
  220. br.Msg = "参数解析异常"
  221. br.ErrMsg = fmt.Sprintf("参数解析失败,%v", e)
  222. return
  223. }
  224. if req.ReportId <= 0 {
  225. br.Msg = "报告ID有误"
  226. br.ErrMsg = fmt.Sprintf("智力共享报告ID有误, %d", req.ReportId)
  227. return
  228. }
  229. req.Title = strings.TrimSpace(req.Title)
  230. if req.Title == "" {
  231. br.Msg = "请输入报告标题"
  232. return
  233. }
  234. req.EndTime = strings.TrimSpace(req.EndTime)
  235. if req.EndTime == "" {
  236. br.Msg = "请输入课题结束时间"
  237. return
  238. }
  239. topicTime, e := time.ParseInLocation(utils.FormatDateTime, req.EndTime, time.Local)
  240. if e != nil {
  241. br.Msg = "课题结束时间格式有误"
  242. return
  243. }
  244. reportExist, pptExist := services.GetReportByOutReportId(req.ReportId)
  245. if reportExist == nil && pptExist == nil {
  246. br.Msg = "报告不存在"
  247. br.ErrMsg = fmt.Sprintf("报告不存在, OutReportId: %d", req.ReportId)
  248. return
  249. }
  250. // 研报类型
  251. if reportExist != nil && reportExist.Id > 0 {
  252. if reportExist.State == models.ReportStatePass {
  253. br.Msg = "报告已发布,不可编辑"
  254. return
  255. }
  256. if e = services.EditReport(reportExist, req.Title, topicTime, req.Authors); e != nil {
  257. br.Msg = "操作失败"
  258. br.ErrMsg = fmt.Sprintf("编辑报告失败, %v", e)
  259. return
  260. }
  261. }
  262. // PPT类型
  263. if pptExist != nil && pptExist.PptId > 0 {
  264. if pptExist.State == models.ReportStatePass {
  265. br.Msg = "报告已发布,不可编辑"
  266. return
  267. }
  268. if e = services.EditPptReport(pptExist, req.Title, topicTime, req.Authors); e != nil {
  269. br.Msg = "操作失败"
  270. br.ErrMsg = fmt.Sprintf("编辑PPT报告失败, %v", e)
  271. return
  272. }
  273. }
  274. br.Ret = 200
  275. br.Success = true
  276. br.Msg = "操作成功"
  277. }
  278. // ReportApprove
  279. // @Title 审批报告
  280. // @Description 审批报告接口
  281. // @Param request body models.ReportApproveReq true "type json string"
  282. // @Success 200 string "操作成功"
  283. // @router /report/approve [post]
  284. func (this *ReportOpenController) ReportApprove() {
  285. br := new(models.BaseResponse).Init()
  286. defer func() {
  287. if br.ErrMsg == "" {
  288. br.IsSendEmail = false
  289. }
  290. this.Data["json"] = br
  291. this.ServeJSON()
  292. }()
  293. var req models.ReportApproveReq
  294. if e := json.Unmarshal(this.Ctx.Input.RequestBody, &req); e != nil {
  295. br.Msg = "参数解析异常"
  296. br.ErrMsg = fmt.Sprintf("参数解析失败,%v", e)
  297. return
  298. }
  299. if len(req.ReportIds) == 0 {
  300. br.Msg = "报告ID有误"
  301. br.ErrMsg = fmt.Sprintf("智力共享报告ID有误, %v", req.ReportIds)
  302. return
  303. }
  304. if req.ApproveType != 1 && req.ApproveType != 2 && req.ApproveType != 3 && req.ApproveType != 4 {
  305. br.Msg = "审批类型异常"
  306. br.ErrMsg = fmt.Sprintf("审批类型异常, %d", req.ApproveType)
  307. return
  308. }
  309. // 先做校验后执行, 报告很可能是研报和PPT类型混着一起审批的
  310. updateReports := make([]*models.Report, 0)
  311. updatePpts := make([]*models.PptV2, 0)
  312. for _, v := range req.ReportIds {
  313. reportExist, pptExist := services.GetReportByOutReportId(v)
  314. if reportExist == nil && pptExist == nil {
  315. utils.FileLog.Info(fmt.Sprintf("ReportApprove-报告ID: %d不存在", v))
  316. continue
  317. }
  318. // 通过和驳回需要审批中/已驳回状态,撤回、提审暂不做限制(若报告未撰写完毕,提审会把状态从待提交直接变为待审批,那么就是撰写终止附件不会再传回去了,这种情况是允许的)
  319. if reportExist != nil && reportExist.Id > 0 {
  320. if (req.ApproveType == 1 || req.ApproveType == 2) && reportExist.State != models.ReportStateWaitApprove && reportExist.State != models.ReportStateRefused {
  321. utils.FileLog.Info(fmt.Sprintf("ReportApprove-报告当前状态不允许审批, OutId: %d, State: %d", v, reportExist.State))
  322. continue
  323. }
  324. updateReports = append(updateReports, reportExist)
  325. }
  326. if pptExist != nil && pptExist.PptId > 0 {
  327. if (req.ApproveType == 1 || req.ApproveType == 2) && pptExist.State != models.ReportStateWaitApprove && pptExist.State != models.ReportStateRefused {
  328. utils.FileLog.Info(fmt.Sprintf("ReportApprove-PPT当前状态不允许审批, OutId: %d, State: %d", v, pptExist.State))
  329. continue
  330. }
  331. updatePpts = append(updatePpts, pptExist)
  332. }
  333. }
  334. // 审批人
  335. approveUser := new(models.Admin)
  336. {
  337. ob := new(models.Admin)
  338. cond := ` AND out_id = ?`
  339. pars := make([]interface{}, 0)
  340. pars = append(pars, req.ApproveUser)
  341. admin, e := ob.GetItemByCondition(cond, pars, "")
  342. if e != nil && !utils.IsErrNoRow(e) {
  343. br.Msg = "操作失败"
  344. br.ErrMsg = fmt.Sprintf("获取审批人信息失败, %v", e)
  345. return
  346. }
  347. if admin != nil && admin.AdminId > 0 {
  348. approveUser = admin
  349. }
  350. }
  351. if approveUser.AdminId == 0 {
  352. utils.FileLog.Info(fmt.Sprintf("ReportApprove-无审批人信息, OutId: %s", req.ApproveUser))
  353. }
  354. reportMessages := make([]*models.ReportMessage, 0)
  355. afterApproveMap := map[int]int{
  356. 1: models.ReportStatePass,
  357. 2: models.ReportStateRefused,
  358. 3: models.ReportStateWaitSubmit,
  359. 4: models.ReportStateWaitApprove,
  360. }
  361. if len(updateReports) > 0 {
  362. for _, v := range updateReports {
  363. updateCols := []string{"State", "ModifyTime"}
  364. v.State = afterApproveMap[req.ApproveType]
  365. v.ModifyTime = time.Now()
  366. if v.State == models.ReportStatePass || v.State == models.ReportStateRefused {
  367. v.ApproveTime = time.Now()
  368. updateCols = append(updateCols, "ApproveTime")
  369. }
  370. if e := v.Update(updateCols); e != nil {
  371. br.Msg = "操作失败"
  372. br.ErrMsg = fmt.Sprintf("更新报告状态失败, %v", e)
  373. return
  374. }
  375. if req.ApproveType == 1 {
  376. // 审批通过,将PPT或研报加入报告库中
  377. go func() {
  378. msg, err := knowledge.ReportKnowledgeResourceAdd(v.Title, []string{v.DetailPdfUrl}, v.AdminId, v.AdminRealName, "市场研究报告/内部报告/日常报告", "市场信息研究所", time.Now())
  379. if err != nil {
  380. utils.FileLog.Error(fmt.Sprintf("ReportKnowledgeResourceAdd-报告知识资源库添加失败,reportId:%d,msg:%s,err: %v", v.Id, msg, err))
  381. return
  382. }
  383. }()
  384. go func() {
  385. // 审批通过,将摘要加入知识资源库观点中
  386. var classifyId int
  387. if v.ClassifyIdThird > 0 {
  388. classifyId = v.ClassifyIdThird
  389. } else if v.ClassifyIdSecond > 0 {
  390. classifyId = v.ClassifyIdSecond
  391. } else {
  392. classifyId = v.ClassifyIdFirst
  393. }
  394. v.Abstract = strings.TrimSpace(v.Abstract)
  395. if v.Abstract == "" {
  396. return
  397. }
  398. classifyId, err := knowledge.ReportKnowledgeResourceClassifyCheckAndSave(classifyId)
  399. if err != nil {
  400. utils.FileLog.Error(fmt.Sprintf("ReportKnowledgeResourceClassifyCheckAndSave-报告分类保存失败,reportId:%d,classifyId:%d,err: %v", v.Id, classifyId, err))
  401. return
  402. }
  403. msg, err := knowledge.ViewPointSave(0, v.AdminId, v.AdminRealName, v.Title, v.Abstract, "市场信息研究所", "", classifyId, &v.ApproveTime)
  404. if err != nil {
  405. utils.FileLog.Error(fmt.Sprintf("ViewPointSave-报告摘要知识资源库添加失败,reportId:%d,msg:%s,err: %v", v.Id, msg, err))
  406. return
  407. }
  408. }()
  409. }
  410. if req.ApproveType == 3 || req.ApproveType == 4 {
  411. // 撤回/提审暂不发消息
  412. continue
  413. }
  414. // 创作人和协作人
  415. userIds := make([]int, 0)
  416. userIds = append(userIds, v.AdminId)
  417. {
  418. grantOb := new(models.ReportGrant)
  419. grantCond := ` AND report_id = ?`
  420. grantPars := make([]interface{}, 0)
  421. grantPars = append(grantPars, v.Id)
  422. grants, e := grantOb.GetItemsByCondition(grantCond, grantPars, []string{"admin_id"}, "")
  423. if e != nil {
  424. br.Msg = "操作失败"
  425. br.ErrMsg = fmt.Sprintf("获取报告协作人失败, %v", e)
  426. return
  427. }
  428. for _, v := range grants {
  429. if utils.InArrayByInt(userIds, v.AdminId) {
  430. continue
  431. }
  432. userIds = append(userIds, v.AdminId)
  433. }
  434. }
  435. // 发送系统消息给创建人和协作人
  436. var messageType int
  437. var tips string
  438. var extraContent string
  439. if v.State == models.ReportStatePass {
  440. messageType = models.ReportMessageTypeApprovePass
  441. tips = "已通过审批!"
  442. }
  443. if v.State == models.ReportStateRefused {
  444. messageType = models.ReportMessageTypeApproveRefuse
  445. tips = "已被驳回,点击查看详情!"
  446. // 审批详情信息
  447. var extra models.ReportMessageApproveItem
  448. extra.Title = v.Title
  449. extra.ApproveType = 2
  450. extra.ApproveUserId = approveUser.AdminId
  451. extra.ApproveUserName = approveUser.RealName
  452. extra.ApproveRemark = req.Suggestion
  453. b, e := json.Marshal(extra)
  454. if e != nil {
  455. br.Msg = "操作失败"
  456. br.ErrMsg = fmt.Sprintf("消息通知-审批信息JSON格式化失败, %v", e)
  457. return
  458. }
  459. extraContent = string(b)
  460. }
  461. for _, u := range userIds {
  462. m := new(models.ReportMessage)
  463. m.SendUserId = approveUser.AdminId
  464. m.ReceiveUserId = u
  465. m.Content = fmt.Sprintf("您提交的【%s】%s", v.Title, tips)
  466. m.ExtraContent = extraContent
  467. m.ReportType = utils.ReportTypeDefault
  468. m.ReportId = v.Id
  469. m.MessageType = messageType
  470. m.CreateTime = time.Now()
  471. m.ModifyTime = time.Now()
  472. reportMessages = append(reportMessages, m)
  473. }
  474. }
  475. }
  476. if len(updatePpts) > 0 {
  477. for _, v := range updatePpts {
  478. updateCols := []string{"State", "ModifyTime"}
  479. v.State = afterApproveMap[req.ApproveType]
  480. v.ModifyTime = time.Now()
  481. if v.State == models.ReportStatePass || v.State == models.ReportStateRefused {
  482. v.ApproveTime = time.Now()
  483. updateCols = append(updateCols, "ApproveTime")
  484. }
  485. if e := v.Update(updateCols); e != nil {
  486. br.Msg = "操作失败"
  487. br.ErrMsg = fmt.Sprintf("更新报告状态失败, %v", e)
  488. return
  489. }
  490. if req.ApproveType == 1 {
  491. // 审批通过,将PPT或研报加入报告库中
  492. go func() {
  493. msg, err := knowledge.ReportKnowledgeResourceAdd(v.Title, []string{v.PptxUrl}, v.AdminId, v.AdminRealName, "市场研究报告/内部报告/日常报告", "市场信息研究所", time.Now())
  494. if err != nil {
  495. utils.FileLog.Error(fmt.Sprintf("ReportKnowledgeResourceAdd-PPT知识资源报告库添加失败,pptId:%d,msg:%s,err: %v", v.PptId, msg, err))
  496. return
  497. }
  498. }()
  499. go func() {
  500. // 审批通过,将摘要加入知识资源库观点中
  501. v.Abstract = strings.TrimSpace(v.Abstract)
  502. if v.Abstract == "" {
  503. return
  504. }
  505. classifyId, err := knowledge.ReportKnowledgeResourceClassifyCheckAndSave(v.ClassifyId)
  506. if err != nil {
  507. utils.FileLog.Error(fmt.Sprintf("ReportKnowledgeResourceClassifyCheckAndSave-PPT分类保存失败,pptId:%d,classifyId:%d,err: %v", v.PptId, v.ClassifyId, err))
  508. return
  509. }
  510. msg, err := knowledge.ViewPointSave(0, v.AdminId, v.AdminRealName, v.Title, v.Abstract, "市场信息研究所", "", classifyId, &v.ApproveTime)
  511. if err != nil {
  512. utils.FileLog.Error(fmt.Sprintf("ViewPointSave-ppt摘要知识库添加失败,pptId:%d,msg:%s,err: %v", v.PptId, msg, err))
  513. return
  514. }
  515. }()
  516. }
  517. if req.ApproveType == 3 || req.ApproveType == 4 {
  518. // 撤回/提审暂不发消息
  519. continue
  520. }
  521. // 创建人和协作人
  522. userIds := make([]int, 0)
  523. userIds = append(userIds, v.AdminId)
  524. if v.CollaborateUsers != "" {
  525. partnerArr := strings.Split(v.CollaborateUsers, ",")
  526. for _, p := range partnerArr {
  527. uid, _ := strconv.Atoi(p)
  528. if uid > 0 && !utils.InArrayByInt(userIds, uid) {
  529. userIds = append(userIds, uid)
  530. }
  531. }
  532. }
  533. // 发送系统消息给创建人和协作人
  534. var messageType int
  535. var tips string
  536. var extraContent string
  537. if v.State == models.ReportStatePass {
  538. messageType = models.ReportMessageTypeApprovePass
  539. tips = "已通过审批!"
  540. }
  541. if v.State == models.ReportStateRefused {
  542. messageType = models.ReportMessageTypeApproveRefuse
  543. tips = "已被驳回,点击查看详情!"
  544. // 审批详情信息
  545. var extra models.ReportMessageApproveItem
  546. extra.Title = v.Title
  547. extra.ApproveType = 2
  548. extra.ApproveUserId = approveUser.AdminId
  549. extra.ApproveUserName = approveUser.RealName
  550. extra.ApproveRemark = req.Suggestion
  551. b, e := json.Marshal(extra)
  552. if e != nil {
  553. br.Msg = "操作失败"
  554. br.ErrMsg = fmt.Sprintf("消息通知-审批信息JSON格式化失败, %v", e)
  555. return
  556. }
  557. extraContent = string(b)
  558. }
  559. for _, u := range userIds {
  560. m := new(models.ReportMessage)
  561. m.SendUserId = approveUser.AdminId
  562. m.ReceiveUserId = u
  563. m.Content = fmt.Sprintf("您提交的【%s】%s", v.Title, tips)
  564. m.ExtraContent = extraContent
  565. m.ReportType = utils.ReportTypePPT
  566. m.ReportId = v.PptId
  567. m.MessageType = messageType
  568. m.CreateTime = time.Now()
  569. m.ModifyTime = time.Now()
  570. reportMessages = append(reportMessages, m)
  571. }
  572. }
  573. }
  574. // 消息通知
  575. if len(reportMessages) > 0 {
  576. messageOb := new(models.ReportMessage)
  577. if e := messageOb.CreateMulti(reportMessages); e != nil {
  578. br.Msg = "操作失败"
  579. br.ErrMsg = fmt.Sprintf("批量新增报告消息失败, %v", e)
  580. return
  581. }
  582. }
  583. br.Ret = 200
  584. br.Success = true
  585. br.Msg = "操作成功"
  586. }
  587. // ReportRemove
  588. // @Title 删除报告
  589. // @Description 删除报告接口
  590. // @Param request body models.ReportRemoveReq true "type json string"
  591. // @Success 200 string "操作成功"
  592. // @router /report/remove [post]
  593. func (this *ReportOpenController) ReportRemove() {
  594. br := new(models.BaseResponse).Init()
  595. defer func() {
  596. if br.ErrMsg == "" {
  597. br.IsSendEmail = false
  598. }
  599. this.Data["json"] = br
  600. this.ServeJSON()
  601. }()
  602. var req models.ReportRemoveReq
  603. if e := json.Unmarshal(this.Ctx.Input.RequestBody, &req); e != nil {
  604. br.Msg = "参数解析异常"
  605. br.ErrMsg = fmt.Sprintf("参数解析失败,%v", e)
  606. return
  607. }
  608. if len(req.ReportIds) == 0 {
  609. br.Msg = "报告ID有误"
  610. br.ErrMsg = fmt.Sprintf("智力共享报告ID有误, %v", req.ReportIds)
  611. return
  612. }
  613. // TODO:(可选优化)若报告的状态是已审批状态,那么把OutReportId置空,不做物理删除
  614. for _, v := range req.ReportIds {
  615. reportExist, pptExist := services.GetReportByOutReportId(v)
  616. // 不存在忽略即可
  617. if reportExist == nil && pptExist == nil {
  618. continue
  619. }
  620. // 研报类型
  621. if reportExist != nil && reportExist.Id > 0 {
  622. if e := reportExist.Remove(); e != nil {
  623. br.Msg = "操作失败"
  624. br.ErrMsg = fmt.Sprintf("删除报告失败, %v", e)
  625. return
  626. }
  627. }
  628. // PPT类型
  629. if pptExist != nil && pptExist.PptId > 0 {
  630. if e := pptExist.Remove(); e != nil {
  631. br.Msg = "操作失败"
  632. br.ErrMsg = fmt.Sprintf("删除PPT报告失败, %v", e)
  633. return
  634. }
  635. }
  636. }
  637. br.Ret = 200
  638. br.Success = true
  639. br.Msg = "操作成功"
  640. }
  641. // ViewPointSave
  642. // @Title 观点保存
  643. // @Description 观点保存
  644. // @Param request body models.ReportViewPointReq true "type json string"
  645. // @Success 200 string "操作成功"
  646. // @router /viewpoint/save [post]
  647. func (this *ReportOpenController) ViewPointSave() {
  648. br := new(models.BaseResponse).Init()
  649. defer func() {
  650. if br.ErrMsg == "" {
  651. br.IsSendEmail = false
  652. }
  653. this.Data["json"] = br
  654. this.ServeJSON()
  655. }()
  656. var req *models.ReportViewPointSaveReq
  657. if e := json.Unmarshal(this.Ctx.Input.RequestBody, &req); e != nil {
  658. br.Msg = "参数解析异常"
  659. br.ErrMsg = fmt.Sprintf("参数解析失败,%v", e)
  660. return
  661. }
  662. req.Content = strings.TrimSpace(req.Content)
  663. req.Title = strings.TrimSpace(req.Title)
  664. req.Category = strings.TrimSpace(req.Category)
  665. if req.Title == "" {
  666. br.Msg = "观点标题不能为空"
  667. return
  668. }
  669. if req.Content == "" {
  670. br.Msg = "观点内容不能为空"
  671. return
  672. }
  673. if req.Category == "" {
  674. br.Msg = "观点分类不能为空"
  675. return
  676. }
  677. if req.DataDate == "" {
  678. br.Msg = "观点日期不能为空"
  679. return
  680. }
  681. dataDate, err := time.ParseInLocation(utils.FormatDateTime, req.DataDate, time.Local)
  682. if err != nil {
  683. br.Msg = "观点日期格式不正确"
  684. return
  685. }
  686. msg, err := knowledge.ViewPointSave(req.Id, 0, "", req.Title, req.Content, req.DataSource, req.Category, 0, &dataDate)
  687. if err != nil {
  688. if msg == "" {
  689. msg = "保存观点失败"
  690. }
  691. br.Msg = msg
  692. br.ErrMsg = fmt.Sprintf("保存观点失败, %v", err)
  693. return
  694. }
  695. br.Msg = "保存观点成功"
  696. br.Success = true
  697. br.Ret = 200
  698. }
  699. // @Title 观点删除
  700. // @Description 观点删除
  701. // @Param request body models.ReportViewPointReq true "type json string"
  702. // @Success 200 string "操作成功"
  703. // @router /viewpoint/delete [post]
  704. func (this *ReportOpenController) ViewPointDelete() {
  705. br := new(models.BaseResponse).Init()
  706. defer func() {
  707. if br.ErrMsg == "" {
  708. br.IsSendEmail = false
  709. }
  710. this.Data["json"] = br
  711. this.ServeJSON()
  712. }()
  713. var req *models.ReportViewPointDeleteRep
  714. if e := json.Unmarshal(this.Ctx.Input.RequestBody, &req); e != nil {
  715. br.Msg = "参数解析异常"
  716. br.ErrMsg = fmt.Sprintf("参数解析失败,%v", e)
  717. return
  718. }
  719. if req.Id == nil {
  720. br.Msg = "观点ID不能为空"
  721. return
  722. }
  723. obj := new(models.KnowledgeResource)
  724. obj, err := obj.GetKnowledgeResourceByOutId(*req.Id)
  725. if err != nil {
  726. if utils.IsErrNoRow(err) {
  727. br.Msg = "观点不存在"
  728. return
  729. }
  730. br.Msg = "删除观点失败"
  731. br.ErrMsg = fmt.Sprintf("删除观点失败, %v", err)
  732. return
  733. }
  734. obj.IsDelete = 1
  735. if err = obj.Update([]string{"IsDelete"}); err != nil {
  736. br.Msg = "删除观点失败"
  737. br.ErrMsg = fmt.Sprintf("删除观点失败, %v", err)
  738. return
  739. }
  740. updateMap := map[string]interface{}{
  741. "IsDelete": "1",
  742. }
  743. err = knowledge.UpdateEsKnowledgeResource(obj.KnowledgeResourceId, updateMap)
  744. if err != nil {
  745. br.Msg = "删除观点失败"
  746. br.ErrMsg = fmt.Sprintf("es删除观点失败, %v", err)
  747. return
  748. }
  749. br.Msg = "删除观点成功"
  750. br.Success = true
  751. br.Ret = 200
  752. }
  753. // @Title 知识资源报告库上传
  754. // @Description 知识资源报告库上传
  755. // @Param request body string/int true/false "Description"
  756. // @Success 200 string "操作成功"
  757. // @router /report/knowledge/upload [post]
  758. func (this *ReportOpenController) ReportKnowledgeUpload() {
  759. br := new(models.BaseResponse).Init()
  760. defer func() {
  761. if br.ErrMsg == "" {
  762. br.IsSendEmail = false
  763. }
  764. this.Data["json"] = br
  765. this.ServeJSON()
  766. }()
  767. var req models.ReportKnowledgeUploadRep
  768. if e := this.BindForm(&req); e != nil {
  769. br.Msg = "参数解析异常"
  770. br.ErrMsg = fmt.Sprintf("参数解析失败,%v", e)
  771. return
  772. }
  773. pushTime, err := time.Parse(utils.FormatDateTime, req.PushTime)
  774. if err != nil {
  775. br.Msg = "发布时间格式不正确"
  776. return
  777. }
  778. fileUrls, msg, err := knowledge.UploadFormFile(this.Ctx.Request.MultipartForm.File)
  779. if err != nil {
  780. if msg == "" {
  781. msg = "上传失败"
  782. }
  783. br.Msg = msg
  784. br.ErrMsg = fmt.Sprintf("上传失败, %v", err)
  785. return
  786. }
  787. msg, err = knowledge.ReportKnowledgeResourceAdd(req.Title, fileUrls, 0, "", req.ClassifyPath, req.Source, pushTime)
  788. if err != nil {
  789. if msg == "" {
  790. msg = "保存失败"
  791. }
  792. br.Msg = msg
  793. br.ErrMsg = fmt.Sprintf("保存失败, %v", err)
  794. return
  795. }
  796. br.Ret = 200
  797. br.Msg = "上传成功"
  798. br.Success = true
  799. }