resource.go 31 KB

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