knowledge_resource.go 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875
  1. package controllers
  2. import (
  3. "encoding/json"
  4. "eta_gn/eta_api/models"
  5. "eta_gn/eta_api/models/knowledge"
  6. knowledgeServ "eta_gn/eta_api/services/knowledge"
  7. "eta_gn/eta_api/utils"
  8. "github.com/rdlucklib/rdluck_tools/paging"
  9. "github.com/tealeg/xlsx"
  10. "html"
  11. "os"
  12. "strconv"
  13. "strings"
  14. "time"
  15. )
  16. // 分类
  17. type KnowledgeResourceController struct {
  18. BaseAuthController
  19. }
  20. // ListReport
  21. // @Title 获取事件列表接口
  22. // @Description 获取事件列表
  23. // @Param PageSize query int true "每页数据条数"
  24. // @Param CurrentIndex query int true "当前页页码,从1开始"
  25. // @Param TimeType query string true "筛选的时间类别:publish_time(发布时间),modify_time(更新时间);approve_time(审批时间)"
  26. // @Param StartDate query string true "开始时间"
  27. // @Param EndDate query string true "结束时间"
  28. // @Param Frequency query string true "频度"
  29. // @Param ClassifyIdFirst query int true "一级分类id"
  30. // @Param ClassifyIdSecond query int true "二级分类id"
  31. // @Param ClassifyIdThird query int true "三级分类id"
  32. // @Param State query int true "状态"
  33. // @Param KeyWord query string true "搜索关键词"
  34. // @Param PublishSort query string true "desc:降序,asc 升序(预留)"
  35. // @Param FilterReportType query string true "筛选事件类型,1:公共研报,2:共享研报,3:我的研报"
  36. // @Success 200 {object} knowledge.KnowledgeResourceListResp
  37. // @router /resource/list [get]
  38. func (this *KnowledgeResourceController) List() {
  39. br := new(models.BaseResponse).Init()
  40. defer func() {
  41. this.Data["json"] = br
  42. this.ServeJSON()
  43. }()
  44. pageSize, _ := this.GetInt("PageSize")
  45. currentIndex, _ := this.GetInt("CurrentIndex")
  46. classifyIds := this.GetString("ClassifyIds")
  47. // 添加人
  48. addUserIds, _ := this.GetInt("SysUserIds")
  49. // 标签
  50. tagIds := this.GetString("TagIds")
  51. keyWord := this.GetString("Keyword")
  52. var startSize int
  53. if pageSize <= 0 {
  54. pageSize = utils.PageSize20
  55. }
  56. if currentIndex <= 0 {
  57. currentIndex = 1
  58. }
  59. startSize = utils.StartIndex(currentIndex, pageSize)
  60. var condition string
  61. var pars []interface{}
  62. if keyWord != "" {
  63. condition += ` AND (title LIKE ?) `
  64. pars = utils.GetLikeKeywordPars(pars, keyWord, 2)
  65. }
  66. if classifyIds != "" {
  67. //转成数组,并把类型转成int
  68. classifyIdsArr := strings.Split(classifyIds, ",")
  69. var classifyIdsInt []int
  70. for _, v := range classifyIdsArr {
  71. classifyId, _ := strconv.Atoi(v)
  72. classifyIdsInt = append(classifyIdsInt, classifyId)
  73. }
  74. condition += ` AND classify_id in (?) `
  75. pars = append(pars, classifyIdsInt)
  76. }
  77. if addUserIds > 0 {
  78. // 转成数组,并把类型转成int
  79. addUserIdsArr := strings.Split(strconv.Itoa(addUserIds), ",")
  80. var addUserIdsInt []int
  81. for _, v := range addUserIdsArr {
  82. addUserId, _ := strconv.Atoi(v)
  83. addUserIdsInt = append(addUserIdsInt, addUserId)
  84. }
  85. condition += ` AND admin_id in (?) `
  86. pars = append(pars, addUserIdsInt)
  87. }
  88. if tagIds != "" {
  89. //转成数组,并把类型转成int
  90. tagIdsArr := strings.Split(tagIds, ",")
  91. var tagIdsInt []int
  92. for _, v := range tagIdsArr {
  93. tagId, _ := strconv.Atoi(v)
  94. tagIdsInt = append(tagIdsInt, tagId)
  95. }
  96. condition += ` AND tag_id in (?) `
  97. pars = append(pars, tagIdsInt)
  98. }
  99. var err error
  100. var total int
  101. var list []*knowledge.KnowledgeResourceList
  102. // 共享事件需要连表查询,所以需要单独写
  103. total, err = knowledge.GetKnowledgeResourceListCount(condition, pars)
  104. if err != nil {
  105. br.Msg = "获取失败"
  106. br.ErrMsg = "获取失败,Err:" + err.Error()
  107. return
  108. }
  109. listTmp, err := knowledge.GetKnowledgeResourceList(condition, pars, startSize, pageSize)
  110. if err != nil {
  111. br.Msg = "获取失败"
  112. br.ErrMsg = "获取失败,Err:" + err.Error()
  113. return
  114. }
  115. // 整理分类ID
  116. classifyFullNameMap := make(map[int]string)
  117. classifyIdsArr := make([]int, 0)
  118. for _, v := range listTmp {
  119. classifyIdsArr = append(classifyIdsArr, v.ClassifyId)
  120. }
  121. // 获取分类列表
  122. classifyList, err := knowledge.GetFullClassifyListByIdList(classifyIdsArr)
  123. if err != nil {
  124. br.Msg = "获取失败"
  125. br.ErrMsg = "获取失败,Err:" + err.Error()
  126. return
  127. }
  128. for _, v := range classifyList {
  129. name := v.ClassifyName
  130. if v.ParentName != "" {
  131. name = v.ParentName + "/" + name
  132. }
  133. if v.RootName != "" {
  134. name = v.RootName + "/" + name
  135. }
  136. classifyFullNameMap[v.ClassifyId] = name
  137. }
  138. // 整理标签ID
  139. tagNameMap := make(map[int]string)
  140. tagIdsArr := make([]int, 0)
  141. for _, v := range listTmp {
  142. tagIdsArr = append(tagIdsArr, v.TagId)
  143. }
  144. // 获取标签列表
  145. tagObj := new(knowledge.KnowledgeTag)
  146. tagList, err := tagObj.GetListByIdList(tagIdsArr)
  147. if err != nil {
  148. br.Msg = "获取失败"
  149. br.ErrMsg = "获取失败,Err:" + err.Error()
  150. }
  151. for _, v := range tagList {
  152. tagNameMap[v.TagId] = v.TagName
  153. }
  154. for _, v := range listTmp {
  155. var startTime, endTime string
  156. if v.StartTime != nil && !v.StartTime.IsZero() {
  157. startTime = v.StartTime.In(time.Local).Format(utils.FormatDateTime)
  158. }
  159. if v.EndTime != nil && !v.EndTime.IsZero() {
  160. endTime = v.EndTime.In(time.Local).Format(utils.FormatDateTime)
  161. }
  162. modifyTime := v.ModifyTime.In(time.Local).Format(utils.FormatDateTime)
  163. createTime := v.CreateTime.In(time.Local).Format(utils.FormatDateTime)
  164. tmp := &knowledge.KnowledgeResourceList{
  165. KnowledgeResourceId: v.KnowledgeResourceId,
  166. ResourceType: v.ResourceType,
  167. ClassifyId: v.ClassifyId,
  168. Title: v.Title,
  169. CreateTime: createTime,
  170. ModifyTime: modifyTime,
  171. State: v.State,
  172. ResourceCode: v.ResourceCode,
  173. AdminId: v.AdminId,
  174. AdminRealName: v.AdminRealName,
  175. SourceFrom: v.SourceFrom,
  176. TagId: v.TagId,
  177. StartTime: startTime,
  178. EndTime: endTime,
  179. }
  180. // todo 编辑状态
  181. /*markStatus, err := services.UpdateReportEditMark(item.Id, 0, this.SysUser.AdminId, 2, this.SysUser.RealName, this.Lang)
  182. if err != nil {
  183. br.Msg = "查询标记状态失败"
  184. br.ErrMsg = "查询标记状态失败,Err:" + err.Error()
  185. return
  186. }
  187. if markStatus.Status == 0 {
  188. item.CanEdit = true
  189. } else {
  190. item.Editor = markStatus.Editor
  191. }*/
  192. classifyName, ok := classifyFullNameMap[tmp.ClassifyId]
  193. if ok {
  194. tmp.ClassifyFullName = classifyName
  195. }
  196. tagName, ok := tagNameMap[v.TagId]
  197. if ok {
  198. tmp.TagName = tagName
  199. }
  200. list = append(list, tmp)
  201. }
  202. page := paging.GetPaging(currentIndex, pageSize, total)
  203. resp := new(knowledge.KnowledgeResourceListResp)
  204. resp.Paging = page
  205. resp.List = list
  206. br.Ret = 200
  207. br.Success = true
  208. br.Msg = "获取成功"
  209. br.Data = resp
  210. }
  211. // Add
  212. // @Title 新增事件接口
  213. // @Description 新增事件(区分事件类型)
  214. // @Param request body knowledge.AddReq true "type json string"
  215. // @Success 200 {object} knowledge.AddResp
  216. // @router /resource/add [post]
  217. func (this *KnowledgeResourceController) Add() {
  218. br := new(models.BaseResponse).Init()
  219. defer func() {
  220. this.Data["json"] = br
  221. this.ServeJSON()
  222. }()
  223. sysUser := this.SysUser
  224. if sysUser == nil {
  225. br.Msg = "请登录"
  226. br.ErrMsg = "请登录,SysUser Is Empty"
  227. br.Ret = 408
  228. return
  229. }
  230. var req knowledge.AddReq
  231. err := json.Unmarshal(this.Ctx.Input.RequestBody, &req)
  232. if err != nil {
  233. br.Msg = "参数解析异常!"
  234. br.ErrMsg = "参数解析失败,Err:" + err.Error()
  235. return
  236. }
  237. if req.Title == `` {
  238. br.Msg = "标题不能为空"
  239. br.ErrMsg = "标题不能为空"
  240. br.IsSendEmail = false
  241. return
  242. }
  243. if req.ClassifyId <= 0 {
  244. br.Msg = "分类必填"
  245. br.ErrMsg = "分类必填"
  246. br.IsSendEmail = false
  247. return
  248. }
  249. if req.ResourceType == knowledge.KnowledgeResourceTypeEvent || req.ResourceType == knowledge.KnowledgeResourceTypePolicy {
  250. if req.StartTime == "" {
  251. br.Msg = "开始时间必填"
  252. br.ErrMsg = "开始时间必填"
  253. return
  254. }
  255. }
  256. item, err, errMsg := knowledgeServ.AddResource(&req, sysUser)
  257. if err != nil {
  258. br.Msg = "保存失败"
  259. if errMsg != "" {
  260. br.Msg = errMsg
  261. }
  262. br.ErrMsg = "保存失败,Err:" + err.Error()
  263. return
  264. }
  265. resp := new(knowledge.AddResp)
  266. resp.KnowledgeResourceId = item.KnowledgeResourceId
  267. resp.ResourceCode = item.ResourceCode
  268. br.Ret = 200
  269. br.Success = true
  270. br.Msg = "保存成功"
  271. br.Data = resp
  272. }
  273. // Edit
  274. // @Title 编辑事件基础信息接口
  275. // @Description 编辑事件基础信息(不区分事件类型)
  276. // @Param request body knowledge.EditReq true "type json string"
  277. // @Success 200 {object} knowledge.EditResp
  278. // @router /resource/edit [post]
  279. func (this *KnowledgeResourceController) Edit() {
  280. br := new(models.BaseResponse).Init()
  281. defer func() {
  282. this.Data["json"] = br
  283. this.ServeJSON()
  284. }()
  285. sysUser := this.SysUser
  286. if sysUser == nil {
  287. br.Msg = "请登录"
  288. br.ErrMsg = "请登录,SysUser Is Empty"
  289. br.Ret = 408
  290. return
  291. }
  292. var req knowledge.EditReq
  293. err := json.Unmarshal(this.Ctx.Input.RequestBody, &req)
  294. if err != nil {
  295. br.Msg = "参数解析异常!"
  296. br.ErrMsg = "参数解析失败,Err:" + err.Error()
  297. return
  298. }
  299. if req.KnowledgeResourceId <= 0 {
  300. br.Msg = "请选择事件"
  301. br.ErrMsg = "请选择事件"
  302. return
  303. }
  304. if req.Title == `` {
  305. br.Msg = "标题不能为空"
  306. br.ErrMsg = "标题不能为空"
  307. br.IsSendEmail = false
  308. return
  309. }
  310. if req.ClassifyId <= 0 {
  311. br.Msg = "分类必填"
  312. br.ErrMsg = "分类必填"
  313. br.IsSendEmail = false
  314. return
  315. }
  316. if req.ResourceType == knowledge.KnowledgeResourceTypeEvent || req.ResourceType == knowledge.KnowledgeResourceTypePolicy {
  317. if req.StartTime == "" {
  318. br.Msg = "开始时间必填"
  319. br.ErrMsg = "开始时间必填"
  320. return
  321. }
  322. }
  323. // todo 更新标记key
  324. /*markStatus, err := services.UpdateReportEditMark(int(req.KnowledgeResourceId), 0, sysUser.AdminId, 1, sysUser.RealName, this.Lang)
  325. if err != nil {
  326. br.Msg = err.Error()
  327. return
  328. }
  329. if markStatus.Status == 1 {
  330. br.Msg = markStatus.Msg
  331. //br.Ret = 202 //202 服务器已接受请求,但尚未处理。
  332. return
  333. }*/
  334. resourceObj := new(knowledge.KnowledgeResource)
  335. resourceInfo, e := resourceObj.GetById(int(req.KnowledgeResourceId))
  336. if e != nil {
  337. if utils.IsErrNoRow(e) {
  338. br.Msg = "事件已被删除, 请刷新页面"
  339. return
  340. }
  341. br.Msg = "操作失败"
  342. br.ErrMsg = "获取事件失败, Err: " + e.Error()
  343. return
  344. }
  345. // 编辑事件信息
  346. err, errMsg := knowledgeServ.EditResource(resourceInfo, req, sysUser)
  347. if err != nil {
  348. br.Msg = "保存失败"
  349. if errMsg != "" {
  350. br.Msg = errMsg
  351. }
  352. br.ErrMsg = "保存失败,Err:" + err.Error()
  353. return
  354. }
  355. resp := new(knowledge.EditResp)
  356. resp.KnowledgeResourceId = req.KnowledgeResourceId
  357. resp.ResourceCode = resourceInfo.ResourceCode
  358. br.Ret = 200
  359. br.Success = true
  360. br.Msg = "保存成功"
  361. br.Data = resp
  362. }
  363. // Detail
  364. // @Title 获取事件详情接口
  365. // @Description 获取事件详情
  366. // @Param request body knowledge.KnowledgeResourceDetailReq true "type json string"
  367. // @Success 200 {object} knowledge.KnowledgeResource
  368. // @router /resource/detail [get]
  369. func (this *KnowledgeResourceController) Detail() {
  370. br := new(models.BaseResponse).Init()
  371. defer func() {
  372. this.Data["json"] = br
  373. this.ServeJSON()
  374. }()
  375. /*var req knowledge.KnowledgeResourceDetailReq
  376. err := json.Unmarshal(this.Ctx.Input.RequestBody, &req)
  377. if err != nil {
  378. br.Msg = "参数解析异常!"
  379. br.ErrMsg = "参数解析失败,Err:" + err.Error()
  380. return
  381. }
  382. if req.KnowledgeResourceId <= 0 {
  383. br.Msg = "参数错误"
  384. return
  385. }*/
  386. knowledgeResourceId, err := this.GetInt("KnowledgeResourceId")
  387. if err != nil {
  388. br.Msg = "获取参数失败!"
  389. br.ErrMsg = "获取参数失败,Err:" + err.Error()
  390. return
  391. }
  392. if knowledgeResourceId <= 0 {
  393. br.Msg = "参数错误"
  394. return
  395. }
  396. resourceObj := new(knowledge.KnowledgeResource)
  397. item, err := resourceObj.GetById(knowledgeResourceId)
  398. if err != nil {
  399. if utils.IsErrNoRow(err) {
  400. br.Msg = "事件已被删除"
  401. return
  402. }
  403. br.Msg = "获取失败"
  404. br.ErrMsg = "获取失败,Err:" + err.Error()
  405. return
  406. }
  407. item.Content = html.UnescapeString(item.Content)
  408. resp := &knowledge.KnowledgeResourceDetailView{
  409. KnowledgeResource: item,
  410. }
  411. br.Ret = 200
  412. br.Success = true
  413. br.Msg = "获取成功"
  414. br.Data = resp
  415. }
  416. // SaveReportContent
  417. // @Title 保存草稿
  418. // @Description 保存草稿
  419. // @Param request body knowledge.SaveReportContent true "type json string"
  420. // @Success 200 {object} knowledge.KnowledgeResourceAuthorResp
  421. // @router /resource/saveContent [post]
  422. func (this *KnowledgeResourceController) SaveReportContent() {
  423. br := new(models.BaseResponse).Init()
  424. br.IsSendEmail = false
  425. defer func() {
  426. this.Data["json"] = br
  427. this.ServeJSON()
  428. }()
  429. sysUser := this.SysUser
  430. if sysUser == nil {
  431. br.Msg = "请登录"
  432. br.ErrMsg = "请登录,SysUser Is Empty"
  433. br.Ret = 408
  434. return
  435. }
  436. var req knowledge.SaveReportContent
  437. err := json.Unmarshal(this.Ctx.Input.RequestBody, &req)
  438. if err != nil {
  439. br.Msg = "参数解析异常!"
  440. br.ErrMsg = "参数解析失败,Err:" + err.Error()
  441. return
  442. }
  443. knowledgeResourceId := req.KnowledgeResourceId
  444. //noChangeFlag := req.NoChange
  445. if knowledgeResourceId <= 0 {
  446. resp := new(knowledge.SaveReportContentResp)
  447. resp.KnowledgeResourceId = knowledgeResourceId
  448. br.Ret = 200
  449. br.Success = true
  450. br.Msg = "保存成功"
  451. br.Data = resp
  452. return
  453. }
  454. // 获取事件详情
  455. /*resourceInfo, _ := knowledge.GetReportByknowledgeResourceId(req.KnowledgeResourceId)
  456. if resourceInfo != nil && resourceInfo.State == 2 {
  457. br.Msg = "该事件已发布,不允许编辑"
  458. br.ErrMsg = "该事件已发布,不允许编辑"
  459. br.IsSendEmail = false
  460. return
  461. }
  462. // 标记更新中
  463. {
  464. markStatus, err := services.UpdateReportEditMark(req.KnowledgeResourceId, 0, sysUser.AdminId, 1, sysUser.RealName, this.Lang)
  465. if err != nil {
  466. br.Msg = err.Error()
  467. return
  468. }
  469. if markStatus.Status == 1 {
  470. br.Msg = markStatus.Msg
  471. return
  472. }
  473. }
  474. // 内容有过修改的话,那么逻辑处理
  475. if noChangeFlag != 1 {
  476. content := req.Content
  477. if content == "" {
  478. content = this.GetString("Content")
  479. }
  480. if content != "" {
  481. e := utils.ContentXssCheck(req.Content)
  482. if e != nil {
  483. br.Msg = "存在非法标签"
  484. br.ErrMsg = "存在非法标签, Err: " + e.Error()
  485. return
  486. }
  487. contentClean, e := services.FilterReportContentBr(req.Content)
  488. if e != nil {
  489. br.Msg = "内容去除前后空格失败"
  490. br.ErrMsg = "内容去除前后空格失败, Err: " + e.Error()
  491. return
  492. }
  493. content = contentClean
  494. contentSub, err := services.GetReportContentSub(content)
  495. if err != nil {
  496. go alarm_msg.SendAlarmMsg("解析 ContentSub 失败,Err:"+err.Error(), 3)
  497. //utils.SendEmail(utils.APPNAME+"失败提醒", "解析 ContentSub 失败,Err:"+err.Error(), utils.EmailSendToUsers)
  498. }
  499. resourceInfo.Content = html.EscapeString(content)
  500. resourceInfo.ContentSub = html.EscapeString(contentSub)
  501. resourceInfo.ContentStruct = html.EscapeString(req.ContentStruct)
  502. resourceInfo.HeadImg = req.HeadImg
  503. resourceInfo.EndImg = req.EndImg
  504. resourceInfo.CanvasColor = req.CanvasColor
  505. resourceInfo.HeadResourceId = req.HeadResourceId
  506. resourceInfo.EndResourceId = req.EndResourceId
  507. resourceInfo.ModifyTime = time.Now()
  508. resourceInfo.ContentModifyTime = time.Now()
  509. updateCols := []string{"Content", "ContentSub", "ContentStruct", "HeadImg", "EndImg", "CanvasColor", "HeadResourceId", "EndResourceId", "ModifyTime", "ContentModifyTime"}
  510. err = resourceInfo.UpdateReport(updateCols)
  511. if err != nil {
  512. br.Msg = "保存失败"
  513. br.ErrMsg = "保存失败,Err:" + err.Error()
  514. return
  515. }
  516. go knowledge.AddReportSaveLog(knowledgeResourceId, this.SysUser.AdminId, resourceInfo.Content, resourceInfo.ContentSub, resourceInfo.ContentStruct, resourceInfo.CanvasColor, this.SysUser.AdminName, resourceInfo.HeadResourceId, resourceInfo.EndResourceId)
  517. }
  518. }
  519. */
  520. resp := new(knowledge.SaveReportContentResp)
  521. resp.KnowledgeResourceId = knowledgeResourceId
  522. br.Ret = 200
  523. br.Success = true
  524. br.Msg = "保存成功"
  525. br.Data = resp
  526. }
  527. // BaseDetail
  528. // @Title 获取事件基础信息详情接口
  529. // @Description 获取事件基础信息详情接口
  530. // @Param request body knowledge.KnowledgeResourceDetailReq true "type json string"
  531. // @Success 200 {object} knowledge.KnowledgeResource
  532. // @router /resource/detail/base [get]
  533. func (this *KnowledgeResourceController) BaseDetail() {
  534. br := new(models.BaseResponse).Init()
  535. defer func() {
  536. this.Data["json"] = br
  537. this.ServeJSON()
  538. }()
  539. /*var req knowledge.KnowledgeResourceDetailReq
  540. err := json.Unmarshal(this.Ctx.Input.RequestBody, &req)
  541. if err != nil {
  542. br.Msg = "参数解析异常!"
  543. br.ErrMsg = "参数解析失败,Err:" + err.Error()
  544. return
  545. }
  546. if req.KnowledgeResourceId <= 0 {
  547. br.Msg = "参数错误"
  548. return
  549. }*/
  550. /*knowledgeResourceId, err := this.GetInt("KnowledgeResourceId")
  551. if err != nil {
  552. br.Msg = "获取参数失败!"
  553. br.ErrMsg = "获取参数失败,Err:" + err.Error()
  554. return
  555. }
  556. if knowledgeResourceId <= 0 {
  557. br.Msg = "参数错误"
  558. return
  559. }
  560. resourceInfo, err := knowledge.GetReportById(knowledgeResourceId)
  561. if err != nil {
  562. if utils.IsErrNoRow(err) {
  563. br.Msg = "事件已被删除"
  564. return
  565. }
  566. br.Msg = "获取失败"
  567. br.ErrMsg = "获取失败,Err:" + err.Error()
  568. return
  569. }
  570. // 基础信息就不获取章节信息了
  571. chapterList := make([]*knowledge.KnowledgeResourceChapter, 0)
  572. resourceInfo.Content = html.UnescapeString(resourceInfo.Content)
  573. resourceInfo.ContentSub = html.UnescapeString(resourceInfo.ContentSub)
  574. grandAdminList := make([]knowledge.KnowledgeResourceDetailViewAdmin, 0)
  575. permissionList := make([]knowledge.KnowledgeResourceDetailViewPermission, 0)
  576. // 处理事件授权用户列表
  577. {
  578. obj := report.ReportGrant{}
  579. grantList, tmpErr := obj.GetGrantListById(knowledgeResourceId)
  580. if tmpErr != nil {
  581. br.Msg = "获取章节id授权用户列表失败"
  582. br.ErrMsg = "获取章节id授权用户列表失败, Err: " + tmpErr.Error()
  583. return
  584. }
  585. if len(grantList) > 0 {
  586. grandAdminIdList := make([]int, 0)
  587. for _, v := range grantList {
  588. grandAdminIdList = append(grandAdminIdList, v.AdminId)
  589. }
  590. adminList, tmpErr := system.GetAdminListByIdList(grandAdminIdList)
  591. if tmpErr != nil {
  592. br.Msg = "获取章节id授权用户列表失败"
  593. br.ErrMsg = "获取章节id授权用户列表失败, Err: " + tmpErr.Error()
  594. return
  595. }
  596. for _, v := range adminList {
  597. grandAdminList = append(grandAdminList, knowledge.KnowledgeResourceDetailViewAdmin{
  598. AdminId: v.AdminId,
  599. AdminName: v.RealName,
  600. })
  601. }
  602. }
  603. }
  604. // 处理章节id关联品种id列表
  605. {
  606. minClassifyId := resourceInfo.ClassifyIdThird
  607. if minClassifyId <= 0 {
  608. minClassifyId = resourceInfo.ClassifyIdSecond
  609. }
  610. if minClassifyId <= 0 {
  611. minClassifyId = resourceInfo.ClassifyIdFirst
  612. }
  613. if minClassifyId <= 0 {
  614. br.Msg = "分类异常"
  615. br.ErrMsg = "分类异常"
  616. return
  617. }
  618. // 获取分类关联的品种id
  619. classifyPermissionList, tmpErr := knowledge.GetPermission(minClassifyId)
  620. if tmpErr != nil {
  621. br.Msg = "获取分类信息失败"
  622. br.ErrMsg = "获取失败,Err:" + tmpErr.Error()
  623. return
  624. }
  625. if len(classifyPermissionList) > 0 {
  626. permissionIdList := make([]int, 0)
  627. for _, v := range classifyPermissionList {
  628. permissionIdList = append(permissionIdList, v.ChartPermissionId)
  629. }
  630. adminList, tmpErr := knowledge.GetChartPermissionByIdList(permissionIdList)
  631. if tmpErr != nil {
  632. br.Msg = "获取章节id授权用户列表失败"
  633. br.ErrMsg = "获取章节id授权用户列表失败, Err: " + tmpErr.Error()
  634. return
  635. }
  636. for _, v := range adminList {
  637. permissionList = append(permissionList, knowledge.KnowledgeResourceDetailViewPermission{
  638. PermissionId: v.ChartPermissionId,
  639. PermissionName: v.PermissionName,
  640. })
  641. }
  642. }
  643. }
  644. resp := &knowledge.KnowledgeResourceDetailView{
  645. ReportDetail: resourceInfo,
  646. ChapterList: chapterList,
  647. GrandAdminList: grandAdminList,
  648. PermissionList: permissionList,
  649. }
  650. br.Ret = 200
  651. br.Success = true
  652. br.Msg = "获取成功"
  653. br.Data = resp*/
  654. }
  655. // Delete
  656. // @Title 删除事件接口
  657. // @Description 删除事件
  658. // @Param request body knowledge.DeleteReq true "type json string"
  659. // @Success 200 Ret=200 删除成功
  660. // @router /resource/delete [post]
  661. func (this *KnowledgeResourceController) Delete() {
  662. br := new(models.BaseResponse).Init()
  663. defer func() {
  664. this.Data["json"] = br
  665. this.ServeJSON()
  666. }()
  667. var req knowledge.DeleteReq
  668. err := json.Unmarshal(this.Ctx.Input.RequestBody, &req)
  669. if err != nil {
  670. br.Msg = "参数解析异常!"
  671. br.ErrMsg = "参数解析失败,Err:" + err.Error()
  672. return
  673. }
  674. if req.KnowledgeResourceId <= 0 {
  675. br.Msg = "参数错误"
  676. br.ErrMsg = "参数错误,事件id不可为空"
  677. return
  678. }
  679. resourceObj := new(knowledge.KnowledgeResource)
  680. if err = resourceObj.Delete(req.KnowledgeResourceId); err != nil {
  681. br.Msg = "删除失败"
  682. br.ErrMsg = "删除事件失败, Err: " + err.Error()
  683. return
  684. }
  685. br.Ret = 200
  686. br.Success = true
  687. br.Msg = "删除成功"
  688. }
  689. // ImportData
  690. // @Title Excel导入事件
  691. // @Description Excel导入事件
  692. // @Param EntryFile query file true "文件"
  693. // @Success 200 Ret=200 录入成功
  694. // @router /resource/import_add [post]
  695. func (c *KnowledgeResourceController) ImportData() {
  696. br := new(models.BaseResponse).Init()
  697. defer func() {
  698. c.Data["json"] = br
  699. c.ServeJSON()
  700. }()
  701. sysUser := c.SysUser
  702. if sysUser == nil {
  703. br.Msg = "请重新登录"
  704. return
  705. }
  706. resourceType, _ := c.GetInt("ResourceType")
  707. file, _, err := c.GetFile("EntryFile")
  708. if err != nil {
  709. br.Msg = "获取文件失败"
  710. br.ErrMsg = "获取文件失败,Err:" + err.Error()
  711. return
  712. }
  713. path := "./static/知识资源导入_" + strconv.Itoa(resourceType) + "_" + time.Now().Format(utils.FormatDateTimeUnSpace) + ".xlsx"
  714. defer file.Close()
  715. err = c.SaveToFile("EntryFile", path)
  716. if err != nil {
  717. br.Msg = "文件保存失败"
  718. br.ErrMsg = "文件保存失败,Err:" + err.Error()
  719. return
  720. }
  721. if utils.RunMode == "debug" {
  722. defer os.Remove(path)
  723. }
  724. successCount, failCount, err, errMsg := knowledgeServ.ImportResourceData(path, resourceType, sysUser)
  725. if err != nil {
  726. br.Msg = errMsg
  727. br.ErrMsg = err.Error()
  728. return
  729. }
  730. resp := models.EdbdataImportResp{
  731. SuccessCount: successCount,
  732. FailCount: failCount,
  733. }
  734. if failCount > 0 {
  735. if successCount == 0 {
  736. resp.Status = -1
  737. resp.Msg = "导入失败"
  738. } else {
  739. resp.Status = 1
  740. resp.Msg = "存在部分导入失败"
  741. }
  742. } else {
  743. resp.Status = 0
  744. }
  745. br.Msg = "导入成功"
  746. br.Ret = 200
  747. br.Success = true
  748. br.Data = resp
  749. }
  750. // ImportFailListDownload
  751. // @Title 下载失败列表
  752. // @Description 下载失败列表
  753. // @Success 200 {object} models.EdbdataClassifyResp
  754. // @router /resource/download_fail [get]
  755. func (this *KnowledgeResourceController) ImportFailListDownload() {
  756. br := new(models.BaseResponse).Init()
  757. defer func() {
  758. this.Data["json"] = br
  759. this.ServeJSON()
  760. }()
  761. sysUser := this.SysUser
  762. if sysUser == nil {
  763. br.Msg = "请重新登录"
  764. return
  765. }
  766. resourceType, _ := this.GetInt("ResourceType")
  767. failObj := new(knowledge.KnowledgeImportFail)
  768. item, err := failObj.GetListBySysUserId(sysUser.AdminId, resourceType)
  769. if err != nil {
  770. br.Msg = "获取数据失败"
  771. br.Msg = "获取数据失败,Err:" + err.Error()
  772. return
  773. }
  774. xlsxFile := xlsx.NewFile()
  775. SheetName := "导入失败数据"
  776. if this.Lang == utils.EnLangVersion {
  777. SheetName = "Import Fail List"
  778. }
  779. sheet, err := xlsxFile.AddSheet(SheetName)
  780. headRow := sheet.AddRow()
  781. headRow.AddCell().SetValue("开始日期")
  782. headRow.AddCell().SetValue("开始时间")
  783. headRow.AddCell().SetValue("结束日期")
  784. headRow.AddCell().SetValue("结束时间")
  785. headRow.AddCell().SetValue("标题")
  786. headRow.AddCell().SetValue("正文")
  787. headRow.AddCell().SetValue("来源")
  788. headRow.AddCell().SetValue("一级分类")
  789. headRow.AddCell().SetValue("二级分类")
  790. headRow.AddCell().SetValue("三级分类")
  791. headRow.AddCell().SetValue("标签")
  792. headRow.AddCell().SetValue("备注")
  793. for _, v := range item {
  794. row := sheet.AddRow()
  795. row.AddCell().SetValue(v.StartDate)
  796. row.AddCell().SetValue(v.StartTime)
  797. row.AddCell().SetValue(v.EndDate)
  798. row.AddCell().SetValue(v.EndTime)
  799. row.AddCell().SetValue(v.Title)
  800. row.AddCell().SetValue(v.Content)
  801. row.AddCell().SetValue(v.SourceFrom)
  802. row.AddCell().SetValue(v.ClassifyFirst)
  803. row.AddCell().SetValue(v.ClassifySecond)
  804. row.AddCell().SetValue(v.ClassifyThird)
  805. row.AddCell().SetValue(v.Tag)
  806. row.AddCell().SetValue(v.Remark)
  807. }
  808. fileName := time.Now().Format(utils.FormatDateTimeUnSpace) + ".xlsx"
  809. savePath := "./static/" + fileName
  810. err = xlsxFile.Save(savePath)
  811. if err != nil {
  812. br.Msg = "文件保存失败"
  813. br.ErrMsg = "文件保存失败,Err:" + err.Error()
  814. return
  815. }
  816. defer func() {
  817. os.Remove(savePath)
  818. }()
  819. finalFileName := "失败列表.xlsx"
  820. if this.Lang == utils.EnLangVersion {
  821. finalFileName = "Failure List.xlsx"
  822. }
  823. this.Ctx.Output.Download(savePath, finalFileName)
  824. }