resource.go 31 KB

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