resource.go 33 KB

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